Annotation of src/usr.bin/mandoc/mansearch.c, Revision 1.62
1.62 ! schwarze 1: /* $OpenBSD: mansearch.c,v 1.61 2018/11/19 19:27:29 schwarze Exp $ */
1.1 schwarze 2: /*
3: * Copyright (c) 2012 Kristaps Dzonsons <kristaps@bsd.lv>
1.61 schwarze 4: * Copyright (c) 2013-2018 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.57 schwarze 104: if (res != NULL)
105: *res = NULL;
1.1 schwarze 106:
1.51 schwarze 107: outkey = KEY_Nd;
108: if (search->outkey != NULL)
109: for (im = 0; im < KEY_MAX; im++)
1.33 schwarze 110: if (0 == strcasecmp(search->outkey,
1.51 schwarze 111: mansearch_keynames[im])) {
112: outkey = im;
1.3 schwarze 113: break;
114: }
115:
1.1 schwarze 116: /*
1.44 schwarze 117: * Remember the original working directory, if possible.
118: * This will be needed if the second or a later directory
119: * is given as a relative path.
120: * Do not error out if the current directory is not
121: * searchable: Maybe it won't be needed after all.
1.1 schwarze 122: */
123:
1.44 schwarze 124: if (getcwd(buf, PATH_MAX) == NULL) {
125: getcwd_status = 0;
126: (void)strlcpy(buf, strerror(errno), sizeof(buf));
127: } else
128: getcwd_status = 1;
1.1 schwarze 129:
130: /*
131: * Loop over the directories (containing databases) for us to
132: * search.
133: * Don't let missing/bad databases/directories phase us.
134: * In each, try to open the resident database and, if it opens,
135: * scan it for our match expression.
136: */
137:
1.44 schwarze 138: chdir_status = 0;
1.1 schwarze 139: for (i = 0; i < paths->sz; i++) {
1.44 schwarze 140: if (chdir_status && paths->paths[i][0] != '/') {
141: if ( ! getcwd_status) {
1.46 schwarze 142: warnx("%s: getcwd: %s", paths->paths[i], buf);
1.44 schwarze 143: continue;
144: } else if (chdir(buf) == -1) {
1.49 schwarze 145: warn("%s", buf);
1.44 schwarze 146: continue;
147: }
148: }
149: if (chdir(paths->paths[i]) == -1) {
1.49 schwarze 150: warn("%s", paths->paths[i]);
1.1 schwarze 151: continue;
1.24 schwarze 152: }
1.44 schwarze 153: chdir_status = 1;
1.1 schwarze 154:
1.51 schwarze 155: if (dbm_open(MANDOC_DB) == -1) {
1.56 schwarze 156: if (errno != ENOENT)
157: warn("%s/%s", paths->paths[i], MANDOC_DB);
1.1 schwarze 158: continue;
159: }
160:
1.51 schwarze 161: if ((htab = manmerge(e, NULL)) == NULL) {
162: dbm_close();
163: continue;
1.1 schwarze 164: }
165:
1.51 schwarze 166: for (rp = ohash_first(htab, &slot); rp != NULL;
167: rp = ohash_next(htab, &slot)) {
168: page = dbm_page_get(rp->page);
1.1 schwarze 169:
1.51 schwarze 170: if (lstmatch(search->sec, page->sect) == 0 ||
1.59 schwarze 171: lstmatch(search->arch, page->arch) == 0 ||
172: (search->argmode == ARG_NAME &&
173: rp->bits <= (int32_t)(NAME_SYN & NAME_MASK)))
1.1 schwarze 174: continue;
175:
1.57 schwarze 176: if (res == NULL) {
177: cur = 1;
178: break;
179: }
1.1 schwarze 180: if (cur + 1 > maxres) {
181: maxres += 1024;
1.26 schwarze 182: *res = mandoc_reallocarray(*res,
1.51 schwarze 183: maxres, sizeof(**res));
1.1 schwarze 184: }
185: mpage = *res + cur;
1.51 schwarze 186: mandoc_asprintf(&mpage->file, "%s/%s",
187: paths->paths[i], page->file + 1);
1.60 schwarze 188: if (access(chdir_status ? page->file + 1 :
189: mpage->file, R_OK) == -1) {
190: warn("%s", mpage->file);
191: warnx("outdated mandoc.db contains "
192: "bogus %s entry, run makewhatis %s",
193: page->file + 1, paths->paths[i]);
194: free(mpage->file);
195: free(rp);
196: continue;
197: }
1.51 schwarze 198: mpage->names = buildnames(page);
1.53 schwarze 199: mpage->output = buildoutput(outkey, page);
1.34 schwarze 200: mpage->ipath = i;
1.51 schwarze 201: mpage->sec = *page->sect - '0';
202: if (mpage->sec < 0 || mpage->sec > 9)
203: mpage->sec = 10;
204: mpage->form = *page->file;
205: free(rp);
206: cur++;
207: }
208: ohash_delete(htab);
209: free(htab);
210: dbm_close();
1.36 schwarze 211:
212: /*
213: * In man(1) mode, prefer matches in earlier trees
214: * over matches in later trees.
215: */
216:
217: if (cur && search->firstmatch)
218: break;
1.1 schwarze 219: }
1.57 schwarze 220: if (res != NULL)
221: qsort(*res, cur, sizeof(struct manpage), manpage_compare);
1.44 schwarze 222: if (chdir_status && getcwd_status && chdir(buf) == -1)
1.49 schwarze 223: warn("%s", buf);
1.1 schwarze 224: exprfree(e);
225: *sz = cur;
1.57 schwarze 226: return res != NULL || cur;
1.2 schwarze 227: }
228:
1.51 schwarze 229: /*
230: * Merge the results for the expression tree rooted at e
231: * into the the result list htab.
232: */
233: static struct ohash *
234: manmerge(struct expr *e, struct ohash *htab)
1.33 schwarze 235: {
1.51 schwarze 236: switch (e->type) {
237: case EXPR_TERM:
238: return manmerge_term(e, htab);
239: case EXPR_OR:
240: return manmerge_or(e->child, htab);
241: case EXPR_AND:
242: return manmerge_and(e->child, htab);
243: default:
244: abort();
1.33 schwarze 245: }
246: }
247:
1.51 schwarze 248: static struct ohash *
249: manmerge_term(struct expr *e, struct ohash *htab)
1.29 schwarze 250: {
1.51 schwarze 251: struct dbm_res res, *rp;
252: uint64_t ib;
253: unsigned int slot;
254: int im;
255:
256: if (htab == NULL) {
257: htab = mandoc_malloc(sizeof(*htab));
258: mandoc_ohash_init(htab, 4, offsetof(struct dbm_res, page));
259: }
260:
261: for (im = 0, ib = 1; im < KEY_MAX; im++, ib <<= 1) {
262: if ((e->bits & ib) == 0)
263: continue;
264:
265: switch (ib) {
266: case TYPE_arch:
267: dbm_page_byarch(&e->match);
268: break;
269: case TYPE_sec:
270: dbm_page_bysect(&e->match);
271: break;
272: case TYPE_Nm:
273: dbm_page_byname(&e->match);
274: break;
275: case TYPE_Nd:
276: dbm_page_bydesc(&e->match);
277: break;
278: default:
279: dbm_page_bymacro(im - 2, &e->match);
280: break;
281: }
282:
283: /*
284: * When hashing for deduplication, use the unique
285: * page ID itself instead of a hash function;
286: * that is quite efficient.
287: */
1.29 schwarze 288:
1.51 schwarze 289: for (;;) {
290: res = dbm_page_next();
291: if (res.page == -1)
292: break;
293: slot = ohash_lookup_memory(htab,
294: (char *)&res, sizeof(res.page), res.page);
1.62 ! schwarze 295: if ((rp = ohash_find(htab, slot)) != NULL)
1.51 schwarze 296: continue;
297: rp = mandoc_malloc(sizeof(*rp));
298: *rp = res;
299: ohash_insert(htab, slot, rp);
300: }
301: }
302: return htab;
1.29 schwarze 303: }
304:
1.51 schwarze 305: static struct ohash *
306: manmerge_or(struct expr *e, struct ohash *htab)
307: {
308: while (e != NULL) {
309: htab = manmerge(e, htab);
310: e = e->next;
311: }
312: return htab;
313: }
1.13 schwarze 314:
1.51 schwarze 315: static struct ohash *
316: manmerge_and(struct expr *e, struct ohash *htab)
317: {
318: struct ohash *hand, *h1, *h2;
319: struct dbm_res *res;
320: unsigned int slot1, slot2;
1.13 schwarze 321:
1.51 schwarze 322: /* Evaluate the first term of the AND clause. */
1.29 schwarze 323:
1.51 schwarze 324: hand = manmerge(e, NULL);
1.29 schwarze 325:
1.51 schwarze 326: while ((e = e->next) != NULL) {
1.13 schwarze 327:
1.51 schwarze 328: /* Evaluate the next term and prepare for ANDing. */
1.13 schwarze 329:
1.51 schwarze 330: h2 = manmerge(e, NULL);
331: if (ohash_entries(h2) < ohash_entries(hand)) {
332: h1 = h2;
333: h2 = hand;
334: } else
335: h1 = hand;
336: hand = mandoc_malloc(sizeof(*hand));
337: mandoc_ohash_init(hand, 4, offsetof(struct dbm_res, page));
1.13 schwarze 338:
1.51 schwarze 339: /* Keep all pages that are in both result sets. */
1.13 schwarze 340:
1.51 schwarze 341: for (res = ohash_first(h1, &slot1); res != NULL;
342: res = ohash_next(h1, &slot1)) {
343: if (ohash_find(h2, ohash_lookup_memory(h2,
344: (char *)res, sizeof(res->page),
345: res->page)) == NULL)
346: free(res);
347: else
348: ohash_insert(hand, ohash_lookup_memory(hand,
349: (char *)res, sizeof(res->page),
350: res->page), res);
1.13 schwarze 351: }
352:
1.51 schwarze 353: /* Discard the merged results. */
354:
355: for (res = ohash_first(h2, &slot2); res != NULL;
356: res = ohash_next(h2, &slot2))
357: free(res);
358: ohash_delete(h2);
359: free(h2);
360: ohash_delete(h1);
361: free(h1);
362: }
1.13 schwarze 363:
1.51 schwarze 364: /* Merge the result of the AND into htab. */
1.8 schwarze 365:
1.51 schwarze 366: if (htab == NULL)
367: return hand;
1.8 schwarze 368:
1.51 schwarze 369: for (res = ohash_first(hand, &slot1); res != NULL;
370: res = ohash_next(hand, &slot1)) {
371: slot2 = ohash_lookup_memory(htab,
372: (char *)res, sizeof(res->page), res->page);
373: if (ohash_find(htab, slot2) == NULL)
374: ohash_insert(htab, slot2, res);
375: else
376: free(res);
377: }
378:
379: /* Discard the merged result. */
1.8 schwarze 380:
1.51 schwarze 381: ohash_delete(hand);
382: free(hand);
383: return htab;
384: }
1.41 schwarze 385:
1.51 schwarze 386: void
387: mansearch_free(struct manpage *res, size_t sz)
388: {
389: size_t i;
1.41 schwarze 390:
1.51 schwarze 391: for (i = 0; i < sz; i++) {
392: free(res[i].file);
393: free(res[i].names);
394: free(res[i].output);
1.13 schwarze 395: }
1.51 schwarze 396: free(res);
397: }
398:
399: static int
400: manpage_compare(const void *vp1, const void *vp2)
401: {
402: const struct manpage *mp1, *mp2;
1.58 schwarze 403: const char *cp1, *cp2;
404: size_t sz1, sz2;
1.51 schwarze 405: int diff;
406:
407: mp1 = vp1;
408: mp2 = vp2;
1.62 ! schwarze 409: if ((diff = mp1->sec - mp2->sec))
1.58 schwarze 410: return diff;
411:
412: /* Fall back to alphabetic ordering of names. */
413: sz1 = strcspn(mp1->names, "(");
414: sz2 = strcspn(mp2->names, "(");
415: if (sz1 < sz2)
416: sz1 = sz2;
417: if ((diff = strncasecmp(mp1->names, mp2->names, sz1)))
418: return diff;
419:
420: /* For identical names and sections, prefer arch-dependent. */
421: cp1 = strchr(mp1->names + sz1, '/');
422: cp2 = strchr(mp2->names + sz2, '/');
423: return cp1 != NULL && cp2 != NULL ? strcasecmp(cp1, cp2) :
424: cp1 != NULL ? -1 : cp2 != NULL ? 1 : 0;
1.3 schwarze 425: }
426:
427: static char *
1.51 schwarze 428: buildnames(const struct dbm_page *page)
1.3 schwarze 429: {
1.51 schwarze 430: char *buf;
431: size_t i, sz;
1.3 schwarze 432:
1.53 schwarze 433: sz = lstlen(page->name, 2) + 1 + lstlen(page->sect, 2) +
434: (page->arch == NULL ? 0 : 1 + lstlen(page->arch, 2)) + 2;
1.51 schwarze 435: buf = mandoc_malloc(sz);
436: i = 0;
1.53 schwarze 437: lstcat(buf, &i, page->name, ", ");
1.51 schwarze 438: buf[i++] = '(';
1.53 schwarze 439: lstcat(buf, &i, page->sect, ", ");
1.51 schwarze 440: if (page->arch != NULL) {
441: buf[i++] = '/';
1.53 schwarze 442: lstcat(buf, &i, page->arch, ", ");
1.51 schwarze 443: }
444: buf[i++] = ')';
445: buf[i++] = '\0';
446: assert(i == sz);
447: return buf;
1.1 schwarze 448: }
449:
450: /*
1.51 schwarze 451: * Count the buffer space needed to print the NUL-terminated
1.53 schwarze 452: * list of NUL-terminated strings, when printing sep separator
1.51 schwarze 453: * characters between strings.
1.1 schwarze 454: */
1.51 schwarze 455: static size_t
1.53 schwarze 456: lstlen(const char *cp, size_t sep)
1.1 schwarze 457: {
1.51 schwarze 458: size_t sz;
1.1 schwarze 459:
1.59 schwarze 460: for (sz = 0; *cp != '\0'; cp++) {
461:
462: /* Skip names appearing only in the SYNOPSIS. */
463: if (*cp <= (char)(NAME_SYN & NAME_MASK)) {
464: while (*cp != '\0')
465: cp++;
466: continue;
467: }
468:
469: /* Skip name class markers. */
470: if (*cp < ' ')
471: cp++;
472:
473: /* Print a separator before each but the first string. */
474: if (sz)
475: sz += sep;
476:
477: /* Copy one string. */
478: while (*cp != '\0') {
479: sz++;
480: cp++;
481: }
1.51 schwarze 482: }
483: return sz;
1.1 schwarze 484: }
485:
486: /*
1.51 schwarze 487: * Print the NUL-terminated list of NUL-terminated strings
1.53 schwarze 488: * into the buffer, seperating strings with sep.
1.1 schwarze 489: */
490: static void
1.53 schwarze 491: lstcat(char *buf, size_t *i, const char *cp, const char *sep)
1.1 schwarze 492: {
1.59 schwarze 493: const char *s;
494: size_t i_start;
1.53 schwarze 495:
1.59 schwarze 496: for (i_start = *i; *cp != '\0'; cp++) {
497:
498: /* Skip names appearing only in the SYNOPSIS. */
499: if (*cp <= (char)(NAME_SYN & NAME_MASK)) {
500: while (*cp != '\0')
501: cp++;
502: continue;
503: }
504:
505: /* Skip name class markers. */
506: if (*cp < ' ')
507: cp++;
508:
509: /* Print a separator before each but the first string. */
510: if (*i > i_start) {
1.53 schwarze 511: s = sep;
512: while (*s != '\0')
513: buf[(*i)++] = *s++;
1.59 schwarze 514: }
515:
516: /* Copy one string. */
517: while (*cp != '\0')
518: buf[(*i)++] = *cp++;
1.51 schwarze 519: }
1.59 schwarze 520:
1.1 schwarze 521: }
522:
1.51 schwarze 523: /*
524: * Return 1 if the string *want occurs in any of the strings
525: * in the NUL-terminated string list *have, or 0 otherwise.
526: * If either argument is NULL or empty, assume no filtering
527: * is desired and return 1.
528: */
529: static int
530: lstmatch(const char *want, const char *have)
1.4 schwarze 531: {
1.51 schwarze 532: if (want == NULL || have == NULL || *have == '\0')
533: return 1;
534: while (*have != '\0') {
535: if (strcasestr(have, want) != NULL)
536: return 1;
537: have = strchr(have, '\0') + 1;
538: }
539: return 0;
1.4 schwarze 540: }
541:
1.1 schwarze 542: /*
1.53 schwarze 543: * Build a list of values taken by the macro im in the manual page.
1.1 schwarze 544: */
545: static char *
1.53 schwarze 546: buildoutput(size_t im, struct dbm_page *page)
1.1 schwarze 547: {
1.53 schwarze 548: const char *oldoutput, *sep, *input;
1.51 schwarze 549: char *output, *newoutput, *value;
1.53 schwarze 550: size_t sz, i;
551:
552: switch (im) {
553: case KEY_Nd:
554: return mandoc_strdup(page->desc);
555: case KEY_Nm:
556: input = page->name;
557: break;
558: case KEY_sec:
559: input = page->sect;
560: break;
561: case KEY_arch:
562: input = page->arch;
563: if (input == NULL)
564: input = "all\0";
565: break;
566: default:
567: input = NULL;
568: break;
569: }
570:
571: if (input != NULL) {
572: sz = lstlen(input, 3) + 1;
573: output = mandoc_malloc(sz);
574: i = 0;
575: lstcat(output, &i, input, " # ");
1.54 schwarze 576: output[i++] = '\0';
577: assert(i == sz);
1.53 schwarze 578: return output;
579: }
1.51 schwarze 580:
581: output = NULL;
1.53 schwarze 582: dbm_macro_bypage(im - 2, page->addr);
1.51 schwarze 583: while ((value = dbm_macro_next()) != NULL) {
584: if (output == NULL) {
585: oldoutput = "";
586: sep = "";
587: } else {
588: oldoutput = output;
589: sep = " # ";
590: }
591: mandoc_asprintf(&newoutput, "%s%s%s", oldoutput, sep, value);
592: free(output);
593: output = newoutput;
1.1 schwarze 594: }
1.51 schwarze 595: return output;
1.1 schwarze 596: }
597:
598: /*
599: * Compile a set of string tokens into an expression.
600: * Tokens in "argv" are assumed to be individual expression atoms (e.g.,
601: * "(", "foo=bar", etc.).
602: */
603: static struct expr *
1.51 schwarze 604: exprcomp(const struct mansearch *search, int argc, char *argv[], int *argi)
1.1 schwarze 605: {
1.51 schwarze 606: struct expr *parent, *child;
607: int needterm, nested;
608:
609: if ((nested = *argi) == argc)
610: return NULL;
611: needterm = 1;
612: parent = child = NULL;
613: while (*argi < argc) {
614: if (strcmp(")", argv[*argi]) == 0) {
615: if (needterm)
616: warnx("missing term "
617: "before closing parenthesis");
618: needterm = 0;
619: if (nested)
620: break;
621: warnx("ignoring unmatched right parenthesis");
622: ++*argi;
1.4 schwarze 623: continue;
1.51 schwarze 624: }
625: if (strcmp("-o", argv[*argi]) == 0) {
626: if (needterm) {
627: if (*argi > 0)
628: warnx("ignoring -o after %s",
629: argv[*argi - 1]);
630: else
631: warnx("ignoring initial -o");
632: }
633: needterm = 1;
634: ++*argi;
1.4 schwarze 635: continue;
1.51 schwarze 636: }
637: needterm = 0;
638: if (child == NULL) {
639: child = expr_and(search, argc, argv, argi);
1.4 schwarze 640: continue;
1.1 schwarze 641: }
1.51 schwarze 642: if (parent == NULL) {
643: parent = mandoc_calloc(1, sizeof(*parent));
644: parent->type = EXPR_OR;
645: parent->next = NULL;
646: parent->child = child;
647: }
648: child->next = expr_and(search, argc, argv, argi);
649: child = child->next;
650: }
651: if (needterm && *argi)
652: warnx("ignoring trailing %s", argv[*argi - 1]);
653: return parent == NULL ? child : parent;
654: }
1.17 schwarze 655:
1.51 schwarze 656: static struct expr *
657: expr_and(const struct mansearch *search, int argc, char *argv[], int *argi)
658: {
659: struct expr *parent, *child;
660: int needterm;
1.17 schwarze 661:
1.51 schwarze 662: needterm = 1;
663: parent = child = NULL;
664: while (*argi < argc) {
665: if (strcmp(")", argv[*argi]) == 0) {
666: if (needterm)
667: warnx("missing term "
668: "before closing parenthesis");
669: needterm = 0;
670: break;
671: }
672: if (strcmp("-o", argv[*argi]) == 0)
673: break;
674: if (strcmp("-a", argv[*argi]) == 0) {
675: if (needterm) {
676: if (*argi > 0)
677: warnx("ignoring -a after %s",
678: argv[*argi - 1]);
679: else
680: warnx("ignoring initial -a");
1.18 schwarze 681: }
1.51 schwarze 682: needterm = 1;
683: ++*argi;
684: continue;
1.18 schwarze 685: }
1.51 schwarze 686: if (needterm == 0)
687: break;
688: if (child == NULL) {
689: child = exprterm(search, argc, argv, argi);
690: if (child != NULL)
691: needterm = 0;
692: continue;
693: }
694: needterm = 0;
695: if (parent == NULL) {
696: parent = mandoc_calloc(1, sizeof(*parent));
697: parent->type = EXPR_AND;
698: parent->next = NULL;
699: parent->child = child;
700: }
701: child->next = exprterm(search, argc, argv, argi);
702: if (child->next != NULL) {
703: child = child->next;
704: needterm = 0;
705: }
706: }
707: if (needterm && *argi)
708: warnx("ignoring trailing %s", argv[*argi - 1]);
709: return parent == NULL ? child : parent;
1.6 schwarze 710: }
711:
712: static struct expr *
1.51 schwarze 713: exprterm(const struct mansearch *search, int argc, char *argv[], int *argi)
1.1 schwarze 714: {
1.6 schwarze 715: char errbuf[BUFSIZ];
1.1 schwarze 716: struct expr *e;
1.28 schwarze 717: char *key, *val;
1.11 schwarze 718: uint64_t iterbit;
1.51 schwarze 719: int cs, i, irc;
1.1 schwarze 720:
1.51 schwarze 721: if (strcmp("(", argv[*argi]) == 0) {
722: ++*argi;
723: e = exprcomp(search, argc, argv, argi);
724: if (*argi < argc) {
725: assert(strcmp(")", argv[*argi]) == 0);
726: ++*argi;
727: } else
728: warnx("unclosed parenthesis");
729: return e;
730: }
1.1 schwarze 731:
1.55 schwarze 732: if (strcmp("-i", argv[*argi]) == 0 && *argi + 1 < argc) {
733: cs = 0;
734: ++*argi;
735: } else
736: cs = 1;
737:
1.51 schwarze 738: e = mandoc_calloc(1, sizeof(*e));
739: e->type = EXPR_TERM;
740: e->bits = 0;
741: e->next = NULL;
742: e->child = NULL;
1.1 schwarze 743:
1.33 schwarze 744: if (search->argmode == ARG_NAME) {
745: e->bits = TYPE_Nm;
1.51 schwarze 746: e->match.type = DBM_EXACT;
747: e->match.str = argv[(*argi)++];
1.45 schwarze 748: return e;
1.1 schwarze 749: }
750:
751: /*
1.33 schwarze 752: * Separate macro keys from search string.
1.51 schwarze 753: * If needed, request regular expression handling.
1.1 schwarze 754: */
755:
1.33 schwarze 756: if (search->argmode == ARG_WORD) {
757: e->bits = TYPE_Nm;
1.51 schwarze 758: e->match.type = DBM_REGEX;
759: mandoc_asprintf(&val, "[[:<:]]%s[[:>:]]", argv[*argi]);
1.33 schwarze 760: cs = 0;
1.51 schwarze 761: } else if ((val = strpbrk(argv[*argi], "=~")) == NULL) {
1.33 schwarze 762: e->bits = TYPE_Nm | TYPE_Nd;
1.61 schwarze 763: e->match.type = DBM_REGEX;
764: val = argv[*argi];
765: cs = 0;
1.28 schwarze 766: } else {
1.51 schwarze 767: if (val == argv[*argi])
1.33 schwarze 768: e->bits = TYPE_Nm | TYPE_Nd;
1.51 schwarze 769: if (*val == '=') {
770: e->match.type = DBM_SUB;
771: e->match.str = val + 1;
772: } else
773: e->match.type = DBM_REGEX;
1.28 schwarze 774: *val++ = '\0';
1.51 schwarze 775: if (strstr(argv[*argi], "arch") != NULL)
1.12 schwarze 776: cs = 0;
1.28 schwarze 777: }
778:
779: /* Compile regular expressions. */
780:
1.51 schwarze 781: if (e->match.type == DBM_REGEX) {
782: e->match.re = mandoc_malloc(sizeof(*e->match.re));
783: irc = regcomp(e->match.re, val,
1.28 schwarze 784: REG_EXTENDED | REG_NOSUB | (cs ? 0 : REG_ICASE));
1.51 schwarze 785: if (irc) {
786: regerror(irc, e->match.re, errbuf, sizeof(errbuf));
787: warnx("regcomp /%s/: %s", val, errbuf);
788: }
1.33 schwarze 789: if (search->argmode == ARG_WORD)
1.28 schwarze 790: free(val);
791: if (irc) {
1.51 schwarze 792: free(e->match.re);
1.1 schwarze 793: free(e);
1.51 schwarze 794: ++*argi;
1.45 schwarze 795: return NULL;
1.1 schwarze 796: }
1.28 schwarze 797: }
798:
1.51 schwarze 799: if (e->bits) {
800: ++*argi;
1.45 schwarze 801: return e;
1.51 schwarze 802: }
1.1 schwarze 803:
804: /*
805: * Parse out all possible fields.
806: * If the field doesn't resolve, bail.
807: */
808:
1.51 schwarze 809: while (NULL != (key = strsep(&argv[*argi], ","))) {
1.1 schwarze 810: if ('\0' == *key)
811: continue;
1.51 schwarze 812: for (i = 0, iterbit = 1; i < KEY_MAX; i++, iterbit <<= 1) {
813: if (0 == strcasecmp(key, mansearch_keynames[i])) {
1.11 schwarze 814: e->bits |= iterbit;
815: break;
816: }
817: }
1.51 schwarze 818: if (i == KEY_MAX) {
819: if (strcasecmp(key, "any"))
820: warnx("treating unknown key "
821: "\"%s\" as \"any\"", key);
1.11 schwarze 822: e->bits |= ~0ULL;
1.1 schwarze 823: }
824: }
825:
1.51 schwarze 826: ++*argi;
1.45 schwarze 827: return e;
1.1 schwarze 828: }
829:
830: static void
1.51 schwarze 831: exprfree(struct expr *e)
1.1 schwarze 832: {
1.51 schwarze 833: if (e->next != NULL)
834: exprfree(e->next);
835: if (e->child != NULL)
836: exprfree(e->child);
837: free(e);
1.1 schwarze 838: }