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 FILE *screenshot_file;
57 static void vbadusage(const char *fmt, va_list) FMT(1,0) NORET;
58 static void vbadusage(const char *fmt, va_list al) {
59 fputs("bad usage: ",stderr);
60 vfprintf(stderr,fmt,al);
64 DEFINE_VWRAPPERF(static, badusage, NORET);
66 static void open_screenshot_file(const char *mode) {
67 screenshot_file= fopen(o_screenshot_fn, mode);
69 fatal("could not open screenshots file `%s': %s",
70 o_screenshot_fn, strerror(errno));
73 static void run_analysis(void) {
76 sysassert( tf= tmpfile() );
77 progress("running recognition...");
80 sysassert( fseek(tf,0,SEEK_SET) == 0);
82 progress_log("processing results (--%s)...", o_outputmode);
84 sysassert( (processor= fork()) != -1 );
87 sysassert( dup2(fileno(tf),0) ==0 );
88 execlp("./yppsc-commod-processor", "yppsc-commod-processor",
89 o_outputmode, (char*)0);
90 sysassert(!"execlp commod-processor failed");
93 waitpid_check_exitstatus(processor, "output processor/uploader");
95 progress_log("all complete.");
98 int main(int argc, char **argv) {
102 #define ARGVAL ((*++argv) ? *argv : \
103 (badusage("missing value for option %s",arg),(char*)0))
105 while ((arg=*++argv)) {
106 if (!strcmp(arg,"--find-window-only"))
107 o_mode= mode_findwindow;
108 else if (!strcmp(arg,"--screenshot-only"))
109 o_mode= mode_screenshot;
110 else if (!strcmp(arg,"--analyse-only") ||
111 !strcmp(arg,"--same"))
112 o_mode= mode_analyse;
113 else if (!strcmp(arg,"--everything"))
115 else if (!strcmp(arg,"--single-page"))
117 else if (!strcmp(arg,"--quiet"))
119 else if (!strcmp(arg,"--raw-tsv"))
121 else if (!strcmp(arg,"--upload") ||
122 !strcmp(arg,"--arbitrage") ||
123 !strcmp(arg,"--tsv") ||
124 !strcmp(arg,"--best-prices"))
126 else if (!strcmp(arg,"--screenshot-file"))
127 o_screenshot_fn= ARGVAL;
129 else if (!strcmp(arg,"-D" #f)) \
130 debug_flags |= dbg_##f;
133 else if (!strcmp(arg,"--window-id")) {
135 unsigned long windowid= strtoul(ARGVAL,&ep,0);
136 if (*ep) badusage("invalid window id");
137 set_yppclient_window(windowid);
139 badusage("unknown option `%s'",arg);
142 if (!o_screenshot_fn) {
143 r= asprintf(&o_screenshot_fn,"%s/#pages#.ppm",get_vardir());
147 if (o_mode & mf_findwindow) {
148 screenshot_startup();
149 find_yppclient_window();
151 if (o_mode & mf_screenshot) {
152 open_screenshot_file("w");
153 if (o_single_page) take_one_screenshot();
154 else take_screenshots();
156 if (o_mode & mf_readscreenshot) {
157 open_screenshot_file("r");
158 if (o_single_page) read_one_screenshot();
159 else read_screenshots();
161 if (o_mode & mf_analyse) {
173 DEFINE_VWRAPPERF(, progress, )
174 DEFINE_VWRAPPERF(, progress_log, )
175 DEFINE_VWRAPPERF(, progress_spinner, )
176 DEFINE_VWRAPPERF(, warning, )
177 DEFINE_VWRAPPERF(, fatal, NORET)
179 static int last_progress_len;
181 static void vprogress_core(int spinner, const char *fmt, va_list al) {
185 if (!isatty(2)) return;
187 if (last_progress_len)
190 r= vfprintf(stderr,fmt,al);
193 putc(spinner,stderr);
197 if (r < last_progress_len) {
198 fprintf(stderr,"%*s", last_progress_len - r, "");
199 if (!r) putc('\r', stderr);
200 else while (last_progress_len-- > r) putc('\b',stderr);
202 last_progress_len= r;
204 if (ferror(stderr) || fflush(stderr)) _exit(16);
207 void vprogress(const char *fmt, va_list al) { vprogress_core(0,fmt,al); }
208 void vprogress_spinner(const char *fmt, va_list al) {
209 static const char spinchars[]="/-\\";
212 vprogress_core(spinchars[spinner],fmt,al);
214 spinner %= (sizeof(spinchars)-1);
217 void vprogress_log(const char *fmt, va_list al) {
221 vfprintf(stderr,fmt,al);
226 void vwarning(const char *fmt, va_list al) {
228 fputs("Warning: ",stderr);
229 vfprintf(stderr,fmt,al);
234 void vfatal(const char *fmt, va_list al) {
236 fputs("\n\nFatal error: ",stderr);
237 vfprintf(stderr,fmt,al);
239 fputs("\n\n",stderr);
243 void sysassert_fail(const char *file, int line, const char *what) {
247 "\nfatal operational error:\n"
248 " unsuccessful execution of: %s\n"
250 what, file,line, strerror(e));
254 void *mmalloc(size_t sz) {
257 sysassert( r= malloc(sz) );
260 void *mrealloc(void *p, size_t sz) {
263 sysassert( r= realloc(p,sz) );
267 void waitpid_check_exitstatus(pid_t pid, const char *what) {
271 got= waitpid(pid, &st, 0);
272 if (pid==-1) { sysassert(errno==EINTR); continue; }
275 sysassert( got==pid );
279 fatal("%s failed with nonzero exit status %d",
280 what, WEXITSTATUS(st));
281 } else if (WIFSIGNALED(st)) {
282 fatal("%s died due to signal %s%s", what,
283 strsignal(WTERMSIG(st)), WCOREDUMP(st)?" (core dumped)":"");
285 fatal("%s gave strange wait status %d", what, st);