chiark / gitweb /
Much expanded and improved pctb client docs
[ypp-sc-tools.db-test.git] / pctb / convert.c
1 /*
2  * ypp-commodities main program: argument parsing etc.
3  */
4 /*
5  *  This is part of ypp-sc-tools, a set of third-party tools for assisting
6  *  players of Yohoho Puzzle Pirates.
7  * 
8  *  Copyright (C) 2009 Ian Jackson <ijackson@chiark.greenend.org.uk>
9  * 
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.
14  * 
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.
19  * 
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/>.
22  * 
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.
26  */
27
28 #include "convert.h"
29
30 void debug_flush(void) {
31   sysassert(!ferror(debug));
32   sysassert(!fflush(debug));
33 }
34
35 const char *get_vardir(void) { return "."; }
36
37 static enum {
38   mf_findwindow=      0001,
39   mf_screenshot=      0010,
40   mf_readscreenshot=  0020,
41   mf_analyse=         0100,
42   
43   mode_findwindow=    0001,
44   mode_screenshot=    0011,
45   mode_analyse=       0120,
46
47   mode_all=           0111,
48 } o_mode= mode_all;
49
50 static char *o_screenshot_fn;
51 static int o_single_page, o_quiet;
52 static const char *o_outputmode= "upload";
53
54 FILE *screenshot_file;
55
56
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);
61   fputc('\n',stderr);
62   exit(12);
63 }
64 DEFINE_VWRAPPERF(static, badusage, NORET);
65
66 static void open_screenshot_file(const char *mode) {
67   screenshot_file= fopen(o_screenshot_fn, mode);
68   if (!screenshot_file)
69     fatal("could not open screenshots file `%s': %s",
70           o_screenshot_fn, strerror(errno));
71 }
72
73 static void run_analysis(void) {
74   FILE *tf;
75
76   sysassert( tf= tmpfile() );
77   progress("running recognition...");
78   analyse(tf);
79
80   if (o_single_page && !strcmp(o_outputmode,"upload"))
81     fatal("Recognition successful, but refusing to upload partial data\n"
82           " (--single-page specified).  Specify an output mode?");
83
84   sysassert( fseek(tf,0,SEEK_SET) == 0);
85
86   progress_log("processing results (--%s)...", o_outputmode);
87   pid_t processor;
88   sysassert( (processor= fork()) != -1 );
89
90   if (!processor) {
91     sysassert( dup2(fileno(tf),0) ==0 );
92     execlp("./yppsc-commod-processor", "yppsc-commod-processor",
93            o_outputmode, (char*)0);
94     sysassert(!"execlp commod-processor failed");
95   }
96
97   waitpid_check_exitstatus(processor, "output processor/uploader");
98   fclose(tf);
99   progress_log("all complete.");
100 }  
101
102 int main(int argc, char **argv) {
103   const char *arg;
104   int r;
105
106 #define ARGVAL  ((*++argv) ? *argv : \
107                  (badusage("missing value for option %s",arg),(char*)0))
108
109   while ((arg=*++argv)) {
110     if (!strcmp(arg,"--find-window-only"))
111       o_mode= mode_findwindow;
112     else if (!strcmp(arg,"--screenshot-only"))
113       o_mode= mode_screenshot;
114     else if (!strcmp(arg,"--analyse-only") ||
115              !strcmp(arg,"--same"))
116       o_mode= mode_analyse;
117     else if (!strcmp(arg,"--everything"))
118       o_mode= mode_all;
119     else if (!strcmp(arg,"--single-page"))
120       o_single_page= 1;
121     else if (!strcmp(arg,"--quiet"))
122       o_quiet= 1;
123     else if (!strcmp(arg,"--raw-tsv"))
124       o_outputmode= 0;
125     else if (!strcmp(arg,"--upload") ||
126              !strcmp(arg,"--arbitrage") ||
127              !strcmp(arg,"--tsv") ||
128              !strcmp(arg,"--best-prices"))
129       o_outputmode= arg+2;
130     else if (!strcmp(arg,"--screenshot-file"))
131       o_screenshot_fn= ARGVAL;
132 #define DF(f)                                   \
133     else if (!strcmp(arg,"-D" #f))              \
134       debug_flags |= dbg_##f;
135     DEBUG_FLAG_LIST
136 #undef DF
137     else if (!strcmp(arg,"--window-id")) {
138       char *ep;
139       unsigned long windowid= strtoul(ARGVAL,&ep,0);
140       if (*ep) badusage("invalid window id");
141       set_yppclient_window(windowid);
142     } else
143       badusage("unknown option `%s'",arg);
144   }
145   
146   if (!o_screenshot_fn) {
147     r= asprintf(&o_screenshot_fn,"%s/#pages#.ppm",get_vardir());
148     sysassert(r>=0);
149   }
150
151   if (o_mode & mf_findwindow) {
152     screenshot_startup();
153     find_yppclient_window();
154   }
155   if (o_mode & mf_screenshot) {
156     open_screenshot_file("w");
157     if (o_single_page) take_one_screenshot();
158     else take_screenshots();
159   }
160   if (o_mode & mf_readscreenshot) {
161     open_screenshot_file("r");
162     if (o_single_page) read_one_screenshot();
163     else read_screenshots();
164   }
165   if (o_mode & mf_analyse) {
166     if (o_outputmode)
167       run_analysis();
168     else
169       analyse(stdout);
170   }
171   return 0;
172 }
173
174
175
176
177 DEFINE_VWRAPPERF(, progress, )
178 DEFINE_VWRAPPERF(, progress_log, )
179 DEFINE_VWRAPPERF(, progress_spinner, )
180 DEFINE_VWRAPPERF(, warning, )
181 DEFINE_VWRAPPERF(, fatal, NORET)
182
183 static int last_progress_len;
184      
185 static void vprogress_core(int spinner, const char *fmt, va_list al) {
186   int r;
187   
188   if (o_quiet) return;
189   if (!isatty(2)) return;
190   
191   if (last_progress_len)
192     putc('\r',stderr);
193
194   r= vfprintf(stderr,fmt,al);
195
196   if (spinner) {
197     putc(spinner,stderr);
198     r++;
199   }
200
201   if (r < last_progress_len) {
202     fprintf(stderr,"%*s", last_progress_len - r, "");
203     if (!r) putc('\r', stderr);
204     else while (last_progress_len-- > r) putc('\b',stderr);
205   }
206   last_progress_len= r;
207
208   if (ferror(stderr) || fflush(stderr)) _exit(16);
209 }
210    
211 void vprogress(const char *fmt, va_list al) { vprogress_core(0,fmt,al); }
212 void vprogress_spinner(const char *fmt, va_list al) {
213   static const char spinchars[]="/-\\";
214   static int spinner;
215
216   vprogress_core(spinchars[spinner],fmt,al);
217   spinner++;
218   spinner %= (sizeof(spinchars)-1);
219 }
220
221 void vprogress_log(const char *fmt, va_list al) {
222   if (o_quiet) return;
223   
224   progress("");
225   vfprintf(stderr,fmt,al);
226   putc('\n',stderr);
227   fflush(stderr);
228 }
229
230 void vwarning(const char *fmt, va_list al) {
231   progress("");
232   fputs("Warning: ",stderr);
233   vfprintf(stderr,fmt,al);
234   fputs("\n",stderr);
235   fflush(stderr);
236 }
237
238 void vfatal(const char *fmt, va_list al) {
239   progress("");
240   fputs("\n\nFatal error: ",stderr);
241   vfprintf(stderr,fmt,al);
242   fflush(stderr);
243   fputs("\n\n",stderr);
244   _exit(4);
245 }
246
247 void sysassert_fail(const char *file, int line, const char *what) {
248   int e= errno;
249   progress("");
250   fprintf(stderr,
251           "\nfatal operational error:\n"
252           " unsuccessful execution of: %s\n"
253           " %s:%d: %s\n\n",
254           what, file,line, strerror(e));
255   _exit(16);
256 }
257
258 void *mmalloc(size_t sz) {
259   void *r;
260   if (!sz) return 0;
261   sysassert( r= malloc(sz) );
262   return r;
263 }
264 void *mrealloc(void *p, size_t sz) {
265   assert(sz);
266   void *r;
267   sysassert( r= realloc(p,sz) );
268   return r;
269 }
270
271 void waitpid_check_exitstatus(pid_t pid, const char *what) { 
272   pid_t got;
273   int st;
274   for (;;) {
275     got= waitpid(pid, &st, 0);
276     if (pid==-1) { sysassert(errno==EINTR); continue; }
277     break;
278   }
279   sysassert( got==pid );
280
281   if (WIFEXITED(st)) {
282     if (WEXITSTATUS(st))
283       fatal("%s failed with nonzero exit status %d",
284             what, WEXITSTATUS(st));
285   } else if (WIFSIGNALED(st)) {
286     fatal("%s died due to signal %s%s", what,
287           strsignal(WTERMSIG(st)), WCOREDUMP(st)?" (core dumped)":"");
288   } else {
289     fatal("%s gave strange wait status %d", what, st);
290   }
291 }