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 *mcalloc(size_t sz) {
39 sysassert( r= malloc(sz) );
43 void *mrealloc(void *p, size_t sz) {
46 sysassert( r= realloc(p,sz) );
50 DEFINE_VWRAPPERF(, progress, )
51 DEFINE_VWRAPPERF(, progress_log, )
52 DEFINE_VWRAPPERF(, progress_spinner, )
53 DEFINE_VWRAPPERF(, warning, )
54 DEFINE_VWRAPPERF(, fatal, NORET)
56 static int last_progress_len;
58 static void vprogress_core(int spinner, const char *fmt, va_list al) {
62 if (!debug_flags && !isatty(2)) return;
64 if (last_progress_len)
67 r= vfprintf(stderr,fmt,al);
74 if (r < last_progress_len) {
75 fprintf(stderr,"%*s", last_progress_len - r, "");
76 if (!r) putc('\r', stderr);
77 else while (last_progress_len-- > r) putc('\b',stderr);
81 if (ferror(stderr) || fflush(stderr)) _exit(16);
84 void vprogress(const char *fmt, va_list al) { vprogress_core(0,fmt,al); }
85 void vprogress_spinner(const char *fmt, va_list al) {
86 static const char spinchars[]="/-\\";
89 vprogress_core(spinchars[spinner],fmt,al);
91 spinner %= (sizeof(spinchars)-1);
94 void vprogress_log(const char *fmt, va_list al) {
98 vfprintf(stderr,fmt,al);
103 void vwarning(const char *fmt, va_list al) {
105 fputs("Warning: ",stderr);
106 vfprintf(stderr,fmt,al);
111 void vfatal(const char *fmt, va_list al) {
113 fputs("\n\nFatal error: ",stderr);
114 vfprintf(stderr,fmt,al);
116 fputs("\n\n",stderr);
120 void sysassert_fail(const char *file, int line, const char *what) {
124 "\nfatal operational error:\n"
125 " unsuccessful execution of: %s\n"
127 what, file,line, strerror(e));
131 void waitpid_check_exitstatus(pid_t pid, const char *what, int sigpipeok) {
135 got= waitpid(pid, &st, 0);
136 if (pid==-1) { sysassert(errno==EINTR); continue; }
139 sysassert( got==pid );
143 fatal("%s failed with nonzero exit status %d",
144 what, WEXITSTATUS(st));
145 } else if (WIFSIGNALED(st)) {
146 if (!sigpipeok || WTERMSIG(st) != SIGPIPE)
147 fatal("%s died due to signal %s%s", what,
148 strsignal(WTERMSIG(st)), WCOREDUMP(st)?" (core dumped)":"");
150 fatal("%s gave strange wait status %d", what, st);
154 char *masprintf(const char *fmt, ...) {
158 sysassert( vasprintf(&r,fmt,al) >= 0);
164 unsigned debug_flags;
166 void debug_flush(void) {
167 sysassert(!ferror(debug));
168 sysassert(!fflush(debug));