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