+
+
+
+DEFINE_VWRAPPERF(, progress, )
+DEFINE_VWRAPPERF(, progress_log, )
+DEFINE_VWRAPPERF(, progress_spinner, )
+DEFINE_VWRAPPERF(, warning, )
+DEFINE_VWRAPPERF(, fatal, NORET)
+
+static int last_progress_len;
+
+static void vprogress_core(int spinner, const char *fmt, va_list al) {
+ int r;
+
+ if (o_quiet) return;
+ if (!isatty(2)) return;
+
+ if (last_progress_len)
+ putc('\r',stderr);
+
+ r= vfprintf(stderr,fmt,al);
+
+ if (spinner) {
+ putc(spinner,stderr);
+ r++;
+ }
+
+ if (r < last_progress_len) {
+ fprintf(stderr,"%*s", last_progress_len - r, "");
+ if (!r) putc('\r', stderr);
+ else while (last_progress_len-- > r) putc('\b',stderr);
+ }
+ last_progress_len= r;
+
+ if (ferror(stderr) || fflush(stderr)) _exit(16);
+}
+
+void vprogress(const char *fmt, va_list al) { vprogress_core(0,fmt,al); }
+void vprogress_spinner(const char *fmt, va_list al) {
+ static const char spinchars[]="/-\\";
+ static int spinner;
+
+ vprogress_core(spinchars[spinner],fmt,al);
+ spinner++;
+ spinner %= (sizeof(spinchars)-1);
+}
+
+void vprogress_log(const char *fmt, va_list al) {
+ if (o_quiet) return;
+
+ progress("");
+ vfprintf(stderr,fmt,al);
+ putc('\n',stderr);
+ fflush(stderr);
+}
+
+void vwarning(const char *fmt, va_list al) {
+ progress("");
+ fputs("Warning: ",stderr);
+ vfprintf(stderr,fmt,al);
+ fputs("\n",stderr);
+ fflush(stderr);
+}
+
+void vfatal(const char *fmt, va_list al) {
+ progress("");
+ fputs("\n\nFatal error: ",stderr);
+ vfprintf(stderr,fmt,al);
+ fflush(stderr);
+ fputs("\n\n",stderr);
+ _exit(4);
+}
+
+void sysassert_fail(const char *file, int line, const char *what) {
+ int e= errno;
+ progress("");
+ fprintf(stderr,
+ "\nfatal operational error:\n"
+ " unsuccessful execution of: %s\n"
+ " %s:%d: %s\n\n",
+ what, file,line, strerror(e));
+ _exit(16);
+}
+
+void waitpid_check_exitstatus(pid_t pid, const char *what) {
+ pid_t got;
+ int st;
+ for (;;) {
+ got= waitpid(pid, &st, 0);
+ if (pid==-1) { sysassert(errno==EINTR); continue; }
+ break;
+ }
+ sysassert( got==pid );
+
+ if (WIFEXITED(st)) {
+ if (WEXITSTATUS(st))
+ fatal("%s failed with nonzero exit status %d",
+ what, WEXITSTATUS(st));
+ } else if (WIFSIGNALED(st)) {
+ fatal("%s died due to signal %s%s", what,
+ strsignal(WTERMSIG(st)), WCOREDUMP(st)?" (core dumped)":"");
+ } else {
+ fatal("%s gave strange wait status %d", what, st);
+ }
+}
+
+char *masprintf(const char *fmt, ...) {
+ char *r;
+ va_list al;
+ va_start(al,fmt);
+ sysassert( vasprintf(&r,fmt,al) >= 0);
+ sysassert(r);
+ va_end(al);
+ return r;
+}