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