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