chiark / gitweb /
mention --upload at top of README
[ypp-sc-tools.db-test.git] / pctb / common.h
1 /*
2  * useful common stuff, mostly error handling and debugging
3  */
4 /*
5  *  This is part of ypp-sc-tools, a set of third-party tools for assisting
6  *  players of Yohoho Puzzle Pirates.
7  * 
8  *  Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
9  * 
10  *  This program is free software: you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation, either version 3 of the License, or
13  *  (at your option) any later version.
14  * 
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  * 
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  * 
23  *  Yohoho and Puzzle Pirates are probably trademarks of Three Rings and
24  *  are used without permission.  This program is not endorsed or
25  *  sponsored by Three Rings.
26  */
27
28 #ifndef COMMON_H
29 #define COMMON_H
30
31 #define _GNU_SOURCE
32
33 #include <locale.h>
34 #include <stdarg.h>
35 #include <stdio.h>
36 #include <errno.h>
37 #include <assert.h>
38 #include <string.h>
39 #include <stdint.h>
40 #include <stdlib.h>
41 #include <unistd.h>
42 #include <dirent.h>
43 #include <pcre.h>
44 #include <inttypes.h>
45
46 #include <sys/wait.h>
47 #include <sys/types.h>
48
49 typedef struct {
50   int w,h;
51   char d[];
52 } CanonImage;
53
54
55 /*----- debugging arrangements, rather contingent -----*/
56
57 typedef struct {
58   int x, y;
59 } Point;
60
61 typedef struct { /* both inclusive */
62   Point tl;
63   Point br;
64 } Rect;
65
66 #define RECT_W(r) ((r).br.x - (r).tl.x + 1)
67 #define RECT_H(r) ((r).br.y - (r).tl.y + 1)
68
69
70
71 #define DEBUG_FLAG_LIST                         \
72    DF(findypp)                                  \
73    DF(pages)                                    \
74    DF(rect)                                     \
75    DF(pixmap)                                   \
76    DF(struct)                                   \
77    DF(ocr)                                      \
78    DF(rsync)                                    \
79    DF(structcolon)                              \
80    DF(callout)
81
82 enum {
83 #define DF(f) dbg__shift_##f,
84   DEBUG_FLAG_LIST
85 #undef DF
86 };
87 enum {
88 #define DF(f) dbg_##f = 1 << dbg__shift_##f,
89   DEBUG_FLAG_LIST
90 #undef DF
91 };
92
93 unsigned debug_flags;
94
95 #define DEBUGP(f) (!!(debug_flags & dbg_##f))
96
97 void debug_flush(void);
98 #define debug stderr
99
100 const char *get_vardir(void);
101 const char *get_libdir(void);
102
103 #define FMT(f,a) __attribute__((format(printf,f,a)))
104 #define SCANFMT(f,a) __attribute__((format(scanf,f,a)))
105 #define NORET __attribute__((noreturn))
106
107 #define DEFINE_VWRAPPERF(decls, funcf, otherattribs)                    \
108   decls void funcf(const char *fmt, ...) FMT(1,2) otherattribs;         \
109   decls void funcf(const char *fmt, ...) {                              \
110     va_list al;  va_start(al,fmt);  v##funcf(fmt,al);  va_end(al);      \
111   }
112
113 #define DEBUG_DEFINE_SOME_DEBUGF(fl,funcf)                              \
114   static void v##funcf(const char *fmt, va_list al) {                   \
115     if (DEBUGP(fl))                                                     \
116       vfprintf(debug,fmt,al);                                           \
117   }                                                                     \
118   DEFINE_VWRAPPERF(static, funcf, )
119
120 #define DEBUG_DEFINE_DEBUGF(fl) DEBUG_DEFINE_SOME_DEBUGF(fl,debugf)
121
122
123 /*---------- error handling ----------*/
124
125 void vfatal(const char *fmt, va_list)  FMT(1,0) NORET;
126 void fatal(const char *fmt, ...)       FMT(1,2) NORET;
127
128 #define sysassert(what) \
129   ((what) ? (void)0 : sysassert_fail(__FILE__, __LINE__, #what))
130
131 void sysassert_fail(const char *file, int line, const char *what)
132    __attribute__((noreturn));
133
134 void waitpid_check_exitstatus(pid_t pid, const char *what);
135
136
137 void *mmalloc(size_t sz);
138 void *mrealloc(void *p, size_t sz);
139
140
141 #define dbassert(x) ((x) ? (void)0 : dbfile_assertfail(__FILE__,__LINE__,#x))
142 void dbfile_assertfail(const char *file, int line, const char *m) NORET;
143
144 FILE *dbfile;
145 void dbfile_getsline(char *lbuf, size_t lbufsz, const char *file, int line);
146 int dbfile_open(const char *tpath); /* 0: ENOENT; 1: worked */
147 void dbfile_close(void); /* idempotent */
148
149 int dbfile_scanf(const char *fmt, ...) SCANFMT(1,2);
150 int dbfile_vscanf(const char *fmt, va_list al) SCANFMT(1,0);
151
152
153 char *masprintf(const char *fmt, ...) FMT(1,2);
154
155 #define EXECLP_HELPER(helper, ...) do{                          \
156     char *helper_path= masprintf("%s/%s",get_libdir(),helper);  \
157     execlp(helper_path,helper, __VA_ARGS__);                    \
158     sysassert(errno==ENOENT);                                   \
159     fatal("Failed to find helper program %s.\n"                 \
160           "(Are you in the correct directory?)", helper);       \
161   }while(0)
162
163
164 #define ARRAYSIZE(a) ((sizeof((a)) / sizeof((a)[0])))
165
166
167 #endif /*COMMON_H*/