+/*
+ * ypp-commodities main program: argument parsing etc.
+ */
+/*
+ * This is part of ypp-sc-tools, a set of third-party tools for assisting
+ * players of Yohoho Puzzle Pirates.
+ *
+ * Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Yohoho and Puzzle Pirates are probably trademarks of Three Rings and
+ * are used without permission. This program is not endorsed or
+ * sponsored by Three Rings.
+ */
#include "convert.h"
-
void debug_flush(void) {
- eassert(!fflush(debug));
- eassert(!ferror(debug));
+ sysassert(!ferror(debug));
+ sysassert(!fflush(debug));
}
-
const char *get_vardir(void) { return "."; }
static enum {
- mf_findwindow= 01,
- mf_screenshot= 02,
- mf_analyse= 04,
+ mf_findwindow= 0001,
+ mf_screenshot= 0010,
+ mf_readscreenshot= 0020,
+ mf_analyse= 0100,
- mode_findwindow= 01,
- mode_screenshot= 03,
- mode_analyse= 04,
+ mode_findwindow= 0001,
+ mode_screenshot= 0011,
+ mode_analyse= 0120,
- mode_all= 07,
+ mode_all= 0111,
} o_mode= mode_all;
static char *o_screenshots_fn;
FILE *screenshots_file;
+
+static void badusage(const char *fmt, ...)
+ __attribute__((format(printf,1,2),noreturn));
+static void badusage(const char *fmt, ...) {
+ va_list al;
+ va_start(al,fmt);
+ exit(12);
+}
+
+static void open_screenshots_file(const char *mode) {
+ screenshots_file= fopen(o_screenshots_fn, mode);
+ if (!screenshots_file)
+ fatal("could not open screenshots file `%s': %s",
+ o_screenshots_fn, strerror(errno));
+}
+
int main(int argc, char **argv) {
const char *arg;
int r;
+#define ARGVAL ((*++argv) ? *argv : \
+ badusage("missing value for option %s",arg),(char*)0)
+
while ((arg=*++argv)) {
if (!strcmp(arg,"--find-window-only"))
o_mode= mode_findwindow;
else if (!strcmp(arg,"--quiet"))
o_quiet= 1;
else if (!strcmp(arg,"--screenshots-file"))
- eassert( o_screenshots_fn= *++argv );
+ o_screenshots_fn= ARGVAL;
#define DF(f) \
else if (!strcmp(arg,"-D" #f)) \
debug_flags |= dbg_##f;
#undef DF
else if (!strcmp(arg,"--window-id")) {
char *ep;
- eassert((arg=*++argv));
- unsigned long windowid= strtoul(arg,&ep,0);
- eassert(!*ep);
+ unsigned long windowid= strtoul(ARGVAL,&ep,0);
+ if (*ep) badusage("invalid window id");
set_yppclient_window(windowid);
} else
- eassert(!"bad option");
+ badusage("unknown option `%s'",arg);
}
if (!o_screenshots_fn) {
r= asprintf(&o_screenshots_fn,"%s/#pages#.ppm",get_vardir());
- eassert(r>=0); eassert(o_screenshots_fn);
+ sysassert(r>=0);
}
if (o_mode & mf_findwindow) {
find_yppclient_window();
}
if (o_mode & mf_screenshot) {
- screenshots_file= fopen(o_screenshots_fn, "w"); eassert(screenshots_file);
+ open_screenshots_file("w");
if (o_single_page) take_one_screenshot();
else take_screenshots();
- } else {
- screenshots_file= fopen(o_screenshots_fn, "r"); eassert(screenshots_file);
+ }
+ if (o_mode & mf_readscreenshot) {
+ open_screenshots_file("r");
if (o_single_page) read_one_screenshot();
else read_screenshots();
}
-DEFINE_VWRAPPERF(, progress)
-DEFINE_VWRAPPERF(, progress_log)
-DEFINE_VWRAPPERF(, progress_spinner)
-DEFINE_VWRAPPERF(, warning)
+DEFINE_VWRAPPERF(, progress, )
+DEFINE_VWRAPPERF(, progress_log, )
+DEFINE_VWRAPPERF(, progress_spinner, )
+DEFINE_VWRAPPERF(, warning, )
+DEFINE_VWRAPPERF(, fatal, NORET)
static int last_progress_len;
putc('\r',stderr);
r= vfprintf(stderr,fmt,al);
- eassert(r>=0);
if (spinner) {
putc(spinner,stderr);
else while (last_progress_len-- > r) putc('\b',stderr);
}
last_progress_len= r;
- fflush(stderr);
+
+ if (ferror(stderr) || fflush(stderr)) _exit(16);
}
void vprogress(const char *fmt, va_list al) { vprogress_core(0,fmt,al); }
void vwarning(const char *fmt, va_list al) {
progress("");
- fputs("warning: ",stderr);
+ 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 *mmalloc(size_t sz) {
+ void *r;
+ if (!sz) return 0;
+ sysassert( r= malloc(sz) );
+ return r;
+}
+void *mrealloc(void *p, size_t sz) {
+ assert(sz);
+ void *r;
+ sysassert( r= realloc(p,sz) );
+ return r;
+}