-static void vmoan(const char *fmt, va_list ap)
- { fprintf(stderr, "%s: ", prog); vfprintf(stderr, fmt, ap); }
-
-__attribute__((format(printf, 1, 2)))
-static void moan(const char *fmt, ...)
-{
- va_list ap;
-
- va_start(ap, fmt); vmoan(fmt, ap); va_end(ap);
- fputc('\n', stderr);
-}
-
-__attribute__((noreturn, format(printf, 1, 2)))
-static void bail(const char *fmt, ...)
-{
- va_list ap;
-
- va_start(ap, fmt); vmoan(fmt, ap); va_end(ap);
- fputc('\n', stderr);
- exit(2);
-}
-
-__attribute__((noreturn, format(printf, 2, 3)))
-static void bail_syserr(int err, const char *fmt, ...)
-{
- va_list ap;
-
- va_start(ap, fmt); vmoan(fmt, ap); va_end(ap);
- if (err) fprintf(stderr, ": %s", strerror(errno));
- fputc('\n', stderr);
- exit(2);
-}
-
-static void carefully_write(int fd, const void *buf, size_t sz)
-{
- const unsigned char *p = buf;
- ssize_t n;
-
- if (fd < 0) return;
- while (sz) {
- n = write(fd, p, sz);
- if (n < 0) {
- if (errno == EINTR) continue;
- bail_syserr(errno, "failed to write to output file");
- }
- if (!n) bail("unexpected short write to output file");
- p += n; sz -= n;
- }
-}
-
-static void open_file_on_demand(const char *file, FILE **fp_inout,
- const char *what)
-{
- FILE *fp;
-
- if (!*fp_inout) {
- fp = fopen(file, "w");
- if (!fp)
- bail_syserr(errno, "failed to open %s file `%s'", what, file);
- fprintf(fp, "## %s\n\n", what);
- *fp_inout = fp;
- }
-}
-
-static void check_write(FILE *fp, const char *what)
-{
- fflush(fp);
- if (ferror(fp)) bail_syserr(errno, "error writing %s file", what);
-}
-
-static void carefully_fclose(FILE *fp, const char *what)
-{
- if (fp && (ferror(fp) || fclose(fp)))
- bail_syserr(errno, "error writing %s file", what);
-}
-
-#define DEFVEC(vtype, etype) \
- typedef struct { etype *v; size_t n, sz; } vtype
-#define VEC_INIT { 0, 0, 0 }
-#define VEC_FREE(vv) do { \
- free((vv)->v); (vv)->v 0; (vv)->n = (vv)->sz = 0; \
-} while (0)
-#define VEC_PUSH(p, vv) do { \
- size_t _want; \
- if ((vv)->n >= (vv)->sz) { \
- (vv)->sz = (vv)->sz ? 2*(vv)->sz : 32; \
- _want = (vv)->sz*sizeof(*(vv)->v); \
- (vv)->v = realloc((vv)->v, _want); \
- if (!(vv)->v) bail("out of memory allocating %zu bytes", _want); \
- } \
- (p) = &(vv)->v[(vv)->n++]; \
-} while (0)
-
-enum { RAW, IFO, VOB, BUP };
-typedef uint_least32_t ident;
-
-static inline ident mkident(unsigned kind, unsigned title, unsigned part)
- { return (((ident)kind << 0) | ((ident)title << 8) | ((ident)part << 16)); }
-static inline unsigned id_kind(ident id) { return ((id >> 0)&0x0ff); }
-static inline unsigned id_title(ident id) { return ((id >> 8)&0x0ff); }
-static inline unsigned id_part(ident id) { return ((id >> 16)&0x0ff); }
-
-#define MAXFNSZ (1 + 8 + 1 + 12 + 1)
-
-static void store_filename(char *buf, ident id)
-{
- switch (id_kind(id)) {
- case RAW:
- sprintf(buf, "#<raw device>");
- break;
- case IFO:
- if (!id_title(id)) sprintf(buf, "/VIDEO_TS/VIDEO_TS.IFO");
- else sprintf(buf, "/VIDEO_TS/VTS_%02u_0.IFO", id_title(id));
- break;
- case BUP:
- if (!id_title(id)) sprintf(buf, "/VIDEO_TS/VIDEO_TS.BUP");
- else sprintf(buf, "/VIDEO_TS/VTS_%02u_0.BUP", id_title(id));
- break;
- case VOB:
- if (!id_title(id)) sprintf(buf, "/VIDEO_TS/VIDEO_TS.VOB");
- else
- sprintf(buf, "/VIDEO_TS/VTS_%02u_%u.VOB", id_title(id), id_part(id));
- break;
- default:
- abort();
- }
-}
-
-typedef uint_least32_t secaddr;
-#define PRIuSEC PRIuLEAST32
-#define SECLIMIT 0x00400000
-