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