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

Diff for /mandoc/mandocdb.c between version 1.103 and 1.112

version 1.103, 2014/01/06 03:02:46 version 1.112, 2014/01/19 22:41:25
Line 48 
Line 48 
 #include "manpath.h"  #include "manpath.h"
 #include "mansearch.h"  #include "mansearch.h"
   
   extern int mansearch_keymax;
   extern const char *const mansearch_keynames[];
   
 #define SQL_EXEC(_v) \  #define SQL_EXEC(_v) \
         if (SQLITE_OK != sqlite3_exec(db, (_v), NULL, NULL, NULL)) \          if (SQLITE_OK != sqlite3_exec(db, (_v), NULL, NULL, NULL)) \
                 fprintf(stderr, "%s\n", sqlite3_errmsg(db))                  fprintf(stderr, "%s\n", sqlite3_errmsg(db))
Line 130  struct mdoc_handler {
Line 133  struct mdoc_handler {
 };  };
   
 static  void     dbclose(int);  static  void     dbclose(int);
 static  void     dbindex(const struct mpage *, struct mchars *);  static  void     dbadd(const struct mpage *, struct mchars *);
 static  int      dbopen(int);  static  int      dbopen(int);
 static  void     dbprune(void);  static  void     dbprune(void);
 static  void     filescan(const char *);  static  void     filescan(const char *);
Line 154  static int  parse_mdoc_Nd(struct mpage *, const struct
Line 157  static int  parse_mdoc_Nd(struct mpage *, const struct
 static  int      parse_mdoc_Nm(struct mpage *, const struct mdoc_node *);  static  int      parse_mdoc_Nm(struct mpage *, const struct mdoc_node *);
 static  int      parse_mdoc_Sh(struct mpage *, const struct mdoc_node *);  static  int      parse_mdoc_Sh(struct mpage *, const struct mdoc_node *);
 static  int      parse_mdoc_Xr(struct mpage *, const struct mdoc_node *);  static  int      parse_mdoc_Xr(struct mpage *, const struct mdoc_node *);
 static  void     putkey(const struct mpage *,  static  void     putkey(const struct mpage *, char *, uint64_t);
                         const char *, uint64_t);  
 static  void     putkeys(const struct mpage *,  static  void     putkeys(const struct mpage *,
                         const char *, size_t, uint64_t);                          const char *, size_t, uint64_t);
 static  void     putmdockey(const struct mpage *,  static  void     putmdockey(const struct mpage *,
Line 700  filescan(const char *file)
Line 702  filescan(const char *file)
                 exitcode = (int)MANDOCLEVEL_BADARG;                  exitcode = (int)MANDOCLEVEL_BADARG;
                 say(file, NULL);                  say(file, NULL);
                 return;                  return;
         } else if (OP_TEST != op && strstr(buf, basedir) != buf) {          }
   
           if (strstr(buf, basedir) == buf)
                   start = buf + strlen(basedir) + 1;
           else if (OP_TEST == op)
                   start = buf;
           else {
                 exitcode = (int)MANDOCLEVEL_BADARG;                  exitcode = (int)MANDOCLEVEL_BADARG;
                 say("", "%s: outside base directory", buf);                  say("", "%s: outside base directory", buf);
                 return;                  return;
         } else if (-1 == stat(buf, &st)) {          }
   
           if (-1 == stat(buf, &st)) {
                 exitcode = (int)MANDOCLEVEL_BADARG;                  exitcode = (int)MANDOCLEVEL_BADARG;
                 say(file, NULL);                  say(file, NULL);
                 return;                  return;
Line 713  filescan(const char *file)
Line 723  filescan(const char *file)
                 say(file, "Not a regular file");                  say(file, "Not a regular file");
                 return;                  return;
         }          }
         start = buf + strlen(basedir);  
         mlink = mandoc_calloc(1, sizeof(struct mlink));          mlink = mandoc_calloc(1, sizeof(struct mlink));
         strlcpy(mlink->file, start, sizeof(mlink->file));          strlcpy(mlink->file, start, sizeof(mlink->file));
   
Line 951  mpages_merge(struct mchars *mc, struct mparse *mp)
Line 961  mpages_merge(struct mchars *mc, struct mparse *mp)
         struct mlink            *mlink;          struct mlink            *mlink;
         struct mdoc             *mdoc;          struct mdoc             *mdoc;
         struct man              *man;          struct man              *man;
         const char              *cp;          char                    *cp;
         int                      match;          int                      match;
         unsigned int             pslot;          unsigned int             pslot;
         enum mandoclevel         lvl;          enum mandoclevel         lvl;
Line 961  mpages_merge(struct mchars *mc, struct mparse *mp)
Line 971  mpages_merge(struct mchars *mc, struct mparse *mp)
         str_info.hfree = hash_free;          str_info.hfree = hash_free;
         str_info.key_offset = offsetof(struct str, key);          str_info.key_offset = offsetof(struct str, key);
   
           if (0 == nodb)
                   SQL_EXEC("BEGIN TRANSACTION");
   
         mpage = ohash_first(&mpages, &pslot);          mpage = ohash_first(&mpages, &pslot);
         while (NULL != mpage) {          while (NULL != mpage) {
                 mlinks_undupe(mpage);                  mlinks_undupe(mpage);
Line 1047  mpages_merge(struct mchars *mc, struct mparse *mp)
Line 1060  mpages_merge(struct mchars *mc, struct mparse *mp)
                 else                  else
                         parse_cat(mpage);                          parse_cat(mpage);
   
                 dbindex(mpage, mc);                  dbadd(mpage, mc);
                 ohash_delete(&strings);                  ohash_delete(&strings);
                 mpage = ohash_next(&mpages, &pslot);                  mpage = ohash_next(&mpages, &pslot);
         }          }
   
           if (0 == nodb)
                   SQL_EXEC("END TRANSACTION");
 }  }
   
 static void  static void
