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