lib.c, lib.h: Introduce `vmoan_syserr' and `moan_syserr'.
[dvdrip] / lib.h
1 #ifndef LIB_H
2 #define LIB_H
3
4 #define _GNU_SOURCE
5 #define _FILE_OFFSET_BITS 64
6
7 #include <assert.h>
8 #include <ctype.h>
9 #include <errno.h>
10 #include <float.h>
11 #include <inttypes.h>
12 #include <limits.h>
13 #include <locale.h>
14 #include <math.h>
15 #include <stdarg.h>
16 #include <stdint.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <time.h>
21
22 #include <unistd.h>
23 #include <fcntl.h>
24 #include <sys/ioctl.h>
25 #include <sys/select.h>
26 #include <sys/stat.h>
27 #include <sys/time.h>
28
29 #include <getopt.h>
30
31 #include <linux/fs.h>
32
33 #include <dvdread/dvd_reader.h>
34 #include <dvdread/dvd_udf.h>
35 #include <dvdread/ifo_read.h>
36 #include <dvdread/ifo_types.h>
37
38 #include "multiprogress.h"
39
40 #define CTYPE_HACK(fn, ch) fn((unsigned char)(ch))
41 #define ISDIGIT(ch) CTYPE_HACK(isdigit, ch)
42 #define ISSPACE(ch) CTYPE_HACK(isspace, ch)
43
44 #define STRCMP(a, op, b) (strcmp((a), (b)) op 0)
45 #define STRNCMP(a, op, b, n) (strncmp((a), (b), (n)) op 0)
46
47 #ifdef DEBUG
48 # define D(x) x
49 #else
50 # define D(x)
51 #endif
52
53 #define N(v) (sizeof(v)/sizeof((v)[0]))
54
55 #define SECTORSZ 2048
56 #define SECTORS(n) (((n) + (SECTORSZ - 1))/SECTORSZ)
57 typedef uint_least32_t secaddr;
58 #define PRIuSEC PRIuLEAST32
59 #define SECLIMIT 0x00400000
60
61 #define PRINTF_LIKE(fmt, dots) __attribute__((format(printf, fmt, dots)))
62 #define NORETURN __attribute__((noreturn))
63
64 extern const char *prog;
65
66 extern void set_prog(const char *p);
67 extern void vmoan(const char *fmt, va_list ap);
68 extern void vmoan_syserr(int err, const char *fmt, va_list ap);
69 extern PRINTF_LIKE(1, 2) void moan(const char *fmt, ...);
70 extern PRINTF_LIKE(2, 3) void moan_syserr(int err, const char *fmt, ...);
71 extern PRINTF_LIKE(1, 2) NORETURN void bail(const char *fmt, ...);
72 extern PRINTF_LIKE(2, 3) NORETURN
73 void bail_syserr(int err, const char *fmt, ...);
74
75 extern void sit(double t);
76
77 extern void carefully_write(int fd, const void *buf, size_t sz);
78 extern void open_file_on_demand(const char *file, FILE **fp_inout,
79 const char *what);
80 extern void check_write(FILE *fp, const char *what);
81 extern void carefully_fclose(FILE *fp, const char *what);
82
83 enum { RAW, IFO, VOB, BUP };
84 typedef uint_least32_t ident;
85
86 static inline ident mkident(unsigned kind, unsigned title, unsigned part)
87 { return (((ident)kind << 0) | ((ident)title << 8) | ((ident)part << 16)); }
88 static inline unsigned id_kind(ident id) { return ((id >> 0)&0x0ff); }
89 static inline unsigned id_title(ident id) { return ((id >> 8)&0x0ff); }
90 static inline unsigned id_part(ident id) { return ((id >> 16)&0x0ff); }
91
92 #define MAXFNSZ (1 + 8 + 1 + 12 + 1)
93 extern void store_filename(char *buf, ident id);
94
95 struct banner_progress_item {
96 struct progress_item _base;
97 const char *msg;
98 };
99
100 extern struct progress_state progress;
101
102 extern void show_banner(const char *msg);
103 extern void hide_banner(void);
104
105 extern void open_dvd(const char *device,
106 int *fd_out, dvd_reader_t **dvd_out);
107
108 #endif