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