2 * ypp-commodities main program: argument parsing etc.
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 debug_flush(void) {
31 sysassert(!ferror(debug));
32 sysassert(!fflush(debug));
35 const char *get_vardir(void) { return "."; }
40 mf_readscreenshot= 0020,
43 mode_findwindow= 0001,
44 mode_screenshot= 0011,
50 static char *o_screenshots_fn;
51 static int o_single_page, o_quiet;
53 FILE *screenshots_file;
56 static void badusage(const char *fmt, ...)
57 __attribute__((format(printf,1,2),noreturn));
58 static void badusage(const char *fmt, ...) {
64 static void open_screenshots_file(const char *mode) {
65 screenshots_file= fopen(o_screenshots_fn, mode);
66 if (!screenshots_file)
67 fatal("could not open screenshots file `%s': %s",
68 o_screenshots_fn, strerror(errno));
71 int main(int argc, char **argv) {
75 #define ARGVAL ((*++argv) ? *argv : \
76 badusage("missing value for option %s",arg),(char*)0)
78 while ((arg=*++argv)) {
79 if (!strcmp(arg,"--find-window-only"))
80 o_mode= mode_findwindow;
81 else if (!strcmp(arg,"--screenshot-only"))
82 o_mode= mode_screenshot;
83 else if (!strcmp(arg,"--analyse-only"))
85 else if (!strcmp(arg,"--single-page"))
87 else if (!strcmp(arg,"--quiet"))
89 else if (!strcmp(arg,"--screenshots-file"))
90 o_screenshots_fn= ARGVAL;
92 else if (!strcmp(arg,"-D" #f)) \
93 debug_flags |= dbg_##f;
96 else if (!strcmp(arg,"--window-id")) {
98 unsigned long windowid= strtoul(ARGVAL,&ep,0);
99 if (*ep) badusage("invalid window id");
100 set_yppclient_window(windowid);
102 badusage("unknown option `%s'",arg);
105 if (!o_screenshots_fn) {
106 r= asprintf(&o_screenshots_fn,"%s/#pages#.ppm",get_vardir());
110 if (o_mode & mf_findwindow) {
111 screenshot_startup();
112 find_yppclient_window();
114 if (o_mode & mf_screenshot) {
115 open_screenshots_file("w");
116 if (o_single_page) take_one_screenshot();
117 else take_screenshots();
119 if (o_mode & mf_readscreenshot) {
120 open_screenshots_file("r");
121 if (o_single_page) read_one_screenshot();
122 else read_screenshots();
124 if (o_mode & mf_analyse) {
134 DEFINE_VWRAPPERF(, progress, )
135 DEFINE_VWRAPPERF(, progress_log, )
136 DEFINE_VWRAPPERF(, progress_spinner, )
137 DEFINE_VWRAPPERF(, warning, )
138 DEFINE_VWRAPPERF(, fatal, NORET)
140 static int last_progress_len;
142 static void vprogress_core(int spinner, const char *fmt, va_list al) {
146 if (!isatty(2)) return;
148 if (last_progress_len)
151 r= vfprintf(stderr,fmt,al);
154 putc(spinner,stderr);
158 if (r < last_progress_len) {
159 fprintf(stderr,"%*s", last_progress_len - r, "");
160 if (!r) putc('\r', stderr);
161 else while (last_progress_len-- > r) putc('\b',stderr);
163 last_progress_len= r;
165 if (ferror(stderr) || fflush(stderr)) _exit(16);
168 void vprogress(const char *fmt, va_list al) { vprogress_core(0,fmt,al); }
169 void vprogress_spinner(const char *fmt, va_list al) {
170 static const char spinchars[]="/-\\";
173 vprogress_core(spinchars[spinner],fmt,al);
175 spinner %= (sizeof(spinchars)-1);
178 void vprogress_log(const char *fmt, va_list al) {
182 vfprintf(stderr,fmt,al);
187 void vwarning(const char *fmt, va_list al) {
189 fputs("Warning: ",stderr);
190 vfprintf(stderr,fmt,al);
195 void vfatal(const char *fmt, va_list al) {
197 fputs("\n\nFatal error: ",stderr);
198 vfprintf(stderr,fmt,al);
200 fputs("\n\n",stderr);
204 void sysassert_fail(const char *file, int line, const char *what) {
208 "\nfatal operational error:\n"
209 " unsuccessful execution of: %s\n"
211 what, file,line, strerror(e));
215 void *mmalloc(size_t sz) {
218 sysassert( r= malloc(sz) );
221 void *mrealloc(void *p, size_t sz) {
224 sysassert( r= realloc(p,sz) );