Annotation of mandoc/mandocdb.c, Revision 1.42
1.42 ! schwarze 1: /* $Id: mandocdb.c,v 1.41 2011/12/25 19:31:25 kristaps Exp $ */
1.1 kristaps 2: /*
3: * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.12 schwarze 4: * Copyright (c) 2011 Ingo Schwarze <schwarze@openbsd.org>
1.1 kristaps 5: *
6: * Permission to use, copy, modify, and distribute this software for any
7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
9: *
10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17: */
18: #ifdef HAVE_CONFIG_H
19: #include "config.h"
20: #endif
21:
22: #include <sys/param.h>
1.14 schwarze 23: #include <sys/types.h>
1.1 kristaps 24:
25: #include <assert.h>
1.4 kristaps 26: #include <dirent.h>
1.1 kristaps 27: #include <fcntl.h>
28: #include <getopt.h>
29: #include <stdio.h>
30: #include <stdint.h>
31: #include <stdlib.h>
32: #include <string.h>
1.17 schwarze 33: #include <unistd.h>
1.1 kristaps 34:
1.21 kristaps 35: #if defined(__linux__)
36: # include <endian.h>
1.1 kristaps 37: # include <db_185.h>
1.21 kristaps 38: #elif defined(__APPLE__)
39: # include <libkern/OSByteOrder.h>
40: # include <db.h>
1.1 kristaps 41: #else
42: # include <db.h>
43: #endif
44:
45: #include "man.h"
46: #include "mdoc.h"
47: #include "mandoc.h"
1.8 schwarze 48: #include "mandocdb.h"
1.10 kristaps 49: #include "manpath.h"
1.1 kristaps 50:
51: #define MANDOC_BUFSZ BUFSIZ
52: #define MANDOC_SLOP 1024
53:
1.14 schwarze 54: #define MANDOC_SRC 0x1
55: #define MANDOC_FORM 0x2
56:
1.38 schwarze 57: /* Access to the mandoc database on disk. */
58:
59: struct mdb {
60: char idxn[MAXPATHLEN]; /* index db filename */
61: char dbn[MAXPATHLEN]; /* keyword db filename */
62: DB *idx; /* index recno database */
63: DB *db; /* keyword btree database */
64: };
65:
66: /* Stack of temporarily unused index records. */
67:
68: struct recs {
69: recno_t *stack; /* pointer to a malloc'ed array */
70: size_t size; /* number of allocated slots */
71: size_t cur; /* current number of empty records */
72: recno_t last; /* last record number in the index */
73: };
74:
1.5 kristaps 75: /* Tiny list for files. No need to bring in QUEUE. */
76:
1.3 kristaps 77: struct of {
1.5 kristaps 78: char *fname; /* heap-allocated */
1.12 schwarze 79: char *sec;
80: char *arch;
81: char *title;
1.14 schwarze 82: int src_form;
1.5 kristaps 83: struct of *next; /* NULL for last one */
84: struct of *first; /* first in list */
1.3 kristaps 85: };
86:
1.1 kristaps 87: /* Buffer for storing growable data. */
88:
89: struct buf {
90: char *cp;
1.5 kristaps 91: size_t len; /* current length */
92: size_t size; /* total buffer size */
1.1 kristaps 93: };
94:
95: /* Operation we're going to perform. */
96:
97: enum op {
1.38 schwarze 98: OP_DEFAULT = 0, /* new dbs from dir list or default config */
99: OP_CONFFILE, /* new databases from custom config file */
1.5 kristaps 100: OP_UPDATE, /* delete/add entries in existing database */
1.38 schwarze 101: OP_DELETE, /* delete entries from existing database */
102: OP_TEST /* change no databases, report potential problems */
1.1 kristaps 103: };
104:
105: #define MAN_ARGS DB *hash, \
106: struct buf *buf, \
107: struct buf *dbuf, \
108: const struct man_node *n
109: #define MDOC_ARGS DB *hash, \
110: struct buf *buf, \
111: struct buf *dbuf, \
112: const struct mdoc_node *n, \
113: const struct mdoc_meta *m
114:
115: static void buf_appendmdoc(struct buf *,
116: const struct mdoc_node *, int);
117: static void buf_append(struct buf *, const char *);
118: static void buf_appendb(struct buf *,
119: const void *, size_t);
120: static void dbt_put(DB *, const char *, DBT *, DBT *);
1.9 kristaps 121: static void hash_put(DB *, const struct buf *, uint64_t);
1.1 kristaps 122: static void hash_reset(DB **);
1.3 kristaps 123: static void index_merge(const struct of *, struct mparse *,
1.16 schwarze 124: struct buf *, struct buf *, DB *,
1.38 schwarze 125: struct mdb *, struct recs *);
126: static void index_prune(const struct of *, struct mdb *,
127: struct recs *);
1.16 schwarze 128: static void ofile_argbuild(int, char *[], struct of **);
1.35 kristaps 129: static void ofile_dirbuild(const char *, const char *,
1.16 schwarze 130: const char *, int, struct of **);
1.4 kristaps 131: static void ofile_free(struct of *);
1.14 schwarze 132: static void pformatted(DB *, struct buf *, struct buf *,
133: const struct of *);
1.1 kristaps 134: static int pman_node(MAN_ARGS);
135: static void pmdoc_node(MDOC_ARGS);
1.25 schwarze 136: static int pmdoc_head(MDOC_ARGS);
137: static int pmdoc_body(MDOC_ARGS);
138: static int pmdoc_Fd(MDOC_ARGS);
139: static int pmdoc_In(MDOC_ARGS);
140: static int pmdoc_Fn(MDOC_ARGS);
141: static int pmdoc_Nd(MDOC_ARGS);
142: static int pmdoc_Nm(MDOC_ARGS);
143: static int pmdoc_Sh(MDOC_ARGS);
144: static int pmdoc_St(MDOC_ARGS);
145: static int pmdoc_Xr(MDOC_ARGS);
1.1 kristaps 146:
1.25 schwarze 147: #define MDOCF_CHILD 0x01 /* Automatically index child nodes. */
1.1 kristaps 148:
1.25 schwarze 149: struct mdoc_handler {
150: int (*fp)(MDOC_ARGS); /* Optional handler. */
151: uint64_t mask; /* Set unless handler returns 0. */
152: int flags; /* For use by pmdoc_node. */
153: };
154:
155: static const struct mdoc_handler mdocs[MDOC_MAX] = {
156: { NULL, 0, 0 }, /* Ap */
157: { NULL, 0, 0 }, /* Dd */
158: { NULL, 0, 0 }, /* Dt */
159: { NULL, 0, 0 }, /* Os */
160: { pmdoc_Sh, TYPE_Sh, MDOCF_CHILD }, /* Sh */
161: { pmdoc_head, TYPE_Ss, MDOCF_CHILD }, /* Ss */
162: { NULL, 0, 0 }, /* Pp */
163: { NULL, 0, 0 }, /* D1 */
164: { NULL, 0, 0 }, /* Dl */
165: { NULL, 0, 0 }, /* Bd */
166: { NULL, 0, 0 }, /* Ed */
167: { NULL, 0, 0 }, /* Bl */
168: { NULL, 0, 0 }, /* El */
169: { NULL, 0, 0 }, /* It */
170: { NULL, 0, 0 }, /* Ad */
171: { NULL, TYPE_An, MDOCF_CHILD }, /* An */
172: { NULL, TYPE_Ar, MDOCF_CHILD }, /* Ar */
173: { NULL, TYPE_Cd, MDOCF_CHILD }, /* Cd */
174: { NULL, TYPE_Cm, MDOCF_CHILD }, /* Cm */
175: { NULL, TYPE_Dv, MDOCF_CHILD }, /* Dv */
176: { NULL, TYPE_Er, MDOCF_CHILD }, /* Er */
177: { NULL, TYPE_Ev, MDOCF_CHILD }, /* Ev */
178: { NULL, 0, 0 }, /* Ex */
179: { NULL, TYPE_Fa, MDOCF_CHILD }, /* Fa */
180: { pmdoc_Fd, TYPE_In, 0 }, /* Fd */
181: { NULL, TYPE_Fl, MDOCF_CHILD }, /* Fl */
182: { pmdoc_Fn, 0, 0 }, /* Fn */
183: { NULL, TYPE_Ft, MDOCF_CHILD }, /* Ft */
184: { NULL, TYPE_Ic, MDOCF_CHILD }, /* Ic */
185: { pmdoc_In, TYPE_In, 0 }, /* In */
186: { NULL, TYPE_Li, MDOCF_CHILD }, /* Li */
187: { pmdoc_Nd, TYPE_Nd, MDOCF_CHILD }, /* Nd */
188: { pmdoc_Nm, TYPE_Nm, MDOCF_CHILD }, /* Nm */
189: { NULL, 0, 0 }, /* Op */
190: { NULL, 0, 0 }, /* Ot */
191: { NULL, TYPE_Pa, MDOCF_CHILD }, /* Pa */
192: { NULL, 0, 0 }, /* Rv */
193: { pmdoc_St, TYPE_St, 0 }, /* St */
194: { NULL, TYPE_Va, MDOCF_CHILD }, /* Va */
195: { pmdoc_body, TYPE_Va, MDOCF_CHILD }, /* Vt */
196: { pmdoc_Xr, TYPE_Xr, 0 }, /* Xr */
197: { NULL, 0, 0 }, /* %A */
198: { NULL, 0, 0 }, /* %B */
199: { NULL, 0, 0 }, /* %D */
200: { NULL, 0, 0 }, /* %I */
201: { NULL, 0, 0 }, /* %J */
202: { NULL, 0, 0 }, /* %N */
203: { NULL, 0, 0 }, /* %O */
204: { NULL, 0, 0 }, /* %P */
205: { NULL, 0, 0 }, /* %R */
206: { NULL, 0, 0 }, /* %T */
207: { NULL, 0, 0 }, /* %V */
208: { NULL, 0, 0 }, /* Ac */
209: { NULL, 0, 0 }, /* Ao */
210: { NULL, 0, 0 }, /* Aq */
211: { NULL, TYPE_At, MDOCF_CHILD }, /* At */
212: { NULL, 0, 0 }, /* Bc */
213: { NULL, 0, 0 }, /* Bf */
214: { NULL, 0, 0 }, /* Bo */
215: { NULL, 0, 0 }, /* Bq */
216: { NULL, TYPE_Bsx, MDOCF_CHILD }, /* Bsx */
217: { NULL, TYPE_Bx, MDOCF_CHILD }, /* Bx */
218: { NULL, 0, 0 }, /* Db */
219: { NULL, 0, 0 }, /* Dc */
220: { NULL, 0, 0 }, /* Do */
221: { NULL, 0, 0 }, /* Dq */
222: { NULL, 0, 0 }, /* Ec */
223: { NULL, 0, 0 }, /* Ef */
224: { NULL, TYPE_Em, MDOCF_CHILD }, /* Em */
225: { NULL, 0, 0 }, /* Eo */
226: { NULL, TYPE_Fx, MDOCF_CHILD }, /* Fx */
227: { NULL, TYPE_Ms, MDOCF_CHILD }, /* Ms */
228: { NULL, 0, 0 }, /* No */
229: { NULL, 0, 0 }, /* Ns */
230: { NULL, TYPE_Nx, MDOCF_CHILD }, /* Nx */
231: { NULL, TYPE_Ox, MDOCF_CHILD }, /* Ox */
232: { NULL, 0, 0 }, /* Pc */
233: { NULL, 0, 0 }, /* Pf */
234: { NULL, 0, 0 }, /* Po */
235: { NULL, 0, 0 }, /* Pq */
236: { NULL, 0, 0 }, /* Qc */
237: { NULL, 0, 0 }, /* Ql */
238: { NULL, 0, 0 }, /* Qo */
239: { NULL, 0, 0 }, /* Qq */
240: { NULL, 0, 0 }, /* Re */
241: { NULL, 0, 0 }, /* Rs */
242: { NULL, 0, 0 }, /* Sc */
243: { NULL, 0, 0 }, /* So */
244: { NULL, 0, 0 }, /* Sq */
245: { NULL, 0, 0 }, /* Sm */
246: { NULL, 0, 0 }, /* Sx */
247: { NULL, TYPE_Sy, MDOCF_CHILD }, /* Sy */
248: { NULL, TYPE_Tn, MDOCF_CHILD }, /* Tn */
249: { NULL, 0, 0 }, /* Ux */
250: { NULL, 0, 0 }, /* Xc */
251: { NULL, 0, 0 }, /* Xo */
252: { pmdoc_head, TYPE_Fn, 0 }, /* Fo */
253: { NULL, 0, 0 }, /* Fc */
254: { NULL, 0, 0 }, /* Oo */
255: { NULL, 0, 0 }, /* Oc */
256: { NULL, 0, 0 }, /* Bk */
257: { NULL, 0, 0 }, /* Ek */
258: { NULL, 0, 0 }, /* Bt */
259: { NULL, 0, 0 }, /* Hf */
260: { NULL, 0, 0 }, /* Fr */
261: { NULL, 0, 0 }, /* Ud */
262: { NULL, TYPE_Lb, MDOCF_CHILD }, /* Lb */
263: { NULL, 0, 0 }, /* Lp */
264: { NULL, TYPE_Lk, MDOCF_CHILD }, /* Lk */
265: { NULL, TYPE_Mt, MDOCF_CHILD }, /* Mt */
266: { NULL, 0, 0 }, /* Brq */
267: { NULL, 0, 0 }, /* Bro */
268: { NULL, 0, 0 }, /* Brc */
269: { NULL, 0, 0 }, /* %C */
270: { NULL, 0, 0 }, /* Es */
271: { NULL, 0, 0 }, /* En */
272: { NULL, TYPE_Dx, MDOCF_CHILD }, /* Dx */
273: { NULL, 0, 0 }, /* %Q */
274: { NULL, 0, 0 }, /* br */
275: { NULL, 0, 0 }, /* sp */
276: { NULL, 0, 0 }, /* %U */
277: { NULL, 0, 0 }, /* Ta */
1.1 kristaps 278: };
279:
280: static const char *progname;
1.16 schwarze 281: static int use_all; /* Use all directories and files. */
282: static int verb; /* Output verbosity level. */
1.38 schwarze 283: static int warnings; /* Potential problems in manuals. */
1.1 kristaps 284:
285: int
286: main(int argc, char *argv[])
287: {
288: struct mparse *mp; /* parse sequence */
1.10 kristaps 289: struct manpaths dirs;
1.38 schwarze 290: struct mdb mdb;
291: struct recs recs;
1.1 kristaps 292: enum op op; /* current operation */
1.5 kristaps 293: const char *dir;
1.26 kristaps 294: char *cp;
1.38 schwarze 295: char pbuf[PATH_MAX];
1.16 schwarze 296: int ch, i, flags;
1.38 schwarze 297: DB *hash; /* temporary keyword hashtable */
1.1 kristaps 298: BTREEINFO info; /* btree configuration */
1.38 schwarze 299: size_t sz1, sz2;
1.1 kristaps 300: struct buf buf, /* keyword buffer */
301: dbuf; /* description buffer */
1.5 kristaps 302: struct of *of; /* list of files for processing */
1.1 kristaps 303: extern int optind;
304: extern char *optarg;
305:
306: progname = strrchr(argv[0], '/');
307: if (progname == NULL)
308: progname = argv[0];
309: else
310: ++progname;
311:
1.10 kristaps 312: memset(&dirs, 0, sizeof(struct manpaths));
1.38 schwarze 313: memset(&mdb, 0, sizeof(struct mdb));
314: memset(&recs, 0, sizeof(struct recs));
1.10 kristaps 315:
1.4 kristaps 316: of = NULL;
1.1 kristaps 317: mp = NULL;
318: hash = NULL;
1.38 schwarze 319: op = OP_DEFAULT;
1.5 kristaps 320: dir = NULL;
1.1 kristaps 321:
1.38 schwarze 322: while (-1 != (ch = getopt(argc, argv, "aC:d:tu:vW")))
1.1 kristaps 323: switch (ch) {
1.12 schwarze 324: case ('a'):
325: use_all = 1;
326: break;
1.34 schwarze 327: case ('C'):
1.38 schwarze 328: if (op) {
329: fprintf(stderr,
330: "-C: conflicting options\n");
331: goto usage;
332: }
333: dir = optarg;
334: op = OP_CONFFILE;
1.34 schwarze 335: break;
1.5 kristaps 336: case ('d'):
1.38 schwarze 337: if (op) {
338: fprintf(stderr,
339: "-d: conflicting options\n");
340: goto usage;
341: }
1.5 kristaps 342: dir = optarg;
343: op = OP_UPDATE;
344: break;
1.38 schwarze 345: case ('t'):
346: dup2(STDOUT_FILENO, STDERR_FILENO);
347: if (op) {
348: fprintf(stderr,
349: "-t: conflicting options\n");
350: goto usage;
351: }
352: op = OP_TEST;
353: use_all = 1;
354: warnings = 1;
355: break;
1.5 kristaps 356: case ('u'):
1.38 schwarze 357: if (op) {
358: fprintf(stderr,
359: "-u: conflicting options\n");
360: goto usage;
361: }
1.5 kristaps 362: dir = optarg;
363: op = OP_DELETE;
364: break;
365: case ('v'):
366: verb++;
367: break;
1.38 schwarze 368: case ('W'):
369: warnings = 1;
370: break;
1.1 kristaps 371: default:
1.38 schwarze 372: goto usage;
1.1 kristaps 373: }
374:
375: argc -= optind;
376: argv += optind;
377:
1.38 schwarze 378: if (OP_CONFFILE == op && argc > 0) {
379: fprintf(stderr, "-C: too many arguments\n");
380: goto usage;
381: }
382:
1.4 kristaps 383: memset(&info, 0, sizeof(BTREEINFO));
384: info.flags = R_DUP;
1.1 kristaps 385:
1.4 kristaps 386: mp = mparse_alloc(MPARSE_AUTO, MANDOCLEVEL_FATAL, NULL, NULL);
1.1 kristaps 387:
1.5 kristaps 388: memset(&buf, 0, sizeof(struct buf));
389: memset(&dbuf, 0, sizeof(struct buf));
1.1 kristaps 390:
1.4 kristaps 391: buf.size = dbuf.size = MANDOC_BUFSZ;
1.1 kristaps 392:
1.4 kristaps 393: buf.cp = mandoc_malloc(buf.size);
394: dbuf.cp = mandoc_malloc(dbuf.size);
1.1 kristaps 395:
1.38 schwarze 396: flags = O_CREAT | O_RDWR;
397: if (OP_DEFAULT == op || OP_CONFFILE == op)
398: flags |= O_TRUNC;
399:
400: if (OP_TEST == op) {
401: ofile_argbuild(argc, argv, &of);
402: if (NULL == of)
403: goto out;
404: index_merge(of, mp, &dbuf, &buf, hash, &mdb, &recs);
405: goto out;
406: }
1.5 kristaps 407:
408: if (OP_UPDATE == op || OP_DELETE == op) {
1.38 schwarze 409: strlcat(mdb.dbn, dir, MAXPATHLEN);
410: strlcat(mdb.dbn, "/", MAXPATHLEN);
411: sz1 = strlcat(mdb.dbn, MANDOC_DB, MAXPATHLEN);
412:
413: strlcat(mdb.idxn, dir, MAXPATHLEN);
414: strlcat(mdb.idxn, "/", MAXPATHLEN);
415: sz2 = strlcat(mdb.idxn, MANDOC_IDX, MAXPATHLEN);
1.5 kristaps 416:
417: if (sz1 >= MAXPATHLEN || sz2 >= MAXPATHLEN) {
1.38 schwarze 418: fprintf(stderr, "%s: path too long\n", dir);
1.5 kristaps 419: exit((int)MANDOCLEVEL_BADARG);
420: }
421:
1.38 schwarze 422: mdb.db = dbopen(mdb.dbn, flags, 0644, DB_BTREE, &info);
423: mdb.idx = dbopen(mdb.idxn, flags, 0644, DB_RECNO, NULL);
1.5 kristaps 424:
1.38 schwarze 425: if (NULL == mdb.db) {
426: perror(mdb.dbn);
1.5 kristaps 427: exit((int)MANDOCLEVEL_SYSERR);
1.38 schwarze 428: } else if (NULL == mdb.idx) {
429: perror(mdb.idxn);
1.5 kristaps 430: exit((int)MANDOCLEVEL_SYSERR);
431: }
432:
1.35 kristaps 433: ofile_argbuild(argc, argv, &of);
1.5 kristaps 434:
435: if (NULL == of)
436: goto out;
437:
1.38 schwarze 438: index_prune(of, &mdb, &recs);
1.5 kristaps 439:
1.17 schwarze 440: /*
1.35 kristaps 441: * Go to the root of the respective manual tree.
442: * This must work or no manuals may be found (they're
443: * indexed relative to the root).
1.17 schwarze 444: */
445:
446: if (OP_UPDATE == op) {
1.35 kristaps 447: if (-1 == chdir(dir)) {
448: perror(dir);
449: exit((int)MANDOCLEVEL_SYSERR);
450: }
1.13 schwarze 451: index_merge(of, mp, &dbuf, &buf, hash,
1.38 schwarze 452: &mdb, &recs);
1.17 schwarze 453: }
1.5 kristaps 454:
455: goto out;
456: }
457:
1.10 kristaps 458: /*
459: * Configure the directories we're going to scan.
460: * If we have command-line arguments, use them.
461: * If not, we use man(1)'s method (see mandocdb.8).
462: */
463:
464: if (argc > 0) {
1.26 kristaps 465: dirs.paths = mandoc_calloc(argc, sizeof(char *));
1.10 kristaps 466: dirs.sz = argc;
1.26 kristaps 467: for (i = 0; i < argc; i++) {
468: if (NULL == (cp = realpath(argv[i], pbuf))) {
469: perror(argv[i]);
470: goto out;
471: }
472: dirs.paths[i] = mandoc_strdup(cp);
473: }
1.10 kristaps 474: } else
1.38 schwarze 475: manpath_parse(&dirs, dir, NULL, NULL);
1.10 kristaps 476:
477: for (i = 0; i < dirs.sz; i++) {
1.38 schwarze 478: mdb.idxn[0] = mdb.dbn[0] = '\0';
1.1 kristaps 479:
1.38 schwarze 480: strlcat(mdb.dbn, dirs.paths[i], MAXPATHLEN);
481: strlcat(mdb.dbn, "/", MAXPATHLEN);
482: sz1 = strlcat(mdb.dbn, MANDOC_DB, MAXPATHLEN);
483:
484: strlcat(mdb.idxn, dirs.paths[i], MAXPATHLEN);
485: strlcat(mdb.idxn, "/", MAXPATHLEN);
486: sz2 = strlcat(mdb.idxn, MANDOC_IDX, MAXPATHLEN);
1.1 kristaps 487:
1.5 kristaps 488: if (sz1 >= MAXPATHLEN || sz2 >= MAXPATHLEN) {
1.38 schwarze 489: fprintf(stderr, "%s: path too long\n",
1.10 kristaps 490: dirs.paths[i]);
1.5 kristaps 491: exit((int)MANDOCLEVEL_BADARG);
1.4 kristaps 492: }
1.3 kristaps 493:
1.38 schwarze 494: if (mdb.db)
495: (*mdb.db->close)(mdb.db);
496: if (mdb.idx)
497: (*mdb.idx->close)(mdb.idx);
1.13 schwarze 498:
1.38 schwarze 499: mdb.db = dbopen(mdb.dbn, flags, 0644, DB_BTREE, &info);
500: mdb.idx = dbopen(mdb.idxn, flags, 0644, DB_RECNO, NULL);
1.3 kristaps 501:
1.38 schwarze 502: if (NULL == mdb.db) {
503: perror(mdb.dbn);
1.5 kristaps 504: exit((int)MANDOCLEVEL_SYSERR);
1.38 schwarze 505: } else if (NULL == mdb.idx) {
506: perror(mdb.idxn);
1.5 kristaps 507: exit((int)MANDOCLEVEL_SYSERR);
508: }
509:
1.4 kristaps 510: ofile_free(of);
511: of = NULL;
1.1 kristaps 512:
1.35 kristaps 513: if (-1 == chdir(dirs.paths[i])) {
514: perror(dirs.paths[i]);
1.5 kristaps 515: exit((int)MANDOCLEVEL_SYSERR);
1.38 schwarze 516: }
1.1 kristaps 517:
1.38 schwarze 518: ofile_dirbuild(".", "", "", 0, &of);
1.5 kristaps 519: if (NULL == of)
520: continue;
1.1 kristaps 521:
1.17 schwarze 522: /*
1.38 schwarze 523: * Go to the root of the respective manual tree.
1.35 kristaps 524: * This must work or no manuals may be found (they're
525: * indexed relative to the root).
1.17 schwarze 526: */
527:
1.35 kristaps 528: if (-1 == chdir(dirs.paths[i])) {
529: perror(dirs.paths[i]);
530: exit((int)MANDOCLEVEL_SYSERR);
531: }
532:
1.38 schwarze 533: index_merge(of, mp, &dbuf, &buf, hash, &mdb, &recs);
1.4 kristaps 534: }
1.3 kristaps 535:
1.5 kristaps 536: out:
1.38 schwarze 537: if (mdb.db)
538: (*mdb.db->close)(mdb.db);
539: if (mdb.idx)
540: (*mdb.idx->close)(mdb.idx);
1.3 kristaps 541: if (hash)
542: (*hash->close)(hash);
543: if (mp)
544: mparse_free(mp);
545:
1.10 kristaps 546: manpath_free(&dirs);
1.4 kristaps 547: ofile_free(of);
1.3 kristaps 548: free(buf.cp);
549: free(dbuf.cp);
1.38 schwarze 550: free(recs.stack);
1.3 kristaps 551:
1.5 kristaps 552: return(MANDOCLEVEL_OK);
1.38 schwarze 553:
554: usage:
555: fprintf(stderr,
556: "usage: %s [-avvv] [-C file] | dir ... | -t file ...\n"
557: " -d dir [file ...] | "
558: "-u dir [file ...]\n",
559: progname);
560:
561: return((int)MANDOCLEVEL_BADARG);
1.3 kristaps 562: }
563:
564: void
565: index_merge(const struct of *of, struct mparse *mp,
1.16 schwarze 566: struct buf *dbuf, struct buf *buf, DB *hash,
1.38 schwarze 567: struct mdb *mdb, struct recs *recs)
1.3 kristaps 568: {
569: recno_t rec;
1.38 schwarze 570: int ch, skip;
1.3 kristaps 571: DBT key, val;
572: struct mdoc *mdoc;
573: struct man *man;
1.38 schwarze 574: const char *fn, *msec, *march, *mtitle;
1.37 schwarze 575: uint64_t mask;
1.3 kristaps 576: size_t sv;
577: unsigned seq;
1.39 schwarze 578: uint64_t vbuf[2];
1.36 kristaps 579: char type;
1.3 kristaps 580:
1.38 schwarze 581: rec = 0;
582: for (of = of->first; of; of = of->next) {
1.3 kristaps 583: fn = of->fname;
1.14 schwarze 584:
585: /*
1.33 schwarze 586: * Try interpreting the file as mdoc(7) or man(7)
587: * source code, unless it is already known to be
588: * formatted. Fall back to formatted mode.
1.14 schwarze 589: */
590:
1.1 kristaps 591: mparse_reset(mp);
1.14 schwarze 592: mdoc = NULL;
593: man = NULL;
1.1 kristaps 594:
1.14 schwarze 595: if ((MANDOC_SRC & of->src_form ||
596: ! (MANDOC_FORM & of->src_form)) &&
597: MANDOCLEVEL_FATAL > mparse_readfd(mp, -1, fn))
598: mparse_result(mp, &mdoc, &man);
599:
600: if (NULL != mdoc) {
601: msec = mdoc_meta(mdoc)->msec;
1.38 schwarze 602: march = mdoc_meta(mdoc)->arch;
603: if (NULL == march)
604: march = "";
1.14 schwarze 605: mtitle = mdoc_meta(mdoc)->title;
606: } else if (NULL != man) {
607: msec = man_meta(man)->msec;
1.38 schwarze 608: march = "";
1.14 schwarze 609: mtitle = man_meta(man)->title;
610: } else {
611: msec = of->sec;
1.38 schwarze 612: march = of->arch;
1.14 schwarze 613: mtitle = of->title;
1.1 kristaps 614: }
615:
1.12 schwarze 616: /*
617: * By default, skip a file if the manual section
1.42 ! schwarze 618: * given in the file disagrees with the directory
! 619: * where the file is located.
1.12 schwarze 620: */
621:
1.38 schwarze 622: skip = 0;
623: assert(of->sec);
624: assert(msec);
625: if (strcasecmp(msec, of->sec)) {
626: if (warnings)
627: fprintf(stderr, "%s: "
628: "section \"%s\" manual "
629: "in \"%s\" directory\n",
630: fn, msec, of->sec);
631: skip = 1;
632: }
633:
1.42 ! schwarze 634: /*
! 635: * Manual page directories exist for each kernel
! 636: * architecture as returned by machine(1).
! 637: * However, many manuals only depend on the
! 638: * application architecture as returned by arch(1).
! 639: * For example, some (2/ARM) manuals are shared
! 640: * across the "armish" and "zaurus" kernel
! 641: * architectures.
! 642: * A few manuals are even shared across completely
! 643: * different architectures, for example fdformat(1)
! 644: * on amd64, i386, sparc, and sparc64.
! 645: * Thus, warn about architecture mismatches,
! 646: * but don't skip manuals for this reason.
! 647: */
! 648:
1.38 schwarze 649: assert(of->arch);
650: assert(march);
651: if (strcasecmp(march, of->arch)) {
652: if (warnings)
653: fprintf(stderr, "%s: "
654: "architecture \"%s\" manual "
655: "in \"%s\" directory\n",
656: fn, march, of->arch);
1.42 ! schwarze 657: march = of->arch;
1.12 schwarze 658: }
659:
1.38 schwarze 660: /*
1.12 schwarze 661: * By default, skip a file if the title given
662: * in the file disagrees with the file name.
663: * If both agree, use the file name as the title,
664: * because the one in the file usually is all caps.
665: */
666:
667: assert(of->title);
668: assert(mtitle);
1.38 schwarze 669: if (strcasecmp(mtitle, of->title)) {
670: if (warnings)
671: fprintf(stderr, "%s: "
672: "title \"%s\" in file "
673: "but \"%s\" in filename\n",
674: fn, mtitle, of->title);
675: skip = 1;
676: } else
677: mtitle = of->title;
1.12 schwarze 678:
1.38 schwarze 679: if (skip && !use_all)
1.12 schwarze 680: continue;
681:
1.38 schwarze 682: /*
1.1 kristaps 683: * The index record value consists of a nil-terminated
684: * filename, a nil-terminated manual section, and a
685: * nil-terminated description. Since the description
686: * may not be set, we set a sentinel to see if we're
687: * going to write a nil byte in its place.
688: */
689:
1.3 kristaps 690: dbuf->len = 0;
1.36 kristaps 691: type = mdoc ? 'd' : (man ? 'a' : 'c');
692: buf_appendb(dbuf, &type, 1);
1.3 kristaps 693: buf_appendb(dbuf, fn, strlen(fn) + 1);
694: buf_appendb(dbuf, msec, strlen(msec) + 1);
695: buf_appendb(dbuf, mtitle, strlen(mtitle) + 1);
1.38 schwarze 696: buf_appendb(dbuf, march, strlen(march) + 1);
1.1 kristaps 697:
1.3 kristaps 698: sv = dbuf->len;
1.1 kristaps 699:
1.33 schwarze 700: /*
701: * Collect keyword/mask pairs.
702: * Each pair will become a new btree node.
703: */
1.1 kristaps 704:
1.33 schwarze 705: hash_reset(&hash);
1.1 kristaps 706: if (mdoc)
1.3 kristaps 707: pmdoc_node(hash, buf, dbuf,
1.1 kristaps 708: mdoc_node(mdoc), mdoc_meta(mdoc));
1.14 schwarze 709: else if (man)
1.3 kristaps 710: pman_node(hash, buf, dbuf, man_node(man));
1.14 schwarze 711: else
712: pformatted(hash, buf, dbuf, of);
1.1 kristaps 713:
1.38 schwarze 714: /* Test mode, do not access any database. */
715:
716: if (NULL == mdb->db || NULL == mdb->idx)
717: continue;
718:
1.1 kristaps 719: /*
1.33 schwarze 720: * Reclaim an empty index record, if available.
721: * Use its record number for all new btree nodes.
1.1 kristaps 722: */
723:
1.38 schwarze 724: if (recs->cur > 0) {
725: recs->cur--;
726: rec = recs->stack[(int)recs->cur];
727: } else if (recs->last > 0) {
728: rec = recs->last;
729: recs->last = 0;
1.33 schwarze 730: } else
731: rec++;
1.39 schwarze 732: vbuf[1] = htobe64(rec);
1.33 schwarze 733:
734: /*
735: * Copy from the in-memory hashtable of pending
736: * keyword/mask pairs into the database.
737: */
738:
1.1 kristaps 739: seq = R_FIRST;
740: while (0 == (ch = (*hash->seq)(hash, &key, &val, seq))) {
741: seq = R_NEXT;
1.37 schwarze 742: assert(sizeof(uint64_t) == val.size);
743: memcpy(&mask, val.data, val.size);
1.39 schwarze 744: vbuf[0] = htobe64(mask);
745: val.size = sizeof(vbuf);
1.9 kristaps 746: val.data = &vbuf;
1.38 schwarze 747: dbt_put(mdb->db, mdb->dbn, &key, &val);
1.1 kristaps 748: }
749: if (ch < 0) {
750: perror("hash");
751: exit((int)MANDOCLEVEL_SYSERR);
752: }
1.38 schwarze 753:
1.1 kristaps 754: /*
755: * Apply to the index. If we haven't had a description
756: * set, put an empty one in now.
757: */
758:
1.3 kristaps 759: if (dbuf->len == sv)
760: buf_appendb(dbuf, "", 1);
1.1 kristaps 761:
762: key.data = &rec;
763: key.size = sizeof(recno_t);
764:
1.3 kristaps 765: val.data = dbuf->cp;
766: val.size = dbuf->len;
1.1 kristaps 767:
1.5 kristaps 768: if (verb)
1.38 schwarze 769: printf("%s: adding to index\n", fn);
1.18 kristaps 770:
1.38 schwarze 771: dbt_put(mdb->idx, mdb->idxn, &key, &val);
1.3 kristaps 772: }
773: }
774:
775: /*
776: * Scan through all entries in the index file `idx' and prune those
777: * entries in `ofile'.
778: * Pruning consists of removing from `db', then invalidating the entry
779: * in `idx' (zeroing its value size).
780: */
781: static void
1.38 schwarze 782: index_prune(const struct of *ofile, struct mdb *mdb, struct recs *recs)
1.3 kristaps 783: {
784: const struct of *of;
1.36 kristaps 785: const char *fn;
1.39 schwarze 786: uint64_t vbuf[2];
1.3 kristaps 787: unsigned seq, sseq;
788: DBT key, val;
789: int ch;
790:
1.38 schwarze 791: recs->cur = 0;
1.3 kristaps 792: seq = R_FIRST;
1.38 schwarze 793: while (0 == (ch = (*mdb->idx->seq)(mdb->idx, &key, &val, seq))) {
1.3 kristaps 794: seq = R_NEXT;
1.37 schwarze 795: assert(sizeof(recno_t) == key.size);
1.38 schwarze 796: memcpy(&recs->last, key.data, key.size);
1.18 kristaps 797:
798: /* Deleted records are zero-sized. Skip them. */
799:
800: if (0 == val.size)
801: goto cont;
802:
803: /*
804: * Make sure we're sane.
805: * Read past our mdoc/man/cat type to the next string,
806: * then make sure it's bounded by a NUL.
807: * Failing any of these, we go into our error handler.
808: */
809:
1.36 kristaps 810: fn = (char *)val.data + 1;
811: if (NULL == memchr(fn, '\0', val.size - 1))
1.18 kristaps 812: break;
813:
1.38 schwarze 814: /*
1.18 kristaps 815: * Search for the file in those we care about.
816: * XXX: build this into a tree. Too slow.
817: */
1.3 kristaps 818:
1.38 schwarze 819: for (of = ofile->first; of; of = of->next)
1.3 kristaps 820: if (0 == strcmp(fn, of->fname))
821: break;
822:
823: if (NULL == of)
824: continue;
825:
1.18 kristaps 826: /*
827: * Search through the keyword database, throwing out all
828: * references to our file.
829: */
830:
1.3 kristaps 831: sseq = R_FIRST;
1.38 schwarze 832: while (0 == (ch = (*mdb->db->seq)(mdb->db,
833: &key, &val, sseq))) {
1.3 kristaps 834: sseq = R_NEXT;
1.39 schwarze 835: if (sizeof(vbuf) != val.size)
1.18 kristaps 836: break;
837:
1.39 schwarze 838: memcpy(vbuf, val.data, val.size);
839: if (recs->last != betoh64(vbuf[1]))
1.3 kristaps 840: continue;
1.18 kristaps 841:
1.38 schwarze 842: if ((ch = (*mdb->db->del)(mdb->db,
843: &key, R_CURSOR)) < 0)
1.3 kristaps 844: break;
845: }
1.18 kristaps 846:
1.3 kristaps 847: if (ch < 0) {
1.38 schwarze 848: perror(mdb->dbn);
1.3 kristaps 849: exit((int)MANDOCLEVEL_SYSERR);
1.18 kristaps 850: } else if (1 != ch) {
1.38 schwarze 851: fprintf(stderr, "%s: corrupt database\n",
852: mdb->dbn);
1.18 kristaps 853: exit((int)MANDOCLEVEL_SYSERR);
1.3 kristaps 854: }
1.1 kristaps 855:
1.5 kristaps 856: if (verb)
1.38 schwarze 857: printf("%s: deleting from index\n", fn);
1.1 kristaps 858:
1.3 kristaps 859: val.size = 0;
1.38 schwarze 860: ch = (*mdb->idx->put)(mdb->idx, &key, &val, R_CURSOR);
1.1 kristaps 861:
1.18 kristaps 862: if (ch < 0)
863: break;
864: cont:
1.38 schwarze 865: if (recs->cur >= recs->size) {
866: recs->size += MANDOC_SLOP;
867: recs->stack = mandoc_realloc(recs->stack,
868: recs->size * sizeof(recno_t));
1.3 kristaps 869: }
1.1 kristaps 870:
1.38 schwarze 871: recs->stack[(int)recs->cur] = recs->last;
872: recs->cur++;
1.3 kristaps 873: }
1.18 kristaps 874:
875: if (ch < 0) {
1.38 schwarze 876: perror(mdb->idxn);
1.18 kristaps 877: exit((int)MANDOCLEVEL_SYSERR);
878: } else if (1 != ch) {
1.38 schwarze 879: fprintf(stderr, "%s: corrupt index\n", mdb->idxn);
1.18 kristaps 880: exit((int)MANDOCLEVEL_SYSERR);
881: }
882:
1.38 schwarze 883: recs->last++;
1.1 kristaps 884: }
885:
886: /*
887: * Grow the buffer (if necessary) and copy in a binary string.
888: */
889: static void
890: buf_appendb(struct buf *buf, const void *cp, size_t sz)
891: {
892:
893: /* Overshoot by MANDOC_BUFSZ. */
894:
895: while (buf->len + sz >= buf->size) {
896: buf->size = buf->len + sz + MANDOC_BUFSZ;
897: buf->cp = mandoc_realloc(buf->cp, buf->size);
898: }
899:
900: memcpy(buf->cp + (int)buf->len, cp, sz);
901: buf->len += sz;
902: }
903:
904: /*
905: * Append a nil-terminated string to the buffer.
906: * This can be invoked multiple times.
907: * The buffer string will be nil-terminated.
908: * If invoked multiple times, a space is put between strings.
909: */
910: static void
911: buf_append(struct buf *buf, const char *cp)
912: {
913: size_t sz;
914:
915: if (0 == (sz = strlen(cp)))
916: return;
917:
918: if (buf->len)
919: buf->cp[(int)buf->len - 1] = ' ';
920:
921: buf_appendb(buf, cp, sz + 1);
922: }
923:
924: /*
925: * Recursively add all text from a given node.
926: * This is optimised for general mdoc nodes in this context, which do
927: * not consist of subexpressions and having a recursive call for n->next
928: * would be wasteful.
929: * The "f" variable should be 0 unless called from pmdoc_Nd for the
930: * description buffer, which does not start at the beginning of the
931: * buffer.
932: */
933: static void
934: buf_appendmdoc(struct buf *buf, const struct mdoc_node *n, int f)
935: {
936:
937: for ( ; n; n = n->next) {
938: if (n->child)
939: buf_appendmdoc(buf, n->child, f);
940:
941: if (MDOC_TEXT == n->type && f) {
942: f = 0;
943: buf_appendb(buf, n->string,
944: strlen(n->string) + 1);
945: } else if (MDOC_TEXT == n->type)
946: buf_append(buf, n->string);
947:
948: }
949: }
950:
951: static void
952: hash_reset(DB **db)
953: {
954: DB *hash;
955:
956: if (NULL != (hash = *db))
957: (*hash->close)(hash);
958:
1.5 kristaps 959: *db = dbopen(NULL, O_CREAT|O_RDWR, 0644, DB_HASH, NULL);
1.1 kristaps 960: if (NULL == *db) {
961: perror("hash");
962: exit((int)MANDOCLEVEL_SYSERR);
963: }
964: }
965:
966: /* ARGSUSED */
1.25 schwarze 967: static int
968: pmdoc_head(MDOC_ARGS)
969: {
970:
971: return(MDOC_HEAD == n->type);
972: }
973:
974: /* ARGSUSED */
975: static int
976: pmdoc_body(MDOC_ARGS)
977: {
978:
979: return(MDOC_BODY == n->type);
980: }
981:
982: /* ARGSUSED */
983: static int
1.1 kristaps 984: pmdoc_Fd(MDOC_ARGS)
985: {
986: const char *start, *end;
987: size_t sz;
1.25 schwarze 988:
1.1 kristaps 989: if (SEC_SYNOPSIS != n->sec)
1.25 schwarze 990: return(0);
1.1 kristaps 991: if (NULL == (n = n->child) || MDOC_TEXT != n->type)
1.25 schwarze 992: return(0);
1.1 kristaps 993:
994: /*
995: * Only consider those `Fd' macro fields that begin with an
996: * "inclusion" token (versus, e.g., #define).
997: */
998: if (strcmp("#include", n->string))
1.25 schwarze 999: return(0);
1.1 kristaps 1000:
1001: if (NULL == (n = n->next) || MDOC_TEXT != n->type)
1.25 schwarze 1002: return(0);
1.1 kristaps 1003:
1004: /*
1005: * Strip away the enclosing angle brackets and make sure we're
1006: * not zero-length.
1007: */
1008:
1009: start = n->string;
1010: if ('<' == *start || '"' == *start)
1011: start++;
1012:
1013: if (0 == (sz = strlen(start)))
1.25 schwarze 1014: return(0);
1.1 kristaps 1015:
1016: end = &start[(int)sz - 1];
1017: if ('>' == *end || '"' == *end)
1018: end--;
1019:
1020: assert(end >= start);
1021:
1022: buf_appendb(buf, start, (size_t)(end - start + 1));
1023: buf_appendb(buf, "", 1);
1.25 schwarze 1024: return(1);
1.1 kristaps 1025: }
1026:
1027: /* ARGSUSED */
1.25 schwarze 1028: static int
1029: pmdoc_In(MDOC_ARGS)
1.1 kristaps 1030: {
1031:
1032: if (NULL == n->child || MDOC_TEXT != n->child->type)
1.25 schwarze 1033: return(0);
1.1 kristaps 1034:
1035: buf_append(buf, n->child->string);
1.25 schwarze 1036: return(1);
1.1 kristaps 1037: }
1038:
1039: /* ARGSUSED */
1.25 schwarze 1040: static int
1.1 kristaps 1041: pmdoc_Fn(MDOC_ARGS)
1042: {
1.25 schwarze 1043: struct mdoc_node *nn;
1.1 kristaps 1044: const char *cp;
1045:
1.25 schwarze 1046: nn = n->child;
1047:
1048: if (NULL == nn || MDOC_TEXT != nn->type)
1049: return(0);
1050:
1051: /* .Fn "struct type *name" "char *arg" */
1.1 kristaps 1052:
1.25 schwarze 1053: cp = strrchr(nn->string, ' ');
1.1 kristaps 1054: if (NULL == cp)
1.25 schwarze 1055: cp = nn->string;
1.1 kristaps 1056:
1057: /* Strip away pointer symbol. */
1058:
1059: while ('*' == *cp)
1060: cp++;
1061:
1.25 schwarze 1062: /* Store the function name. */
1063:
1.1 kristaps 1064: buf_append(buf, cp);
1.8 schwarze 1065: hash_put(hash, buf, TYPE_Fn);
1.25 schwarze 1066:
1067: /* Store the function type. */
1068:
1069: if (nn->string < cp) {
1070: buf->len = 0;
1071: buf_appendb(buf, nn->string, cp - nn->string);
1072: buf_appendb(buf, "", 1);
1073: hash_put(hash, buf, TYPE_Ft);
1074: }
1075:
1076: /* Store the arguments. */
1077:
1078: for (nn = nn->next; nn; nn = nn->next) {
1079: if (MDOC_TEXT != nn->type)
1080: continue;
1081: buf->len = 0;
1082: buf_append(buf, nn->string);
1083: hash_put(hash, buf, TYPE_Fa);
1084: }
1085:
1086: return(0);
1.1 kristaps 1087: }
1088:
1089: /* ARGSUSED */
1.25 schwarze 1090: static int
1.1 kristaps 1091: pmdoc_St(MDOC_ARGS)
1092: {
1.25 schwarze 1093:
1.1 kristaps 1094: if (NULL == n->child || MDOC_TEXT != n->child->type)
1.25 schwarze 1095: return(0);
1.1 kristaps 1096:
1097: buf_append(buf, n->child->string);
1.25 schwarze 1098: return(1);
1.1 kristaps 1099: }
1100:
1101: /* ARGSUSED */
1.25 schwarze 1102: static int
1.1 kristaps 1103: pmdoc_Xr(MDOC_ARGS)
1104: {
1105:
1106: if (NULL == (n = n->child))
1.25 schwarze 1107: return(0);
1.1 kristaps 1108:
1109: buf_appendb(buf, n->string, strlen(n->string));
1110:
1111: if (NULL != (n = n->next)) {
1112: buf_appendb(buf, ".", 1);
1113: buf_appendb(buf, n->string, strlen(n->string) + 1);
1114: } else
1115: buf_appendb(buf, ".", 2);
1116:
1.25 schwarze 1117: return(1);
1.1 kristaps 1118: }
1119:
1120: /* ARGSUSED */
1.25 schwarze 1121: static int
1.1 kristaps 1122: pmdoc_Nd(MDOC_ARGS)
1123: {
1124:
1125: if (MDOC_BODY != n->type)
1.25 schwarze 1126: return(0);
1.1 kristaps 1127:
1128: buf_appendmdoc(dbuf, n->child, 1);
1.25 schwarze 1129: return(1);
1.1 kristaps 1130: }
1131:
1132: /* ARGSUSED */
1.25 schwarze 1133: static int
1134: pmdoc_Nm(MDOC_ARGS)
1.1 kristaps 1135: {
1136:
1.25 schwarze 1137: if (SEC_NAME == n->sec)
1138: return(1);
1139: else if (SEC_SYNOPSIS != n->sec || MDOC_HEAD != n->type)
1140: return(0);
1.1 kristaps 1141:
1.25 schwarze 1142: if (NULL == n->child)
1143: buf_append(buf, m->name);
1.1 kristaps 1144:
1.25 schwarze 1145: return(1);
1.1 kristaps 1146: }
1147:
1148: /* ARGSUSED */
1.25 schwarze 1149: static int
1150: pmdoc_Sh(MDOC_ARGS)
1.1 kristaps 1151: {
1152:
1.25 schwarze 1153: return(SEC_CUSTOM == n->sec && MDOC_HEAD == n->type);
1.1 kristaps 1154: }
1155:
1156: static void
1.9 kristaps 1157: hash_put(DB *db, const struct buf *buf, uint64_t mask)
1.1 kristaps 1158: {
1.37 schwarze 1159: uint64_t oldmask;
1.1 kristaps 1160: DBT key, val;
1161: int rc;
1162:
1163: if (buf->len < 2)
1164: return;
1165:
1166: key.data = buf->cp;
1167: key.size = buf->len;
1168:
1169: if ((rc = (*db->get)(db, &key, &val, 0)) < 0) {
1170: perror("hash");
1171: exit((int)MANDOCLEVEL_SYSERR);
1.37 schwarze 1172: } else if (0 == rc) {
1173: assert(sizeof(uint64_t) == val.size);
1174: memcpy(&oldmask, val.data, val.size);
1175: mask |= oldmask;
1176: }
1.1 kristaps 1177:
1178: val.data = &mask;
1.9 kristaps 1179: val.size = sizeof(uint64_t);
1.1 kristaps 1180:
1181: if ((rc = (*db->put)(db, &key, &val, 0)) < 0) {
1182: perror("hash");
1183: exit((int)MANDOCLEVEL_SYSERR);
1184: }
1185: }
1186:
1187: static void
1188: dbt_put(DB *db, const char *dbn, DBT *key, DBT *val)
1189: {
1190:
1191: assert(key->size);
1192: assert(val->size);
1193:
1194: if (0 == (*db->put)(db, key, val, 0))
1195: return;
1196:
1197: perror(dbn);
1198: exit((int)MANDOCLEVEL_SYSERR);
1199: /* NOTREACHED */
1200: }
1201:
1202: /*
1203: * Call out to per-macro handlers after clearing the persistent database
1204: * key. If the macro sets the database key, flush it to the database.
1205: */
1206: static void
1207: pmdoc_node(MDOC_ARGS)
1208: {
1209:
1210: if (NULL == n)
1211: return;
1212:
1213: switch (n->type) {
1214: case (MDOC_HEAD):
1215: /* FALLTHROUGH */
1216: case (MDOC_BODY):
1217: /* FALLTHROUGH */
1218: case (MDOC_TAIL):
1219: /* FALLTHROUGH */
1220: case (MDOC_BLOCK):
1221: /* FALLTHROUGH */
1222: case (MDOC_ELEM):
1.25 schwarze 1223: buf->len = 0;
1224:
1225: /*
1226: * Both NULL handlers and handlers returning true
1227: * request using the data. Only skip the element
1228: * when the handler returns false.
1229: */
1230:
1231: if (NULL != mdocs[n->tok].fp &&
1232: 0 == (*mdocs[n->tok].fp)(hash, buf, dbuf, n, m))
1.1 kristaps 1233: break;
1234:
1.25 schwarze 1235: /*
1236: * For many macros, use the text from all children.
1237: * Set zero flags for macros not needing this.
1238: * In that case, the handler must fill the buffer.
1239: */
1240:
1241: if (MDOCF_CHILD & mdocs[n->tok].flags)
1242: buf_appendmdoc(buf, n->child, 0);
1243:
1244: /*
1245: * Cover the most common case:
1246: * Automatically stage one string per element.
1247: * Set a zero mask for macros not needing this.
1248: * Additional staging can be done in the handler.
1249: */
1250:
1251: if (mdocs[n->tok].mask)
1252: hash_put(hash, buf, mdocs[n->tok].mask);
1.1 kristaps 1253: break;
1254: default:
1255: break;
1256: }
1257:
1258: pmdoc_node(hash, buf, dbuf, n->child, m);
1259: pmdoc_node(hash, buf, dbuf, n->next, m);
1260: }
1261:
1262: static int
1263: pman_node(MAN_ARGS)
1264: {
1265: const struct man_node *head, *body;
1266: const char *start, *sv;
1267: size_t sz;
1268:
1269: if (NULL == n)
1270: return(0);
1271:
1272: /*
1273: * We're only searching for one thing: the first text child in
1274: * the BODY of a NAME section. Since we don't keep track of
1275: * sections in -man, run some hoops to find out whether we're in
1276: * the correct section or not.
1277: */
1278:
1279: if (MAN_BODY == n->type && MAN_SH == n->tok) {
1280: body = n;
1281: assert(body->parent);
1282: if (NULL != (head = body->parent->head) &&
1283: 1 == head->nchild &&
1284: NULL != (head = (head->child)) &&
1285: MAN_TEXT == head->type &&
1286: 0 == strcmp(head->string, "NAME") &&
1287: NULL != (body = body->child) &&
1288: MAN_TEXT == body->type) {
1289:
1290: assert(body->string);
1291: start = sv = body->string;
1292:
1293: /*
1294: * Go through a special heuristic dance here.
1295: * This is why -man manuals are great!
1296: * (I'm being sarcastic: my eyes are bleeding.)
1297: * Conventionally, one or more manual names are
1298: * comma-specified prior to a whitespace, then a
1299: * dash, then a description. Try to puzzle out
1300: * the name parts here.
1301: */
1302:
1303: for ( ;; ) {
1304: sz = strcspn(start, " ,");
1305: if ('\0' == start[(int)sz])
1306: break;
1307:
1308: buf->len = 0;
1309: buf_appendb(buf, start, sz);
1310: buf_appendb(buf, "", 1);
1311:
1.8 schwarze 1312: hash_put(hash, buf, TYPE_Nm);
1.1 kristaps 1313:
1314: if (' ' == start[(int)sz]) {
1315: start += (int)sz + 1;
1316: break;
1317: }
1318:
1319: assert(',' == start[(int)sz]);
1320: start += (int)sz + 1;
1321: while (' ' == *start)
1322: start++;
1323: }
1324:
1325: buf->len = 0;
1326:
1327: if (sv == start) {
1328: buf_append(buf, start);
1329: return(1);
1330: }
1331:
1332: while (' ' == *start)
1333: start++;
1334:
1335: if (0 == strncmp(start, "-", 1))
1336: start += 1;
1337: else if (0 == strncmp(start, "\\-", 2))
1338: start += 2;
1339: else if (0 == strncmp(start, "\\(en", 4))
1340: start += 4;
1341: else if (0 == strncmp(start, "\\(em", 4))
1342: start += 4;
1343:
1344: while (' ' == *start)
1345: start++;
1346:
1347: sz = strlen(start) + 1;
1348: buf_appendb(dbuf, start, sz);
1349: buf_appendb(buf, start, sz);
1350:
1.8 schwarze 1351: hash_put(hash, buf, TYPE_Nd);
1.1 kristaps 1352: }
1353: }
1354:
1.7 schwarze 1355: for (n = n->child; n; n = n->next)
1356: if (pman_node(hash, buf, dbuf, n))
1357: return(1);
1.1 kristaps 1358:
1359: return(0);
1360: }
1361:
1.14 schwarze 1362: /*
1363: * Parse a formatted manual page.
1364: * By necessity, this involves rather crude guesswork.
1365: */
1366: static void
1367: pformatted(DB *hash, struct buf *buf, struct buf *dbuf,
1368: const struct of *of)
1369: {
1370: FILE *stream;
1371: char *line, *p;
1372: size_t len, plen;
1373:
1374: if (NULL == (stream = fopen(of->fname, "r"))) {
1.38 schwarze 1375: if (warnings)
1376: perror(of->fname);
1.14 schwarze 1377: return;
1378: }
1379:
1380: /*
1381: * Always use the title derived from the filename up front,
1382: * do not even try to find it in the file. This also makes
1383: * sure we don't end up with an orphan index record, even if
1384: * the file content turns out to be completely unintelligible.
1385: */
1386:
1387: buf->len = 0;
1388: buf_append(buf, of->title);
1389: hash_put(hash, buf, TYPE_Nm);
1390:
1.31 schwarze 1391: /* Skip to first blank line. */
1.14 schwarze 1392:
1.28 kristaps 1393: while (NULL != (line = fgetln(stream, &len)))
1.31 schwarze 1394: if ('\n' == *line)
1.28 kristaps 1395: break;
1396:
1.31 schwarze 1397: /*
1398: * Assume the first line that is not indented
1399: * is the first section header. Skip to it.
1.28 kristaps 1400: */
1401:
1402: while (NULL != (line = fgetln(stream, &len)))
1.31 schwarze 1403: if ('\n' != *line && ' ' != *line)
1.28 kristaps 1404: break;
1.14 schwarze 1405:
1406: /*
1.31 schwarze 1407: * If no page content can be found, or the input line
1408: * is already the next section header, or there is no
1409: * trailing newline, reuse the page title as the page
1410: * description.
1.14 schwarze 1411: */
1412:
1.28 kristaps 1413: line = fgetln(stream, &len);
1.31 schwarze 1414: if (NULL == line || ' ' != *line || '\n' != line[(int)len - 1]) {
1.38 schwarze 1415: if (warnings)
1416: fprintf(stderr, "%s: cannot find NAME section\n",
1417: of->fname);
1.14 schwarze 1418: buf_appendb(dbuf, buf->cp, buf->size);
1419: hash_put(hash, buf, TYPE_Nd);
1420: fclose(stream);
1421: return;
1422: }
1423:
1.28 kristaps 1424: line[(int)--len] = '\0';
1425:
1.31 schwarze 1426: /*
1427: * Skip to the first dash.
1.28 kristaps 1428: * Use the remaining line as the description (no more than 70
1429: * bytes).
1.14 schwarze 1430: */
1431:
1.30 kristaps 1432: if (NULL != (p = strstr(line, "- "))) {
1433: for (p += 2; ' ' == *p || '\b' == *p; p++)
1.28 kristaps 1434: /* Skip to next word. */ ;
1.38 schwarze 1435: } else {
1436: if (warnings)
1437: fprintf(stderr, "%s: no dash in title line\n",
1438: of->fname);
1.14 schwarze 1439: p = line;
1.38 schwarze 1440: }
1.28 kristaps 1441:
1442: if ((plen = strlen(p)) > 70) {
1443: plen = 70;
1444: p[plen] = '\0';
1.29 kristaps 1445: }
1446:
1447: /* Strip backspace-encoding from line. */
1448:
1449: while (NULL != (line = memchr(p, '\b', plen))) {
1450: len = line - p;
1451: if (0 == len) {
1452: memmove(line, line + 1, plen--);
1453: continue;
1454: }
1455: memmove(line - 1, line + 1, plen - len);
1456: plen -= 2;
1.14 schwarze 1457: }
1458:
1.28 kristaps 1459: buf_appendb(dbuf, p, plen + 1);
1.14 schwarze 1460: buf->len = 0;
1.28 kristaps 1461: buf_appendb(buf, p, plen + 1);
1.14 schwarze 1462: hash_put(hash, buf, TYPE_Nd);
1.28 kristaps 1463: fclose(stream);
1.14 schwarze 1464: }
1465:
1.5 kristaps 1466: static void
1.16 schwarze 1467: ofile_argbuild(int argc, char *argv[], struct of **of)
1.5 kristaps 1468: {
1.12 schwarze 1469: char buf[MAXPATHLEN];
1.41 kristaps 1470: const char *sec, *arch, *title;
1471: char *p;
1.14 schwarze 1472: int i, src_form;
1.5 kristaps 1473: struct of *nof;
1474:
1475: for (i = 0; i < argc; i++) {
1.12 schwarze 1476:
1477: /*
1478: * Try to infer the manual section, architecture and
1479: * page title from the path, assuming it looks like
1.14 schwarze 1480: * man*[/<arch>]/<title>.<section> or
1481: * cat<section>[/<arch>]/<title>.0
1.12 schwarze 1482: */
1483:
1484: if (strlcpy(buf, argv[i], sizeof(buf)) >= sizeof(buf)) {
1.38 schwarze 1485: fprintf(stderr, "%s: path too long\n", argv[i]);
1.12 schwarze 1486: continue;
1487: }
1.38 schwarze 1488: sec = arch = title = "";
1.14 schwarze 1489: src_form = 0;
1.12 schwarze 1490: p = strrchr(buf, '\0');
1491: while (p-- > buf) {
1.38 schwarze 1492: if ('\0' == *sec && '.' == *p) {
1.12 schwarze 1493: sec = p + 1;
1494: *p = '\0';
1.14 schwarze 1495: if ('0' == *sec)
1496: src_form |= MANDOC_FORM;
1497: else if ('1' <= *sec && '9' >= *sec)
1498: src_form |= MANDOC_SRC;
1.12 schwarze 1499: continue;
1500: }
1501: if ('/' != *p)
1502: continue;
1.38 schwarze 1503: if ('\0' == *title) {
1.12 schwarze 1504: title = p + 1;
1505: *p = '\0';
1506: continue;
1507: }
1.24 schwarze 1508: if (0 == strncmp("man", p + 1, 3))
1.14 schwarze 1509: src_form |= MANDOC_SRC;
1.24 schwarze 1510: else if (0 == strncmp("cat", p + 1, 3))
1.14 schwarze 1511: src_form |= MANDOC_FORM;
1.24 schwarze 1512: else
1.12 schwarze 1513: arch = p + 1;
1514: break;
1515: }
1.38 schwarze 1516: if ('\0' == *title) {
1517: if (warnings)
1518: fprintf(stderr,
1519: "%s: cannot deduce title "
1520: "from filename\n",
1521: argv[i]);
1.12 schwarze 1522: title = buf;
1.38 schwarze 1523: }
1.12 schwarze 1524:
1525: /*
1526: * Build the file structure.
1527: */
1528:
1.5 kristaps 1529: nof = mandoc_calloc(1, sizeof(struct of));
1.12 schwarze 1530: nof->fname = mandoc_strdup(argv[i]);
1.38 schwarze 1531: nof->sec = mandoc_strdup(sec);
1532: nof->arch = mandoc_strdup(arch);
1.12 schwarze 1533: nof->title = mandoc_strdup(title);
1.14 schwarze 1534: nof->src_form = src_form;
1.12 schwarze 1535:
1536: /*
1537: * Add the structure to the list.
1538: */
1539:
1.38 schwarze 1540: if (verb > 1)
1541: printf("%s: scheduling\n", argv[i]);
1.5 kristaps 1542: if (NULL == *of) {
1543: *of = nof;
1544: (*of)->first = nof;
1545: } else {
1546: nof->first = (*of)->first;
1547: (*of)->next = nof;
1548: *of = nof;
1549: }
1550: }
1551: }
1552:
1.4 kristaps 1553: /*
1554: * Recursively build up a list of files to parse.
1555: * We use this instead of ftw() and so on because I don't want global
1556: * variables hanging around.
1.40 schwarze 1557: * This ignores the whatis.db and whatis.index files, but assumes that
1.4 kristaps 1558: * everything else is a manual.
1559: * Pass in a pointer to a NULL structure for the first invocation.
1560: */
1.35 kristaps 1561: static void
1.12 schwarze 1562: ofile_dirbuild(const char *dir, const char* psec, const char *parch,
1.16 schwarze 1563: int p_src_form, struct of **of)
1.4 kristaps 1564: {
1.5 kristaps 1565: char buf[MAXPATHLEN];
1566: size_t sz;
1.4 kristaps 1567: DIR *d;
1.12 schwarze 1568: const char *fn, *sec, *arch;
1.14 schwarze 1569: char *p, *q, *suffix;
1.4 kristaps 1570: struct of *nof;
1571: struct dirent *dp;
1.14 schwarze 1572: int src_form;
1.4 kristaps 1573:
1574: if (NULL == (d = opendir(dir))) {
1.38 schwarze 1575: if (warnings)
1576: perror(dir);
1577: return;
1.4 kristaps 1578: }
1579:
1580: while (NULL != (dp = readdir(d))) {
1581: fn = dp->d_name;
1.12 schwarze 1582:
1583: if ('.' == *fn)
1584: continue;
1585:
1.14 schwarze 1586: src_form = p_src_form;
1587:
1.4 kristaps 1588: if (DT_DIR == dp->d_type) {
1.12 schwarze 1589: sec = psec;
1590: arch = parch;
1591:
1592: /*
1593: * By default, only use directories called:
1.14 schwarze 1594: * man<section>/[<arch>/] or
1595: * cat<section>/[<arch>/]
1.12 schwarze 1596: */
1597:
1.38 schwarze 1598: if ('\0' == *sec) {
1.14 schwarze 1599: if(0 == strncmp("man", fn, 3)) {
1600: src_form |= MANDOC_SRC;
1.12 schwarze 1601: sec = fn + 3;
1.14 schwarze 1602: } else if (0 == strncmp("cat", fn, 3)) {
1603: src_form |= MANDOC_FORM;
1604: sec = fn + 3;
1.38 schwarze 1605: } else {
1606: if (warnings) fprintf(stderr,
1607: "%s/%s: bad section\n",
1608: dir, fn);
1609: if (use_all)
1610: sec = fn;
1611: else
1612: continue;
1613: }
1614: } else if ('\0' == *arch) {
1615: if (NULL != strchr(fn, '.')) {
1616: if (warnings) fprintf(stderr,
1617: "%s/%s: bad architecture\n",
1618: dir, fn);
1619: if (0 == use_all)
1620: continue;
1621: }
1622: arch = fn;
1623: } else {
1624: if (warnings) fprintf(stderr, "%s/%s: "
1625: "excessive subdirectory\n", dir, fn);
1626: if (0 == use_all)
1.12 schwarze 1627: continue;
1.38 schwarze 1628: }
1.5 kristaps 1629:
1630: buf[0] = '\0';
1631: strlcat(buf, dir, MAXPATHLEN);
1632: strlcat(buf, "/", MAXPATHLEN);
1633: sz = strlcat(buf, fn, MAXPATHLEN);
1634:
1.12 schwarze 1635: if (MAXPATHLEN <= sz) {
1.38 schwarze 1636: if (warnings) fprintf(stderr, "%s/%s: "
1637: "path too long\n", dir, fn);
1638: continue;
1.12 schwarze 1639: }
1.38 schwarze 1640:
1641: if (verb > 1)
1642: printf("%s: scanning\n", buf);
1643:
1.35 kristaps 1644: ofile_dirbuild(buf, sec, arch, src_form, of);
1.38 schwarze 1645: continue;
1.35 kristaps 1646: }
1.12 schwarze 1647:
1.38 schwarze 1648: if (DT_REG != dp->d_type) {
1649: if (warnings)
1650: fprintf(stderr,
1651: "%s/%s: not a regular file\n",
1652: dir, fn);
1653: continue;
1654: }
1655: if (!strcmp(MANDOC_DB, fn) || !strcmp(MANDOC_IDX, fn))
1.12 schwarze 1656: continue;
1.38 schwarze 1657: if ('\0' == *psec) {
1658: if (warnings)
1659: fprintf(stderr,
1660: "%s/%s: file outside section\n",
1661: dir, fn);
1662: if (0 == use_all)
1663: continue;
1664: }
1.12 schwarze 1665:
1666: /*
1667: * By default, skip files where the file name suffix
1668: * does not agree with the section directory
1669: * they are located in.
1670: */
1671:
1672: suffix = strrchr(fn, '.');
1.38 schwarze 1673: if (NULL == suffix) {
1674: if (warnings)
1675: fprintf(stderr,
1676: "%s/%s: no filename suffix\n",
1677: dir, fn);
1678: if (0 == use_all)
1.5 kristaps 1679: continue;
1.38 schwarze 1680: } else if ((MANDOC_SRC & src_form &&
1681: strcmp(suffix + 1, psec)) ||
1.14 schwarze 1682: (MANDOC_FORM & src_form &&
1.38 schwarze 1683: strcmp(suffix + 1, "0"))) {
1684: if (warnings)
1685: fprintf(stderr,
1686: "%s/%s: wrong filename suffix\n",
1687: dir, fn);
1688: if (0 == use_all)
1689: continue;
1.14 schwarze 1690: if ('0' == suffix[1])
1691: src_form |= MANDOC_FORM;
1692: else if ('1' <= suffix[1] && '9' >= suffix[1])
1693: src_form |= MANDOC_SRC;
1694: }
1695:
1696: /*
1697: * Skip formatted manuals if a source version is
1698: * available. Ignore the age: it is very unlikely
1699: * that people install newer formatted base manuals
1700: * when they used to have source manuals before,
1701: * and in ports, old manuals get removed on update.
1702: */
1703: if (0 == use_all && MANDOC_FORM & src_form &&
1.38 schwarze 1704: '\0' != *psec) {
1.14 schwarze 1705: buf[0] = '\0';
1706: strlcat(buf, dir, MAXPATHLEN);
1707: p = strrchr(buf, '/');
1.38 schwarze 1708: if ('\0' != *parch && NULL != p)
1.32 schwarze 1709: for (p--; p > buf; p--)
1710: if ('/' == *p)
1711: break;
1.14 schwarze 1712: if (NULL == p)
1713: p = buf;
1714: else
1715: p++;
1716: if (0 == strncmp("cat", p, 3))
1717: memcpy(p, "man", 3);
1718: strlcat(buf, "/", MAXPATHLEN);
1719: sz = strlcat(buf, fn, MAXPATHLEN);
1720: if (sz >= MAXPATHLEN) {
1.38 schwarze 1721: if (warnings) fprintf(stderr,
1722: "%s/%s: path too long\n",
1723: dir, fn);
1.5 kristaps 1724: continue;
1.14 schwarze 1725: }
1726: q = strrchr(buf, '.');
1727: if (NULL != q && p < q++) {
1728: *q = '\0';
1729: sz = strlcat(buf, psec, MAXPATHLEN);
1730: if (sz >= MAXPATHLEN) {
1.38 schwarze 1731: if (warnings) fprintf(stderr,
1732: "%s/%s: path too long\n",
1733: dir, fn);
1.14 schwarze 1734: continue;
1735: }
1.35 kristaps 1736: if (0 == access(buf, R_OK))
1.14 schwarze 1737: continue;
1738: }
1.5 kristaps 1739: }
1.4 kristaps 1740:
1.38 schwarze 1741: buf[0] = '\0';
1.35 kristaps 1742: assert('.' == dir[0]);
1.38 schwarze 1743: if ('/' == dir[1]) {
1744: strlcat(buf, dir + 2, MAXPATHLEN);
1745: strlcat(buf, "/", MAXPATHLEN);
1746: }
1.6 schwarze 1747: sz = strlcat(buf, fn, MAXPATHLEN);
1.5 kristaps 1748: if (sz >= MAXPATHLEN) {
1.38 schwarze 1749: if (warnings) fprintf(stderr,
1750: "%s/%s: path too long\n", dir, fn);
1.14 schwarze 1751: continue;
1.5 kristaps 1752: }
1753:
1.4 kristaps 1754: nof = mandoc_calloc(1, sizeof(struct of));
1.5 kristaps 1755: nof->fname = mandoc_strdup(buf);
1.38 schwarze 1756: nof->sec = mandoc_strdup(psec);
1757: nof->arch = mandoc_strdup(parch);
1.14 schwarze 1758: nof->src_form = src_form;
1.12 schwarze 1759:
1760: /*
1761: * Remember the file name without the extension,
1762: * to be used as the page title in the database.
1763: */
1764:
1765: if (NULL != suffix)
1766: *suffix = '\0';
1767: nof->title = mandoc_strdup(fn);
1.5 kristaps 1768:
1.14 schwarze 1769: /*
1770: * Add the structure to the list.
1771: */
1772:
1.38 schwarze 1773: if (verb > 1)
1774: printf("%s: scheduling\n", buf);
1.41 kristaps 1775:
1.4 kristaps 1776: if (NULL == *of) {
1777: *of = nof;
1778: (*of)->first = nof;
1779: } else {
1.5 kristaps 1780: nof->first = (*of)->first;
1.4 kristaps 1781: (*of)->next = nof;
1782: *of = nof;
1783: }
1784: }
1785:
1.7 schwarze 1786: closedir(d);
1.4 kristaps 1787: }
1788:
1789: static void
1790: ofile_free(struct of *of)
1791: {
1792: struct of *nof;
1793:
1.41 kristaps 1794: if (NULL != of)
1795: of = of->first;
1796:
1797: while (NULL != of) {
1.4 kristaps 1798: nof = of->next;
1799: free(of->fname);
1.12 schwarze 1800: free(of->sec);
1801: free(of->arch);
1802: free(of->title);
1.4 kristaps 1803: free(of);
1804: of = nof;
1805: }
1.1 kristaps 1806: }
CVSweb