Annotation of mandoc/mandocdb.c, Revision 1.52
1.52 ! kristaps 1: /* $Id: mandocdb.c,v 1.51 2012/06/08 12:05:27 kristaps Exp $ */
1.1 kristaps 2: /*
1.48 schwarze 3: * Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
4: * Copyright (c) 2011, 2012 Ingo Schwarze <schwarze@openbsd.org>
1.1 kristaps 5: *
6: * Permission to use, copy, modify, and distribute this software for any
7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
9: *
10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17: */
18: #ifdef HAVE_CONFIG_H
19: #include "config.h"
20: #endif
21:
22: #include <sys/param.h>
1.50 kristaps 23: #include <sys/stat.h>
1.1 kristaps 24:
25: #include <assert.h>
1.43 kristaps 26: #include <ctype.h>
1.45 kristaps 27: #include <errno.h>
1.1 kristaps 28: #include <fcntl.h>
1.50 kristaps 29: #include <fts.h>
1.1 kristaps 30: #include <getopt.h>
1.50 kristaps 31: #include <stddef.h>
1.1 kristaps 32: #include <stdint.h>
33: #include <stdlib.h>
34: #include <string.h>
1.17 schwarze 35: #include <unistd.h>
1.1 kristaps 36:
1.50 kristaps 37: #include <ohash.h>
38: #include <sqlite3.h>
1.1 kristaps 39:
1.50 kristaps 40: #include "mdoc.h"
1.1 kristaps 41: #include "man.h"
42: #include "mandoc.h"
1.8 schwarze 43: #include "mandocdb.h"
1.10 kristaps 44: #include "manpath.h"
1.1 kristaps 45:
1.50 kristaps 46: /* Post a warning to stderr. */
1.45 kristaps 47: #define WARNING(_f, _b, _fmt, _args...) \
48: do if (warnings) { \
49: fprintf(stderr, "%s: ", (_b)); \
50: fprintf(stderr, (_fmt), ##_args); \
51: if ('\0' != *(_f)) \
52: fprintf(stderr, ": %s", (_f)); \
53: fprintf(stderr, "\n"); \
54: } while (/* CONSTCOND */ 0)
1.50 kristaps 55: /* Post a "verbose" message to stderr. */
56: #define DEBUG(_f, _b, _fmt, _args...) \
57: do if (verb) { \
58: fprintf(stderr, "%s: ", (_b)); \
59: fprintf(stderr, (_fmt), ##_args); \
60: fprintf(stderr, ": %s\n", (_f)); \
61: } while (/* CONSTCOND */ 0)
1.38 schwarze 62:
1.52 ! kristaps 63: #define SQL_EXEC(_v) \
! 64: if (SQLITE_OK != sqlite3_exec(db, (_v), NULL, NULL, NULL)) \
! 65: fprintf(stderr, "%s\n", sqlite3_errmsg(db))
! 66: #define SQL_BIND_TEXT(_s, _i, _v) \
! 67: if (SQLITE_OK != sqlite3_bind_text \
! 68: ((_s), (_i)++, (_v), -1, SQLITE_STATIC)) \
! 69: fprintf(stderr, "%s\n", sqlite3_errmsg(db))
! 70: #define SQL_BIND_INT(_s, _i, _v) \
! 71: if (SQLITE_OK != sqlite3_bind_int \
! 72: ((_s), (_i)++, (_v))) \
! 73: fprintf(stderr, "%s\n", sqlite3_errmsg(db))
! 74: #define SQL_BIND_INT64(_s, _i, _v) \
! 75: if (SQLITE_OK != sqlite3_bind_int64 \
! 76: ((_s), (_i)++, (_v))) \
! 77: fprintf(stderr, "%s\n", sqlite3_errmsg(db))
! 78: #define SQL_STEP(_s) \
! 79: if (SQLITE_DONE != sqlite3_step((_s))) \
! 80: fprintf(stderr, "%s\n", sqlite3_errmsg(db))
! 81:
1.50 kristaps 82: enum op {
83: OP_DEFAULT = 0, /* new dbs from dir list or default config */
84: OP_CONFFILE, /* new databases from custom config file */
85: OP_UPDATE, /* delete/add entries in existing database */
86: OP_DELETE, /* delete entries from existing database */
87: OP_TEST /* change no databases, report potential problems */
1.38 schwarze 88: };
89:
1.50 kristaps 90: enum form {
91: FORM_SRC, /* format is -man or -mdoc */
92: FORM_CAT, /* format is cat */
93: FORM_NONE /* format is unknown */
94: };
1.38 schwarze 95:
1.50 kristaps 96: struct str {
97: char *utf8; /* key in UTF-8 form */
98: const struct of *of; /* if set, the owning parse */
99: struct str *next; /* next in owning parse sequence */
100: uint64_t mask; /* bitmask in sequence */
1.51 kristaps 101: char key[]; /* the string itself */
1.38 schwarze 102: };
103:
1.50 kristaps 104: struct id {
105: ino_t ino;
106: dev_t dev;
107: };
1.5 kristaps 108:
1.3 kristaps 109: struct of {
1.50 kristaps 110: struct id id; /* used for hashing routine */
111: struct of *next; /* next in ofs */
112: enum form dform; /* path-cued form */
113: enum form sform; /* suffix-cued form */
114: char file[MAXPATHLEN]; /* filename rel. to manpath */
115: const char *desc; /* parsed description */
116: const char *sec; /* suffix-cued section (or empty) */
117: const char *dsec; /* path-cued section (or empty) */
118: const char *arch; /* path-cued arch. (or empty) */
119: const char *name; /* name (from filename) (not empty) */
1.3 kristaps 120: };
121:
1.50 kristaps 122: enum stmt {
123: STMT_DELETE = 0, /* delete manpage */
124: STMT_INSERT_DOC, /* insert manpage */
125: STMT_INSERT_KEY, /* insert parsed key */
126: STMT__MAX
1.1 kristaps 127: };
128:
1.50 kristaps 129: typedef int (*mdoc_fp)(struct of *, const struct mdoc_node *);
1.1 kristaps 130:
1.50 kristaps 131: struct mdoc_handler {
132: mdoc_fp fp; /* optional handler */
133: uint64_t mask; /* set unless handler returns 0 */
134: int flags; /* for use by pmdoc_node */
135: #define MDOCF_CHILD 0x01 /* automatically index child nodes */
1.1 kristaps 136: };
137:
1.50 kristaps 138: static void dbclose(const char *, int);
139: static void dbindex(struct mchars *, int,
140: const struct of *, const char *);
141: static int dbopen(const char *, int);
142: static void dbprune(const char *);
143: static void fileadd(struct of *);
144: static int filecheck(const char *);
145: static void filescan(const char *, const char *);
146: static struct str *hashget(const char *, size_t);
147: static void *hash_alloc(size_t, void *);
148: static void hash_free(void *, size_t, void *);
149: static void *hash_halloc(size_t, void *);
150: static void inoadd(const struct stat *, struct of *);
151: static int inocheck(const struct stat *);
152: static void ofadd(const char *, int, const char *,
153: const char *, const char *, const char *,
154: const char *, const struct stat *);
155: static void offree(void);
156: static int ofmerge(struct mchars *, struct mparse *, const char *);
157: static void parse_catpage(struct of *, const char *);
158: static int parse_man(struct of *,
159: const struct man_node *);
160: static void parse_mdoc(struct of *, const struct mdoc_node *);
161: static int parse_mdoc_body(struct of *, const struct mdoc_node *);
162: static int parse_mdoc_head(struct of *, const struct mdoc_node *);
163: static int parse_mdoc_Fd(struct of *, const struct mdoc_node *);
164: static int parse_mdoc_Fn(struct of *, const struct mdoc_node *);
165: static int parse_mdoc_In(struct of *, const struct mdoc_node *);
166: static int parse_mdoc_Nd(struct of *, const struct mdoc_node *);
167: static int parse_mdoc_Nm(struct of *, const struct mdoc_node *);
168: static int parse_mdoc_Sh(struct of *, const struct mdoc_node *);
169: static int parse_mdoc_St(struct of *, const struct mdoc_node *);
170: static int parse_mdoc_Xr(struct of *, const struct mdoc_node *);
171: static int path_reset(const char *, int, const char *);
172: static void putkey(const struct of *,
173: const char *, uint64_t);
174: static void putkeys(const struct of *,
175: const char *, int, uint64_t);
176: static void putmdockey(const struct of *,
177: const struct mdoc_node *, uint64_t);
178: static char *stradd(const char *);
179: static char *straddbuf(const char *, size_t);
180: static int treescan(const char *);
181: static size_t utf8(unsigned int, char [7]);
182: static void utf8key(struct mchars *, struct str *);
183: static void wordaddbuf(const struct of *,
184: const char *, size_t, uint64_t);
185:
186: static char *progname;
187: static int use_all; /* use all found files */
188: static int nodb; /* no database changes */
189: static int verb; /* print what we're doing */
190: static int warnings; /* warn about crap */
191: static enum op op; /* operational mode */
192: static struct ohash inos; /* table of inodes/devices */
193: static struct ohash filenames; /* table of filenames */
194: static struct ohash strings; /* table of all strings */
195: static struct of *ofs = NULL; /* vector of files to parse */
196: static struct str *words = NULL; /* word list in current parse */
197: static sqlite3 *db = NULL; /* current database */
198: static sqlite3_stmt *stmts[STMT__MAX]; /* current statements */
1.25 schwarze 199:
200: static const struct mdoc_handler mdocs[MDOC_MAX] = {
201: { NULL, 0, 0 }, /* Ap */
202: { NULL, 0, 0 }, /* Dd */
203: { NULL, 0, 0 }, /* Dt */
204: { NULL, 0, 0 }, /* Os */
1.50 kristaps 205: { parse_mdoc_Sh, TYPE_Sh, MDOCF_CHILD }, /* Sh */
206: { parse_mdoc_head, TYPE_Ss, MDOCF_CHILD }, /* Ss */
1.25 schwarze 207: { NULL, 0, 0 }, /* Pp */
208: { NULL, 0, 0 }, /* D1 */
209: { NULL, 0, 0 }, /* Dl */
210: { NULL, 0, 0 }, /* Bd */
211: { NULL, 0, 0 }, /* Ed */
212: { NULL, 0, 0 }, /* Bl */
213: { NULL, 0, 0 }, /* El */
214: { NULL, 0, 0 }, /* It */
215: { NULL, 0, 0 }, /* Ad */
216: { NULL, TYPE_An, MDOCF_CHILD }, /* An */
217: { NULL, TYPE_Ar, MDOCF_CHILD }, /* Ar */
218: { NULL, TYPE_Cd, MDOCF_CHILD }, /* Cd */
219: { NULL, TYPE_Cm, MDOCF_CHILD }, /* Cm */
220: { NULL, TYPE_Dv, MDOCF_CHILD }, /* Dv */
221: { NULL, TYPE_Er, MDOCF_CHILD }, /* Er */
222: { NULL, TYPE_Ev, MDOCF_CHILD }, /* Ev */
223: { NULL, 0, 0 }, /* Ex */
224: { NULL, TYPE_Fa, MDOCF_CHILD }, /* Fa */
1.50 kristaps 225: { parse_mdoc_Fd, TYPE_In, 0 }, /* Fd */
1.25 schwarze 226: { NULL, TYPE_Fl, MDOCF_CHILD }, /* Fl */
1.50 kristaps 227: { parse_mdoc_Fn, 0, 0 }, /* Fn */
1.25 schwarze 228: { NULL, TYPE_Ft, MDOCF_CHILD }, /* Ft */
229: { NULL, TYPE_Ic, MDOCF_CHILD }, /* Ic */
1.50 kristaps 230: { parse_mdoc_In, TYPE_In, MDOCF_CHILD }, /* In */
1.25 schwarze 231: { NULL, TYPE_Li, MDOCF_CHILD }, /* Li */
1.50 kristaps 232: { parse_mdoc_Nd, TYPE_Nd, MDOCF_CHILD }, /* Nd */
233: { parse_mdoc_Nm, TYPE_Nm, MDOCF_CHILD }, /* Nm */
1.25 schwarze 234: { NULL, 0, 0 }, /* Op */
235: { NULL, 0, 0 }, /* Ot */
236: { NULL, TYPE_Pa, MDOCF_CHILD }, /* Pa */
237: { NULL, 0, 0 }, /* Rv */
1.50 kristaps 238: { parse_mdoc_St, TYPE_St, 0 }, /* St */
1.25 schwarze 239: { NULL, TYPE_Va, MDOCF_CHILD }, /* Va */
1.50 kristaps 240: { parse_mdoc_body, TYPE_Va, MDOCF_CHILD }, /* Vt */
241: { parse_mdoc_Xr, TYPE_Xr, 0 }, /* Xr */
1.25 schwarze 242: { NULL, 0, 0 }, /* %A */
243: { NULL, 0, 0 }, /* %B */
244: { NULL, 0, 0 }, /* %D */
245: { NULL, 0, 0 }, /* %I */
246: { NULL, 0, 0 }, /* %J */
247: { NULL, 0, 0 }, /* %N */
248: { NULL, 0, 0 }, /* %O */
249: { NULL, 0, 0 }, /* %P */
250: { NULL, 0, 0 }, /* %R */
251: { NULL, 0, 0 }, /* %T */
252: { NULL, 0, 0 }, /* %V */
253: { NULL, 0, 0 }, /* Ac */
254: { NULL, 0, 0 }, /* Ao */
255: { NULL, 0, 0 }, /* Aq */
256: { NULL, TYPE_At, MDOCF_CHILD }, /* At */
257: { NULL, 0, 0 }, /* Bc */
258: { NULL, 0, 0 }, /* Bf */
259: { NULL, 0, 0 }, /* Bo */
260: { NULL, 0, 0 }, /* Bq */
261: { NULL, TYPE_Bsx, MDOCF_CHILD }, /* Bsx */
262: { NULL, TYPE_Bx, MDOCF_CHILD }, /* Bx */
263: { NULL, 0, 0 }, /* Db */
264: { NULL, 0, 0 }, /* Dc */
265: { NULL, 0, 0 }, /* Do */
266: { NULL, 0, 0 }, /* Dq */
267: { NULL, 0, 0 }, /* Ec */
268: { NULL, 0, 0 }, /* Ef */
269: { NULL, TYPE_Em, MDOCF_CHILD }, /* Em */
270: { NULL, 0, 0 }, /* Eo */
271: { NULL, TYPE_Fx, MDOCF_CHILD }, /* Fx */
272: { NULL, TYPE_Ms, MDOCF_CHILD }, /* Ms */
273: { NULL, 0, 0 }, /* No */
274: { NULL, 0, 0 }, /* Ns */
275: { NULL, TYPE_Nx, MDOCF_CHILD }, /* Nx */
276: { NULL, TYPE_Ox, MDOCF_CHILD }, /* Ox */
277: { NULL, 0, 0 }, /* Pc */
278: { NULL, 0, 0 }, /* Pf */
279: { NULL, 0, 0 }, /* Po */
280: { NULL, 0, 0 }, /* Pq */
281: { NULL, 0, 0 }, /* Qc */
282: { NULL, 0, 0 }, /* Ql */
283: { NULL, 0, 0 }, /* Qo */
284: { NULL, 0, 0 }, /* Qq */
285: { NULL, 0, 0 }, /* Re */
286: { NULL, 0, 0 }, /* Rs */
287: { NULL, 0, 0 }, /* Sc */
288: { NULL, 0, 0 }, /* So */
289: { NULL, 0, 0 }, /* Sq */
290: { NULL, 0, 0 }, /* Sm */
291: { NULL, 0, 0 }, /* Sx */
292: { NULL, TYPE_Sy, MDOCF_CHILD }, /* Sy */
293: { NULL, TYPE_Tn, MDOCF_CHILD }, /* Tn */
294: { NULL, 0, 0 }, /* Ux */
295: { NULL, 0, 0 }, /* Xc */
296: { NULL, 0, 0 }, /* Xo */
1.50 kristaps 297: { parse_mdoc_head, TYPE_Fn, 0 }, /* Fo */
1.25 schwarze 298: { NULL, 0, 0 }, /* Fc */
299: { NULL, 0, 0 }, /* Oo */
300: { NULL, 0, 0 }, /* Oc */
301: { NULL, 0, 0 }, /* Bk */
302: { NULL, 0, 0 }, /* Ek */
303: { NULL, 0, 0 }, /* Bt */
304: { NULL, 0, 0 }, /* Hf */
305: { NULL, 0, 0 }, /* Fr */
306: { NULL, 0, 0 }, /* Ud */
307: { NULL, TYPE_Lb, MDOCF_CHILD }, /* Lb */
308: { NULL, 0, 0 }, /* Lp */
309: { NULL, TYPE_Lk, MDOCF_CHILD }, /* Lk */
310: { NULL, TYPE_Mt, MDOCF_CHILD }, /* Mt */
311: { NULL, 0, 0 }, /* Brq */
312: { NULL, 0, 0 }, /* Bro */
313: { NULL, 0, 0 }, /* Brc */
314: { NULL, 0, 0 }, /* %C */
315: { NULL, 0, 0 }, /* Es */
316: { NULL, 0, 0 }, /* En */
317: { NULL, TYPE_Dx, MDOCF_CHILD }, /* Dx */
318: { NULL, 0, 0 }, /* %Q */
319: { NULL, 0, 0 }, /* br */
320: { NULL, 0, 0 }, /* sp */
321: { NULL, 0, 0 }, /* %U */
322: { NULL, 0, 0 }, /* Ta */
1.1 kristaps 323: };
324:
325: int
326: main(int argc, char *argv[])
327: {
1.50 kristaps 328: char cwd[MAXPATHLEN];
329: int ch, rc, fd, i;
330: size_t j, sz;
331: const char *dir;
332: struct str *s;
333: struct mchars *mc;
334: struct manpaths dirs;
335: struct mparse *mp;
336: struct ohash_info ino_info, filename_info, str_info;
337:
338: memset(stmts, 0, STMT__MAX * sizeof(sqlite3_stmt *));
339: memset(&dirs, 0, sizeof(struct manpaths));
340:
341: ino_info.halloc = filename_info.halloc =
342: str_info.halloc = hash_halloc;
343: ino_info.hfree = filename_info.hfree =
344: str_info.hfree = hash_free;
345: ino_info.alloc = filename_info.alloc =
346: str_info.alloc = hash_alloc;
347:
348: ino_info.key_offset = offsetof(struct of, id);
349: filename_info.key_offset = offsetof(struct of, file);
350: str_info.key_offset = offsetof(struct str, key);
1.1 kristaps 351:
352: progname = strrchr(argv[0], '/');
353: if (progname == NULL)
354: progname = argv[0];
355: else
356: ++progname;
357:
1.50 kristaps 358: /*
359: * Remember where we started by keeping a fd open to the origin
360: * path component: throughout this utility, we chdir() a lot to
361: * handle relative paths, and by doing this, we can return to
362: * the starting point.
363: */
364: if (NULL == getcwd(cwd, MAXPATHLEN)) {
365: perror(NULL);
366: return(EXIT_FAILURE);
367: } else if (-1 == (fd = open(cwd, O_RDONLY, 0))) {
368: perror(cwd);
369: return(EXIT_FAILURE);
370: }
371:
372: /*
373: * We accept a few different invocations.
374: * The CHECKOP macro makes sure that invocation styles don't
375: * clobber each other.
376: */
377: #define CHECKOP(_op, _ch) do \
378: if (OP_DEFAULT != (_op)) { \
379: fprintf(stderr, "-%c: Conflicting option\n", (_ch)); \
380: goto usage; \
381: } while (/*CONSTCOND*/0)
1.10 kristaps 382:
1.50 kristaps 383: dir = NULL;
1.38 schwarze 384: op = OP_DEFAULT;
1.1 kristaps 385:
1.50 kristaps 386: while (-1 != (ch = getopt(argc, argv, "aC:d:ntu:vW")))
1.1 kristaps 387: switch (ch) {
1.12 schwarze 388: case ('a'):
389: use_all = 1;
390: break;
1.34 schwarze 391: case ('C'):
1.50 kristaps 392: CHECKOP(op, ch);
1.38 schwarze 393: dir = optarg;
394: op = OP_CONFFILE;
1.34 schwarze 395: break;
1.5 kristaps 396: case ('d'):
1.50 kristaps 397: CHECKOP(op, ch);
1.5 kristaps 398: dir = optarg;
399: op = OP_UPDATE;
400: break;
1.50 kristaps 401: case ('n'):
402: nodb = 1;
403: break;
1.38 schwarze 404: case ('t'):
1.50 kristaps 405: CHECKOP(op, ch);
1.38 schwarze 406: dup2(STDOUT_FILENO, STDERR_FILENO);
407: op = OP_TEST;
1.50 kristaps 408: nodb = warnings = 1;
1.38 schwarze 409: break;
1.5 kristaps 410: case ('u'):
1.50 kristaps 411: CHECKOP(op, ch);
1.5 kristaps 412: dir = optarg;
413: op = OP_DELETE;
414: break;
415: case ('v'):
416: verb++;
417: break;
1.38 schwarze 418: case ('W'):
419: warnings = 1;
420: break;
1.1 kristaps 421: default:
1.38 schwarze 422: goto usage;
1.1 kristaps 423: }
424:
425: argc -= optind;
426: argv += optind;
427:
1.38 schwarze 428: if (OP_CONFFILE == op && argc > 0) {
1.50 kristaps 429: fprintf(stderr, "-C: Too many arguments\n");
1.38 schwarze 430: goto usage;
431: }
432:
1.50 kristaps 433: rc = 1;
434: mp = mparse_alloc(MPARSE_AUTO,
435: MANDOCLEVEL_FATAL, NULL, NULL, NULL);
436: mc = mchars_alloc();
437:
438: ohash_init(&strings, 6, &str_info);
439: ohash_init(&inos, 6, &ino_info);
440: ohash_init(&filenames, 6, &filename_info);
441:
442: if (OP_UPDATE == op || OP_DELETE == op || OP_TEST == op) {
443: /*
444: * Force processing all files.
445: */
446: use_all = 1;
447: if (NULL == dir)
448: dir = cwd;
449: /*
450: * All of these deal with a specific directory.
451: * Jump into that directory then collect files specified
452: * on the command-line.
453: */
454: if (0 == path_reset(cwd, fd, dir))
455: goto out;
456: for (i = 0; i < argc; i++)
457: filescan(argv[i], dir);
458: if (0 == dbopen(dir, 1))
459: goto out;
460: if (OP_TEST != op)
461: dbprune(dir);
462: if (OP_DELETE != op)
463: rc = ofmerge(mc, mp, dir);
464: dbclose(dir, 1);
465: } else {
466: /*
467: * If we have arguments, use them as our manpaths.
468: * If we don't, grok from manpath(1) or however else
469: * manpath_parse() wants to do it.
470: */
471: if (argc > 0) {
472: dirs.paths = mandoc_calloc
473: (argc, sizeof(char *));
474: dirs.sz = (size_t)argc;
475: for (i = 0; i < argc; i++)
476: dirs.paths[i] = mandoc_strdup(argv[i]);
477: } else
478: manpath_parse(&dirs, dir, NULL, NULL);
479:
480: /*
481: * First scan the tree rooted at a base directory.
482: * Then whak its database (if one exists), parse, and
483: * build up the database.
484: * Ignore zero-length directories and strip trailing
485: * slashes.
486: */
487: for (j = 0; j < dirs.sz; j++) {
488: sz = strlen(dirs.paths[j]);
489: if (sz && '/' == dirs.paths[j][sz - 1])
490: dirs.paths[j][--sz] = '\0';
491: if (0 == sz)
492: continue;
493: if (0 == path_reset(cwd, fd, dirs.paths[j]))
494: goto out;
495: if (0 == treescan(dirs.paths[j]))
496: goto out;
497: if (0 == path_reset(cwd, fd, dirs.paths[j]))
498: goto out;
499: if (0 == dbopen(dirs.paths[j], 0))
500: goto out;
1.52 ! kristaps 501:
! 502: /*
! 503: * Since we're opening up a new database, we can
! 504: * turn off synchronous mode for much better
! 505: * performance.
! 506: */
! 507: SQL_EXEC("PRAGMA synchronous = OFF");
! 508:
1.50 kristaps 509: if (0 == ofmerge(mc, mp, dirs.paths[j]))
510: goto out;
511: dbclose(dirs.paths[j], 0);
512: offree();
513: ohash_delete(&inos);
514: ohash_init(&inos, 6, &ino_info);
515: ohash_delete(&filenames);
516: ohash_init(&filenames, 6, &filename_info);
517: }
518: }
519: out:
520: close(fd);
521: manpath_free(&dirs);
522: mchars_free(mc);
523: mparse_free(mp);
524: for (s = ohash_first(&strings, &ch);
525: NULL != s; s = ohash_next(&strings, &ch)) {
526: if (s->utf8 != s->key)
527: free(s->utf8);
528: free(s);
529: }
530: ohash_delete(&strings);
531: ohash_delete(&inos);
532: ohash_delete(&filenames);
533: offree();
534: return(rc ? EXIT_SUCCESS : EXIT_FAILURE);
535: usage:
536: fprintf(stderr, "usage: %s [-anvW] [-C file]\n"
537: " %s [-anvW] dir ...\n"
538: " %s [-nvW] -d dir [file ...]\n"
539: " %s [-nvW] -u dir [file ...]\n"
540: " %s -t file ...\n",
541: progname, progname, progname,
542: progname, progname);
543:
544: return(EXIT_FAILURE);
545: }
546:
547: /*
548: * Scan a directory tree rooted at "base" for manpages.
549: * We use fts(), scanning directory parts along the way for clues to our
550: * section and architecture.
551: *
552: * If use_all has been specified, grok all files.
553: * If not, sanitise paths to the following:
554: *
555: * [./]man*[/<arch>]/<name>.<section>
556: * or
557: * [./]cat<section>[/<arch>]/<name>.0
558: *
559: * TODO: accomodate for multi-language directories.
560: */
561: static int
562: treescan(const char *base)
563: {
564: FTS *f;
565: FTSENT *ff;
566: int dform;
567: char *sec;
568: const char *dsec, *arch, *cp, *name, *path;
569: const char *argv[2];
570:
571: argv[0] = ".";
572: argv[1] = (char *)NULL;
573:
574: /*
575: * Walk through all components under the directory, using the
576: * logical descent of files.
577: */
578: f = fts_open((char * const *)argv, FTS_LOGICAL, NULL);
579: if (NULL == f) {
580: perror(base);
581: return(0);
582: }
583:
584: dsec = arch = NULL;
585: dform = FORM_NONE;
586:
587: while (NULL != (ff = fts_read(f))) {
588: path = ff->fts_path + 2;
589: /*
590: * If we're a regular file, add an "of" by using the
591: * stored directory data and handling the filename.
592: * Disallow duplicate (hard-linked) files.
593: */
594: if (FTS_F == ff->fts_info) {
595: if ( ! use_all && ff->fts_level < 2) {
596: WARNING(path, base, "Extraneous file");
597: continue;
598: } else if (inocheck(ff->fts_statp)) {
599: WARNING(path, base, "Duplicate file");
600: continue;
601: }
602:
603: cp = ff->fts_name;
604:
605: if (0 == strcmp(cp, "mandocdb.db")) {
606: WARNING(path, base, "Skip database");
607: continue;
608: } else if (NULL != (cp = strrchr(cp, '.'))) {
609: if (0 == strcmp(cp + 1, "html")) {
610: WARNING(path, base, "Skip html");
611: continue;
612: } else if (0 == strcmp(cp + 1, "gz")) {
613: WARNING(path, base, "Skip gz");
614: continue;
615: } else if (0 == strcmp(cp + 1, "ps")) {
616: WARNING(path, base, "Skip ps");
617: continue;
618: } else if (0 == strcmp(cp + 1, "pdf")) {
619: WARNING(path, base, "Skip pdf");
620: continue;
621: }
622: }
1.1 kristaps 623:
1.50 kristaps 624: if (NULL != (sec = strrchr(ff->fts_name, '.'))) {
625: *sec = '\0';
626: sec = stradd(sec + 1);
627: }
628: name = stradd(ff->fts_name);
629: ofadd(base, dform, path,
630: name, dsec, sec, arch, ff->fts_statp);
631: continue;
632: } else if (FTS_D != ff->fts_info &&
633: FTS_DP != ff->fts_info)
634: continue;
1.1 kristaps 635:
1.50 kristaps 636: switch (ff->fts_level) {
637: case (0):
638: /* Ignore the root directory. */
639: break;
640: case (1):
641: /*
642: * This might contain manX/ or catX/.
643: * Try to infer this from the name.
644: * If we're not in use_all, enforce it.
645: */
646: dsec = NULL;
647: dform = FORM_NONE;
648: cp = ff->fts_name;
649: if (FTS_DP == ff->fts_info)
650: break;
1.1 kristaps 651:
1.50 kristaps 652: if (0 == strncmp(cp, "man", 3)) {
653: dform = FORM_SRC;
654: dsec = stradd(cp + 3);
655: } else if (0 == strncmp(cp, "cat", 3)) {
656: dform = FORM_CAT;
657: dsec = stradd(cp + 3);
658: }
1.1 kristaps 659:
1.50 kristaps 660: if (NULL != dsec || use_all)
661: break;
1.1 kristaps 662:
1.50 kristaps 663: WARNING(path, base, "Unknown directory part");
664: fts_set(f, ff, FTS_SKIP);
665: break;
666: case (2):
667: /*
668: * Possibly our architecture.
669: * If we're descending, keep tabs on it.
670: */
671: arch = NULL;
672: if (FTS_DP != ff->fts_info && NULL != dsec)
673: arch = stradd(ff->fts_name);
674: break;
675: default:
676: if (FTS_DP == ff->fts_info || use_all)
677: break;
678: WARNING(path, base, "Extraneous directory part");
679: fts_set(f, ff, FTS_SKIP);
680: break;
1.5 kristaps 681: }
1.50 kristaps 682: }
683:
684: fts_close(f);
685: return(1);
686: }
1.5 kristaps 687:
1.50 kristaps 688: /*
689: * Add a file to the file vector.
690: * Do not verify that it's a "valid" looking manpage (we'll do that
691: * later).
692: *
693: * Try to infer the manual section, architecture, and page name from the
694: * path, assuming it looks like
695: *
696: * [./]man*[/<arch>]/<name>.<section>
697: * or
698: * [./]cat<section>[/<arch>]/<name>.0
699: *
700: * Stuff this information directly into the "of" vector.
701: * See treescan() for the fts(3) version of this.
702: */
703: static void
704: filescan(const char *file, const char *base)
705: {
706: const char *sec, *arch, *name, *dsec;
707: char *p, *start, *buf;
708: int dform;
709: struct stat st;
1.5 kristaps 710:
1.50 kristaps 711: assert(use_all);
1.5 kristaps 712:
1.50 kristaps 713: if (0 == strncmp(file, "./", 2))
714: file += 2;
1.5 kristaps 715:
1.50 kristaps 716: if (-1 == stat(file, &st)) {
717: WARNING(file, base, "%s", strerror(errno));
718: return;
719: } else if ( ! (S_IFREG & st.st_mode)) {
720: WARNING(file, base, "Not a regular file");
721: return;
722: } else if (inocheck(&st)) {
723: WARNING(file, base, "Duplicate file");
724: return;
725: }
1.5 kristaps 726:
1.50 kristaps 727: buf = mandoc_strdup(file);
728: start = buf;
729: sec = arch = name = dsec = NULL;
730: dform = FORM_NONE;
1.17 schwarze 731:
1.50 kristaps 732: /*
733: * First try to guess our directory structure.
734: * If we find a separator, try to look for man* or cat*.
735: * If we find one of these and what's underneath is a directory,
736: * assume it's an architecture.
737: */
738: if (NULL != (p = strchr(start, '/'))) {
739: *p++ = '\0';
740: if (0 == strncmp(start, "man", 3)) {
741: dform = FORM_SRC;
742: dsec = start + 3;
743: } else if (0 == strncmp(start, "cat", 3)) {
744: dform = FORM_CAT;
745: dsec = start + 3;
1.17 schwarze 746: }
1.5 kristaps 747:
1.50 kristaps 748: start = p;
749: if (NULL != dsec && NULL != (p = strchr(start, '/'))) {
750: *p++ = '\0';
751: arch = start;
752: start = p;
753: }
754: }
755:
756: /*
757: * Now check the file suffix.
758: * Suffix of `.0' indicates a catpage, `.1-9' is a manpage.
759: */
760: p = strrchr(start, '\0');
761: while (p-- > start && '/' != *p && '.' != *p)
762: /* Loop. */ ;
763:
764: if ('.' == *p) {
765: *p++ = '\0';
766: sec = p;
1.5 kristaps 767: }
768:
1.10 kristaps 769: /*
1.50 kristaps 770: * Now try to parse the name.
771: * Use the filename portion of the path.
1.10 kristaps 772: */
1.50 kristaps 773: name = start;
774: if (NULL != (p = strrchr(start, '/'))) {
775: name = p + 1;
776: *p = '\0';
777: }
778:
779: ofadd(base, dform, file, name, dsec, sec, arch, &st);
780: free(buf);
781: }
782:
783: /*
784: * See fileadd().
785: */
786: static int
787: filecheck(const char *name)
788: {
789: unsigned int index;
1.10 kristaps 790:
1.50 kristaps 791: index = ohash_qlookup(&filenames, name);
792: return(NULL != ohash_find(&filenames, index));
793: }
1.10 kristaps 794:
1.50 kristaps 795: /*
796: * Use the standard hashing mechanism (K&R) to see if the given filename
797: * already exists.
798: */
799: static void
800: fileadd(struct of *of)
801: {
802: unsigned int index;
1.1 kristaps 803:
1.50 kristaps 804: index = ohash_qlookup(&filenames, of->file);
805: assert(NULL == ohash_find(&filenames, index));
806: ohash_insert(&filenames, index, of);
807: }
1.3 kristaps 808:
1.50 kristaps 809: /*
810: * See inoadd().
811: */
812: static int
813: inocheck(const struct stat *st)
814: {
815: struct id id;
816: uint32_t hash;
817: unsigned int index;
818:
819: memset(&id, 0, sizeof(id));
820: id.ino = hash = st->st_ino;
821: id.dev = st->st_dev;
822: index = ohash_lookup_memory
823: (&inos, (char *)&id, sizeof(id), hash);
1.13 schwarze 824:
1.50 kristaps 825: return(NULL != ohash_find(&inos, index));
826: }
1.5 kristaps 827:
1.50 kristaps 828: /*
829: * The hashing function used here is quite simple: simply take the inode
830: * and use uint32_t of its bits.
831: * Then when we do the lookup, use both the inode and device identifier.
832: */
833: static void
834: inoadd(const struct stat *st, struct of *of)
835: {
836: uint32_t hash;
837: unsigned int index;
1.1 kristaps 838:
1.50 kristaps 839: of->id.ino = hash = st->st_ino;
840: of->id.dev = st->st_dev;
841: index = ohash_lookup_memory
842: (&inos, (char *)&of->id, sizeof(of->id), hash);
1.1 kristaps 843:
1.50 kristaps 844: assert(NULL == ohash_find(&inos, index));
845: ohash_insert(&inos, index, of);
846: }
1.35 kristaps 847:
1.50 kristaps 848: static void
849: ofadd(const char *base, int dform, const char *file,
850: const char *name, const char *dsec, const char *sec,
851: const char *arch, const struct stat *st)
852: {
853: struct of *of;
854: int sform;
855:
856: assert(NULL != file);
857:
858: if (NULL == name)
859: name = "";
860: if (NULL == sec)
861: sec = "";
862: if (NULL == dsec)
863: dsec = "";
864: if (NULL == arch)
865: arch = "";
866:
867: sform = FORM_NONE;
868: if (NULL != sec && *sec <= '9' && *sec >= '1')
869: sform = FORM_SRC;
870: else if (NULL != sec && *sec == '0') {
871: sec = dsec;
872: sform = FORM_CAT;
873: }
874:
875: of = mandoc_calloc(1, sizeof(struct of));
876: strlcpy(of->file, file, MAXPATHLEN);
877: of->name = name;
878: of->sec = sec;
879: of->dsec = dsec;
880: of->arch = arch;
881: of->sform = sform;
882: of->dform = dform;
883: of->next = ofs;
884: ofs = of;
1.3 kristaps 885:
1.50 kristaps 886: /*
887: * Add to unique identifier hash.
888: * Then if it's a source manual and we're going to use source in
889: * favour of catpages, add it to that hash.
890: */
891: inoadd(st, of);
892: fileadd(of);
893: }
1.3 kristaps 894:
1.50 kristaps 895: static void
896: offree(void)
897: {
898: struct of *of;
1.3 kristaps 899:
1.50 kristaps 900: while (NULL != (of = ofs)) {
901: ofs = of->next;
902: free(of);
903: }
904: }
1.38 schwarze 905:
1.50 kristaps 906: /*
907: * Run through the files in the global vector "ofs" and add them to the
908: * database specified in "base".
909: *
910: * This handles the parsing scheme itself, using the cues of directory
911: * and filename to determine whether the file is parsable or not.
912: */
913: static int
914: ofmerge(struct mchars *mc, struct mparse *mp, const char *base)
915: {
916: int form;
917: size_t sz;
1.3 kristaps 918: struct mdoc *mdoc;
919: struct man *man;
1.50 kristaps 920: char buf[MAXPATHLEN];
921: char *bufp;
922: const char *msec, *march, *mtitle, *cp;
923: struct of *of;
924: enum mandoclevel lvl;
925:
926: for (of = ofs; NULL != of; of = of->next) {
927: /*
928: * If we're a catpage (as defined by our path), then see
929: * if a manpage exists by the same name (ignoring the
930: * suffix).
931: * If it does, then we want to use it instead of our
932: * own.
933: */
934: if ( ! use_all && FORM_CAT == of->dform) {
935: sz = strlcpy(buf, of->file, MAXPATHLEN);
936: if (sz >= MAXPATHLEN) {
937: WARNING(of->file, base,
938: "Filename too long");
939: continue;
940: }
941: bufp = strstr(buf, "cat");
942: assert(NULL != bufp);
943: memcpy(bufp, "man", 3);
944: if (NULL != (bufp = strrchr(buf, '.')))
945: *++bufp = '\0';
946: strlcat(buf, of->dsec, MAXPATHLEN);
947: if (filecheck(buf)) {
948: WARNING(of->file, base, "Man "
949: "source exists: %s", buf);
950: continue;
951: }
952: }
953:
954: words = NULL;
955: mparse_reset(mp);
956: mdoc = NULL;
957: man = NULL;
958: form = 0;
959: msec = of->dsec;
960: march = of->arch;
961: mtitle = of->name;
1.14 schwarze 962:
963: /*
1.33 schwarze 964: * Try interpreting the file as mdoc(7) or man(7)
965: * source code, unless it is already known to be
966: * formatted. Fall back to formatted mode.
1.14 schwarze 967: */
1.50 kristaps 968: if (FORM_SRC == of->dform || FORM_SRC == of->sform) {
969: lvl = mparse_readfd(mp, -1, of->file);
970: if (lvl < MANDOCLEVEL_FATAL)
971: mparse_result(mp, &mdoc, &man);
972: }
1.14 schwarze 973:
974: if (NULL != mdoc) {
1.50 kristaps 975: form = 1;
1.14 schwarze 976: msec = mdoc_meta(mdoc)->msec;
1.38 schwarze 977: march = mdoc_meta(mdoc)->arch;
1.14 schwarze 978: mtitle = mdoc_meta(mdoc)->title;
979: } else if (NULL != man) {
1.50 kristaps 980: form = 1;
1.14 schwarze 981: msec = man_meta(man)->msec;
1.38 schwarze 982: march = "";
1.14 schwarze 983: mtitle = man_meta(man)->title;
1.50 kristaps 984: }
985:
986: if (NULL == msec)
987: msec = "";
988: if (NULL == march)
989: march = "";
990: if (NULL == mtitle)
991: mtitle = "";
1.1 kristaps 992:
1.12 schwarze 993: /*
1.44 kristaps 994: * Check whether the manual section given in a file
995: * agrees with the directory where the file is located.
996: * Some manuals have suffixes like (3p) on their
997: * section number either inside the file or in the
998: * directory name, some are linked into more than one
999: * section, like encrypt(1) = makekey(8). Do not skip
1000: * manuals for such reasons.
1.12 schwarze 1001: */
1.50 kristaps 1002: if ( ! use_all && form && strcasecmp(msec, of->dsec))
1003: WARNING(of->file, base, "Section \"%s\" "
1004: "manual in %s directory",
1005: msec, of->dsec);
1.12 schwarze 1006:
1.42 schwarze 1007: /*
1008: * Manual page directories exist for each kernel
1009: * architecture as returned by machine(1).
1010: * However, many manuals only depend on the
1011: * application architecture as returned by arch(1).
1012: * For example, some (2/ARM) manuals are shared
1013: * across the "armish" and "zaurus" kernel
1014: * architectures.
1015: * A few manuals are even shared across completely
1016: * different architectures, for example fdformat(1)
1017: * on amd64, i386, sparc, and sparc64.
1018: * Thus, warn about architecture mismatches,
1019: * but don't skip manuals for this reason.
1020: */
1.50 kristaps 1021: if ( ! use_all && strcasecmp(march, of->arch))
1022: WARNING(of->file, base, "Architecture \"%s\" "
1.47 schwarze 1023: "manual in \"%s\" directory",
1.45 kristaps 1024: march, of->arch);
1.12 schwarze 1025:
1.50 kristaps 1026: putkey(of, of->name, TYPE_Nm);
1.12 schwarze 1027:
1.50 kristaps 1028: if (NULL != mdoc) {
1029: if (NULL != (cp = mdoc_meta(mdoc)->name))
1030: putkey(of, cp, TYPE_Nm);
1031: parse_mdoc(of, mdoc_node(mdoc));
1032: } else if (NULL != man)
1033: parse_man(of, man_node(man));
1034: else
1035: parse_catpage(of, base);
1.44 kristaps 1036:
1.50 kristaps 1037: dbindex(mc, form, of, base);
1038: }
1.44 kristaps 1039:
1.50 kristaps 1040: return(1);
1041: }
1.12 schwarze 1042:
1.50 kristaps 1043: static void
1044: parse_catpage(struct of *of, const char *base)
1045: {
1046: FILE *stream;
1047: char *line, *p, *title;
1048: size_t len, plen, titlesz;
1.12 schwarze 1049:
1.50 kristaps 1050: if (NULL == (stream = fopen(of->file, "r"))) {
1051: WARNING(of->file, base, "%s", strerror(errno));
1052: return;
1053: }
1.1 kristaps 1054:
1.50 kristaps 1055: /* Skip to first blank line. */
1.1 kristaps 1056:
1.50 kristaps 1057: while (NULL != (line = fgetln(stream, &len)))
1058: if ('\n' == *line)
1059: break;
1.1 kristaps 1060:
1.50 kristaps 1061: /*
1062: * Assume the first line that is not indented
1063: * is the first section header. Skip to it.
1064: */
1.1 kristaps 1065:
1.50 kristaps 1066: while (NULL != (line = fgetln(stream, &len)))
1067: if ('\n' != *line && ' ' != *line)
1068: break;
1069:
1070: /*
1071: * Read up until the next section into a buffer.
1072: * Strip the leading and trailing newline from each read line,
1073: * appending a trailing space.
1074: * Ignore empty (whitespace-only) lines.
1075: */
1.1 kristaps 1076:
1.50 kristaps 1077: titlesz = 0;
1078: title = NULL;
1.38 schwarze 1079:
1.50 kristaps 1080: while (NULL != (line = fgetln(stream, &len))) {
1081: if (' ' != *line || '\n' != line[len - 1])
1082: break;
1083: while (len > 0 && isspace((unsigned char)*line)) {
1084: line++;
1085: len--;
1086: }
1087: if (1 == len)
1.38 schwarze 1088: continue;
1.50 kristaps 1089: title = mandoc_realloc(title, titlesz + len);
1090: memcpy(title + titlesz, line, len);
1091: titlesz += len;
1092: title[titlesz - 1] = ' ';
1093: }
1.38 schwarze 1094:
1.50 kristaps 1095: /*
1096: * If no page content can be found, or the input line
1097: * is already the next section header, or there is no
1098: * trailing newline, reuse the page title as the page
1099: * description.
1100: */
1.44 kristaps 1101:
1.50 kristaps 1102: if (NULL == title || '\0' == *title) {
1103: WARNING(of->file, base, "Cannot find NAME section");
1104: fclose(stream);
1105: free(title);
1106: return;
1107: }
1.1 kristaps 1108:
1.50 kristaps 1109: title = mandoc_realloc(title, titlesz + 1);
1110: title[titlesz] = '\0';
1.33 schwarze 1111:
1.50 kristaps 1112: /*
1113: * Skip to the first dash.
1114: * Use the remaining line as the description (no more than 70
1115: * bytes).
1116: */
1.33 schwarze 1117:
1.50 kristaps 1118: if (NULL != (p = strstr(title, "- "))) {
1119: for (p += 2; ' ' == *p || '\b' == *p; p++)
1120: /* Skip to next word. */ ;
1121: } else {
1122: WARNING(of->file, base, "No dash in title line");
1123: p = title;
1124: }
1.38 schwarze 1125:
1.50 kristaps 1126: plen = strlen(p);
1.1 kristaps 1127:
1.50 kristaps 1128: /* Strip backspace-encoding from line. */
1.1 kristaps 1129:
1.50 kristaps 1130: while (NULL != (line = memchr(p, '\b', plen))) {
1131: len = line - p;
1132: if (0 == len) {
1133: memmove(line, line + 1, plen--);
1134: continue;
1135: }
1136: memmove(line - 1, line + 1, plen - len);
1137: plen -= 2;
1138: }
1.1 kristaps 1139:
1.50 kristaps 1140: of->desc = stradd(p);
1141: putkey(of, p, TYPE_Nd);
1142: fclose(stream);
1143: free(title);
1144: }
1.1 kristaps 1145:
1.50 kristaps 1146: /*
1147: * Put a type/word pair into the word database for this particular file.
1148: */
1149: static void
1150: putkey(const struct of *of, const char *value, uint64_t type)
1151: {
1.18 kristaps 1152:
1.50 kristaps 1153: assert(NULL != value);
1154: wordaddbuf(of, value, strlen(value), type);
1155: }
1.44 kristaps 1156:
1.50 kristaps 1157: /*
1158: * Like putkey() but for unterminated strings.
1159: */
1160: static void
1161: putkeys(const struct of *of, const char *value, int sz, uint64_t type)
1162: {
1.44 kristaps 1163:
1.50 kristaps 1164: wordaddbuf(of, value, sz, type);
1.3 kristaps 1165: }
1166:
1167: /*
1.50 kristaps 1168: * Grok all nodes at or below a certain mdoc node into putkey().
1.3 kristaps 1169: */
1170: static void
1.50 kristaps 1171: putmdockey(const struct of *of, const struct mdoc_node *n, uint64_t m)
1.3 kristaps 1172: {
1.18 kristaps 1173:
1.50 kristaps 1174: for ( ; NULL != n; n = n->next) {
1175: if (NULL != n->child)
1176: putmdockey(of, n->child, m);
1177: if (MDOC_TEXT == n->type)
1178: putkey(of, n->string, m);
1179: }
1180: }
1.18 kristaps 1181:
1.50 kristaps 1182: static int
1183: parse_man(struct of *of, const struct man_node *n)
1184: {
1185: const struct man_node *head, *body;
1186: char *start, *sv, *title;
1187: char byte;
1188: size_t sz, titlesz;
1.18 kristaps 1189:
1.50 kristaps 1190: if (NULL == n)
1191: return(0);
1.18 kristaps 1192:
1.50 kristaps 1193: /*
1194: * We're only searching for one thing: the first text child in
1195: * the BODY of a NAME section. Since we don't keep track of
1196: * sections in -man, run some hoops to find out whether we're in
1197: * the correct section or not.
1198: */
1.18 kristaps 1199:
1.50 kristaps 1200: if (MAN_BODY == n->type && MAN_SH == n->tok) {
1201: body = n;
1202: assert(body->parent);
1203: if (NULL != (head = body->parent->head) &&
1204: 1 == head->nchild &&
1205: NULL != (head = (head->child)) &&
1206: MAN_TEXT == head->type &&
1207: 0 == strcmp(head->string, "NAME") &&
1208: NULL != (body = body->child) &&
1209: MAN_TEXT == body->type) {
1.3 kristaps 1210:
1.50 kristaps 1211: title = NULL;
1212: titlesz = 0;
1.3 kristaps 1213:
1.50 kristaps 1214: /*
1215: * Suck the entire NAME section into memory.
1216: * Yes, we might run away.
1217: * But too many manuals have big, spread-out
1218: * NAME sections over many lines.
1219: */
1.3 kristaps 1220:
1.50 kristaps 1221: for ( ; NULL != body; body = body->next) {
1222: if (MAN_TEXT != body->type)
1223: break;
1224: if (0 == (sz = strlen(body->string)))
1225: continue;
1226: title = mandoc_realloc
1227: (title, titlesz + sz + 1);
1228: memcpy(title + titlesz, body->string, sz);
1229: titlesz += sz + 1;
1230: title[titlesz - 1] = ' ';
1231: }
1232: if (NULL == title)
1233: return(1);
1.18 kristaps 1234:
1.50 kristaps 1235: title = mandoc_realloc(title, titlesz + 1);
1236: title[titlesz] = '\0';
1.18 kristaps 1237:
1.50 kristaps 1238: /* Skip leading space. */
1.18 kristaps 1239:
1.50 kristaps 1240: sv = title;
1241: while (isspace((unsigned char)*sv))
1242: sv++;
1.18 kristaps 1243:
1.50 kristaps 1244: if (0 == (sz = strlen(sv))) {
1245: free(title);
1246: return(1);
1247: }
1.1 kristaps 1248:
1.50 kristaps 1249: /* Erase trailing space. */
1.1 kristaps 1250:
1.50 kristaps 1251: start = &sv[sz - 1];
1252: while (start > sv && isspace((unsigned char)*start))
1253: *start-- = '\0';
1.1 kristaps 1254:
1.50 kristaps 1255: if (start == sv) {
1256: free(title);
1257: return(1);
1258: }
1.1 kristaps 1259:
1.50 kristaps 1260: start = sv;
1.18 kristaps 1261:
1.50 kristaps 1262: /*
1263: * Go through a special heuristic dance here.
1264: * Conventionally, one or more manual names are
1265: * comma-specified prior to a whitespace, then a
1266: * dash, then a description. Try to puzzle out
1267: * the name parts here.
1268: */
1.18 kristaps 1269:
1.50 kristaps 1270: for ( ;; ) {
1271: sz = strcspn(start, " ,");
1272: if ('\0' == start[sz])
1273: break;
1.1 kristaps 1274:
1.50 kristaps 1275: byte = start[sz];
1276: start[sz] = '\0';
1.1 kristaps 1277:
1.50 kristaps 1278: putkey(of, start, TYPE_Nm);
1.1 kristaps 1279:
1.50 kristaps 1280: if (' ' == byte) {
1281: start += sz + 1;
1282: break;
1283: }
1.1 kristaps 1284:
1.50 kristaps 1285: assert(',' == byte);
1286: start += sz + 1;
1287: while (' ' == *start)
1288: start++;
1289: }
1.1 kristaps 1290:
1.50 kristaps 1291: if (sv == start) {
1292: putkey(of, start, TYPE_Nm);
1293: free(title);
1294: return(1);
1295: }
1.1 kristaps 1296:
1.50 kristaps 1297: while (isspace((unsigned char)*start))
1298: start++;
1.1 kristaps 1299:
1.50 kristaps 1300: if (0 == strncmp(start, "-", 1))
1301: start += 1;
1302: else if (0 == strncmp(start, "\\-\\-", 4))
1303: start += 4;
1304: else if (0 == strncmp(start, "\\-", 2))
1305: start += 2;
1306: else if (0 == strncmp(start, "\\(en", 4))
1307: start += 4;
1308: else if (0 == strncmp(start, "\\(em", 4))
1309: start += 4;
1.1 kristaps 1310:
1.50 kristaps 1311: while (' ' == *start)
1312: start++;
1.1 kristaps 1313:
1.50 kristaps 1314: assert(NULL == of->desc);
1315: of->desc = stradd(start);
1316: putkey(of, start, TYPE_Nd);
1317: free(title);
1318: return(1);
1319: }
1320: }
1.1 kristaps 1321:
1.50 kristaps 1322: for (n = n->child; n; n = n->next)
1323: if (parse_man(of, n))
1324: return(1);
1.1 kristaps 1325:
1.50 kristaps 1326: return(0);
1.1 kristaps 1327: }
1328:
1329: static void
1.50 kristaps 1330: parse_mdoc(struct of *of, const struct mdoc_node *n)
1.1 kristaps 1331: {
1332:
1.50 kristaps 1333: assert(NULL != n);
1334: for (n = n->child; NULL != n; n = n->next) {
1335: switch (n->type) {
1336: case (MDOC_ELEM):
1337: /* FALLTHROUGH */
1338: case (MDOC_BLOCK):
1339: /* FALLTHROUGH */
1340: case (MDOC_HEAD):
1341: /* FALLTHROUGH */
1342: case (MDOC_BODY):
1343: /* FALLTHROUGH */
1344: case (MDOC_TAIL):
1345: if (NULL != mdocs[n->tok].fp)
1346: if (0 == (*mdocs[n->tok].fp)(of, n))
1347: break;
1.1 kristaps 1348:
1.50 kristaps 1349: if (MDOCF_CHILD & mdocs[n->tok].flags)
1350: putmdockey(of, n->child, mdocs[n->tok].mask);
1351: break;
1352: default:
1353: assert(MDOC_ROOT != n->type);
1354: continue;
1355: }
1356: if (NULL != n->child)
1357: parse_mdoc(of, n);
1.1 kristaps 1358: }
1359: }
1360:
1.25 schwarze 1361: static int
1.50 kristaps 1362: parse_mdoc_Fd(struct of *of, const struct mdoc_node *n)
1.1 kristaps 1363: {
1364: const char *start, *end;
1365: size_t sz;
1.25 schwarze 1366:
1.50 kristaps 1367: if (SEC_SYNOPSIS != n->sec ||
1368: NULL == (n = n->child) ||
1369: MDOC_TEXT != n->type)
1.25 schwarze 1370: return(0);
1.1 kristaps 1371:
1372: /*
1373: * Only consider those `Fd' macro fields that begin with an
1374: * "inclusion" token (versus, e.g., #define).
1375: */
1.50 kristaps 1376:
1.1 kristaps 1377: if (strcmp("#include", n->string))
1.25 schwarze 1378: return(0);
1.1 kristaps 1379:
1380: if (NULL == (n = n->next) || MDOC_TEXT != n->type)
1.25 schwarze 1381: return(0);
1.1 kristaps 1382:
1383: /*
1384: * Strip away the enclosing angle brackets and make sure we're
1385: * not zero-length.
1386: */
1387:
1388: start = n->string;
1389: if ('<' == *start || '"' == *start)
1390: start++;
1391:
1392: if (0 == (sz = strlen(start)))
1.25 schwarze 1393: return(0);
1.1 kristaps 1394:
1395: end = &start[(int)sz - 1];
1396: if ('>' == *end || '"' == *end)
1397: end--;
1398:
1.50 kristaps 1399: if (end > start)
1400: putkeys(of, start, end - start + 1, TYPE_In);
1.25 schwarze 1401: return(1);
1.1 kristaps 1402: }
1403:
1.25 schwarze 1404: static int
1.50 kristaps 1405: parse_mdoc_In(struct of *of, const struct mdoc_node *n)
1.1 kristaps 1406: {
1407:
1.50 kristaps 1408: if (NULL != n->child && MDOC_TEXT == n->child->type)
1.25 schwarze 1409: return(0);
1.1 kristaps 1410:
1.50 kristaps 1411: putkey(of, n->child->string, TYPE_In);
1.25 schwarze 1412: return(1);
1.1 kristaps 1413: }
1414:
1.25 schwarze 1415: static int
1.50 kristaps 1416: parse_mdoc_Fn(struct of *of, const struct mdoc_node *n)
1.1 kristaps 1417: {
1418: const char *cp;
1419:
1.50 kristaps 1420: if (NULL == (n = n->child) || MDOC_TEXT != n->type)
1.25 schwarze 1421: return(0);
1422:
1.50 kristaps 1423: /*
1424: * Parse: .Fn "struct type *name" "char *arg".
1425: * First strip away pointer symbol.
1426: * Then store the function name, then type.
1427: * Finally, store the arguments.
1428: */
1.1 kristaps 1429:
1.50 kristaps 1430: if (NULL == (cp = strrchr(n->string, ' ')))
1431: cp = n->string;
1.1 kristaps 1432:
1433: while ('*' == *cp)
1434: cp++;
1435:
1.50 kristaps 1436: putkey(of, cp, TYPE_Fn);
1.25 schwarze 1437:
1.50 kristaps 1438: if (n->string < cp)
1439: putkeys(of, n->string, cp - n->string, TYPE_Ft);
1.25 schwarze 1440:
1.50 kristaps 1441: for (n = n->next; NULL != n; n = n->next)
1442: if (MDOC_TEXT == n->type)
1443: putkey(of, n->string, TYPE_Fa);
1.25 schwarze 1444:
1445: return(0);
1.1 kristaps 1446: }
1447:
1.25 schwarze 1448: static int
1.50 kristaps 1449: parse_mdoc_St(struct of *of, const struct mdoc_node *n)
1.1 kristaps 1450: {
1.25 schwarze 1451:
1.1 kristaps 1452: if (NULL == n->child || MDOC_TEXT != n->child->type)
1.25 schwarze 1453: return(0);
1.1 kristaps 1454:
1.50 kristaps 1455: putkey(of, n->child->string, TYPE_St);
1.25 schwarze 1456: return(1);
1.1 kristaps 1457: }
1458:
1.25 schwarze 1459: static int
1.50 kristaps 1460: parse_mdoc_Xr(struct of *of, const struct mdoc_node *n)
1.1 kristaps 1461: {
1462:
1463: if (NULL == (n = n->child))
1.25 schwarze 1464: return(0);
1.1 kristaps 1465:
1.50 kristaps 1466: putkey(of, n->string, TYPE_Xr);
1.25 schwarze 1467: return(1);
1.1 kristaps 1468: }
1469:
1.25 schwarze 1470: static int
1.50 kristaps 1471: parse_mdoc_Nd(struct of *of, const struct mdoc_node *n)
1.1 kristaps 1472: {
1.50 kristaps 1473: size_t sz;
1474: char *sv, *desc;
1.1 kristaps 1475:
1476: if (MDOC_BODY != n->type)
1.25 schwarze 1477: return(0);
1.1 kristaps 1478:
1.50 kristaps 1479: /*
1480: * Special-case the `Nd' because we need to put the description
1481: * into the document table.
1482: */
1483:
1484: desc = NULL;
1485: for (n = n->child; NULL != n; n = n->next) {
1486: if (MDOC_TEXT == n->type) {
1487: sz = strlen(n->string) + 1;
1488: if (NULL != (sv = desc))
1489: sz += strlen(desc) + 1;
1490: desc = mandoc_realloc(desc, sz);
1491: if (NULL != sv)
1492: strlcat(desc, " ", sz);
1493: else
1494: *desc = '\0';
1495: strlcat(desc, n->string, sz);
1496: }
1497: if (NULL != n->child)
1498: parse_mdoc_Nd(of, n);
1499: }
1500:
1501: of->desc = NULL != desc ? stradd(desc) : NULL;
1502: free(desc);
1.25 schwarze 1503: return(1);
1.1 kristaps 1504: }
1505:
1.25 schwarze 1506: static int
1.50 kristaps 1507: parse_mdoc_Nm(struct of *of, const struct mdoc_node *n)
1.1 kristaps 1508: {
1509:
1.25 schwarze 1510: if (SEC_NAME == n->sec)
1511: return(1);
1512: else if (SEC_SYNOPSIS != n->sec || MDOC_HEAD != n->type)
1513: return(0);
1.1 kristaps 1514:
1.25 schwarze 1515: return(1);
1.1 kristaps 1516: }
1517:
1.25 schwarze 1518: static int
1.50 kristaps 1519: parse_mdoc_Sh(struct of *of, const struct mdoc_node *n)
1.1 kristaps 1520: {
1521:
1.25 schwarze 1522: return(SEC_CUSTOM == n->sec && MDOC_HEAD == n->type);
1.1 kristaps 1523: }
1524:
1.50 kristaps 1525: static int
1526: parse_mdoc_head(struct of *of, const struct mdoc_node *n)
1.1 kristaps 1527: {
1528:
1.50 kristaps 1529: return(MDOC_HEAD == n->type);
1530: }
1.1 kristaps 1531:
1.50 kristaps 1532: static int
1533: parse_mdoc_body(struct of *of, const struct mdoc_node *n)
1534: {
1.1 kristaps 1535:
1.50 kristaps 1536: return(MDOC_BODY == n->type);
1.1 kristaps 1537: }
1538:
1.50 kristaps 1539: /*
1540: * See straddbuf().
1541: */
1542: static char *
1543: stradd(const char *cp)
1.1 kristaps 1544: {
1545:
1.50 kristaps 1546: return(straddbuf(cp, strlen(cp)));
1.1 kristaps 1547: }
1548:
1549: /*
1.50 kristaps 1550: * This looks up or adds a string to the string table.
1551: * The string table is a table of all strings encountered during parse
1552: * or file scan.
1553: * In using it, we avoid having thousands of (e.g.) "cat1" string
1554: * allocations for the "of" table.
1555: * We also have a layer atop the string table for keeping track of words
1556: * in a parse sequence (see wordaddbuf()).
1.1 kristaps 1557: */
1.50 kristaps 1558: static char *
1559: straddbuf(const char *cp, size_t sz)
1.1 kristaps 1560: {
1.50 kristaps 1561: struct str *s;
1562: unsigned int index;
1563: const char *end;
1564:
1565: if (NULL != (s = hashget(cp, sz)))
1566: return(s->key);
1.1 kristaps 1567:
1.51 kristaps 1568: s = mandoc_calloc(sizeof(struct str) + sz + 1, 1);
1.50 kristaps 1569: memcpy(s->key, cp, sz);
1.1 kristaps 1570:
1.50 kristaps 1571: end = cp + sz;
1572: index = ohash_qlookupi(&strings, cp, &end);
1573: assert(NULL == ohash_find(&strings, index));
1574: ohash_insert(&strings, index, s);
1575: return(s->key);
1576: }
1.25 schwarze 1577:
1.50 kristaps 1578: static struct str *
1579: hashget(const char *cp, size_t sz)
1580: {
1581: unsigned int index;
1582: const char *end;
1.25 schwarze 1583:
1.50 kristaps 1584: end = cp + sz;
1585: index = ohash_qlookupi(&strings, cp, &end);
1586: return(ohash_find(&strings, index));
1587: }
1.1 kristaps 1588:
1.50 kristaps 1589: /*
1590: * Add a word to the current parse sequence.
1591: * Within the hashtable of strings, we maintain a list of strings that
1592: * are currently indexed.
1593: * Each of these ("words") has a bitmask modified within the parse.
1594: * When we finish a parse, we'll dump the list, then remove the head
1595: * entry -- since the next parse will have a new "of", it can keep track
1596: * of its entries without conflict.
1597: */
1598: static void
1599: wordaddbuf(const struct of *of,
1600: const char *cp, size_t sz, uint64_t v)
1601: {
1602: struct str *s;
1603: unsigned int index;
1604: const char *end;
1.25 schwarze 1605:
1.50 kristaps 1606: if (0 == sz)
1607: return;
1.25 schwarze 1608:
1.50 kristaps 1609: s = hashget(cp, sz);
1.25 schwarze 1610:
1.50 kristaps 1611: if (NULL != s && of == s->of) {
1612: s->mask |= v;
1613: return;
1614: } else if (NULL == s) {
1.51 kristaps 1615: s = mandoc_calloc(sizeof(struct str) + sz + 1, 1);
1.50 kristaps 1616: memcpy(s->key, cp, sz);
1617: end = cp + sz;
1618: index = ohash_qlookupi(&strings, cp, &end);
1619: assert(NULL == ohash_find(&strings, index));
1620: ohash_insert(&strings, index, s);
1.1 kristaps 1621: }
1622:
1.50 kristaps 1623: s->next = words;
1624: s->of = of;
1625: s->mask = v;
1626: words = s;
1.1 kristaps 1627: }
1628:
1.50 kristaps 1629: /*
1630: * Take a Unicode codepoint and produce its UTF-8 encoding.
1631: * This isn't the best way to do this, but it works.
1632: * The magic numbers are from the UTF-8 packaging.
1633: * They're not as scary as they seem: read the UTF-8 spec for details.
1634: */
1635: static size_t
1636: utf8(unsigned int cp, char out[7])
1.1 kristaps 1637: {
1.50 kristaps 1638: size_t rc;
1.1 kristaps 1639:
1.50 kristaps 1640: rc = 0;
1641: if (cp <= 0x0000007F) {
1642: rc = 1;
1643: out[0] = (char)cp;
1644: } else if (cp <= 0x000007FF) {
1645: rc = 2;
1646: out[0] = (cp >> 6 & 31) | 192;
1647: out[1] = (cp & 63) | 128;
1648: } else if (cp <= 0x0000FFFF) {
1649: rc = 3;
1650: out[0] = (cp >> 12 & 15) | 224;
1651: out[1] = (cp >> 6 & 63) | 128;
1652: out[2] = (cp & 63) | 128;
1653: } else if (cp <= 0x001FFFFF) {
1654: rc = 4;
1655: out[0] = (cp >> 18 & 7) | 240;
1656: out[1] = (cp >> 12 & 63) | 128;
1657: out[2] = (cp >> 6 & 63) | 128;
1658: out[3] = (cp & 63) | 128;
1659: } else if (cp <= 0x03FFFFFF) {
1660: rc = 5;
1661: out[0] = (cp >> 24 & 3) | 248;
1662: out[1] = (cp >> 18 & 63) | 128;
1663: out[2] = (cp >> 12 & 63) | 128;
1664: out[3] = (cp >> 6 & 63) | 128;
1665: out[4] = (cp & 63) | 128;
1666: } else if (cp <= 0x7FFFFFFF) {
1667: rc = 6;
1668: out[0] = (cp >> 30 & 1) | 252;
1669: out[1] = (cp >> 24 & 63) | 128;
1670: out[2] = (cp >> 18 & 63) | 128;
1671: out[3] = (cp >> 12 & 63) | 128;
1672: out[4] = (cp >> 6 & 63) | 128;
1673: out[5] = (cp & 63) | 128;
1674: } else
1.1 kristaps 1675: return(0);
1676:
1.50 kristaps 1677: out[rc] = '\0';
1678: return(rc);
1679: }
1.1 kristaps 1680:
1.50 kristaps 1681: /*
1682: * Store the UTF-8 version of a key, or alias the pointer if the key has
1683: * no UTF-8 transcription marks in it.
1684: */
1685: static void
1686: utf8key(struct mchars *mc, struct str *key)
1687: {
1688: size_t sz, bsz, pos;
1689: char utfbuf[7], res[5];
1690: char *buf;
1691: const char *seq, *cpp, *val;
1692: int len, u;
1693: enum mandoc_esc esc;
1694:
1695: assert(NULL == key->utf8);
1696:
1697: res[0] = '\\';
1698: res[1] = '\t';
1699: res[2] = ASCII_NBRSP;
1700: res[3] = ASCII_HYPH;
1701: res[4] = '\0';
1.1 kristaps 1702:
1.50 kristaps 1703: val = key->key;
1704: bsz = strlen(val);
1.46 kristaps 1705:
1.50 kristaps 1706: /*
1707: * Pre-check: if we have no stop-characters, then set the
1708: * pointer as ourselvse and get out of here.
1709: */
1710: if (strcspn(val, res) == bsz) {
1711: key->utf8 = key->key;
1712: return;
1713: }
1.46 kristaps 1714:
1.50 kristaps 1715: /* Pre-allocate by the length of the input */
1.46 kristaps 1716:
1.50 kristaps 1717: buf = mandoc_malloc(++bsz);
1718: pos = 0;
1.46 kristaps 1719:
1.50 kristaps 1720: while ('\0' != *val) {
1721: /*
1722: * Halt on the first escape sequence.
1723: * This also halts on the end of string, in which case
1724: * we just copy, fallthrough, and exit the loop.
1725: */
1726: if ((sz = strcspn(val, res)) > 0) {
1727: memcpy(&buf[pos], val, sz);
1728: pos += sz;
1729: val += sz;
1730: }
1.46 kristaps 1731:
1.50 kristaps 1732: if (ASCII_HYPH == *val) {
1733: buf[pos++] = '-';
1734: val++;
1735: continue;
1736: } else if ('\t' == *val || ASCII_NBRSP == *val) {
1737: buf[pos++] = ' ';
1738: val++;
1739: continue;
1740: } else if ('\\' != *val)
1741: break;
1.46 kristaps 1742:
1.50 kristaps 1743: /* Read past the slash. */
1.46 kristaps 1744:
1.50 kristaps 1745: val++;
1746: u = 0;
1.46 kristaps 1747:
1.50 kristaps 1748: /*
1749: * Parse the escape sequence and see if it's a
1750: * predefined character or special character.
1751: */
1752: esc = mandoc_escape
1753: ((const char **)&val, &seq, &len);
1754: if (ESCAPE_ERROR == esc)
1755: break;
1.1 kristaps 1756:
1.50 kristaps 1757: if (ESCAPE_SPECIAL != esc)
1758: continue;
1759: if (0 == (u = mchars_spec2cp(mc, seq, len)))
1760: continue;
1.1 kristaps 1761:
1.50 kristaps 1762: /*
1763: * If we have a Unicode codepoint, try to convert that
1764: * to a UTF-8 byte string.
1765: */
1766: cpp = utfbuf;
1767: if (0 == (sz = utf8(u, utfbuf)))
1768: continue;
1.1 kristaps 1769:
1.50 kristaps 1770: /* Copy the rendered glyph into the stream. */
1.1 kristaps 1771:
1.50 kristaps 1772: sz = strlen(cpp);
1773: bsz += sz;
1.1 kristaps 1774:
1.50 kristaps 1775: buf = mandoc_realloc(buf, bsz);
1.1 kristaps 1776:
1.50 kristaps 1777: memcpy(&buf[pos], cpp, sz);
1778: pos += sz;
1.1 kristaps 1779: }
1780:
1.50 kristaps 1781: buf[pos] = '\0';
1782: key->utf8 = buf;
1.1 kristaps 1783: }
1784:
1.14 schwarze 1785: /*
1.50 kristaps 1786: * Flush the current page's terms (and their bits) into the database.
1787: * Wrap the entire set of additions in a transaction to make sqlite be a
1788: * little faster.
1789: * Also, UTF-8-encode the description at the last possible moment.
1.14 schwarze 1790: */
1791: static void
1.50 kristaps 1792: dbindex(struct mchars *mc, int form,
1793: const struct of *of, const char *base)
1.14 schwarze 1794: {
1.50 kristaps 1795: struct str *key;
1796: const char *desc;
1797: int64_t recno;
1.52 ! kristaps 1798: size_t i;
1.14 schwarze 1799:
1.50 kristaps 1800: DEBUG(of->file, base, "Adding to index");
1.43 kristaps 1801:
1.50 kristaps 1802: if (nodb)
1.14 schwarze 1803: return;
1.28 kristaps 1804:
1.50 kristaps 1805: desc = "";
1806: if (NULL != of->desc) {
1807: key = hashget(of->desc, strlen(of->desc));
1808: assert(NULL != key);
1809: if (NULL == key->utf8)
1810: utf8key(mc, key);
1811: desc = key->utf8;
1812: }
1813:
1.52 ! kristaps 1814: SQL_EXEC("BEGIN TRANSACTION");
1.50 kristaps 1815:
1.52 ! kristaps 1816: i = 1;
! 1817: SQL_BIND_TEXT(stmts[STMT_INSERT_DOC], i, of->file);
! 1818: SQL_BIND_TEXT(stmts[STMT_INSERT_DOC], i, of->sec);
! 1819: SQL_BIND_TEXT(stmts[STMT_INSERT_DOC], i, of->arch);
! 1820: SQL_BIND_TEXT(stmts[STMT_INSERT_DOC], i, desc);
! 1821: SQL_BIND_INT(stmts[STMT_INSERT_DOC], i, form);
! 1822: SQL_STEP(stmts[STMT_INSERT_DOC]);
1.50 kristaps 1823: recno = sqlite3_last_insert_rowid(db);
1824: sqlite3_reset(stmts[STMT_INSERT_DOC]);
1825:
1826: for (key = words; NULL != key; key = key->next) {
1827: assert(key->of == of);
1828: if (NULL == key->utf8)
1829: utf8key(mc, key);
1.52 ! kristaps 1830: i = 1;
! 1831: SQL_BIND_INT64(stmts[STMT_INSERT_KEY], i, key->mask);
! 1832: SQL_BIND_TEXT(stmts[STMT_INSERT_KEY], i, key->utf8);
! 1833: SQL_BIND_INT64(stmts[STMT_INSERT_KEY], i, recno);
! 1834: SQL_STEP(stmts[STMT_INSERT_KEY]);
1.50 kristaps 1835: sqlite3_reset(stmts[STMT_INSERT_KEY]);
1.38 schwarze 1836: }
1.28 kristaps 1837:
1.52 ! kristaps 1838: SQL_EXEC("END TRANSACTION");
1.14 schwarze 1839: }
1840:
1.5 kristaps 1841: static void
1.50 kristaps 1842: dbprune(const char *base)
1.5 kristaps 1843: {
1.50 kristaps 1844: struct of *of;
1.52 ! kristaps 1845: size_t i;
1.5 kristaps 1846:
1.50 kristaps 1847: if (nodb)
1848: return;
1.12 schwarze 1849:
1.50 kristaps 1850: for (of = ofs; NULL != of; of = of->next) {
1.52 ! kristaps 1851: i = 1;
! 1852: SQL_BIND_TEXT(stmts[STMT_DELETE], i, of->file);
! 1853: SQL_STEP(stmts[STMT_DELETE]);
1.50 kristaps 1854: sqlite3_reset(stmts[STMT_DELETE]);
1855: DEBUG(of->file, base, "Deleted from index");
1.5 kristaps 1856: }
1857: }
1858:
1.4 kristaps 1859: /*
1.50 kristaps 1860: * Close an existing database and its prepared statements.
1861: * If "real" is not set, rename the temporary file into the real one.
1.4 kristaps 1862: */
1.35 kristaps 1863: static void
1.50 kristaps 1864: dbclose(const char *base, int real)
1.4 kristaps 1865: {
1.50 kristaps 1866: size_t i;
1867: char file[MAXPATHLEN];
1.4 kristaps 1868:
1.50 kristaps 1869: if (nodb)
1.38 schwarze 1870: return;
1.50 kristaps 1871:
1872: for (i = 0; i < STMT__MAX; i++) {
1873: sqlite3_finalize(stmts[i]);
1874: stmts[i] = NULL;
1.4 kristaps 1875: }
1876:
1.50 kristaps 1877: sqlite3_close(db);
1878: db = NULL;
1.12 schwarze 1879:
1.50 kristaps 1880: if (real)
1881: return;
1.12 schwarze 1882:
1.50 kristaps 1883: strlcpy(file, MANDOC_DB, MAXPATHLEN);
1884: strlcat(file, "~", MAXPATHLEN);
1885: if (-1 == rename(file, MANDOC_DB))
1886: perror(MANDOC_DB);
1887: }
1.14 schwarze 1888:
1.50 kristaps 1889: /*
1890: * This is straightforward stuff.
1891: * Open a database connection to a "temporary" database, then open a set
1892: * of prepared statements we'll use over and over again.
1893: * If "real" is set, we use the existing database; if not, we truncate a
1894: * temporary one.
1895: * Must be matched by dbclose().
1896: */
1897: static int
1898: dbopen(const char *base, int real)
1899: {
1900: char file[MAXPATHLEN];
1901: const char *sql;
1902: int rc, ofl;
1903: size_t sz;
1.12 schwarze 1904:
1.50 kristaps 1905: if (nodb)
1906: return(1);
1.12 schwarze 1907:
1.50 kristaps 1908: sz = strlcpy(file, MANDOC_DB, MAXPATHLEN);
1909: if ( ! real)
1910: sz = strlcat(file, "~", MAXPATHLEN);
1.5 kristaps 1911:
1.50 kristaps 1912: if (sz >= MAXPATHLEN) {
1913: fprintf(stderr, "%s: Path too long\n", file);
1914: return(0);
1915: }
1.5 kristaps 1916:
1.50 kristaps 1917: if ( ! real)
1918: remove(file);
1.38 schwarze 1919:
1.52 ! kristaps 1920: ofl = SQLITE_OPEN_READWRITE |
1.50 kristaps 1921: (0 == real ? SQLITE_OPEN_EXCLUSIVE : 0);
1.45 kristaps 1922:
1.50 kristaps 1923: rc = sqlite3_open_v2(file, &db, ofl, NULL);
1924: if (SQLITE_OK == rc)
1925: return(1);
1926: if (SQLITE_CANTOPEN != rc) {
1927: perror(file);
1928: return(0);
1929: }
1.12 schwarze 1930:
1.50 kristaps 1931: sqlite3_close(db);
1932: db = NULL;
1.12 schwarze 1933:
1.50 kristaps 1934: if (SQLITE_OK != (rc = sqlite3_open(file, &db))) {
1935: perror(file);
1936: return(0);
1937: }
1.12 schwarze 1938:
1.50 kristaps 1939: sql = "CREATE TABLE \"docs\" (\n"
1940: " \"file\" TEXT NOT NULL,\n"
1941: " \"sec\" TEXT NOT NULL,\n"
1942: " \"arch\" TEXT NOT NULL,\n"
1943: " \"desc\" TEXT NOT NULL,\n"
1944: " \"form\" INTEGER NOT NULL,\n"
1945: " \"id\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL\n"
1946: ");\n"
1947: "\n"
1948: "CREATE TABLE \"keys\" (\n"
1949: " \"bits\" INTEGER NOT NULL,\n"
1950: " \"key\" TEXT NOT NULL,\n"
1951: " \"docid\" INTEGER NOT NULL REFERENCES docs(id) "
1952: "ON DELETE CASCADE,\n"
1953: " \"id\" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL\n"
1954: ");\n"
1955: "\n"
1956: "CREATE INDEX \"key_index\" ON keys (key);\n";
1.14 schwarze 1957:
1.50 kristaps 1958: if (SQLITE_OK != sqlite3_exec(db, sql, NULL, NULL, NULL)) {
1959: perror(sqlite3_errmsg(db));
1960: return(0);
1961: }
1.4 kristaps 1962:
1.50 kristaps 1963: sql = "DELETE FROM docs where file=?";
1964: sqlite3_prepare_v2(db, sql, -1, &stmts[STMT_DELETE], NULL);
1965: sql = "INSERT INTO docs "
1966: "(file,sec,arch,desc,form) VALUES (?,?,?,?,?)";
1967: sqlite3_prepare_v2(db, sql, -1, &stmts[STMT_INSERT_DOC], NULL);
1968: sql = "INSERT INTO keys "
1969: "(bits,key,docid) VALUES (?,?,?)";
1970: sqlite3_prepare_v2(db, sql, -1, &stmts[STMT_INSERT_KEY], NULL);
1971: return(1);
1972: }
1.5 kristaps 1973:
1.50 kristaps 1974: static void *
1975: hash_halloc(size_t sz, void *arg)
1976: {
1.12 schwarze 1977:
1.50 kristaps 1978: return(mandoc_calloc(sz, 1));
1979: }
1.12 schwarze 1980:
1.50 kristaps 1981: static void *
1982: hash_alloc(size_t sz, void *arg)
1983: {
1.5 kristaps 1984:
1.50 kristaps 1985: return(mandoc_malloc(sz));
1986: }
1.41 kristaps 1987:
1.50 kristaps 1988: static void
1989: hash_free(void *p, size_t sz, void *arg)
1990: {
1.4 kristaps 1991:
1.50 kristaps 1992: free(p);
1.4 kristaps 1993: }
1994:
1.50 kristaps 1995: static int
1996: path_reset(const char *cwd, int fd, const char *base)
1.4 kristaps 1997: {
1998:
1.50 kristaps 1999: if (-1 == fchdir(fd)) {
2000: perror(cwd);
2001: return(0);
2002: } else if (-1 == chdir(base)) {
2003: perror(base);
2004: return(0);
1.4 kristaps 2005: }
1.50 kristaps 2006: return(1);
1.1 kristaps 2007: }
CVSweb