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