version 1.60, 2013/06/05 20:27:11 |
version 1.65, 2013/06/06 19:00:28 |
|
|
|
|
#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 129 static void dbprune(void); |
|
Line 130 static void dbprune(void); |
|
static void fileadd(struct of *); |
static void fileadd(struct of *); |
static int filecheck(const char *); |
static int filecheck(const char *); |
static void filescan(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_alloc(size_t, void *); |
static void hash_free(void *, size_t, void *); |
static void hash_free(void *, size_t, void *); |
static void *hash_halloc(size_t, void *); |
static void *hash_halloc(size_t, void *); |
Line 140 static void ofadd(int, const char *, const char *, co |
|
Line 140 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 156 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 */ |
|
|
main(int argc, char *argv[]) |
main(int argc, char *argv[]) |
{ |
{ |
int ch, i; |
int ch, i; |
unsigned int index; |
unsigned int slot; |
size_t j, sz; |
size_t j, sz; |
const char *path_arg; |
const char *path_arg; |
struct str *s; |
struct str *s; |
|
|
manpath_free(&dirs); |
manpath_free(&dirs); |
mchars_free(mc); |
mchars_free(mc); |
mparse_free(mp); |
mparse_free(mp); |
for (s = ohash_first(&strings, &index); |
for (s = ohash_first(&strings, &slot); NULL != s; |
NULL != s; s = ohash_next(&strings, &index)) { |
s = ohash_next(&strings, &slot)) { |
if (s->utf8 != s->key) |
if (s->utf8 != s->key) |
free(s->utf8); |
free(s->utf8); |
free(s); |
free(s); |
Line 786 filescan(const char *file) |
|
Line 783 filescan(const char *file) |
|
static int |
static int |
filecheck(const char *name) |
filecheck(const char *name) |
{ |
{ |
unsigned int index; |
|
|
|
index = ohash_qlookup(&filenames, name); |
return(NULL != ohash_find(&filenames, |
return(NULL != ohash_find(&filenames, index)); |
ohash_qlookup(&filenames, name))); |
} |
} |
|
|
/* |
/* |
Line 799 filecheck(const char *name) |
|
Line 795 filecheck(const char *name) |
|
static void |
static void |
fileadd(struct of *of) |
fileadd(struct of *of) |
{ |
{ |
unsigned int index; |
unsigned int slot; |
|
|
index = ohash_qlookup(&filenames, of->file); |
slot = ohash_qlookup(&filenames, of->file); |
assert(NULL == ohash_find(&filenames, index)); |
assert(NULL == ohash_find(&filenames, slot)); |
ohash_insert(&filenames, index, of); |
ohash_insert(&filenames, slot, of); |
} |
} |
|
|
/* |
/* |
Line 814 inocheck(const struct stat *st) |
|
Line 810 inocheck(const struct stat *st) |
|
{ |
{ |
struct id id; |
struct id id; |
uint32_t hash; |
uint32_t hash; |
unsigned int index; |
|
|
|
memset(&id, 0, sizeof(id)); |
memset(&id, 0, sizeof(id)); |
id.ino = hash = st->st_ino; |
id.ino = hash = st->st_ino; |
id.dev = st->st_dev; |
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))); |
} |
} |
|
|
/* |
/* |
|
|
inoadd(const struct stat *st, struct of *of) |
inoadd(const struct stat *st, struct of *of) |
{ |
{ |
uint32_t hash; |
uint32_t hash; |
unsigned int index; |
unsigned int slot; |
|
|
of->id.ino = hash = st->st_ino; |
of->id.ino = hash = st->st_ino; |
of->id.dev = st->st_dev; |
of->id.dev = st->st_dev; |
index = ohash_lookup_memory |
slot = ohash_lookup_memory |
(&inos, (char *)&of->id, sizeof(of->id), hash); |
(&inos, (char *)&of->id, sizeof(of->id), hash); |
|
|
assert(NULL == ohash_find(&inos, index)); |
assert(NULL == ohash_find(&inos, slot)); |
ohash_insert(&inos, index, of); |
ohash_insert(&inos, slot, of); |
} |
} |
|
|
static void |
static void |
Line 1102 parse_catpage(struct of *of) |
|
Line 1096 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 1153 putkey(const struct of *of, const char *value, uint64_ |
|
Line 1148 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 1181 putmdockey(const struct of *of, const struct mdoc_node |
|
Line 1166 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 1190 parse_man(struct of *of, const struct man_node *n) |
|
Line 1175 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 1232 parse_man(struct of *of, const struct man_node *n) |
|
Line 1217 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 1245 parse_man(struct of *of, const struct man_node *n) |
|
Line 1230 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 1256 parse_man(struct of *of, const struct man_node *n) |
|
Line 1241 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 1293 parse_man(struct of *of, const struct man_node *n) |
|
Line 1278 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 1317 parse_man(struct of *of, const struct man_node *n) |
|
Line 1302 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 1539 parse_mdoc_body(struct of *of, const struct mdoc_node |
|
Line 1521 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 1555 stradd(const char *cp) |
|
Line 1537 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 slot; |
const char *end; |
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); |
return(s->key); |
|
|
s = mandoc_calloc(sizeof(struct str) + sz + 1, 1); |
s = mandoc_calloc(sizeof(struct str) + sz + 1, 1); |
memcpy(s->key, cp, sz); |
memcpy(s->key, cp, sz); |
|
ohash_insert(&strings, slot, s); |
end = cp + sz; |
|
index = ohash_qlookupi(&strings, cp, &end); |
|
assert(NULL == ohash_find(&strings, index)); |
|
ohash_insert(&strings, index, s); |
|
return(s->key); |
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. |
* Add a word to the current parse sequence. |
* Within the hashtable of strings, we maintain a list of strings that |
* Within the hashtable of strings, we maintain a list of strings that |
Line 1598 hashget(const char *cp, size_t sz) |
|
Line 1567 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 slot; |
const char *end; |
const char *end; |
|
|
if (0 == sz) |
if (0 == sz) |
return; |
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) { |
if (NULL != s && of == s->of) { |
s->mask |= v; |
s->mask |= v; |
Line 1616 wordaddbuf(const struct of *of, |
|
Line 1586 wordaddbuf(const struct of *of, |
|
} else if (NULL == s) { |
} else if (NULL == s) { |
s = mandoc_calloc(sizeof(struct str) + sz + 1, 1); |
s = mandoc_calloc(sizeof(struct str) + sz + 1, 1); |
memcpy(s->key, cp, sz); |
memcpy(s->key, cp, sz); |
end = cp + sz; |
ohash_insert(&strings, slot, s); |
index = ohash_qlookupi(&strings, cp, &end); |
|
assert(NULL == ohash_find(&strings, index)); |
|
ohash_insert(&strings, index, s); |
|
} |
} |
|
|
s->next = words; |
s->next = words; |
Line 1806 dbindex(struct mchars *mc, int form, const struct of * |
|
Line 1773 dbindex(struct mchars *mc, int form, const struct of * |
|
|
|
desc = ""; |
desc = ""; |
if (NULL != 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); |
assert(NULL != key); |
if (NULL == key->utf8) |
if (NULL == key->utf8) |
utf8key(mc, key); |
utf8key(mc, key); |
|
|
dbclose(int real) |
dbclose(int real) |
{ |
{ |
size_t i; |
size_t i; |
char file[PATH_MAX]; |
|
|
|
if (nodb) |
if (nodb) |
return; |
return; |
Line 1883 dbclose(int real) |
|
Line 1850 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 1902 dbclose(int real) |
|
Line 1867 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) |