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