chiark / gitweb /
lib.h: Publish a `MEMCMP' macro, like `STRCMP'.
[dvdrip] / lib.c
1 #include "lib.h"
2
3 const char *prog = "<unset>";
4
5 void set_prog(const char *p)
6   { const char *q = strrchr(p, '/'); prog = q ? q + 1 : p; }
7
8 void vmoan(const char *fmt, va_list ap)
9   { vmoan_syserr(0, fmt, ap); }
10
11 void vmoan_syserr(int err, const char *fmt, va_list ap)
12 {
13   fprintf(stderr, "%s: ", prog);
14   vfprintf(stderr, fmt, ap);
15   if (err) fprintf(stderr, ": %s", strerror(errno));
16   fputc('\n', stderr);
17 }
18
19 void moan(const char *fmt, ...)
20   { va_list ap; va_start(ap, fmt); vmoan(fmt, ap); va_end(ap); }
21
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); }
24
25 void bail(const char *fmt, ...)
26   { va_list ap; va_start(ap, fmt); vmoan(fmt, ap); va_end(ap); exit(2); }
27
28 void bail_syserr(int err, const char *fmt, ...)
29 {
30   va_list ap;
31
32   va_start(ap, fmt); vmoan_syserr(err, fmt, ap); va_end(ap);
33   exit(2);
34 }
35
36 void sit(double t)
37 {
38   struct timeval tv;
39   double whole = floor(t);
40
41   if (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");
44   }
45 }
46
47 void carefully_write(int fd, const void *buf, size_t sz)
48 {
49   const unsigned char *p = buf;
50   ssize_t n;
51
52   if (fd < 0) return;
53   while (sz) {
54     n = write(fd, p, sz);
55     if (n < 0) {
56       if (errno == EINTR) continue;
57       bail_syserr(errno, "failed to write to output file");
58     }
59     if (!n) bail("unexpected short write to output file");
60     p += n; sz -= n;
61   }
62 }
63
64 void open_file_on_demand(const char *file, FILE **fp_inout, const char *what)
65 {
66   FILE *fp;
67
68   if (!*fp_inout) {
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);
72     *fp_inout = fp;
73   }
74 }
75
76 void check_write(FILE *fp, const char *what)
77 {
78   fflush(fp);
79   if (ferror(fp)) bail_syserr(errno, "error writing %s file", what);
80 }
81
82 void carefully_fclose(FILE *fp, const char *what)
83 {
84   if (fp && (ferror(fp) || fclose(fp)))
85     bail_syserr(errno, "error writing %s file", what);
86 }
87
88 void store_filename(char *buf, ident id)
89 {
90   switch (id_kind(id)) {
91     case RAW:
92       sprintf(buf, "#<raw device>");
93       break;
94     case IFO:
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));
97       break;
98     case BUP:
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));
101       break;
102     case VOB:
103       if (!id_title(id)) sprintf(buf, "/VIDEO_TS/VIDEO_TS.VOB");
104       else
105         sprintf(buf, "/VIDEO_TS/VTS_%02u_%u.VOB", id_title(id), id_part(id));
106       break;
107     default:
108       abort();
109   }
110 }
111
112 struct progress_state progress = PROGRESS_STATE_INIT;
113 static struct banner_progress_item banner_progress;
114
115 static void render_banner_progress(struct progress_item *item,
116                             struct progress_render_state *render)
117 {
118   struct banner_progress_item *bi = (struct banner_progress_item *)item;
119
120   progress_putleft(render, " %s", bi->msg);
121   progress_shownotice(render, 4, 7);
122 }
123
124 void show_banner(const char *msg)
125 {
126   banner_progress._base.render = render_banner_progress;
127   progress_additem(&progress, &banner_progress._base);
128   banner_progress.msg = msg;
129   progress_update(&progress);
130 }
131
132 void hide_banner(void)
133 {
134   if (!progress_removeitem(&progress, &banner_progress._base))
135     progress_update(&progress);
136 }
137
138 #ifdef notdef
139 static void logfn(void *p, dvd_logger_level_t lev,
140                   const char *fmt, va_list ap)
141 {
142   switch (lev) {
143     case DVD_LOGGER_LEVEL_ERROR:
144       fprintf("%s (libdvdread error): ", prog);
145       break;
146     case DVD_LOGGER_LEVEL_WARN:
147       fprintf("%s (libdvdread warning): ", prog);
148       break;
149     default:
150       return;
151   }
152   vfprintf(stderr, fmt, ap);
153   fputc('\n', stderr);
154 }
155 static const dvd_logger_cb logger = { logfn };
156 #endif
157
158 void open_dvd(const char *device, int *fd_out, dvd_reader_t **dvd_out)
159 {
160   int fd;
161   dvd_reader_t *dvd;
162   int bannerp = 0;
163
164   for (;;) {
165     fd = open(device, O_RDONLY);
166     if (fd >= 0 || errno != ENOMEDIUM) break;
167     if (!bannerp) {
168       show_banner("Waiting for disc to be inserted...");
169       bannerp = 1;
170     }
171     sit(0.2);
172   }
173   if (bannerp) hide_banner();
174   if (fd < 0) bail_syserr(errno, "failed to open device `%s'", device);
175   if (dvd_out) {
176 #ifdef notdef
177     dvd = DVDOpen2(0, &logger, device);
178 #else
179     dvd = DVDOpen(device);
180 #endif
181     if (!dvd) bail("failed to open DVD on `%s'", device);
182     *dvd_out = dvd;
183   }
184   if (fd_out) *fd_out = fd;
185   else close(fd);
186 }