chiark / gitweb /
WIP routesearch; allow -Ddebugflags=0
[ypp-sc-tools.web-live.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
46 #include <fcntl.h>
47 #include <unistd.h>
48 #include <sys/wait.h>
49 #include <sys/types.h>
50
51 typedef struct {
52   int w,h;
53   char d[];
54 } CanonImage;
55
56
57 /*----- debugging arrangements, rather contingent -----*/
58
59 typedef struct {
60   int x, y;
61 } Point;
62
63 typedef struct { /* both inclusive */
64   Point tl;
65   Point br;
66 } Rect;
67
68 #define RECT_W(r) ((r).br.x - (r).tl.x + 1)
69 #define RECT_H(r) ((r).br.y - (r).tl.y + 1)
70
71 #ifdef DEBUG_FLAG_LIST
72 enum {
73 #define DF(f) dbg__shift_##f,
74   DEBUG_FLAG_LIST
75 #undef DF
76 };
77 enum {
78 #define DF(f) dbg_##f = 1 << dbg__shift_##f,
79   DEBUG_FLAG_LIST
80 #undef DF
81 };
82 #define DEBUGP(f) (!!(debug_flags & dbg_##f))
83
84 #endif /*DEBUG_FLAG_LIST*/
85
86 #ifndef debug_flags
87 extern unsigned debug_flags;
88 #endif
89
90 void debug_flush(void);
91 #ifndef debug
92 # define debug stderr
93 #endif
94
95 #define FMT(f,a) __attribute__((format(printf,f,a)))
96 #define SCANFMT(f,a) __attribute__((format(scanf,f,a)))
97 #define NORET __attribute__((noreturn))
98
99 #define DEFINE_VWRAPPERF(decls, funcf, otherattribs)                    \
100   decls void funcf(const char *fmt, ...) FMT(1,2) otherattribs;         \
101   decls void funcf(const char *fmt, ...) {                              \
102     va_list al;  va_start(al,fmt);  v##funcf(fmt,al);  va_end(al);      \
103   }
104
105 #define DEBUG_DEFINE_SOME_DEBUGF(fl,funcf)                              \
106   static void v##funcf(const char *fmt, va_list al) {                   \
107     if (DEBUGP(fl))                                                     \
108       vfprintf(debug,fmt,al);                                           \
109   }                                                                     \
110   DEFINE_VWRAPPERF(static, funcf, )
111
112 #define DEBUG_DEFINE_DEBUGF(fl) DEBUG_DEFINE_SOME_DEBUGF(fl,debugf)
113
114
115 /*---------- error handling ----------*/
116
117 extern int o_quiet;
118
119 void vwarning(const char *fmt, va_list) FMT(1,0);
120 void warning(const char *fmt, ...)      FMT(1,2);
121
122 void vprogress(const char *fmt, va_list) FMT(1,0);
123 void progress(const char *fmt, ...)      FMT(1,2);
124
125 void vprogress_log(const char *fmt, va_list) FMT(1,0);
126 void progress_log(const char *fmt, ...)      FMT(1,2);
127
128 void vprogress_spinner(const char *fmt, va_list) FMT(1,0);
129 void progress_spinner(const char *fmt, ...)      FMT(1,2);
130
131 void vfatal(const char *fmt, va_list)  FMT(1,0) NORET;
132 void fatal(const char *fmt, ...)       FMT(1,2) NORET;
133
134 #define sysassert(what) \
135   ((what) ? (void)0 : sysassert_fail(__FILE__, __LINE__, #what))
136
137 void sysassert_fail(const char *file, int line, const char *what)
138    __attribute__((noreturn));
139
140 void waitpid_check_exitstatus(pid_t pid, const char *what, int sigpipeok);
141
142
143 void *mmalloc(size_t sz);
144 void *mcalloc(size_t sz);
145 void *mrealloc(void *p, size_t sz);
146
147 char *masprintf(const char *fmt, ...) FMT(1,2);
148
149
150 #define ARRAYSIZE(a) ((sizeof((a)) / sizeof((a)[0])))
151 #define FILLZERO(obj) (memset(&(obj),0,sizeof((obj))))
152
153 #define STRING2(x) #x
154 #define STRING(x) STRING2(x)
155
156 #endif /*COMMON_H*/