-/* $Id: mansearch.c,v 1.39 2014/07/24 20:30:45 schwarze Exp $ */
+/* $Id: mansearch.c,v 1.48 2014/09/03 18:09:14 schwarze Exp $ */
/*
* Copyright (c) 2012 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) 2013, 2014 Ingo Schwarze <schwarze@openbsd.org>
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
-#ifdef HAVE_CONFIG_H
#include "config.h"
-#endif
#include <sys/mman.h>
+#include <sys/types.h>
+
#include <assert.h>
#include <fcntl.h>
#include <getopt.h>
#include <string.h>
#include <unistd.h>
-#ifdef HAVE_OHASH
+#if HAVE_OHASH
#include <ohash.h>
#else
#include "compat_ohash.h"
#endif
#include <sqlite3.h>
+#ifndef SQLITE_DETERMINISTIC
+#define SQLITE_DETERMINISTIC 0
+#endif
#include "mandoc.h"
#include "mandoc_aux.h"
struct match {
uint64_t pageid; /* identifier in database */
char *desc; /* manual page description */
- int form; /* 0 == catpage */
+ int form; /* bit field: formatted, zipped? */
};
static void buildnames(struct manpage *, sqlite3 *,
}
pagecache = mmap(NULL, PC_PAGESIZE * PC_NUMPAGES,
- PROT_READ | PROT_WRITE, MAP_ANON, -1, 0);
+ PROT_READ | PROT_WRITE,
+ MAP_SHARED | MAP_ANON, -1, 0);
if (MAP_FAILED == pagecache) {
perror("mmap");
mansearch(const struct mansearch *search,
const struct manpaths *paths,
int argc, char *argv[],
- const char *outkey,
struct manpage **res, size_t *sz)
{
int fd, rc, c, indexbit;
goto out;
outbit = 0;
- if (NULL != outkey) {
+ if (NULL != search->outkey) {
for (indexbit = 0, iterbit = 1;
indexbit < mansearch_keymax;
indexbit++, iterbit <<= 1) {
- if (0 == strcasecmp(outkey,
+ if (0 == strcasecmp(search->outkey,
mansearch_keynames[indexbit])) {
outbit = iterbit;
break;
mp->pageid = pageid;
mp->form = sqlite3_column_int(s, 1);
if (TYPE_Nd == outbit)
- mp->desc = mandoc_strdup(
+ mp->desc = mandoc_strdup((const char *)
sqlite3_column_text(s, 0));
ohash_insert(&htab, idx, mp);
}
maxres, sizeof(struct manpage));
}
mpage = *res + cur;
+ mpage->ipath = i;
mpage->sec = 10;
mpage->form = mp->form;
buildnames(mpage, db, s, mp->pageid,
return(rc);
}
+void
+mansearch_free(struct manpage *res, size_t sz)
+{
+ size_t i;
+
+ for (i = 0; i < sz; i++) {
+ free(res[i].file);
+ free(res[i].names);
+ free(res[i].output);
+ }
+ free(res);
+}
+
static int
manpage_compare(const void *vp1, const void *vp2)
{
{
char *newnames, *prevsec, *prevarch;
const char *oldnames, *sep1, *name, *sec, *sep2, *arch, *fsec;
+ const char *gzip;
size_t i;
int c;
/* Fetch the next name. */
- sec = sqlite3_column_text(s, 0);
- arch = sqlite3_column_text(s, 1);
- name = sqlite3_column_text(s, 2);
+ sec = (const char *)sqlite3_column_text(s, 0);
+ arch = (const char *)sqlite3_column_text(s, 1);
+ name = (const char *)sqlite3_column_text(s, 2);
/* Remember the first section found. */
if (NULL != mpage->file)
continue;
- if (form) {
+ if (form & FORM_SRC) {
sep1 = "man";
fsec = sec;
} else {
sep1 = "cat";
fsec = "0";
}
+ if (form & FORM_GZ)
+ gzip = ".gz";
+ else
+ gzip = "";
sep2 = '\0' == *arch ? "" : "/";
- mandoc_asprintf(&mpage->file, "%s/%s%s%s%s/%s.%s",
- path, sep1, sec, sep2, arch, name, fsec);
+ mandoc_asprintf(&mpage->file, "%s/%s%s%s%s/%s.%s%s",
+ path, sep1, sec, sep2, arch, name, fsec, gzip);
}
if (SQLITE_DONE != c)
fprintf(stderr, "%s\n", sqlite3_errmsg(db));
oldoutput = output;
sep1 = " # ";
}
- data = sqlite3_column_text(s, 1);
+ data = (const char *)sqlite3_column_text(s, 1);
mandoc_asprintf(&newoutput, "%s%s%s",
oldoutput, sep1, data);
free(output);
e = mandoc_calloc(1, sizeof(struct expr));
- if (MANSEARCH_MAN & search->flags) {
- e->bits = search->deftype;
+ if (search->argmode == ARG_NAME) {
+ e->bits = TYPE_Nm;
e->substr = buf;
e->equal = 1;
return(e);
}
/*
- * Look for an '=' or '~' operator,
- * unless forced to some fixed macro keys.
+ * Separate macro keys from search string.
+ * If needed, request regular expression handling
+ * by setting e->substr to NULL.
*/
- if (MANSEARCH_WHATIS & search->flags)
- val = NULL;
- else
- val = strpbrk(buf, "=~");
-
- if (NULL == val) {
- e->bits = search->deftype;
+ if (search->argmode == ARG_WORD) {
+ e->bits = TYPE_Nm;
+ e->substr = NULL;
+ mandoc_asprintf(&val, "[[:<:]]%s[[:>:]]", buf);
+ cs = 0;
+ } else if ((val = strpbrk(buf, "=~")) == NULL) {
+ e->bits = TYPE_Nm | TYPE_Nd;
e->substr = buf;
-
- /*
- * Found an operator.
- * Regexp search is requested by !e->substr.
- */
-
} else {
if (val == buf)
- e->bits = search->deftype;
+ e->bits = TYPE_Nm | TYPE_Nd;
if ('=' == *val)
e->substr = val + 1;
*val++ = '\0';
/* Compile regular expressions. */
- if (MANSEARCH_WHATIS & search->flags) {
- e->substr = NULL;
- mandoc_asprintf(&val, "[[:<:]]%s[[:>:]]", buf);
- }
-
if (NULL == e->substr) {
irc = regcomp(&e->regexp, val,
REG_EXTENDED | REG_NOSUB | (cs ? 0 : REG_ICASE));
- if (MANSEARCH_WHATIS & search->flags)
+ if (search->argmode == ARG_WORD)
free(val);
if (irc) {
regerror(irc, &e->regexp, errbuf, sizeof(errbuf));