Annotation of mandoc/mansearch.c, Revision 1.51
1.51 ! schwarze 1: /* $Id: mansearch.c,v 1.50 2014/11/18 01:15:21 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: #include "config.h"
19:
1.28 schwarze 20: #include <sys/mman.h>
1.43 schwarze 21: #include <sys/types.h>
22:
1.1 kristaps 23: #include <assert.h>
24: #include <fcntl.h>
25: #include <getopt.h>
1.6 schwarze 26: #include <limits.h>
1.8 schwarze 27: #include <regex.h>
1.1 kristaps 28: #include <stdio.h>
29: #include <stdint.h>
30: #include <stddef.h>
31: #include <stdlib.h>
32: #include <string.h>
33: #include <unistd.h>
34:
1.44 schwarze 35: #if HAVE_OHASH
1.1 kristaps 36: #include <ohash.h>
1.4 kristaps 37: #else
38: #include "compat_ohash.h"
39: #endif
1.1 kristaps 40: #include <sqlite3.h>
1.40 schwarze 41: #ifndef SQLITE_DETERMINISTIC
42: #define SQLITE_DETERMINISTIC 0
43: #endif
1.1 kristaps 44:
45: #include "mandoc.h"
1.23 schwarze 46: #include "mandoc_aux.h"
1.1 kristaps 47: #include "manpath.h"
48: #include "mansearch.h"
49:
1.20 schwarze 50: extern int mansearch_keymax;
51: extern const char *const mansearch_keynames[];
52:
1.3 kristaps 53: #define SQL_BIND_TEXT(_db, _s, _i, _v) \
1.8 schwarze 54: do { if (SQLITE_OK != sqlite3_bind_text \
1.2 kristaps 55: ((_s), (_i)++, (_v), -1, SQLITE_STATIC)) \
1.8 schwarze 56: fprintf(stderr, "%s\n", sqlite3_errmsg((_db))); \
57: } while (0)
1.3 kristaps 58: #define SQL_BIND_INT64(_db, _s, _i, _v) \
1.8 schwarze 59: do { if (SQLITE_OK != sqlite3_bind_int64 \
1.2 kristaps 60: ((_s), (_i)++, (_v))) \
1.8 schwarze 61: fprintf(stderr, "%s\n", sqlite3_errmsg((_db))); \
62: } while (0)
63: #define SQL_BIND_BLOB(_db, _s, _i, _v) \
64: do { if (SQLITE_OK != sqlite3_bind_blob \
65: ((_s), (_i)++, (&_v), sizeof(_v), SQLITE_STATIC)) \
66: fprintf(stderr, "%s\n", sqlite3_errmsg((_db))); \
67: } while (0)
1.2 kristaps 68:
1.1 kristaps 69: struct expr {
1.38 schwarze 70: regex_t regexp; /* compiled regexp, if applicable */
71: const char *substr; /* to search for, if applicable */
72: struct expr *next; /* next in sequence */
1.34 schwarze 73: uint64_t bits; /* type-mask */
1.38 schwarze 74: int equal; /* equality, not subsring match */
1.13 schwarze 75: int open; /* opening parentheses before */
76: int and; /* logical AND before */
77: int close; /* closing parentheses after */
1.1 kristaps 78: };
79:
80: struct match {
1.32 schwarze 81: uint64_t pageid; /* identifier in database */
1.50 schwarze 82: uint64_t bits; /* name type mask */
1.26 schwarze 83: char *desc; /* manual page description */
1.48 schwarze 84: int form; /* bit field: formatted, zipped? */
1.1 kristaps 85: };
86:
1.17 schwarze 87: static void buildnames(struct manpage *, sqlite3 *,
1.19 schwarze 88: sqlite3_stmt *, uint64_t,
89: const char *, int form);
1.12 schwarze 90: static char *buildoutput(sqlite3 *, sqlite3_stmt *,
91: uint64_t, uint64_t);
1.1 kristaps 92: static void *hash_alloc(size_t, void *);
1.37 schwarze 93: static void hash_free(void *, void *);
94: static void *hash_calloc(size_t, size_t, void *);
1.34 schwarze 95: static struct expr *exprcomp(const struct mansearch *,
1.5 kristaps 96: int, char *[]);
1.1 kristaps 97: static void exprfree(struct expr *);
1.15 schwarze 98: static struct expr *exprspec(struct expr *, uint64_t,
99: const char *, const char *);
1.8 schwarze 100: static struct expr *exprterm(const struct mansearch *, char *, int);
1.39 schwarze 101: static int manpage_compare(const void *, const void *);
1.13 schwarze 102: static void sql_append(char **sql, size_t *sz,
103: const char *newstr, int count);
1.7 schwarze 104: static void sql_match(sqlite3_context *context,
105: int argc, sqlite3_value **argv);
1.8 schwarze 106: static void sql_regexp(sqlite3_context *context,
107: int argc, sqlite3_value **argv);
1.15 schwarze 108: static char *sql_statement(const struct expr *);
1.28 schwarze 109:
1.34 schwarze 110:
1.28 schwarze 111: int
112: mansearch_setup(int start)
113: {
114: static void *pagecache;
115: int c;
116:
117: #define PC_PAGESIZE 1280
118: #define PC_NUMPAGES 256
119:
120: if (start) {
121: if (NULL != pagecache) {
122: fprintf(stderr, "pagecache already enabled\n");
123: return((int)MANDOCLEVEL_BADARG);
124: }
125:
126: pagecache = mmap(NULL, PC_PAGESIZE * PC_NUMPAGES,
1.42 schwarze 127: PROT_READ | PROT_WRITE,
128: MAP_SHARED | MAP_ANON, -1, 0);
1.28 schwarze 129:
130: if (MAP_FAILED == pagecache) {
131: perror("mmap");
132: pagecache = NULL;
133: return((int)MANDOCLEVEL_SYSERR);
134: }
135:
136: c = sqlite3_config(SQLITE_CONFIG_PAGECACHE,
137: pagecache, PC_PAGESIZE, PC_NUMPAGES);
138:
139: if (SQLITE_OK == c)
140: return((int)MANDOCLEVEL_OK);
141:
142: fprintf(stderr, "pagecache: %s\n", sqlite3_errstr(c));
143:
144: } else if (NULL == pagecache) {
145: fprintf(stderr, "pagecache missing\n");
146: return((int)MANDOCLEVEL_BADARG);
147: }
148:
149: if (-1 == munmap(pagecache, PC_PAGESIZE * PC_NUMPAGES)) {
150: perror("munmap");
151: pagecache = NULL;
152: return((int)MANDOCLEVEL_SYSERR);
153: }
154:
155: pagecache = NULL;
156: return((int)MANDOCLEVEL_OK);
157: }
1.1 kristaps 158:
159: int
1.5 kristaps 160: mansearch(const struct mansearch *search,
1.12 schwarze 161: const struct manpaths *paths,
162: int argc, char *argv[],
1.1 kristaps 163: struct manpage **res, size_t *sz)
164: {
1.20 schwarze 165: int fd, rc, c, indexbit;
1.32 schwarze 166: int64_t pageid;
1.20 schwarze 167: uint64_t outbit, iterbit;
1.6 schwarze 168: char buf[PATH_MAX];
1.11 schwarze 169: char *sql;
1.10 schwarze 170: struct manpage *mpage;
1.1 kristaps 171: struct expr *e, *ep;
172: sqlite3 *db;
1.12 schwarze 173: sqlite3_stmt *s, *s2;
1.1 kristaps 174: struct match *mp;
175: struct ohash_info info;
176: struct ohash htab;
177: unsigned int idx;
178: size_t i, j, cur, maxres;
179:
1.37 schwarze 180: info.calloc = hash_calloc;
1.1 kristaps 181: info.alloc = hash_alloc;
1.37 schwarze 182: info.free = hash_free;
1.32 schwarze 183: info.key_offset = offsetof(struct match, pageid);
1.1 kristaps 184:
1.2 kristaps 185: *sz = cur = maxres = 0;
1.1 kristaps 186: sql = NULL;
187: *res = NULL;
188: fd = -1;
189: e = NULL;
1.2 kristaps 190: rc = 0;
1.1 kristaps 191:
192: if (0 == argc)
193: goto out;
1.5 kristaps 194: if (NULL == (e = exprcomp(search, argc, argv)))
1.1 kristaps 195: goto out;
196:
1.12 schwarze 197: outbit = 0;
1.45 schwarze 198: if (NULL != search->outkey) {
1.20 schwarze 199: for (indexbit = 0, iterbit = 1;
200: indexbit < mansearch_keymax;
201: indexbit++, iterbit <<= 1) {
1.45 schwarze 202: if (0 == strcasecmp(search->outkey,
1.20 schwarze 203: mansearch_keynames[indexbit])) {
204: outbit = iterbit;
1.12 schwarze 205: break;
206: }
207: }
208: }
209:
1.1 kristaps 210: /*
211: * Save a descriptor to the current working directory.
212: * Since pathnames in the "paths" variable might be relative,
213: * and we'll be chdir()ing into them, we need to keep a handle
214: * on our current directory from which to start the chdir().
215: */
216:
1.6 schwarze 217: if (NULL == getcwd(buf, PATH_MAX)) {
1.30 schwarze 218: perror("getcwd");
1.1 kristaps 219: goto out;
220: } else if (-1 == (fd = open(buf, O_RDONLY, 0))) {
221: perror(buf);
222: goto out;
223: }
224:
1.15 schwarze 225: sql = sql_statement(e);
1.1 kristaps 226:
227: /*
228: * Loop over the directories (containing databases) for us to
229: * search.
230: * Don't let missing/bad databases/directories phase us.
231: * In each, try to open the resident database and, if it opens,
232: * scan it for our match expression.
233: */
234:
235: for (i = 0; i < paths->sz; i++) {
236: if (-1 == fchdir(fd)) {
237: perror(buf);
238: free(*res);
239: break;
240: } else if (-1 == chdir(paths->paths[i])) {
241: perror(paths->paths[i]);
242: continue;
1.34 schwarze 243: }
1.1 kristaps 244:
1.34 schwarze 245: c = sqlite3_open_v2(MANDOC_DB, &db,
246: SQLITE_OPEN_READONLY, NULL);
1.1 kristaps 247:
1.2 kristaps 248: if (SQLITE_OK != c) {
1.1 kristaps 249: perror(MANDOC_DB);
250: sqlite3_close(db);
251: continue;
252: }
253:
1.8 schwarze 254: /*
255: * Define the SQL functions for substring
256: * and regular expression matching.
257: */
1.7 schwarze 258:
259: c = sqlite3_create_function(db, "match", 2,
1.31 schwarze 260: SQLITE_UTF8 | SQLITE_DETERMINISTIC,
261: NULL, sql_match, NULL, NULL);
1.8 schwarze 262: assert(SQLITE_OK == c);
263: c = sqlite3_create_function(db, "regexp", 2,
1.31 schwarze 264: SQLITE_UTF8 | SQLITE_DETERMINISTIC,
265: NULL, sql_regexp, NULL, NULL);
1.8 schwarze 266: assert(SQLITE_OK == c);
1.7 schwarze 267:
1.1 kristaps 268: j = 1;
1.2 kristaps 269: c = sqlite3_prepare_v2(db, sql, -1, &s, NULL);
270: if (SQLITE_OK != c)
271: fprintf(stderr, "%s\n", sqlite3_errmsg(db));
1.1 kristaps 272:
273: for (ep = e; NULL != ep; ep = ep->next) {
1.8 schwarze 274: if (NULL == ep->substr) {
275: SQL_BIND_BLOB(db, s, j, ep->regexp);
276: } else
277: SQL_BIND_TEXT(db, s, j, ep->substr);
1.27 schwarze 278: if (0 == ((TYPE_Nd | TYPE_Nm) & ep->bits))
1.26 schwarze 279: SQL_BIND_INT64(db, s, j, ep->bits);
1.1 kristaps 280: }
281:
282: memset(&htab, 0, sizeof(struct ohash));
283: ohash_init(&htab, 4, &info);
284:
285: /*
286: * Hash each entry on its [unique] document identifier.
287: * This is a uint64_t.
288: * Instead of using a hash function, simply convert the
289: * uint64_t to a uint32_t, the hash value's type.
290: * This gives good performance and preserves the
291: * distribution of buckets in the table.
292: */
1.2 kristaps 293: while (SQLITE_ROW == (c = sqlite3_step(s))) {
1.32 schwarze 294: pageid = sqlite3_column_int64(s, 2);
1.34 schwarze 295: idx = ohash_lookup_memory(&htab,
296: (char *)&pageid, sizeof(uint64_t),
297: (uint32_t)pageid);
1.1 kristaps 298:
299: if (NULL != ohash_find(&htab, idx))
300: continue;
301:
302: mp = mandoc_calloc(1, sizeof(struct match));
1.32 schwarze 303: mp->pageid = pageid;
1.26 schwarze 304: mp->form = sqlite3_column_int(s, 1);
1.50 schwarze 305: mp->bits = sqlite3_column_int64(s, 3);
1.26 schwarze 306: if (TYPE_Nd == outbit)
1.41 schwarze 307: mp->desc = mandoc_strdup((const char *)
1.26 schwarze 308: sqlite3_column_text(s, 0));
1.1 kristaps 309: ohash_insert(&htab, idx, mp);
310: }
311:
1.2 kristaps 312: if (SQLITE_DONE != c)
313: fprintf(stderr, "%s\n", sqlite3_errmsg(db));
314:
1.1 kristaps 315: sqlite3_finalize(s);
1.10 schwarze 316:
1.34 schwarze 317: c = sqlite3_prepare_v2(db,
1.35 schwarze 318: "SELECT sec, arch, name, pageid FROM mlinks "
319: "WHERE pageid=? ORDER BY sec, arch, name",
1.10 schwarze 320: -1, &s, NULL);
321: if (SQLITE_OK != c)
322: fprintf(stderr, "%s\n", sqlite3_errmsg(db));
1.1 kristaps 323:
1.12 schwarze 324: c = sqlite3_prepare_v2(db,
1.35 schwarze 325: "SELECT bits, key, pageid FROM keys "
326: "WHERE pageid=? AND bits & ?",
1.12 schwarze 327: -1, &s2, NULL);
328: if (SQLITE_OK != c)
329: fprintf(stderr, "%s\n", sqlite3_errmsg(db));
330:
1.1 kristaps 331: for (mp = ohash_first(&htab, &idx);
332: NULL != mp;
333: mp = ohash_next(&htab, &idx)) {
334: if (cur + 1 > maxres) {
335: maxres += 1024;
1.36 schwarze 336: *res = mandoc_reallocarray(*res,
337: maxres, sizeof(struct manpage));
1.1 kristaps 338: }
1.10 schwarze 339: mpage = *res + cur;
1.47 schwarze 340: mpage->ipath = i;
1.50 schwarze 341: mpage->bits = mp->bits;
1.39 schwarze 342: mpage->sec = 10;
1.10 schwarze 343: mpage->form = mp->form;
1.32 schwarze 344: buildnames(mpage, db, s, mp->pageid,
1.19 schwarze 345: paths->paths[i], mp->form);
1.26 schwarze 346: mpage->output = TYPE_Nd & outbit ?
347: mp->desc : outbit ?
1.32 schwarze 348: buildoutput(db, s2, mp->pageid, outbit) : NULL;
1.10 schwarze 349:
1.1 kristaps 350: free(mp);
351: cur++;
352: }
1.10 schwarze 353:
354: sqlite3_finalize(s);
1.12 schwarze 355: sqlite3_finalize(s2);
1.10 schwarze 356: sqlite3_close(db);
1.1 kristaps 357: ohash_delete(&htab);
1.49 schwarze 358:
359: /*
360: * In man(1) mode, prefer matches in earlier trees
361: * over matches in later trees.
362: */
363:
364: if (cur && search->firstmatch)
365: break;
1.1 kristaps 366: }
1.39 schwarze 367: qsort(*res, cur, sizeof(struct manpage), manpage_compare);
1.2 kristaps 368: rc = 1;
1.1 kristaps 369: out:
1.30 schwarze 370: if (-1 != fd) {
371: if (-1 == fchdir(fd))
372: perror(buf);
373: close(fd);
374: }
1.1 kristaps 375: exprfree(e);
376: free(sql);
377: *sz = cur;
1.2 kristaps 378: return(rc);
1.11 schwarze 379: }
380:
1.45 schwarze 381: void
382: mansearch_free(struct manpage *res, size_t sz)
383: {
384: size_t i;
385:
386: for (i = 0; i < sz; i++) {
387: free(res[i].file);
388: free(res[i].names);
389: free(res[i].output);
390: }
391: free(res);
392: }
393:
1.39 schwarze 394: static int
395: manpage_compare(const void *vp1, const void *vp2)
396: {
397: const struct manpage *mp1, *mp2;
398: int diff;
399:
400: mp1 = vp1;
401: mp2 = vp2;
1.50 schwarze 402: return( (diff = mp2->bits - mp1->bits) ? diff :
403: (diff = mp1->sec - mp2->sec) ? diff :
404: strcasecmp(mp1->names, mp2->names));
1.39 schwarze 405: }
406:
1.17 schwarze 407: static void
408: buildnames(struct manpage *mpage, sqlite3 *db, sqlite3_stmt *s,
1.32 schwarze 409: uint64_t pageid, const char *path, int form)
1.11 schwarze 410: {
1.22 schwarze 411: char *newnames, *prevsec, *prevarch;
1.19 schwarze 412: const char *oldnames, *sep1, *name, *sec, *sep2, *arch, *fsec;
1.11 schwarze 413: size_t i;
414: int c;
415:
1.25 schwarze 416: mpage->file = NULL;
1.17 schwarze 417: mpage->names = NULL;
1.22 schwarze 418: prevsec = prevarch = NULL;
1.11 schwarze 419: i = 1;
1.32 schwarze 420: SQL_BIND_INT64(db, s, i, pageid);
1.11 schwarze 421: while (SQLITE_ROW == (c = sqlite3_step(s))) {
1.17 schwarze 422:
1.22 schwarze 423: /* Decide whether we already have some names. */
1.17 schwarze 424:
425: if (NULL == mpage->names) {
1.11 schwarze 426: oldnames = "";
427: sep1 = "";
428: } else {
1.17 schwarze 429: oldnames = mpage->names;
1.11 schwarze 430: sep1 = ", ";
431: }
1.22 schwarze 432:
433: /* Fetch the next name. */
434:
1.41 schwarze 435: sec = (const char *)sqlite3_column_text(s, 0);
436: arch = (const char *)sqlite3_column_text(s, 1);
437: name = (const char *)sqlite3_column_text(s, 2);
1.39 schwarze 438:
439: /* Remember the first section found. */
440:
441: if (9 < mpage->sec && '1' <= *sec && '9' >= *sec)
442: mpage->sec = (*sec - '1') + 1;
1.22 schwarze 443:
444: /* If the section changed, append the old one. */
445:
446: if (NULL != prevsec &&
447: (strcmp(sec, prevsec) ||
448: strcmp(arch, prevarch))) {
449: sep2 = '\0' == *prevarch ? "" : "/";
1.24 schwarze 450: mandoc_asprintf(&newnames, "%s(%s%s%s)",
451: oldnames, prevsec, sep2, prevarch);
1.22 schwarze 452: free(mpage->names);
453: oldnames = mpage->names = newnames;
454: free(prevsec);
455: free(prevarch);
456: prevsec = prevarch = NULL;
457: }
458:
459: /* Save the new section, to append it later. */
460:
461: if (NULL == prevsec) {
462: prevsec = mandoc_strdup(sec);
463: prevarch = mandoc_strdup(arch);
464: }
465:
466: /* Append the new name. */
467:
1.24 schwarze 468: mandoc_asprintf(&newnames, "%s%s%s",
469: oldnames, sep1, name);
1.17 schwarze 470: free(mpage->names);
471: mpage->names = newnames;
472:
473: /* Also save the first file name encountered. */
474:
1.51 ! schwarze 475: if (mpage->file != NULL)
1.17 schwarze 476: continue;
477:
1.48 schwarze 478: if (form & FORM_SRC) {
1.19 schwarze 479: sep1 = "man";
480: fsec = sec;
481: } else {
482: sep1 = "cat";
483: fsec = "0";
484: }
1.51 ! schwarze 485: sep2 = *arch == '\0' ? "" : "/";
! 486: mandoc_asprintf(&mpage->file, "%s/%s%s%s%s/%s.%s",
! 487: path, sep1, sec, sep2, arch, name, fsec);
1.11 schwarze 488: }
1.51 ! schwarze 489: if (c != SQLITE_DONE)
1.11 schwarze 490: fprintf(stderr, "%s\n", sqlite3_errmsg(db));
491: sqlite3_reset(s);
1.22 schwarze 492:
493: /* Append one final section to the names. */
494:
1.51 ! schwarze 495: if (prevsec != NULL) {
! 496: sep2 = *prevarch == '\0' ? "" : "/";
1.24 schwarze 497: mandoc_asprintf(&newnames, "%s(%s%s%s)",
498: mpage->names, prevsec, sep2, prevarch);
1.22 schwarze 499: free(mpage->names);
500: mpage->names = newnames;
501: free(prevsec);
502: free(prevarch);
503: }
1.12 schwarze 504: }
505:
506: static char *
1.32 schwarze 507: buildoutput(sqlite3 *db, sqlite3_stmt *s, uint64_t pageid, uint64_t outbit)
1.12 schwarze 508: {
509: char *output, *newoutput;
510: const char *oldoutput, *sep1, *data;
511: size_t i;
512: int c;
513:
514: output = NULL;
515: i = 1;
1.32 schwarze 516: SQL_BIND_INT64(db, s, i, pageid);
1.12 schwarze 517: SQL_BIND_INT64(db, s, i, outbit);
518: while (SQLITE_ROW == (c = sqlite3_step(s))) {
519: if (NULL == output) {
520: oldoutput = "";
521: sep1 = "";
522: } else {
523: oldoutput = output;
524: sep1 = " # ";
525: }
1.41 schwarze 526: data = (const char *)sqlite3_column_text(s, 1);
1.24 schwarze 527: mandoc_asprintf(&newoutput, "%s%s%s",
528: oldoutput, sep1, data);
1.12 schwarze 529: free(output);
530: output = newoutput;
531: }
532: if (SQLITE_DONE != c)
533: fprintf(stderr, "%s\n", sqlite3_errmsg(db));
534: sqlite3_reset(s);
535: return(output);
1.1 kristaps 536: }
537:
538: /*
1.7 schwarze 539: * Implement substring match as an application-defined SQL function.
540: * Using the SQL LIKE or GLOB operators instead would be a bad idea
541: * because that would require escaping metacharacters in the string
542: * being searched for.
543: */
544: static void
545: sql_match(sqlite3_context *context, int argc, sqlite3_value **argv)
546: {
547:
548: assert(2 == argc);
549: sqlite3_result_int(context, NULL != strcasestr(
550: (const char *)sqlite3_value_text(argv[1]),
551: (const char *)sqlite3_value_text(argv[0])));
552: }
553:
554: /*
1.8 schwarze 555: * Implement regular expression match
556: * as an application-defined SQL function.
557: */
558: static void
559: sql_regexp(sqlite3_context *context, int argc, sqlite3_value **argv)
560: {
561:
562: assert(2 == argc);
563: sqlite3_result_int(context, !regexec(
564: (regex_t *)sqlite3_value_blob(argv[0]),
565: (const char *)sqlite3_value_text(argv[1]),
566: 0, NULL, 0));
567: }
568:
1.13 schwarze 569: static void
570: sql_append(char **sql, size_t *sz, const char *newstr, int count)
571: {
572: size_t newsz;
573:
574: newsz = 1 < count ? (size_t)count : strlen(newstr);
575: *sql = mandoc_realloc(*sql, *sz + newsz + 1);
576: if (1 < count)
577: memset(*sql + *sz, *newstr, (size_t)count);
578: else
579: memcpy(*sql + *sz, newstr, newsz);
580: *sz += newsz;
581: (*sql)[*sz] = '\0';
582: }
583:
1.8 schwarze 584: /*
1.1 kristaps 585: * Prepare the search SQL statement.
586: */
587: static char *
1.15 schwarze 588: sql_statement(const struct expr *e)
1.1 kristaps 589: {
590: char *sql;
591: size_t sz;
1.13 schwarze 592: int needop;
1.1 kristaps 593:
1.50 schwarze 594: sql = mandoc_strdup(e->equal ?
595: "SELECT desc, form, pageid, bits "
596: "FROM mpages NATURAL JOIN names WHERE " :
597: "SELECT desc, form, pageid, 0 FROM mpages WHERE ");
1.1 kristaps 598: sz = strlen(sql);
1.2 kristaps 599:
1.13 schwarze 600: for (needop = 0; NULL != e; e = e->next) {
601: if (e->and)
602: sql_append(&sql, &sz, " AND ", 1);
603: else if (needop)
604: sql_append(&sql, &sz, " OR ", 1);
605: if (e->open)
606: sql_append(&sql, &sz, "(", e->open);
1.26 schwarze 607: sql_append(&sql, &sz,
608: TYPE_Nd & e->bits
609: ? (NULL == e->substr
610: ? "desc REGEXP ?"
611: : "desc MATCH ?")
1.27 schwarze 612: : TYPE_Nm == e->bits
613: ? (NULL == e->substr
1.32 schwarze 614: ? "pageid IN (SELECT pageid FROM names "
1.27 schwarze 615: "WHERE name REGEXP ?)"
1.38 schwarze 616: : e->equal
1.50 schwarze 617: ? "name = ? "
1.32 schwarze 618: : "pageid IN (SELECT pageid FROM names "
1.27 schwarze 619: "WHERE name MATCH ?)")
1.26 schwarze 620: : (NULL == e->substr
1.32 schwarze 621: ? "pageid IN (SELECT pageid FROM keys "
1.26 schwarze 622: "WHERE key REGEXP ? AND bits & ?)"
1.32 schwarze 623: : "pageid IN (SELECT pageid FROM keys "
1.26 schwarze 624: "WHERE key MATCH ? AND bits & ?)"), 1);
1.13 schwarze 625: if (e->close)
626: sql_append(&sql, &sz, ")", e->close);
627: needop = 1;
1.1 kristaps 628: }
629:
630: return(sql);
631: }
632:
633: /*
634: * Compile a set of string tokens into an expression.
635: * Tokens in "argv" are assumed to be individual expression atoms (e.g.,
636: * "(", "foo=bar", etc.).
637: */
638: static struct expr *
1.5 kristaps 639: exprcomp(const struct mansearch *search, int argc, char *argv[])
1.1 kristaps 640: {
1.27 schwarze 641: uint64_t mask;
1.13 schwarze 642: int i, toopen, logic, igncase, toclose;
1.27 schwarze 643: struct expr *first, *prev, *cur, *next;
1.1 kristaps 644:
645: first = cur = NULL;
1.15 schwarze 646: logic = igncase = toclose = 0;
1.33 schwarze 647: toopen = NULL != search->sec || NULL != search->arch;
1.1 kristaps 648:
649: for (i = 0; i < argc; i++) {
1.13 schwarze 650: if (0 == strcmp("(", argv[i])) {
651: if (igncase)
652: goto fail;
653: toopen++;
654: toclose++;
655: continue;
656: } else if (0 == strcmp(")", argv[i])) {
657: if (toopen || logic || igncase || NULL == cur)
658: goto fail;
659: cur->close++;
660: if (0 > --toclose)
661: goto fail;
662: continue;
663: } else if (0 == strcmp("-a", argv[i])) {
664: if (toopen || logic || igncase || NULL == cur)
665: goto fail;
666: logic = 1;
667: continue;
668: } else if (0 == strcmp("-o", argv[i])) {
669: if (toopen || logic || igncase || NULL == cur)
670: goto fail;
671: logic = 2;
672: continue;
673: } else if (0 == strcmp("-i", argv[i])) {
674: if (igncase)
675: goto fail;
676: igncase = 1;
677: continue;
1.1 kristaps 678: }
1.13 schwarze 679: next = exprterm(search, argv[i], !igncase);
680: if (NULL == next)
681: goto fail;
1.26 schwarze 682: if (NULL == first)
683: first = next;
684: else
1.1 kristaps 685: cur->next = next;
1.27 schwarze 686: prev = cur = next;
1.26 schwarze 687:
688: /*
689: * Searching for descriptions must be split out
690: * because they are stored in the mpages table,
691: * not in the keys table.
692: */
693:
1.27 schwarze 694: for (mask = TYPE_Nm; mask <= TYPE_Nd; mask <<= 1) {
695: if (mask & cur->bits && ~mask & cur->bits) {
696: next = mandoc_calloc(1,
697: sizeof(struct expr));
698: memcpy(next, cur, sizeof(struct expr));
699: prev->open = 1;
700: cur->bits = mask;
701: cur->next = next;
702: cur = next;
703: cur->bits &= ~mask;
704: }
705: }
706: prev->and = (1 == logic);
707: prev->open += toopen;
708: if (cur != prev)
1.26 schwarze 709: cur->close = 1;
1.27 schwarze 710:
1.13 schwarze 711: toopen = logic = igncase = 0;
1.1 kristaps 712: }
1.15 schwarze 713: if (toopen || logic || igncase || toclose)
714: goto fail;
715:
1.33 schwarze 716: if (NULL != search->sec || NULL != search->arch)
717: cur->close++;
718: if (NULL != search->arch)
719: cur = exprspec(cur, TYPE_arch, search->arch, "^(%s|any)$");
720: if (NULL != search->sec)
721: exprspec(cur, TYPE_sec, search->sec, "^%s$");
1.15 schwarze 722:
723: return(first);
724:
1.13 schwarze 725: fail:
726: if (NULL != first)
727: exprfree(first);
728: return(NULL);
1.1 kristaps 729: }
730:
731: static struct expr *
1.15 schwarze 732: exprspec(struct expr *cur, uint64_t key, const char *value,
733: const char *format)
734: {
735: char errbuf[BUFSIZ];
736: char *cp;
737: int irc;
738:
1.24 schwarze 739: mandoc_asprintf(&cp, format, value);
1.15 schwarze 740: cur->next = mandoc_calloc(1, sizeof(struct expr));
741: cur = cur->next;
742: cur->and = 1;
743: cur->bits = key;
744: if (0 != (irc = regcomp(&cur->regexp, cp,
745: REG_EXTENDED | REG_NOSUB | REG_ICASE))) {
746: regerror(irc, &cur->regexp, errbuf, sizeof(errbuf));
747: fprintf(stderr, "regcomp: %s\n", errbuf);
748: cur->substr = value;
749: }
750: free(cp);
751: return(cur);
752: }
753:
754: static struct expr *
1.8 schwarze 755: exprterm(const struct mansearch *search, char *buf, int cs)
1.1 kristaps 756: {
1.15 schwarze 757: char errbuf[BUFSIZ];
1.1 kristaps 758: struct expr *e;
1.38 schwarze 759: char *key, *val;
1.20 schwarze 760: uint64_t iterbit;
761: int i, irc;
1.1 kristaps 762:
763: if ('\0' == *buf)
764: return(NULL);
765:
766: e = mandoc_calloc(1, sizeof(struct expr));
767:
1.45 schwarze 768: if (search->argmode == ARG_NAME) {
769: e->bits = TYPE_Nm;
1.8 schwarze 770: e->substr = buf;
1.38 schwarze 771: e->equal = 1;
1.5 kristaps 772: return(e);
773: }
774:
1.1 kristaps 775: /*
1.45 schwarze 776: * Separate macro keys from search string.
777: * If needed, request regular expression handling
778: * by setting e->substr to NULL.
1.1 kristaps 779: */
780:
1.45 schwarze 781: if (search->argmode == ARG_WORD) {
782: e->bits = TYPE_Nm;
783: e->substr = NULL;
784: mandoc_asprintf(&val, "[[:<:]]%s[[:>:]]", buf);
1.46 schwarze 785: cs = 0;
1.45 schwarze 786: } else if ((val = strpbrk(buf, "=~")) == NULL) {
787: e->bits = TYPE_Nm | TYPE_Nd;
1.8 schwarze 788: e->substr = buf;
1.38 schwarze 789: } else {
790: if (val == buf)
1.45 schwarze 791: e->bits = TYPE_Nm | TYPE_Nd;
1.38 schwarze 792: if ('=' == *val)
793: e->substr = val + 1;
794: *val++ = '\0';
1.21 schwarze 795: if (NULL != strstr(buf, "arch"))
796: cs = 0;
1.38 schwarze 797: }
798:
799: /* Compile regular expressions. */
800:
801: if (NULL == e->substr) {
802: irc = regcomp(&e->regexp, val,
803: REG_EXTENDED | REG_NOSUB | (cs ? 0 : REG_ICASE));
1.45 schwarze 804: if (search->argmode == ARG_WORD)
1.38 schwarze 805: free(val);
806: if (irc) {
1.15 schwarze 807: regerror(irc, &e->regexp, errbuf, sizeof(errbuf));
808: fprintf(stderr, "regcomp: %s\n", errbuf);
1.8 schwarze 809: free(e);
810: return(NULL);
811: }
1.38 schwarze 812: }
813:
814: if (e->bits)
815: return(e);
1.1 kristaps 816:
817: /*
818: * Parse out all possible fields.
819: * If the field doesn't resolve, bail.
820: */
821:
822: while (NULL != (key = strsep(&buf, ","))) {
823: if ('\0' == *key)
824: continue;
1.20 schwarze 825: for (i = 0, iterbit = 1;
826: i < mansearch_keymax;
827: i++, iterbit <<= 1) {
828: if (0 == strcasecmp(key,
829: mansearch_keynames[i])) {
830: e->bits |= iterbit;
831: break;
832: }
833: }
834: if (i == mansearch_keymax) {
835: if (strcasecmp(key, "any")) {
836: free(e);
837: return(NULL);
838: }
839: e->bits |= ~0ULL;
1.1 kristaps 840: }
841: }
842:
843: return(e);
844: }
845:
846: static void
847: exprfree(struct expr *p)
848: {
849: struct expr *pp;
850:
851: while (NULL != p) {
852: pp = p->next;
853: free(p);
854: p = pp;
855: }
856: }
857:
858: static void *
1.37 schwarze 859: hash_calloc(size_t nmemb, size_t sz, void *arg)
1.1 kristaps 860: {
861:
1.37 schwarze 862: return(mandoc_calloc(nmemb, sz));
1.1 kristaps 863: }
864:
865: static void *
866: hash_alloc(size_t sz, void *arg)
867: {
868:
869: return(mandoc_malloc(sz));
870: }
871:
872: static void
1.37 schwarze 873: hash_free(void *p, void *arg)
1.1 kristaps 874: {
875:
876: free(p);
877: }
CVSweb