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