2 * useful common stuff, mostly error handling and debugging
5 * This is part of ypp-sc-tools, a set of third-party tools for assisting
6 * players of Yohoho Puzzle Pirates.
8 * Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
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.
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.
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/>.
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.
48 #include <sys/types.h>
52 const struct RgbImage *rgb;
57 /*----- debugging arrangements, rather contingent -----*/
63 typedef struct { /* both inclusive */
68 #define RECT_W(r) ((r).br.x - (r).tl.x + 1)
69 #define RECT_H(r) ((r).br.y - (r).tl.y + 1)
73 #define DEBUG_FLAG_LIST \
85 #define DF(f) dbg__shift_##f,
90 #define DF(f) dbg_##f = 1 << dbg__shift_##f,
97 #define DEBUGP(f) (!!(debug_flags & dbg_##f))
99 void debug_flush(void);
102 const char *get_vardir(void);
103 const char *get_libdir(void);
105 #define FMT(f,a) __attribute__((format(printf,f,a)))
106 #define SCANFMT(f,a) __attribute__((format(scanf,f,a)))
107 #define NORET __attribute__((noreturn))
109 #define DEFINE_VWRAPPERF(decls, funcf, otherattribs) \
110 decls void funcf(const char *fmt, ...) FMT(1,2) otherattribs; \
111 decls void funcf(const char *fmt, ...) { \
112 va_list al; va_start(al,fmt); v##funcf(fmt,al); va_end(al); \
115 #define DEBUG_DEFINE_SOME_DEBUGF(fl,funcf) \
116 static void v##funcf(const char *fmt, va_list al) { \
118 vfprintf(debug,fmt,al); \
120 DEFINE_VWRAPPERF(static, funcf, )
122 #define DEBUG_DEFINE_DEBUGF(fl) DEBUG_DEFINE_SOME_DEBUGF(fl,debugf)
125 /*---------- error handling ----------*/
127 void vfatal(const char *fmt, va_list) FMT(1,0) NORET;
128 void fatal(const char *fmt, ...) FMT(1,2) NORET;
130 #define sysassert(what) \
131 ((what) ? (void)0 : sysassert_fail(__FILE__, __LINE__, #what))
133 void sysassert_fail(const char *file, int line, const char *what)
134 __attribute__((noreturn));
136 void waitpid_check_exitstatus(pid_t pid, const char *what);
139 void *mmalloc(size_t sz);
140 void *mrealloc(void *p, size_t sz);
143 #define dbassert(x) ((x) ? (void)0 : dbfile_assertfail(__FILE__,__LINE__,#x))
144 void dbfile_assertfail(const char *file, int line, const char *m) NORET;
147 void dbfile_getsline(char *lbuf, size_t lbufsz, const char *file, int line);
148 int dbfile_open(const char *tpath); /* 0: ENOENT; 1: worked */
149 void dbfile_close(void); /* idempotent */
151 int dbfile_scanf(const char *fmt, ...) SCANFMT(1,2);
152 int dbfile_vscanf(const char *fmt, va_list al) SCANFMT(1,0);
155 char *masprintf(const char *fmt, ...) FMT(1,2);
157 #define EXECLP_HELPER(helper, ...) do{ \
158 char *helper_path= masprintf("%s/%s",get_libdir(),helper); \
159 execlp(helper_path,helper, __VA_ARGS__); \
160 sysassert(errno==ENOENT); \
161 fatal("Failed to find helper program %s.\n" \
162 "(Are you in the correct directory?)", helper); \
166 #define ARRAYSIZE(a) ((sizeof((a)) / sizeof((a)[0])))
167 #define FILLZERO(obj) (memset(&(obj),0,sizeof((obj))))
169 #define STRING2(x) #x
170 #define STRING(x) STRING2(x)