=================================================================== RCS file: /cvs/mandoc/mansearch.c,v retrieving revision 1.9 retrieving revision 1.11 diff -u -p -r1.9 -r1.11 --- mandoc/mansearch.c 2013/12/27 01:16:54 1.9 +++ mandoc/mansearch.c 2013/12/31 02:42:29 1.11 @@ -1,4 +1,4 @@ -/* $Id: mansearch.c,v 1.9 2013/12/27 01:16:54 schwarze Exp $ */ +/* $Id: mansearch.c,v 1.11 2013/12/31 02:42:29 schwarze Exp $ */ /* * Copyright (c) 2012 Kristaps Dzonsons * Copyright (c) 2013 Ingo Schwarze @@ -121,6 +121,7 @@ static const struct type types[] = { { 0ULL, NULL } }; +static char *buildnames(sqlite3 *, sqlite3_stmt *, uint64_t); static void *hash_alloc(size_t, void *); static void hash_free(void *, size_t, void *); static void *hash_halloc(size_t, void *); @@ -145,6 +146,7 @@ mansearch(const struct mansearch *search, int64_t id; char buf[PATH_MAX]; char *sql; + struct manpage *mpage; struct expr *e, *ep; sqlite3 *db; sqlite3_stmt *s; @@ -282,8 +284,13 @@ mansearch(const struct mansearch *search, fprintf(stderr, "%s\n", sqlite3_errmsg(db)); sqlite3_finalize(s); - sqlite3_close(db); + c = sqlite3_prepare_v2(db, + "SELECT * FROM mlinks WHERE pageid=?", + -1, &s, NULL); + if (SQLITE_OK != c) + fprintf(stderr, "%s\n", sqlite3_errmsg(db)); + for (mp = ohash_first(&htab, &idx); NULL != mp; mp = ohash_next(&htab, &idx)) { @@ -292,16 +299,23 @@ mansearch(const struct mansearch *search, *res = mandoc_realloc (*res, maxres * sizeof(struct manpage)); } - strlcpy((*res)[cur].file, - paths->paths[i], PATH_MAX); - strlcat((*res)[cur].file, "/", PATH_MAX); - strlcat((*res)[cur].file, mp->file, PATH_MAX); - (*res)[cur].desc = mp->desc; - (*res)[cur].form = mp->form; + mpage = *res + cur; + if (-1 == asprintf(&mpage->file, "%s/%s", + paths->paths[i], mp->file)) { + perror(0); + exit((int)MANDOCLEVEL_SYSERR); + } + mpage->desc = mp->desc; + mpage->form = mp->form; + mpage->names = buildnames(db, s, mp->id); + free(mp->file); free(mp); cur++; } + + sqlite3_finalize(s); + sqlite3_close(db); ohash_delete(&htab); } rc = 1; @@ -312,6 +326,43 @@ out: free(sql); *sz = cur; return(rc); +} + +static char * +buildnames(sqlite3 *db, sqlite3_stmt *s, uint64_t id) +{ + char *names, *newnames; + const char *oldnames, *sep1, *name, *sec, *sep2, *arch; + size_t i; + int c; + + names = NULL; + i = 1; + SQL_BIND_INT64(db, s, i, id); + while (SQLITE_ROW == (c = sqlite3_step(s))) { + if (NULL == names) { + oldnames = ""; + sep1 = ""; + } else { + oldnames = names; + sep1 = ", "; + } + sec = sqlite3_column_text(s, 1); + arch = sqlite3_column_text(s, 2); + name = sqlite3_column_text(s, 3); + sep2 = '\0' == *arch ? "" : "/"; + if (-1 == asprintf(&newnames, "%s%s%s(%s%s%s)", + oldnames, sep1, name, sec, sep2, arch)) { + perror(0); + exit((int)MANDOCLEVEL_SYSERR); + } + free(names); + names = newnames; + } + if (SQLITE_DONE != c) + fprintf(stderr, "%s\n", sqlite3_errmsg(db)); + sqlite3_reset(s); + return(names); } /*