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