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.
49 #include <sys/types.h>
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)
71 #ifdef DEBUG_FLAG_LIST
73 #define DF(f) dbg__shift_##f,
78 #define DF(f) dbg_##f = 1 << dbg__shift_##f,
82 #define DEBUGP(f) (!!(debug_flags & dbg_##f))
84 #endif /*DEBUG_FLAG_LIST*/
86 extern unsigned debug_flags;
88 void debug_flush(void);
93 #define FMT(f,a) __attribute__((format(printf,f,a)))
94 #define SCANFMT(f,a) __attribute__((format(scanf,f,a)))
95 #define NORET __attribute__((noreturn))
97 #define DEFINE_VWRAPPERF(decls, funcf, otherattribs) \
98 decls void funcf(const char *fmt, ...) FMT(1,2) otherattribs; \
99 decls void funcf(const char *fmt, ...) { \
100 va_list al; va_start(al,fmt); v##funcf(fmt,al); va_end(al); \
103 #define DEBUG_DEFINE_SOME_DEBUGF(fl,funcf) \
104 static void v##funcf(const char *fmt, va_list al) { \
106 vfprintf(debug,fmt,al); \
108 DEFINE_VWRAPPERF(static, funcf, )
110 #define DEBUG_DEFINE_DEBUGF(fl) DEBUG_DEFINE_SOME_DEBUGF(fl,debugf)
113 /*---------- error handling ----------*/
117 void vwarning(const char *fmt, va_list) FMT(1,0);
118 void warning(const char *fmt, ...) FMT(1,2);
120 void vprogress(const char *fmt, va_list) FMT(1,0);
121 void progress(const char *fmt, ...) FMT(1,2);
123 void vprogress_log(const char *fmt, va_list) FMT(1,0);
124 void progress_log(const char *fmt, ...) FMT(1,2);
126 void vprogress_spinner(const char *fmt, va_list) FMT(1,0);
127 void progress_spinner(const char *fmt, ...) FMT(1,2);
129 void vfatal(const char *fmt, va_list) FMT(1,0) NORET;
130 void fatal(const char *fmt, ...) FMT(1,2) NORET;
132 #define sysassert(what) \
133 ((what) ? (void)0 : sysassert_fail(__FILE__, __LINE__, #what))
135 void sysassert_fail(const char *file, int line, const char *what)
136 __attribute__((noreturn));
138 void waitpid_check_exitstatus(pid_t pid, const char *what, int sigpipeok);
141 void *mmalloc(size_t sz);
142 void *mcalloc(size_t sz);
143 void *mrealloc(void *p, size_t sz);
145 char *masprintf(const char *fmt, ...) FMT(1,2);
148 #define ARRAYSIZE(a) ((sizeof((a)) / sizeof((a)[0])))
149 #define FILLZERO(obj) (memset(&(obj),0,sizeof((obj))))
151 #define STRING2(x) #x
152 #define STRING(x) STRING2(x)