chiark / gitweb /
Compress the screenshots file on output
[ypp-sc-tools.main.git] / pctb / convert.c
1 /*
2  * ypp-commodities 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 mode {
40   mf_findwindow=      00001,
41   mf_screenshot=      00010,
42   mf_readscreenshot=  00020,
43   mf_analyse=         00100,
44   mfm_special=        07000,
45   
46   mode_findwindow=    00001,
47   mode_screenshot=    00011,
48   mode_analyse=       00120,
49   mode_showcharset=   01000,
50
51   mode_all=           00111,
52 };
53
54 enum outmodekind {
55   omk_unset, omk_upload, omk_str, omk_raw, omk_none
56 };
57 static enum outmodekind o_outmode_kind;
58 static const char *o_outmode_str= 0;
59
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;
63
64 const char *o_resolver= "./dictionary-manager";
65 FILE *screenshot_file;
66 const char *o_ocean, *o_pirate;
67 int o_quiet;
68
69 static pid_t screenshot_compressor=-1;
70
71 enum flags o_flags=
72    ff_charset_allowedit |
73    ff_dict_fetch|ff_dict_submit|ff_dict_pirate;
74
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);
79   fputc('\n',stderr);
80   exit(12);
81 }
82 DEFINE_VWRAPPERF(static, badusage, NORET);
83
84 static void open_screenshot_file(const char *mode) {
85   if (!fnmatch("*.gz",o_screenshot_fn,0)) {
86     int zfd, pipefds[2];
87     sysassert( (zfd= open(o_screenshot_fn, O_WRONLY|O_CREAT|O_TRUNC,
88                           0666)) >= 0);
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");
99     }
100     sysassert(! close(zfd) );
101     sysassert(! close(pipefds[0]) );
102     sysassert( screenshot_file= fdopen(pipefds[1], "w") );
103
104   } else {
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));
109   }
110 }
111
112 static void run_analysis(void) {
113   FILE *tf;
114
115   sysassert( tf= tmpfile() );
116   progress("running recognition...");
117   analyse(tf);
118
119   if (o_flags & ff_upload) {
120     if (npages<=1)
121       fatal("Recognition successful, but refusing to upload partial data\n"
122             " (--single-page specified).  Specify an output mode?");
123   }
124
125   sysassert( fseek(tf,0,SEEK_SET) == 0);
126
127   progress_log("processing results (--%s)...", o_outmode_str);
128   pid_t processor;
129   sysassert( (processor= fork()) != -1 );
130
131   if (!processor) {
132     sysassert( dup2(fileno(tf),0) ==0 );
133     EXECLP_HELPER("commod-results-processor", o_outmode_str, (char*)0);
134   }
135
136   waitpid_check_exitstatus(processor, "output processor/uploader", 0);
137   fclose(tf);
138   progress_log("all complete.");
139 }
140
141 static void rsync_core(const char *stem, const char *suffix,
142                        const char *zopt) {
143   pid_t fetcher;
144
145   progress("Updating dictionary %s...",stem);
146
147   sysassert( (fetcher= fork()) != -1 );
148   if (!fetcher) {
149     const char *rsync= getenv("YPPSC_PCTB_RSYNC");
150     if (!rsync) rsync= "rsync";
151   
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);
155     if (DEBUGP(rsync))
156       fprintf(stderr,"executing rsync to fetch %s to %s\n",remote,local);
157     char *opts= masprintf("-Lt%s%s",
158                           zopt,
159                           DEBUGP(rsync) ? "v" : "");
160     execlp(rsync, "rsync",opts,"--",remote,local,(char*)0);
161     sysassert(!"exec rsync failed");
162   }
163
164   waitpid_check_exitstatus(fetcher, "rsync", 0);
165 }
166
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"); }
169
170 static void set_server(const char *envname, const char *defprotocol,
171                        const char *defvalue, const char *defvalue_test,
172                        const char *userspecified,
173                        int enable) {
174   const char *value;
175   
176   if (!enable) { value= "0"; goto ok; }
177
178   if (userspecified)
179     value= userspecified;
180   else if ((value= getenv(envname)))
181     ;
182   else if (o_flags & ff_testservers)
183     value= defvalue_test;
184   else
185     value= defvalue;
186
187   if (value[0]=='/' || (value[0]=='.' && value[1]=='/'))
188     /* absolute or relative pathname - or anyway, something with no hostname */
189     goto ok;
190
191   const char *colon= strchr(value, ':');
192   const char *slash= strchr(value, '/');
193
194   if (colon && (!slash || colon < slash))
195     /* colon before the first slash, if any */
196     /* rsync :: protocol specification - anyway, adding scheme:// won't help */
197     goto ok;
198
199   int vallen= strlen(value);
200
201   value= masprintf("%s%s%s", defprotocol, value,
202                    vallen && value[vallen-1]=='/' ? "" : "/");
203
204  ok:
205   sysassert(! setenv(envname,value,1) );
206 }
207
208 int main(int argc, char **argv) {
209   const char *arg;
210
211   sysassert( setlocale(LC_MESSAGES,"") );
212   sysassert( setlocale(LC_CTYPE,"en_GB.UTF-8") ||
213              setlocale(LC_CTYPE,"en.UTF-8") );
214
215 #define ARGVAL  ((*++argv) ? *argv : \
216                  (badusage("missing value for option %s",arg),(char*)0))
217
218 #define IS(s) (!strcmp(arg,(s)))
219
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") ||
240              IS("--tsv") ||
241              IS("--best-prices"))      o_outmode_kind=omk_str,
242                                        o_outmode_str=arg+2;
243
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;
251 #define DF(f)                                   \
252     else if (IS("-D" #f))                       \
253       debug_flags |= dbg_##f;
254     DEBUG_FLAG_LIST
255 #undef DF
256     else if (IS("--window-id")) {
257       char *ep;
258       unsigned long windowid= strtoul(ARGVAL,&ep,0);
259       if (*ep) badusage("invalid window id");
260       set_yppclient_window(windowid);
261     } else
262       badusage("unknown option `%s'",arg);
263   }
264
265   /* Consequential changes to options */
266
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;
272       } else {
273         o_outmode_kind= omk_upload;
274       }
275     }
276
277     if (o_outmode_kind==omk_upload) {
278       o_flags |= ffs_upload;
279       o_outmode_str= "upload";
280     }
281   }
282
283   /* Defaults */
284   
285   set_server("YPPSC_PCTB_PCTB",
286              "http://",          "pctb.ilk.org" /*pctb.crabdance.com*/,
287                                  "pctb.ilk.org",
288              o_serv_pctb,        o_flags & (ff_needisland|ff_upload));
289              
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);
294
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);
299
300   if (!o_screenshot_fn)
301     o_screenshot_fn= masprintf("%s/_pages.ppm%s", get_vardir(),
302                                o_mode & mf_readscreenshot ? "" : ".gz");
303
304   /* Actually do the work */
305              
306   canon_colour_prepare();
307   
308   if (o_mode & mf_findwindow) {
309     screenshot_startup();
310     find_yppclient_window();
311   }
312   if (!ocean)  ocean=  o_ocean;
313   if (!pirate) pirate= o_pirate;
314   
315   if (o_flags & ff_needisland)
316     if (!ocean)
317       badusage("need --ocean option when not using actual YPP client window"
318                " (consider supplying --pirate too)");
319   if (ocean)
320     sysassert(! setenv("YPPSC_OCEAN",ocean,1) );
321   if (pirate && (o_flags & ff_dict_pirate))
322     sysassert(! setenv("YPPSC_PIRATE",pirate,1) );
323
324   switch (o_mode & mfm_special) {
325   case 0:                                      break;
326   case mode_showcharset:  ocr_showcharsets();  exit(0);
327   default:                                     abort();
328   }
329
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.");
336   }
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();
343   }
344   if (o_mode & mf_analyse) {
345     if (o_flags & ff_needisland) {
346       find_islandname();
347       if (o_flags & ff_printisland)
348         printf("%s, %s\n", archipelago, island);
349       sysassert(! setenv("YPPSC_ISLAND",island,1) );
350     }
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;
355     default: abort();
356     }
357   }
358   if (screenshot_compressor!=-1) {
359     progress("Finishing compressing screenshots...");
360     waitpid_check_exitstatus(screenshot_compressor,"screenshots gzip",0);
361   }
362   progress_log("Finished.");
363   return 0;
364 }
365
366
367
368
369 DEFINE_VWRAPPERF(, progress, )
370 DEFINE_VWRAPPERF(, progress_log, )
371 DEFINE_VWRAPPERF(, progress_spinner, )
372 DEFINE_VWRAPPERF(, warning, )
373 DEFINE_VWRAPPERF(, fatal, NORET)
374
375 static int last_progress_len;
376      
377 static void vprogress_core(int spinner, const char *fmt, va_list al) {
378   int r;
379   
380   if (o_quiet) return;
381   if (!isatty(2)) return;
382   
383   if (last_progress_len)
384     putc('\r',stderr);
385
386   r= vfprintf(stderr,fmt,al);
387
388   if (spinner) {
389     putc(spinner,stderr);
390     r++;
391   }
392
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);
397   }
398   last_progress_len= r;
399
400   if (ferror(stderr) || fflush(stderr)) _exit(16);
401 }
402    
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[]="/-\\";
406   static int spinner;
407
408   vprogress_core(spinchars[spinner],fmt,al);
409   spinner++;
410   spinner %= (sizeof(spinchars)-1);
411 }
412
413 void vprogress_log(const char *fmt, va_list al) {
414   if (o_quiet) return;
415   
416   progress("");
417   vfprintf(stderr,fmt,al);
418   putc('\n',stderr);
419   fflush(stderr);
420 }
421
422 void vwarning(const char *fmt, va_list al) {
423   progress("");
424   fputs("Warning: ",stderr);
425   vfprintf(stderr,fmt,al);
426   fputs("\n",stderr);
427   fflush(stderr);
428 }
429
430 void vfatal(const char *fmt, va_list al) {
431   progress("");
432   fputs("\n\nFatal error: ",stderr);
433   vfprintf(stderr,fmt,al);
434   fflush(stderr);
435   fputs("\n\n",stderr);
436   _exit(4);
437 }
438
439 void sysassert_fail(const char *file, int line, const char *what) {
440   int e= errno;
441   progress("");
442   fprintf(stderr,
443           "\nfatal operational error:\n"
444           " unsuccessful execution of: %s\n"
445           " %s:%d: %s\n\n",
446           what, file,line, strerror(e));
447   _exit(16);
448 }
449
450 void waitpid_check_exitstatus(pid_t pid, const char *what, int sigpipeok) { 
451   pid_t got;
452   int st;
453   for (;;) {
454     got= waitpid(pid, &st, 0);
455     if (pid==-1) { sysassert(errno==EINTR); continue; }
456     break;
457   }
458   sysassert( got==pid );
459
460   if (WIFEXITED(st)) {
461     if (WEXITSTATUS(st))
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)":"");
468   } else {
469     fatal("%s gave strange wait status %d", what, st);
470   }
471 }
472
473 char *masprintf(const char *fmt, ...) {
474   char *r;
475   va_list al;
476   va_start(al,fmt);
477   sysassert( vasprintf(&r,fmt,al) >= 0);
478   sysassert(r);
479   va_end(al);
480   return r;
481 }