=================================================================== RCS file: /cvs/mandoc/mandocdb.c,v retrieving revision 1.62 retrieving revision 1.65 diff -u -p -r1.62 -r1.65 --- mandoc/mandocdb.c 2013/06/06 02:40:37 1.62 +++ mandoc/mandocdb.c 2013/06/06 19:00:28 1.65 @@ -1,4 +1,4 @@ -/* $Id: mandocdb.c,v 1.62 2013/06/06 02:40:37 schwarze Exp $ */ +/* $Id: mandocdb.c,v 1.65 2013/06/06 19:00:28 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 @@ -129,7 +130,6 @@ static void dbprune(void); static void fileadd(struct of *); static int filecheck(const char *); static void filescan(const char *); -static struct str *hashget(const char *, size_t); static void *hash_alloc(size_t, void *); static void hash_free(void *, size_t, void *); static void *hash_halloc(size_t, void *); @@ -156,17 +156,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 */ @@ -313,7 +311,7 @@ int main(int argc, char *argv[]) { int ch, i; - unsigned int index; + unsigned int slot; size_t j, sz; const char *path_arg; struct str *s; @@ -494,8 +492,8 @@ out: manpath_free(&dirs); mchars_free(mc); mparse_free(mp); - for (s = ohash_first(&strings, &index); - NULL != s; s = ohash_next(&strings, &index)) { + for (s = ohash_first(&strings, &slot); NULL != s; + s = ohash_next(&strings, &slot)) { if (s->utf8 != s->key) free(s->utf8); free(s); @@ -785,10 +783,9 @@ filescan(const char *file) static int filecheck(const char *name) { - unsigned int index; - index = ohash_qlookup(&filenames, name); - return(NULL != ohash_find(&filenames, index)); + return(NULL != ohash_find(&filenames, + ohash_qlookup(&filenames, name))); } /* @@ -798,11 +795,11 @@ filecheck(const char *name) static void fileadd(struct of *of) { - unsigned int index; + unsigned int slot; - index = ohash_qlookup(&filenames, of->file); - assert(NULL == ohash_find(&filenames, index)); - ohash_insert(&filenames, index, of); + slot = ohash_qlookup(&filenames, of->file); + assert(NULL == ohash_find(&filenames, slot)); + ohash_insert(&filenames, slot, of); } /* @@ -813,15 +810,13 @@ inocheck(const struct stat *st) { struct id id; uint32_t hash; - unsigned int index; memset(&id, 0, sizeof(id)); id.ino = hash = st->st_ino; id.dev = st->st_dev; - index = ohash_lookup_memory - (&inos, (char *)&id, sizeof(id), hash); - return(NULL != ohash_find(&inos, index)); + return(NULL != ohash_find(&inos, ohash_lookup_memory( + &inos, (char *)&id, sizeof(id), hash))); } /* @@ -833,15 +828,15 @@ static void inoadd(const struct stat *st, struct of *of) { uint32_t hash; - unsigned int index; + unsigned int slot; of->id.ino = hash = st->st_ino; of->id.dev = st->st_dev; - index = ohash_lookup_memory + slot = ohash_lookup_memory (&inos, (char *)&of->id, sizeof(of->id), hash); - assert(NULL == ohash_find(&inos, index)); - ohash_insert(&inos, index, of); + assert(NULL == ohash_find(&inos, slot)); + ohash_insert(&inos, slot, of); } static void @@ -1153,20 +1148,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 @@ -1536,13 +1521,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))); } /* @@ -1552,39 +1537,26 @@ 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; + unsigned int slot; const char *end; - if (NULL != (s = hashget(cp, sz))) + end = cp + sz; + slot = ohash_qlookupi(&strings, cp, &end); + if (NULL != (s = ohash_find(&strings, slot))) return(s->key); s = mandoc_calloc(sizeof(struct str) + sz + 1, 1); memcpy(s->key, cp, sz); - - end = cp + sz; - index = ohash_qlookupi(&strings, cp, &end); - assert(NULL == ohash_find(&strings, index)); - ohash_insert(&strings, index, s); + ohash_insert(&strings, slot, s); return(s->key); } -static struct str * -hashget(const char *cp, size_t sz) -{ - unsigned int index; - const char *end; - - end = cp + sz; - index = ohash_qlookupi(&strings, cp, &end); - return(ohash_find(&strings, index)); -} - /* * Add a word to the current parse sequence. * Within the hashtable of strings, we maintain a list of strings that @@ -1595,17 +1567,18 @@ 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; + unsigned int slot; const char *end; if (0 == sz) return; - s = hashget(cp, sz); + end = cp + sz; + slot = ohash_qlookupi(&strings, cp, &end); + s = ohash_find(&strings, slot); if (NULL != s && of == s->of) { s->mask |= v; @@ -1613,10 +1586,7 @@ wordaddbuf(const struct of *of, } else if (NULL == s) { s = mandoc_calloc(sizeof(struct str) + sz + 1, 1); memcpy(s->key, cp, sz); - end = cp + sz; - index = ohash_qlookupi(&strings, cp, &end); - assert(NULL == ohash_find(&strings, index)); - ohash_insert(&strings, index, s); + ohash_insert(&strings, slot, s); } s->next = words; @@ -1803,7 +1773,8 @@ dbindex(struct mchars *mc, int form, const struct of * desc = ""; if (NULL != of->desc) { - key = hashget(of->desc, strlen(of->desc)); + key = ohash_find(&strings, + ohash_qlookup(&strings, of->desc)); assert(NULL != key); if (NULL == key->utf8) utf8key(mc, key); @@ -1864,7 +1835,6 @@ static void dbclose(int real) { size_t i; - char file[PATH_MAX]; if (nodb) return; @@ -1880,9 +1850,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); } @@ -1899,28 +1867,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)