+DEFINE_VWRAPPERF(static, badusage, NORET);
+
+static void open_screenshot_file(int for_write) {
+ if (!fnmatch("*.gz",o_screenshot_fn,0)) {
+ int mode= for_write ? O_WRONLY|O_CREAT|O_TRUNC : O_RDONLY;
+ sysassert(! gzopen(o_screenshot_fn, mode, &screenshot_file,
+ &screenshot_compressor, "-1") );
+ } else {
+ screenshot_file= fopen(o_screenshot_fn, for_write ? "w" : "r");
+ if (!screenshot_file)
+ fatal("could not open screenshots file `%s': %s",
+ o_screenshot_fn, strerror(errno));
+ }
+}
+
+static void run_analysis(void) {
+ FILE *tf;
+
+ sysassert( tf= tmpfile() );
+ progress("running recognition...");
+ analyse(tf);
+
+ if (o_flags & ff_upload) {
+ if (npages<=1)
+ fatal("Recognition successful, but refusing to upload partial data\n"
+ " (--single-page specified). Specify an output mode?");
+ }
+
+ sysassert( fseek(tf,0,SEEK_SET) == 0);
+
+ progress_log("processing results (--%s)...", o_outmode_str);
+ pid_t processor;
+ sysassert( (processor= fork()) != -1 );
+
+ if (!processor) {
+ sysassert( dup2(fileno(tf),0) ==0 );
+ EXECLP_HELPER("commod-results-processor", o_outmode_str, (char*)0);
+ }
+
+ waitpid_check_exitstatus(processor, "output processor/uploader", 0);
+ fclose(tf);
+ progress_log("all complete.");
+}
+
+static void rsync_core(const char *stem, const char *suffix,
+ const char *zopt) {
+ pid_t fetcher;
+
+ progress("Updating dictionary %s...",stem);
+
+ sysassert( (fetcher= fork()) != -1 );
+ if (!fetcher) {
+ const char *rsync= getenv("YPPSC_PCTB_RSYNC");
+ if (!rsync) rsync= "rsync";
+
+ const char *src= getenv("YPPSC_PCTB_DICT_UPDATE");
+ char *remote= masprintf("%s/master-%s.txt%s", src, stem, suffix);
+ char *local= masprintf("_master-%s.txt%s", stem, suffix);
+ if (DEBUGP(rsync))
+ fprintf(stderr,"executing rsync to fetch %s to %s\n",remote,local);
+ char *opts= masprintf("-Lt%s%s",
+ zopt,
+ DEBUGP(rsync) ? "v" : "");
+ execlp(rsync, "rsync",opts,"--",remote,local,(char*)0);
+ sysassert(!"exec rsync failed");
+ }
+
+ waitpid_check_exitstatus(fetcher, "rsync", 0);
+}
+
+void fetch_with_rsync_gz(const char *stem) { rsync_core(stem,".gz",""); }
+void fetch_with_rsync(const char *stem) { rsync_core(stem,"","z"); }
+
+static void set_server(const char *envname, const char *defprotocol,
+ const char *defvalue, const char *defvalue_test,
+ const char *userspecified,
+ int enable) {
+ const char *value;
+
+ if (!enable) { value= "0"; goto ok; }
+
+ if (userspecified)
+ value= userspecified;
+ else if ((value= getenv(envname)))
+ ;
+ else if (o_flags & ff_testservers)
+ value= defvalue_test;
+ else
+ value= defvalue;