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 "."; }
40 mf_readscreenshot= 0020,
43 mode_findwindow= 0001,
44 mode_screenshot= 0011,
50 static char *o_screenshot_fn;
51 static int o_single_page, o_quiet;
52 static const char *o_outputmode= "upload";
54 const char *o_resolver;
55 FILE *screenshot_file;
58 static void vbadusage(const char *fmt, va_list) FMT(1,0) NORET;
59 static void vbadusage(const char *fmt, va_list al) {
60 fputs("bad usage: ",stderr);
61 vfprintf(stderr,fmt,al);
65 DEFINE_VWRAPPERF(static, badusage, NORET);
67 static void open_screenshot_file(const char *mode) {
68 screenshot_file= fopen(o_screenshot_fn, mode);
70 fatal("could not open screenshots file `%s': %s",
71 o_screenshot_fn, strerror(errno));
74 static void run_analysis(void) {
77 sysassert( tf= tmpfile() );
78 progress("running recognition...");
81 if (o_single_page && !strcmp(o_outputmode,"upload"))
82 fatal("Recognition successful, but refusing to upload partial data\n"
83 " (--single-page specified). Specify an output mode?");
85 sysassert( fseek(tf,0,SEEK_SET) == 0);
87 progress_log("processing results (--%s)...", o_outputmode);
89 sysassert( (processor= fork()) != -1 );
92 sysassert( dup2(fileno(tf),0) ==0 );
93 execlp("./yppsc-commod-processor", "yppsc-commod-processor",
94 o_outputmode, (char*)0);
95 sysassert(!"execlp commod-processor failed");
98 waitpid_check_exitstatus(processor, "output processor/uploader");
100 progress_log("all complete.");
103 int main(int argc, char **argv) {
107 #define ARGVAL ((*++argv) ? *argv : \
108 (badusage("missing value for option %s",arg),(char*)0))
110 while ((arg=*++argv)) {
111 if (!strcmp(arg,"--find-window-only"))
112 o_mode= mode_findwindow;
113 else if (!strcmp(arg,"--screenshot-only"))
114 o_mode= mode_screenshot;
115 else if (!strcmp(arg,"--analyse-only") ||
116 !strcmp(arg,"--same"))
117 o_mode= mode_analyse;
118 else if (!strcmp(arg,"--everything"))
120 else if (!strcmp(arg,"--single-page"))
122 else if (!strcmp(arg,"--quiet"))
124 else if (!strcmp(arg,"--edit-charset"))
125 o_resolver= "./yppsc-ocr-resolver";
126 else if (!strcmp(arg,"--raw-tsv"))
128 else if (!strcmp(arg,"--upload") ||
129 !strcmp(arg,"--arbitrage") ||
130 !strcmp(arg,"--tsv") ||
131 !strcmp(arg,"--best-prices"))
133 else if (!strcmp(arg,"--screenshot-file"))
134 o_screenshot_fn= ARGVAL;
136 else if (!strcmp(arg,"-D" #f)) \
137 debug_flags |= dbg_##f;
140 else if (!strcmp(arg,"--window-id")) {
142 unsigned long windowid= strtoul(ARGVAL,&ep,0);
143 if (*ep) badusage("invalid window id");
144 set_yppclient_window(windowid);
146 badusage("unknown option `%s'",arg);
149 if (!o_screenshot_fn) {
150 r= asprintf(&o_screenshot_fn,"%s/#pages#.ppm",get_vardir());
154 if (o_mode & mf_findwindow) {
155 screenshot_startup();
156 find_yppclient_window();
158 if (o_mode & mf_screenshot) {
159 open_screenshot_file("w");
160 if (o_single_page) take_one_screenshot();
161 else take_screenshots();
163 if (o_mode & mf_readscreenshot) {
164 open_screenshot_file("r");
165 if (o_single_page) read_one_screenshot();
166 else read_screenshots();
168 if (o_mode & mf_analyse) {
181 DEFINE_VWRAPPERF(, progress, )
182 DEFINE_VWRAPPERF(, progress_log, )
183 DEFINE_VWRAPPERF(, progress_spinner, )
184 DEFINE_VWRAPPERF(, warning, )
185 DEFINE_VWRAPPERF(, fatal, NORET)
187 static int last_progress_len;
189 static void vprogress_core(int spinner, const char *fmt, va_list al) {
193 if (!isatty(2)) return;
195 if (last_progress_len)
198 r= vfprintf(stderr,fmt,al);
201 putc(spinner,stderr);
205 if (r < last_progress_len) {
206 fprintf(stderr,"%*s", last_progress_len - r, "");
207 if (!r) putc('\r', stderr);
208 else while (last_progress_len-- > r) putc('\b',stderr);
210 last_progress_len= r;
212 if (ferror(stderr) || fflush(stderr)) _exit(16);
215 void vprogress(const char *fmt, va_list al) { vprogress_core(0,fmt,al); }
216 void vprogress_spinner(const char *fmt, va_list al) {
217 static const char spinchars[]="/-\\";
220 vprogress_core(spinchars[spinner],fmt,al);
222 spinner %= (sizeof(spinchars)-1);
225 void vprogress_log(const char *fmt, va_list al) {
229 vfprintf(stderr,fmt,al);
234 void vwarning(const char *fmt, va_list al) {
236 fputs("Warning: ",stderr);
237 vfprintf(stderr,fmt,al);
242 void vfatal(const char *fmt, va_list al) {
244 fputs("\n\nFatal error: ",stderr);
245 vfprintf(stderr,fmt,al);
247 fputs("\n\n",stderr);
251 void sysassert_fail(const char *file, int line, const char *what) {
255 "\nfatal operational error:\n"
256 " unsuccessful execution of: %s\n"
258 what, file,line, strerror(e));
262 void waitpid_check_exitstatus(pid_t pid, const char *what) {
266 got= waitpid(pid, &st, 0);
267 if (pid==-1) { sysassert(errno==EINTR); continue; }
270 sysassert( got==pid );
274 fatal("%s failed with nonzero exit status %d",
275 what, WEXITSTATUS(st));
276 } else if (WIFSIGNALED(st)) {
277 fatal("%s died due to signal %s%s", what,
278 strsignal(WTERMSIG(st)), WCOREDUMP(st)?" (core dumped)":"");
280 fatal("%s gave strange wait status %d", what, st);