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