From a5c969e80989ea2c678a99b46e7db08ae553e59d Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sun, 7 Jun 2009 20:18:38 +0100 Subject: [PATCH] Output processing. --- pctb/common.h | 2 ++ pctb/convert.c | 70 ++++++++++++++++++++++++++++++++----- pctb/convert.h | 2 +- pctb/ocr.c | 27 +++----------- pctb/structure.c | 17 ++++----- pctb/yppsc-commod-processor | 5 +-- 6 files changed, 82 insertions(+), 41 deletions(-) diff --git a/pctb/common.h b/pctb/common.h index 73e55ae..f942c72 100644 --- a/pctb/common.h +++ b/pctb/common.h @@ -110,6 +110,8 @@ void fatal(const char *fmt, ...) FMT(1,2) NORET; void sysassert_fail(const char *file, int line, const char *what) __attribute__((noreturn)); +void waitpid_check_exitstatus(pid_t pid, const char *what); + void *mmalloc(size_t sz); void *mrealloc(void *p, size_t sz); diff --git a/pctb/convert.c b/pctb/convert.c index bac41d9..291647e 100644 --- a/pctb/convert.c +++ b/pctb/convert.c @@ -49,17 +49,19 @@ static enum { static char *o_screenshots_fn; static int o_single_page, o_quiet; +static const char *o_outputmode= "upload"; FILE *screenshots_file; -static void badusage(const char *fmt, ...) - __attribute__((format(printf,1,2),noreturn)); -static void badusage(const char *fmt, ...) { - va_list al; - va_start(al,fmt); +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_screenshots_file(const char *mode) { screenshots_file= fopen(o_screenshots_fn, mode); @@ -68,6 +70,31 @@ static void open_screenshots_file(const char *mode) { o_screenshots_fn, strerror(errno)); } +static void run_analysis(void) { + FILE *tf; + + sysassert( tf= tmpfile() ); + progress("running recognition..."); + analyse(tf); + + sysassert( fseek(tf,0,SEEK_SET) == 0); + + progress_log("processing results (--%s)...", o_outputmode); + pid_t processor; + sysassert( (processor= fork()) != -1 ); + + if (!processor) { + sysassert( dup2(fileno(tf),0) ==0 ); + execlp("./yppsc-commod-processor", "yppsc-commod-processor", + o_outputmode, (char*)0); + sysassert(!"execlp commod-processor failed"); + } + + waitpid_check_exitstatus(processor, "output processor/uploader"); + fclose(tf); + progress_log("all complete."); +} + int main(int argc, char **argv) { const char *arg; int r; @@ -80,12 +107,18 @@ int main(int argc, char **argv) { o_mode= mode_findwindow; else if (!strcmp(arg,"--screenshot-only")) o_mode= mode_screenshot; - else if (!strcmp(arg,"--analyse-only")) + else if (!strcmp(arg,"--analyse-only") || + !strcmp(arg,"--same")) o_mode= mode_analyse; else if (!strcmp(arg,"--single-page")) o_single_page= 1; else if (!strcmp(arg,"--quiet")) o_quiet= 1; + else if (!strcmp(arg,"--upload") || + !strcmp(arg,"--arbitrage") || + !strcmp(arg,"--tsv") || + !strcmp(arg,"--best-prices")) + o_outputmode= arg+2; else if (!strcmp(arg,"--screenshots-file")) o_screenshots_fn= ARGVAL; #define DF(f) \ @@ -122,8 +155,7 @@ int main(int argc, char **argv) { else read_screenshots(); } if (o_mode & mf_analyse) { - analyse(); - //output_tsv(); + run_analysis(); } return 0; } @@ -224,3 +256,25 @@ void *mrealloc(void *p, size_t sz) { sysassert( r= realloc(p,sz) ); return r; } + +void waitpid_check_exitstatus(pid_t pid, const char *what) { + pid_t got; + int st; + for (;;) { + got= waitpid(pid, &st, 0); + if (pid==-1) { sysassert(errno==EINTR); continue; } + break; + } + sysassert( got==pid ); + + if (WIFEXITED(st)) { + if (WEXITSTATUS(st)) + fatal("%s failed with nonzero exit status %d", + what, WEXITSTATUS(st)); + } else if (WIFSIGNALED(st)) { + fatal("%s died due to signal %s%s", what, + strsignal(WTERMSIG(st)), WCOREDUMP(st)?" (core dumped)":""); + } else { + fatal("%s gave strange wait status %d", what, st); + } +} diff --git a/pctb/convert.h b/pctb/convert.h index 15ec06d..90e5e24 100644 --- a/pctb/convert.h +++ b/pctb/convert.h @@ -43,7 +43,7 @@ void find_structure(CanonImage *im); void read_screenshots(void); void read_one_screenshot(void); -void analyse(void); +void analyse(FILE *tsv_output); /*----- from convert.c -----*/ diff --git a/pctb/ocr.c b/pctb/ocr.c index d12218f..6a91935 100644 --- a/pctb/ocr.c +++ b/pctb/ocr.c @@ -245,7 +245,7 @@ static void callout_unknown(OcrReader *rd, int w, Pixcol cols[], DEBUGP(callout) ? "--debug" : "--noop-arg", "--automatic-1", (char*)0); - sysassert(!"execlp failed"); + sysassert(!"execlp ocr-resolver failed"); } sysassert(! close(jobpipe[0]) ); sysassert(! close(donepipe[1]) ); @@ -295,27 +295,10 @@ static void callout_unknown(OcrReader *rd, int w, Pixcol cols[], } if (r==0) { - pid_t pid; - int st; - for (;;) { - pid= waitpid(resolver_pid, &st, 0); - if (pid==-1) { sysassert(errno==EINTR); continue; } - break; - } - sysassert(pid==resolver_pid); - if (WIFEXITED(st)) { - if (WEXITSTATUS(st)) - fatal("character resolver failed with nonzero exit status %d", - WEXITSTATUS(st)); - fclose(resolver); - close(resolver_done); - resolver= 0; - } else if (WIFSIGNALED(st)) { - fatal("character resolver died due to signal %s%s", - strsignal(WTERMSIG(st)), WCOREDUMP(st)?" (core dumped)":""); - } else { - fatal("character resolver gave strange wait status %d",st); - } + waitpid_check_exitstatus(resolver_pid, "character resolver"); + fclose(resolver); + close(resolver_done); + resolver= 0; } else { assert(r==1); sysassert(cb==0); diff --git a/pctb/structure.c b/pctb/structure.c index 8919e0b..8141feb 100644 --- a/pctb/structure.c +++ b/pctb/structure.c @@ -339,7 +339,7 @@ static void find_table_entry(Rect commod, int colno, Rect *cellr) { require_rectangle_r(*cellr, " o"); } -static void ocr_rectangle(Rect r, const OcrCellType ct) { +static void ocr_rectangle(Rect r, const OcrCellType ct, FILE *tsv_output) { OcrResultGlyph *results, *res; int w= r.br.x - r.tl.x + 1; @@ -364,10 +364,10 @@ static void ocr_rectangle(Rect r, const OcrCellType ct) { results= ocr(rd,ct,w,cols); for (res=results; res->s; res++) - printf("%s",res->s); + fputs(res->s,tsv_output); } -void analyse(void) { +void analyse(FILE *tsv_output) { Rect thisr, entryr; int page, tryrect, colno; @@ -386,16 +386,17 @@ void analyse(void) { const char *tab= ""; for (colno=0; colno