+enum flags o_flags= ff_dict_fetch|ff_dict_submit|ff_dict_pirate;
+
+static void vbadusage(const char *fmt, va_list) FMT(1,0) NORET;
+static void vbadusage(const char *fmt, va_list al) {
+ fputs("bad usage: ",stderr);
+ vfprintf(stderr,fmt,al);
+ fputc('\n',stderr);
+ exit(12);
+}
+DEFINE_VWRAPPERF(static, badusage, NORET);
+
+static void open_screenshot_file(const char *mode) {
+ screenshot_file= fopen(o_screenshot_fn, mode);
+ 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 (o_flags & ff_singlepage)
+ 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");
+ fclose(tf);
+ progress_log("all complete.");
+}
+
+void fetch_with_rsync(const char *stem) {
+ pid_t fetcher;
+
+ 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", src, stem);
+ char *local= masprintf("#master-%s#.txt", stem);
+ execlp(rsync, "rsync",
+ DEBUGP(rsync) ? "-vLt" : "-Lt",
+ "--",remote,local,(char*)0);
+ sysassert(!"exec rsync failed");
+ }
+
+ waitpid_check_exitstatus(fetcher, "rsync");
+}
+
+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;
+
+ if (value[0]=='/' || (value[0]=='.' && value[1]=='/'))
+ /* absolute or relative pathname - or anyway, something with no hostname */
+ goto ok;
+
+ const char *colon= strchr(value, ':');
+ const char *slash= strchr(value, '/');
+
+ if (colon && (!slash || colon < slash))
+ /* colon before the first slash, if any */
+ /* rsync :: protocol specification - anyway, adding scheme:// won't help */
+ goto ok;
+
+ int vallen= strlen(value);
+
+ value= masprintf("%s%s%s", defprotocol, value,
+ vallen && value[vallen-1]=='/' ? "" : "/");
+
+ ok:
+ sysassert(! setenv(envname,value,1) );
+}