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