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= 0020,
45 mode_findwindow= 0001,
46 mode_screenshot= 0011,
53 omk_unset, omk_upload, omk_str, omk_raw, omk_none
55 static enum outmodekind o_outmode_kind;
56 static const char *o_outmode_str= 0;
58 static enum mode o_mode= mode_all;
59 static char *o_screenshot_fn;
61 static const char *o_serv_pctb, *o_serv_dict_fetch, *o_serv_dict_submit;
63 const char *o_resolver= "./dictionary-manager";
64 FILE *screenshot_file;
65 const char *o_ocean, *o_pirate;
67 enum flags o_flags= ff_dict_fetch|ff_dict_submit|ff_dict_pirate;
69 static void vbadusage(const char *fmt, va_list) FMT(1,0) NORET;
70 static void vbadusage(const char *fmt, va_list al) {
71 fputs("bad usage: ",stderr);
72 vfprintf(stderr,fmt,al);
76 DEFINE_VWRAPPERF(static, badusage, NORET);
78 static void open_screenshot_file(const char *mode) {
79 screenshot_file= fopen(o_screenshot_fn, mode);
81 fatal("could not open screenshots file `%s': %s",
82 o_screenshot_fn, strerror(errno));
85 static void run_analysis(void) {
88 sysassert( tf= tmpfile() );
89 progress("running recognition...");
92 if (o_flags & ff_upload) {
93 if (o_flags & ff_singlepage)
94 fatal("Recognition successful, but refusing to upload partial data\n"
95 " (--single-page specified). Specify an output mode?");
98 sysassert( fseek(tf,0,SEEK_SET) == 0);
100 progress_log("processing results (--%s)...", o_outmode_str);
102 sysassert( (processor= fork()) != -1 );
105 sysassert( dup2(fileno(tf),0) ==0 );
106 EXECLP_HELPER("commod-results-processor", o_outmode_str, (char*)0);
109 waitpid_check_exitstatus(processor, "output processor/uploader");
111 progress_log("all complete.");
114 void fetch_with_rsync(const char *stem) {
117 sysassert( (fetcher= fork()) != -1 );
119 const char *rsync= getenv("YPPSC_PCTB_RSYNC");
120 if (!rsync) rsync= "rsync";
122 const char *src= getenv("YPPSC_PCTB_DICT_UPDATE");
123 char *remote= masprintf("%s/master-%s.txt", src, stem);
124 char *local= masprintf("#master-%s#.txt", stem);
125 execlp(rsync, "rsync",
126 DEBUGP(rsync) ? "-vLt" : "-Lt",
127 "--",remote,local,(char*)0);
128 sysassert(!"exec rsync failed");
131 waitpid_check_exitstatus(fetcher, "rsync");
134 static void set_server(const char *envname, const char *defprotocol,
135 const char *defvalue, const char *userspecified,
139 if (!enable) { value= "0"; goto ok; }
142 value= userspecified;
143 else if ((value= getenv(envname)))
148 if (value[0]=='/' || (value[0]=='.' && value[1]=='/'))
149 /* absolute or relative pathname - or anyway, something with no hostname */
152 const char *colon= strchr(value, ':');
153 const char *slash= strchr(value, '/');
155 if (colon && (!slash || colon < slash))
156 /* colon before the first slash, if any */
157 /* rsync :: protocol specification - anyway, adding scheme:// won't help */
160 int vallen= strlen(value);
162 value= masprintf("%s%s%s", defprotocol, value,
163 vallen && value[vallen-1]=='/' ? "" : "/");
166 sysassert(! setenv(envname,value,1) );
169 int main(int argc, char **argv) {
172 sysassert( setlocale(LC_MESSAGES,"") );
173 sysassert( setlocale(LC_CTYPE,"en_GB.UTF-8") ||
174 setlocale(LC_CTYPE,"en.UTF-8") );
176 #define ARGVAL ((*++argv) ? *argv : \
177 (badusage("missing value for option %s",arg),(char*)0))
179 #define IS(s) (!strcmp(arg,(s)))
181 while ((arg=*++argv)) {
182 if (IS("--find-window-only")) o_mode= mode_findwindow;
183 else if (IS("--screenshot-only")) o_mode= mode_screenshot;
184 else if (IS("--analyse-only") ||
185 IS("--same")) o_mode= mode_analyse;
186 else if (IS("--everything")) o_mode= mode_all;
187 else if (IS("--find-island")) o_flags |= ffs_printisland;
188 else if (IS("--single-page")) o_flags |= ff_singlepage;
189 else if (IS("--quiet")) o_quiet= 1;
190 else if (IS("--edit-charset")) o_flags |= ff_editcharset;
191 else if (IS("--dict-local-only")) o_flags &= ~ffs_dict;
192 else if (IS("--dict-read-only")) o_flags &= (~ffs_dict | ff_dict_fetch);
193 else if (IS("--dict-anon")) o_flags &= ~ff_dict_pirate;
194 else if (IS("--dict-submit")) o_flags |= ff_dict_fetch|ff_dict_submit;
195 else if (IS("--raw-tsv")) o_outmode_kind= omk_raw;
196 else if (IS("--upload")) o_outmode_kind= omk_upload;
197 else if (IS("--arbitrage") ||
199 IS("--best-prices")) o_outmode_kind=omk_str,
202 else if (IS("--screenshot-file")) o_screenshot_fn= ARGVAL;
203 else if (IS("--pctb-server")) o_serv_pctb= ARGVAL;
204 else if (IS("--dict-submit-server")) o_serv_dict_submit= ARGVAL;
205 else if (IS("--dict-update-server")) o_serv_dict_fetch= ARGVAL;
206 else if (IS("--ocean")) o_ocean= ARGVAL;
207 else if (IS("--pirate")) o_pirate= ARGVAL;
209 else if (IS("-D" #f)) \
210 debug_flags |= dbg_##f;
213 else if (IS("--window-id")) {
215 unsigned long windowid= strtoul(ARGVAL,&ep,0);
216 if (*ep) badusage("invalid window id");
217 set_yppclient_window(windowid);
219 badusage("unknown option `%s'",arg);
222 /* Consequential changes to options */
224 if (o_mode & mf_analyse) {
226 o_outmode_kind= (o_flags & ff_printisland) ? omk_none : omk_str;
228 if (o_outmode_kind==omk_upload) {
229 o_flags |= ffs_upload;
230 o_outmode_str= "upload";
236 set_server("YPPSC_PCTB_PCTB",
237 "http://", "pctb.ilk.org",
238 o_serv_pctb, o_flags & (ff_needisland|ff_upload));
240 set_server("YPPSC_PCTB_DICT_UPDATE",
241 "rsync://", "rsync.pctb.chiark.greenend.org.uk/pctb",
242 o_serv_dict_fetch, o_flags & ff_dict_fetch);
244 set_server("YPPSC_PCTB_DICT_SUBMIT",
245 "http://", "dictup.pctb.chiark.greenend.org.uk",
246 o_serv_dict_submit, o_flags & ff_dict_submit);
248 if (!o_screenshot_fn)
249 o_screenshot_fn= masprintf("%s/#pages#.ppm",get_vardir());
251 /* Actually do the work */
253 if (o_mode & mf_findwindow) {
254 screenshot_startup();
255 find_yppclient_window();
257 if (!ocean) ocean= o_ocean;
258 if (!pirate) pirate= o_pirate;
260 if (o_flags & ff_needisland) {
262 badusage("need --ocean option when replaying images"
263 " (consider supplying --pirate too)");
264 sysassert(! setenv("YPPSC_OCEAN",ocean,1) );
265 if (pirate && (o_flags & ff_dict_pirate))
266 sysassert(! setenv("YPPSC_PIRATE",pirate,1) );
268 if (o_mode & mf_screenshot) {
269 open_screenshot_file("w");
270 if (o_flags & ff_singlepage) take_one_screenshot();
271 else take_screenshots();
273 if (o_mode & mf_readscreenshot) {
274 open_screenshot_file("r");
275 if (o_flags & ff_singlepage) read_one_screenshot();
276 else read_screenshots();
278 if (o_mode & mf_analyse) {
279 if (o_flags & ff_needisland) {
281 if (o_flags & ff_printisland)
282 printf("%s, %s\n", archipelago, island);
284 switch (o_outmode_kind) {
285 case omk_upload: case omk_str: run_analysis(); break;
286 case omk_raw: analyse(stdout); break;
287 case omk_none: break;
297 DEFINE_VWRAPPERF(, progress, )
298 DEFINE_VWRAPPERF(, progress_log, )
299 DEFINE_VWRAPPERF(, progress_spinner, )
300 DEFINE_VWRAPPERF(, warning, )
301 DEFINE_VWRAPPERF(, fatal, NORET)
303 static int last_progress_len;
305 static void vprogress_core(int spinner, const char *fmt, va_list al) {
309 if (!isatty(2)) return;
311 if (last_progress_len)
314 r= vfprintf(stderr,fmt,al);
317 putc(spinner,stderr);
321 if (r < last_progress_len) {
322 fprintf(stderr,"%*s", last_progress_len - r, "");
323 if (!r) putc('\r', stderr);
324 else while (last_progress_len-- > r) putc('\b',stderr);
326 last_progress_len= r;
328 if (ferror(stderr) || fflush(stderr)) _exit(16);
331 void vprogress(const char *fmt, va_list al) { vprogress_core(0,fmt,al); }
332 void vprogress_spinner(const char *fmt, va_list al) {
333 static const char spinchars[]="/-\\";
336 vprogress_core(spinchars[spinner],fmt,al);
338 spinner %= (sizeof(spinchars)-1);
341 void vprogress_log(const char *fmt, va_list al) {
345 vfprintf(stderr,fmt,al);
350 void vwarning(const char *fmt, va_list al) {
352 fputs("Warning: ",stderr);
353 vfprintf(stderr,fmt,al);
358 void vfatal(const char *fmt, va_list al) {
360 fputs("\n\nFatal error: ",stderr);
361 vfprintf(stderr,fmt,al);
363 fputs("\n\n",stderr);
367 void sysassert_fail(const char *file, int line, const char *what) {
371 "\nfatal operational error:\n"
372 " unsuccessful execution of: %s\n"
374 what, file,line, strerror(e));
378 void waitpid_check_exitstatus(pid_t pid, const char *what) {
382 got= waitpid(pid, &st, 0);
383 if (pid==-1) { sysassert(errno==EINTR); continue; }
386 sysassert( got==pid );
390 fatal("%s failed with nonzero exit status %d",
391 what, WEXITSTATUS(st));
392 } else if (WIFSIGNALED(st)) {
393 fatal("%s died due to signal %s%s", what,
394 strsignal(WTERMSIG(st)), WCOREDUMP(st)?" (core dumped)":"");
396 fatal("%s gave strange wait status %d", what, st);
400 char *masprintf(const char *fmt, ...) {
404 sysassert( vasprintf(&r,fmt,al) >= 0);