chiark / gitweb /
Decombobulate island names in building screen (/w)
[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 <inttypes.h>
43
44 #include <sys/wait.h>
45 #include <sys/types.h>
46
47 typedef struct {
48   int w,h;
49   char d[];
50 } CanonImage;
51
52
53 /*----- debugging arrangements, rather contingent -----*/
54
55 typedef struct {
56   int x, y;
57 } Point;
58
59 typedef struct { /* both inclusive */
60   Point tl;
61   Point br;
62 } Rect;
63
64 #define RECT_W(r) ((r).br.x - (r).tl.x + 1)
65 #define RECT_H(r) ((r).br.y - (r).tl.y + 1)
66
67
68
69 #define DEBUG_FLAG_LIST                         \
70    DF(findypp)                                  \
71    DF(pages)                                    \
72    DF(rect)                                     \
73    DF(pixmap)                                   \
74    DF(struct)                                   \
75    DF(ocr)                                      \
76    DF(rsync)                                    \
77    DF(structcolon)                              \
78    DF(callout)
79
80 enum {
81 #define DF(f) dbg__shift_##f,
82   DEBUG_FLAG_LIST
83 #undef DF
84 };
85 enum {
86 #define DF(f) dbg_##f = 1 << dbg__shift_##f,
87   DEBUG_FLAG_LIST
88 #undef DF
89 };
90
91 unsigned debug_flags;
92
93 #define DEBUGP(f) (!!(debug_flags & dbg_##f))
94
95 void debug_flush(void);
96 #define debug stderr
97
98 const char *get_vardir(void);
99 const char *get_libdir(void);
100
101 #define FMT(f,a) __attribute__((format(printf,f,a)))
102 #define SCANFMT(f,a) __attribute__((format(scanf,f,a)))
103 #define NORET __attribute__((noreturn))
104
105 #define DEFINE_VWRAPPERF(decls, funcf, otherattribs)                    \
106   decls void funcf(const char *fmt, ...) FMT(1,2) otherattribs;         \
107   decls void funcf(const char *fmt, ...) {                              \
108     va_list al;  va_start(al,fmt);  v##funcf(fmt,al);  va_end(al);      \
109   }
110
111 #define DEBUG_DEFINE_SOME_DEBUGF(fl,funcf)                              \
112   static void v##funcf(const char *fmt, va_list al) {                   \
113     if (DEBUGP(fl))                                                     \
114       vfprintf(debug,fmt,al);                                           \
115   }                                                                     \
116   DEFINE_VWRAPPERF(static, funcf, )
117
118 #define DEBUG_DEFINE_DEBUGF(fl) DEBUG_DEFINE_SOME_DEBUGF(fl,debugf)
119
120
121 /*---------- error handling ----------*/
122
123 void vfatal(const char *fmt, va_list)  FMT(1,0) NORET;
124 void fatal(const char *fmt, ...)       FMT(1,2) NORET;
125
126 #define sysassert(what) \
127   ((what) ? (void)0 : sysassert_fail(__FILE__, __LINE__, #what))
128
129 void sysassert_fail(const char *file, int line, const char *what)
130    __attribute__((noreturn));
131
132 void waitpid_check_exitstatus(pid_t pid, const char *what);
133
134
135 void *mmalloc(size_t sz);
136 void *mrealloc(void *p, size_t sz);
137
138
139 #define dbassert(x) ((x) ? (void)0 : dbfile_assertfail(__FILE__,__LINE__,#x))
140 void dbfile_assertfail(const char *file, int line, const char *m) NORET;
141
142 FILE *dbfile;
143 void dbfile_getsline(char *lbuf, size_t lbufsz, const char *file, int line);
144 int dbfile_open(const char *tpath); /* 0: ENOENT; 1: worked */
145 void dbfile_close(void); /* idempotent */
146
147 int dbfile_scanf(const char *fmt, ...) SCANFMT(1,2);
148 int dbfile_vscanf(const char *fmt, va_list al) SCANFMT(1,0);
149
150
151 char *masprintf(const char *fmt, ...) FMT(1,2);
152
153 #define EXECLP_HELPER(helper, ...) do{                          \
154     char *helper_path= masprintf("%s/%s",get_libdir(),helper);  \
155     execlp(helper_path,helper, __VA_ARGS__);                    \
156     sysassert(errno==ENOENT);                                   \
157     fatal("Failed to find helper program %s.\n"                 \
158           "(Are you in the correct directory?)", helper);       \
159   }while(0)
160
161 #endif /*COMMON_H*/