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