chiark / gitweb /
Much better error handling.
[ypp-sc-tools.db-test.git] / pctb / common.h
1
2 #ifndef COMMON_H
3 #define COMMON_H
4
5 #define _GNU_SOURCE
6
7 #include <stdarg.h>
8
9 #include <sys/types.h>
10
11 typedef struct {
12   int w,h;
13   char d[];
14 } CanonImage;
15
16
17 /*----- debugging arrangements, rather contingent -----*/
18
19 typedef struct {
20   int x, y;
21 } Point;
22
23 typedef struct { /* both inclusive */
24   Point tl;
25   Point br;
26 } Rect;
27
28
29
30 #define DEBUG_FLAG_LIST                         \
31    DF(findypp)                                  \
32    DF(pages)                                    \
33    DF(rect)                                     \
34    DF(ocr)                                      \
35    DF(callout)
36
37 enum {
38 #define DF(f) dbg__shift_##f,
39   DEBUG_FLAG_LIST
40 #undef DF
41 };
42 enum {
43 #define DF(f) dbg_##f = 1 << dbg__shift_##f,
44   DEBUG_FLAG_LIST
45 #undef DF
46 };
47
48 unsigned debug_flags;
49
50 #define DEBUGP(f) (!!(debug_flags & dbg_##f))
51
52 void debug_flush(void);
53 #define debug stderr
54
55 const char *get_vardir(void);
56
57 #define FMT(f,a) __attribute__((format(printf,f,a)))
58 #define NORET __attribute__((noreturn))
59
60 #define DEFINE_VWRAPPERF(decls, funcf, otherattribs)                    \
61   decls void funcf(const char *fmt, ...) FMT(1,2) otherattribs;         \
62   decls void funcf(const char *fmt, ...) {                              \
63     va_list al;  va_start(al,fmt);  v##funcf(fmt,al);  va_end(al);      \
64   }
65
66 #define DEBUG_DEFINE_SOME_DEBUGF(fl,funcf)                              \
67   static void v##funcf(const char *fmt, va_list al) {                   \
68     if (DEBUGP(fl))                                                     \
69       vfprintf(debug,fmt,al);                                           \
70   }                                                                     \
71   DEFINE_VWRAPPERF(static, funcf, )
72
73 #define DEBUG_DEFINE_DEBUGF(fl) DEBUG_DEFINE_SOME_DEBUGF(fl,debugf)
74
75
76 /*---------- error handling ----------*/
77
78 void vfatal(const char *fmt, va_list)  FMT(1,0) NORET;
79 void fatal(const char *fmt, ...)       FMT(1,2) NORET;
80
81 #define sysassert(what) \
82   ((what) ? (void)0 : sysassert_fail(__FILE__, __LINE__, #what))
83
84 void sysassert_fail(const char *file, int line, const char *what)
85    __attribute__((noreturn));
86
87
88 void *mmalloc(size_t sz);
89 void *mrealloc(void *p, size_t sz);
90
91
92 #endif /*COMMON_H*/