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