=================================================================== RCS file: /cvs/mandoc/mandocdb.c,v retrieving revision 1.115 retrieving revision 1.122 diff -u -p -r1.115 -r1.122 --- mandoc/mandocdb.c 2014/03/18 16:56:10 1.115 +++ mandoc/mandocdb.c 2014/03/23 12:44:56 1.122 @@ -1,4 +1,4 @@ -/* $Id: mandocdb.c,v 1.115 2014/03/18 16:56:10 schwarze Exp $ */ +/* $Id: mandocdb.c,v 1.122 2014/03/23 12:44:56 schwarze Exp $ */ /* * Copyright (c) 2011, 2012 Kristaps Dzonsons * Copyright (c) 2011, 2012, 2013, 2014 Ingo Schwarze @@ -46,6 +46,7 @@ #include "mdoc.h" #include "man.h" #include "mandoc.h" +#include "mandoc_aux.h" #include "manpath.h" #include "mansearch.h" @@ -99,6 +100,7 @@ struct inodev { struct mpage { struct inodev inodev; /* used for hashing routine */ + int64_t recno; /* id in mpages SQL table */ enum form form; /* format from file content */ char *sec; /* section from file content */ char *arch; /* architecture from file content */ @@ -116,6 +118,7 @@ struct mlink { char *name; /* name from file name (not empty) */ char *fsec; /* section from file name suffix */ struct mlink *next; /* singly linked list */ + struct mpage *mpage; /* parent */ }; enum stmt { @@ -134,7 +137,8 @@ struct mdoc_handler { }; static void dbclose(int); -static void dbadd(const struct mpage *, struct mchars *); +static void dbadd(struct mpage *, struct mchars *); +static void dbadd_mlink(const struct mlink *mlink); static int dbopen(int); static void dbprune(void); static void filescan(const char *); @@ -172,7 +176,7 @@ static size_t utf8(unsigned int, char [7]); static char tempfilename[32]; static char *progname; static int nodb; /* no database changes */ -static int quick; /* abort the parse early */ +static int mparse_options; /* abort the parse early */ static int use_all; /* use all found files */ static int verb; /* print what we're doing */ static int warnings; /* warn about crap */ @@ -371,7 +375,7 @@ main(int argc, char *argv[]) nodb = 1; break; case ('Q'): - quick = 1; + mparse_options |= MPARSE_QUICK; break; case ('T'): if (strcmp(optarg, "utf8")) { @@ -411,8 +415,7 @@ main(int argc, char *argv[]) } exitcode = (int)MANDOCLEVEL_OK; - mp = mparse_alloc(MPARSE_AUTO, - MANDOCLEVEL_FATAL, NULL, NULL, quick); + mp = mparse_alloc(mparse_options, MANDOCLEVEL_FATAL, NULL, NULL); mc = mchars_alloc(); ohash_init(&mpages, 6, &mpages_info); @@ -818,6 +821,7 @@ mlink_add(struct mlink *mlink, const struct stat *st) } else mlink->next = mpage->mlinks; mpage->mlinks = mlink; + mlink->mpage = mpage; } static void @@ -960,10 +964,11 @@ mpages_merge(struct mchars *mc, struct mparse *mp) { char any[] = "any"; struct ohash_info str_info; - struct mpage *mpage; - struct mlink *mlink; + struct mpage *mpage, *mpage_dest; + struct mlink *mlink, *mlink_dest; struct mdoc *mdoc; struct man *man; + char *sodest; char *cp; int match; unsigned int pslot; @@ -999,10 +1004,48 @@ mpages_merge(struct mchars *mc, struct mparse *mp) FORM_CAT != mpage->mlinks->fform) { lvl = mparse_readfd(mp, -1, mpage->mlinks->file); if (lvl < MANDOCLEVEL_FATAL) - mparse_result(mp, &mdoc, &man); + mparse_result(mp, &mdoc, &man, &sodest); } - if (NULL != mdoc) { + if (NULL != sodest) { + mlink_dest = ohash_find(&mlinks, + ohash_qlookup(&mlinks, sodest)); + if (NULL != mlink_dest) { + + /* The .so target exists. */ + + mpage_dest = mlink_dest->mpage; + mlink = mpage->mlinks; + while (1) { + mlink->mpage = mpage_dest; + + /* + * If the target was already + * processed, add the links + * to the database now. + * Otherwise, this will + * happen when we come + * to the target. + */ + + if (mpage_dest->recno) + dbadd_mlink(mlink); + + if (NULL == mlink->next) + break; + mlink = mlink->next; + } + + /* Move all links to the target. */ + + mlink->next = mlink_dest->next; + mlink_dest->next = mpage->mlinks; + mpage->mlinks = NULL; + } + ohash_delete(&strings); + mpage = ohash_next(&mpages, &pslot); + continue; + } else if (NULL != mdoc) { mpage->form = FORM_SRC; mpage->sec = mandoc_strdup(mdoc_meta(mdoc)->msec); @@ -1220,9 +1263,9 @@ static void parse_man(struct mpage *mpage, const struct man_node *n) { const struct man_node *head, *body; - char *start, *sv, *title; + char *start, *title; char byte; - size_t sz, titlesz; + size_t sz; if (NULL == n) return; @@ -1242,12 +1285,8 @@ parse_man(struct mpage *mpage, const struct man_node * NULL != (head = (head->child)) && MAN_TEXT == head->type && 0 == strcmp(head->string, "NAME") && - NULL != (body = body->child) && - MAN_TEXT == body->type) { + NULL != body->child) { - title = NULL; - titlesz = 0; - /* * Suck the entire NAME section into memory. * Yes, we might run away. @@ -1255,47 +1294,11 @@ parse_man(struct mpage *mpage, const struct man_node * * NAME sections over many lines. */ - for ( ; NULL != body; body = body->next) { - if (MAN_TEXT != body->type) - break; - if (0 == (sz = strlen(body->string))) - continue; - title = mandoc_realloc - (title, titlesz + sz + 1); - memcpy(title + titlesz, body->string, sz); - titlesz += sz + 1; - title[titlesz - 1] = ' '; - } + title = NULL; + man_deroff(&title, body); if (NULL == title) return; - title = mandoc_realloc(title, titlesz + 1); - title[titlesz] = '\0'; - - /* Skip leading space. */ - - sv = title; - while (isspace((unsigned char)*sv)) - sv++; - - if (0 == (sz = strlen(sv))) { - free(title); - return; - } - - /* Erase trailing space. */ - - start = &sv[sz - 1]; - while (start > sv && isspace((unsigned char)*start)) - *start-- = '\0'; - - if (start == sv) { - free(title); - return; - } - - start = sv; - /* * Go through a special heuristic dance here. * Conventionally, one or more manual names are @@ -1304,6 +1307,7 @@ parse_man(struct mpage *mpage, const struct man_node * * the name parts here. */ + start = title; for ( ;; ) { sz = strcspn(start, " ,"); if ('\0' == start[sz]) @@ -1334,7 +1338,7 @@ parse_man(struct mpage *mpage, const struct man_node * start++; } - if (sv == start) { + if (start == title) { putkey(mpage, start, TYPE_Nm); free(title); return; @@ -1493,10 +1497,7 @@ parse_mdoc_Xr(struct mpage *mpage, const struct mdoc_n return(0); } - if (-1 == asprintf(&cp, "%s(%s)", n->string, n->next->string)) { - perror(NULL); - exit((int)MANDOCLEVEL_SYSERR); - } + mandoc_asprintf(&cp, "%s(%s)", n->string, n->next->string); putkey(mpage, cp, TYPE_Xr); free(cp); return(0); @@ -1505,32 +1506,10 @@ parse_mdoc_Xr(struct mpage *mpage, const struct mdoc_n static int parse_mdoc_Nd(struct mpage *mpage, const struct mdoc_node *n) { - size_t sz; - if (MDOC_BODY != n->type) - return(0); - - /* - * Special-case the `Nd' because we need to put the description - * into the document table. - */ - - for (n = n->child; NULL != n; n = n->next) { - if (MDOC_TEXT == n->type) { - if (NULL != mpage->desc) { - sz = strlen(mpage->desc) + - strlen(n->string) + 2; - mpage->desc = mandoc_realloc( - mpage->desc, sz); - strlcat(mpage->desc, " ", sz); - strlcat(mpage->desc, n->string, sz); - } else - mpage->desc = mandoc_strdup(n->string); - } - if (NULL != n->child) - parse_mdoc_Nd(mpage, n); - } - return(1); + if (MDOC_BODY == n->type) + mdoc_deroff(&mpage->desc, n); + return(0); } static int @@ -1779,6 +1758,20 @@ render_key(struct mchars *mc, struct str *key) key->rendered = buf; } +static void +dbadd_mlink(const struct mlink *mlink) +{ + size_t i; + + i = 1; + 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->name); + SQL_BIND_INT64(stmts[STMT_INSERT_LINK], i, mlink->mpage->recno); + SQL_STEP(stmts[STMT_INSERT_LINK]); + sqlite3_reset(stmts[STMT_INSERT_LINK]); +} + /* * Flush the current page's terms (and their bits) into the database. * Wrap the entire set of additions in a transaction to make sqlite be a @@ -1786,11 +1779,10 @@ render_key(struct mchars *mc, struct str *key) * Also, handle escape sequences at the last possible moment. */ static void -dbadd(const struct mpage *mpage, struct mchars *mc) +dbadd(struct mpage *mpage, struct mchars *mc) { struct mlink *mlink; struct str *key; - int64_t recno; size_t i; unsigned int slot; @@ -1803,18 +1795,11 @@ dbadd(const struct mpage *mpage, struct mchars *mc) i = 1; SQL_BIND_INT(stmts[STMT_INSERT_PAGE], i, FORM_SRC == mpage->form); SQL_STEP(stmts[STMT_INSERT_PAGE]); - recno = sqlite3_last_insert_rowid(db); + mpage->recno = sqlite3_last_insert_rowid(db); sqlite3_reset(stmts[STMT_INSERT_PAGE]); - for (mlink = mpage->mlinks; mlink; mlink = mlink->next) { - i = 1; - 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->name); - SQL_BIND_INT64(stmts[STMT_INSERT_LINK], i, recno); - SQL_STEP(stmts[STMT_INSERT_LINK]); - sqlite3_reset(stmts[STMT_INSERT_LINK]); - } + for (mlink = mpage->mlinks; mlink; mlink = mlink->next) + dbadd_mlink(mlink); for (key = ohash_first(&strings, &slot); NULL != key; key = ohash_next(&strings, &slot)) { @@ -1824,7 +1809,7 @@ dbadd(const struct mpage *mpage, struct mchars *mc) i = 1; SQL_BIND_INT64(stmts[STMT_INSERT_KEY], i, key->mask); SQL_BIND_TEXT(stmts[STMT_INSERT_KEY], i, key->rendered); - SQL_BIND_INT64(stmts[STMT_INSERT_KEY], i, recno); + SQL_BIND_INT64(stmts[STMT_INSERT_KEY], i, mpage->recno); SQL_STEP(stmts[STMT_INSERT_KEY]); sqlite3_reset(stmts[STMT_INSERT_KEY]); if (key->rendered != key->key) @@ -1985,7 +1970,7 @@ dbopen(int real) rc = sqlite3_open_v2(MANDOC_DB "~", &db, ofl, NULL); if (SQLITE_OK == rc) goto create_tables; - if (quick) { + if (MPARSE_QUICK & mparse_options) { exitcode = (int)MANDOCLEVEL_SYSERR; say(MANDOC_DB "~", "%s", sqlite3_errmsg(db)); return(0);