=================================================================== RCS file: /cvs/mandoc/mansearch.c,v retrieving revision 1.28 retrieving revision 1.33 diff -u -p -r1.28 -r1.33 --- mandoc/mansearch.c 2014/04/11 15:46:52 1.28 +++ mandoc/mansearch.c 2014/04/17 19:20:01 1.33 @@ -1,4 +1,4 @@ -/* $Id: mansearch.c,v 1.28 2014/04/11 15:46:52 schwarze Exp $ */ +/* $Id: mansearch.c,v 1.33 2014/04/17 19:20:01 schwarze Exp $ */ /* * Copyright (c) 2012 Kristaps Dzonsons * Copyright (c) 2013, 2014 Ingo Schwarze @@ -74,7 +74,7 @@ struct expr { }; struct match { - uint64_t id; /* identifier in database */ + uint64_t pageid; /* identifier in database */ char *desc; /* manual page description */ int form; /* 0 == catpage */ }; @@ -156,7 +156,7 @@ mansearch(const struct mansearch *search, struct manpage **res, size_t *sz) { int fd, rc, c, indexbit; - int64_t id; + int64_t pageid; uint64_t outbit, iterbit; char buf[PATH_MAX]; char *sql; @@ -175,7 +175,7 @@ mansearch(const struct mansearch *search, info.halloc = hash_halloc; info.alloc = hash_alloc; info.hfree = hash_free; - info.key_offset = offsetof(struct match, id); + info.key_offset = offsetof(struct match, pageid); *sz = cur = maxres = 0; sql = NULL; @@ -210,7 +210,7 @@ mansearch(const struct mansearch *search, */ if (NULL == getcwd(buf, PATH_MAX)) { - perror(NULL); + perror("getcwd"); goto out; } else if (-1 == (fd = open(buf, O_RDONLY, 0))) { perror(buf); @@ -253,10 +253,12 @@ mansearch(const struct mansearch *search, */ c = sqlite3_create_function(db, "match", 2, - SQLITE_ANY, NULL, sql_match, NULL, NULL); + SQLITE_UTF8 | SQLITE_DETERMINISTIC, + NULL, sql_match, NULL, NULL); assert(SQLITE_OK == c); c = sqlite3_create_function(db, "regexp", 2, - SQLITE_ANY, NULL, sql_regexp, NULL, NULL); + SQLITE_UTF8 | SQLITE_DETERMINISTIC, + NULL, sql_regexp, NULL, NULL); assert(SQLITE_OK == c); j = 1; @@ -285,16 +287,16 @@ mansearch(const struct mansearch *search, * distribution of buckets in the table. */ while (SQLITE_ROW == (c = sqlite3_step(s))) { - id = sqlite3_column_int64(s, 2); + pageid = sqlite3_column_int64(s, 2); idx = ohash_lookup_memory - (&htab, (char *)&id, - sizeof(uint64_t), (uint32_t)id); + (&htab, (char *)&pageid, + sizeof(uint64_t), (uint32_t)pageid); if (NULL != ohash_find(&htab, idx)) continue; mp = mandoc_calloc(1, sizeof(struct match)); - mp->id = id; + mp->pageid = pageid; mp->form = sqlite3_column_int(s, 1); if (TYPE_Nd == outbit) mp->desc = mandoc_strdup( @@ -330,11 +332,11 @@ mansearch(const struct mansearch *search, } mpage = *res + cur; mpage->form = mp->form; - buildnames(mpage, db, s, mp->id, + buildnames(mpage, db, s, mp->pageid, paths->paths[i], mp->form); mpage->output = TYPE_Nd & outbit ? mp->desc : outbit ? - buildoutput(db, s2, mp->id, outbit) : NULL; + buildoutput(db, s2, mp->pageid, outbit) : NULL; free(mp); cur++; @@ -347,9 +349,12 @@ mansearch(const struct mansearch *search, } rc = 1; out: - exprfree(e); - if (-1 != fd) + if (-1 != fd) { + if (-1 == fchdir(fd)) + perror(buf); close(fd); + } + exprfree(e); free(sql); *sz = cur; return(rc); @@ -357,7 +362,7 @@ out: static void buildnames(struct manpage *mpage, sqlite3 *db, sqlite3_stmt *s, - uint64_t id, const char *path, int form) + uint64_t pageid, const char *path, int form) { char *newnames, *prevsec, *prevarch; const char *oldnames, *sep1, *name, *sec, *sep2, *arch, *fsec; @@ -368,7 +373,7 @@ buildnames(struct manpage *mpage, sqlite3 *db, sqlite3 mpage->names = NULL; prevsec = prevarch = NULL; i = 1; - SQL_BIND_INT64(db, s, i, id); + SQL_BIND_INT64(db, s, i, pageid); while (SQLITE_ROW == (c = sqlite3_step(s))) { /* Decide whether we already have some names. */ @@ -450,7 +455,7 @@ buildnames(struct manpage *mpage, sqlite3 *db, sqlite3 } static char * -buildoutput(sqlite3 *db, sqlite3_stmt *s, uint64_t id, uint64_t outbit) +buildoutput(sqlite3 *db, sqlite3_stmt *s, uint64_t pageid, uint64_t outbit) { char *output, *newoutput; const char *oldoutput, *sep1, *data; @@ -459,7 +464,7 @@ buildoutput(sqlite3 *db, sqlite3_stmt *s, uint64_t id, output = NULL; i = 1; - SQL_BIND_INT64(db, s, i, id); + SQL_BIND_INT64(db, s, i, pageid); SQL_BIND_INT64(db, s, i, outbit); while (SQLITE_ROW == (c = sqlite3_step(s))) { if (NULL == output) { @@ -554,14 +559,14 @@ sql_statement(const struct expr *e) : "desc MATCH ?") : TYPE_Nm == e->bits ? (NULL == e->substr - ? "id IN (SELECT pageid FROM names " + ? "pageid IN (SELECT pageid FROM names " "WHERE name REGEXP ?)" - : "id IN (SELECT pageid FROM names " + : "pageid IN (SELECT pageid FROM names " "WHERE name MATCH ?)") : (NULL == e->substr - ? "id IN (SELECT pageid FROM keys " + ? "pageid IN (SELECT pageid FROM keys " "WHERE key REGEXP ? AND bits & ?)" - : "id IN (SELECT pageid FROM keys " + : "pageid IN (SELECT pageid FROM keys " "WHERE key MATCH ? AND bits & ?)"), 1); if (e->close) sql_append(&sql, &sz, ")", e->close); @@ -585,7 +590,7 @@ exprcomp(const struct mansearch *search, int argc, cha first = cur = NULL; logic = igncase = toclose = 0; - toopen = 1; + toopen = NULL != search->sec || NULL != search->arch; for (i = 0; i < argc; i++) { if (0 == strcmp("(", argv[i])) { @@ -654,9 +659,12 @@ exprcomp(const struct mansearch *search, int argc, cha if (toopen || logic || igncase || toclose) goto fail; - cur->close++; - cur = exprspec(cur, TYPE_arch, search->arch, "^(%s|any)$"); - exprspec(cur, TYPE_sec, search->sec, "^%s$"); + if (NULL != search->sec || NULL != search->arch) + cur->close++; + if (NULL != search->arch) + cur = exprspec(cur, TYPE_arch, search->arch, "^(%s|any)$"); + if (NULL != search->sec) + exprspec(cur, TYPE_sec, search->sec, "^%s$"); return(first); @@ -673,9 +681,6 @@ exprspec(struct expr *cur, uint64_t key, const char *v char errbuf[BUFSIZ]; char *cp; int irc; - - if (NULL == value) - return(cur); mandoc_asprintf(&cp, format, value); cur->next = mandoc_calloc(1, sizeof(struct expr));