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