1 /* $OpenBSD: mansearch.c,v 1.67 2022/12/26 19:16:02 jmc Exp $ */
2 /*
3 * Copyright (c) 2012 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2013-2018 Ingo Schwarze <schwarze@openbsd.org>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <sys/mman.h>
20 #include <sys/types.h>
21
22 #include <assert.h>
23 #include <err.h>
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <glob.h>
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_aux.h"
37 #include "mandoc_ohash.h"
38 #include "manconf.h"
39 #include "mansearch.h"
40 #include "dbm.h"
41
42 struct expr {
43 /* Used for terms: */
44 struct dbm_match match; /* Match type and expression. */
45 uint64_t bits; /* Type mask. */
46 /* Used for OR and AND groups: */
47 struct expr *next; /* Next child in the parent group. */
48 struct expr *child; /* First child in this group. */
49 enum { EXPR_TERM, EXPR_OR, EXPR_AND } type;
50 };
51
52 const char *const mansearch_keynames[KEY_MAX] = {
53 "arch", "sec", "Xr", "Ar", "Fa", "Fl", "Dv", "Fn",
54 "Ic", "Pa", "Cm", "Li", "Em", "Cd", "Va", "Ft",
55 "Tn", "Er", "Ev", "Sy", "Sh", "In", "Ss", "Ox",
56 "An", "Mt", "St", "Bx", "At", "Nx", "Fx", "Lk",
57 "Ms", "Bsx", "Dx", "Rs", "Vt", "Lb", "Nm", "Nd"
58 };
59
60
61 static struct ohash *manmerge(struct expr *, struct ohash *);
62 static struct ohash *manmerge_term(struct expr *, struct ohash *);
63 static struct ohash *manmerge_or(struct expr *, struct ohash *);
64 static struct ohash *manmerge_and(struct expr *, struct ohash *);
65 static char *buildnames(const struct dbm_page *);
66 static char *buildoutput(size_t, struct dbm_page *);
67 static size_t lstlen(const char *, size_t);
68 static void lstcat(char *, size_t *, const char *, const char *);
69 static int lstmatch(const char *, const char *);
70 static struct expr *exprcomp(const struct mansearch *,
71 int, char *[], int *);
72 static struct expr *expr_and(const struct mansearch *,
73 int, char *[], int *);
74 static struct expr *exprterm(const struct mansearch *,
75 int, char *[], int *);
76 static void exprfree(struct expr *);
77 static int manpage_compare(const void *, const void *);
78
79
80 int
mansearch(const struct mansearch * search,const struct manpaths * paths,int argc,char * argv[],struct manpage ** res,size_t * sz)81 mansearch(const struct mansearch *search,
82 const struct manpaths *paths,
83 int argc, char *argv[],
84 struct manpage **res, size_t *sz)
85 {
86 char buf[PATH_MAX];
87 struct dbm_res *rp;
88 struct expr *e;
89 struct dbm_page *page;
90 struct manpage *mpage;
91 struct ohash *htab;
92 size_t cur, i, maxres, outkey;
93 unsigned int slot;
94 int argi, chdir_status, getcwd_status, im;
95
96 argi = 0;
97 if ((e = exprcomp(search, argc, argv, &argi)) == NULL) {
98 *sz = 0;
99 return 0;
100 }
101
102 cur = maxres = 0;
103 if (res != NULL)
104 *res = NULL;
105
106 outkey = KEY_Nd;
107 if (search->outkey != NULL)
108 for (im = 0; im < KEY_MAX; im++)
109 if (0 == strcasecmp(search->outkey,
110 mansearch_keynames[im])) {
111 outkey = im;
112 break;
113 }
114
115 /*
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.
121 */
122
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;
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
137 chdir_status = 0;
138 for (i = 0; i < paths->sz; i++) {
139 if (chdir_status && paths->paths[i][0] != '/') {
140 if ( ! getcwd_status) {
141 warnx("%s: getcwd: %s", paths->paths[i], buf);
142 continue;
143 } else if (chdir(buf) == -1) {
144 warn("%s", buf);
145 continue;
146 }
147 }
148 if (chdir(paths->paths[i]) == -1) {
149 warn("%s", paths->paths[i]);
150 continue;
151 }
152 chdir_status = 1;
153
154 if (dbm_open(MANDOC_DB) == -1) {
155 if (errno != ENOENT)
156 warn("%s/%s", paths->paths[i], MANDOC_DB);
157 continue;
158 }
159
160 if ((htab = manmerge(e, NULL)) == NULL) {
161 dbm_close();
162 continue;
163 }
164
165 for (rp = ohash_first(htab, &slot); rp != NULL;
166 rp = ohash_next(htab, &slot)) {
167 page = dbm_page_get(rp->page);
168
169 if (lstmatch(search->sec, page->sect) == 0 ||
170 lstmatch(search->arch, page->arch) == 0 ||
171 (search->argmode == ARG_NAME &&
172 rp->bits <= (int32_t)(NAME_SYN & NAME_MASK)))
173 continue;
174
175 if (res == NULL) {
176 cur = 1;
177 break;
178 }
179 if (cur + 1 > maxres) {
180 maxres += 1024;
181 *res = mandoc_reallocarray(*res,
182 maxres, sizeof(**res));
183 }
184 mpage = *res + cur;
185 mandoc_asprintf(&mpage->file, "%s/%s",
186 paths->paths[i], page->file + 1);
187 if (access(chdir_status ? page->file + 1 :
188 mpage->file, R_OK) == -1) {
189 warn("%s", mpage->file);
190 warnx("outdated mandoc.db contains "
191 "bogus %s entry, run makewhatis %s",
192 page->file + 1, paths->paths[i]);
193 free(mpage->file);
194 free(rp);
195 continue;
196 }
197 mpage->names = buildnames(page);
198 mpage->output = buildoutput(outkey, page);
199 mpage->bits = search->firstmatch ? rp->bits : 0;
200 mpage->ipath = i;
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();
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;
219 }
220 if (res != NULL && cur > 1)
221 qsort(*res, cur, sizeof(struct manpage), manpage_compare);
222 if (chdir_status && getcwd_status && chdir(buf) == -1)
223 warn("%s", buf);
224 exprfree(e);
225 *sz = cur;
226 return res != NULL || cur;
227 }
228
229 /*
230 * Merge the results for the expression tree rooted at e
231 * into the the result list htab.
232 */
233 static struct ohash *
manmerge(struct expr * e,struct ohash * htab)234 manmerge(struct expr *e, struct ohash *htab)
235 {
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();
245 }
246 }
247
248 static struct ohash *
manmerge_term(struct expr * e,struct ohash * htab)249 manmerge_term(struct expr *e, struct ohash *htab)
250 {
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 */
288
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);
295 if ((rp = ohash_find(htab, slot)) != NULL) {
296 rp->bits |= res.bits;
297 continue;
298 }
299 rp = mandoc_malloc(sizeof(*rp));
300 *rp = res;
301 ohash_insert(htab, slot, rp);
302 }
303 }
304 return htab;
305 }
306
307 static struct ohash *
manmerge_or(struct expr * e,struct ohash * htab)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 }
316
317 static struct ohash *
manmerge_and(struct expr * e,struct ohash * htab)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;
323
324 /* Evaluate the first term of the AND clause. */
325
326 hand = manmerge(e, NULL);
327
328 while ((e = e->next) != NULL) {
329
330 /* Evaluate the next term and prepare for ANDing. */
331
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));
340
341 /* Keep all pages that are in both result sets. */
342
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);
353 }
354
355 /* Discard the merged results. */
356
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 }
365
366 /* Merge the result of the AND into htab. */
367
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. */
382
383 ohash_delete(hand);
384 free(hand);
385 return htab;
386 }
387
388 void
mansearch_free(struct manpage * res,size_t sz)389 mansearch_free(struct manpage *res, size_t sz)
390 {
391 size_t i;
392
393 for (i = 0; i < sz; i++) {
394 free(res[i].file);
395 free(res[i].names);
396 free(res[i].output);
397 }
398 free(res);
399 }
400
401 static int
manpage_compare(const void * vp1,const void * vp2)402 manpage_compare(const void *vp1, const void *vp2)
403 {
404 const struct manpage *mp1, *mp2;
405 const char *cp1, *cp2;
406 size_t sz1, sz2;
407 int diff;
408
409 mp1 = vp1;
410 mp2 = vp2;
411 if ((diff = mp2->bits - mp1->bits) ||
412 (diff = mp1->sec - mp2->sec))
413 return diff;
414
415 /* Fall back to alphabetic ordering of names. */
416 sz1 = strcspn(mp1->names, "(");
417 sz2 = strcspn(mp2->names, "(");
418 if (sz1 < sz2)
419 sz1 = sz2;
420 if ((diff = strncasecmp(mp1->names, mp2->names, sz1)))
421 return diff;
422
423 /* For identical names and sections, prefer arch-dependent. */
424 cp1 = strchr(mp1->names + sz1, '/');
425 cp2 = strchr(mp2->names + sz2, '/');
426 return cp1 != NULL && cp2 != NULL ? strcasecmp(cp1, cp2) :
427 cp1 != NULL ? -1 : cp2 != NULL ? 1 : 0;
428 }
429
430 static char *
buildnames(const struct dbm_page * page)431 buildnames(const struct dbm_page *page)
432 {
433 char *buf;
434 size_t i, sz;
435
436 sz = lstlen(page->name, 2) + 1 + lstlen(page->sect, 2) +
437 (page->arch == NULL ? 0 : 1 + lstlen(page->arch, 2)) + 2;
438 buf = mandoc_malloc(sz);
439 i = 0;
440 lstcat(buf, &i, page->name, ", ");
441 buf[i++] = '(';
442 lstcat(buf, &i, page->sect, ", ");
443 if (page->arch != NULL) {
444 buf[i++] = '/';
445 lstcat(buf, &i, page->arch, ", ");
446 }
447 buf[i++] = ')';
448 buf[i++] = '\0';
449 assert(i == sz);
450 return buf;
451 }
452
453 /*
454 * Count the buffer space needed to print the NUL-terminated
455 * list of NUL-terminated strings, when printing sep separator
456 * characters between strings.
457 */
458 static size_t
lstlen(const char * cp,size_t sep)459 lstlen(const char *cp, size_t sep)
460 {
461 size_t sz;
462
463 for (sz = 0; *cp != '\0'; cp++) {
464
465 /* Skip names appearing only in the SYNOPSIS. */
466 if (*cp <= (char)(NAME_SYN & NAME_MASK)) {
467 while (*cp != '\0')
468 cp++;
469 continue;
470 }
471
472 /* Skip name class markers. */
473 if (*cp < ' ')
474 cp++;
475
476 /* Print a separator before each but the first string. */
477 if (sz)
478 sz += sep;
479
480 /* Copy one string. */
481 while (*cp != '\0') {
482 sz++;
483 cp++;
484 }
485 }
486 return sz;
487 }
488
489 /*
490 * Print the NUL-terminated list of NUL-terminated strings
491 * into the buffer, separating strings with sep.
492 */
493 static void
lstcat(char * buf,size_t * i,const char * cp,const char * sep)494 lstcat(char *buf, size_t *i, const char *cp, const char *sep)
495 {
496 const char *s;
497 size_t i_start;
498
499 for (i_start = *i; *cp != '\0'; cp++) {
500
501 /* Skip names appearing only in the SYNOPSIS. */
502 if (*cp <= (char)(NAME_SYN & NAME_MASK)) {
503 while (*cp != '\0')
504 cp++;
505 continue;
506 }
507
508 /* Skip name class markers. */
509 if (*cp < ' ')
510 cp++;
511
512 /* Print a separator before each but the first string. */
513 if (*i > i_start) {
514 s = sep;
515 while (*s != '\0')
516 buf[(*i)++] = *s++;
517 }
518
519 /* Copy one string. */
520 while (*cp != '\0')
521 buf[(*i)++] = *cp++;
522 }
523
524 }
525
526 /*
527 * Return 1 if the string *want occurs in any of the strings
528 * in the NUL-terminated string list *have, or 0 otherwise.
529 * If either argument is NULL or empty, assume no filtering
530 * is desired and return 1.
531 */
532 static int
lstmatch(const char * want,const char * have)533 lstmatch(const char *want, const char *have)
534 {
535 if (want == NULL || have == NULL || *have == '\0')
536 return 1;
537 while (*have != '\0') {
538 if (strcasestr(have, want) != NULL)
539 return 1;
540 have = strchr(have, '\0') + 1;
541 }
542 return 0;
543 }
544
545 /*
546 * Build a list of values taken by the macro im in the manual page.
547 */
548 static char *
buildoutput(size_t im,struct dbm_page * page)549 buildoutput(size_t im, struct dbm_page *page)
550 {
551 const char *oldoutput, *sep, *input;
552 char *output, *newoutput, *value;
553 size_t sz, i;
554
555 switch (im) {
556 case KEY_Nd:
557 return mandoc_strdup(page->desc);
558 case KEY_Nm:
559 input = page->name;
560 break;
561 case KEY_sec:
562 input = page->sect;
563 break;
564 case KEY_arch:
565 input = page->arch;
566 if (input == NULL)
567 input = "all\0";
568 break;
569 default:
570 input = NULL;
571 break;
572 }
573
574 if (input != NULL) {
575 sz = lstlen(input, 3) + 1;
576 output = mandoc_malloc(sz);
577 i = 0;
578 lstcat(output, &i, input, " # ");
579 output[i++] = '\0';
580 assert(i == sz);
581 return output;
582 }
583
584 output = NULL;
585 dbm_macro_bypage(im - 2, page->addr);
586 while ((value = dbm_macro_next()) != NULL) {
587 if (output == NULL) {
588 oldoutput = "";
589 sep = "";
590 } else {
591 oldoutput = output;
592 sep = " # ";
593 }
594 mandoc_asprintf(&newoutput, "%s%s%s", oldoutput, sep, value);
595 free(output);
596 output = newoutput;
597 }
598 return output;
599 }
600
601 /*
602 * Compile a set of string tokens into an expression.
603 * Tokens in "argv" are assumed to be individual expression atoms (e.g.,
604 * "(", "foo=bar", etc.).
605 */
606 static struct expr *
exprcomp(const struct mansearch * search,int argc,char * argv[],int * argi)607 exprcomp(const struct mansearch *search, int argc, char *argv[], int *argi)
608 {
609 struct expr *parent, *child;
610 int needterm, nested;
611
612 if ((nested = *argi) == argc)
613 return NULL;
614 needterm = 1;
615 parent = child = NULL;
616 while (*argi < argc) {
617 if (strcmp(")", argv[*argi]) == 0) {
618 if (needterm)
619 warnx("missing term "
620 "before closing parenthesis");
621 needterm = 0;
622 if (nested)
623 break;
624 warnx("ignoring unmatched right parenthesis");
625 ++*argi;
626 continue;
627 }
628 if (strcmp("-o", argv[*argi]) == 0) {
629 if (needterm) {
630 if (*argi > 0)
631 warnx("ignoring -o after %s",
632 argv[*argi - 1]);
633 else
634 warnx("ignoring initial -o");
635 }
636 needterm = 1;
637 ++*argi;
638 continue;
639 }
640 needterm = 0;
641 if (child == NULL) {
642 child = expr_and(search, argc, argv, argi);
643 continue;
644 }
645 if (parent == NULL) {
646 parent = mandoc_calloc(1, sizeof(*parent));
647 parent->type = EXPR_OR;
648 parent->next = NULL;
649 parent->child = child;
650 }
651 child->next = expr_and(search, argc, argv, argi);
652 child = child->next;
653 }
654 if (needterm && *argi)
655 warnx("ignoring trailing %s", argv[*argi - 1]);
656 return parent == NULL ? child : parent;
657 }
658
659 static struct expr *
expr_and(const struct mansearch * search,int argc,char * argv[],int * argi)660 expr_and(const struct mansearch *search, int argc, char *argv[], int *argi)
661 {
662 struct expr *parent, *child;
663 int needterm;
664
665 needterm = 1;
666 parent = child = NULL;
667 while (*argi < argc) {
668 if (strcmp(")", argv[*argi]) == 0) {
669 if (needterm)
670 warnx("missing term "
671 "before closing parenthesis");
672 needterm = 0;
673 break;
674 }
675 if (strcmp("-o", argv[*argi]) == 0)
676 break;
677 if (strcmp("-a", argv[*argi]) == 0) {
678 if (needterm) {
679 if (*argi > 0)
680 warnx("ignoring -a after %s",
681 argv[*argi - 1]);
682 else
683 warnx("ignoring initial -a");
684 }
685 needterm = 1;
686 ++*argi;
687 continue;
688 }
689 if (needterm == 0)
690 break;
691 if (child == NULL) {
692 child = exprterm(search, argc, argv, argi);
693 if (child != NULL)
694 needterm = 0;
695 continue;
696 }
697 needterm = 0;
698 if (parent == NULL) {
699 parent = mandoc_calloc(1, sizeof(*parent));
700 parent->type = EXPR_AND;
701 parent->next = NULL;
702 parent->child = child;
703 }
704 child->next = exprterm(search, argc, argv, argi);
705 if (child->next != NULL) {
706 child = child->next;
707 needterm = 0;
708 }
709 }
710 if (needterm && *argi)
711 warnx("ignoring trailing %s", argv[*argi - 1]);
712 return parent == NULL ? child : parent;
713 }
714
715 static struct expr *
exprterm(const struct mansearch * search,int argc,char * argv[],int * argi)716 exprterm(const struct mansearch *search, int argc, char *argv[], int *argi)
717 {
718 char errbuf[BUFSIZ];
719 struct expr *e;
720 char *key, *val;
721 uint64_t iterbit;
722 int cs, i, irc;
723
724 if (strcmp("(", argv[*argi]) == 0) {
725 ++*argi;
726 e = exprcomp(search, argc, argv, argi);
727 if (*argi < argc) {
728 assert(strcmp(")", argv[*argi]) == 0);
729 ++*argi;
730 } else
731 warnx("unclosed parenthesis");
732 return e;
733 }
734
735 if (strcmp("-i", argv[*argi]) == 0 && *argi + 1 < argc) {
736 cs = 0;
737 ++*argi;
738 } else
739 cs = 1;
740
741 e = mandoc_calloc(1, sizeof(*e));
742 e->type = EXPR_TERM;
743 e->bits = 0;
744 e->next = NULL;
745 e->child = NULL;
746
747 if (search->argmode == ARG_NAME) {
748 e->bits = TYPE_Nm;
749 e->match.type = DBM_EXACT;
750 e->match.str = argv[(*argi)++];
751 return e;
752 }
753
754 /*
755 * Separate macro keys from search string.
756 * If needed, request regular expression handling.
757 */
758
759 if (search->argmode == ARG_WORD) {
760 e->bits = TYPE_Nm;
761 e->match.type = DBM_REGEX;
762 mandoc_asprintf(&val, "[[:<:]]%s[[:>:]]", argv[*argi]);
763 cs = 0;
764 } else if ((val = strpbrk(argv[*argi], "=~")) == NULL) {
765 e->bits = TYPE_Nm | TYPE_Nd;
766 e->match.type = DBM_REGEX;
767 val = argv[*argi];
768 cs = 0;
769 } else {
770 if (val == argv[*argi])
771 e->bits = TYPE_Nm | TYPE_Nd;
772 if (*val == '=') {
773 e->match.type = DBM_SUB;
774 e->match.str = val + 1;
775 } else
776 e->match.type = DBM_REGEX;
777 *val++ = '\0';
778 if (strstr(argv[*argi], "arch") != NULL)
779 cs = 0;
780 }
781
782 /* Compile regular expressions. */
783
784 if (e->match.type == DBM_REGEX) {
785 e->match.re = mandoc_malloc(sizeof(*e->match.re));
786 irc = regcomp(e->match.re, val,
787 REG_EXTENDED | REG_NOSUB | (cs ? 0 : REG_ICASE));
788 if (irc) {
789 regerror(irc, e->match.re, errbuf, sizeof(errbuf));
790 warnx("regcomp /%s/: %s", val, errbuf);
791 }
792 if (search->argmode == ARG_WORD)
793 free(val);
794 if (irc) {
795 free(e->match.re);
796 free(e);
797 ++*argi;
798 return NULL;
799 }
800 }
801
802 if (e->bits) {
803 ++*argi;
804 return e;
805 }
806
807 /*
808 * Parse out all possible fields.
809 * If the field doesn't resolve, bail.
810 */
811
812 while (NULL != (key = strsep(&argv[*argi], ","))) {
813 if ('\0' == *key)
814 continue;
815 for (i = 0, iterbit = 1; i < KEY_MAX; i++, iterbit <<= 1) {
816 if (0 == strcasecmp(key, mansearch_keynames[i])) {
817 e->bits |= iterbit;
818 break;
819 }
820 }
821 if (i == KEY_MAX) {
822 if (strcasecmp(key, "any"))
823 warnx("treating unknown key "
824 "\"%s\" as \"any\"", key);
825 e->bits |= ~0ULL;
826 }
827 }
828
829 ++*argi;
830 return e;
831 }
832
833 static void
exprfree(struct expr * e)834 exprfree(struct expr *e)
835 {
836 if (e->next != NULL)
837 exprfree(e->next);
838 if (e->child != NULL)
839 exprfree(e->child);
840 free(e);
841 }
842