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