]> git.cameronkatri.com Git - mandoc.git/blob - mansearch.c
In man(1) mode without -a, stop searching after the first manual tree
[mandoc.git] / mansearch.c
1 /* $Id: mansearch.c,v 1.49 2014/11/11 19:04:55 schwarze Exp $ */
2 /*
3 * Copyright (c) 2012 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2013, 2014 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 AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR 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 #include "config.h"
19
20 #include <sys/mman.h>
21 #include <sys/types.h>
22
23 #include <assert.h>
24 #include <fcntl.h>
25 #include <getopt.h>
26 #include <limits.h>
27 #include <regex.h>
28 #include <stdio.h>
29 #include <stdint.h>
30 #include <stddef.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34
35 #if HAVE_OHASH
36 #include <ohash.h>
37 #else
38 #include "compat_ohash.h"
39 #endif
40 #include <sqlite3.h>
41 #ifndef SQLITE_DETERMINISTIC
42 #define SQLITE_DETERMINISTIC 0
43 #endif
44
45 #include "mandoc.h"
46 #include "mandoc_aux.h"
47 #include "manpath.h"
48 #include "mansearch.h"
49
50 extern int mansearch_keymax;
51 extern const char *const mansearch_keynames[];
52
53 #define SQL_BIND_TEXT(_db, _s, _i, _v) \
54 do { if (SQLITE_OK != sqlite3_bind_text \
55 ((_s), (_i)++, (_v), -1, SQLITE_STATIC)) \
56 fprintf(stderr, "%s\n", sqlite3_errmsg((_db))); \
57 } while (0)
58 #define SQL_BIND_INT64(_db, _s, _i, _v) \
59 do { if (SQLITE_OK != sqlite3_bind_int64 \
60 ((_s), (_i)++, (_v))) \
61 fprintf(stderr, "%s\n", sqlite3_errmsg((_db))); \
62 } while (0)
63 #define SQL_BIND_BLOB(_db, _s, _i, _v) \
64 do { if (SQLITE_OK != sqlite3_bind_blob \
65 ((_s), (_i)++, (&_v), sizeof(_v), SQLITE_STATIC)) \
66 fprintf(stderr, "%s\n", sqlite3_errmsg((_db))); \
67 } while (0)
68
69 struct expr {
70 regex_t regexp; /* compiled regexp, if applicable */
71 const char *substr; /* to search for, if applicable */
72 struct expr *next; /* next in sequence */
73 uint64_t bits; /* type-mask */
74 int equal; /* equality, not subsring match */
75 int open; /* opening parentheses before */
76 int and; /* logical AND before */
77 int close; /* closing parentheses after */
78 };
79
80 struct match {
81 uint64_t pageid; /* identifier in database */
82 char *desc; /* manual page description */
83 int form; /* bit field: formatted, zipped? */
84 };
85
86 static void buildnames(struct manpage *, sqlite3 *,
87 sqlite3_stmt *, uint64_t,
88 const char *, int form);
89 static char *buildoutput(sqlite3 *, sqlite3_stmt *,
90 uint64_t, uint64_t);
91 static void *hash_alloc(size_t, void *);
92 static void hash_free(void *, void *);
93 static void *hash_calloc(size_t, size_t, void *);
94 static struct expr *exprcomp(const struct mansearch *,
95 int, char *[]);
96 static void exprfree(struct expr *);
97 static struct expr *exprspec(struct expr *, uint64_t,
98 const char *, const char *);
99 static struct expr *exprterm(const struct mansearch *, char *, int);
100 static int manpage_compare(const void *, const void *);
101 static void sql_append(char **sql, size_t *sz,
102 const char *newstr, int count);
103 static void sql_match(sqlite3_context *context,
104 int argc, sqlite3_value **argv);
105 static void sql_regexp(sqlite3_context *context,
106 int argc, sqlite3_value **argv);
107 static char *sql_statement(const struct expr *);
108
109
110 int
111 mansearch_setup(int start)
112 {
113 static void *pagecache;
114 int c;
115
116 #define PC_PAGESIZE 1280
117 #define PC_NUMPAGES 256
118
119 if (start) {
120 if (NULL != pagecache) {
121 fprintf(stderr, "pagecache already enabled\n");
122 return((int)MANDOCLEVEL_BADARG);
123 }
124
125 pagecache = mmap(NULL, PC_PAGESIZE * PC_NUMPAGES,
126 PROT_READ | PROT_WRITE,
127 MAP_SHARED | MAP_ANON, -1, 0);
128
129 if (MAP_FAILED == pagecache) {
130 perror("mmap");
131 pagecache = NULL;
132 return((int)MANDOCLEVEL_SYSERR);
133 }
134
135 c = sqlite3_config(SQLITE_CONFIG_PAGECACHE,
136 pagecache, PC_PAGESIZE, PC_NUMPAGES);
137
138 if (SQLITE_OK == c)
139 return((int)MANDOCLEVEL_OK);
140
141 fprintf(stderr, "pagecache: %s\n", sqlite3_errstr(c));
142
143 } else if (NULL == pagecache) {
144 fprintf(stderr, "pagecache missing\n");
145 return((int)MANDOCLEVEL_BADARG);
146 }
147
148 if (-1 == munmap(pagecache, PC_PAGESIZE * PC_NUMPAGES)) {
149 perror("munmap");
150 pagecache = NULL;
151 return((int)MANDOCLEVEL_SYSERR);
152 }
153
154 pagecache = NULL;
155 return((int)MANDOCLEVEL_OK);
156 }
157
158 int
159 mansearch(const struct mansearch *search,
160 const struct manpaths *paths,
161 int argc, char *argv[],
162 struct manpage **res, size_t *sz)
163 {
164 int fd, rc, c, indexbit;
165 int64_t pageid;
166 uint64_t outbit, iterbit;
167 char buf[PATH_MAX];
168 char *sql;
169 struct manpage *mpage;
170 struct expr *e, *ep;
171 sqlite3 *db;
172 sqlite3_stmt *s, *s2;
173 struct match *mp;
174 struct ohash_info info;
175 struct ohash htab;
176 unsigned int idx;
177 size_t i, j, cur, maxres;
178
179 info.calloc = hash_calloc;
180 info.alloc = hash_alloc;
181 info.free = hash_free;
182 info.key_offset = offsetof(struct match, pageid);
183
184 *sz = cur = maxres = 0;
185 sql = NULL;
186 *res = NULL;
187 fd = -1;
188 e = NULL;
189 rc = 0;
190
191 if (0 == argc)
192 goto out;
193 if (NULL == (e = exprcomp(search, argc, argv)))
194 goto out;
195
196 outbit = 0;
197 if (NULL != search->outkey) {
198 for (indexbit = 0, iterbit = 1;
199 indexbit < mansearch_keymax;
200 indexbit++, iterbit <<= 1) {
201 if (0 == strcasecmp(search->outkey,
202 mansearch_keynames[indexbit])) {
203 outbit = iterbit;
204 break;
205 }
206 }
207 }
208
209 /*
210 * Save a descriptor to the current working directory.
211 * Since pathnames in the "paths" variable might be relative,
212 * and we'll be chdir()ing into them, we need to keep a handle
213 * on our current directory from which to start the chdir().
214 */
215
216 if (NULL == getcwd(buf, PATH_MAX)) {
217 perror("getcwd");
218 goto out;
219 } else if (-1 == (fd = open(buf, O_RDONLY, 0))) {
220 perror(buf);
221 goto out;
222 }
223
224 sql = sql_statement(e);
225
226 /*
227 * Loop over the directories (containing databases) for us to
228 * search.
229 * Don't let missing/bad databases/directories phase us.
230 * In each, try to open the resident database and, if it opens,
231 * scan it for our match expression.
232 */
233
234 for (i = 0; i < paths->sz; i++) {
235 if (-1 == fchdir(fd)) {
236 perror(buf);
237 free(*res);
238 break;
239 } else if (-1 == chdir(paths->paths[i])) {
240 perror(paths->paths[i]);
241 continue;
242 }
243
244 c = sqlite3_open_v2(MANDOC_DB, &db,
245 SQLITE_OPEN_READONLY, NULL);
246
247 if (SQLITE_OK != c) {
248 perror(MANDOC_DB);
249 sqlite3_close(db);
250 continue;
251 }
252
253 /*
254 * Define the SQL functions for substring
255 * and regular expression matching.
256 */
257
258 c = sqlite3_create_function(db, "match", 2,
259 SQLITE_UTF8 | SQLITE_DETERMINISTIC,
260 NULL, sql_match, NULL, NULL);
261 assert(SQLITE_OK == c);
262 c = sqlite3_create_function(db, "regexp", 2,
263 SQLITE_UTF8 | SQLITE_DETERMINISTIC,
264 NULL, sql_regexp, NULL, NULL);
265 assert(SQLITE_OK == c);
266
267 j = 1;
268 c = sqlite3_prepare_v2(db, sql, -1, &s, NULL);
269 if (SQLITE_OK != c)
270 fprintf(stderr, "%s\n", sqlite3_errmsg(db));
271
272 for (ep = e; NULL != ep; ep = ep->next) {
273 if (NULL == ep->substr) {
274 SQL_BIND_BLOB(db, s, j, ep->regexp);
275 } else
276 SQL_BIND_TEXT(db, s, j, ep->substr);
277 if (0 == ((TYPE_Nd | TYPE_Nm) & ep->bits))
278 SQL_BIND_INT64(db, s, j, ep->bits);
279 }
280
281 memset(&htab, 0, sizeof(struct ohash));
282 ohash_init(&htab, 4, &info);
283
284 /*
285 * Hash each entry on its [unique] document identifier.
286 * This is a uint64_t.
287 * Instead of using a hash function, simply convert the
288 * uint64_t to a uint32_t, the hash value's type.
289 * This gives good performance and preserves the
290 * distribution of buckets in the table.
291 */
292 while (SQLITE_ROW == (c = sqlite3_step(s))) {
293 pageid = sqlite3_column_int64(s, 2);
294 idx = ohash_lookup_memory(&htab,
295 (char *)&pageid, sizeof(uint64_t),
296 (uint32_t)pageid);
297
298 if (NULL != ohash_find(&htab, idx))
299 continue;
300
301 mp = mandoc_calloc(1, sizeof(struct match));
302 mp->pageid = pageid;
303 mp->form = sqlite3_column_int(s, 1);
304 if (TYPE_Nd == outbit)
305 mp->desc = mandoc_strdup((const char *)
306 sqlite3_column_text(s, 0));
307 ohash_insert(&htab, idx, mp);
308 }
309
310 if (SQLITE_DONE != c)
311 fprintf(stderr, "%s\n", sqlite3_errmsg(db));
312
313 sqlite3_finalize(s);
314
315 c = sqlite3_prepare_v2(db,
316 "SELECT sec, arch, name, pageid FROM mlinks "
317 "WHERE pageid=? ORDER BY sec, arch, name",
318 -1, &s, NULL);
319 if (SQLITE_OK != c)
320 fprintf(stderr, "%s\n", sqlite3_errmsg(db));
321
322 c = sqlite3_prepare_v2(db,
323 "SELECT bits, key, pageid FROM keys "
324 "WHERE pageid=? AND bits & ?",
325 -1, &s2, NULL);
326 if (SQLITE_OK != c)
327 fprintf(stderr, "%s\n", sqlite3_errmsg(db));
328
329 for (mp = ohash_first(&htab, &idx);
330 NULL != mp;
331 mp = ohash_next(&htab, &idx)) {
332 if (cur + 1 > maxres) {
333 maxres += 1024;
334 *res = mandoc_reallocarray(*res,
335 maxres, sizeof(struct manpage));
336 }
337 mpage = *res + cur;
338 mpage->ipath = i;
339 mpage->sec = 10;
340 mpage->form = mp->form;
341 buildnames(mpage, db, s, mp->pageid,
342 paths->paths[i], mp->form);
343 mpage->output = TYPE_Nd & outbit ?
344 mp->desc : outbit ?
345 buildoutput(db, s2, mp->pageid, outbit) : NULL;
346
347 free(mp);
348 cur++;
349 }
350
351 sqlite3_finalize(s);
352 sqlite3_finalize(s2);
353 sqlite3_close(db);
354 ohash_delete(&htab);
355
356 /*
357 * In man(1) mode, prefer matches in earlier trees
358 * over matches in later trees.
359 */
360
361 if (cur && search->firstmatch)
362 break;
363 }
364 qsort(*res, cur, sizeof(struct manpage), manpage_compare);
365 rc = 1;
366 out:
367 if (-1 != fd) {
368 if (-1 == fchdir(fd))
369 perror(buf);
370 close(fd);
371 }
372 exprfree(e);
373 free(sql);
374 *sz = cur;
375 return(rc);
376 }
377
378 void
379 mansearch_free(struct manpage *res, size_t sz)
380 {
381 size_t i;
382
383 for (i = 0; i < sz; i++) {
384 free(res[i].file);
385 free(res[i].names);
386 free(res[i].output);
387 }
388 free(res);
389 }
390
391 static int
392 manpage_compare(const void *vp1, const void *vp2)
393 {
394 const struct manpage *mp1, *mp2;
395 int diff;
396
397 mp1 = vp1;
398 mp2 = vp2;
399 diff = mp1->sec - mp2->sec;
400 return(diff ? diff : strcasecmp(mp1->names, mp2->names));
401 }
402
403 static void
404 buildnames(struct manpage *mpage, sqlite3 *db, sqlite3_stmt *s,
405 uint64_t pageid, const char *path, int form)
406 {
407 char *newnames, *prevsec, *prevarch;
408 const char *oldnames, *sep1, *name, *sec, *sep2, *arch, *fsec;
409 const char *gzip;
410 size_t i;
411 int c;
412
413 mpage->file = NULL;
414 mpage->names = NULL;
415 prevsec = prevarch = NULL;
416 i = 1;
417 SQL_BIND_INT64(db, s, i, pageid);
418 while (SQLITE_ROW == (c = sqlite3_step(s))) {
419
420 /* Decide whether we already have some names. */
421
422 if (NULL == mpage->names) {
423 oldnames = "";
424 sep1 = "";
425 } else {
426 oldnames = mpage->names;
427 sep1 = ", ";
428 }
429
430 /* Fetch the next name. */
431
432 sec = (const char *)sqlite3_column_text(s, 0);
433 arch = (const char *)sqlite3_column_text(s, 1);
434 name = (const char *)sqlite3_column_text(s, 2);
435
436 /* Remember the first section found. */
437
438 if (9 < mpage->sec && '1' <= *sec && '9' >= *sec)
439 mpage->sec = (*sec - '1') + 1;
440
441 /* If the section changed, append the old one. */
442
443 if (NULL != prevsec &&
444 (strcmp(sec, prevsec) ||
445 strcmp(arch, prevarch))) {
446 sep2 = '\0' == *prevarch ? "" : "/";
447 mandoc_asprintf(&newnames, "%s(%s%s%s)",
448 oldnames, prevsec, sep2, prevarch);
449 free(mpage->names);
450 oldnames = mpage->names = newnames;
451 free(prevsec);
452 free(prevarch);
453 prevsec = prevarch = NULL;
454 }
455
456 /* Save the new section, to append it later. */
457
458 if (NULL == prevsec) {
459 prevsec = mandoc_strdup(sec);
460 prevarch = mandoc_strdup(arch);
461 }
462
463 /* Append the new name. */
464
465 mandoc_asprintf(&newnames, "%s%s%s",
466 oldnames, sep1, name);
467 free(mpage->names);
468 mpage->names = newnames;
469
470 /* Also save the first file name encountered. */
471
472 if (NULL != mpage->file)
473 continue;
474
475 if (form & FORM_SRC) {
476 sep1 = "man";
477 fsec = sec;
478 } else {
479 sep1 = "cat";
480 fsec = "0";
481 }
482 if (form & FORM_GZ)
483 gzip = ".gz";
484 else
485 gzip = "";
486 sep2 = '\0' == *arch ? "" : "/";
487 mandoc_asprintf(&mpage->file, "%s/%s%s%s%s/%s.%s%s",
488 path, sep1, sec, sep2, arch, name, fsec, gzip);
489 }
490 if (SQLITE_DONE != c)
491 fprintf(stderr, "%s\n", sqlite3_errmsg(db));
492 sqlite3_reset(s);
493
494 /* Append one final section to the names. */
495
496 if (NULL != prevsec) {
497 sep2 = '\0' == *prevarch ? "" : "/";
498 mandoc_asprintf(&newnames, "%s(%s%s%s)",
499 mpage->names, prevsec, sep2, prevarch);
500 free(mpage->names);
501 mpage->names = newnames;
502 free(prevsec);
503 free(prevarch);
504 }
505 }
506
507 static char *
508 buildoutput(sqlite3 *db, sqlite3_stmt *s, uint64_t pageid, uint64_t outbit)
509 {
510 char *output, *newoutput;
511 const char *oldoutput, *sep1, *data;
512 size_t i;
513 int c;
514
515 output = NULL;
516 i = 1;
517 SQL_BIND_INT64(db, s, i, pageid);
518 SQL_BIND_INT64(db, s, i, outbit);
519 while (SQLITE_ROW == (c = sqlite3_step(s))) {
520 if (NULL == output) {
521 oldoutput = "";
522 sep1 = "";
523 } else {
524 oldoutput = output;
525 sep1 = " # ";
526 }
527 data = (const char *)sqlite3_column_text(s, 1);
528 mandoc_asprintf(&newoutput, "%s%s%s",
529 oldoutput, sep1, data);
530 free(output);
531 output = newoutput;
532 }
533 if (SQLITE_DONE != c)
534 fprintf(stderr, "%s\n", sqlite3_errmsg(db));
535 sqlite3_reset(s);
536 return(output);
537 }
538
539 /*
540 * Implement substring match as an application-defined SQL function.
541 * Using the SQL LIKE or GLOB operators instead would be a bad idea
542 * because that would require escaping metacharacters in the string
543 * being searched for.
544 */
545 static void
546 sql_match(sqlite3_context *context, int argc, sqlite3_value **argv)
547 {
548
549 assert(2 == argc);
550 sqlite3_result_int(context, NULL != strcasestr(
551 (const char *)sqlite3_value_text(argv[1]),
552 (const char *)sqlite3_value_text(argv[0])));
553 }
554
555 /*
556 * Implement regular expression match
557 * as an application-defined SQL function.
558 */
559 static void
560 sql_regexp(sqlite3_context *context, int argc, sqlite3_value **argv)
561 {
562
563 assert(2 == argc);
564 sqlite3_result_int(context, !regexec(
565 (regex_t *)sqlite3_value_blob(argv[0]),
566 (const char *)sqlite3_value_text(argv[1]),
567 0, NULL, 0));
568 }
569
570 static void
571 sql_append(char **sql, size_t *sz, const char *newstr, int count)
572 {
573 size_t newsz;
574
575 newsz = 1 < count ? (size_t)count : strlen(newstr);
576 *sql = mandoc_realloc(*sql, *sz + newsz + 1);
577 if (1 < count)
578 memset(*sql + *sz, *newstr, (size_t)count);
579 else
580 memcpy(*sql + *sz, newstr, newsz);
581 *sz += newsz;
582 (*sql)[*sz] = '\0';
583 }
584
585 /*
586 * Prepare the search SQL statement.
587 */
588 static char *
589 sql_statement(const struct expr *e)
590 {
591 char *sql;
592 size_t sz;
593 int needop;
594
595 sql = mandoc_strdup(
596 "SELECT desc, form, pageid FROM mpages WHERE ");
597 sz = strlen(sql);
598
599 for (needop = 0; NULL != e; e = e->next) {
600 if (e->and)
601 sql_append(&sql, &sz, " AND ", 1);
602 else if (needop)
603 sql_append(&sql, &sz, " OR ", 1);
604 if (e->open)
605 sql_append(&sql, &sz, "(", e->open);
606 sql_append(&sql, &sz,
607 TYPE_Nd & e->bits
608 ? (NULL == e->substr
609 ? "desc REGEXP ?"
610 : "desc MATCH ?")
611 : TYPE_Nm == e->bits
612 ? (NULL == e->substr
613 ? "pageid IN (SELECT pageid FROM names "
614 "WHERE name REGEXP ?)"
615 : e->equal
616 ? "pageid IN (SELECT pageid FROM names "
617 "WHERE name = ?)"
618 : "pageid IN (SELECT pageid FROM names "
619 "WHERE name MATCH ?)")
620 : (NULL == e->substr
621 ? "pageid IN (SELECT pageid FROM keys "
622 "WHERE key REGEXP ? AND bits & ?)"
623 : "pageid IN (SELECT pageid FROM keys "
624 "WHERE key MATCH ? AND bits & ?)"), 1);
625 if (e->close)
626 sql_append(&sql, &sz, ")", e->close);
627 needop = 1;
628 }
629
630 return(sql);
631 }
632
633 /*
634 * Compile a set of string tokens into an expression.
635 * Tokens in "argv" are assumed to be individual expression atoms (e.g.,
636 * "(", "foo=bar", etc.).
637 */
638 static struct expr *
639 exprcomp(const struct mansearch *search, int argc, char *argv[])
640 {
641 uint64_t mask;
642 int i, toopen, logic, igncase, toclose;
643 struct expr *first, *prev, *cur, *next;
644
645 first = cur = NULL;
646 logic = igncase = toclose = 0;
647 toopen = NULL != search->sec || NULL != search->arch;
648
649 for (i = 0; i < argc; i++) {
650 if (0 == strcmp("(", argv[i])) {
651 if (igncase)
652 goto fail;
653 toopen++;
654 toclose++;
655 continue;
656 } else if (0 == strcmp(")", argv[i])) {
657 if (toopen || logic || igncase || NULL == cur)
658 goto fail;
659 cur->close++;
660 if (0 > --toclose)
661 goto fail;
662 continue;
663 } else if (0 == strcmp("-a", argv[i])) {
664 if (toopen || logic || igncase || NULL == cur)
665 goto fail;
666 logic = 1;
667 continue;
668 } else if (0 == strcmp("-o", argv[i])) {
669 if (toopen || logic || igncase || NULL == cur)
670 goto fail;
671 logic = 2;
672 continue;
673 } else if (0 == strcmp("-i", argv[i])) {
674 if (igncase)
675 goto fail;
676 igncase = 1;
677 continue;
678 }
679 next = exprterm(search, argv[i], !igncase);
680 if (NULL == next)
681 goto fail;
682 if (NULL == first)
683 first = next;
684 else
685 cur->next = next;
686 prev = cur = next;
687
688 /*
689 * Searching for descriptions must be split out
690 * because they are stored in the mpages table,
691 * not in the keys table.
692 */
693
694 for (mask = TYPE_Nm; mask <= TYPE_Nd; mask <<= 1) {
695 if (mask & cur->bits && ~mask & cur->bits) {
696 next = mandoc_calloc(1,
697 sizeof(struct expr));
698 memcpy(next, cur, sizeof(struct expr));
699 prev->open = 1;
700 cur->bits = mask;
701 cur->next = next;
702 cur = next;
703 cur->bits &= ~mask;
704 }
705 }
706 prev->and = (1 == logic);
707 prev->open += toopen;
708 if (cur != prev)
709 cur->close = 1;
710
711 toopen = logic = igncase = 0;
712 }
713 if (toopen || logic || igncase || toclose)
714 goto fail;
715
716 if (NULL != search->sec || NULL != search->arch)
717 cur->close++;
718 if (NULL != search->arch)
719 cur = exprspec(cur, TYPE_arch, search->arch, "^(%s|any)$");
720 if (NULL != search->sec)
721 exprspec(cur, TYPE_sec, search->sec, "^%s$");
722
723 return(first);
724
725 fail:
726 if (NULL != first)
727 exprfree(first);
728 return(NULL);
729 }
730
731 static struct expr *
732 exprspec(struct expr *cur, uint64_t key, const char *value,
733 const char *format)
734 {
735 char errbuf[BUFSIZ];
736 char *cp;
737 int irc;
738
739 mandoc_asprintf(&cp, format, value);
740 cur->next = mandoc_calloc(1, sizeof(struct expr));
741 cur = cur->next;
742 cur->and = 1;
743 cur->bits = key;
744 if (0 != (irc = regcomp(&cur->regexp, cp,
745 REG_EXTENDED | REG_NOSUB | REG_ICASE))) {
746 regerror(irc, &cur->regexp, errbuf, sizeof(errbuf));
747 fprintf(stderr, "regcomp: %s\n", errbuf);
748 cur->substr = value;
749 }
750 free(cp);
751 return(cur);
752 }
753
754 static struct expr *
755 exprterm(const struct mansearch *search, char *buf, int cs)
756 {
757 char errbuf[BUFSIZ];
758 struct expr *e;
759 char *key, *val;
760 uint64_t iterbit;
761 int i, irc;
762
763 if ('\0' == *buf)
764 return(NULL);
765
766 e = mandoc_calloc(1, sizeof(struct expr));
767
768 if (search->argmode == ARG_NAME) {
769 e->bits = TYPE_Nm;
770 e->substr = buf;
771 e->equal = 1;
772 return(e);
773 }
774
775 /*
776 * Separate macro keys from search string.
777 * If needed, request regular expression handling
778 * by setting e->substr to NULL.
779 */
780
781 if (search->argmode == ARG_WORD) {
782 e->bits = TYPE_Nm;
783 e->substr = NULL;
784 mandoc_asprintf(&val, "[[:<:]]%s[[:>:]]", buf);
785 cs = 0;
786 } else if ((val = strpbrk(buf, "=~")) == NULL) {
787 e->bits = TYPE_Nm | TYPE_Nd;
788 e->substr = buf;
789 } else {
790 if (val == buf)
791 e->bits = TYPE_Nm | TYPE_Nd;
792 if ('=' == *val)
793 e->substr = val + 1;
794 *val++ = '\0';
795 if (NULL != strstr(buf, "arch"))
796 cs = 0;
797 }
798
799 /* Compile regular expressions. */
800
801 if (NULL == e->substr) {
802 irc = regcomp(&e->regexp, val,
803 REG_EXTENDED | REG_NOSUB | (cs ? 0 : REG_ICASE));
804 if (search->argmode == ARG_WORD)
805 free(val);
806 if (irc) {
807 regerror(irc, &e->regexp, errbuf, sizeof(errbuf));
808 fprintf(stderr, "regcomp: %s\n", errbuf);
809 free(e);
810 return(NULL);
811 }
812 }
813
814 if (e->bits)
815 return(e);
816
817 /*
818 * Parse out all possible fields.
819 * If the field doesn't resolve, bail.
820 */
821
822 while (NULL != (key = strsep(&buf, ","))) {
823 if ('\0' == *key)
824 continue;
825 for (i = 0, iterbit = 1;
826 i < mansearch_keymax;
827 i++, iterbit <<= 1) {
828 if (0 == strcasecmp(key,
829 mansearch_keynames[i])) {
830 e->bits |= iterbit;
831 break;
832 }
833 }
834 if (i == mansearch_keymax) {
835 if (strcasecmp(key, "any")) {
836 free(e);
837 return(NULL);
838 }
839 e->bits |= ~0ULL;
840 }
841 }
842
843 return(e);
844 }
845
846 static void
847 exprfree(struct expr *p)
848 {
849 struct expr *pp;
850
851 while (NULL != p) {
852 pp = p->next;
853 free(p);
854 p = pp;
855 }
856 }
857
858 static void *
859 hash_calloc(size_t nmemb, size_t sz, void *arg)
860 {
861
862 return(mandoc_calloc(nmemb, sz));
863 }
864
865 static void *
866 hash_alloc(size_t sz, void *arg)
867 {
868
869 return(mandoc_malloc(sz));
870 }
871
872 static void
873 hash_free(void *p, void *arg)
874 {
875
876 free(p);
877 }