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

Diff for /mandoc/mandocdb.c between version 1.175 and 1.176

version 1.175, 2014/12/04 21:48:48 version 1.176, 2014/12/05 14:26:40
Line 84  enum op {
Line 84  enum op {
 };  };
   
 struct  str {  struct  str {
         char            *rendered; /* key in UTF-8 or ASCII form */  
         const struct mpage *mpage; /* if set, the owning parse */          const struct mpage *mpage; /* if set, the owning parse */
         uint64_t         mask; /* bitmask in sequence */          uint64_t         mask; /* bitmask in sequence */
         char             key[]; /* may contain escape sequences */          char             key[]; /* rendered text */
 };  };
   
 struct  inodev {  struct  inodev {
Line 139  struct mdoc_handler {
Line 138  struct mdoc_handler {
 };  };
   
 static  void     dbclose(int);  static  void     dbclose(int);
 static  void     dbadd(struct mpage *, struct mchars *);  static  void     dbadd(struct mpage *);
 static  void     dbadd_mlink(const struct mlink *mlink);  static  void     dbadd_mlink(const struct mlink *mlink);
 static  void     dbadd_mlink_name(const struct mlink *mlink);  static  void     dbadd_mlink_name(const struct mlink *mlink);
 static  int      dbopen(int);  static  int      dbopen(int);
Line 153  static void  mlink_check(struct mpage *, struct mlink 
Line 152  static void  mlink_check(struct mpage *, struct mlink 
 static  void     mlink_free(struct mlink *);  static  void     mlink_free(struct mlink *);
 static  void     mlinks_undupe(struct mpage *);  static  void     mlinks_undupe(struct mpage *);
 static  void     mpages_free(void);  static  void     mpages_free(void);
 static  void     mpages_merge(struct mchars *, struct mparse *);  static  void     mpages_merge(struct mparse *);
 static  void     names_check(void);  static  void     names_check(void);
 static  void     parse_cat(struct mpage *, int);  static  void     parse_cat(struct mpage *, int);
 static  void     parse_man(struct mpage *, const struct man_meta *,  static  void     parse_man(struct mpage *, const struct man_meta *,
Line 179  static int  parse_mdoc_Sh(struct mpage *, const struct
Line 178  static int  parse_mdoc_Sh(struct mpage *, const struct
 static  int      parse_mdoc_Xr(struct mpage *, const struct mdoc_meta *,  static  int      parse_mdoc_Xr(struct mpage *, const struct mdoc_meta *,
                         const struct mdoc_node *);                          const struct mdoc_node *);
 static  void     putkey(const struct mpage *, char *, uint64_t);  static  void     putkey(const struct mpage *, char *, uint64_t);
 static  void     putkeys(const struct mpage *,  static  void     putkeys(const struct mpage *, char *, size_t, uint64_t);
                         const char *, size_t, uint64_t);  
 static  void     putmdockey(const struct mpage *,  static  void     putmdockey(const struct mpage *,
                         const struct mdoc_node *, uint64_t);                          const struct mdoc_node *, uint64_t);
 static  void     render_key(struct mchars *, struct str *);  static  int      render_string(char **, size_t *);
 static  void     say(const char *, const char *, ...);  static  void     say(const char *, const char *, ...);
 static  int      set_basedir(const char *, int);  static  int      set_basedir(const char *, int);
 static  int      treescan(void);  static  int      treescan(void);
Line 200  static int   write_utf8; /* write UTF-8 output; else A
Line 198  static int   write_utf8; /* write UTF-8 output; else A
 static  int              exitcode; /* to be returned by main */  static  int              exitcode; /* to be returned by main */
 static  enum op          op; /* operational mode */  static  enum op          op; /* operational mode */
 static  char             basedir[PATH_MAX]; /* current base directory */  static  char             basedir[PATH_MAX]; /* current base directory */
   static  struct mchars   *mchars; /* table of named characters */
 static  struct ohash     mpages; /* table of distinct manual pages */  static  struct ohash     mpages; /* table of distinct manual pages */
 static  struct ohash     mlinks; /* table of directory entries */  static  struct ohash     mlinks; /* table of directory entries */
 static  struct ohash     names; /* table of all names */  static  struct ohash     names; /* table of all names */
Line 341  main(int argc, char *argv[])
Line 340  main(int argc, char *argv[])
         int               ch, i;          int               ch, i;
         size_t            j, sz;          size_t            j, sz;
         const char       *path_arg;          const char       *path_arg;
         struct mchars    *mc;  
         struct manpaths   dirs;          struct manpaths   dirs;
         struct mparse    *mp;          struct mparse    *mp;
         struct ohash_info mpages_info, mlinks_info;          struct ohash_info mpages_info, mlinks_info;
Line 441  main(int argc, char *argv[])
Line 439  main(int argc, char *argv[])
         }          }
   
         exitcode = (int)MANDOCLEVEL_OK;          exitcode = (int)MANDOCLEVEL_OK;
         mc = mchars_alloc();          mchars = mchars_alloc();
         mp = mparse_alloc(mparse_options, MANDOCLEVEL_FATAL, NULL,          mp = mparse_alloc(mparse_options, MANDOCLEVEL_FATAL, NULL,
             mc, NULL);              mchars, NULL);
         ohash_init(&mpages, 6, &mpages_info);          ohash_init(&mpages, 6, &mpages_info);
         ohash_init(&mlinks, 6, &mlinks_info);          ohash_init(&mlinks, 6, &mlinks_info);
   
Line 479  main(int argc, char *argv[])
Line 477  main(int argc, char *argv[])
                                 goto out;                                  goto out;
                 }                  }
                 if (OP_DELETE != op)                  if (OP_DELETE != op)
                         mpages_merge(mc, mp);                          mpages_merge(mp);
                 dbclose(OP_DEFAULT == op ? 0 : 1);                  dbclose(OP_DEFAULT == op ? 0 : 1);
         } else {          } else {
                 /*                  /*
Line 526  main(int argc, char *argv[])
Line 524  main(int argc, char *argv[])
                         if (0 == dbopen(0))                          if (0 == dbopen(0))
                                 continue;                                  continue;
   
                         mpages_merge(mc, mp);                          mpages_merge(mp);
                         if (warnings && !nodb &&                          if (warnings && !nodb &&
                             ! (MPARSE_QUICK & mparse_options))                              ! (MPARSE_QUICK & mparse_options))
                                 names_check();                                  names_check();
Line 542  main(int argc, char *argv[])
Line 540  main(int argc, char *argv[])
 out:  out:
         manpath_free(&dirs);          manpath_free(&dirs);
         mparse_free(mp);          mparse_free(mp);
         mchars_free(mc);          mchars_free(mchars);
         mpages_free();          mpages_free();
         ohash_delete(&mpages);          ohash_delete(&mpages);
         ohash_delete(&mlinks);          ohash_delete(&mlinks);
Line 1089  mlink_check(struct mpage *mpage, struct mlink *mlink)
Line 1087  mlink_check(struct mpage *mpage, struct mlink *mlink)
  * and filename to determine whether the file is parsable or not.   * and filename to determine whether the file is parsable or not.
  */   */
 static void  static void
 mpages_merge(struct mchars *mc, struct mparse *mp)  mpages_merge(struct mparse *mp)
 {  {
         char                     any[] = "any";          char                     any[] = "any";
         struct ohash_info        str_info;          struct ohash_info        str_info;
Line 1243  mpages_merge(struct mchars *mc, struct mparse *mp)
Line 1241  mpages_merge(struct mchars *mc, struct mparse *mp)
                              mlink = mlink->next)                               mlink = mlink->next)
                                 mlink_check(mpage, mlink);                                  mlink_check(mpage, mlink);
   
                 dbadd(mpage, mc);                  dbadd(mpage);
   
 nextpage:  nextpage:
                 if (mparse_wait(mp) != MANDOCLEVEL_OK) {                  if (mparse_wait(mp) != MANDOCLEVEL_OK) {
Line 1602  static int
Line 1600  static int
 parse_mdoc_Fd(struct mpage *mpage, const struct mdoc_meta *meta,  parse_mdoc_Fd(struct mpage *mpage, const struct mdoc_meta *meta,
         const struct mdoc_node *n)          const struct mdoc_node *n)
 {  {
         const char      *start, *end;          char            *start, *end;
         size_t           sz;          size_t           sz;
   
         if (SEC_SYNOPSIS != n->sec ||          if (SEC_SYNOPSIS != n->sec ||
Line 1771  parse_mdoc_body(struct mpage *mpage, const struct mdoc
Line 1769  parse_mdoc_body(struct mpage *mpage, const struct mdoc
  * When we finish the manual, we'll dump the table.   * When we finish the manual, we'll dump the table.
  */   */
 static void  static void
 putkeys(const struct mpage *mpage,  putkeys(const struct mpage *mpage, char *cp, size_t sz, uint64_t v)
         const char *cp, size_t sz, uint64_t v)  
 {  {
         struct ohash    *htab;          struct ohash    *htab;
         struct str      *s;          struct str      *s;
         const char      *end;          const char      *end;
         unsigned int     slot;          unsigned int     slot;
         int              i;          int              i, mustfree;
   
         if (0 == sz)          if (0 == sz)
                 return;                  return;
   
           mustfree = render_string(&cp, &sz);
   
         if (TYPE_Nm & v) {          if (TYPE_Nm & v) {
                 htab = &names;                  htab = &names;
                 v &= name_mask;                  v &= name_mask;
Line 1815  putkeys(const struct mpage *mpage,
Line 1814  putkeys(const struct mpage *mpage,
         }          }
         s->mpage = mpage;          s->mpage = mpage;
         s->mask = v;          s->mask = v;
   
           if (mustfree)
                   free(cp);
 }  }
   
 /*  /*
Line 1870  utf8(unsigned int cp, char out[7])
Line 1872  utf8(unsigned int cp, char out[7])
 }  }
   
 /*  /*
  * Store the rendered version of a key, or alias the pointer   * If the string contains escape sequences,
  * if the key contains no escape sequences.   * replace it with an allocated rendering and return 1,
    * such that the caller can free it after use.
    * Otherwise, do nothing and return 0.
  */   */
 static void  static int
 render_key(struct mchars *mc, struct str *key)  render_string(char **public, size_t *psz)
 {  {
         size_t           sz, bsz, pos;          const char      *src, *scp, *addcp, *seq;
           char            *dst;
           size_t           ssz, dsz, addsz;
         char             utfbuf[7], res[6];          char             utfbuf[7], res[6];
         char            *buf;          int              seqlen, unicode;
         const char      *seq, *cpp, *val;  
         int              len, u;  
         enum mandoc_esc  esc;  
   
         assert(NULL == key->rendered);  
   
         res[0] = '\\';          res[0] = '\\';
         res[1] = '\t';          res[1] = '\t';
         res[2] = ASCII_NBRSP;          res[2] = ASCII_NBRSP;
Line 1892  render_key(struct mchars *mc, struct str *key)
Line 1893  render_key(struct mchars *mc, struct str *key)
         res[4] = ASCII_BREAK;          res[4] = ASCII_BREAK;
         res[5] = '\0';          res[5] = '\0';
   
         val = key->key;          src = scp = *public;
         bsz = strlen(val);          ssz = *psz;
           dst = NULL;
           dsz = 0;
   
         /*          while (scp < src + *psz) {
          * Pre-check: if we have no stop-characters, then set the  
          * pointer as ourselvse and get out of here.  
          */  
         if (strcspn(val, res) == bsz) {  
                 key->rendered = key->key;  
                 return;  
         }  
   
         /* Pre-allocate by the length of the input */                  /* Leave normal characters unchanged. */
   
         buf = mandoc_malloc(++bsz);                  if (strchr(res, *scp) == NULL) {
         pos = 0;                          if (dst != NULL)
                                   dst[dsz++] = *scp;
                           scp++;
                           continue;
                   }
   
         while ('\0' != *val) {  
                 /*                  /*
                  * Halt on the first escape sequence.                   * Found something that requires replacing,
                  * This also halts on the end of string, in which case                   * make sure we have a destination buffer.
                  * we just copy, fallthrough, and exit the loop.  
                  */                   */
                 if ((sz = strcspn(val, res)) > 0) {  
                         memcpy(&buf[pos], val, sz);                  if (dst == NULL) {
                         pos += sz;                          dst = mandoc_malloc(ssz + 1);
                         val += sz;                          dsz = scp - src;
                           memcpy(dst, src, dsz);
                 }                  }
   
                 switch (*val) {                  /* Handle single-char special characters. */
                 case ASCII_HYPH:  
                         buf[pos++] = '-';                  switch (*scp) {
                         val++;                  case '\\':
                         continue;                          break;
                 case '\t':                  case '\t':
                         /* FALLTHROUGH */                          /* FALLTHROUGH */
                 case ASCII_NBRSP:                  case ASCII_NBRSP:
                         buf[pos++] = ' ';                          dst[dsz++] = ' ';
                         val++;                          scp++;
                           continue;
                   case ASCII_HYPH:
                           dst[dsz++] = '-';
                         /* FALLTHROUGH */                          /* FALLTHROUGH */
                 case ASCII_BREAK:                  case ASCII_BREAK:
                           scp++;
                         continue;                          continue;
                 default:                  default:
                         break;                          abort();
                 }                  }
                 if ('\\' != *val)  
                         break;  
   
                 /* Read past the slash. */  
   
                 val++;  
   
                 /*                  /*
                  * Parse the escape sequence and see if it's a                   * Found an escape sequence.
                  * predefined character or special character.                   * Read past the slash, then parse it.
                    * Ignore everything except characters.
                  */                   */
   
                 esc = mandoc_escape((const char **)&val,                  scp++;
                     &seq, &len);                  if (mandoc_escape(&scp, &seq, &seqlen) != ESCAPE_SPECIAL)
                 if (ESCAPE_ERROR == esc)  
                         break;  
                 if (ESCAPE_SPECIAL != esc)  
                         continue;                          continue;
   
                 /*                  /*
Line 1962  render_key(struct mchars *mc, struct str *key)
Line 1957  render_key(struct mchars *mc, struct str *key)
                  */                   */
   
                 if (write_utf8) {                  if (write_utf8) {
                         if ((u = mchars_spec2cp(mc, seq, len)) <= 0)                          unicode = mchars_spec2cp(mchars, seq, seqlen);
                           if (unicode <= 0)
                                 continue;                                  continue;
                         cpp = utfbuf;                          addsz = utf8(unicode, utfbuf);
                         if (0 == (sz = utf8(u, utfbuf)))                          if (addsz == 0)
                                 continue;                                  continue;
                         sz = strlen(cpp);                          addcp = utfbuf;
                 } else {                  } else {
                         cpp = mchars_spec2str(mc, seq, len, &sz);                          addcp = mchars_spec2str(mchars, seq, seqlen, &addsz);
                         if (NULL == cpp)                          if (addcp == NULL)
                                 continue;                                  continue;
                         if (ASCII_NBRSP == *cpp) {                          if (*addcp == ASCII_NBRSP) {
                                 cpp = " ";                                  addcp = " ";
                                 sz = 1;                                  addsz = 1;
                         }                          }
                 }                  }
   
                 /* Copy the rendered glyph into the stream. */                  /* Copy the rendered glyph into the stream. */
   
                 bsz += sz;                  ssz += addsz;
                 buf = mandoc_realloc(buf, bsz);                  dst = mandoc_realloc(dst, ssz + 1);
                 memcpy(&buf[pos], cpp, sz);                  memcpy(dst + dsz, addcp, addsz);
                 pos += sz;                  dsz += addsz;
         }          }
           if (dst != NULL) {
                   *public = dst;
                   *psz = dsz;
           }
   
         buf[pos] = '\0';          /* Trim trailing whitespace and NUL-terminate. */
         key->rendered = buf;  
           while (*psz > 0 && (*public)[*psz - 1] == ' ')
                   --*psz;
           if (dst != NULL) {
                   (*public)[*psz] = '\0';
                   return(1);
           } else
                   return(0);
 }  }
   
 static void  static void
Line 2035  dbadd_mlink_name(const struct mlink *mlink)
Line 2042  dbadd_mlink_name(const struct mlink *mlink)
  * Also, handle escape sequences at the last possible moment.   * Also, handle escape sequences at the last possible moment.
  */   */
 static void  static void
 dbadd(struct mpage *mpage, struct mchars *mc)  dbadd(struct mpage *mpage)
 {  {
         struct mlink    *mlink;          struct mlink    *mlink;
         struct str      *key;          struct str      *key;
           char            *cp;
         size_t           i;          size_t           i;
         unsigned int     slot;          unsigned int     slot;
           int              mustfree;
   
         mlink = mpage->mlinks;          mlink = mpage->mlinks;
   
         if (nodb) {          if (nodb) {
                 for (key = ohash_first(&names, &slot); NULL != key;                  for (key = ohash_first(&names, &slot); NULL != key;
                      key = ohash_next(&names, &slot)) {                       key = ohash_next(&names, &slot))
                         if (key->rendered != key->key)  
                                 free(key->rendered);  
                         free(key);                          free(key);
                 }  
                 for (key = ohash_first(&strings, &slot); NULL != key;                  for (key = ohash_first(&strings, &slot); NULL != key;
                      key = ohash_next(&strings, &slot)) {                       key = ohash_next(&strings, &slot))
                         if (key->rendered != key->key)  
                                 free(key->rendered);  
                         free(key);                          free(key);
                 }  
                 if (0 == debug)                  if (0 == debug)
                         return;                          return;
                 while (NULL != mlink) {                  while (NULL != mlink) {
Line 2085  dbadd(struct mpage *mpage, struct mchars *mc)
Line 2088  dbadd(struct mpage *mpage, struct mchars *mc)
         if (debug)          if (debug)
                 say(mlink->file, "Adding to database");                  say(mlink->file, "Adding to database");
   
         i = strlen(mpage->desc) + 1;          cp = mpage->desc;
         key = mandoc_calloc(1, sizeof(struct str) + i);          i = strlen(cp);
         memcpy(key->key, mpage->desc, i);          mustfree = render_string(&cp, &i);
         render_key(mc, key);  
   
         i = 1;          i = 1;
         SQL_BIND_TEXT(stmts[STMT_INSERT_PAGE], i, key->rendered);          SQL_BIND_TEXT(stmts[STMT_INSERT_PAGE], i, cp);
         SQL_BIND_INT(stmts[STMT_INSERT_PAGE], i, mpage->form);          SQL_BIND_INT(stmts[STMT_INSERT_PAGE], i, mpage->form);
         SQL_STEP(stmts[STMT_INSERT_PAGE]);          SQL_STEP(stmts[STMT_INSERT_PAGE]);
         mpage->pageid = sqlite3_last_insert_rowid(db);          mpage->pageid = sqlite3_last_insert_rowid(db);
         sqlite3_reset(stmts[STMT_INSERT_PAGE]);          sqlite3_reset(stmts[STMT_INSERT_PAGE]);
           if (mustfree)
                   free(cp);
   
         if (key->rendered != key->key)  
                 free(key->rendered);  
         free(key);  
   
         while (NULL != mlink) {          while (NULL != mlink) {
                 dbadd_mlink(mlink);                  dbadd_mlink(mlink);
                 mlink = mlink->next;                  mlink = mlink->next;
Line 2110  dbadd(struct mpage *mpage, struct mchars *mc)
Line 2109  dbadd(struct mpage *mpage, struct mchars *mc)
         for (key = ohash_first(&names, &slot); NULL != key;          for (key = ohash_first(&names, &slot); NULL != key;
              key = ohash_next(&names, &slot)) {               key = ohash_next(&names, &slot)) {
                 assert(key->mpage == mpage);                  assert(key->mpage == mpage);
                 if (NULL == key->rendered)  
                         render_key(mc, key);  
                 i = 1;                  i = 1;
                 SQL_BIND_INT64(stmts[STMT_INSERT_NAME], i, key->mask);                  SQL_BIND_INT64(stmts[STMT_INSERT_NAME], i, key->mask);
                 SQL_BIND_TEXT(stmts[STMT_INSERT_NAME], i, key->rendered);                  SQL_BIND_TEXT(stmts[STMT_INSERT_NAME], i, key->key);
                 SQL_BIND_INT64(stmts[STMT_INSERT_NAME], i, mpage->pageid);                  SQL_BIND_INT64(stmts[STMT_INSERT_NAME], i, mpage->pageid);
                 SQL_STEP(stmts[STMT_INSERT_NAME]);                  SQL_STEP(stmts[STMT_INSERT_NAME]);
                 sqlite3_reset(stmts[STMT_INSERT_NAME]);                  sqlite3_reset(stmts[STMT_INSERT_NAME]);
                 if (key->rendered != key->key)  
                         free(key->rendered);  
                 free(key);                  free(key);
         }          }
         for (key = ohash_first(&strings, &slot); NULL != key;          for (key = ohash_first(&strings, &slot); NULL != key;
              key = ohash_next(&strings, &slot)) {               key = ohash_next(&strings, &slot)) {
                 assert(key->mpage == mpage);                  assert(key->mpage == mpage);
                 if (NULL == key->rendered)  
                         render_key(mc, key);  
                 i = 1;                  i = 1;
                 SQL_BIND_INT64(stmts[STMT_INSERT_KEY], i, key->mask);                  SQL_BIND_INT64(stmts[STMT_INSERT_KEY], i, key->mask);
                 SQL_BIND_TEXT(stmts[STMT_INSERT_KEY], i, key->rendered);                  SQL_BIND_TEXT(stmts[STMT_INSERT_KEY], i, key->key);
                 SQL_BIND_INT64(stmts[STMT_INSERT_KEY], i, mpage->pageid);                  SQL_BIND_INT64(stmts[STMT_INSERT_KEY], i, mpage->pageid);
                 SQL_STEP(stmts[STMT_INSERT_KEY]);                  SQL_STEP(stmts[STMT_INSERT_KEY]);
                 sqlite3_reset(stmts[STMT_INSERT_KEY]);                  sqlite3_reset(stmts[STMT_INSERT_KEY]);
                 if (key->rendered != key->key)  
                         free(key->rendered);  
                 free(key);                  free(key);
         }          }
 }  }

Legend:
Removed from v.1.175  
changed lines
  Added in v.1.176

CVSweb