Annotation of src/usr.bin/mandoc/mansearch.c, Revision 1.18
1.18 ! schwarze 1: /* $Id: mansearch.c,v 1.17 2014/04/09 21:49:31 schwarze Exp $ */
1.1 schwarze 2: /*
3: * Copyright (c) 2012 Kristaps Dzonsons <kristaps@bsd.lv>
1.4 schwarze 4: * Copyright (c) 2013, 2014 Ingo Schwarze <schwarze@openbsd.org>
1.1 schwarze 5: *
6: * Permission to use, copy, modify, and distribute this software for any
7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
9: *
10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17: */
18: #include <assert.h>
19: #include <fcntl.h>
20: #include <getopt.h>
21: #include <limits.h>
22: #include <regex.h>
23: #include <stdio.h>
24: #include <stdint.h>
25: #include <stddef.h>
26: #include <stdlib.h>
27: #include <string.h>
28: #include <unistd.h>
29:
30: #include <ohash.h>
31: #include <sqlite3.h>
32:
33: #include "mandoc.h"
1.14 schwarze 34: #include "mandoc_aux.h"
1.1 schwarze 35: #include "manpath.h"
36: #include "mansearch.h"
37:
1.11 schwarze 38: extern int mansearch_keymax;
39: extern const char *const mansearch_keynames[];
40:
1.1 schwarze 41: #define SQL_BIND_TEXT(_db, _s, _i, _v) \
42: do { if (SQLITE_OK != sqlite3_bind_text \
43: ((_s), (_i)++, (_v), -1, SQLITE_STATIC)) \
44: fprintf(stderr, "%s\n", sqlite3_errmsg((_db))); \
45: } while (0)
46: #define SQL_BIND_INT64(_db, _s, _i, _v) \
47: do { if (SQLITE_OK != sqlite3_bind_int64 \
48: ((_s), (_i)++, (_v))) \
49: fprintf(stderr, "%s\n", sqlite3_errmsg((_db))); \
50: } while (0)
51: #define SQL_BIND_BLOB(_db, _s, _i, _v) \
52: do { if (SQLITE_OK != sqlite3_bind_blob \
53: ((_s), (_i)++, (&_v), sizeof(_v), SQLITE_STATIC)) \
54: fprintf(stderr, "%s\n", sqlite3_errmsg((_db))); \
55: } while (0)
56:
57: struct expr {
58: uint64_t bits; /* type-mask */
59: const char *substr; /* to search for, if applicable */
60: regex_t regexp; /* compiled regexp, if applicable */
1.4 schwarze 61: int open; /* opening parentheses before */
62: int and; /* logical AND before */
63: int close; /* closing parentheses after */
1.1 schwarze 64: struct expr *next; /* next in sequence */
65: };
66:
67: struct match {
68: uint64_t id; /* identifier in database */
1.17 schwarze 69: char *desc; /* manual page description */
1.1 schwarze 70: int form; /* 0 == catpage */
71: };
72:
1.8 schwarze 73: static void buildnames(struct manpage *, sqlite3 *,
1.10 schwarze 74: sqlite3_stmt *, uint64_t,
75: const char *, int form);
1.3 schwarze 76: static char *buildoutput(sqlite3 *, sqlite3_stmt *,
77: uint64_t, uint64_t);
1.1 schwarze 78: static void *hash_alloc(size_t, void *);
79: static void hash_free(void *, size_t, void *);
80: static void *hash_halloc(size_t, void *);
81: static struct expr *exprcomp(const struct mansearch *,
82: int, char *[]);
83: static void exprfree(struct expr *);
1.6 schwarze 84: static struct expr *exprspec(struct expr *, uint64_t,
85: const char *, const char *);
1.1 schwarze 86: static struct expr *exprterm(const struct mansearch *, char *, int);
1.4 schwarze 87: static void sql_append(char **sql, size_t *sz,
88: const char *newstr, int count);
1.1 schwarze 89: static void sql_match(sqlite3_context *context,
90: int argc, sqlite3_value **argv);
91: static void sql_regexp(sqlite3_context *context,
92: int argc, sqlite3_value **argv);
1.6 schwarze 93: static char *sql_statement(const struct expr *);
1.1 schwarze 94:
95: int
96: mansearch(const struct mansearch *search,
1.3 schwarze 97: const struct manpaths *paths,
98: int argc, char *argv[],
99: const char *outkey,
1.1 schwarze 100: struct manpage **res, size_t *sz)
101: {
1.11 schwarze 102: int fd, rc, c, indexbit;
1.1 schwarze 103: int64_t id;
1.11 schwarze 104: uint64_t outbit, iterbit;
1.1 schwarze 105: char buf[PATH_MAX];
1.2 schwarze 106: char *sql;
1.1 schwarze 107: struct manpage *mpage;
108: struct expr *e, *ep;
109: sqlite3 *db;
1.3 schwarze 110: sqlite3_stmt *s, *s2;
1.1 schwarze 111: struct match *mp;
112: struct ohash_info info;
113: struct ohash htab;
114: unsigned int idx;
115: size_t i, j, cur, maxres;
116:
117: memset(&info, 0, sizeof(struct ohash_info));
118:
119: info.halloc = hash_halloc;
120: info.alloc = hash_alloc;
121: info.hfree = hash_free;
122: info.key_offset = offsetof(struct match, id);
123:
124: *sz = cur = maxres = 0;
125: sql = NULL;
126: *res = NULL;
127: fd = -1;
128: e = NULL;
129: rc = 0;
130:
131: if (0 == argc)
132: goto out;
133: if (NULL == (e = exprcomp(search, argc, argv)))
134: goto out;
135:
1.3 schwarze 136: outbit = 0;
137: if (NULL != outkey) {
1.11 schwarze 138: for (indexbit = 0, iterbit = 1;
139: indexbit < mansearch_keymax;
140: indexbit++, iterbit <<= 1) {
141: if (0 == strcasecmp(outkey,
142: mansearch_keynames[indexbit])) {
143: outbit = iterbit;
1.3 schwarze 144: break;
145: }
146: }
147: }
148:
1.1 schwarze 149: /*
150: * Save a descriptor to the current working directory.
151: * Since pathnames in the "paths" variable might be relative,
152: * and we'll be chdir()ing into them, we need to keep a handle
153: * on our current directory from which to start the chdir().
154: */
155:
156: if (NULL == getcwd(buf, PATH_MAX)) {
157: perror(NULL);
158: goto out;
159: } else if (-1 == (fd = open(buf, O_RDONLY, 0))) {
160: perror(buf);
161: goto out;
162: }
163:
1.6 schwarze 164: sql = sql_statement(e);
1.1 schwarze 165:
166: /*
167: * Loop over the directories (containing databases) for us to
168: * search.
169: * Don't let missing/bad databases/directories phase us.
170: * In each, try to open the resident database and, if it opens,
171: * scan it for our match expression.
172: */
173:
174: for (i = 0; i < paths->sz; i++) {
175: if (-1 == fchdir(fd)) {
176: perror(buf);
177: free(*res);
178: break;
179: } else if (-1 == chdir(paths->paths[i])) {
180: perror(paths->paths[i]);
181: continue;
182: }
183:
184: c = sqlite3_open_v2
185: (MANDOC_DB, &db,
186: SQLITE_OPEN_READONLY, NULL);
187:
188: if (SQLITE_OK != c) {
189: perror(MANDOC_DB);
190: sqlite3_close(db);
191: continue;
192: }
193:
194: /*
195: * Define the SQL functions for substring
196: * and regular expression matching.
197: */
198:
199: c = sqlite3_create_function(db, "match", 2,
200: SQLITE_ANY, NULL, sql_match, NULL, NULL);
201: assert(SQLITE_OK == c);
202: c = sqlite3_create_function(db, "regexp", 2,
203: SQLITE_ANY, NULL, sql_regexp, NULL, NULL);
204: assert(SQLITE_OK == c);
205:
206: j = 1;
207: c = sqlite3_prepare_v2(db, sql, -1, &s, NULL);
208: if (SQLITE_OK != c)
209: fprintf(stderr, "%s\n", sqlite3_errmsg(db));
210:
211: for (ep = e; NULL != ep; ep = ep->next) {
212: if (NULL == ep->substr) {
213: SQL_BIND_BLOB(db, s, j, ep->regexp);
214: } else
215: SQL_BIND_TEXT(db, s, j, ep->substr);
1.18 ! schwarze 216: if (0 == ((TYPE_Nd | TYPE_Nm) & ep->bits))
1.17 schwarze 217: SQL_BIND_INT64(db, s, j, ep->bits);
1.1 schwarze 218: }
219:
220: memset(&htab, 0, sizeof(struct ohash));
221: ohash_init(&htab, 4, &info);
222:
223: /*
224: * Hash each entry on its [unique] document identifier.
225: * This is a uint64_t.
226: * Instead of using a hash function, simply convert the
227: * uint64_t to a uint32_t, the hash value's type.
228: * This gives good performance and preserves the
229: * distribution of buckets in the table.
230: */
231: while (SQLITE_ROW == (c = sqlite3_step(s))) {
1.17 schwarze 232: id = sqlite3_column_int64(s, 2);
1.1 schwarze 233: idx = ohash_lookup_memory
1.17 schwarze 234: (&htab, (char *)&id,
1.1 schwarze 235: sizeof(uint64_t), (uint32_t)id);
236:
237: if (NULL != ohash_find(&htab, idx))
238: continue;
239:
240: mp = mandoc_calloc(1, sizeof(struct match));
241: mp->id = id;
1.17 schwarze 242: mp->form = sqlite3_column_int(s, 1);
243: if (TYPE_Nd == outbit)
244: mp->desc = mandoc_strdup(
245: sqlite3_column_text(s, 0));
1.1 schwarze 246: ohash_insert(&htab, idx, mp);
247: }
248:
249: if (SQLITE_DONE != c)
250: fprintf(stderr, "%s\n", sqlite3_errmsg(db));
251:
252: sqlite3_finalize(s);
253:
254: c = sqlite3_prepare_v2(db,
1.13 schwarze 255: "SELECT * FROM mlinks WHERE pageid=?"
256: " ORDER BY sec, arch, name",
1.1 schwarze 257: -1, &s, NULL);
258: if (SQLITE_OK != c)
259: fprintf(stderr, "%s\n", sqlite3_errmsg(db));
260:
1.3 schwarze 261: c = sqlite3_prepare_v2(db,
262: "SELECT * FROM keys WHERE pageid=? AND bits & ?",
263: -1, &s2, NULL);
264: if (SQLITE_OK != c)
265: fprintf(stderr, "%s\n", sqlite3_errmsg(db));
266:
1.1 schwarze 267: for (mp = ohash_first(&htab, &idx);
268: NULL != mp;
269: mp = ohash_next(&htab, &idx)) {
270: if (cur + 1 > maxres) {
271: maxres += 1024;
272: *res = mandoc_realloc
273: (*res, maxres * sizeof(struct manpage));
274: }
275: mpage = *res + cur;
276: mpage->form = mp->form;
1.10 schwarze 277: buildnames(mpage, db, s, mp->id,
278: paths->paths[i], mp->form);
1.17 schwarze 279: mpage->output = TYPE_Nd & outbit ?
280: mp->desc : outbit ?
1.3 schwarze 281: buildoutput(db, s2, mp->id, outbit) : NULL;
1.1 schwarze 282:
283: free(mp);
284: cur++;
285: }
286:
287: sqlite3_finalize(s);
1.3 schwarze 288: sqlite3_finalize(s2);
1.1 schwarze 289: sqlite3_close(db);
290: ohash_delete(&htab);
291: }
292: rc = 1;
293: out:
294: exprfree(e);
295: if (-1 != fd)
296: close(fd);
297: free(sql);
298: *sz = cur;
299: return(rc);
1.2 schwarze 300: }
301:
1.8 schwarze 302: static void
303: buildnames(struct manpage *mpage, sqlite3 *db, sqlite3_stmt *s,
1.10 schwarze 304: uint64_t id, const char *path, int form)
1.2 schwarze 305: {
1.13 schwarze 306: char *newnames, *prevsec, *prevarch;
1.10 schwarze 307: const char *oldnames, *sep1, *name, *sec, *sep2, *arch, *fsec;
1.2 schwarze 308: size_t i;
309: int c;
310:
1.16 schwarze 311: mpage->file = NULL;
1.8 schwarze 312: mpage->names = NULL;
1.13 schwarze 313: prevsec = prevarch = NULL;
1.2 schwarze 314: i = 1;
315: SQL_BIND_INT64(db, s, i, id);
316: while (SQLITE_ROW == (c = sqlite3_step(s))) {
1.8 schwarze 317:
1.13 schwarze 318: /* Decide whether we already have some names. */
1.8 schwarze 319:
320: if (NULL == mpage->names) {
1.2 schwarze 321: oldnames = "";
322: sep1 = "";
323: } else {
1.8 schwarze 324: oldnames = mpage->names;
1.2 schwarze 325: sep1 = ", ";
326: }
1.13 schwarze 327:
328: /* Fetch the next name. */
329:
1.10 schwarze 330: sec = sqlite3_column_text(s, 0);
331: arch = sqlite3_column_text(s, 1);
332: name = sqlite3_column_text(s, 2);
1.13 schwarze 333:
334: /* If the section changed, append the old one. */
335:
336: if (NULL != prevsec &&
337: (strcmp(sec, prevsec) ||
338: strcmp(arch, prevarch))) {
339: sep2 = '\0' == *prevarch ? "" : "/";
1.15 schwarze 340: mandoc_asprintf(&newnames, "%s(%s%s%s)",
341: oldnames, prevsec, sep2, prevarch);
1.13 schwarze 342: free(mpage->names);
343: oldnames = mpage->names = newnames;
344: free(prevsec);
345: free(prevarch);
346: prevsec = prevarch = NULL;
347: }
348:
349: /* Save the new section, to append it later. */
350:
351: if (NULL == prevsec) {
352: prevsec = mandoc_strdup(sec);
353: prevarch = mandoc_strdup(arch);
354: }
355:
356: /* Append the new name. */
357:
1.15 schwarze 358: mandoc_asprintf(&newnames, "%s%s%s",
359: oldnames, sep1, name);
1.8 schwarze 360: free(mpage->names);
361: mpage->names = newnames;
362:
363: /* Also save the first file name encountered. */
364:
365: if (NULL != mpage->file)
366: continue;
367:
1.10 schwarze 368: if (form) {
369: sep1 = "man";
370: fsec = sec;
371: } else {
372: sep1 = "cat";
373: fsec = "0";
374: }
1.13 schwarze 375: sep2 = '\0' == *arch ? "" : "/";
1.15 schwarze 376: mandoc_asprintf(&mpage->file, "%s/%s%s%s%s/%s.%s",
377: path, sep1, sec, sep2, arch, name, fsec);
1.2 schwarze 378: }
379: if (SQLITE_DONE != c)
380: fprintf(stderr, "%s\n", sqlite3_errmsg(db));
381: sqlite3_reset(s);
1.13 schwarze 382:
383: /* Append one final section to the names. */
384:
385: if (NULL != prevsec) {
386: sep2 = '\0' == *prevarch ? "" : "/";
1.15 schwarze 387: mandoc_asprintf(&newnames, "%s(%s%s%s)",
388: mpage->names, prevsec, sep2, prevarch);
1.13 schwarze 389: free(mpage->names);
390: mpage->names = newnames;
391: free(prevsec);
392: free(prevarch);
393: }
1.3 schwarze 394: }
395:
396: static char *
397: buildoutput(sqlite3 *db, sqlite3_stmt *s, uint64_t id, uint64_t outbit)
398: {
399: char *output, *newoutput;
400: const char *oldoutput, *sep1, *data;
401: size_t i;
402: int c;
403:
404: output = NULL;
405: i = 1;
406: SQL_BIND_INT64(db, s, i, id);
407: SQL_BIND_INT64(db, s, i, outbit);
408: while (SQLITE_ROW == (c = sqlite3_step(s))) {
409: if (NULL == output) {
410: oldoutput = "";
411: sep1 = "";
412: } else {
413: oldoutput = output;
414: sep1 = " # ";
415: }
416: data = sqlite3_column_text(s, 1);
1.15 schwarze 417: mandoc_asprintf(&newoutput, "%s%s%s",
418: oldoutput, sep1, data);
1.3 schwarze 419: free(output);
420: output = newoutput;
421: }
422: if (SQLITE_DONE != c)
423: fprintf(stderr, "%s\n", sqlite3_errmsg(db));
424: sqlite3_reset(s);
425: return(output);
1.1 schwarze 426: }
427:
428: /*
429: * Implement substring match as an application-defined SQL function.
430: * Using the SQL LIKE or GLOB operators instead would be a bad idea
431: * because that would require escaping metacharacters in the string
432: * being searched for.
433: */
434: static void
435: sql_match(sqlite3_context *context, int argc, sqlite3_value **argv)
436: {
437:
438: assert(2 == argc);
439: sqlite3_result_int(context, NULL != strcasestr(
440: (const char *)sqlite3_value_text(argv[1]),
441: (const char *)sqlite3_value_text(argv[0])));
442: }
443:
444: /*
445: * Implement regular expression match
446: * as an application-defined SQL function.
447: */
448: static void
449: sql_regexp(sqlite3_context *context, int argc, sqlite3_value **argv)
450: {
451:
452: assert(2 == argc);
453: sqlite3_result_int(context, !regexec(
454: (regex_t *)sqlite3_value_blob(argv[0]),
455: (const char *)sqlite3_value_text(argv[1]),
456: 0, NULL, 0));
457: }
458:
1.4 schwarze 459: static void
460: sql_append(char **sql, size_t *sz, const char *newstr, int count)
461: {
462: size_t newsz;
463:
464: newsz = 1 < count ? (size_t)count : strlen(newstr);
465: *sql = mandoc_realloc(*sql, *sz + newsz + 1);
466: if (1 < count)
467: memset(*sql + *sz, *newstr, (size_t)count);
468: else
469: memcpy(*sql + *sz, newstr, newsz);
470: *sz += newsz;
471: (*sql)[*sz] = '\0';
472: }
473:
1.1 schwarze 474: /*
475: * Prepare the search SQL statement.
476: */
477: static char *
1.6 schwarze 478: sql_statement(const struct expr *e)
1.1 schwarze 479: {
480: char *sql;
481: size_t sz;
1.4 schwarze 482: int needop;
1.1 schwarze 483:
1.4 schwarze 484: sql = mandoc_strdup("SELECT * FROM mpages WHERE ");
1.1 schwarze 485: sz = strlen(sql);
486:
1.4 schwarze 487: for (needop = 0; NULL != e; e = e->next) {
488: if (e->and)
489: sql_append(&sql, &sz, " AND ", 1);
490: else if (needop)
491: sql_append(&sql, &sz, " OR ", 1);
492: if (e->open)
493: sql_append(&sql, &sz, "(", e->open);
1.17 schwarze 494: sql_append(&sql, &sz,
495: TYPE_Nd & e->bits
496: ? (NULL == e->substr
497: ? "desc REGEXP ?"
498: : "desc MATCH ?")
1.18 ! schwarze 499: : TYPE_Nm == e->bits
! 500: ? (NULL == e->substr
! 501: ? "id IN (SELECT pageid FROM names "
! 502: "WHERE name REGEXP ?)"
! 503: : "id IN (SELECT pageid FROM names "
! 504: "WHERE name MATCH ?)")
1.17 schwarze 505: : (NULL == e->substr
506: ? "id IN (SELECT pageid FROM keys "
507: "WHERE key REGEXP ? AND bits & ?)"
508: : "id IN (SELECT pageid FROM keys "
509: "WHERE key MATCH ? AND bits & ?)"), 1);
1.4 schwarze 510: if (e->close)
511: sql_append(&sql, &sz, ")", e->close);
512: needop = 1;
1.1 schwarze 513: }
514:
515: return(sql);
516: }
517:
518: /*
519: * Compile a set of string tokens into an expression.
520: * Tokens in "argv" are assumed to be individual expression atoms (e.g.,
521: * "(", "foo=bar", etc.).
522: */
523: static struct expr *
524: exprcomp(const struct mansearch *search, int argc, char *argv[])
525: {
1.18 ! schwarze 526: uint64_t mask;
1.4 schwarze 527: int i, toopen, logic, igncase, toclose;
1.18 ! schwarze 528: struct expr *first, *prev, *cur, *next;
1.1 schwarze 529:
530: first = cur = NULL;
1.6 schwarze 531: logic = igncase = toclose = 0;
532: toopen = 1;
1.1 schwarze 533:
534: for (i = 0; i < argc; i++) {
1.4 schwarze 535: if (0 == strcmp("(", argv[i])) {
536: if (igncase)
537: goto fail;
538: toopen++;
539: toclose++;
540: continue;
541: } else if (0 == strcmp(")", argv[i])) {
542: if (toopen || logic || igncase || NULL == cur)
543: goto fail;
544: cur->close++;
545: if (0 > --toclose)
546: goto fail;
547: continue;
548: } else if (0 == strcmp("-a", argv[i])) {
549: if (toopen || logic || igncase || NULL == cur)
550: goto fail;
551: logic = 1;
552: continue;
553: } else if (0 == strcmp("-o", argv[i])) {
554: if (toopen || logic || igncase || NULL == cur)
555: goto fail;
556: logic = 2;
557: continue;
558: } else if (0 == strcmp("-i", argv[i])) {
559: if (igncase)
560: goto fail;
561: igncase = 1;
562: continue;
1.1 schwarze 563: }
1.4 schwarze 564: next = exprterm(search, argv[i], !igncase);
565: if (NULL == next)
566: goto fail;
1.17 schwarze 567: if (NULL == first)
568: first = next;
569: else
1.1 schwarze 570: cur->next = next;
1.18 ! schwarze 571: prev = cur = next;
1.17 schwarze 572:
573: /*
574: * Searching for descriptions must be split out
575: * because they are stored in the mpages table,
576: * not in the keys table.
577: */
578:
1.18 ! schwarze 579: for (mask = TYPE_Nm; mask <= TYPE_Nd; mask <<= 1) {
! 580: if (mask & cur->bits && ~mask & cur->bits) {
! 581: next = mandoc_calloc(1,
! 582: sizeof(struct expr));
! 583: memcpy(next, cur, sizeof(struct expr));
! 584: prev->open = 1;
! 585: cur->bits = mask;
! 586: cur->next = next;
! 587: cur = next;
! 588: cur->bits &= ~mask;
! 589: }
! 590: }
! 591: prev->and = (1 == logic);
! 592: prev->open += toopen;
! 593: if (cur != prev)
1.17 schwarze 594: cur->close = 1;
1.18 ! schwarze 595:
1.4 schwarze 596: toopen = logic = igncase = 0;
1.1 schwarze 597: }
1.6 schwarze 598: if (toopen || logic || igncase || toclose)
599: goto fail;
600:
601: cur->close++;
602: cur = exprspec(cur, TYPE_arch, search->arch, "^(%s|any)$");
603: exprspec(cur, TYPE_sec, search->sec, "^%s$");
604:
605: return(first);
606:
1.4 schwarze 607: fail:
608: if (NULL != first)
609: exprfree(first);
610: return(NULL);
1.1 schwarze 611: }
612:
613: static struct expr *
1.6 schwarze 614: exprspec(struct expr *cur, uint64_t key, const char *value,
615: const char *format)
616: {
617: char errbuf[BUFSIZ];
618: char *cp;
619: int irc;
620:
621: if (NULL == value)
622: return(cur);
623:
1.15 schwarze 624: mandoc_asprintf(&cp, format, value);
1.6 schwarze 625: cur->next = mandoc_calloc(1, sizeof(struct expr));
626: cur = cur->next;
627: cur->and = 1;
628: cur->bits = key;
629: if (0 != (irc = regcomp(&cur->regexp, cp,
630: REG_EXTENDED | REG_NOSUB | REG_ICASE))) {
631: regerror(irc, &cur->regexp, errbuf, sizeof(errbuf));
632: fprintf(stderr, "regcomp: %s\n", errbuf);
633: cur->substr = value;
634: }
635: free(cp);
636: return(cur);
637: }
638:
639: static struct expr *
1.1 schwarze 640: exprterm(const struct mansearch *search, char *buf, int cs)
641: {
1.6 schwarze 642: char errbuf[BUFSIZ];
1.1 schwarze 643: struct expr *e;
644: char *key, *v;
1.11 schwarze 645: uint64_t iterbit;
646: int i, irc;
1.1 schwarze 647:
648: if ('\0' == *buf)
649: return(NULL);
650:
651: e = mandoc_calloc(1, sizeof(struct expr));
652:
653: /*"whatis" mode uses an opaque string and default fields. */
654:
655: if (MANSEARCH_WHATIS & search->flags) {
656: e->substr = buf;
657: e->bits = search->deftype;
658: return(e);
659: }
660:
661: /*
662: * If no =~ is specified, search with equality over names and
663: * descriptions.
664: * If =~ begins the phrase, use name and description fields.
665: */
666:
667: if (NULL == (v = strpbrk(buf, "=~"))) {
668: e->substr = buf;
669: e->bits = search->deftype;
670: return(e);
671: } else if (v == buf)
672: e->bits = search->deftype;
673:
674: if ('~' == *v++) {
1.12 schwarze 675: if (NULL != strstr(buf, "arch"))
676: cs = 0;
1.6 schwarze 677: if (0 != (irc = regcomp(&e->regexp, v,
678: REG_EXTENDED | REG_NOSUB | (cs ? 0 : REG_ICASE)))) {
679: regerror(irc, &e->regexp, errbuf, sizeof(errbuf));
680: fprintf(stderr, "regcomp: %s\n", errbuf);
1.1 schwarze 681: free(e);
682: return(NULL);
683: }
684: } else
685: e->substr = v;
686: v[-1] = '\0';
687:
688: /*
689: * Parse out all possible fields.
690: * If the field doesn't resolve, bail.
691: */
692:
693: while (NULL != (key = strsep(&buf, ","))) {
694: if ('\0' == *key)
695: continue;
1.11 schwarze 696: for (i = 0, iterbit = 1;
697: i < mansearch_keymax;
698: i++, iterbit <<= 1) {
699: if (0 == strcasecmp(key,
700: mansearch_keynames[i])) {
701: e->bits |= iterbit;
702: break;
703: }
704: }
705: if (i == mansearch_keymax) {
706: if (strcasecmp(key, "any")) {
707: free(e);
708: return(NULL);
709: }
710: e->bits |= ~0ULL;
1.1 schwarze 711: }
712: }
713:
714: return(e);
715: }
716:
717: static void
718: exprfree(struct expr *p)
719: {
720: struct expr *pp;
721:
722: while (NULL != p) {
723: pp = p->next;
724: free(p);
725: p = pp;
726: }
727: }
728:
729: static void *
730: hash_halloc(size_t sz, void *arg)
731: {
732:
733: return(mandoc_calloc(sz, 1));
734: }
735:
736: static void *
737: hash_alloc(size_t sz, void *arg)
738: {
739:
740: return(mandoc_malloc(sz));
741: }
742:
743: static void
744: hash_free(void *p, size_t sz, void *arg)
745: {
746:
747: free(p);
748: }