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