Line 1169  parse_cat(struct mpage *mpage)
Line 1185  parse_cat(struct mpage *mpage)
  * Put a type/word pair into the word database for this particular file.   * Put a type/word pair into the word database for this particular file.
  */   */
 static void  static void
 putkey(const struct mpage *mpage, const char *value, uint64_t type)  putkey(const struct mpage *mpage, char *value, uint64_t type)
 {  {
           char     *cp;
   
         assert(NULL != value);          assert(NULL != value);
           if (TYPE_arch == type)
                   for (cp = value; *cp; cp++)
                           if (isupper((unsigned char)*cp))
                                   *cp = _tolower((unsigned char)*cp);
         putkeys(mpage, value, strlen(value), type);          putkeys(mpage, value, strlen(value), type);
 }  }
   
Line 1288  parse_man(struct mpage *mpage, const struct man_node *
Line 1309  parse_man(struct mpage *mpage, const struct man_node *
                                 byte = start[sz];                                  byte = start[sz];
                                 start[sz] = '\0';                                  start[sz] = '\0';
   
                                   /*
                                    * Assume a stray trailing comma in the
                                    * name list if a name begins with a dash.
                                    */
   
                                   if ('-' == start[0] ||
                                       ('\\' == start[0] && '-' == start[1]))
                                           break;
   
                                 putkey(mpage, start, TYPE_Nm);                                  putkey(mpage, start, TYPE_Nm);
   
                                 if (' ' == byte) {                                  if (' ' == byte) {
Line 1417  parse_mdoc_Fd(struct mpage *mpage, const struct mdoc_n
Line 1447  parse_mdoc_Fd(struct mpage *mpage, const struct mdoc_n
 static int  static int
 parse_mdoc_Fn(struct mpage *mpage, const struct mdoc_node *n)  parse_mdoc_Fn(struct mpage *mpage, const struct mdoc_node *n)
 {  {
         const char      *cp;          char    *cp;
   
         if (NULL == (n = n->child) || MDOC_TEXT != n->type)          if (NULL == (n = n->child) || MDOC_TEXT != n->type)
                 return(0);                  return(0);
Line 1539  putkeys(const struct mpage *mpage,
Line 1569  putkeys(const struct mpage *mpage,
         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     slot;  
         const char      *end;          const char      *end;
           uint64_t         mask;
           unsigned int     slot;
           int              i;
   
         if (0 == sz)          if (0 == sz)
                 return;                  return;
   
           if (verb > 1) {
                   for (i = 0, mask = 1;
                        i < mansearch_keymax;
                        i++, mask <<= 1)
                           if (mask & v)
                                   break;
                   say(mpage->mlinks->file, "Adding key %s=%*s",
                       mansearch_keynames[i], sz, cp);
           }
   
         end = cp + sz;          end = cp + sz;
         slot = ohash_qlookupi(&strings, cp, &end);          slot = ohash_qlookupi(&strings, cp, &end);
         s = ohash_find(&strings, slot);          s = ohash_find(&strings, slot);
Line 1732  render_key(struct mchars *mc, struct str *key)
Line 1774  render_key(struct mchars *mc, struct str *key)
  * Also, handle escape sequences at the last possible moment.   * Also, handle escape sequences at the last possible moment.
  */   */
 static void  static void
 dbindex(const struct mpage *mpage, struct mchars *mc)  dbadd(const struct mpage *mpage, struct mchars *mc)
 {  {
         struct mlink    *mlink;          struct mlink    *mlink;
         struct str      *key;          struct str      *key;
Line 1741  dbindex(const struct mpage *mpage, struct mchars *mc)
Line 1783  dbindex(const struct mpage *mpage, struct mchars *mc)
         unsigned int     slot;          unsigned int     slot;
   
         if (verb)          if (verb)
                 say(mpage->mlinks->file, "Adding to index");                  say(mpage->mlinks->file, "Adding to database");
   
         if (nodb)          if (nodb)
                 return;                  return;
   
         SQL_EXEC("BEGIN TRANSACTION");  
   
         i = 1;          i = 1;
         SQL_BIND_INT(stmts[STMT_INSERT_PAGE], i, FORM_SRC == mpage->form);          SQL_BIND_INT(stmts[STMT_INSERT_PAGE], i, FORM_SRC == mpage->form);
         SQL_STEP(stmts[STMT_INSERT_PAGE]);          SQL_STEP(stmts[STMT_INSERT_PAGE]);
Line 1756  dbindex(const struct mpage *mpage, struct mchars *mc)
Line 1796  dbindex(const struct mpage *mpage, struct mchars *mc)
   
         for (mlink = mpage->mlinks; mlink; mlink = mlink->next) {          for (mlink = mpage->mlinks; mlink; mlink = mlink->next) {
                 i = 1;                  i = 1;
                 SQL_BIND_TEXT(stmts[STMT_INSERT_LINK], i, mlink->file);  
                 SQL_BIND_TEXT(stmts[STMT_INSERT_LINK], i, mlink->dsec);                  SQL_BIND_TEXT(stmts[STMT_INSERT_LINK], i, mlink->dsec);
                 SQL_BIND_TEXT(stmts[STMT_INSERT_LINK], i, mlink->arch);                  SQL_BIND_TEXT(stmts[STMT_INSERT_LINK], i, mlink->arch);
                 SQL_BIND_TEXT(stmts[STMT_INSERT_LINK], i, mlink->name);                  SQL_BIND_TEXT(stmts[STMT_INSERT_LINK], i, mlink->name);
Line 1780  dbindex(const struct mpage *mpage, struct mchars *mc)
Line 1819  dbindex(const struct mpage *mpage, struct mchars *mc)
                         free(key->rendered);                          free(key->rendered);
                 free(key);                  free(key);
         }          }
   
         SQL_EXEC("END TRANSACTION");  
 }  }
   
 static void  static void
Line 1792  dbprune(void)
Line 1829  dbprune(void)
         size_t           i;          size_t           i;
         unsigned int     slot;          unsigned int     slot;
   
         if (nodb)          if (0 == nodb)
                 return;                  SQL_EXEC("BEGIN TRANSACTION");
   
         mpage = ohash_first(&mpages, &slot);          for (mpage = ohash_first(&mpages, &slot); NULL != mpage;
         while (NULL != mpage) {               mpage = ohash_next(&mpages, &slot)) {
                 mlink = mpage->mlinks;                  mlink = mpage->mlinks;
                 i = 1;  
                 SQL_BIND_TEXT(stmts[STMT_DELETE_PAGE], i, mlink->file);  
                 SQL_STEP(stmts[STMT_DELETE_PAGE]);  
                 sqlite3_reset(stmts[STMT_DELETE_PAGE]);  
                 if (verb)                  if (verb)
                         say(mlink->file, "Deleted from index");                          say(mlink->file, "Deleting from database");
                 mpage = ohash_next(&mpages, &slot);                  if (nodb)
                           continue;
                   for ( ; NULL != mlink; mlink = mlink->next) {
                           i = 1;
                           SQL_BIND_TEXT(stmts[STMT_DELETE_PAGE],
                               i, mlink->dsec);
                           SQL_BIND_TEXT(stmts[STMT_DELETE_PAGE],
                               i, mlink->arch);
                           SQL_BIND_TEXT(stmts[STMT_DELETE_PAGE],
                               i, mlink->name);
                           SQL_STEP(stmts[STMT_DELETE_PAGE]);
                           sqlite3_reset(stmts[STMT_DELETE_PAGE]);
                   }
         }          }
   
           if (0 == nodb)
                   SQL_EXEC("END TRANSACTION");
 }  }
   
 /*  /*
Line 1890  dbopen(int real)
Line 1938  dbopen(int real)
               ");\n"                ");\n"
               "\n"                "\n"
               "CREATE TABLE \"mlinks\" (\n"                "CREATE TABLE \"mlinks\" (\n"
               " \"file\" TEXT NOT NULL,\n"  
               " \"sec\" TEXT NOT NULL,\n"                " \"sec\" TEXT NOT NULL,\n"
               " \"arch\" TEXT NOT NULL,\n"                " \"arch\" TEXT NOT NULL,\n"
               " \"name\" TEXT NOT NULL,\n"                " \"name\" TEXT NOT NULL,\n"
               " \"pageid\" INTEGER NOT NULL REFERENCES mpages(id) "                " \"pageid\" INTEGER NOT NULL REFERENCES mpages(id) "
                 "ON DELETE CASCADE,\n"                  "ON DELETE CASCADE\n"
               " \"id\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL\n"  
               ");\n"                ");\n"
               "\n"                "\n"
               "CREATE TABLE \"keys\" (\n"                "CREATE TABLE \"keys\" (\n"
               " \"bits\" INTEGER NOT NULL,\n"                " \"bits\" INTEGER NOT NULL,\n"
               " \"key\" TEXT NOT NULL,\n"                " \"key\" TEXT NOT NULL,\n"
               " \"pageid\" INTEGER NOT NULL REFERENCES mpages(id) "                " \"pageid\" INTEGER NOT NULL REFERENCES mpages(id) "
                 "ON DELETE CASCADE,\n"                  "ON DELETE CASCADE\n"
               " \"id\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL\n"                ");\n";
               ");\n"  
               "\n"  
               "CREATE INDEX \"key_index\" ON keys (key);\n";  
   
         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;
Line 1917  dbopen(int real)
Line 1960  dbopen(int real)
   
 prepare_statements:  prepare_statements:
         SQL_EXEC("PRAGMA foreign_keys = ON");          SQL_EXEC("PRAGMA foreign_keys = ON");
         sql = "DELETE FROM mpages where file=?";          sql = "DELETE FROM mpages WHERE id IN "
                   "(SELECT pageid FROM mlinks WHERE "
                   "sec=? AND arch=? AND name=?)";
         sqlite3_prepare_v2(db, sql, -1, &stmts[STMT_DELETE_PAGE], NULL);          sqlite3_prepare_v2(db, sql, -1, &stmts[STMT_DELETE_PAGE], NULL);
         sql = "INSERT INTO mpages "          sql = "INSERT INTO mpages "
                 "(form) VALUES (?)";                  "(form) VALUES (?)";
         sqlite3_prepare_v2(db, sql, -1, &stmts[STMT_INSERT_PAGE], NULL);          sqlite3_prepare_v2(db, sql, -1, &stmts[STMT_INSERT_PAGE], NULL);
         sql = "INSERT INTO mlinks "          sql = "INSERT INTO mlinks "
                 "(file,sec,arch,name,pageid) VALUES (?,?,?,?,?)";                  "(sec,arch,name,pageid) VALUES (?,?,?,?)";
         sqlite3_prepare_v2(db, sql, -1, &stmts[STMT_INSERT_LINK], NULL);          sqlite3_prepare_v2(db, sql, -1, &stmts[STMT_INSERT_LINK], NULL);
         sql = "INSERT INTO keys "          sql = "INSERT INTO keys "
                 "(bits,key,pageid) VALUES (?,?,?)";                  "(bits,key,pageid) VALUES (?,?,?)";

Legend:
Removed from v.1.103  
changed lines
  Added in v.1.112

CVSweb