Annotation of mandoc/mansearch.c, Revision 1.29
1.29 ! schwarze 1: /* $Id: mansearch.c,v 1.28 2014/04/11 15:46:52 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.1 kristaps 213: perror(NULL);
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.29 ! schwarze 221: printf("%s\n", sql);
1.1 kristaps 222:
223: /*
224: * Loop over the directories (containing databases) for us to
225: * search.
226: * Don't let missing/bad databases/directories phase us.
227: * In each, try to open the resident database and, if it opens,
228: * scan it for our match expression.
229: */
230:
231: for (i = 0; i < paths->sz; i++) {
232: if (-1 == fchdir(fd)) {
233: perror(buf);
234: free(*res);
235: break;
236: } else if (-1 == chdir(paths->paths[i])) {
237: perror(paths->paths[i]);
238: continue;
239: }
240:
1.2 kristaps 241: c = sqlite3_open_v2
242: (MANDOC_DB, &db,
243: SQLITE_OPEN_READONLY, NULL);
1.1 kristaps 244:
1.2 kristaps 245: if (SQLITE_OK != c) {
1.1 kristaps 246: perror(MANDOC_DB);
247: sqlite3_close(db);
248: continue;
249: }
250:
1.8 schwarze 251: /*
252: * Define the SQL functions for substring
253: * and regular expression matching.
254: */
1.7 schwarze 255:
256: c = sqlite3_create_function(db, "match", 2,
257: SQLITE_ANY, NULL, sql_match, NULL, NULL);
1.8 schwarze 258: assert(SQLITE_OK == c);
259: c = sqlite3_create_function(db, "regexp", 2,
260: SQLITE_ANY, NULL, sql_regexp, NULL, NULL);
261: assert(SQLITE_OK == c);
1.7 schwarze 262:
1.1 kristaps 263: j = 1;
1.2 kristaps 264: c = sqlite3_prepare_v2(db, sql, -1, &s, NULL);
265: if (SQLITE_OK != c)
266: fprintf(stderr, "%s\n", sqlite3_errmsg(db));
1.1 kristaps 267:
268: for (ep = e; NULL != ep; ep = ep->next) {
1.8 schwarze 269: if (NULL == ep->substr) {
270: SQL_BIND_BLOB(db, s, j, ep->regexp);
271: } else
272: SQL_BIND_TEXT(db, s, j, ep->substr);
1.27 schwarze 273: if (0 == ((TYPE_Nd | TYPE_Nm) & ep->bits))
1.26 schwarze 274: SQL_BIND_INT64(db, s, j, ep->bits);
1.1 kristaps 275: }
276:
277: memset(&htab, 0, sizeof(struct ohash));
278: ohash_init(&htab, 4, &info);
279:
280: /*
281: * Hash each entry on its [unique] document identifier.
282: * This is a uint64_t.
283: * Instead of using a hash function, simply convert the
284: * uint64_t to a uint32_t, the hash value's type.
285: * This gives good performance and preserves the
286: * distribution of buckets in the table.
287: */
1.2 kristaps 288: while (SQLITE_ROW == (c = sqlite3_step(s))) {
1.26 schwarze 289: id = sqlite3_column_int64(s, 2);
1.1 kristaps 290: idx = ohash_lookup_memory
1.26 schwarze 291: (&htab, (char *)&id,
1.1 kristaps 292: sizeof(uint64_t), (uint32_t)id);
293:
294: if (NULL != ohash_find(&htab, idx))
295: continue;
296:
297: mp = mandoc_calloc(1, sizeof(struct match));
298: mp->id = id;
1.26 schwarze 299: mp->form = sqlite3_column_int(s, 1);
300: if (TYPE_Nd == outbit)
301: mp->desc = mandoc_strdup(
302: sqlite3_column_text(s, 0));
1.1 kristaps 303: ohash_insert(&htab, idx, mp);
304: }
305:
1.2 kristaps 306: if (SQLITE_DONE != c)
307: fprintf(stderr, "%s\n", sqlite3_errmsg(db));
308:
1.1 kristaps 309: sqlite3_finalize(s);
1.10 schwarze 310:
311: c = sqlite3_prepare_v2(db,
1.22 schwarze 312: "SELECT * FROM mlinks WHERE pageid=?"
313: " ORDER BY sec, arch, name",
1.10 schwarze 314: -1, &s, NULL);
315: if (SQLITE_OK != c)
316: fprintf(stderr, "%s\n", sqlite3_errmsg(db));
1.1 kristaps 317:
1.12 schwarze 318: c = sqlite3_prepare_v2(db,
319: "SELECT * FROM keys WHERE pageid=? AND bits & ?",
320: -1, &s2, NULL);
321: if (SQLITE_OK != c)
322: fprintf(stderr, "%s\n", sqlite3_errmsg(db));
323:
1.1 kristaps 324: for (mp = ohash_first(&htab, &idx);
325: NULL != mp;
326: mp = ohash_next(&htab, &idx)) {
327: if (cur + 1 > maxres) {
328: maxres += 1024;
329: *res = mandoc_realloc
330: (*res, maxres * sizeof(struct manpage));
331: }
1.10 schwarze 332: mpage = *res + cur;
333: mpage->form = mp->form;
1.19 schwarze 334: buildnames(mpage, db, s, mp->id,
335: paths->paths[i], mp->form);
1.26 schwarze 336: mpage->output = TYPE_Nd & outbit ?
337: mp->desc : outbit ?
1.12 schwarze 338: buildoutput(db, s2, mp->id, outbit) : NULL;
1.10 schwarze 339:
1.1 kristaps 340: free(mp);
341: cur++;
342: }
1.10 schwarze 343:
344: sqlite3_finalize(s);
1.12 schwarze 345: sqlite3_finalize(s2);
1.10 schwarze 346: sqlite3_close(db);
1.1 kristaps 347: ohash_delete(&htab);
348: }
1.2 kristaps 349: rc = 1;
1.1 kristaps 350: out:
351: exprfree(e);
352: if (-1 != fd)
353: close(fd);
354: free(sql);
355: *sz = cur;
1.2 kristaps 356: return(rc);
1.11 schwarze 357: }
358:
1.17 schwarze 359: static void
360: buildnames(struct manpage *mpage, sqlite3 *db, sqlite3_stmt *s,
1.19 schwarze 361: uint64_t id, const char *path, int form)
1.11 schwarze 362: {
1.22 schwarze 363: char *newnames, *prevsec, *prevarch;
1.19 schwarze 364: const char *oldnames, *sep1, *name, *sec, *sep2, *arch, *fsec;
1.11 schwarze 365: size_t i;
366: int c;
367:
1.25 schwarze 368: mpage->file = NULL;
1.17 schwarze 369: mpage->names = NULL;
1.22 schwarze 370: prevsec = prevarch = NULL;
1.11 schwarze 371: i = 1;
372: SQL_BIND_INT64(db, s, i, id);
373: while (SQLITE_ROW == (c = sqlite3_step(s))) {
1.17 schwarze 374:
1.22 schwarze 375: /* Decide whether we already have some names. */
1.17 schwarze 376:
377: if (NULL == mpage->names) {
1.11 schwarze 378: oldnames = "";
379: sep1 = "";
380: } else {
1.17 schwarze 381: oldnames = mpage->names;
1.11 schwarze 382: sep1 = ", ";
383: }
1.22 schwarze 384:
385: /* Fetch the next name. */
386:
1.19 schwarze 387: sec = sqlite3_column_text(s, 0);
388: arch = sqlite3_column_text(s, 1);
389: name = sqlite3_column_text(s, 2);
1.22 schwarze 390:
391: /* If the section changed, append the old one. */
392:
393: if (NULL != prevsec &&
394: (strcmp(sec, prevsec) ||
395: strcmp(arch, prevarch))) {
396: sep2 = '\0' == *prevarch ? "" : "/";
1.24 schwarze 397: mandoc_asprintf(&newnames, "%s(%s%s%s)",
398: oldnames, prevsec, sep2, prevarch);
1.22 schwarze 399: free(mpage->names);
400: oldnames = mpage->names = newnames;
401: free(prevsec);
402: free(prevarch);
403: prevsec = prevarch = NULL;
404: }
405:
406: /* Save the new section, to append it later. */
407:
408: if (NULL == prevsec) {
409: prevsec = mandoc_strdup(sec);
410: prevarch = mandoc_strdup(arch);
411: }
412:
413: /* Append the new name. */
414:
1.24 schwarze 415: mandoc_asprintf(&newnames, "%s%s%s",
416: oldnames, sep1, name);
1.17 schwarze 417: free(mpage->names);
418: mpage->names = newnames;
419:
420: /* Also save the first file name encountered. */
421:
422: if (NULL != mpage->file)
423: continue;
424:
1.19 schwarze 425: if (form) {
426: sep1 = "man";
427: fsec = sec;
428: } else {
429: sep1 = "cat";
430: fsec = "0";
431: }
1.22 schwarze 432: sep2 = '\0' == *arch ? "" : "/";
1.24 schwarze 433: mandoc_asprintf(&mpage->file, "%s/%s%s%s%s/%s.%s",
434: path, sep1, sec, sep2, arch, name, fsec);
1.11 schwarze 435: }
436: if (SQLITE_DONE != c)
437: fprintf(stderr, "%s\n", sqlite3_errmsg(db));
438: sqlite3_reset(s);
1.22 schwarze 439:
440: /* Append one final section to the names. */
441:
442: if (NULL != prevsec) {
443: sep2 = '\0' == *prevarch ? "" : "/";
1.24 schwarze 444: mandoc_asprintf(&newnames, "%s(%s%s%s)",
445: mpage->names, prevsec, sep2, prevarch);
1.22 schwarze 446: free(mpage->names);
447: mpage->names = newnames;
448: free(prevsec);
449: free(prevarch);
450: }
1.12 schwarze 451: }
452:
453: static char *
454: buildoutput(sqlite3 *db, sqlite3_stmt *s, uint64_t id, uint64_t outbit)
455: {
456: char *output, *newoutput;
457: const char *oldoutput, *sep1, *data;
458: size_t i;
459: int c;
460:
461: output = NULL;
462: i = 1;
463: SQL_BIND_INT64(db, s, i, id);
464: SQL_BIND_INT64(db, s, i, outbit);
465: while (SQLITE_ROW == (c = sqlite3_step(s))) {
466: if (NULL == output) {
467: oldoutput = "";
468: sep1 = "";
469: } else {
470: oldoutput = output;
471: sep1 = " # ";
472: }
473: data = sqlite3_column_text(s, 1);
1.24 schwarze 474: mandoc_asprintf(&newoutput, "%s%s%s",
475: oldoutput, sep1, data);
1.12 schwarze 476: free(output);
477: output = newoutput;
478: }
479: if (SQLITE_DONE != c)
480: fprintf(stderr, "%s\n", sqlite3_errmsg(db));
481: sqlite3_reset(s);
482: return(output);
1.1 kristaps 483: }
484:
485: /*
1.7 schwarze 486: * Implement substring match as an application-defined SQL function.
487: * Using the SQL LIKE or GLOB operators instead would be a bad idea
488: * because that would require escaping metacharacters in the string
489: * being searched for.
490: */
491: static void
492: sql_match(sqlite3_context *context, int argc, sqlite3_value **argv)
493: {
494:
495: assert(2 == argc);
496: sqlite3_result_int(context, NULL != strcasestr(
497: (const char *)sqlite3_value_text(argv[1]),
498: (const char *)sqlite3_value_text(argv[0])));
499: }
500:
501: /*
1.8 schwarze 502: * Implement regular expression match
503: * as an application-defined SQL function.
504: */
505: static void
506: sql_regexp(sqlite3_context *context, int argc, sqlite3_value **argv)
507: {
508:
509: assert(2 == argc);
510: sqlite3_result_int(context, !regexec(
511: (regex_t *)sqlite3_value_blob(argv[0]),
512: (const char *)sqlite3_value_text(argv[1]),
513: 0, NULL, 0));
514: }
515:
1.13 schwarze 516: static void
517: sql_append(char **sql, size_t *sz, const char *newstr, int count)
518: {
519: size_t newsz;
520:
521: newsz = 1 < count ? (size_t)count : strlen(newstr);
522: *sql = mandoc_realloc(*sql, *sz + newsz + 1);
523: if (1 < count)
524: memset(*sql + *sz, *newstr, (size_t)count);
525: else
526: memcpy(*sql + *sz, newstr, newsz);
527: *sz += newsz;
528: (*sql)[*sz] = '\0';
529: }
530:
1.8 schwarze 531: /*
1.1 kristaps 532: * Prepare the search SQL statement.
533: */
534: static char *
1.15 schwarze 535: sql_statement(const struct expr *e)
1.1 kristaps 536: {
537: char *sql;
538: size_t sz;
1.13 schwarze 539: int needop;
1.1 kristaps 540:
1.13 schwarze 541: sql = mandoc_strdup("SELECT * FROM mpages WHERE ");
1.1 kristaps 542: sz = strlen(sql);
1.2 kristaps 543:
1.13 schwarze 544: for (needop = 0; NULL != e; e = e->next) {
545: if (e->and)
546: sql_append(&sql, &sz, " AND ", 1);
547: else if (needop)
548: sql_append(&sql, &sz, " OR ", 1);
549: if (e->open)
550: sql_append(&sql, &sz, "(", e->open);
1.26 schwarze 551: sql_append(&sql, &sz,
552: TYPE_Nd & e->bits
553: ? (NULL == e->substr
554: ? "desc REGEXP ?"
555: : "desc MATCH ?")
1.27 schwarze 556: : TYPE_Nm == e->bits
557: ? (NULL == e->substr
558: ? "id IN (SELECT pageid FROM names "
559: "WHERE name REGEXP ?)"
560: : "id IN (SELECT pageid FROM names "
561: "WHERE name MATCH ?)")
1.26 schwarze 562: : (NULL == e->substr
563: ? "id IN (SELECT pageid FROM keys "
564: "WHERE key REGEXP ? AND bits & ?)"
565: : "id IN (SELECT pageid FROM keys "
566: "WHERE key MATCH ? AND bits & ?)"), 1);
1.13 schwarze 567: if (e->close)
568: sql_append(&sql, &sz, ")", e->close);
569: needop = 1;
1.1 kristaps 570: }
571:
572: return(sql);
573: }
574:
575: /*
576: * Compile a set of string tokens into an expression.
577: * Tokens in "argv" are assumed to be individual expression atoms (e.g.,
578: * "(", "foo=bar", etc.).
579: */
580: static struct expr *
1.5 kristaps 581: exprcomp(const struct mansearch *search, int argc, char *argv[])
1.1 kristaps 582: {
1.27 schwarze 583: uint64_t mask;
1.13 schwarze 584: int i, toopen, logic, igncase, toclose;
1.27 schwarze 585: struct expr *first, *prev, *cur, *next;
1.1 kristaps 586:
587: first = cur = NULL;
1.15 schwarze 588: logic = igncase = toclose = 0;
589: toopen = 1;
1.1 kristaps 590:
591: for (i = 0; i < argc; i++) {
1.13 schwarze 592: if (0 == strcmp("(", argv[i])) {
593: if (igncase)
594: goto fail;
595: toopen++;
596: toclose++;
597: continue;
598: } else if (0 == strcmp(")", argv[i])) {
599: if (toopen || logic || igncase || NULL == cur)
600: goto fail;
601: cur->close++;
602: if (0 > --toclose)
603: goto fail;
604: continue;
605: } else if (0 == strcmp("-a", argv[i])) {
606: if (toopen || logic || igncase || NULL == cur)
607: goto fail;
608: logic = 1;
609: continue;
610: } else if (0 == strcmp("-o", argv[i])) {
611: if (toopen || logic || igncase || NULL == cur)
612: goto fail;
613: logic = 2;
614: continue;
615: } else if (0 == strcmp("-i", argv[i])) {
616: if (igncase)
617: goto fail;
618: igncase = 1;
619: continue;
1.1 kristaps 620: }
1.13 schwarze 621: next = exprterm(search, argv[i], !igncase);
622: if (NULL == next)
623: goto fail;
1.26 schwarze 624: if (NULL == first)
625: first = next;
626: else
1.1 kristaps 627: cur->next = next;
1.27 schwarze 628: prev = cur = next;
1.26 schwarze 629:
630: /*
631: * Searching for descriptions must be split out
632: * because they are stored in the mpages table,
633: * not in the keys table.
634: */
635:
1.27 schwarze 636: for (mask = TYPE_Nm; mask <= TYPE_Nd; mask <<= 1) {
637: if (mask & cur->bits && ~mask & cur->bits) {
638: next = mandoc_calloc(1,
639: sizeof(struct expr));
640: memcpy(next, cur, sizeof(struct expr));
641: prev->open = 1;
642: cur->bits = mask;
643: cur->next = next;
644: cur = next;
645: cur->bits &= ~mask;
646: }
647: }
648: prev->and = (1 == logic);
649: prev->open += toopen;
650: if (cur != prev)
1.26 schwarze 651: cur->close = 1;
1.27 schwarze 652:
1.13 schwarze 653: toopen = logic = igncase = 0;
1.1 kristaps 654: }
1.15 schwarze 655: if (toopen || logic || igncase || toclose)
656: goto fail;
657:
658: cur->close++;
659: cur = exprspec(cur, TYPE_arch, search->arch, "^(%s|any)$");
660: exprspec(cur, TYPE_sec, search->sec, "^%s$");
661:
662: return(first);
663:
1.13 schwarze 664: fail:
665: if (NULL != first)
666: exprfree(first);
667: return(NULL);
1.1 kristaps 668: }
669:
670: static struct expr *
1.15 schwarze 671: exprspec(struct expr *cur, uint64_t key, const char *value,
672: const char *format)
673: {
674: char errbuf[BUFSIZ];
675: char *cp;
676: int irc;
677:
678: if (NULL == value)
679: return(cur);
680:
1.24 schwarze 681: mandoc_asprintf(&cp, format, value);
1.15 schwarze 682: cur->next = mandoc_calloc(1, sizeof(struct expr));
683: cur = cur->next;
684: cur->and = 1;
685: cur->bits = key;
686: if (0 != (irc = regcomp(&cur->regexp, cp,
687: REG_EXTENDED | REG_NOSUB | REG_ICASE))) {
688: regerror(irc, &cur->regexp, errbuf, sizeof(errbuf));
689: fprintf(stderr, "regcomp: %s\n", errbuf);
690: cur->substr = value;
691: }
692: free(cp);
693: return(cur);
694: }
695:
696: static struct expr *
1.8 schwarze 697: exprterm(const struct mansearch *search, char *buf, int cs)
1.1 kristaps 698: {
1.15 schwarze 699: char errbuf[BUFSIZ];
1.1 kristaps 700: struct expr *e;
701: char *key, *v;
1.20 schwarze 702: uint64_t iterbit;
703: int i, irc;
1.1 kristaps 704:
705: if ('\0' == *buf)
706: return(NULL);
707:
708: e = mandoc_calloc(1, sizeof(struct expr));
709:
1.5 kristaps 710: /*"whatis" mode uses an opaque string and default fields. */
711:
712: if (MANSEARCH_WHATIS & search->flags) {
1.8 schwarze 713: e->substr = buf;
1.5 kristaps 714: e->bits = search->deftype;
715: return(e);
716: }
717:
1.1 kristaps 718: /*
719: * If no =~ is specified, search with equality over names and
720: * descriptions.
721: * If =~ begins the phrase, use name and description fields.
722: */
723:
724: if (NULL == (v = strpbrk(buf, "=~"))) {
1.8 schwarze 725: e->substr = buf;
1.5 kristaps 726: e->bits = search->deftype;
1.1 kristaps 727: return(e);
728: } else if (v == buf)
1.5 kristaps 729: e->bits = search->deftype;
1.1 kristaps 730:
1.8 schwarze 731: if ('~' == *v++) {
1.21 schwarze 732: if (NULL != strstr(buf, "arch"))
733: cs = 0;
1.15 schwarze 734: if (0 != (irc = regcomp(&e->regexp, v,
735: REG_EXTENDED | REG_NOSUB | (cs ? 0 : REG_ICASE)))) {
736: regerror(irc, &e->regexp, errbuf, sizeof(errbuf));
737: fprintf(stderr, "regcomp: %s\n", errbuf);
1.8 schwarze 738: free(e);
739: return(NULL);
740: }
741: } else
742: e->substr = v;
743: v[-1] = '\0';
1.1 kristaps 744:
745: /*
746: * Parse out all possible fields.
747: * If the field doesn't resolve, bail.
748: */
749:
750: while (NULL != (key = strsep(&buf, ","))) {
751: if ('\0' == *key)
752: continue;
1.20 schwarze 753: for (i = 0, iterbit = 1;
754: i < mansearch_keymax;
755: i++, iterbit <<= 1) {
756: if (0 == strcasecmp(key,
757: mansearch_keynames[i])) {
758: e->bits |= iterbit;
759: break;
760: }
761: }
762: if (i == mansearch_keymax) {
763: if (strcasecmp(key, "any")) {
764: free(e);
765: return(NULL);
766: }
767: e->bits |= ~0ULL;
1.1 kristaps 768: }
769: }
770:
771: return(e);
772: }
773:
774: static void
775: exprfree(struct expr *p)
776: {
777: struct expr *pp;
778:
779: while (NULL != p) {
780: pp = p->next;
781: free(p);
782: p = pp;
783: }
784: }
785:
786: static void *
787: hash_halloc(size_t sz, void *arg)
788: {
789:
790: return(mandoc_calloc(sz, 1));
791: }
792:
793: static void *
794: hash_alloc(size_t sz, void *arg)
795: {
796:
797: return(mandoc_malloc(sz));
798: }
799:
800: static void
801: hash_free(void *p, size_t sz, void *arg)
802: {
803:
804: free(p);
805: }
CVSweb