-/* $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>
#include "mandoc.h"
#include "manpath.h"
-#include "mandocdb.h"
#include "mansearch.h"
#define SQL_BIND_TEXT(_db, _s, _i, _v) \
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;
if (0 == argc)
goto out;
- if (NULL == (e = exprcomp(argc, argv)))
+ if (NULL == (e = exprcomp(search, argc, argv)))
goto out;
/*
* 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))) {
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
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);
(*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);
* "(", "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;
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);
}
static struct expr *
-exprterm(char *buf)
+exprterm(const struct mansearch *search, char *buf)
{
struct expr *e;
char *key, *v;
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.
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';