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

Diff for /mandoc/mandocdb.c between version 1.59 and 1.64

version 1.59, 2013/06/05 17:48:14 version 1.64, 2013/06/06 17:51:31
Line 23 
Line 23 
   
 #include <assert.h>  #include <assert.h>
 #include <ctype.h>  #include <ctype.h>
   #include <errno.h>
 #include <fcntl.h>  #include <fcntl.h>
 #include <fts.h>  #include <fts.h>
 #include <getopt.h>  #include <getopt.h>
Line 140  static void  ofadd(int, const char *, const char *, co
Line 141  static void  ofadd(int, const char *, const char *, co
 static  void     offree(void);  static  void     offree(void);
 static  void     ofmerge(struct mchars *, struct mparse *);  static  void     ofmerge(struct mchars *, struct mparse *);
 static  void     parse_catpage(struct of *);  static  void     parse_catpage(struct of *);
 static  int      parse_man(struct of *,  static  void     parse_man(struct of *, const struct man_node *);
                         const struct man_node *);  
 static  void     parse_mdoc(struct of *, const struct mdoc_node *);  static  void     parse_mdoc(struct of *, const struct mdoc_node *);
 static  int      parse_mdoc_body(struct of *, const struct mdoc_node *);  static  int      parse_mdoc_body(struct of *, const struct mdoc_node *);
 static  int      parse_mdoc_head(struct of *, const struct mdoc_node *);  static  int      parse_mdoc_head(struct of *, const struct mdoc_node *);
Line 157  static int  set_basedir(const char *);
Line 157  static int  set_basedir(const char *);
 static  void     putkey(const struct of *,  static  void     putkey(const struct of *,
                         const char *, uint64_t);                          const char *, uint64_t);
 static  void     putkeys(const struct of *,  static  void     putkeys(const struct of *,
                         const char *, int, uint64_t);                          const char *, size_t, uint64_t);
 static  void     putmdockey(const struct of *,  static  void     putmdockey(const struct of *,
                         const struct mdoc_node *, uint64_t);                          const struct mdoc_node *, uint64_t);
 static  void     say(const char *, const char *, ...);  static  void     say(const char *, const char *, ...);
 static  char    *stradd(const char *);  static  char    *stradd(const char *);
 static  char    *straddbuf(const char *, size_t);  static  char    *stradds(const char *, size_t);
 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  void     utf8key(struct mchars *, struct str *);  static  void     utf8key(struct mchars *, struct str *);
 static  void     wordaddbuf(const struct of *,  
                         const char *, size_t, uint64_t);  
   
 static  char            *progname;  static  char            *progname;
 static  int              use_all; /* use all found files */  static  int              use_all; /* use all found files */
Line 567  treescan(void)
Line 565  treescan(void)
                  * Disallow duplicate (hard-linked) files.                   * Disallow duplicate (hard-linked) files.
                  */                   */
                 if (FTS_F == ff->fts_info) {                  if (FTS_F == ff->fts_info) {
                           if (0 == strcmp(path, MANDOC_DB))
                                   continue;
                         if ( ! use_all && ff->fts_level < 2) {                          if ( ! use_all && ff->fts_level < 2) {
                                 if (warnings)                                  if (warnings)
                                         say(path, "Extraneous file");                                          say(path, "Extraneous file");
Line 575  treescan(void)
Line 575  treescan(void)
                                 if (warnings)                                  if (warnings)
                                         say(path, "Duplicate file");                                          say(path, "Duplicate file");
                                 continue;                                  continue;
                         }                          } else if (NULL == (sec =
                                           strrchr(ff->fts_name, '.'))) {
                         cp = ff->fts_name;                                  if ( ! use_all) {
   
                         if (0 == strcmp(cp, "mandocdb.db")) {  
                                 if (warnings)  
                                         say(path, "Skip database");  
                                 continue;  
                         } else if (NULL != (cp = strrchr(cp, '.'))) {  
                                 if (0 == strcmp(cp + 1, "html")) {  
                                         if (warnings)                                          if (warnings)
                                                 say(path, "Skip html");                                                  say(path,
                                                       "No filename suffix");
                                         continue;                                          continue;
                                 } else if (0 == strcmp(cp + 1, "gz")) {  
                                         if (warnings)  
                                                 say(path, "Skip gz");  
                                         continue;  
                                 } else if (0 == strcmp(cp + 1, "ps")) {  
                                         if (warnings)  
                                                 say(path, "Skip ps");  
                                         continue;  
                                 } else if (0 == strcmp(cp + 1, "pdf")) {  
                                         if (warnings)  
                                                 say(path, "Skip pdf");  
                                         continue;  
                                 }                                  }
                           } else if (0 == strcmp(++sec, "html")) {
                                   if (warnings)
                                           say(path, "Skip html");
                                   continue;
                           } else if (0 == strcmp(sec, "gz")) {
                                   if (warnings)
                                           say(path, "Skip gz");
                                   continue;
                           } else if (0 == strcmp(sec, "ps")) {
                                   if (warnings)
                                           say(path, "Skip ps");
                                   continue;
                           } else if (0 == strcmp(sec, "pdf")) {
                                   if (warnings)
                                           say(path, "Skip pdf");
                                   continue;
                           } else if ( ! use_all &&
                               ((FORM_SRC == dform && strcmp(sec, dsec)) ||
                                (FORM_CAT == dform && strcmp(sec, "0")))) {
                                   if (warnings)
                                           say(path, "Wrong filename suffix");
                                   continue;
                           } else {
                                   sec[-1] = '\0';
                                   sec = stradd(sec);
                         }                          }
   
                         if (NULL != (sec = strrchr(ff->fts_name, '.'))) {  
                                 *sec = '\0';  
                                 sec = stradd(sec + 1);  
                         }  
                         name = stradd(ff->fts_name);                          name = stradd(ff->fts_name);
                         ofadd(dform, path,                          ofadd(dform, path,
                                 name, dsec, sec, arch, ff->fts_statp);                                  name, dsec, sec, arch, ff->fts_statp);
                         continue;                          continue;
                 } else if (FTS_D != ff->fts_info &&                  } else if (FTS_D != ff->fts_info &&
                                 FTS_DP != ff->fts_info)                                  FTS_DP != ff->fts_info) {
                           if (warnings)
                                   say(path, "Not a regular file");
                         continue;                          continue;
                   }
   
                 switch (ff->fts_level) {                  switch (ff->fts_level) {
                 case (0):                  case (0):
Line 1095  parse_catpage(struct of *of)
Line 1100  parse_catpage(struct of *of)
         if (NULL == title || '\0' == *title) {          if (NULL == title || '\0' == *title) {
                 if (warnings)                  if (warnings)
                         say(of->file, "Cannot find NAME section");                          say(of->file, "Cannot find NAME section");
                   putkey(of, of->name, TYPE_Nd);
                 fclose(stream);                  fclose(stream);
                 free(title);                  free(title);
                 return;                  return;
Line 1146  putkey(const struct of *of, const char *value, uint64_
Line 1152  putkey(const struct of *of, const char *value, uint64_
 {  {
   
         assert(NULL != value);          assert(NULL != value);
         wordaddbuf(of, value, strlen(value), type);          putkeys(of, value, strlen(value), type);
 }  }
   
 /*  /*
  * Like putkey() but for unterminated strings.  
  */  
 static void  
 putkeys(const struct of *of, const char *value, int sz, uint64_t type)  
 {  
   
         wordaddbuf(of, value, sz, type);  
 }  
   
 /*  
  * Grok all nodes at or below a certain mdoc node into putkey().   * Grok all nodes at or below a certain mdoc node into putkey().
  */   */
 static void  static void
Line 1174  putmdockey(const struct of *of, const struct mdoc_node
Line 1170  putmdockey(const struct of *of, const struct mdoc_node
         }          }
 }  }
   
 static int  static void
 parse_man(struct of *of, const struct man_node *n)  parse_man(struct of *of, const struct man_node *n)
 {  {
         const struct man_node *head, *body;          const struct man_node *head, *body;
Line 1183  parse_man(struct of *of, const struct man_node *n)
Line 1179  parse_man(struct of *of, const struct man_node *n)
         size_t           sz, titlesz;          size_t           sz, titlesz;
   
         if (NULL == n)          if (NULL == n)
                 return(0);                  return;
   
         /*          /*
          * We're only searching for one thing: the first text child in           * We're only searching for one thing: the first text child in
Line 1225  parse_man(struct of *of, const struct man_node *n)
Line 1221  parse_man(struct of *of, const struct man_node *n)
                                 title[titlesz - 1] = ' ';                                  title[titlesz - 1] = ' ';
                         }                          }
                         if (NULL == title)                          if (NULL == title)
                                 return(1);                                  return;
   
                         title = mandoc_realloc(title, titlesz + 1);                          title = mandoc_realloc(title, titlesz + 1);
                         title[titlesz] = '\0';                          title[titlesz] = '\0';
Line 1238  parse_man(struct of *of, const struct man_node *n)
Line 1234  parse_man(struct of *of, const struct man_node *n)
   
                         if (0 == (sz = strlen(sv))) {                          if (0 == (sz = strlen(sv))) {
                                 free(title);                                  free(title);
                                 return(1);                                  return;
                         }                          }
   
                         /* Erase trailing space. */                          /* Erase trailing space. */
Line 1249  parse_man(struct of *of, const struct man_node *n)
Line 1245  parse_man(struct of *of, const struct man_node *n)
   
                         if (start == sv) {                          if (start == sv) {
                                 free(title);                                  free(title);
                                 return(1);                                  return;
                         }                          }
   
                         start = sv;                          start = sv;
Line 1286  parse_man(struct of *of, const struct man_node *n)
Line 1282  parse_man(struct of *of, const struct man_node *n)
                         if (sv == start) {                          if (sv == start) {
                                 putkey(of, start, TYPE_Nm);                                  putkey(of, start, TYPE_Nm);
                                 free(title);                                  free(title);
                                 return(1);                                  return;
                         }                          }
   
                         while (isspace((unsigned char)*start))                          while (isspace((unsigned char)*start))
Line 1310  parse_man(struct of *of, const struct man_node *n)
Line 1306  parse_man(struct of *of, const struct man_node *n)
                         of->desc = stradd(start);                          of->desc = stradd(start);
                         putkey(of, start, TYPE_Nd);                          putkey(of, start, TYPE_Nd);
                         free(title);                          free(title);
                         return(1);                          return;
                 }                  }
         }          }
   
         for (n = n->child; n; n = n->next)          for (n = n->child; n; n = n->next)
                 if (parse_man(of, n))                  parse_man(of, n);
                         return(1);  
   
         return(0);  
 }  }
   
 static void  static void
Line 1532  parse_mdoc_body(struct of *of, const struct mdoc_node 
Line 1525  parse_mdoc_body(struct of *of, const struct mdoc_node 
 }  }
   
 /*  /*
  * See straddbuf().   * See stradds().
  */   */
 static char *  static char *
 stradd(const char *cp)  stradd(const char *cp)
 {  {
   
         return(straddbuf(cp, strlen(cp)));          return(stradds(cp, strlen(cp)));
 }  }
   
 /*  /*
Line 1548  stradd(const char *cp)
Line 1541  stradd(const char *cp)
  * In using it, we avoid having thousands of (e.g.) "cat1" string   * In using it, we avoid having thousands of (e.g.) "cat1" string
  * allocations for the "of" table.   * allocations for the "of" table.
  * We also have a layer atop the string table for keeping track of words   * We also have a layer atop the string table for keeping track of words
  * in a parse sequence (see wordaddbuf()).   * in a parse sequence (see putkeys()).
  */   */
 static char *  static char *
 straddbuf(const char *cp, size_t sz)  stradds(const char *cp, size_t sz)
 {  {
         struct str      *s;          struct str      *s;
         unsigned int     index;          unsigned int     index;
Line 1591  hashget(const char *cp, size_t sz)
Line 1584  hashget(const char *cp, size_t sz)
  * of its entries without conflict.   * of its entries without conflict.
  */   */
 static void  static void
 wordaddbuf(const struct of *of,  putkeys(const struct of *of, const char *cp, size_t sz, uint64_t v)
                 const char *cp, size_t sz, uint64_t v)  
 {  {
         struct str      *s;          struct str      *s;
         unsigned int     index;          unsigned int     index;
Line 1860  static void
Line 1852  static void
 dbclose(int real)  dbclose(int real)
 {  {
         size_t           i;          size_t           i;
         char             file[PATH_MAX];  
   
         if (nodb)          if (nodb)
                 return;                  return;
Line 1876  dbclose(int real)
Line 1867  dbclose(int real)
         if (real)          if (real)
                 return;                  return;
   
         strlcpy(file, MANDOC_DB, PATH_MAX);          if (-1 == rename(MANDOC_DB "~", MANDOC_DB)) {
         strlcat(file, "~", PATH_MAX);  
         if (-1 == rename(file, MANDOC_DB)) {  
                 exitcode = (int)MANDOCLEVEL_SYSERR;                  exitcode = (int)MANDOCLEVEL_SYSERR;
                 say(MANDOC_DB, NULL);                  say(MANDOC_DB, NULL);
         }          }
Line 1895  dbclose(int real)
Line 1884  dbclose(int real)
 static int  static int
 dbopen(int real)  dbopen(int real)
 {  {
         char             file[PATH_MAX];          const char      *file, *sql;
         const char      *sql;  
         int              rc, ofl;          int              rc, ofl;
         size_t           sz;  
   
         if (nodb)          if (nodb)
                 return(1);                  return(1);
   
         sz = strlcpy(file, MANDOC_DB, PATH_MAX);          ofl = SQLITE_OPEN_READWRITE;
         if ( ! real)          if (0 == real) {
                 sz = strlcat(file, "~", PATH_MAX);                  file = MANDOC_DB "~";
                   if (-1 == remove(file) && ENOENT != errno) {
         if (sz >= PATH_MAX) {                          exitcode = (int)MANDOCLEVEL_SYSERR;
                 fprintf(stderr, "%s: Path too long\n", file);                          say(file, NULL);
                 return(0);                          return(0);
         }                  }
                   ofl |= SQLITE_OPEN_EXCLUSIVE;
         if ( ! real)          } else
                 remove(file);                  file = MANDOC_DB;
   
         ofl = SQLITE_OPEN_READWRITE |  
                 (0 == real ? SQLITE_OPEN_EXCLUSIVE : 0);  
   
         rc = sqlite3_open_v2(file, &db, ofl, NULL);          rc = sqlite3_open_v2(file, &db, ofl, NULL);
         if (SQLITE_OK == rc)          if (SQLITE_OK == rc)

Legend:
Removed from v.1.59  
changed lines
  Added in v.1.64

CVSweb