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