=================================================================== RCS file: /cvs/mandoc/mandocdb.c,v retrieving revision 1.59 retrieving revision 1.64 diff -u -p -r1.59 -r1.64 --- mandoc/mandocdb.c 2013/06/05 17:48:14 1.59 +++ mandoc/mandocdb.c 2013/06/06 17:51:31 1.64 @@ -1,4 +1,4 @@ -/* $Id: mandocdb.c,v 1.59 2013/06/05 17:48:14 schwarze Exp $ */ +/* $Id: mandocdb.c,v 1.64 2013/06/06 17:51:31 schwarze Exp $ */ /* * Copyright (c) 2011, 2012 Kristaps Dzonsons * Copyright (c) 2011, 2012, 2013 Ingo Schwarze @@ -23,6 +23,7 @@ #include #include +#include #include #include #include @@ -140,8 +141,7 @@ static void ofadd(int, const char *, const char *, co static void offree(void); static void ofmerge(struct mchars *, struct mparse *); static void parse_catpage(struct of *); -static int parse_man(struct of *, - const struct man_node *); +static void parse_man(struct of *, const struct man_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_head(struct of *, const struct mdoc_node *); @@ -157,17 +157,15 @@ static int set_basedir(const char *); static void putkey(const struct of *, const char *, uint64_t); static void putkeys(const struct of *, - const char *, int, uint64_t); + const char *, size_t, uint64_t); static void putmdockey(const struct of *, const struct mdoc_node *, uint64_t); static void say(const char *, 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 size_t utf8(unsigned int, char [7]); static void utf8key(struct mchars *, struct str *); -static void wordaddbuf(const struct of *, - const char *, size_t, uint64_t); static char *progname; static int use_all; /* use all found files */ @@ -567,6 +565,8 @@ treescan(void) * Disallow duplicate (hard-linked) files. */ if (FTS_F == ff->fts_info) { + if (0 == strcmp(path, MANDOC_DB)) + continue; if ( ! use_all && ff->fts_level < 2) { if (warnings) say(path, "Extraneous file"); @@ -575,45 +575,50 @@ treescan(void) if (warnings) say(path, "Duplicate file"); continue; - } - - cp = ff->fts_name; - - 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")) { + } else if (NULL == (sec = + strrchr(ff->fts_name, '.'))) { + if ( ! use_all) { if (warnings) - say(path, "Skip html"); + say(path, + "No filename suffix"); 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); ofadd(dform, path, name, dsec, sec, arch, ff->fts_statp); continue; } 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; + } switch (ff->fts_level) { case (0): @@ -1095,6 +1100,7 @@ parse_catpage(struct of *of) if (NULL == title || '\0' == *title) { if (warnings) say(of->file, "Cannot find NAME section"); + putkey(of, of->name, TYPE_Nd); fclose(stream); free(title); return; @@ -1146,20 +1152,10 @@ putkey(const struct of *of, const char *value, uint64_ { 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(). */ static void @@ -1174,7 +1170,7 @@ putmdockey(const struct of *of, const struct mdoc_node } } -static int +static void parse_man(struct of *of, const struct man_node *n) { const struct man_node *head, *body; @@ -1183,7 +1179,7 @@ parse_man(struct of *of, const struct man_node *n) size_t sz, titlesz; if (NULL == n) - return(0); + return; /* * We're only searching for one thing: the first text child in @@ -1225,7 +1221,7 @@ parse_man(struct of *of, const struct man_node *n) title[titlesz - 1] = ' '; } if (NULL == title) - return(1); + return; title = mandoc_realloc(title, titlesz + 1); title[titlesz] = '\0'; @@ -1238,7 +1234,7 @@ parse_man(struct of *of, const struct man_node *n) if (0 == (sz = strlen(sv))) { free(title); - return(1); + return; } /* Erase trailing space. */ @@ -1249,7 +1245,7 @@ parse_man(struct of *of, const struct man_node *n) if (start == sv) { free(title); - return(1); + return; } start = sv; @@ -1286,7 +1282,7 @@ parse_man(struct of *of, const struct man_node *n) if (sv == start) { putkey(of, start, TYPE_Nm); free(title); - return(1); + return; } while (isspace((unsigned char)*start)) @@ -1310,15 +1306,12 @@ parse_man(struct of *of, const struct man_node *n) of->desc = stradd(start); putkey(of, start, TYPE_Nd); free(title); - return(1); + return; } } for (n = n->child; n; n = n->next) - if (parse_man(of, n)) - return(1); - - return(0); + parse_man(of, n); } static void @@ -1532,13 +1525,13 @@ parse_mdoc_body(struct of *of, const struct mdoc_node } /* - * See straddbuf(). + * See stradds(). */ static char * stradd(const char *cp) { - return(straddbuf(cp, strlen(cp))); + return(stradds(cp, strlen(cp))); } /* @@ -1548,10 +1541,10 @@ stradd(const char *cp) * In using it, we avoid having thousands of (e.g.) "cat1" string * allocations for the "of" table. * 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 * -straddbuf(const char *cp, size_t sz) +stradds(const char *cp, size_t sz) { struct str *s; unsigned int index; @@ -1591,8 +1584,7 @@ hashget(const char *cp, size_t sz) * of its entries without conflict. */ static void -wordaddbuf(const struct of *of, - const char *cp, size_t sz, uint64_t v) +putkeys(const struct of *of, const char *cp, size_t sz, uint64_t v) { struct str *s; unsigned int index; @@ -1860,7 +1852,6 @@ static void dbclose(int real) { size_t i; - char file[PATH_MAX]; if (nodb) return; @@ -1876,9 +1867,7 @@ dbclose(int real) if (real) return; - strlcpy(file, MANDOC_DB, PATH_MAX); - strlcat(file, "~", PATH_MAX); - if (-1 == rename(file, MANDOC_DB)) { + if (-1 == rename(MANDOC_DB "~", MANDOC_DB)) { exitcode = (int)MANDOCLEVEL_SYSERR; say(MANDOC_DB, NULL); } @@ -1895,28 +1884,23 @@ dbclose(int real) static int dbopen(int real) { - char file[PATH_MAX]; - const char *sql; + const char *file, *sql; int rc, ofl; - size_t sz; if (nodb) return(1); - sz = strlcpy(file, MANDOC_DB, PATH_MAX); - if ( ! real) - sz = strlcat(file, "~", PATH_MAX); - - if (sz >= PATH_MAX) { - fprintf(stderr, "%s: Path too long\n", file); - return(0); - } - - if ( ! real) - remove(file); - - ofl = SQLITE_OPEN_READWRITE | - (0 == real ? SQLITE_OPEN_EXCLUSIVE : 0); + ofl = SQLITE_OPEN_READWRITE; + if (0 == real) { + file = MANDOC_DB "~"; + if (-1 == remove(file) && ENOENT != errno) { + exitcode = (int)MANDOCLEVEL_SYSERR; + say(file, NULL); + return(0); + } + ofl |= SQLITE_OPEN_EXCLUSIVE; + } else + file = MANDOC_DB; rc = sqlite3_open_v2(file, &db, ofl, NULL); if (SQLITE_OK == rc)