Annotation of src/usr.bin/mandoc/apropos_db.c, Revision 1.18
1.18 ! schwarze 1: /* $Id: apropos_db.c,v 1.17 2011/12/25 14:51:33 schwarze Exp $ */
1.1 schwarze 2: /*
1.18 ! schwarze 3: * Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
1.3 schwarze 4: * Copyright (c) 2011 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: */
1.18 ! schwarze 18: #include <sys/param.h>
1.12 schwarze 19: #include <sys/types.h>
1.18 ! schwarze 20:
1.1 schwarze 21: #include <assert.h>
22: #include <fcntl.h>
23: #include <regex.h>
24: #include <stdarg.h>
1.6 schwarze 25: #include <stdint.h>
1.1 schwarze 26: #include <stdlib.h>
27: #include <string.h>
1.5 schwarze 28: #include <unistd.h>
1.13 schwarze 29: #include <db.h>
1.1 schwarze 30:
1.2 schwarze 31: #include "mandocdb.h"
1.1 schwarze 32: #include "apropos_db.h"
33: #include "mandoc.h"
34:
1.18 ! schwarze 35: #define RESFREE(_x) \
! 36: do { \
! 37: free((_x)->file); \
! 38: free((_x)->cat); \
! 39: free((_x)->title); \
! 40: free((_x)->arch); \
! 41: free((_x)->desc); \
! 42: free((_x)->matches); \
! 43: } while (/*CONSTCOND*/0)
1.7 schwarze 44:
1.1 schwarze 45: struct expr {
1.7 schwarze 46: int regex; /* is regex? */
47: int index; /* index in match array */
48: uint64_t mask; /* type-mask */
49: int and; /* is rhs of logical AND? */
50: char *v; /* search value */
51: regex_t re; /* compiled re, if regex */
52: struct expr *next; /* next in sequence */
53: struct expr *subexpr;
1.1 schwarze 54: };
55:
56: struct type {
1.6 schwarze 57: uint64_t mask;
1.1 schwarze 58: const char *name;
59: };
60:
1.8 schwarze 61: struct rectree {
1.18 ! schwarze 62: struct res *node; /* record array for dir tree */
1.8 schwarze 63: int len; /* length of record array */
64: };
65:
1.1 schwarze 66: static const struct type types[] = {
1.2 schwarze 67: { TYPE_An, "An" },
1.6 schwarze 68: { TYPE_Ar, "Ar" },
69: { TYPE_At, "At" },
70: { TYPE_Bsx, "Bsx" },
71: { TYPE_Bx, "Bx" },
1.2 schwarze 72: { TYPE_Cd, "Cd" },
1.6 schwarze 73: { TYPE_Cm, "Cm" },
74: { TYPE_Dv, "Dv" },
75: { TYPE_Dx, "Dx" },
76: { TYPE_Em, "Em" },
1.2 schwarze 77: { TYPE_Er, "Er" },
78: { TYPE_Ev, "Ev" },
1.6 schwarze 79: { TYPE_Fa, "Fa" },
80: { TYPE_Fl, "Fl" },
1.2 schwarze 81: { TYPE_Fn, "Fn" },
82: { TYPE_Fn, "Fo" },
1.6 schwarze 83: { TYPE_Ft, "Ft" },
84: { TYPE_Fx, "Fx" },
85: { TYPE_Ic, "Ic" },
1.2 schwarze 86: { TYPE_In, "In" },
1.6 schwarze 87: { TYPE_Lb, "Lb" },
88: { TYPE_Li, "Li" },
89: { TYPE_Lk, "Lk" },
90: { TYPE_Ms, "Ms" },
91: { TYPE_Mt, "Mt" },
1.2 schwarze 92: { TYPE_Nd, "Nd" },
93: { TYPE_Nm, "Nm" },
1.6 schwarze 94: { TYPE_Nx, "Nx" },
95: { TYPE_Ox, "Ox" },
1.2 schwarze 96: { TYPE_Pa, "Pa" },
1.6 schwarze 97: { TYPE_Rs, "Rs" },
98: { TYPE_Sh, "Sh" },
99: { TYPE_Ss, "Ss" },
1.2 schwarze 100: { TYPE_St, "St" },
1.6 schwarze 101: { TYPE_Sy, "Sy" },
102: { TYPE_Tn, "Tn" },
1.2 schwarze 103: { TYPE_Va, "Va" },
104: { TYPE_Va, "Vt" },
105: { TYPE_Xr, "Xr" },
1.15 schwarze 106: { UINT64_MAX, "any" },
1.1 schwarze 107: { 0, NULL }
108: };
109:
110: static DB *btree_open(void);
1.12 schwarze 111: static int btree_read(const DBT *, const DBT *,
1.17 schwarze 112: const struct mchars *,
113: uint64_t *, recno_t *, char **);
1.7 schwarze 114: static int expreval(const struct expr *, int *);
115: static void exprexec(const struct expr *,
1.18 ! schwarze 116: const char *, uint64_t, struct res *);
1.7 schwarze 117: static int exprmark(const struct expr *,
118: const char *, uint64_t, int *);
119: static struct expr *exprexpr(int, char *[], int *, int *, size_t *);
120: static struct expr *exprterm(char *, int);
1.1 schwarze 121: static DB *index_open(void);
1.8 schwarze 122: static int index_read(const DBT *, const DBT *, int,
1.18 ! schwarze 123: const struct mchars *, struct res *);
1.1 schwarze 124: static void norm_string(const char *,
125: const struct mchars *, char **);
126: static size_t norm_utf8(unsigned int, char[7]);
1.7 schwarze 127: static int single_search(struct rectree *, const struct opts *,
1.5 schwarze 128: const struct expr *, size_t terms,
1.8 schwarze 129: struct mchars *, int);
1.1 schwarze 130:
131: /*
132: * Open the keyword mandoc-db database.
133: */
134: static DB *
135: btree_open(void)
136: {
137: BTREEINFO info;
138: DB *db;
139:
140: memset(&info, 0, sizeof(BTREEINFO));
1.12 schwarze 141: info.lorder = 4321;
1.1 schwarze 142: info.flags = R_DUP;
143:
1.2 schwarze 144: db = dbopen(MANDOC_DB, O_RDONLY, 0, DB_BTREE, &info);
1.8 schwarze 145: if (NULL != db)
1.1 schwarze 146: return(db);
147:
148: return(NULL);
149: }
150:
151: /*
152: * Read a keyword from the database and normalise it.
153: * Return 0 if the database is insane, else 1.
154: */
155: static int
1.17 schwarze 156: btree_read(const DBT *k, const DBT *v, const struct mchars *mc,
157: uint64_t *mask, recno_t *rec, char **buf)
1.1 schwarze 158: {
1.17 schwarze 159: uint64_t vbuf[2];
1.1 schwarze 160:
1.12 schwarze 161: /* Are our sizes sane? */
1.17 schwarze 162: if (k->size < 2 || sizeof(vbuf) != v->size)
1.12 schwarze 163: return(0);
1.8 schwarze 164:
1.12 schwarze 165: /* Is our string nil-terminated? */
166: if ('\0' != ((const char *)k->data)[(int)k->size - 1])
1.1 schwarze 167: return(0);
168:
1.12 schwarze 169: norm_string((const char *)k->data, mc, buf);
1.17 schwarze 170: memcpy(vbuf, v->data, v->size);
171: *mask = betoh64(vbuf[0]);
172: *rec = betoh64(vbuf[1]);
1.1 schwarze 173: return(1);
174: }
175:
176: /*
177: * Take a Unicode codepoint and produce its UTF-8 encoding.
178: * This isn't the best way to do this, but it works.
1.8 schwarze 179: * The magic numbers are from the UTF-8 packaging.
1.1 schwarze 180: * They're not as scary as they seem: read the UTF-8 spec for details.
181: */
182: static size_t
183: norm_utf8(unsigned int cp, char out[7])
184: {
1.15 schwarze 185: int rc;
1.1 schwarze 186:
187: rc = 0;
188:
189: if (cp <= 0x0000007F) {
190: rc = 1;
191: out[0] = (char)cp;
192: } else if (cp <= 0x000007FF) {
193: rc = 2;
194: out[0] = (cp >> 6 & 31) | 192;
195: out[1] = (cp & 63) | 128;
196: } else if (cp <= 0x0000FFFF) {
197: rc = 3;
198: out[0] = (cp >> 12 & 15) | 224;
199: out[1] = (cp >> 6 & 63) | 128;
200: out[2] = (cp & 63) | 128;
201: } else if (cp <= 0x001FFFFF) {
202: rc = 4;
203: out[0] = (cp >> 18 & 7) | 240;
204: out[1] = (cp >> 12 & 63) | 128;
205: out[2] = (cp >> 6 & 63) | 128;
206: out[3] = (cp & 63) | 128;
207: } else if (cp <= 0x03FFFFFF) {
208: rc = 5;
209: out[0] = (cp >> 24 & 3) | 248;
210: out[1] = (cp >> 18 & 63) | 128;
211: out[2] = (cp >> 12 & 63) | 128;
212: out[3] = (cp >> 6 & 63) | 128;
213: out[4] = (cp & 63) | 128;
214: } else if (cp <= 0x7FFFFFFF) {
215: rc = 6;
216: out[0] = (cp >> 30 & 1) | 252;
217: out[1] = (cp >> 24 & 63) | 128;
218: out[2] = (cp >> 18 & 63) | 128;
219: out[3] = (cp >> 12 & 63) | 128;
220: out[4] = (cp >> 6 & 63) | 128;
221: out[5] = (cp & 63) | 128;
222: } else
223: return(0);
224:
225: out[rc] = '\0';
1.15 schwarze 226: return((size_t)rc);
1.1 schwarze 227: }
228:
229: /*
230: * Normalise strings from the index and database.
231: * These strings are escaped as defined by mandoc_char(7) along with
232: * other goop in mandoc.h (e.g., soft hyphens).
233: * This function normalises these into a nice UTF-8 string.
234: * Returns 0 if the database is fucked.
235: */
236: static void
237: norm_string(const char *val, const struct mchars *mc, char **buf)
238: {
239: size_t sz, bsz;
240: char utfbuf[7];
241: const char *seq, *cpp;
242: int len, u, pos;
243: enum mandoc_esc esc;
1.8 schwarze 244: static const char res[] = { '\\', '\t',
1.1 schwarze 245: ASCII_NBRSP, ASCII_HYPH, '\0' };
246:
247: /* Pre-allocate by the length of the input */
248:
249: bsz = strlen(val) + 1;
250: *buf = mandoc_realloc(*buf, bsz);
251: pos = 0;
252:
253: while ('\0' != *val) {
254: /*
255: * Halt on the first escape sequence.
256: * This also halts on the end of string, in which case
257: * we just copy, fallthrough, and exit the loop.
258: */
259: if ((sz = strcspn(val, res)) > 0) {
260: memcpy(&(*buf)[pos], val, sz);
261: pos += (int)sz;
262: val += (int)sz;
263: }
264:
265: if (ASCII_HYPH == *val) {
266: (*buf)[pos++] = '-';
267: val++;
268: continue;
269: } else if ('\t' == *val || ASCII_NBRSP == *val) {
270: (*buf)[pos++] = ' ';
271: val++;
272: continue;
273: } else if ('\\' != *val)
274: break;
275:
276: /* Read past the slash. */
277:
278: val++;
279: u = 0;
280:
281: /*
282: * Parse the escape sequence and see if it's a
283: * predefined character or special character.
284: */
285:
286: esc = mandoc_escape(&val, &seq, &len);
287: if (ESCAPE_ERROR == esc)
288: break;
289:
1.8 schwarze 290: /*
1.1 schwarze 291: * XXX - this just does UTF-8, but we need to know
292: * beforehand whether we should do text substitution.
293: */
294:
295: switch (esc) {
296: case (ESCAPE_SPECIAL):
297: if (0 != (u = mchars_spec2cp(mc, seq, len)))
298: break;
299: /* FALLTHROUGH */
300: default:
301: continue;
302: }
303:
304: /*
305: * If we have a Unicode codepoint, try to convert that
306: * to a UTF-8 byte string.
307: */
308:
309: cpp = utfbuf;
310: if (0 == (sz = norm_utf8(u, utfbuf)))
311: continue;
312:
313: /* Copy the rendered glyph into the stream. */
314:
315: sz = strlen(cpp);
316: bsz += sz;
317:
318: *buf = mandoc_realloc(*buf, bsz);
319:
320: memcpy(&(*buf)[pos], cpp, sz);
321: pos += (int)sz;
322: }
323:
324: (*buf)[pos] = '\0';
325: }
326:
327: /*
328: * Open the filename-index mandoc-db database.
329: * Returns NULL if opening failed.
330: */
331: static DB *
332: index_open(void)
333: {
334: DB *db;
335:
1.2 schwarze 336: db = dbopen(MANDOC_IDX, O_RDONLY, 0, DB_RECNO, NULL);
1.1 schwarze 337: if (NULL != db)
338: return(db);
339:
340: return(NULL);
341: }
342:
343: /*
344: * Safely unpack from an index file record into the structure.
345: * Returns 1 if an entry was unpacked, 0 if the database is insane.
346: */
347: static int
1.8 schwarze 348: index_read(const DBT *key, const DBT *val, int index,
1.18 ! schwarze 349: const struct mchars *mc, struct res *rec)
1.1 schwarze 350: {
351: size_t left;
352: char *np, *cp;
1.15 schwarze 353: char type;
1.1 schwarze 354:
355: #define INDEX_BREAD(_dst) \
356: do { \
357: if (NULL == (np = memchr(cp, '\0', left))) \
358: return(0); \
359: norm_string(cp, mc, &(_dst)); \
360: left -= (np - cp) + 1; \
361: cp = np + 1; \
362: } while (/* CONSTCOND */ 0)
363:
1.15 schwarze 364: if (0 == (left = val->size))
365: return(0);
1.1 schwarze 366:
1.15 schwarze 367: cp = val->data;
1.16 schwarze 368: assert(sizeof(recno_t) == key->size);
1.18 ! schwarze 369: memcpy(&rec->rec, key->data, key->size);
! 370: rec->volume = index;
1.1 schwarze 371:
1.15 schwarze 372: if ('d' == (type = *cp++))
1.18 ! schwarze 373: rec->type = RESTYPE_MDOC;
1.15 schwarze 374: else if ('a' == type)
1.18 ! schwarze 375: rec->type = RESTYPE_MAN;
1.15 schwarze 376: else if ('c' == type)
1.18 ! schwarze 377: rec->type = RESTYPE_CAT;
1.15 schwarze 378: else
379: return(0);
380:
381: left--;
1.18 ! schwarze 382: INDEX_BREAD(rec->file);
! 383: INDEX_BREAD(rec->cat);
! 384: INDEX_BREAD(rec->title);
! 385: INDEX_BREAD(rec->arch);
! 386: INDEX_BREAD(rec->desc);
1.1 schwarze 387: return(1);
388: }
389:
390: /*
1.8 schwarze 391: * Search mandocdb databases in paths for expression "expr".
1.1 schwarze 392: * Filter out by "opts".
393: * Call "res" with the results, which may be zero.
1.7 schwarze 394: * Return 0 if there was a database error, else return 1.
1.1 schwarze 395: */
1.7 schwarze 396: int
1.8 schwarze 397: apropos_search(int pathsz, char **paths, const struct opts *opts,
398: const struct expr *expr, size_t terms, void *arg,
1.18 ! schwarze 399: size_t *sz, struct res **resp,
1.7 schwarze 400: void (*res)(struct res *, size_t, void *))
1.1 schwarze 401: {
1.5 schwarze 402: struct rectree tree;
403: struct mchars *mc;
1.18 ! schwarze 404: int i, rc;
1.5 schwarze 405:
406: memset(&tree, 0, sizeof(struct rectree));
407:
1.8 schwarze 408: rc = 0;
1.5 schwarze 409: mc = mchars_alloc();
1.18 ! schwarze 410: *sz = 0;
! 411: *resp = NULL;
1.5 schwarze 412:
1.8 schwarze 413: /*
414: * Main loop. Change into the directory containing manpage
415: * databases. Run our expession over each database in the set.
416: */
417:
418: for (i = 0; i < pathsz; i++) {
1.18 ! schwarze 419: assert('/' == paths[i][0]);
1.8 schwarze 420: if (chdir(paths[i]))
1.5 schwarze 421: continue;
1.18 ! schwarze 422: if (single_search(&tree, opts, expr, terms, mc, i))
! 423: continue;
! 424:
! 425: resfree(tree.node, tree.len);
! 426: mchars_free(mc);
! 427: return(0);
1.5 schwarze 428: }
429:
1.18 ! schwarze 430: (*res)(tree.node, tree.len, arg);
! 431: *sz = tree.len;
! 432: *resp = tree.node;
1.8 schwarze 433: mchars_free(mc);
1.18 ! schwarze 434: return(1);
1.5 schwarze 435: }
436:
1.7 schwarze 437: static int
1.5 schwarze 438: single_search(struct rectree *tree, const struct opts *opts,
439: const struct expr *expr, size_t terms,
1.8 schwarze 440: struct mchars *mc, int vol)
1.5 schwarze 441: {
1.8 schwarze 442: int root, leaf, ch;
1.1 schwarze 443: DBT key, val;
444: DB *btree, *idx;
445: char *buf;
1.18 ! schwarze 446: struct res *rs;
! 447: struct res r;
1.17 schwarze 448: uint64_t mask;
449: recno_t rec;
1.1 schwarze 450:
451: root = -1;
452: leaf = -1;
453: btree = NULL;
454: idx = NULL;
455: buf = NULL;
1.7 schwarze 456: rs = tree->node;
1.1 schwarze 457:
1.18 ! schwarze 458: memset(&r, 0, sizeof(struct res));
1.1 schwarze 459:
1.8 schwarze 460: if (NULL == (btree = btree_open()))
461: return(1);
462:
463: if (NULL == (idx = index_open())) {
464: (*btree->close)(btree);
465: return(1);
466: }
1.1 schwarze 467:
1.8 schwarze 468: while (0 == (ch = (*btree->seq)(btree, &key, &val, R_NEXT))) {
1.17 schwarze 469: if ( ! btree_read(&key, &val, mc, &mask, &rec, &buf))
1.8 schwarze 470: break;
471:
1.4 schwarze 472: /*
473: * See if this keyword record matches any of the
474: * expressions we have stored.
475: */
1.17 schwarze 476: if ( ! exprmark(expr, buf, mask, NULL))
1.1 schwarze 477: continue;
478:
479: /*
480: * O(log n) scan for prior records. Since a record
481: * number is unbounded, this has decent performance over
482: * a complex hash function.
483: */
484:
485: for (leaf = root; leaf >= 0; )
1.18 ! schwarze 486: if (rec > rs[leaf].rec &&
1.7 schwarze 487: rs[leaf].rhs >= 0)
488: leaf = rs[leaf].rhs;
1.18 ! schwarze 489: else if (rec < rs[leaf].rec &&
1.7 schwarze 490: rs[leaf].lhs >= 0)
491: leaf = rs[leaf].lhs;
1.8 schwarze 492: else
1.1 schwarze 493: break;
494:
1.7 schwarze 495: /*
496: * If we find a record, see if it has already evaluated
497: * to true. If it has, great, just keep going. If not,
498: * try to evaluate it now and continue anyway.
499: */
500:
1.18 ! schwarze 501: if (leaf >= 0 && rs[leaf].rec == rec) {
1.7 schwarze 502: if (0 == rs[leaf].matched)
1.17 schwarze 503: exprexec(expr, buf, mask, &rs[leaf]);
1.1 schwarze 504: continue;
1.4 schwarze 505: }
1.1 schwarze 506:
507: /*
1.7 schwarze 508: * We have a new file to examine.
509: * Extract the manpage's metadata from the index
510: * database, then begin partial evaluation.
1.1 schwarze 511: */
512:
1.17 schwarze 513: key.data = &rec;
1.1 schwarze 514: key.size = sizeof(recno_t);
515:
516: if (0 != (*idx->get)(idx, &key, &val, 0))
1.8 schwarze 517: break;
1.1 schwarze 518:
1.7 schwarze 519: r.lhs = r.rhs = -1;
1.8 schwarze 520: if ( ! index_read(&key, &val, vol, mc, &r))
521: break;
1.1 schwarze 522:
1.7 schwarze 523: /* XXX: this should be elsewhere, I guess? */
1.8 schwarze 524:
1.18 ! schwarze 525: if (opts->cat && strcasecmp(opts->cat, r.cat))
1.1 schwarze 526: continue;
1.14 schwarze 527:
1.18 ! schwarze 528: if (opts->arch && *r.arch)
! 529: if (strcasecmp(opts->arch, r.arch))
1.14 schwarze 530: continue;
1.1 schwarze 531:
1.7 schwarze 532: tree->node = rs = mandoc_realloc
1.18 ! schwarze 533: (rs, (tree->len + 1) * sizeof(struct res));
1.1 schwarze 534:
1.18 ! schwarze 535: memcpy(&rs[tree->len], &r, sizeof(struct res));
! 536: memset(&r, 0, sizeof(struct res));
1.8 schwarze 537: rs[tree->len].matches =
538: mandoc_calloc(terms, sizeof(int));
1.4 schwarze 539:
1.17 schwarze 540: exprexec(expr, buf, mask, &rs[tree->len]);
1.1 schwarze 541:
542: /* Append to our tree. */
543:
544: if (leaf >= 0) {
1.18 ! schwarze 545: if (rec > rs[leaf].rec)
1.7 schwarze 546: rs[leaf].rhs = tree->len;
1.1 schwarze 547: else
1.7 schwarze 548: rs[leaf].lhs = tree->len;
1.1 schwarze 549: } else
1.5 schwarze 550: root = tree->len;
1.8 schwarze 551:
1.5 schwarze 552: tree->len++;
1.1 schwarze 553: }
554:
1.8 schwarze 555: (*btree->close)(btree);
556: (*idx->close)(idx);
1.1 schwarze 557:
558: free(buf);
1.18 ! schwarze 559: RESFREE(&r);
1.8 schwarze 560: return(1 == ch);
1.1 schwarze 561: }
562:
1.18 ! schwarze 563: void
! 564: resfree(struct res *rec, size_t sz)
1.4 schwarze 565: {
1.18 ! schwarze 566: size_t i;
1.4 schwarze 567:
1.18 ! schwarze 568: for (i = 0; i < sz; i++)
! 569: RESFREE(&rec[i]);
! 570: free(rec);
1.4 schwarze 571: }
572:
1.10 schwarze 573: /*
574: * Compile a list of straight-up terms.
575: * The arguments are re-written into ~[[:<:]]term[[:>:]], or "term"
576: * surrounded by word boundaries, then pumped through exprterm().
577: * Terms are case-insensitive.
578: * This emulates whatis(1) behaviour.
579: */
580: struct expr *
581: termcomp(int argc, char *argv[], size_t *tt)
582: {
583: char *buf;
584: int pos;
585: struct expr *e, *next;
586: size_t sz;
587:
588: buf = NULL;
589: e = NULL;
590: *tt = 0;
591:
592: for (pos = argc - 1; pos >= 0; pos--) {
593: sz = strlen(argv[pos]) + 18;
594: buf = mandoc_realloc(buf, sz);
595: strlcpy(buf, "Nm~[[:<:]]", sz);
596: strlcat(buf, argv[pos], sz);
597: strlcat(buf, "[[:>:]]", sz);
598: if (NULL == (next = exprterm(buf, 0))) {
599: free(buf);
600: exprfree(e);
601: return(NULL);
602: }
603: next->next = e;
604: e = next;
605: (*tt)++;
606: }
607:
608: free(buf);
609: return(e);
610: }
611:
612: /*
613: * Compile a sequence of logical expressions.
614: * See apropos.1 for a grammar of this sequence.
615: */
1.1 schwarze 616: struct expr *
1.4 schwarze 617: exprcomp(int argc, char *argv[], size_t *tt)
618: {
1.7 schwarze 619: int pos, lvl;
620: struct expr *e;
621:
622: pos = lvl = 0;
623: *tt = 0;
624:
625: e = exprexpr(argc, argv, &pos, &lvl, tt);
626:
627: if (0 == lvl && pos >= argc)
628: return(e);
629:
630: exprfree(e);
631: return(NULL);
632: }
633:
634: /*
635: * Compile an array of tokens into an expression.
636: * An informal expression grammar is defined in apropos(1).
637: * Return NULL if we fail doing so. All memory will be cleaned up.
638: * Return the root of the expression sequence if alright.
639: */
640: static struct expr *
641: exprexpr(int argc, char *argv[], int *pos, int *lvl, size_t *tt)
642: {
1.4 schwarze 643: struct expr *e, *first, *next;
1.7 schwarze 644: int log;
1.4 schwarze 645:
646: first = next = NULL;
647:
1.7 schwarze 648: for ( ; *pos < argc; (*pos)++) {
1.4 schwarze 649: e = next;
1.7 schwarze 650:
651: /*
652: * Close out a subexpression.
653: */
654:
655: if (NULL != e && 0 == strcmp(")", argv[*pos])) {
656: if (--(*lvl) < 0)
657: goto err;
658: break;
659: }
660:
661: /*
662: * Small note: if we're just starting, don't let "-a"
663: * and "-o" be considered logical operators: they're
664: * just tokens unless pairwise joining, in which case we
665: * record their existence (or assume "OR").
666: */
1.4 schwarze 667: log = 0;
668:
1.7 schwarze 669: if (NULL != e && 0 == strcmp("-a", argv[*pos]))
1.8 schwarze 670: log = 1;
1.7 schwarze 671: else if (NULL != e && 0 == strcmp("-o", argv[*pos]))
1.4 schwarze 672: log = 2;
673:
1.7 schwarze 674: if (log > 0 && ++(*pos) >= argc)
1.4 schwarze 675: goto err;
676:
1.7 schwarze 677: /*
678: * Now we parse the term part. This can begin with
679: * "-i", in which case the expression is case
680: * insensitive.
681: */
682:
683: if (0 == strcmp("(", argv[*pos])) {
684: ++(*pos);
685: ++(*lvl);
686: next = mandoc_calloc(1, sizeof(struct expr));
687: next->subexpr = exprexpr(argc, argv, pos, lvl, tt);
688: if (NULL == next->subexpr) {
689: free(next);
690: next = NULL;
691: }
692: } else if (0 == strcmp("-i", argv[*pos])) {
693: if (++(*pos) >= argc)
1.4 schwarze 694: goto err;
1.7 schwarze 695: next = exprterm(argv[*pos], 0);
1.4 schwarze 696: } else
1.7 schwarze 697: next = exprterm(argv[*pos], 1);
1.4 schwarze 698:
699: if (NULL == next)
700: goto err;
701:
1.7 schwarze 702: next->and = log == 1;
1.4 schwarze 703: next->index = (int)(*tt)++;
704:
1.7 schwarze 705: /* Append to our chain of expressions. */
706:
1.4 schwarze 707: if (NULL == first) {
708: assert(NULL == e);
709: first = next;
710: } else {
711: assert(NULL != e);
712: e->next = next;
713: }
714: }
715:
716: return(first);
717: err:
718: exprfree(first);
719: return(NULL);
720: }
721:
1.7 schwarze 722: /*
723: * Parse a terminal expression with the grammar as defined in
724: * apropos(1).
725: * Return NULL if we fail the parse.
726: */
1.4 schwarze 727: static struct expr *
1.7 schwarze 728: exprterm(char *buf, int cs)
1.1 schwarze 729: {
1.4 schwarze 730: struct expr e;
1.1 schwarze 731: struct expr *p;
1.3 schwarze 732: char *key;
1.4 schwarze 733: int i;
1.1 schwarze 734:
1.4 schwarze 735: memset(&e, 0, sizeof(struct expr));
1.1 schwarze 736:
1.7 schwarze 737: /* Choose regex or substring match. */
1.3 schwarze 738:
1.4 schwarze 739: if (NULL == (e.v = strpbrk(buf, "=~"))) {
1.3 schwarze 740: e.regex = 0;
1.4 schwarze 741: e.v = buf;
1.3 schwarze 742: } else {
743: e.regex = '~' == *e.v;
744: *e.v++ = '\0';
745: }
1.1 schwarze 746:
1.7 schwarze 747: /* Determine the record types to search for. */
1.3 schwarze 748:
749: e.mask = 0;
1.4 schwarze 750: if (buf < e.v) {
751: while (NULL != (key = strsep(&buf, ","))) {
1.3 schwarze 752: i = 0;
753: while (types[i].mask &&
1.7 schwarze 754: strcmp(types[i].name, key))
1.3 schwarze 755: i++;
756: e.mask |= types[i].mask;
757: }
758: }
759: if (0 == e.mask)
760: e.mask = TYPE_Nm | TYPE_Nd;
1.1 schwarze 761:
1.4 schwarze 762: if (e.regex) {
1.10 schwarze 763: i = REG_EXTENDED | REG_NOSUB | (cs ? 0 : REG_ICASE);
1.4 schwarze 764: if (regcomp(&e.re, e.v, i))
765: return(NULL);
766: }
1.1 schwarze 767:
1.3 schwarze 768: e.v = mandoc_strdup(e.v);
1.1 schwarze 769:
770: p = mandoc_calloc(1, sizeof(struct expr));
771: memcpy(p, &e, sizeof(struct expr));
772: return(p);
773: }
774:
775: void
776: exprfree(struct expr *p)
777: {
1.4 schwarze 778: struct expr *pp;
1.8 schwarze 779:
1.4 schwarze 780: while (NULL != p) {
1.7 schwarze 781: if (p->subexpr)
782: exprfree(p->subexpr);
1.4 schwarze 783: if (p->regex)
784: regfree(&p->re);
785: free(p->v);
786: pp = p->next;
787: free(p);
788: p = pp;
789: }
790: }
1.1 schwarze 791:
1.4 schwarze 792: static int
1.8 schwarze 793: exprmark(const struct expr *p, const char *cp,
794: uint64_t mask, int *ms)
1.4 schwarze 795: {
1.1 schwarze 796:
1.7 schwarze 797: for ( ; p; p = p->next) {
798: if (p->subexpr) {
799: if (exprmark(p->subexpr, cp, mask, ms))
800: return(1);
801: continue;
802: } else if ( ! (mask & p->mask))
1.4 schwarze 803: continue;
1.7 schwarze 804:
1.4 schwarze 805: if (p->regex) {
1.7 schwarze 806: if (regexec(&p->re, cp, 0, NULL, 0))
807: continue;
1.11 schwarze 808: } else if (NULL == strcasestr(cp, p->v))
809: continue;
1.7 schwarze 810:
811: if (NULL == ms)
1.4 schwarze 812: return(1);
1.7 schwarze 813: else
814: ms[p->index] = 1;
1.4 schwarze 815: }
1.8 schwarze 816:
817: return(0);
1.7 schwarze 818: }
819:
820: static int
821: expreval(const struct expr *p, int *ms)
822: {
823: int match;
824:
825: /*
826: * AND has precedence over OR. Analysis is left-right, though
827: * it doesn't matter because there are no side-effects.
828: * Thus, step through pairwise ANDs and accumulate their Boolean
829: * evaluation. If we encounter a single true AND collection or
830: * standalone term, the whole expression is true (by definition
831: * of OR).
832: */
833:
834: for (match = 0; p && ! match; p = p->next) {
835: /* Evaluate a subexpression, if applicable. */
836: if (p->subexpr && ! ms[p->index])
837: ms[p->index] = expreval(p->subexpr, ms);
838:
839: match = ms[p->index];
840: for ( ; p->next && p->next->and; p = p->next) {
841: /* Evaluate a subexpression, if applicable. */
842: if (p->next->subexpr && ! ms[p->next->index])
843: ms[p->next->index] =
844: expreval(p->next->subexpr, ms);
845: match = match && ms[p->next->index];
846: }
847: }
848:
849: return(match);
1.1 schwarze 850: }
851:
1.4 schwarze 852: /*
853: * First, update the array of terms for which this expression evaluates
854: * to true.
855: * Second, logically evaluate all terms over the updated array of truth
856: * values.
857: * If this evaluates to true, mark the expression as satisfied.
858: */
859: static void
1.8 schwarze 860: exprexec(const struct expr *e, const char *cp,
1.18 ! schwarze 861: uint64_t mask, struct res *r)
1.1 schwarze 862: {
863:
1.7 schwarze 864: assert(0 == r->matched);
865: exprmark(e, cp, mask, r->matches);
866: r->matched = expreval(e, r->matches);
1.1 schwarze 867: }