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.
30 void *mmalloc(size_t sz) {
33 sysassert( r= malloc(sz) );
36 void *mrealloc(void *p, size_t sz) {
39 sysassert( r= realloc(p,sz) );
43 DEFINE_VWRAPPERF(, progress, )
44 DEFINE_VWRAPPERF(, progress_log, )
45 DEFINE_VWRAPPERF(, progress_spinner, )
46 DEFINE_VWRAPPERF(, warning, )
47 DEFINE_VWRAPPERF(, fatal, NORET)
49 static int last_progress_len;
51 static void vprogress_core(int spinner, const char *fmt, va_list al) {
55 if (!debug_flags && !isatty(2)) return;
57 if (last_progress_len)
60 r= vfprintf(stderr,fmt,al);
67 if (r < last_progress_len) {
68 fprintf(stderr,"%*s", last_progress_len - r, "");
69 if (!r) putc('\r', stderr);
70 else while (last_progress_len-- > r) putc('\b',stderr);
74 if (ferror(stderr) || fflush(stderr)) _exit(16);
77 void vprogress(const char *fmt, va_list al) { vprogress_core(0,fmt,al); }
78 void vprogress_spinner(const char *fmt, va_list al) {
79 static const char spinchars[]="/-\\";
82 vprogress_core(spinchars[spinner],fmt,al);
84 spinner %= (sizeof(spinchars)-1);
87 void vprogress_log(const char *fmt, va_list al) {
91 vfprintf(stderr,fmt,al);
96 void vwarning(const char *fmt, va_list al) {
98 fputs("Warning: ",stderr);
99 vfprintf(stderr,fmt,al);
104 void vfatal(const char *fmt, va_list al) {
106 fputs("\n\nFatal error: ",stderr);
107 vfprintf(stderr,fmt,al);
109 fputs("\n\n",stderr);
113 void sysassert_fail(const char *file, int line, const char *what) {
117 "\nfatal operational error:\n"
118 " unsuccessful execution of: %s\n"
120 what, file,line, strerror(e));
124 void waitpid_check_exitstatus(pid_t pid, const char *what, int sigpipeok) {
128 got= waitpid(pid, &st, 0);
129 if (pid==-1) { sysassert(errno==EINTR); continue; }
132 sysassert( got==pid );
136 fatal("%s failed with nonzero exit status %d",
137 what, WEXITSTATUS(st));
138 } else if (WIFSIGNALED(st)) {
139 if (!sigpipeok || WTERMSIG(st) != SIGPIPE)
140 fatal("%s died due to signal %s%s", what,
141 strsignal(WTERMSIG(st)), WCOREDUMP(st)?" (core dumped)":"");
143 fatal("%s gave strange wait status %d", what, st);
147 char *masprintf(const char *fmt, ...) {
151 sysassert( vasprintf(&r,fmt,al) >= 0);
158 unsigned debug_flags;
160 void debug_flush(void) {
161 sysassert(!ferror(debug));
162 sysassert(!fflush(debug));