Annotation of src/usr.bin/mandoc/mansearch.c, Revision 1.53
1.53 ! schwarze 1: /* $OpenBSD: mansearch.c,v 1.52 2017/03/03 13:10:55 schwarze Exp $ */
1.1 schwarze 2: /*
3: * Copyright (c) 2012 Kristaps Dzonsons <kristaps@bsd.lv>
1.53 ! schwarze 4: * Copyright (c) 2013-2017 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: *
1.43 schwarze 10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
1.1 schwarze 11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1.43 schwarze 12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
1.1 schwarze 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.33 schwarze 18:
1.19 schwarze 19: #include <sys/mman.h>
1.33 schwarze 20: #include <sys/types.h>
21:
1.1 schwarze 22: #include <assert.h>
1.46 schwarze 23: #include <err.h>
1.39 schwarze 24: #include <errno.h>
1.1 schwarze 25: #include <fcntl.h>
1.41 schwarze 26: #include <glob.h>
1.1 schwarze 27: #include <limits.h>
28: #include <regex.h>
29: #include <stdio.h>
30: #include <stdint.h>
31: #include <stddef.h>
32: #include <stdlib.h>
33: #include <string.h>
34: #include <unistd.h>
35:
36: #include "mandoc.h"
1.14 schwarze 37: #include "mandoc_aux.h"
1.47 schwarze 38: #include "mandoc_ohash.h"
1.43 schwarze 39: #include "manconf.h"
1.1 schwarze 40: #include "mansearch.h"
1.51 schwarze 41: #include "dbm.h"
1.1 schwarze 42:
43: struct expr {
1.51 schwarze 44: /* Used for terms: */
45: struct dbm_match match; /* Match type and expression. */
46: uint64_t bits; /* Type mask. */
47: /* Used for OR and AND groups: */
48: struct expr *next; /* Next child in the parent group. */
49: struct expr *child; /* First child in this group. */
50: enum { EXPR_TERM, EXPR_OR, EXPR_AND } type;
1.1 schwarze 51: };
52:
1.51 schwarze 53: const char *const mansearch_keynames[KEY_MAX] = {
54: "arch", "sec", "Xr", "Ar", "Fa", "Fl", "Dv", "Fn",
55: "Ic", "Pa", "Cm", "Li", "Em", "Cd", "Va", "Ft",
56: "Tn", "Er", "Ev", "Sy", "Sh", "In", "Ss", "Ox",
57: "An", "Mt", "St", "Bx", "At", "Nx", "Fx", "Lk",
58: "Ms", "Bsx", "Dx", "Rs", "Vt", "Lb", "Nm", "Nd"
1.1 schwarze 59: };
60:
1.51 schwarze 61:
62: static struct ohash *manmerge(struct expr *, struct ohash *);
63: static struct ohash *manmerge_term(struct expr *, struct ohash *);
64: static struct ohash *manmerge_or(struct expr *, struct ohash *);
65: static struct ohash *manmerge_and(struct expr *, struct ohash *);
66: static char *buildnames(const struct dbm_page *);
1.53 ! schwarze 67: static char *buildoutput(size_t, struct dbm_page *);
! 68: static size_t lstlen(const char *, size_t);
! 69: static void lstcat(char *, size_t *, const char *, const char *);
1.51 schwarze 70: static int lstmatch(const char *, const char *);
1.24 schwarze 71: static struct expr *exprcomp(const struct mansearch *,
1.51 schwarze 72: int, char *[], int *);
73: static struct expr *expr_and(const struct mansearch *,
74: int, char *[], int *);
75: static struct expr *exprterm(const struct mansearch *,
76: int, char *[], int *);
1.1 schwarze 77: static void exprfree(struct expr *);
1.29 schwarze 78: static int manpage_compare(const void *, const void *);
1.19 schwarze 79:
1.24 schwarze 80:
1.19 schwarze 81: int
1.1 schwarze 82: mansearch(const struct mansearch *search,
1.3 schwarze 83: const struct manpaths *paths,
84: int argc, char *argv[],
1.1 schwarze 85: struct manpage **res, size_t *sz)
86: {
87: char buf[PATH_MAX];
1.51 schwarze 88: struct dbm_res *rp;
89: struct expr *e;
90: struct dbm_page *page;
1.1 schwarze 91: struct manpage *mpage;
1.51 schwarze 92: struct ohash *htab;
93: size_t cur, i, maxres, outkey;
94: unsigned int slot;
95: int argi, chdir_status, getcwd_status, im;
1.44 schwarze 96:
1.51 schwarze 97: argi = 0;
98: if ((e = exprcomp(search, argc, argv, &argi)) == NULL) {
1.44 schwarze 99: *sz = 0;
1.45 schwarze 100: return 0;
1.44 schwarze 101: }
1.1 schwarze 102:
1.44 schwarze 103: cur = maxres = 0;
1.1 schwarze 104: *res = NULL;
105:
1.51 schwarze 106: outkey = KEY_Nd;
107: if (search->outkey != NULL)
108: for (im = 0; im < KEY_MAX; im++)
1.33 schwarze 109: if (0 == strcasecmp(search->outkey,
1.51 schwarze 110: mansearch_keynames[im])) {
111: outkey = im;
1.3 schwarze 112: break;
113: }
114:
1.1 schwarze 115: /*
1.44 schwarze 116: * Remember the original working directory, if possible.
117: * This will be needed if the second or a later directory
118: * is given as a relative path.
119: * Do not error out if the current directory is not
120: * searchable: Maybe it won't be needed after all.
1.1 schwarze 121: */
122:
1.44 schwarze 123: if (getcwd(buf, PATH_MAX) == NULL) {
124: getcwd_status = 0;
125: (void)strlcpy(buf, strerror(errno), sizeof(buf));
126: } else
127: getcwd_status = 1;
1.1 schwarze 128:
129: /*
130: * Loop over the directories (containing databases) for us to
131: * search.
132: * Don't let missing/bad databases/directories phase us.
133: * In each, try to open the resident database and, if it opens,
134: * scan it for our match expression.
135: */
136:
1.44 schwarze 137: chdir_status = 0;
1.1 schwarze 138: for (i = 0; i < paths->sz; i++) {
1.44 schwarze 139: if (chdir_status && paths->paths[i][0] != '/') {
140: if ( ! getcwd_status) {
1.46 schwarze 141: warnx("%s: getcwd: %s", paths->paths[i], buf);
1.44 schwarze 142: continue;
143: } else if (chdir(buf) == -1) {
1.49 schwarze 144: warn("%s", buf);
1.44 schwarze 145: continue;
146: }
147: }
148: if (chdir(paths->paths[i]) == -1) {
1.49 schwarze 149: warn("%s", paths->paths[i]);
1.1 schwarze 150: continue;
1.24 schwarze 151: }
1.44 schwarze 152: chdir_status = 1;
1.1 schwarze 153:
1.51 schwarze 154: if (dbm_open(MANDOC_DB) == -1) {
1.46 schwarze 155: warn("%s/%s", paths->paths[i], MANDOC_DB);
1.1 schwarze 156: continue;
157: }
158:
1.51 schwarze 159: if ((htab = manmerge(e, NULL)) == NULL) {
160: dbm_close();
161: continue;
1.1 schwarze 162: }
163:
1.51 schwarze 164: for (rp = ohash_first(htab, &slot); rp != NULL;
165: rp = ohash_next(htab, &slot)) {
166: page = dbm_page_get(rp->page);
1.1 schwarze 167:
1.51 schwarze 168: if (lstmatch(search->sec, page->sect) == 0 ||
169: lstmatch(search->arch, page->arch) == 0)
1.1 schwarze 170: continue;
171:
172: if (cur + 1 > maxres) {
173: maxres += 1024;
1.26 schwarze 174: *res = mandoc_reallocarray(*res,
1.51 schwarze 175: maxres, sizeof(**res));
1.1 schwarze 176: }
177: mpage = *res + cur;
1.51 schwarze 178: mandoc_asprintf(&mpage->file, "%s/%s",
179: paths->paths[i], page->file + 1);
180: mpage->names = buildnames(page);
1.53 ! schwarze 181: mpage->output = buildoutput(outkey, page);
1.34 schwarze 182: mpage->ipath = i;
1.51 schwarze 183: mpage->bits = rp->bits;
184: mpage->sec = *page->sect - '0';
185: if (mpage->sec < 0 || mpage->sec > 9)
186: mpage->sec = 10;
187: mpage->form = *page->file;
188: free(rp);
189: cur++;
190: }
191: ohash_delete(htab);
192: free(htab);
193: dbm_close();
1.36 schwarze 194:
195: /*
196: * In man(1) mode, prefer matches in earlier trees
197: * over matches in later trees.
198: */
199:
200: if (cur && search->firstmatch)
201: break;
1.1 schwarze 202: }
1.29 schwarze 203: qsort(*res, cur, sizeof(struct manpage), manpage_compare);
1.44 schwarze 204: if (chdir_status && getcwd_status && chdir(buf) == -1)
1.49 schwarze 205: warn("%s", buf);
1.1 schwarze 206: exprfree(e);
207: *sz = cur;
1.45 schwarze 208: return 1;
1.2 schwarze 209: }
210:
1.51 schwarze 211: /*
212: * Merge the results for the expression tree rooted at e
213: * into the the result list htab.
214: */
215: static struct ohash *
216: manmerge(struct expr *e, struct ohash *htab)
1.33 schwarze 217: {
1.51 schwarze 218: switch (e->type) {
219: case EXPR_TERM:
220: return manmerge_term(e, htab);
221: case EXPR_OR:
222: return manmerge_or(e->child, htab);
223: case EXPR_AND:
224: return manmerge_and(e->child, htab);
225: default:
226: abort();
1.33 schwarze 227: }
228: }
229:
1.51 schwarze 230: static struct ohash *
231: manmerge_term(struct expr *e, struct ohash *htab)
1.29 schwarze 232: {
1.51 schwarze 233: struct dbm_res res, *rp;
234: uint64_t ib;
235: unsigned int slot;
236: int im;
237:
238: if (htab == NULL) {
239: htab = mandoc_malloc(sizeof(*htab));
240: mandoc_ohash_init(htab, 4, offsetof(struct dbm_res, page));
241: }
242:
243: for (im = 0, ib = 1; im < KEY_MAX; im++, ib <<= 1) {
244: if ((e->bits & ib) == 0)
245: continue;
246:
247: switch (ib) {
248: case TYPE_arch:
249: dbm_page_byarch(&e->match);
250: break;
251: case TYPE_sec:
252: dbm_page_bysect(&e->match);
253: break;
254: case TYPE_Nm:
255: dbm_page_byname(&e->match);
256: break;
257: case TYPE_Nd:
258: dbm_page_bydesc(&e->match);
259: break;
260: default:
261: dbm_page_bymacro(im - 2, &e->match);
262: break;
263: }
264:
265: /*
266: * When hashing for deduplication, use the unique
267: * page ID itself instead of a hash function;
268: * that is quite efficient.
269: */
1.29 schwarze 270:
1.51 schwarze 271: for (;;) {
272: res = dbm_page_next();
273: if (res.page == -1)
274: break;
275: slot = ohash_lookup_memory(htab,
276: (char *)&res, sizeof(res.page), res.page);
277: if ((rp = ohash_find(htab, slot)) != NULL) {
278: rp->bits |= res.bits;
279: continue;
280: }
281: rp = mandoc_malloc(sizeof(*rp));
282: *rp = res;
283: ohash_insert(htab, slot, rp);
284: }
285: }
286: return htab;
1.29 schwarze 287: }
288:
1.51 schwarze 289: static struct ohash *
290: manmerge_or(struct expr *e, struct ohash *htab)
291: {
292: while (e != NULL) {
293: htab = manmerge(e, htab);
294: e = e->next;
295: }
296: return htab;
297: }
1.13 schwarze 298:
1.51 schwarze 299: static struct ohash *
300: manmerge_and(struct expr *e, struct ohash *htab)
301: {
302: struct ohash *hand, *h1, *h2;
303: struct dbm_res *res;
304: unsigned int slot1, slot2;
1.13 schwarze 305:
1.51 schwarze 306: /* Evaluate the first term of the AND clause. */
1.29 schwarze 307:
1.51 schwarze 308: hand = manmerge(e, NULL);
1.29 schwarze 309:
1.51 schwarze 310: while ((e = e->next) != NULL) {
1.13 schwarze 311:
1.51 schwarze 312: /* Evaluate the next term and prepare for ANDing. */
1.13 schwarze 313:
1.51 schwarze 314: h2 = manmerge(e, NULL);
315: if (ohash_entries(h2) < ohash_entries(hand)) {
316: h1 = h2;
317: h2 = hand;
318: } else
319: h1 = hand;
320: hand = mandoc_malloc(sizeof(*hand));
321: mandoc_ohash_init(hand, 4, offsetof(struct dbm_res, page));
1.13 schwarze 322:
1.51 schwarze 323: /* Keep all pages that are in both result sets. */
1.13 schwarze 324:
1.51 schwarze 325: for (res = ohash_first(h1, &slot1); res != NULL;
326: res = ohash_next(h1, &slot1)) {
327: if (ohash_find(h2, ohash_lookup_memory(h2,
328: (char *)res, sizeof(res->page),
329: res->page)) == NULL)
330: free(res);
331: else
332: ohash_insert(hand, ohash_lookup_memory(hand,
333: (char *)res, sizeof(res->page),
334: res->page), res);
1.13 schwarze 335: }
336:
1.51 schwarze 337: /* Discard the merged results. */
338:
339: for (res = ohash_first(h2, &slot2); res != NULL;
340: res = ohash_next(h2, &slot2))
341: free(res);
342: ohash_delete(h2);
343: free(h2);
344: ohash_delete(h1);
345: free(h1);
346: }
1.13 schwarze 347:
1.51 schwarze 348: /* Merge the result of the AND into htab. */
1.8 schwarze 349:
1.51 schwarze 350: if (htab == NULL)
351: return hand;
1.8 schwarze 352:
1.51 schwarze 353: for (res = ohash_first(hand, &slot1); res != NULL;
354: res = ohash_next(hand, &slot1)) {
355: slot2 = ohash_lookup_memory(htab,
356: (char *)res, sizeof(res->page), res->page);
357: if (ohash_find(htab, slot2) == NULL)
358: ohash_insert(htab, slot2, res);
359: else
360: free(res);
361: }
362:
363: /* Discard the merged result. */
1.8 schwarze 364:
1.51 schwarze 365: ohash_delete(hand);
366: free(hand);
367: return htab;
368: }
1.41 schwarze 369:
1.51 schwarze 370: void
371: mansearch_free(struct manpage *res, size_t sz)
372: {
373: size_t i;
1.41 schwarze 374:
1.51 schwarze 375: for (i = 0; i < sz; i++) {
376: free(res[i].file);
377: free(res[i].names);
378: free(res[i].output);
1.13 schwarze 379: }
1.51 schwarze 380: free(res);
381: }
382:
383: static int
384: manpage_compare(const void *vp1, const void *vp2)
385: {
386: const struct manpage *mp1, *mp2;
387: int diff;
388:
389: mp1 = vp1;
390: mp2 = vp2;
391: return (diff = mp2->bits - mp1->bits) ? diff :
392: (diff = mp1->sec - mp2->sec) ? diff :
393: strcasecmp(mp1->names, mp2->names);
1.3 schwarze 394: }
395:
396: static char *
1.51 schwarze 397: buildnames(const struct dbm_page *page)
1.3 schwarze 398: {
1.51 schwarze 399: char *buf;
400: size_t i, sz;
1.3 schwarze 401:
1.53 ! schwarze 402: sz = lstlen(page->name, 2) + 1 + lstlen(page->sect, 2) +
! 403: (page->arch == NULL ? 0 : 1 + lstlen(page->arch, 2)) + 2;
1.51 schwarze 404: buf = mandoc_malloc(sz);
405: i = 0;
1.53 ! schwarze 406: lstcat(buf, &i, page->name, ", ");
1.51 schwarze 407: buf[i++] = '(';
1.53 ! schwarze 408: lstcat(buf, &i, page->sect, ", ");
1.51 schwarze 409: if (page->arch != NULL) {
410: buf[i++] = '/';
1.53 ! schwarze 411: lstcat(buf, &i, page->arch, ", ");
1.51 schwarze 412: }
413: buf[i++] = ')';
414: buf[i++] = '\0';
415: assert(i == sz);
416: return buf;
1.1 schwarze 417: }
418:
419: /*
1.51 schwarze 420: * Count the buffer space needed to print the NUL-terminated
1.53 ! schwarze 421: * list of NUL-terminated strings, when printing sep separator
1.51 schwarze 422: * characters between strings.
1.1 schwarze 423: */
1.51 schwarze 424: static size_t
1.53 ! schwarze 425: lstlen(const char *cp, size_t sep)
1.1 schwarze 426: {
1.51 schwarze 427: size_t sz;
1.1 schwarze 428:
1.51 schwarze 429: for (sz = 0;; sz++) {
430: if (cp[0] == '\0') {
431: if (cp[1] == '\0')
432: break;
1.53 ! schwarze 433: sz += sep - 1;
1.51 schwarze 434: } else if (cp[0] < ' ')
435: sz--;
436: cp++;
437: }
438: return sz;
1.1 schwarze 439: }
440:
441: /*
1.51 schwarze 442: * Print the NUL-terminated list of NUL-terminated strings
1.53 ! schwarze 443: * into the buffer, seperating strings with sep.
1.1 schwarze 444: */
445: static void
1.53 ! schwarze 446: lstcat(char *buf, size_t *i, const char *cp, const char *sep)
1.1 schwarze 447: {
1.53 ! schwarze 448: const char *s;
! 449:
1.51 schwarze 450: for (;;) {
451: if (cp[0] == '\0') {
452: if (cp[1] == '\0')
453: break;
1.53 ! schwarze 454: s = sep;
! 455: while (*s != '\0')
! 456: buf[(*i)++] = *s++;
1.51 schwarze 457: } else if (cp[0] >= ' ')
458: buf[(*i)++] = cp[0];
459: cp++;
460: }
1.1 schwarze 461: }
462:
1.51 schwarze 463: /*
464: * Return 1 if the string *want occurs in any of the strings
465: * in the NUL-terminated string list *have, or 0 otherwise.
466: * If either argument is NULL or empty, assume no filtering
467: * is desired and return 1.
468: */
469: static int
470: lstmatch(const char *want, const char *have)
1.4 schwarze 471: {
1.51 schwarze 472: if (want == NULL || have == NULL || *have == '\0')
473: return 1;
474: while (*have != '\0') {
475: if (strcasestr(have, want) != NULL)
476: return 1;
477: have = strchr(have, '\0') + 1;
478: }
479: return 0;
1.4 schwarze 480: }
481:
1.1 schwarze 482: /*
1.53 ! schwarze 483: * Build a list of values taken by the macro im in the manual page.
1.1 schwarze 484: */
485: static char *
1.53 ! schwarze 486: buildoutput(size_t im, struct dbm_page *page)
1.1 schwarze 487: {
1.53 ! schwarze 488: const char *oldoutput, *sep, *input;
1.51 schwarze 489: char *output, *newoutput, *value;
1.53 ! schwarze 490: size_t sz, i;
! 491:
! 492: switch (im) {
! 493: case KEY_Nd:
! 494: return mandoc_strdup(page->desc);
! 495: case KEY_Nm:
! 496: input = page->name;
! 497: break;
! 498: case KEY_sec:
! 499: input = page->sect;
! 500: break;
! 501: case KEY_arch:
! 502: input = page->arch;
! 503: if (input == NULL)
! 504: input = "all\0";
! 505: break;
! 506: default:
! 507: input = NULL;
! 508: break;
! 509: }
! 510:
! 511: if (input != NULL) {
! 512: sz = lstlen(input, 3) + 1;
! 513: output = mandoc_malloc(sz);
! 514: i = 0;
! 515: lstcat(output, &i, input, " # ");
! 516: return output;
! 517: }
1.51 schwarze 518:
519: output = NULL;
1.53 ! schwarze 520: dbm_macro_bypage(im - 2, page->addr);
1.51 schwarze 521: while ((value = dbm_macro_next()) != NULL) {
522: if (output == NULL) {
523: oldoutput = "";
524: sep = "";
525: } else {
526: oldoutput = output;
527: sep = " # ";
528: }
529: mandoc_asprintf(&newoutput, "%s%s%s", oldoutput, sep, value);
530: free(output);
531: output = newoutput;
1.1 schwarze 532: }
1.51 schwarze 533: return output;
1.1 schwarze 534: }
535:
536: /*
537: * Compile a set of string tokens into an expression.
538: * Tokens in "argv" are assumed to be individual expression atoms (e.g.,
539: * "(", "foo=bar", etc.).
540: */
541: static struct expr *
1.51 schwarze 542: exprcomp(const struct mansearch *search, int argc, char *argv[], int *argi)
1.1 schwarze 543: {
1.51 schwarze 544: struct expr *parent, *child;
545: int needterm, nested;
546:
547: if ((nested = *argi) == argc)
548: return NULL;
549: needterm = 1;
550: parent = child = NULL;
551: while (*argi < argc) {
552: if (strcmp(")", argv[*argi]) == 0) {
553: if (needterm)
554: warnx("missing term "
555: "before closing parenthesis");
556: needterm = 0;
557: if (nested)
558: break;
559: warnx("ignoring unmatched right parenthesis");
560: ++*argi;
1.4 schwarze 561: continue;
1.51 schwarze 562: }
563: if (strcmp("-o", argv[*argi]) == 0) {
564: if (needterm) {
565: if (*argi > 0)
566: warnx("ignoring -o after %s",
567: argv[*argi - 1]);
568: else
569: warnx("ignoring initial -o");
570: }
571: needterm = 1;
572: ++*argi;
1.4 schwarze 573: continue;
1.51 schwarze 574: }
575: needterm = 0;
576: if (child == NULL) {
577: child = expr_and(search, argc, argv, argi);
1.4 schwarze 578: continue;
1.1 schwarze 579: }
1.51 schwarze 580: if (parent == NULL) {
581: parent = mandoc_calloc(1, sizeof(*parent));
582: parent->type = EXPR_OR;
583: parent->next = NULL;
584: parent->child = child;
585: }
586: child->next = expr_and(search, argc, argv, argi);
587: child = child->next;
588: }
589: if (needterm && *argi)
590: warnx("ignoring trailing %s", argv[*argi - 1]);
591: return parent == NULL ? child : parent;
592: }
1.17 schwarze 593:
1.51 schwarze 594: static struct expr *
595: expr_and(const struct mansearch *search, int argc, char *argv[], int *argi)
596: {
597: struct expr *parent, *child;
598: int needterm;
1.17 schwarze 599:
1.51 schwarze 600: needterm = 1;
601: parent = child = NULL;
602: while (*argi < argc) {
603: if (strcmp(")", argv[*argi]) == 0) {
604: if (needterm)
605: warnx("missing term "
606: "before closing parenthesis");
607: needterm = 0;
608: break;
609: }
610: if (strcmp("-o", argv[*argi]) == 0)
611: break;
612: if (strcmp("-a", argv[*argi]) == 0) {
613: if (needterm) {
614: if (*argi > 0)
615: warnx("ignoring -a after %s",
616: argv[*argi - 1]);
617: else
618: warnx("ignoring initial -a");
1.18 schwarze 619: }
1.51 schwarze 620: needterm = 1;
621: ++*argi;
622: continue;
1.18 schwarze 623: }
1.51 schwarze 624: if (needterm == 0)
625: break;
626: if (child == NULL) {
627: child = exprterm(search, argc, argv, argi);
628: if (child != NULL)
629: needterm = 0;
630: continue;
631: }
632: needterm = 0;
633: if (parent == NULL) {
634: parent = mandoc_calloc(1, sizeof(*parent));
635: parent->type = EXPR_AND;
636: parent->next = NULL;
637: parent->child = child;
638: }
639: child->next = exprterm(search, argc, argv, argi);
640: if (child->next != NULL) {
641: child = child->next;
642: needterm = 0;
643: }
644: }
645: if (needterm && *argi)
646: warnx("ignoring trailing %s", argv[*argi - 1]);
647: return parent == NULL ? child : parent;
1.6 schwarze 648: }
649:
650: static struct expr *
1.51 schwarze 651: exprterm(const struct mansearch *search, int argc, char *argv[], int *argi)
1.1 schwarze 652: {
1.6 schwarze 653: char errbuf[BUFSIZ];
1.1 schwarze 654: struct expr *e;
1.28 schwarze 655: char *key, *val;
1.11 schwarze 656: uint64_t iterbit;
1.51 schwarze 657: int cs, i, irc;
1.1 schwarze 658:
1.51 schwarze 659: if (strcmp("(", argv[*argi]) == 0) {
660: ++*argi;
661: e = exprcomp(search, argc, argv, argi);
662: if (*argi < argc) {
663: assert(strcmp(")", argv[*argi]) == 0);
664: ++*argi;
665: } else
666: warnx("unclosed parenthesis");
667: return e;
668: }
1.1 schwarze 669:
1.51 schwarze 670: e = mandoc_calloc(1, sizeof(*e));
671: e->type = EXPR_TERM;
672: e->bits = 0;
673: e->next = NULL;
674: e->child = NULL;
1.1 schwarze 675:
1.33 schwarze 676: if (search->argmode == ARG_NAME) {
677: e->bits = TYPE_Nm;
1.51 schwarze 678: e->match.type = DBM_EXACT;
679: e->match.str = argv[(*argi)++];
1.45 schwarze 680: return e;
1.1 schwarze 681: }
682:
683: /*
1.33 schwarze 684: * Separate macro keys from search string.
1.51 schwarze 685: * If needed, request regular expression handling.
1.1 schwarze 686: */
687:
1.52 schwarze 688: cs = 1;
1.33 schwarze 689: if (search->argmode == ARG_WORD) {
690: e->bits = TYPE_Nm;
1.51 schwarze 691: e->match.type = DBM_REGEX;
692: mandoc_asprintf(&val, "[[:<:]]%s[[:>:]]", argv[*argi]);
1.33 schwarze 693: cs = 0;
1.51 schwarze 694: } else if ((val = strpbrk(argv[*argi], "=~")) == NULL) {
1.33 schwarze 695: e->bits = TYPE_Nm | TYPE_Nd;
1.51 schwarze 696: e->match.type = DBM_SUB;
697: e->match.str = argv[*argi];
1.28 schwarze 698: } else {
1.51 schwarze 699: if (val == argv[*argi])
1.33 schwarze 700: e->bits = TYPE_Nm | TYPE_Nd;
1.51 schwarze 701: if (*val == '=') {
702: e->match.type = DBM_SUB;
703: e->match.str = val + 1;
704: } else
705: e->match.type = DBM_REGEX;
1.28 schwarze 706: *val++ = '\0';
1.51 schwarze 707: if (strstr(argv[*argi], "arch") != NULL)
1.12 schwarze 708: cs = 0;
1.28 schwarze 709: }
710:
711: /* Compile regular expressions. */
712:
1.51 schwarze 713: if (e->match.type == DBM_REGEX) {
714: e->match.re = mandoc_malloc(sizeof(*e->match.re));
715: irc = regcomp(e->match.re, val,
1.28 schwarze 716: REG_EXTENDED | REG_NOSUB | (cs ? 0 : REG_ICASE));
1.51 schwarze 717: if (irc) {
718: regerror(irc, e->match.re, errbuf, sizeof(errbuf));
719: warnx("regcomp /%s/: %s", val, errbuf);
720: }
1.33 schwarze 721: if (search->argmode == ARG_WORD)
1.28 schwarze 722: free(val);
723: if (irc) {
1.51 schwarze 724: free(e->match.re);
1.1 schwarze 725: free(e);
1.51 schwarze 726: ++*argi;
1.45 schwarze 727: return NULL;
1.1 schwarze 728: }
1.28 schwarze 729: }
730:
1.51 schwarze 731: if (e->bits) {
732: ++*argi;
1.45 schwarze 733: return e;
1.51 schwarze 734: }
1.1 schwarze 735:
736: /*
737: * Parse out all possible fields.
738: * If the field doesn't resolve, bail.
739: */
740:
1.51 schwarze 741: while (NULL != (key = strsep(&argv[*argi], ","))) {
1.1 schwarze 742: if ('\0' == *key)
743: continue;
1.51 schwarze 744: for (i = 0, iterbit = 1; i < KEY_MAX; i++, iterbit <<= 1) {
745: if (0 == strcasecmp(key, mansearch_keynames[i])) {
1.11 schwarze 746: e->bits |= iterbit;
747: break;
748: }
749: }
1.51 schwarze 750: if (i == KEY_MAX) {
751: if (strcasecmp(key, "any"))
752: warnx("treating unknown key "
753: "\"%s\" as \"any\"", key);
1.11 schwarze 754: e->bits |= ~0ULL;
1.1 schwarze 755: }
756: }
757:
1.51 schwarze 758: ++*argi;
1.45 schwarze 759: return e;
1.1 schwarze 760: }
761:
762: static void
1.51 schwarze 763: exprfree(struct expr *e)
1.1 schwarze 764: {
1.51 schwarze 765: if (e->next != NULL)
766: exprfree(e->next);
767: if (e->child != NULL)
768: exprfree(e->child);
769: free(e);
1.1 schwarze 770: }