chiark / gitweb /
WIP island determination; pixmap handling in progress
[ypp-sc-tools.web-live.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 <stdarg.h>
34 #include <stdio.h>
35 #include <errno.h>
36 #include <assert.h>
37 #include <string.h>
38 #include <stdint.h>
39 #include <stdlib.h>
40 #include <unistd.h>
41 #include <inttypes.h>
42
43 #include <sys/wait.h>
44 #include <sys/types.h>
45
46 typedef struct {
47   int w,h;
48   char d[];
49 } CanonImage;
50
51
52 /*----- debugging arrangements, rather contingent -----*/
53
54 typedef struct {
55   int x, y;
56 } Point;
57
58 typedef struct { /* both inclusive */
59   Point tl;
60   Point br;
61 } Rect;
62
63 #define RECT_W(r) ((r).br.x - (r).tl.x + 1)
64 #define RECT_H(r) ((r).br.y - (r).tl.y + 1)
65
66
67
68 #define DEBUG_FLAG_LIST                         \
69    DF(findypp)                                  \
70    DF(pages)                                    \
71    DF(rect)                                     \
72    DF(pixmap)                                   \
73    DF(struct)                                   \
74    DF(ocr)                                      \
75    DF(callout)
76
77 enum {
78 #define DF(f) dbg__shift_##f,
79   DEBUG_FLAG_LIST
80 #undef DF
81 };
82 enum {
83 #define DF(f) dbg_##f = 1 << dbg__shift_##f,
84   DEBUG_FLAG_LIST
85 #undef DF
86 };
87
88 unsigned debug_flags;
89
90 #define DEBUGP(f) (!!(debug_flags & dbg_##f))
91
92 void debug_flush(void);
93 #define debug stderr
94
95 const char *get_vardir(void);
96
97 #define FMT(f,a) __attribute__((format(printf,f,a)))
98 #define NORET __attribute__((noreturn))
99
100 #define DEFINE_VWRAPPERF(decls, funcf, otherattribs)                    \
101   decls void funcf(const char *fmt, ...) FMT(1,2) otherattribs;         \
102   decls void funcf(const char *fmt, ...) {                              \
103     va_list al;  va_start(al,fmt);  v##funcf(fmt,al);  va_end(al);      \
104   }
105
106 #define DEBUG_DEFINE_SOME_DEBUGF(fl,funcf)                              \
107   static void v##funcf(const char *fmt, va_list al) {                   \
108     if (DEBUGP(fl))                                                     \
109       vfprintf(debug,fmt,al);                                           \
110   }                                                                     \
111   DEFINE_VWRAPPERF(static, funcf, )
112
113 #define DEBUG_DEFINE_DEBUGF(fl) DEBUG_DEFINE_SOME_DEBUGF(fl,debugf)
114
115
116 /*---------- error handling ----------*/
117
118 void vfatal(const char *fmt, va_list)  FMT(1,0) NORET;
119 void fatal(const char *fmt, ...)       FMT(1,2) NORET;
120
121 #define sysassert(what) \
122   ((what) ? (void)0 : sysassert_fail(__FILE__, __LINE__, #what))
123
124 void sysassert_fail(const char *file, int line, const char *what)
125    __attribute__((noreturn));
126
127 void waitpid_check_exitstatus(pid_t pid, const char *what);
128
129
130 void *mmalloc(size_t sz);
131 void *mrealloc(void *p, size_t sz);
132
133 void fgetsline(FILE *f, char *lbuf, size_t lbufsz);
134
135
136 #endif /*COMMON_H*/