chiark / gitweb /
Fix location of Manu
[ypp-sc-tools.db-test.git] / yarrg / 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 <math.h>
36 #include <stdio.h>
37 #include <errno.h>
38 #include <assert.h>
39 #include <string.h>
40 #include <stdint.h>
41 #include <stdlib.h>
42 #include <unistd.h>
43 #include <dirent.h>
44 #include <inttypes.h>
45 #include <fnmatch.h>
46
47 #include <pcre.h>
48
49 #include <fcntl.h>
50 #include <unistd.h>
51 #include <sys/wait.h>
52 #include <sys/types.h>
53
54 typedef struct {
55   int w,h;
56   char d[];
57 } CanonImage;
58
59
60 /*----- debugging arrangements, rather contingent -----*/
61
62 typedef struct {
63   int x, y;
64 } Point;
65
66 typedef struct { /* both inclusive */
67   Point tl;
68   Point br;
69 } Rect;
70
71 #define RECT_W(r) ((r).br.x - (r).tl.x + 1)
72 #define RECT_H(r) ((r).br.y - (r).tl.y + 1)
73
74
75
76 #define DEBUG_FLAG_LIST                         \
77    DF(findypp)                                  \
78    DF(pages)                                    \
79    DF(rect)                                     \
80    DF(pixmap)                                   \
81    DF(struct)                                   \
82    DF(ocr)                                      \
83    DF(rsync)                                    \
84    DF(structcolon)                              \
85    DF(callout)
86
87 enum {
88 #define DF(f) dbg__shift_##f,
89   DEBUG_FLAG_LIST
90 #undef DF
91 };
92 enum {
93 #define DF(f) dbg_##f = 1 << dbg__shift_##f,
94   DEBUG_FLAG_LIST
95 #undef DF
96 };
97
98 unsigned debug_flags;
99
100 #define DEBUGP(f) (!!(debug_flags & dbg_##f))
101
102 void debug_flush(void);
103 #define debug stderr
104
105 const char *get_vardir(void);
106 const char *get_libdir(void);
107
108 #define FMT(f,a) __attribute__((format(printf,f,a)))
109 #define SCANFMT(f,a) __attribute__((format(scanf,f,a)))
110 #define NORET __attribute__((noreturn))
111
112 #define DEFINE_VWRAPPERF(decls, funcf, otherattribs)                    \
113   decls void funcf(const char *fmt, ...) FMT(1,2) otherattribs;         \
114   decls void funcf(const char *fmt, ...) {                              \
115     va_list al;  va_start(al,fmt);  v##funcf(fmt,al);  va_end(al);      \
116   }
117
118 #define DEBUG_DEFINE_SOME_DEBUGF(fl,funcf)                              \
119   static void v##funcf(const char *fmt, va_list al) {                   \
120     if (DEBUGP(fl))                                                     \
121       vfprintf(debug,fmt,al);                                           \
122   }                                                                     \
123   DEFINE_VWRAPPERF(static, funcf, )
124
125 #define DEBUG_DEFINE_DEBUGF(fl) DEBUG_DEFINE_SOME_DEBUGF(fl,debugf)
126
127
128 /*---------- error handling ----------*/
129
130 void vfatal(const char *fmt, va_list)  FMT(1,0) NORET;
131 void fatal(const char *fmt, ...)       FMT(1,2) NORET;
132
133 #define sysassert(what) \
134   ((what) ? (void)0 : sysassert_fail(__FILE__, __LINE__, #what))
135
136 void sysassert_fail(const char *file, int line, const char *what)
137    __attribute__((noreturn));
138
139 void waitpid_check_exitstatus(pid_t pid, const char *what, int sigpipeok);
140
141
142 void *mmalloc(size_t sz);
143 void *mrealloc(void *p, size_t sz);
144
145
146 #define dbassert(x) ((x) ? (void)0 : dbfile_assertfail(__FILE__,__LINE__,#x))
147 void dbfile_assertfail(const char *file, int line, const char *m) NORET;
148
149 FILE *dbfile;
150 void dbfile_getsline(char *lbuf, size_t lbufsz, const char *file, int line);
151 int dbfile_open(const char *tpath);   /* 0: ENOENT; 1: worked */
152 int dbfile_gzopen(const char *tpath); /* 0: ENOENT; 1: worked */
153 void dbfile_close(void); /* idempotent */
154
155 int dbfile_scanf(const char *fmt, ...) SCANFMT(1,2);
156 int dbfile_vscanf(const char *fmt, va_list al) SCANFMT(1,0);
157
158 int gzopen(const char *zpath, int oflags, FILE **f_r, pid_t *pid_r,
159            const char *gziplevel /* 0 for read; may be 0, or "-1" etc. */);
160   /* returns errno value from open */
161 void gzclose(FILE **f, pid_t *p, const char *what);
162   /* also OK with f==0, or p==-1 */
163
164 char *masprintf(const char *fmt, ...) FMT(1,2);
165
166 #define EXECLP_HELPER(helper, ...) do{                          \
167     char *helper_path= masprintf("%s/%s",get_libdir(),helper);  \
168     execlp(helper_path,helper, __VA_ARGS__);                    \
169     sysassert(errno==ENOENT);                                   \
170     fatal("Failed to find helper program %s.\n"                 \
171           "(Are you in the correct directory?)", helper);       \
172   }while(0)
173
174
175 #define ARRAYSIZE(a) ((sizeof((a)) / sizeof((a)[0])))
176 #define FILLZERO(obj) (memset(&(obj),0,sizeof((obj))))
177
178 #define STRING2(x) #x
179 #define STRING(x) STRING2(x)
180
181 #endif /*COMMON_H*/