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