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 "."; }
36 const char *get_libdir(void) { return "."; }
42 mf_readscreenshot= 00020,
46 mode_findwindow= 00001,
47 mode_screenshot= 00011,
49 mode_showcharset= 01000,
55 omk_unset, omk_upload, omk_str, omk_raw, omk_none
57 static enum outmodekind o_outmode_kind;
58 static const char *o_outmode_str= 0;
60 static enum mode o_mode= mode_all;
61 static char *o_screenshot_fn;
62 static const char *o_serv_pctb, *o_serv_dict_fetch, *o_serv_dict_submit;
64 const char *o_resolver= "./dictionary-manager";
65 FILE *screenshot_file;
66 const char *o_ocean, *o_pirate;
69 static pid_t screenshot_compressor=-1;
72 ff_charset_allowedit |
73 ff_dict_fetch|ff_dict_submit|ff_dict_pirate;
75 static void vbadusage(const char *fmt, va_list) FMT(1,0) NORET;
76 static void vbadusage(const char *fmt, va_list al) {
77 fputs("bad usage: ",stderr);
78 vfprintf(stderr,fmt,al);
82 DEFINE_VWRAPPERF(static, badusage, NORET);
84 static void open_screenshot_file(const char *mode) {
85 if (!fnmatch("*.gz",o_screenshot_fn,0)) {
87 sysassert( (zfd= open(o_screenshot_fn, O_WRONLY|O_CREAT|O_TRUNC,
89 sysassert(! pipe(pipefds) );
90 sysassert( (screenshot_compressor=fork()) != -1 );
91 if (!screenshot_compressor) {
92 sysassert( dup2(pipefds[0],0)==0 );
93 sysassert( dup2(zfd,1)==1 );
94 sysassert(! close(zfd) );
95 sysassert(! close(pipefds[0]) );
96 sysassert(! close(pipefds[1]) );
97 execlp("gzip","gzip","-1",(char*)0);
98 sysassert(!"execlp gzip for screenshots");
100 sysassert(! close(zfd) );
101 sysassert(! close(pipefds[0]) );
102 sysassert( screenshot_file= fdopen(pipefds[1], "w") );
105 screenshot_file= fopen(o_screenshot_fn, mode);
106 if (!screenshot_file)
107 fatal("could not open screenshots file `%s': %s",
108 o_screenshot_fn, strerror(errno));
112 static void run_analysis(void) {
115 sysassert( tf= tmpfile() );
116 progress("running recognition...");
119 if (o_flags & ff_upload) {
121 fatal("Recognition successful, but refusing to upload partial data\n"
122 " (--single-page specified). Specify an output mode?");
125 sysassert( fseek(tf,0,SEEK_SET) == 0);
127 progress_log("processing results (--%s)...", o_outmode_str);
129 sysassert( (processor= fork()) != -1 );
132 sysassert( dup2(fileno(tf),0) ==0 );
133 EXECLP_HELPER("commod-results-processor", o_outmode_str, (char*)0);
136 waitpid_check_exitstatus(processor, "output processor/uploader", 0);
138 progress_log("all complete.");
141 static void rsync_core(const char *stem, const char *suffix,
145 progress("Updating dictionary %s...",stem);
147 sysassert( (fetcher= fork()) != -1 );
149 const char *rsync= getenv("YPPSC_PCTB_RSYNC");
150 if (!rsync) rsync= "rsync";
152 const char *src= getenv("YPPSC_PCTB_DICT_UPDATE");
153 char *remote= masprintf("%s/master-%s.txt%s", src, stem, suffix);
154 char *local= masprintf("_master-%s.txt%s", stem, suffix);
156 fprintf(stderr,"executing rsync to fetch %s to %s\n",remote,local);
157 char *opts= masprintf("-Lt%s%s",
159 DEBUGP(rsync) ? "v" : "");
160 execlp(rsync, "rsync",opts,"--",remote,local,(char*)0);
161 sysassert(!"exec rsync failed");
164 waitpid_check_exitstatus(fetcher, "rsync", 0);
167 void fetch_with_rsync_gz(const char *stem) { rsync_core(stem,".gz",""); }
168 void fetch_with_rsync(const char *stem) { rsync_core(stem,"","z"); }
170 static void set_server(const char *envname, const char *defprotocol,
171 const char *defvalue, const char *defvalue_test,
172 const char *userspecified,
176 if (!enable) { value= "0"; goto ok; }
179 value= userspecified;
180 else if ((value= getenv(envname)))
182 else if (o_flags & ff_testservers)
183 value= defvalue_test;
187 if (value[0]=='/' || (value[0]=='.' && value[1]=='/'))
188 /* absolute or relative pathname - or anyway, something with no hostname */
191 const char *colon= strchr(value, ':');
192 const char *slash= strchr(value, '/');
194 if (colon && (!slash || colon < slash))
195 /* colon before the first slash, if any */
196 /* rsync :: protocol specification - anyway, adding scheme:// won't help */
199 int vallen= strlen(value);
201 value= masprintf("%s%s%s", defprotocol, value,
202 vallen && value[vallen-1]=='/' ? "" : "/");
205 sysassert(! setenv(envname,value,1) );
208 int main(int argc, char **argv) {
211 sysassert( setlocale(LC_MESSAGES,"") );
212 sysassert( setlocale(LC_CTYPE,"en_GB.UTF-8") ||
213 setlocale(LC_CTYPE,"en.UTF-8") );
215 #define ARGVAL ((*++argv) ? *argv : \
216 (badusage("missing value for option %s",arg),(char*)0))
218 #define IS(s) (!strcmp(arg,(s)))
220 while ((arg=*++argv)) {
221 if (IS("--find-window-only")) o_mode= mode_findwindow;
222 else if (IS("--screenshot-only")) o_mode= mode_screenshot;
223 else if (IS("--show-charset")) o_mode= mode_showcharset;
224 else if (IS("--analyse-only") ||
225 IS("--same")) o_mode= mode_analyse;
226 else if (IS("--everything")) o_mode= mode_all;
227 else if (IS("--find-island")) o_flags |= ffs_printisland;
228 else if (IS("--single-page")) o_flags |= ff_singlepage;
229 else if (IS("--quiet")) o_quiet= 1;
230 else if (IS("--edit-charset")) o_flags |= ff_charset_edit;
231 else if (IS("--no-edit-charset")) o_flags &= ~(ffm_charset);
232 else if (IS("--test-servers")) o_flags |= ff_testservers;
233 else if (IS("--dict-local-only")) o_flags &= ~ffs_dict;
234 else if (IS("--dict-read-only")) o_flags &= (~ffs_dict | ff_dict_fetch);
235 else if (IS("--dict-anon")) o_flags &= ~ff_dict_pirate;
236 else if (IS("--dict-submit")) o_flags |= ff_dict_fetch|ff_dict_submit;
237 else if (IS("--raw-tsv")) o_outmode_kind= omk_raw;
238 else if (IS("--upload")) o_outmode_kind= omk_upload;
239 else if (IS("--arbitrage") ||
241 IS("--best-prices")) o_outmode_kind=omk_str,
244 else if (IS("--screenshot-file")||
245 IS("--screenshots-file")) o_screenshot_fn= ARGVAL;
246 else if (IS("--pctb-server")) o_serv_pctb= ARGVAL;
247 else if (IS("--dict-submit-server")) o_serv_dict_submit= ARGVAL;
248 else if (IS("--dict-update-server")) o_serv_dict_fetch= ARGVAL;
249 else if (IS("--ocean")) o_ocean= ARGVAL;
250 else if (IS("--pirate")) o_pirate= ARGVAL;
252 else if (IS("-D" #f)) \
253 debug_flags |= dbg_##f;
256 else if (IS("--window-id")) {
258 unsigned long windowid= strtoul(ARGVAL,&ep,0);
259 if (*ep) badusage("invalid window id");
260 set_yppclient_window(windowid);
262 badusage("unknown option `%s'",arg);
265 /* Consequential changes to options */
267 if (o_mode & mf_analyse) {
268 if (!o_outmode_kind) {
269 if (o_flags & ff_printisland) {
270 o_outmode_kind= omk_none;
271 o_flags |= ff_singlepage;
273 o_outmode_kind= omk_upload;
277 if (o_outmode_kind==omk_upload) {
278 o_flags |= ffs_upload;
279 o_outmode_str= "upload";
285 set_server("YPPSC_PCTB_PCTB",
286 "http://", "pctb.ilk.org" /*pctb.crabdance.com*/,
288 o_serv_pctb, o_flags & (ff_needisland|ff_upload));
290 set_server("YPPSC_PCTB_DICT_UPDATE",
291 "rsync://", "rsync.pctb.chiark.greenend.org.uk/pctb",
292 "rsync.pctb.chiark.greenend.org.uk/pctb/test",
293 o_serv_dict_fetch, o_flags & ff_dict_fetch);
295 set_server("YPPSC_PCTB_DICT_SUBMIT",
296 "http://", "dictup.pctb.chiark.greenend.org.uk",
297 "dictup.pctb.chiark.greenend.org.uk/test",
298 o_serv_dict_submit, o_flags & ff_dict_submit);
300 if (!o_screenshot_fn)
301 o_screenshot_fn= masprintf("%s/_pages.ppm%s", get_vardir(),
302 o_mode & mf_readscreenshot ? "" : ".gz");
304 /* Actually do the work */
306 canon_colour_prepare();
308 if (o_mode & mf_findwindow) {
309 screenshot_startup();
310 find_yppclient_window();
312 if (!ocean) ocean= o_ocean;
313 if (!pirate) pirate= o_pirate;
315 if (o_flags & ff_needisland)
317 badusage("need --ocean option when not using actual YPP client window"
318 " (consider supplying --pirate too)");
320 sysassert(! setenv("YPPSC_OCEAN",ocean,1) );
321 if (pirate && (o_flags & ff_dict_pirate))
322 sysassert(! setenv("YPPSC_PIRATE",pirate,1) );
324 switch (o_mode & mfm_special) {
326 case mode_showcharset: ocr_showcharsets(); exit(0);
330 if (o_mode & mf_screenshot) {
331 open_screenshot_file("w");
332 if (o_flags & ff_singlepage) take_one_screenshot();
333 else take_screenshots();
334 progress_log("OK for you to move the mouse now, and you can"
335 " use the YPP client again.");
337 if (o_mode & mf_readscreenshot) {
338 if ((o_flags & ff_upload) && !(o_flags & ff_testservers))
339 badusage("must not reuse screenshots for upload to live PCTB database");
340 open_screenshot_file("r");
341 if (o_flags & ff_singlepage) read_one_screenshot();
342 else read_screenshots();
344 if (o_mode & mf_analyse) {
345 if (o_flags & ff_needisland) {
347 if (o_flags & ff_printisland)
348 printf("%s, %s\n", archipelago, island);
349 sysassert(! setenv("YPPSC_ISLAND",island,1) );
351 switch (o_outmode_kind) {
352 case omk_upload: case omk_str: run_analysis(); break;
353 case omk_raw: analyse(stdout); break;
354 case omk_none: break;
358 if (screenshot_compressor!=-1) {
359 progress("Finishing compressing screenshots...");
360 waitpid_check_exitstatus(screenshot_compressor,"screenshots gzip",0);
362 progress_log("Finished.");
369 DEFINE_VWRAPPERF(, progress, )
370 DEFINE_VWRAPPERF(, progress_log, )
371 DEFINE_VWRAPPERF(, progress_spinner, )
372 DEFINE_VWRAPPERF(, warning, )
373 DEFINE_VWRAPPERF(, fatal, NORET)
375 static int last_progress_len;
377 static void vprogress_core(int spinner, const char *fmt, va_list al) {
381 if (!isatty(2)) return;
383 if (last_progress_len)
386 r= vfprintf(stderr,fmt,al);
389 putc(spinner,stderr);
393 if (r < last_progress_len) {
394 fprintf(stderr,"%*s", last_progress_len - r, "");
395 if (!r) putc('\r', stderr);
396 else while (last_progress_len-- > r) putc('\b',stderr);
398 last_progress_len= r;
400 if (ferror(stderr) || fflush(stderr)) _exit(16);
403 void vprogress(const char *fmt, va_list al) { vprogress_core(0,fmt,al); }
404 void vprogress_spinner(const char *fmt, va_list al) {
405 static const char spinchars[]="/-\\";
408 vprogress_core(spinchars[spinner],fmt,al);
410 spinner %= (sizeof(spinchars)-1);
413 void vprogress_log(const char *fmt, va_list al) {
417 vfprintf(stderr,fmt,al);
422 void vwarning(const char *fmt, va_list al) {
424 fputs("Warning: ",stderr);
425 vfprintf(stderr,fmt,al);
430 void vfatal(const char *fmt, va_list al) {
432 fputs("\n\nFatal error: ",stderr);
433 vfprintf(stderr,fmt,al);
435 fputs("\n\n",stderr);
439 void sysassert_fail(const char *file, int line, const char *what) {
443 "\nfatal operational error:\n"
444 " unsuccessful execution of: %s\n"
446 what, file,line, strerror(e));
450 void waitpid_check_exitstatus(pid_t pid, const char *what, int sigpipeok) {
454 got= waitpid(pid, &st, 0);
455 if (pid==-1) { sysassert(errno==EINTR); continue; }
458 sysassert( got==pid );
462 fatal("%s failed with nonzero exit status %d",
463 what, WEXITSTATUS(st));
464 } else if (WIFSIGNALED(st)) {
465 if (!sigpipeok || WTERMSIG(st) != SIGPIPE)
466 fatal("%s died due to signal %s%s", what,
467 strsignal(WTERMSIG(st)), WCOREDUMP(st)?" (core dumped)":"");
469 fatal("%s gave strange wait status %d", what, st);
473 char *masprintf(const char *fmt, ...) {
477 sysassert( vasprintf(&r,fmt,al) >= 0);