chiark / gitweb /
Fix stupid optimisation bug (typoed variable name)
[ypp-sc-tools.main.git] / yarrg / convert.c
1 /*
2  * yarrg main program: argument parsing etc.
3  */
4 /*
5  *  This is part of ypp-sc-tools, a set of third-party tools for assisting
6  *  players of Yohoho Puzzle Pirates.
7  * 
8  *  Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
9  * 
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.
14  * 
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.
19  * 
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/>.
22  * 
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.
26  */
27
28 #include "convert.h"
29
30 void debug_flush(void) {
31   sysassert(!ferror(debug));
32   sysassert(!fflush(debug));
33 }
34
35 const char *get_vardir(void) { return "."; }
36 const char *get_libdir(void) { return "."; }
37
38
39 enum outmodekind {
40   omk_unset, omk_raw, omk_str, omk_upload_yarrg, omk_upload_pctb
41 };
42
43 typedef struct {
44   enum outmodekind kind; /* unset is sentinel */
45   const char *str; /* non-0 iff not unset or raw */
46 } OutputMode;
47
48 static OutputMode o_outmodes[10];
49
50 static char *o_screenshot_fn;
51 static const char *o_serv_pctb, *o_serv_yarrg;
52 static const char *o_serv_dict_fetch, *o_serv_dict_submit;
53
54 const char *o_resolver= "./dictionary-manager";
55 FILE *screenshot_file;
56 const char *o_ocean, *o_pirate;
57 int o_quiet;
58
59 static pid_t screenshot_compressor=-1;
60
61 enum mode o_mode= mode_all;
62 enum flags o_flags=
63    ff_charset_allowedit |
64    ff_dict_fetch|ff_dict_submit|ff_dict_pirate;
65
66 static void vbadusage(const char *fmt, va_list) FMT(1,0) NORET;
67 static void vbadusage(const char *fmt, va_list al) {
68   fputs("bad usage: ",stderr);
69   vfprintf(stderr,fmt,al);
70   fputc('\n',stderr);
71   exit(12);
72 }
73 DEFINE_VWRAPPERF(static, badusage, NORET);
74
75 static void open_screenshot_file(int for_write) {
76   if (!fnmatch("*.gz",o_screenshot_fn,0)) {
77     int mode= for_write ? O_WRONLY|O_CREAT|O_TRUNC : O_RDONLY;
78     sysassert(! gzopen(o_screenshot_fn, mode, &screenshot_file,
79                        &screenshot_compressor, "-1") );
80   } else {
81     screenshot_file= fopen(o_screenshot_fn, for_write ? "w" : "r");
82     if (!screenshot_file)
83       fatal("could not open screenshots file `%s': %s",
84             o_screenshot_fn, strerror(errno));
85   }
86 }
87
88 static void run_analysis(void) {
89   FILE *tf;
90   OutputMode *om;
91
92   sysassert( tf= tmpfile() );
93   progress("running recognition...");
94   analyse(tf);
95
96   if (o_flags & ff_upload) {
97     if (npages<=1)
98       fatal("Recognition successful, but refusing to upload partial data\n"
99             " (--single-page specified).  Specify an output mode?");
100   }
101
102   for (om=o_outmodes; om->kind != omk_unset; om++) {
103     sysassert( fseek(tf,0,SEEK_SET) == 0);
104
105     progress_log("processing results (--%s)...", om->str);
106     pid_t processor;
107     sysassert( (processor= fork()) != -1 );
108
109     if (!processor) {
110       sysassert( dup2(fileno(tf),0) ==0 );
111       if (om->kind==omk_raw) {
112         execlp("cat","cat",(char*)0);
113         sysassert(!"execute cat");
114       } else {
115         EXECLP_HELPER("commod-results-processor", om->str, (char*)0);
116       }
117     }
118
119     waitpid_check_exitstatus(processor, "output processor/uploader", 0);
120   }
121   
122   fclose(tf);
123   progress_log("all complete.");
124 }
125
126 static void rsync_core(const char *stem, const char *suffix,
127                        const char *zopt) {
128   pid_t fetcher;
129
130   progress("Updating dictionary %s...",stem);
131
132   sysassert( (fetcher= fork()) != -1 );
133   if (!fetcher) {
134     const char *rsync= getenv("YPPSC_YARRG_RSYNC");
135     if (!rsync) rsync= "rsync";
136   
137     const char *src= getenv("YPPSC_YARRG_DICT_UPDATE");
138     char *remote= masprintf("%s/master-%s.txt%s", src, stem, suffix);
139     char *local= masprintf("_master-%s.txt%s", stem, suffix);
140     if (DEBUGP(rsync))
141       fprintf(stderr,"executing rsync to fetch %s to %s\n",remote,local);
142     char *opts= masprintf("-Lt%s%s",
143                           zopt,
144                           DEBUGP(rsync) ? "v" : "");
145     execlp(rsync, "rsync",opts,"--",remote,local,(char*)0);
146     sysassert(!"exec rsync failed");
147   }
148
149   waitpid_check_exitstatus(fetcher, "rsync", 0);
150 }
151
152 void fetch_with_rsync_gz(const char *stem) { rsync_core(stem,".gz",""); }
153 void fetch_with_rsync(const char *stem) { rsync_core(stem,"","z"); }
154
155 static void get_timestamp(void) {
156   FILE *tf;
157   pid_t child;
158   sysassert( tf= tmpfile() );
159
160   sysassert( (child= fork()) != -1 );
161   if (!child) {
162     sysassert( dup2(fileno(tf),1)==1 );
163     EXECLP_HELPER("database-info-fetch","timestamp",(char*)0);
164   }
165   waitpid_check_exitstatus(child,"timestamp request",0);
166
167   sysassert( fseek(tf,0,SEEK_SET) == 0 );
168   static char lbuf[30];
169   int l= fread(lbuf,1,sizeof(lbuf),tf);
170   sysassert( !ferror(tf) );
171   assert( feof(tf) );
172   assert( l>1 );
173   l--;
174   assert( lbuf[l]=='\n' );
175   lbuf[l]= 0;
176
177   sysassert(! setenv("YPPSC_DATA_TIMESTAMP",lbuf,1) );
178   fclose(tf);
179 }
180
181 static void set_server(const char *envname, const char *defprotocol,
182                        const char *defvalue, const char *defvalue_test,
183                        const char *userspecified,
184                        int enable) {
185   const char *value;
186   
187   if (!enable) { value= "0"; goto ok; }
188
189   if (userspecified)
190     value= userspecified;
191   else if ((value= getenv(envname)))
192     ;
193   else if (o_flags & ff_testservers)
194     value= defvalue_test;
195   else
196     value= defvalue;
197
198   if (value[0]=='/' || (value[0]=='.' && value[1]=='/'))
199     /* absolute or relative pathname - or anyway, something with no hostname */
200     goto ok;
201
202   const char *colon= strchr(value, ':');
203   const char *slash= strchr(value, '/');
204
205   if (colon && (!slash || colon < slash))
206     /* colon before the first slash, if any */
207     /* rsync :: protocol specification - anyway, adding scheme:// won't help */
208     goto ok;
209
210   int vallen= strlen(value);
211
212   value= masprintf("%s%s%s", defprotocol, value,
213                    vallen && value[vallen-1]=='/' ? "" : "/");
214
215  ok:
216   sysassert(! setenv(envname,value,1) );
217 }
218
219 static void outputmode(enum outmodekind kind, const char *str) {
220   OutputMode *om= o_outmodes;
221   OutputMode *sentinel= o_outmodes + ARRAYSIZE(o_outmodes) - 1;
222   for (;;) {
223     if (om==sentinel) badusage("too many output modes specified");
224     if (!om->kind) break;
225     om++;
226   }
227   om->kind= kind;
228   om->str=  str;
229 }
230
231 static void outputmode_uploads(void) {
232   outputmode(omk_upload_yarrg, "upload-yarrg");
233   outputmode(omk_upload_pctb, "upload-pctb");
234 }
235
236 int main(int argc, char **argv) {
237   const char *arg;
238
239   sysassert( setlocale(LC_MESSAGES,"") );
240   sysassert( setlocale(LC_CTYPE,"en_GB.UTF-8") ||
241              setlocale(LC_CTYPE,"en.UTF-8") );
242
243 #define ARGVAL  ((*++argv) ? *argv : \
244                  (badusage("missing value for option %s",arg),(char*)0))
245
246 #define IS(s) (!strcmp(arg,(s)))
247
248   while ((arg=*++argv)) {
249     if (IS("--find-window-only"))      o_mode= mode_findwindow;
250     else if (IS("--screenshot-only"))  o_mode= mode_screenshot;
251     else if (IS("--show-charset"))     o_mode= mode_showcharset;
252     else if (IS("--analyse-only") ||
253              IS("--same"))             o_mode= mode_analyse;
254     else if (IS("--everything"))       o_mode= mode_all;
255     else if (IS("--find-island"))      o_flags |= ffs_printisland;
256     else if (IS("--single-page"))      o_flags |= ff_singlepage;
257     else if (IS("--quiet"))            o_quiet= 1;
258     else if (IS("--edit-charset"))     o_flags |= ff_charset_edit;
259     else if (IS("--no-edit-charset"))  o_flags &= ~(ffm_charset);
260     else if (IS("--test-servers"))     o_flags |= ff_testservers;
261     else if (IS("--dict-local-only"))  o_flags &= ~ffs_dict;
262     else if (IS("--dict-read-only"))   o_flags &= (~ffs_dict | ff_dict_fetch);
263     else if (IS("--dict-anon"))        o_flags &= ~ff_dict_pirate;
264     else if (IS("--dict-submit"))      o_flags |= ff_dict_fetch|ff_dict_submit;
265     else if (IS("--dict-no-update"))   o_flags &= ~ff_dict_fetch; // testing
266     else if (IS("--raw-tsv"))          outputmode(omk_raw,0);
267     else if (IS("--upload"))           outputmode_uploads();
268     else if (IS("--upload-yarrg"))     outputmode(omk_upload_yarrg,arg+2);
269     else if (IS("--upload-pctb"))      outputmode(omk_upload_pctb,arg+2);
270     else if (IS("--arbitrage") ||
271              IS("--tsv") ||
272              IS("--best-prices"))      outputmode(omk_str,arg+2);
273     else if (IS("--screenshot-file")||
274              IS("--screenshots-file")) o_screenshot_fn= ARGVAL;
275     else if (IS("--yarrg-server"))        o_serv_yarrg=       ARGVAL;
276     else if (IS("--pctb-server"))         o_serv_pctb=        ARGVAL;
277     else if (IS("--dict-submit-server"))  o_serv_dict_submit= ARGVAL;
278     else if (IS("--dict-update-server"))  o_serv_dict_fetch=  ARGVAL;
279     else if (IS("--ocean"))            o_ocean=  ARGVAL;
280     else if (IS("--pirate"))           o_pirate= ARGVAL;
281 #define DF(f)                                   \
282     else if (IS("-D" #f))                       \
283       debug_flags |= dbg_##f;
284     DEBUG_FLAG_LIST
285 #undef DF
286     else if (IS("--window-id")) {
287       char *ep;
288       unsigned long windowid= strtoul(ARGVAL,&ep,0);
289       if (*ep) badusage("invalid window id");
290       set_yppclient_window(windowid);
291     } else
292       badusage("unknown option `%s'",arg);
293   }
294
295   /* Consequential changes to options */
296
297   if (o_mode & mf_analyse) {
298     if (!o_outmodes[0].kind) {
299       if (o_flags & ff_printisland) {
300         o_flags |= ff_singlepage;
301       } else {
302         outputmode_uploads();
303       }
304     }
305   } else {
306     if (o_outmodes[0].kind)
307       badusage("overall mode does not include analysis but output option(s)"
308                " (eg `--%s') specified",  o_outmodes[0].str);
309   }
310
311   OutputMode *om;
312   for (om=o_outmodes; om->kind; om++) {
313     switch (om->kind) {
314     case omk_upload_yarrg: o_flags |= ffs_upload | ff_use_yarrg; break;
315     case omk_upload_pctb:  o_flags |= ffs_upload | ff_use_pctb;  break;
316     default: ;
317     }
318   }
319
320   if ((o_flags & (ff_needisland|ff_upload)) &&
321       !(o_flags & (ffm_use)))
322     o_flags |= ffm_use; /* all */
323
324   if (o_serv_yarrg && !o_serv_dict_submit)
325     o_serv_dict_submit= o_serv_yarrg;
326   
327   /* Defaults */
328
329   set_server("YPPSC_YARRG_YARRG",
330              "http://",          "upload.yarrg.chiark.net",
331                                  "upload.yarrg.chiark.net/test",
332              o_serv_yarrg,       o_flags & ff_use_yarrg);
333
334   set_server("YPPSC_YARRG_PCTB",
335              "http://",          "pctb.ilk.org" /*pctb.crabdance.com*/,
336                                  "pctb.ilk.org",
337              o_serv_pctb,        o_flags & ff_use_pctb);
338              
339   set_server("YPPSC_YARRG_DICT_UPDATE",
340              "rsync://",         "rsync.yarrg.chiark.net/yarrg",
341                                  "rsync.yarrg.chiark.net/yarrg/test",
342              o_serv_dict_fetch,   o_flags & ff_dict_fetch);
343
344   set_server("YPPSC_YARRG_DICT_SUBMIT",
345              "http://",           "upload.yarrg.chiark.net",
346                                   "upload.yarrg.chiark.net/test",
347              o_serv_dict_submit,  o_flags & ff_dict_submit);
348
349   if (!o_screenshot_fn)
350     o_screenshot_fn= masprintf("%s/_pages.ppm.gz", get_vardir());
351
352   /* Actually do the work */
353
354   if ((o_flags & ff_upload) && (o_flags & ff_use_yarrg))
355     get_timestamp();
356              
357   canon_colour_prepare();
358   
359   if (o_mode & mf_findwindow) {
360     screenshot_startup();
361     find_yppclient_window();
362   }
363   if (!ocean)  ocean=  o_ocean;
364   if (!pirate) pirate= o_pirate;
365   
366   if (o_flags & ff_needisland)
367     if (!ocean)
368       badusage("need --ocean option when not using actual YPP client window"
369                " (consider supplying --pirate too)");
370   if (ocean)
371     sysassert(! setenv("YPPSC_OCEAN",ocean,1) );
372   if (pirate && (o_flags & ff_dict_pirate))
373     sysassert(! setenv("YPPSC_PIRATE",pirate,1) );
374
375   switch (o_mode & mfm_special) {
376   case 0:                                      break;
377   case mode_showcharset:  ocr_showcharsets();  exit(0);
378   default:                                     abort();
379   }
380
381   if (o_mode & mf_screenshot) {
382     open_screenshot_file(1);
383     if (o_flags & ff_singlepage) take_one_screenshot();
384     else take_screenshots();
385     progress_log("OK for you to move the mouse now, and you can"
386                  " use the YPP client again.");
387     progress("Finishing handling screenshots...");
388     gzclose(&screenshot_file,&screenshot_compressor,"screenshots output");
389   }
390   if (o_mode & mf_readscreenshot) {
391     if ((o_flags & ff_upload) && !(o_flags & ff_testservers))
392       badusage("must not reuse screenshots for upload to live databases");
393     open_screenshot_file(0);
394     if (o_flags & ff_singlepage) read_one_screenshot();
395     else read_screenshots();
396     gzclose(&screenshot_file,&screenshot_compressor,"screenshots input");
397   }
398   if (o_mode & mf_analyse) {
399     if (o_flags & ff_needisland) {
400       find_islandname();
401       if (o_flags & ff_printisland)
402         printf("%s, %s\n", archipelago, island);
403       sysassert(! setenv("YPPSC_ISLAND",island,1) );
404     }
405     if (o_outmodes[0].kind==omk_raw && o_outmodes[1].kind==omk_unset)
406       analyse(stdout);
407     else
408       run_analysis();
409   }
410   progress_log("Finished.");
411   return 0;
412 }
413
414
415
416
417 DEFINE_VWRAPPERF(, progress, )
418 DEFINE_VWRAPPERF(, progress_log, )
419 DEFINE_VWRAPPERF(, progress_spinner, )
420 DEFINE_VWRAPPERF(, warning, )
421 DEFINE_VWRAPPERF(, fatal, NORET)
422
423 static int last_progress_len;
424      
425 static void vprogress_core(int spinner, const char *fmt, va_list al) {
426   int r;
427   
428   if (o_quiet) return;
429   if (!debug_flags && !isatty(2)) return;
430   
431   if (last_progress_len)
432     putc('\r',stderr);
433
434   r= vfprintf(stderr,fmt,al);
435
436   if (spinner) {
437     putc(spinner,stderr);
438     r++;
439   }
440
441   if (r < last_progress_len) {
442     fprintf(stderr,"%*s", last_progress_len - r, "");
443     if (!r) putc('\r', stderr);
444     else while (last_progress_len-- > r) putc('\b',stderr);
445   }
446   last_progress_len= r;
447
448   if (ferror(stderr) || fflush(stderr)) _exit(16);
449 }
450    
451 void vprogress(const char *fmt, va_list al) { vprogress_core(0,fmt,al); }
452 void vprogress_spinner(const char *fmt, va_list al) {
453   static const char spinchars[]="/-\\";
454   static int spinner;
455
456   vprogress_core(spinchars[spinner],fmt,al);
457   spinner++;
458   spinner %= (sizeof(spinchars)-1);
459 }
460
461 void vprogress_log(const char *fmt, va_list al) {
462   if (o_quiet) return;
463   
464   progress("");
465   vfprintf(stderr,fmt,al);
466   putc('\n',stderr);
467   fflush(stderr);
468 }
469
470 void vwarning(const char *fmt, va_list al) {
471   progress("");
472   fputs("Warning: ",stderr);
473   vfprintf(stderr,fmt,al);
474   fputs("\n",stderr);
475   fflush(stderr);
476 }
477
478 void vfatal(const char *fmt, va_list al) {
479   progress("");
480   fputs("\n\nFatal error: ",stderr);
481   vfprintf(stderr,fmt,al);
482   fflush(stderr);
483   fputs("\n\n",stderr);
484   _exit(4);
485 }
486
487 void sysassert_fail(const char *file, int line, const char *what) {
488   int e= errno;
489   progress("");
490   fprintf(stderr,
491           "\nfatal operational error:\n"
492           " unsuccessful execution of: %s\n"
493           " %s:%d: %s\n\n",
494           what, file,line, strerror(e));
495   _exit(16);
496 }
497
498 void waitpid_check_exitstatus(pid_t pid, const char *what, int sigpipeok) { 
499   pid_t got;
500   int st;
501   for (;;) {
502     got= waitpid(pid, &st, 0);
503     if (pid==-1) { sysassert(errno==EINTR); continue; }
504     break;
505   }
506   sysassert( got==pid );
507
508   if (WIFEXITED(st)) {
509     if (WEXITSTATUS(st))
510       fatal("%s failed with nonzero exit status %d",
511             what, WEXITSTATUS(st));
512   } else if (WIFSIGNALED(st)) {
513     if (!sigpipeok || WTERMSIG(st) != SIGPIPE)
514       fatal("%s died due to signal %s%s", what,
515             strsignal(WTERMSIG(st)), WCOREDUMP(st)?" (core dumped)":"");
516   } else {
517     fatal("%s gave strange wait status %d", what, st);
518   }
519 }
520
521 char *masprintf(const char *fmt, ...) {
522   char *r;
523   va_list al;
524   va_start(al,fmt);
525   sysassert( vasprintf(&r,fmt,al) >= 0);
526   sysassert(r);
527   va_end(al);
528   return r;
529 }