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