]> git.cameronkatri.com Git - mandoc.git/blobdiff - mansearch.c
One of the WARNING messages has to use the word "section" twice in two
[mandoc.git] / mansearch.c
index 70e6ad05cbf7f49485154b19e3f1225163187d3f..116b64dcb8d4bdad15ea8935ec8c21489431d17e 100644 (file)
@@ -1,4 +1,4 @@
-/*     $Id: mansearch.c,v 1.4 2012/06/09 11:00:13 kristaps Exp $ */
+/*     $Id: mansearch.c,v 1.6 2013/06/05 02:00:26 schwarze Exp $ */
 /*
  * Copyright (c) 2012 Kristaps Dzonsons <kristaps@bsd.lv>
  *
 #include "config.h"
 #endif
 
-#include <sys/param.h>
-
 #include <assert.h>
 #include <fcntl.h>
 #include <getopt.h>
+#include <limits.h>
 #include <stdio.h>
 #include <stdint.h>
 #include <stddef.h>
@@ -39,7 +38,6 @@
 
 #include "mandoc.h"
 #include "manpath.h"
-#include "mandocdb.h"
 #include "mansearch.h"
 
 #define        SQL_BIND_TEXT(_db, _s, _i, _v) \
@@ -117,21 +115,22 @@ static    const struct type types[] = {
 static void            *hash_alloc(size_t, void *);
 static void             hash_free(void *, size_t, void *);
 static void            *hash_halloc(size_t, void *);
-static struct expr     *exprcomp(int, char *[]);
+static struct expr     *exprcomp(const struct mansearch *, 
+                               int, char *[]);
 static void             exprfree(struct expr *);
-static struct expr     *exprterm(char *);
+static struct expr     *exprterm(const struct mansearch *, char *);
 static char            *sql_statement(const struct expr *,
                                const char *, const char *);
 
 int
-mansearch(const struct manpaths *paths, 
-               const char *arch, const char *sec,
+mansearch(const struct mansearch *search,
+               const struct manpaths *paths, 
                int argc, char *argv[], 
                struct manpage **res, size_t *sz)
 {
        int              fd, rc, c;
        int64_t          id;
-       char             buf[MAXPATHLEN];
+       char             buf[PATH_MAX];
        char            *sql;
        struct expr     *e, *ep;
        sqlite3         *db;
@@ -158,7 +157,7 @@ mansearch(const struct manpaths *paths,
 
        if (0 == argc)
                goto out;
-       if (NULL == (e = exprcomp(argc, argv)))
+       if (NULL == (e = exprcomp(search, argc, argv)))
                goto out;
 
        /*
@@ -168,7 +167,7 @@ mansearch(const struct manpaths *paths,
         * on our current directory from which to start the chdir().
         */
 
-       if (NULL == getcwd(buf, MAXPATHLEN)) {
+       if (NULL == getcwd(buf, PATH_MAX)) {
                perror(NULL);
                goto out;
        } else if (-1 == (fd = open(buf, O_RDONLY, 0))) {
@@ -176,7 +175,7 @@ mansearch(const struct manpaths *paths,
                goto out;
        }
 
-       sql = sql_statement(e, arch, sec);
+       sql = sql_statement(e, search->arch, search->sec);
 
        /*
         * Loop over the directories (containing databases) for us to
@@ -211,10 +210,10 @@ mansearch(const struct manpaths *paths,
                if (SQLITE_OK != c)
                        fprintf(stderr, "%s\n", sqlite3_errmsg(db));
 
-               if (NULL != arch)
-                       SQL_BIND_TEXT(db, s, j, arch);
-               if (NULL != sec)
-                       SQL_BIND_TEXT(db, s, j, arch);
+               if (NULL != search->arch)
+                       SQL_BIND_TEXT(db, s, j, search->arch);
+               if (NULL != search->sec)
+                       SQL_BIND_TEXT(db, s, j, search->sec);
 
                for (ep = e; NULL != ep; ep = ep->next) {
                        SQL_BIND_TEXT(db, s, j, ep->v);
@@ -266,9 +265,9 @@ mansearch(const struct manpaths *paths,
                                        (*res, maxres * sizeof(struct manpage));
                        }
                        strlcpy((*res)[cur].file, 
-                               paths->paths[i], MAXPATHLEN);
-                       strlcat((*res)[cur].file, "/", MAXPATHLEN);
-                       strlcat((*res)[cur].file, mp->file, MAXPATHLEN);
+                               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;
                        free(mp->file);
@@ -346,7 +345,7 @@ sql_statement(const struct expr *e, const char *arch, const char *sec)
  * "(", "foo=bar", etc.).
  */
 static struct expr *
-exprcomp(int argc, char *argv[])
+exprcomp(const struct mansearch *search, int argc, char *argv[])
 {
        int              i;
        struct expr     *first, *next, *cur;
@@ -354,7 +353,7 @@ exprcomp(int argc, char *argv[])
        first = cur = NULL;
 
        for (i = 0; i < argc; i++) {
-               next = exprterm(argv[i]);
+               next = exprterm(search, argv[i]);
                if (NULL == next) {
                        exprfree(first);
                        return(NULL);
@@ -370,7 +369,7 @@ exprcomp(int argc, char *argv[])
 }
 
 static struct expr *
-exprterm(char *buf)
+exprterm(const struct mansearch *search, char *buf)
 {
        struct expr     *e;
        char            *key, *v;
@@ -381,6 +380,14 @@ exprterm(char *buf)
 
        e = mandoc_calloc(1, sizeof(struct expr));
 
+       /*"whatis" mode uses an opaque string and default fields. */
+
+       if (MANSEARCH_WHATIS & search->flags) {
+               e->v = buf;
+               e->bits = search->deftype;
+               return(e);
+       }
+
        /*
         * If no =~ is specified, search with equality over names and
         * descriptions.
@@ -389,10 +396,10 @@ exprterm(char *buf)
 
        if (NULL == (v = strpbrk(buf, "=~"))) {
                e->v = buf;
-               e->bits = TYPE_Nm | TYPE_Nd;
+               e->bits = search->deftype;
                return(e);
        } else if (v == buf)
-               e->bits = TYPE_Nm | TYPE_Nd;
+               e->bits = search->deftype;
 
        e->glob = '~' == *v;
        *v++ = '\0';