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