[BACK]Return to mandocdb.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / mandoc

Diff for /mandoc/mandocdb.c between version 1.113 and 1.117

version 1.113, 2014/01/19 22:48:16 version 1.117, 2014/03/19 22:20:43
Line 20 
Line 20 
 #endif  #endif
   
 #include <sys/stat.h>  #include <sys/stat.h>
   #include <sys/wait.h>
   
 #include <assert.h>  #include <assert.h>
 #include <ctype.h>  #include <ctype.h>
Line 168  static int  set_basedir(const char *);
Line 169  static int  set_basedir(const char *);
 static  int      treescan(void);  static  int      treescan(void);
 static  size_t   utf8(unsigned int, char [7]);  static  size_t   utf8(unsigned int, char [7]);
   
   static  char             tempfilename[32];
 static  char            *progname;  static  char            *progname;
 static  int              nodb; /* no database changes */  static  int              nodb; /* no database changes */
 static  int              quick; /* abort the parse early */  static  int              mparse_options; /* abort the parse early */
 static  int              use_all; /* use all found files */  static  int              use_all; /* use all found files */
 static  int              verb; /* print what we're doing */  static  int              verb; /* print what we're doing */
 static  int              warnings; /* warn about crap */  static  int              warnings; /* warn about crap */
Line 349  main(int argc, char *argv[])
Line 351  main(int argc, char *argv[])
   
         path_arg = NULL;          path_arg = NULL;
         op = OP_DEFAULT;          op = OP_DEFAULT;
           mparse_options = MPARSE_SO;
   
         while (-1 != (ch = getopt(argc, argv, "aC:d:nQT:tu:vW")))          while (-1 != (ch = getopt(argc, argv, "aC:d:nQT:tu:vW")))
                 switch (ch) {                  switch (ch) {
Line 369  main(int argc, char *argv[])
Line 372  main(int argc, char *argv[])
                         nodb = 1;                          nodb = 1;
                         break;                          break;
                 case ('Q'):                  case ('Q'):
                         quick = 1;                          mparse_options |= MPARSE_QUICK;
                         break;                          break;
                 case ('T'):                  case ('T'):
                         if (strcmp(optarg, "utf8")) {                          if (strcmp(optarg, "utf8")) {
Line 409  main(int argc, char *argv[])
Line 412  main(int argc, char *argv[])
         }          }
   
         exitcode = (int)MANDOCLEVEL_OK;          exitcode = (int)MANDOCLEVEL_OK;
         mp = mparse_alloc(MPARSE_AUTO,          mp = mparse_alloc(mparse_options, MANDOCLEVEL_FATAL, NULL, NULL);
                 MANDOCLEVEL_FATAL, NULL, NULL, quick);  
         mc = mchars_alloc();          mc = mchars_alloc();
   
         ohash_init(&mpages, 6, &mpages_info);          ohash_init(&mpages, 6, &mpages_info);
Line 997  mpages_merge(struct mchars *mc, struct mparse *mp)
Line 999  mpages_merge(struct mchars *mc, struct mparse *mp)
                     FORM_CAT != mpage->mlinks->fform) {                      FORM_CAT != mpage->mlinks->fform) {
                         lvl = mparse_readfd(mp, -1, mpage->mlinks->file);                          lvl = mparse_readfd(mp, -1, mpage->mlinks->file);
                         if (lvl < MANDOCLEVEL_FATAL)                          if (lvl < MANDOCLEVEL_FATAL)
                                 mparse_result(mp, &mdoc, &man);                                  mparse_result(mp, &mdoc, &man, NULL);
                 }                  }
   
                 if (NULL != mdoc) {                  if (NULL != mdoc) {
Line 1664  static void
Line 1666  static void
 render_key(struct mchars *mc, struct str *key)  render_key(struct mchars *mc, struct str *key)
 {  {
         size_t           sz, bsz, pos;          size_t           sz, bsz, pos;
         char             utfbuf[7], res[5];          char             utfbuf[7], res[6];
         char            *buf;          char            *buf;
         const char      *seq, *cpp, *val;          const char      *seq, *cpp, *val;
         int              len, u;          int              len, u;
Line 1676  render_key(struct mchars *mc, struct str *key)
Line 1678  render_key(struct mchars *mc, struct str *key)
         res[1] = '\t';          res[1] = '\t';
         res[2] = ASCII_NBRSP;          res[2] = ASCII_NBRSP;
         res[3] = ASCII_HYPH;          res[3] = ASCII_HYPH;
         res[4] = '\0';          res[4] = ASCII_BREAK;
           res[5] = '\0';
   
         val = key->key;          val = key->key;
         bsz = strlen(val);          bsz = strlen(val);
Line 1707  render_key(struct mchars *mc, struct str *key)
Line 1710  render_key(struct mchars *mc, struct str *key)
                         val += sz;                          val += sz;
                 }                  }
   
                 if (ASCII_HYPH == *val) {                  switch (*val) {
                   case (ASCII_HYPH):
                         buf[pos++] = '-';                          buf[pos++] = '-';
                         val++;                          val++;
                         continue;                          continue;
                 } else if ('\t' == *val || ASCII_NBRSP == *val) {                  case ('\t'):
                           /* FALLTHROUGH */
                   case (ASCII_NBRSP):
                         buf[pos++] = ' ';                          buf[pos++] = ' ';
                         val++;                          val++;
                           /* FALLTHROUGH */
                   case (ASCII_BREAK):
                         continue;                          continue;
                 } else if ('\\' != *val)                  default:
                         break;                          break;
                   }
                   if ('\\' != *val)
                           break;
   
                 /* Read past the slash. */                  /* Read past the slash. */
   
Line 1865  static void
Line 1876  static void
 dbclose(int real)  dbclose(int real)
 {  {
         size_t           i;          size_t           i;
           int              status;
           pid_t            child;
   
         if (nodb)          if (nodb)
                 return;                  return;
Line 1880  dbclose(int real)
Line 1893  dbclose(int real)
         if (real)          if (real)
                 return;                  return;
   
         if (-1 == rename(MANDOC_DB "~", MANDOC_DB)) {          if ('\0' == *tempfilename) {
                   if (-1 == rename(MANDOC_DB "~", MANDOC_DB)) {
                           exitcode = (int)MANDOCLEVEL_SYSERR;
                           say(MANDOC_DB, "%s", strerror(errno));
                   }
                   return;
           }
   
           switch (child = fork()) {
           case (-1):
                 exitcode = (int)MANDOCLEVEL_SYSERR;                  exitcode = (int)MANDOCLEVEL_SYSERR;
                 say(MANDOC_DB, NULL);                  say("fork cmp", "%s", strerror(errno));
                   return;
           case (0):
                   execlp("cmp", "cmp", "-s",
                       tempfilename, MANDOC_DB, NULL);
                   say("exec cmp", "%s", strerror(errno));
                   exit(0);
           default:
                   break;
         }          }
           if (-1 == waitpid(child, &status, 0)) {
                   exitcode = (int)MANDOCLEVEL_SYSERR;
                   say("wait cmp", "%s", strerror(errno));
           } else if (WIFSIGNALED(status)) {
                   exitcode = (int)MANDOCLEVEL_SYSERR;
                   say("cmp", "Died from a signal");
           } else if (WEXITSTATUS(status)) {
                   exitcode = (int)MANDOCLEVEL_SYSERR;
                   say(MANDOC_DB,
                       "Data changed, but cannot replace database");
           }
   
           *strrchr(tempfilename, '/') = '\0';
           switch (child = fork()) {
           case (-1):
                   exitcode = (int)MANDOCLEVEL_SYSERR;
                   say("fork rm", "%s", strerror(errno));
                   return;
           case (0):
                   execlp("rm", "rm", "-rf", tempfilename, NULL);
                   say("exec rm", "%s", strerror(errno));
                   exit((int)MANDOCLEVEL_SYSERR);
           default:
                   break;
           }
           if (-1 == waitpid(child, &status, 0)) {
                   exitcode = (int)MANDOCLEVEL_SYSERR;
                   say("wait rm", "%s", strerror(errno));
           } else if (WIFSIGNALED(status) || WEXITSTATUS(status)) {
                   exitcode = (int)MANDOCLEVEL_SYSERR;
                   say(tempfilename,
                       "Cannot remove temporary directory");
           }
 }  }
   
 /*  /*
Line 1897  dbclose(int real)
Line 1960  dbclose(int real)
 static int  static int
 dbopen(int real)  dbopen(int real)
 {  {
         const char      *file, *sql;          const char      *sql;
         int              rc, ofl;          int              rc, ofl;
   
         if (nodb)          if (nodb)
                 return(1);                  return(1);
   
           *tempfilename = '\0';
         ofl = SQLITE_OPEN_READWRITE;          ofl = SQLITE_OPEN_READWRITE;
         if (0 == real) {  
                 file = MANDOC_DB "~";          if (real) {
                 if (-1 == remove(file) && ENOENT != errno) {                  rc = sqlite3_open_v2(MANDOC_DB, &db, ofl, NULL);
                   if (SQLITE_OK != rc) {
                         exitcode = (int)MANDOCLEVEL_SYSERR;                          exitcode = (int)MANDOCLEVEL_SYSERR;
                         say(file, NULL);                          say(MANDOC_DB, "%s", sqlite3_errmsg(db));
                         return(0);                          return(0);
                 }                  }
                 ofl |= SQLITE_OPEN_EXCLUSIVE;                  goto prepare_statements;
         } else          }
                 file = MANDOC_DB;  
   
         rc = sqlite3_open_v2(file, &db, ofl, NULL);          ofl |= SQLITE_OPEN_CREATE | SQLITE_OPEN_EXCLUSIVE;
   
           remove(MANDOC_DB "~");
           rc = sqlite3_open_v2(MANDOC_DB "~", &db, ofl, NULL);
         if (SQLITE_OK == rc)          if (SQLITE_OK == rc)
                 goto prepare_statements;                  goto create_tables;
         if (SQLITE_CANTOPEN != rc) {          if (MPARSE_QUICK & mparse_options) {
                 exitcode = (int)MANDOCLEVEL_SYSERR;                  exitcode = (int)MANDOCLEVEL_SYSERR;
                 say(file, NULL);                  say(MANDOC_DB "~", "%s", sqlite3_errmsg(db));
                 return(0);                  return(0);
         }          }
   
         sqlite3_close(db);          if (strlcpy(tempfilename, "/tmp/mandocdb.XXXXXX",
         db = NULL;              sizeof(tempfilename)) >= sizeof(tempfilename)) {
   
         if (SQLITE_OK != (rc = sqlite3_open(file, &db))) {  
                 exitcode = (int)MANDOCLEVEL_SYSERR;                  exitcode = (int)MANDOCLEVEL_SYSERR;
                 say(file, NULL);                  say("/tmp/mandocdb.XXXXXX", "Filename too long");
                 return(0);                  return(0);
         }          }
           if (NULL == mkdtemp(tempfilename)) {
                   exitcode = (int)MANDOCLEVEL_SYSERR;
                   say(tempfilename, "%s", strerror(errno));
                   return(0);
           }
           if (strlcat(tempfilename, "/" MANDOC_DB,
               sizeof(tempfilename)) >= sizeof(tempfilename)) {
                   exitcode = (int)MANDOCLEVEL_SYSERR;
                   say(tempfilename, "Filename too long");
                   return(0);
           }
           rc = sqlite3_open_v2(tempfilename, &db, ofl, NULL);
           if (SQLITE_OK != rc) {
                   exitcode = (int)MANDOCLEVEL_SYSERR;
                   say(tempfilename, "%s", sqlite3_errmsg(db));
                   return(0);
           }
   
   create_tables:
         sql = "CREATE TABLE \"mpages\" (\n"          sql = "CREATE TABLE \"mpages\" (\n"
               " \"form\" INTEGER NOT NULL,\n"                " \"form\" INTEGER NOT NULL,\n"
               " \"id\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL\n"                " \"id\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL\n"
Line 1955  dbopen(int real)
Line 2038  dbopen(int real)
   
         if (SQLITE_OK != sqlite3_exec(db, sql, NULL, NULL, NULL)) {          if (SQLITE_OK != sqlite3_exec(db, sql, NULL, NULL, NULL)) {
                 exitcode = (int)MANDOCLEVEL_SYSERR;                  exitcode = (int)MANDOCLEVEL_SYSERR;
                 say(file, "%s", sqlite3_errmsg(db));                  say(MANDOC_DB, "%s", sqlite3_errmsg(db));
                 return(0);                  return(0);
         }          }
   

Legend:
Removed from v.1.113  
changed lines
  Added in v.1.117

CVSweb