3 const char *prog = "<unset>";
5 void set_prog(const char *p)
6 { const char *q = strrchr(p, '/'); prog = q ? q + 1 : p; }
8 void vmoan(const char *fmt, va_list ap)
9 { vmoan_syserr(0, fmt, ap); }
11 void vmoan_syserr(int err, const char *fmt, va_list ap)
13 fprintf(stderr, "%s: ", prog);
14 vfprintf(stderr, fmt, ap);
15 if (err) fprintf(stderr, ": %s", strerror(errno));
19 void moan(const char *fmt, ...)
20 { va_list ap; va_start(ap, fmt); vmoan(fmt, ap); va_end(ap); }
22 void moan_syserr(int err, const char *fmt, ...)
23 { va_list ap; va_start(ap, fmt); vmoan_syserr(err, fmt, ap); va_end(ap); }
25 void bail(const char *fmt, ...)
26 { va_list ap; va_start(ap, fmt); vmoan(fmt, ap); va_end(ap); exit(2); }
28 void bail_syserr(int err, const char *fmt, ...)
32 va_start(ap, fmt); vmoan_syserr(err, fmt, ap); va_end(ap);
39 double whole = floor(t);
42 tv.tv_sec = whole; tv.tv_usec = floor((t - whole)*1.0e6) + 1;
43 if (select(0, 0, 0, 0, &tv) < 0) bail_syserr(errno, "failed to sleep");
47 void carefully_write(int fd, const void *buf, size_t sz)
49 const unsigned char *p = buf;
56 if (errno == EINTR) continue;
57 bail_syserr(errno, "failed to write to output file");
59 if (!n) bail("unexpected short write to output file");
64 void open_file_on_demand(const char *file, FILE **fp_inout, const char *what)
69 fp = fopen(file, "w");
70 if (!fp) bail_syserr(errno, "failed to open %s file `%s'", what, file);
71 fprintf(fp, "## %s\n\n", what);
76 void check_write(FILE *fp, const char *what)
79 if (ferror(fp)) bail_syserr(errno, "error writing %s file", what);
82 void carefully_fclose(FILE *fp, const char *what)
84 if (fp && (ferror(fp) || fclose(fp)))
85 bail_syserr(errno, "error writing %s file", what);
88 void store_filename(char *buf, ident id)
90 switch (id_kind(id)) {
92 sprintf(buf, "#<raw device>");
95 if (!id_title(id)) sprintf(buf, "/VIDEO_TS/VIDEO_TS.IFO");
96 else sprintf(buf, "/VIDEO_TS/VTS_%02u_0.IFO", id_title(id));
99 if (!id_title(id)) sprintf(buf, "/VIDEO_TS/VIDEO_TS.BUP");
100 else sprintf(buf, "/VIDEO_TS/VTS_%02u_0.BUP", id_title(id));
103 if (!id_title(id)) sprintf(buf, "/VIDEO_TS/VIDEO_TS.VOB");
105 sprintf(buf, "/VIDEO_TS/VTS_%02u_%u.VOB", id_title(id), id_part(id));
112 struct progress_state progress = PROGRESS_STATE_INIT;
113 static struct banner_progress_item banner_progress;
115 static void render_banner_progress(struct progress_item *item,
116 struct progress_render_state *render)
118 struct banner_progress_item *bi = (struct banner_progress_item *)item;
120 progress_putleft(render, " %s", bi->msg);
121 progress_shownotice(render, 4, 7);
124 void show_banner(const char *msg)
126 banner_progress._base.render = render_banner_progress;
127 progress_additem(&progress, &banner_progress._base);
128 banner_progress.msg = msg;
129 progress_update(&progress);
132 void hide_banner(void)
134 if (!progress_removeitem(&progress, &banner_progress._base))
135 progress_update(&progress);
139 static void logfn(void *p, dvd_logger_level_t lev,
140 const char *fmt, va_list ap)
143 case DVD_LOGGER_LEVEL_ERROR:
144 fprintf("%s (libdvdread error): ", prog);
146 case DVD_LOGGER_LEVEL_WARN:
147 fprintf("%s (libdvdread warning): ", prog);
152 vfprintf(stderr, fmt, ap);
155 static const dvd_logger_cb logger = { logfn };
158 void open_dvd(const char *device, int *fd_out, dvd_reader_t **dvd_out)
165 fd = open(device, O_RDONLY);
166 if (fd >= 0 || errno != ENOMEDIUM) break;
168 show_banner("Waiting for disc to be inserted...");
173 if (bannerp) hide_banner();
174 if (fd < 0) bail_syserr(errno, "failed to open device `%s'", device);
177 dvd = DVDOpen2(0, &logger, device);
179 dvd = DVDOpen(device);
181 if (!dvd) bail("failed to open DVD on `%s'", device);
184 if (fd_out) *fd_out = fd;