| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * Common functions for the DVDrip C utilities. |
| 4 | * |
| 5 | * (c) 2022 Mark Wooding |
| 6 | */ |
| 7 | |
| 8 | /*----- Licensing notice --------------------------------------------------* |
| 9 | * |
| 10 | * This file is part of the DVD ripping toolset. |
| 11 | * |
| 12 | * DVDrip is free software: you can redistribute it and/or modify it |
| 13 | * under the terms of the GNU General Public License as published by the |
| 14 | * Free Software Foundation; either version 3 of the License, or (at your |
| 15 | * option) any later version. |
| 16 | * |
| 17 | * DVDrip is distributed in the hope that it will be useful, but WITHOUT |
| 18 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 19 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 20 | * for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License |
| 23 | * along with DVDrip. If not, see <https://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
| 26 | /*----- Header files ------------------------------------------------------*/ |
| 27 | |
| 28 | #include "lib.h" |
| 29 | |
| 30 | /*----- Diagnostics -------------------------------------------------------*/ |
| 31 | |
| 32 | const char *prog = "<unset>"; |
| 33 | |
| 34 | void set_prog(const char *p) |
| 35 | { const char *q = strrchr(p, '/'); prog = q ? q + 1 : p; } |
| 36 | |
| 37 | void vmoan(const char *fmt, va_list ap) |
| 38 | { vmoan_syserr(0, fmt, ap); } |
| 39 | |
| 40 | void vmoan_syserr(int err, const char *fmt, va_list ap) |
| 41 | { |
| 42 | fprintf(stderr, "%s: ", prog); |
| 43 | vfprintf(stderr, fmt, ap); |
| 44 | if (err) fprintf(stderr, ": %s", strerror(errno)); |
| 45 | fputc('\n', stderr); |
| 46 | } |
| 47 | |
| 48 | void moan(const char *fmt, ...) |
| 49 | { va_list ap; va_start(ap, fmt); vmoan(fmt, ap); va_end(ap); } |
| 50 | |
| 51 | void moan_syserr(int err, const char *fmt, ...) |
| 52 | { va_list ap; va_start(ap, fmt); vmoan_syserr(err, fmt, ap); va_end(ap); } |
| 53 | |
| 54 | void bail(const char *fmt, ...) |
| 55 | { va_list ap; va_start(ap, fmt); vmoan(fmt, ap); va_end(ap); exit(2); } |
| 56 | |
| 57 | void bail_syserr(int err, const char *fmt, ...) |
| 58 | { |
| 59 | va_list ap; |
| 60 | |
| 61 | va_start(ap, fmt); vmoan_syserr(err, fmt, ap); va_end(ap); |
| 62 | exit(2); |
| 63 | } |
| 64 | |
| 65 | /*----- Parsing utilities -------------------------------------------------*/ |
| 66 | |
| 67 | double parse_float(const char **p_inout, unsigned f, |
| 68 | double min, double max, const char *what) |
| 69 | { |
| 70 | const char *p; |
| 71 | char *q; |
| 72 | double x; |
| 73 | int err; |
| 74 | |
| 75 | err = errno; errno = 0; |
| 76 | p = *p_inout; |
| 77 | x = strtod(p, &q); |
| 78 | if (errno || x < min || x > max || (!(f&PNF_JUNK) && *q)) |
| 79 | bail("bad %s `%s'", what, p); |
| 80 | *p_inout = q; errno = err; |
| 81 | return (x); |
| 82 | } |
| 83 | |
| 84 | long parse_int(const char **p_inout, unsigned f, |
| 85 | long min, long max, const char *what) |
| 86 | { |
| 87 | const char *p; |
| 88 | char *q; |
| 89 | long x; |
| 90 | int err; |
| 91 | |
| 92 | err = errno; errno = 0; |
| 93 | p = *p_inout; |
| 94 | x = strtoul(p, &q, 0); |
| 95 | if (errno || x < min || x > max || (!(f&PNF_JUNK) && *q)) |
| 96 | bail("bad %s `%s'", what, p); |
| 97 | *p_inout = q; errno = err; |
| 98 | return (x); |
| 99 | } |
| 100 | |
| 101 | /*----- System utilities --------------------------------------------------*/ |
| 102 | |
| 103 | void sit(double t) |
| 104 | { |
| 105 | struct timeval tv; |
| 106 | double whole = floor(t); |
| 107 | |
| 108 | if (t) { |
| 109 | tv.tv_sec = whole; tv.tv_usec = floor((t - whole)*1.0e6) + 1; |
| 110 | if (select(0, 0, 0, 0, &tv) < 0) bail_syserr(errno, "failed to sleep"); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | void carefully_write(int fd, const void *buf, size_t sz) |
| 115 | { |
| 116 | const unsigned char *p = buf; |
| 117 | ssize_t n; |
| 118 | |
| 119 | if (fd < 0) return; |
| 120 | while (sz) { |
| 121 | n = write(fd, p, sz); |
| 122 | if (n < 0) { |
| 123 | if (errno == EINTR) continue; |
| 124 | bail_syserr(errno, "failed to write to output file"); |
| 125 | } |
| 126 | if (!n) bail("unexpected short write to output file"); |
| 127 | p += n; sz -= n; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | void open_file_on_demand(const char *file, FILE **fp_inout, const char *what) |
| 132 | { |
| 133 | FILE *fp; |
| 134 | |
| 135 | if (!*fp_inout) { |
| 136 | fp = fopen(file, "w"); |
| 137 | if (!fp) bail_syserr(errno, "failed to open %s file `%s'", what, file); |
| 138 | fprintf(fp, "## %s\n\n", what); |
| 139 | *fp_inout = fp; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | void check_write(FILE *fp, const char *what) |
| 144 | { |
| 145 | fflush(fp); |
| 146 | if (ferror(fp)) bail_syserr(errno, "error writing %s file", what); |
| 147 | } |
| 148 | |
| 149 | void carefully_fclose(FILE *fp, const char *what) |
| 150 | { |
| 151 | if (fp && (ferror(fp) || fclose(fp))) |
| 152 | bail_syserr(errno, "error writing %s file", what); |
| 153 | } |
| 154 | |
| 155 | off_t device_size(int fd, const char *file, int *blksz_out) |
| 156 | { |
| 157 | struct stat st; |
| 158 | uint64_t volsz; |
| 159 | |
| 160 | if (fstat(fd, &st)) |
| 161 | bail_syserr(errno, "failed to obtain status for `%s'", file); |
| 162 | if (S_ISREG(st.st_mode)) |
| 163 | volsz = st.st_size; |
| 164 | else if (S_ISBLK(st.st_mode)) { |
| 165 | if (ioctl(fd, BLKGETSIZE64, &volsz)) |
| 166 | bail_syserr(errno, "failed to get volume size for `%s'", file); |
| 167 | if (ioctl(fd, BLKSSZGET, blksz_out)) |
| 168 | bail_syserr(errno, "failed to get block size for `%s'", file); |
| 169 | } else |
| 170 | bail("can't read size for `%s': expected file or block device", file); |
| 171 | return ((off_t)volsz); |
| 172 | } |
| 173 | |
| 174 | /*----- Progress utilities ------------------------------------------------*/ |
| 175 | |
| 176 | struct progress_state progress = PROGRESS_STATE_INIT; |
| 177 | static struct banner_progress_item banner_progress; |
| 178 | |
| 179 | static void render_banner_progress(struct progress_item *item, |
| 180 | struct progress_render_state *render) |
| 181 | { |
| 182 | struct banner_progress_item *bi = (struct banner_progress_item *)item; |
| 183 | |
| 184 | progress_putleft(render, " %s", bi->msg); |
| 185 | progress_shownotice(render, 4, 7); |
| 186 | } |
| 187 | |
| 188 | void show_banner(const char *msg) |
| 189 | { |
| 190 | banner_progress._base.render = render_banner_progress; |
| 191 | progress_additem(&progress, &banner_progress._base); |
| 192 | banner_progress.msg = msg; |
| 193 | progress_update(&progress); |
| 194 | } |
| 195 | |
| 196 | void hide_banner(void) |
| 197 | { |
| 198 | if (!progress_removeitem(&progress, &banner_progress._base)) |
| 199 | progress_update(&progress); |
| 200 | } |
| 201 | |
| 202 | /*----- DVD utilities -----------------------------------------------------*/ |
| 203 | |
| 204 | #ifdef notdef |
| 205 | static void logfn(void *p, dvd_logger_level_t lev, |
| 206 | const char *fmt, va_list ap) |
| 207 | { |
| 208 | switch (lev) { |
| 209 | case DVD_LOGGER_LEVEL_ERROR: |
| 210 | fprintf("%s (libdvdread error): ", prog); |
| 211 | break; |
| 212 | case DVD_LOGGER_LEVEL_WARN: |
| 213 | fprintf("%s (libdvdread warning): ", prog); |
| 214 | break; |
| 215 | default: |
| 216 | return; |
| 217 | } |
| 218 | vfprintf(stderr, fmt, ap); |
| 219 | fputc('\n', stderr); |
| 220 | } |
| 221 | static const dvd_logger_cb logger = { logfn }; |
| 222 | #endif |
| 223 | |
| 224 | void open_dvd(const char *device, int mode, |
| 225 | int *fd_out, dvd_reader_t **dvd_out) |
| 226 | { |
| 227 | int fd; |
| 228 | dvd_reader_t *dvd; |
| 229 | int bannerp = 0; |
| 230 | |
| 231 | for (;;) { |
| 232 | fd = open(device, mode); |
| 233 | if (fd >= 0 || errno != ENOMEDIUM) break; |
| 234 | if (!bannerp) { |
| 235 | show_banner("Waiting for disc to be inserted..."); |
| 236 | bannerp = 1; |
| 237 | } |
| 238 | sit(0.2); |
| 239 | } |
| 240 | if (bannerp) hide_banner(); |
| 241 | if (fd < 0) bail_syserr(errno, "failed to open device `%s'", device); |
| 242 | if (dvd_out) { |
| 243 | #ifdef notdef |
| 244 | dvd = DVDOpen2(0, &logger, device); |
| 245 | #else |
| 246 | dvd = DVDOpen(device); |
| 247 | #endif |
| 248 | if (!dvd) bail("failed to open DVD on `%s'", device); |
| 249 | *dvd_out = dvd; |
| 250 | } |
| 251 | if (fd_out) *fd_out = fd; |
| 252 | else close(fd); |
| 253 | } |
| 254 | |
| 255 | void store_filename(char *buf, ident id) |
| 256 | { |
| 257 | switch (id_kind(id)) { |
| 258 | case RAW: |
| 259 | sprintf(buf, "#<raw device>"); |
| 260 | break; |
| 261 | case IFO: |
| 262 | if (!id_title(id)) sprintf(buf, "/VIDEO_TS/VIDEO_TS.IFO"); |
| 263 | else sprintf(buf, "/VIDEO_TS/VTS_%02u_0.IFO", id_title(id)); |
| 264 | break; |
| 265 | case BUP: |
| 266 | if (!id_title(id)) sprintf(buf, "/VIDEO_TS/VIDEO_TS.BUP"); |
| 267 | else sprintf(buf, "/VIDEO_TS/VTS_%02u_0.BUP", id_title(id)); |
| 268 | break; |
| 269 | case VOB: |
| 270 | if (!id_title(id)) sprintf(buf, "/VIDEO_TS/VIDEO_TS.VOB"); |
| 271 | else |
| 272 | sprintf(buf, "/VIDEO_TS/VTS_%02u_%u.VOB", id_title(id), id_part(id)); |
| 273 | break; |
| 274 | default: |
| 275 | abort(); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | static char *copy_string(char *p, const char *q) |
| 280 | { |
| 281 | while (*q) *p++ = *q++; |
| 282 | *p = 0; return (p); |
| 283 | } |
| 284 | |
| 285 | static char *copy_hex(char *p, const unsigned char *q, size_t sz) |
| 286 | { |
| 287 | while (sz) { |
| 288 | sprintf(p, "%02x", *q); |
| 289 | p += 2; q++; sz--; |
| 290 | } |
| 291 | return (p); |
| 292 | } |
| 293 | |
| 294 | int dvd_id(char *p, dvd_reader_t *dvd, unsigned f, const char *file) |
| 295 | { |
| 296 | char volid[33]; |
| 297 | unsigned char volsetid[16], discid[16]; |
| 298 | int rc; |
| 299 | size_t n; |
| 300 | |
| 301 | rc = DVDUDFVolumeInfo(dvd, |
| 302 | volid, sizeof(volid), |
| 303 | volsetid, sizeof(volsetid)); |
| 304 | if (!rc) { |
| 305 | p = copy_string(p, volid); |
| 306 | *p++ = '-'; |
| 307 | for (n = sizeof(volsetid); n && !volsetid[n - 1]; n--); |
| 308 | p = copy_hex(p, volsetid, n); |
| 309 | } else if (f&DIF_MUSTVOLINF) { |
| 310 | if (file) moan("failed to read volume info for `%s'", file); |
| 311 | else moan("failed to read volume info"); |
| 312 | return (-1); |
| 313 | } else |
| 314 | p = copy_string(p, "<error reading volume info>"); |
| 315 | |
| 316 | *p++ = ':'; |
| 317 | rc = DVDDiscID(dvd, discid); |
| 318 | if (!rc) |
| 319 | p = copy_hex(p, discid, sizeof(discid)); |
| 320 | else if (f&DIF_MUSTIFOHASH) { |
| 321 | if (file) moan("failed to determine disc id of `%s'", file); |
| 322 | else moan("failed to determine disc id"); |
| 323 | return (-1); |
| 324 | } else |
| 325 | p = copy_string(p, "<error reading disc-id>"); |
| 326 | |
| 327 | return (0); |
| 328 | } |
| 329 | |
| 330 | /*----- That's all, folks -------------------------------------------------*/ |