chiark / gitweb /
pctb.ilk.org was just down
[ypp-sc-tools.db-test.git] / yarrg / convert.c
index adccb1cfdae6e52acf51fa6243600cc3fbaeb9f9..ce3ad0b49619f5517e9a5f0384d69535921b0fc0 100644 (file)
 
 #include "convert.h"
 
-void debug_flush(void) {
-  sysassert(!ferror(debug));
-  sysassert(!fflush(debug));
-}
-
 const char *get_vardir(void) { return "."; }
 const char *get_libdir(void) { return "."; }
 
@@ -94,7 +89,7 @@ static void run_analysis(void) {
   analyse(tf);
 
   if (o_flags & ff_upload) {
-    if (npages<=1)
+    if (o_flags & ff_singlepage)
       fatal("Recognition successful, but refusing to upload partial data\n"
            " (--single-page specified).  Specify an output mode?");
   }
@@ -262,6 +257,7 @@ int main(int argc, char **argv) {
     else if (IS("--dict-read-only"))   o_flags &= (~ffs_dict | ff_dict_fetch);
     else if (IS("--dict-anon"))        o_flags &= ~ff_dict_pirate;
     else if (IS("--dict-submit"))      o_flags |= ff_dict_fetch|ff_dict_submit;
+    else if (IS("--dict-no-update"))   o_flags &= ~ff_dict_fetch; // testing
     else if (IS("--raw-tsv"))          outputmode(omk_raw,0);
     else if (IS("--upload"))           outputmode_uploads();
     else if (IS("--upload-yarrg"))     outputmode(omk_upload_yarrg,arg+2);
@@ -331,7 +327,7 @@ int main(int argc, char **argv) {
             o_serv_yarrg,       o_flags & ff_use_yarrg);
 
   set_server("YPPSC_YARRG_PCTB",
-            "http://",          "pctb.ilk.org" /*pctb.crabdance.com*/,
+            "http://",          "pctb.crabdance.com",
                                 "pctb.ilk.org",
             o_serv_pctb,        o_flags & ff_use_pctb);
             
@@ -411,118 +407,128 @@ int main(int argc, char **argv) {
 }
 
 
+FILE *dbfile;
+static const char *basepath; /* as passed in by caller */
+static pid_t dbzcat;
 
+int dbfile_gzopen(const char *basepath_spec) {
+  assert(!dbfile);
 
-DEFINE_VWRAPPERF(, progress, )
-DEFINE_VWRAPPERF(, progress_log, )
-DEFINE_VWRAPPERF(, progress_spinner, )
-DEFINE_VWRAPPERF(, warning, )
-DEFINE_VWRAPPERF(, fatal, NORET)
+  basepath= basepath_spec;
 
-static int last_progress_len;
-     
-static void vprogress_core(int spinner, const char *fmt, va_list al) {
-  int r;
-  
-  if (o_quiet) return;
-  if (!debug_flags && !isatty(2)) return;
+  char *zpath= masprintf("%s.gz", basepath);
+  int e= gzopen(zpath, O_RDONLY, &dbfile, &dbzcat, 0);
+  free(zpath);
+  if (e) { errno=e; sysassert(errno==ENOENT); return 0; }
   
-  if (last_progress_len)
-    putc('\r',stderr);
+  return 1;
+}  
 
-  r= vfprintf(stderr,fmt,al);
+int dbfile_open(const char *tpath) {
+  assert(!dbfile);
 
-  if (spinner) {
-    putc(spinner,stderr);
-    r++;
-  }
+  basepath= tpath;
 
-  if (r < last_progress_len) {
-    fprintf(stderr,"%*s", last_progress_len - r, "");
-    if (!r) putc('\r', stderr);
-    else while (last_progress_len-- > r) putc('\b',stderr);
-  }
-  last_progress_len= r;
+  dbzcat= -1;
+  dbfile= fopen(tpath,"r");
+  if (!dbfile) { sysassert(errno==ENOENT); return 0; }
+  return 1;
+}  
 
-  if (ferror(stderr) || fflush(stderr)) _exit(16);
-}
-   
-void vprogress(const char *fmt, va_list al) { vprogress_core(0,fmt,al); }
-void vprogress_spinner(const char *fmt, va_list al) {
-  static const char spinchars[]="/-\\";
-  static int spinner;
-
-  vprogress_core(spinchars[spinner],fmt,al);
-  spinner++;
-  spinner %= (sizeof(spinchars)-1);
+void dbfile_close(void) {
+  gzclose(&dbfile, &dbzcat, basepath);
 }
 
-void vprogress_log(const char *fmt, va_list al) {
-  if (o_quiet) return;
-  
-  progress("");
-  vfprintf(stderr,fmt,al);
-  putc('\n',stderr);
-  fflush(stderr);
+#define dbassertgl(x) ((x) ? (void)0 : dbfile_assertfail(file,line,#x))
+
+void dbfile_getsline(char *lbuf, size_t lbufsz, const char *file, int line) {
+  errno=0;
+  char *s= fgets(lbuf,lbufsz,dbfile);
+  sysassert(!ferror(dbfile));
+  dbassertgl(!feof(dbfile));
+  assert(s);
+  int l= strlen(lbuf);
+  dbassertgl(l>0);  dbassertgl(lbuf[--l]=='\n');
+  lbuf[l]= 0;
 }
 
-void vwarning(const char *fmt, va_list al) {
-  progress("");
-  fputs("Warning: ",stderr);
-  vfprintf(stderr,fmt,al);
-  fputs("\n",stderr);
-  fflush(stderr);
+int dbfile_vscanf(const char *fmt, va_list al) {
+  int r= vfscanf(dbfile,fmt,al);
+  sysassert(!ferror(dbfile));
+  return r;
 }
 
-void vfatal(const char *fmt, va_list al) {
-  progress("");
-  fputs("\n\nFatal error: ",stderr);
-  vfprintf(stderr,fmt,al);
-  fflush(stderr);
-  fputs("\n\n",stderr);
-  _exit(4);
+int dbfile_scanf(const char *fmt, ...) {
+  va_list al;
+  va_start(al,fmt);
+  int r= dbfile_vscanf(fmt,al);
+  va_end(al);
+  return r;
 }
 
-void sysassert_fail(const char *file, int line, const char *what) {
-  int e= errno;
-  progress("");
-  fprintf(stderr,
-         "\nfatal operational error:\n"
-         " unsuccessful execution of: %s\n"
-         " %s:%d: %s\n\n",
-         what, file,line, strerror(e));
-  _exit(16);
+void dbfile_assertfail(const char *file, int line, const char *m) {
+  if (dbzcat)
+    fatal("Error in dictionary file %s.gz:\n"
+         " Requirement not met at %s:%d:\n"
+         " %s",
+         basepath, file,line, m);
+  else if (dbfile)
+    fatal("Error in dictionary file %s at byte %ld:\n"
+         " Requirement not met at %s:%d:\n"
+         " %s",
+         basepath,(long)ftell(dbfile), file,line, m);
+  else
+    fatal("Semantic error in dictionaries:\n"
+         " Requirement not met at %s:%d:\n"
+         " %s",
+         file,line, m);
 }
 
-void waitpid_check_exitstatus(pid_t pid, const char *what, int sigpipeok) { 
-  pid_t got;
-  int st;
-  for (;;) {
-    got= waitpid(pid, &st, 0);
-    if (pid==-1) { sysassert(errno==EINTR); continue; }
-    break;
+int gzopen(const char *zpath, int oflags, FILE **f_r, pid_t *pid_r,
+          const char *gziplevel /* 0 for read; may be 0, or "-1" etc. */) {
+
+  int zfd= open(zpath, oflags, 0666);
+  if (zfd<0) return errno;
+
+  int pipefds[2];
+  sysassert(! pipe(pipefds) );
+
+  int oi,io; const char *cmd; const char *stdiomode;
+  switch ((oflags & O_ACCMODE)) {
+  case O_RDONLY: oi=0; io=1; cmd="gunzip"; stdiomode="r"; break;
+  case O_WRONLY: oi=1; io=0; cmd="gzip";   stdiomode="w"; break;
+  default: abort();
   }
-  sysassert( got==pid );
-
-  if (WIFEXITED(st)) {
-    if (WEXITSTATUS(st))
-      fatal("%s failed with nonzero exit status %d",
-           what, WEXITSTATUS(st));
-  } else if (WIFSIGNALED(st)) {
-    if (!sigpipeok || WTERMSIG(st) != SIGPIPE)
-      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);
+
+  sysassert( (*pid_r=fork()) != -1 );
+  if (!*pid_r) {
+    sysassert( dup2(zfd,oi)==oi );
+    sysassert( dup2(pipefds[io],io)==io );
+    sysassert(! close(zfd) );
+    sysassert(! close(pipefds[0]) );
+    sysassert(! close(pipefds[1]) );
+    execlp(cmd,cmd,gziplevel,(char*)0);
+    sysassert(!"execlp gzip/gunzip");
   }
+  sysassert(! close(zfd) );
+  sysassert(! close(pipefds[io]) );
+  sysassert( *f_r= fdopen(pipefds[oi], stdiomode) );
+
+  return 0;
 }
 
-char *masprintf(const char *fmt, ...) {
-  char *r;
-  va_list al;
-  va_start(al,fmt);
-  sysassert( vasprintf(&r,fmt,al) >= 0);
-  sysassert(r);
-  va_end(al);
-  return r;
+void gzclose(FILE **f, pid_t *p, const char *what) {
+  if (!*f) return;
+  
+  sysassert(!ferror(*f));
+  sysassert(!fclose(*f));
+
+  if (*p != -1) {
+    char *process= masprintf("%s (de)compressor",what);
+    waitpid_check_exitstatus(*p,process,1);
+    free(process);
+    *p= -1;
+  }
+
+  *f= 0;
 }