Annotation of mandoc/mandocdb.c, Revision 1.49.2.14
1.49.2.14! schwarze 1: /* $Id: mandocdb.c,v 1.49.2.13 2014/03/19 22:24:12 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;
987: recs->stack = mandoc_realloc(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.46 kristaps 1386: char *start, *sv, *title;
1387: size_t sz, titlesz;
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") &&
1407: NULL != (body = body->child) &&
1408: MAN_TEXT == body->type) {
1409:
1.46 kristaps 1410: title = NULL;
1411: titlesz = 0;
1412: /*
1413: * Suck the entire NAME section into memory.
1414: * Yes, we might run away.
1415: * But too many manuals have big, spread-out
1416: * NAME sections over many lines.
1417: */
1418: for ( ; NULL != body; body = body->next) {
1419: if (MAN_TEXT != body->type)
1420: break;
1421: if (0 == (sz = strlen(body->string)))
1422: continue;
1423: title = mandoc_realloc
1424: (title, titlesz + sz + 1);
1425: memcpy(title + titlesz, body->string, sz);
1426: titlesz += sz + 1;
1427: title[(int)titlesz - 1] = ' ';
1428: }
1429: if (NULL == title)
1430: return(0);
1431:
1432: title = mandoc_realloc(title, titlesz + 1);
1433: title[(int)titlesz] = '\0';
1434:
1435: /* Skip leading space. */
1436:
1437: sv = title;
1438: while (isspace((unsigned char)*sv))
1439: sv++;
1440:
1441: if (0 == (sz = strlen(sv))) {
1442: free(title);
1443: return(0);
1444: }
1445:
1446: /* Erase trailing space. */
1447:
1448: start = &sv[sz - 1];
1449: while (start > sv && isspace((unsigned char)*start))
1450: *start-- = '\0';
1451:
1452: if (start == sv) {
1453: free(title);
1454: return(0);
1455: }
1456:
1457: start = sv;
1.1 kristaps 1458:
1459: /*
1460: * Go through a special heuristic dance here.
1461: * This is why -man manuals are great!
1462: * (I'm being sarcastic: my eyes are bleeding.)
1463: * Conventionally, one or more manual names are
1464: * comma-specified prior to a whitespace, then a
1465: * dash, then a description. Try to puzzle out
1466: * the name parts here.
1467: */
1468:
1469: for ( ;; ) {
1470: sz = strcspn(start, " ,");
1471: if ('\0' == start[(int)sz])
1472: break;
1473:
1474: buf->len = 0;
1475: buf_appendb(buf, start, sz);
1476: buf_appendb(buf, "", 1);
1477:
1.8 schwarze 1478: hash_put(hash, buf, TYPE_Nm);
1.1 kristaps 1479:
1480: if (' ' == start[(int)sz]) {
1481: start += (int)sz + 1;
1482: break;
1483: }
1484:
1485: assert(',' == start[(int)sz]);
1486: start += (int)sz + 1;
1487: while (' ' == *start)
1488: start++;
1489: }
1490:
1491: buf->len = 0;
1492:
1493: if (sv == start) {
1494: buf_append(buf, start);
1.46 kristaps 1495: free(title);
1.1 kristaps 1496: return(1);
1497: }
1498:
1.46 kristaps 1499: while (isspace((unsigned char)*start))
1.1 kristaps 1500: start++;
1501:
1502: if (0 == strncmp(start, "-", 1))
1503: start += 1;
1.43 kristaps 1504: else if (0 == strncmp(start, "\\-\\-", 4))
1505: start += 4;
1.1 kristaps 1506: else if (0 == strncmp(start, "\\-", 2))
1507: start += 2;
1508: else if (0 == strncmp(start, "\\(en", 4))
1509: start += 4;
1510: else if (0 == strncmp(start, "\\(em", 4))
1511: start += 4;
1512:
1513: while (' ' == *start)
1514: start++;
1515:
1516: sz = strlen(start) + 1;
1517: buf_appendb(dbuf, start, sz);
1518: buf_appendb(buf, start, sz);
1519:
1.8 schwarze 1520: hash_put(hash, buf, TYPE_Nd);
1.46 kristaps 1521: free(title);
1.1 kristaps 1522: }
1523: }
1524:
1.7 schwarze 1525: for (n = n->child; n; n = n->next)
1526: if (pman_node(hash, buf, dbuf, n))
1527: return(1);
1.1 kristaps 1528:
1529: return(0);
1530: }
1531:
1.14 schwarze 1532: /*
1533: * Parse a formatted manual page.
1534: * By necessity, this involves rather crude guesswork.
1535: */
1536: static void
1.49.2.2 schwarze 1537: pformatted(DB *hash, struct buf *buf,
1538: struct buf *dbuf, const struct of *of)
1.14 schwarze 1539: {
1540: FILE *stream;
1.43 kristaps 1541: char *line, *p, *title;
1542: size_t len, plen, titlesz;
1.14 schwarze 1543:
1544: if (NULL == (stream = fopen(of->fname, "r"))) {
1.49.2.2 schwarze 1545: if (warnings)
1546: perror(of->fname);
1.14 schwarze 1547: return;
1548: }
1549:
1550: /*
1551: * Always use the title derived from the filename up front,
1552: * do not even try to find it in the file. This also makes
1553: * sure we don't end up with an orphan index record, even if
1554: * the file content turns out to be completely unintelligible.
1555: */
1556:
1557: buf->len = 0;
1558: buf_append(buf, of->title);
1559: hash_put(hash, buf, TYPE_Nm);
1560:
1.31 schwarze 1561: /* Skip to first blank line. */
1.14 schwarze 1562:
1.28 kristaps 1563: while (NULL != (line = fgetln(stream, &len)))
1.31 schwarze 1564: if ('\n' == *line)
1.28 kristaps 1565: break;
1566:
1.31 schwarze 1567: /*
1568: * Assume the first line that is not indented
1569: * is the first section header. Skip to it.
1.28 kristaps 1570: */
1571:
1572: while (NULL != (line = fgetln(stream, &len)))
1.31 schwarze 1573: if ('\n' != *line && ' ' != *line)
1.28 kristaps 1574: break;
1.43 kristaps 1575:
1576: /*
1577: * Read up until the next section into a buffer.
1578: * Strip the leading and trailing newline from each read line,
1579: * appending a trailing space.
1580: * Ignore empty (whitespace-only) lines.
1581: */
1582:
1583: titlesz = 0;
1584: title = NULL;
1585:
1586: while (NULL != (line = fgetln(stream, &len))) {
1587: if (' ' != *line || '\n' != line[(int)len - 1])
1588: break;
1589: while (len > 0 && isspace((unsigned char)*line)) {
1590: line++;
1591: len--;
1592: }
1593: if (1 == len)
1594: continue;
1595: title = mandoc_realloc(title, titlesz + len);
1596: memcpy(title + titlesz, line, len);
1597: titlesz += len;
1598: title[(int)titlesz - 1] = ' ';
1599: }
1600:
1.49.2.2 schwarze 1601:
1.14 schwarze 1602: /*
1.31 schwarze 1603: * If no page content can be found, or the input line
1604: * is already the next section header, or there is no
1605: * trailing newline, reuse the page title as the page
1606: * description.
1.14 schwarze 1607: */
1608:
1.43 kristaps 1609: if (NULL == title || '\0' == *title) {
1.49.2.2 schwarze 1610: if (warnings)
1611: fprintf(stderr, "%s: cannot find NAME section\n",
1612: of->fname);
1.14 schwarze 1613: buf_appendb(dbuf, buf->cp, buf->size);
1614: hash_put(hash, buf, TYPE_Nd);
1615: fclose(stream);
1.43 kristaps 1616: free(title);
1.14 schwarze 1617: return;
1618: }
1619:
1.43 kristaps 1620: title = mandoc_realloc(title, titlesz + 1);
1621: title[(int)titlesz] = '\0';
1.28 kristaps 1622:
1.31 schwarze 1623: /*
1624: * Skip to the first dash.
1.28 kristaps 1625: * Use the remaining line as the description (no more than 70
1626: * bytes).
1.14 schwarze 1627: */
1628:
1.43 kristaps 1629: if (NULL != (p = strstr(title, "- "))) {
1.30 kristaps 1630: for (p += 2; ' ' == *p || '\b' == *p; p++)
1.28 kristaps 1631: /* Skip to next word. */ ;
1.38 schwarze 1632: } else {
1.49.2.2 schwarze 1633: if (warnings)
1634: fprintf(stderr, "%s: no dash in title line\n",
1635: of->fname);
1.43 kristaps 1636: p = title;
1.38 schwarze 1637: }
1.28 kristaps 1638:
1.43 kristaps 1639: plen = strlen(p);
1.29 kristaps 1640:
1641: /* Strip backspace-encoding from line. */
1642:
1643: while (NULL != (line = memchr(p, '\b', plen))) {
1644: len = line - p;
1645: if (0 == len) {
1646: memmove(line, line + 1, plen--);
1647: continue;
1648: }
1649: memmove(line - 1, line + 1, plen - len);
1650: plen -= 2;
1.14 schwarze 1651: }
1652:
1.28 kristaps 1653: buf_appendb(dbuf, p, plen + 1);
1.14 schwarze 1654: buf->len = 0;
1.28 kristaps 1655: buf_appendb(buf, p, plen + 1);
1.14 schwarze 1656: hash_put(hash, buf, TYPE_Nd);
1.28 kristaps 1657: fclose(stream);
1.43 kristaps 1658: free(title);
1.14 schwarze 1659: }
1660:
1.5 kristaps 1661: static void
1.49.2.2 schwarze 1662: ofile_argbuild(int argc, char *argv[], struct of **of,
1663: const char *basedir)
1.5 kristaps 1664: {
1.49.2.1 schwarze 1665: char buf[PATH_MAX];
1.49.2.2 schwarze 1666: char pbuf[PATH_MAX];
1.41 kristaps 1667: const char *sec, *arch, *title;
1.49.2.2 schwarze 1668: char *relpath, *p;
1.14 schwarze 1669: int i, src_form;
1.5 kristaps 1670: struct of *nof;
1671:
1672: for (i = 0; i < argc; i++) {
1.49.2.2 schwarze 1673: if (NULL == (relpath = realpath(argv[i], pbuf))) {
1674: perror(argv[i]);
1675: continue;
1676: }
1677: if (NULL != basedir) {
1678: if (strstr(pbuf, basedir) != pbuf) {
1679: fprintf(stderr, "%s: file outside "
1680: "base directory %s\n",
1681: pbuf, basedir);
1682: continue;
1683: }
1684: relpath = pbuf + strlen(basedir);
1685: }
1.12 schwarze 1686:
1687: /*
1688: * Try to infer the manual section, architecture and
1689: * page title from the path, assuming it looks like
1.14 schwarze 1690: * man*[/<arch>]/<title>.<section> or
1691: * cat<section>[/<arch>]/<title>.0
1.12 schwarze 1692: */
1693:
1.49.2.2 schwarze 1694: if (strlcpy(buf, relpath, sizeof(buf)) >= sizeof(buf)) {
1695: fprintf(stderr, "%s: path too long\n", relpath);
1.12 schwarze 1696: continue;
1697: }
1.38 schwarze 1698: sec = arch = title = "";
1.14 schwarze 1699: src_form = 0;
1.12 schwarze 1700: p = strrchr(buf, '\0');
1701: while (p-- > buf) {
1.38 schwarze 1702: if ('\0' == *sec && '.' == *p) {
1.12 schwarze 1703: sec = p + 1;
1704: *p = '\0';
1.14 schwarze 1705: if ('0' == *sec)
1706: src_form |= MANDOC_FORM;
1707: else if ('1' <= *sec && '9' >= *sec)
1708: src_form |= MANDOC_SRC;
1.12 schwarze 1709: continue;
1710: }
1711: if ('/' != *p)
1712: continue;
1.38 schwarze 1713: if ('\0' == *title) {
1.12 schwarze 1714: title = p + 1;
1715: *p = '\0';
1716: continue;
1717: }
1.24 schwarze 1718: if (0 == strncmp("man", p + 1, 3))
1.14 schwarze 1719: src_form |= MANDOC_SRC;
1.24 schwarze 1720: else if (0 == strncmp("cat", p + 1, 3))
1.14 schwarze 1721: src_form |= MANDOC_FORM;
1.24 schwarze 1722: else
1.12 schwarze 1723: arch = p + 1;
1724: break;
1725: }
1.38 schwarze 1726: if ('\0' == *title) {
1.49.2.2 schwarze 1727: if (warnings)
1728: fprintf(stderr,
1729: "%s: cannot deduce title "
1730: "from filename\n",
1731: relpath);
1.12 schwarze 1732: title = buf;
1.38 schwarze 1733: }
1.12 schwarze 1734:
1735: /*
1736: * Build the file structure.
1737: */
1738:
1.5 kristaps 1739: nof = mandoc_calloc(1, sizeof(struct of));
1.49.2.2 schwarze 1740: nof->fname = mandoc_strdup(relpath);
1.38 schwarze 1741: nof->sec = mandoc_strdup(sec);
1742: nof->arch = mandoc_strdup(arch);
1.12 schwarze 1743: nof->title = mandoc_strdup(title);
1.14 schwarze 1744: nof->src_form = src_form;
1.12 schwarze 1745:
1746: /*
1747: * Add the structure to the list.
1748: */
1749:
1.5 kristaps 1750: if (NULL == *of) {
1751: *of = nof;
1752: (*of)->first = nof;
1753: } else {
1754: nof->first = (*of)->first;
1755: (*of)->next = nof;
1756: *of = nof;
1757: }
1758: }
1759: }
1760:
1.4 kristaps 1761: /*
1762: * Recursively build up a list of files to parse.
1763: * We use this instead of ftw() and so on because I don't want global
1764: * variables hanging around.
1.49.2.5 schwarze 1765: * This ignores the mandoc.db and mandoc.index files, but assumes that
1.4 kristaps 1766: * everything else is a manual.
1767: * Pass in a pointer to a NULL structure for the first invocation.
1768: */
1.35 kristaps 1769: static void
1.12 schwarze 1770: ofile_dirbuild(const char *dir, const char* psec, const char *parch,
1.49.2.2 schwarze 1771: int p_src_form, struct of **of)
1.4 kristaps 1772: {
1.49.2.1 schwarze 1773: char buf[PATH_MAX];
1.49.2.6 schwarze 1774: #if defined(__sun)
1775: struct stat sb;
1776: #endif
1.5 kristaps 1777: size_t sz;
1.4 kristaps 1778: DIR *d;
1.12 schwarze 1779: const char *fn, *sec, *arch;
1.14 schwarze 1780: char *p, *q, *suffix;
1.4 kristaps 1781: struct of *nof;
1782: struct dirent *dp;
1.14 schwarze 1783: int src_form;
1.4 kristaps 1784:
1785: if (NULL == (d = opendir(dir))) {
1.49.2.2 schwarze 1786: if (warnings)
1787: perror(dir);
1.38 schwarze 1788: return;
1.4 kristaps 1789: }
1790:
1791: while (NULL != (dp = readdir(d))) {
1792: fn = dp->d_name;
1.12 schwarze 1793:
1794: if ('.' == *fn)
1795: continue;
1796:
1.14 schwarze 1797: src_form = p_src_form;
1798:
1.49.2.6 schwarze 1799: #if defined(__sun)
1800: stat(dp->d_name, &sb);
1801: if (S_IFDIR & sb.st_mode) {
1802: #else
1.4 kristaps 1803: if (DT_DIR == dp->d_type) {
1.49.2.6 schwarze 1804: #endif
1.12 schwarze 1805: sec = psec;
1806: arch = parch;
1807:
1808: /*
1809: * By default, only use directories called:
1.14 schwarze 1810: * man<section>/[<arch>/] or
1811: * cat<section>/[<arch>/]
1.12 schwarze 1812: */
1813:
1.38 schwarze 1814: if ('\0' == *sec) {
1.14 schwarze 1815: if(0 == strncmp("man", fn, 3)) {
1816: src_form |= MANDOC_SRC;
1.12 schwarze 1817: sec = fn + 3;
1.14 schwarze 1818: } else if (0 == strncmp("cat", fn, 3)) {
1819: src_form |= MANDOC_FORM;
1820: sec = fn + 3;
1.38 schwarze 1821: } else {
1.49.2.2 schwarze 1822: if (warnings) fprintf(stderr,
1823: "%s/%s: bad section\n",
1824: dir, fn);
1.38 schwarze 1825: if (use_all)
1826: sec = fn;
1827: else
1828: continue;
1829: }
1830: } else if ('\0' == *arch) {
1831: if (NULL != strchr(fn, '.')) {
1.49.2.2 schwarze 1832: if (warnings) fprintf(stderr,
1833: "%s/%s: bad architecture\n",
1834: dir, fn);
1.38 schwarze 1835: if (0 == use_all)
1836: continue;
1837: }
1838: arch = fn;
1839: } else {
1.49.2.2 schwarze 1840: if (warnings) fprintf(stderr, "%s/%s: "
1841: "excessive subdirectory\n", dir, fn);
1.38 schwarze 1842: if (0 == use_all)
1.12 schwarze 1843: continue;
1.38 schwarze 1844: }
1.5 kristaps 1845:
1846: buf[0] = '\0';
1.49.2.1 schwarze 1847: strlcat(buf, dir, PATH_MAX);
1848: strlcat(buf, "/", PATH_MAX);
1849: sz = strlcat(buf, fn, PATH_MAX);
1.5 kristaps 1850:
1.49.2.1 schwarze 1851: if (PATH_MAX <= sz) {
1.49.2.2 schwarze 1852: if (warnings) fprintf(stderr, "%s/%s: "
1853: "path too long\n", dir, fn);
1.38 schwarze 1854: continue;
1.12 schwarze 1855: }
1.38 schwarze 1856:
1.49.2.2 schwarze 1857: ofile_dirbuild(buf, sec, arch, src_form, of);
1.38 schwarze 1858: continue;
1.35 kristaps 1859: }
1.12 schwarze 1860:
1.49.2.6 schwarze 1861: #if defined(__sun)
1862: if (0 == S_IFREG & sb.st_mode) {
1863: #else
1.38 schwarze 1864: if (DT_REG != dp->d_type) {
1.49.2.6 schwarze 1865: #endif
1.49.2.2 schwarze 1866: if (warnings)
1867: fprintf(stderr,
1868: "%s/%s: not a regular file\n",
1869: dir, fn);
1.38 schwarze 1870: continue;
1871: }
1872: if (!strcmp(MANDOC_DB, fn) || !strcmp(MANDOC_IDX, fn))
1.12 schwarze 1873: continue;
1.38 schwarze 1874: if ('\0' == *psec) {
1.49.2.2 schwarze 1875: if (warnings)
1876: fprintf(stderr,
1877: "%s/%s: file outside section\n",
1878: dir, fn);
1.38 schwarze 1879: if (0 == use_all)
1880: continue;
1881: }
1.12 schwarze 1882:
1883: /*
1884: * By default, skip files where the file name suffix
1885: * does not agree with the section directory
1886: * they are located in.
1887: */
1888:
1889: suffix = strrchr(fn, '.');
1.38 schwarze 1890: if (NULL == suffix) {
1.49.2.2 schwarze 1891: if (warnings)
1892: fprintf(stderr,
1893: "%s/%s: no filename suffix\n",
1894: dir, fn);
1.38 schwarze 1895: if (0 == use_all)
1.5 kristaps 1896: continue;
1.38 schwarze 1897: } else if ((MANDOC_SRC & src_form &&
1898: strcmp(suffix + 1, psec)) ||
1.14 schwarze 1899: (MANDOC_FORM & src_form &&
1.38 schwarze 1900: strcmp(suffix + 1, "0"))) {
1.49.2.2 schwarze 1901: if (warnings)
1902: fprintf(stderr,
1903: "%s/%s: wrong filename suffix\n",
1904: dir, fn);
1.38 schwarze 1905: if (0 == use_all)
1906: continue;
1.14 schwarze 1907: if ('0' == suffix[1])
1908: src_form |= MANDOC_FORM;
1909: else if ('1' <= suffix[1] && '9' >= suffix[1])
1910: src_form |= MANDOC_SRC;
1911: }
1912:
1913: /*
1914: * Skip formatted manuals if a source version is
1915: * available. Ignore the age: it is very unlikely
1916: * that people install newer formatted base manuals
1917: * when they used to have source manuals before,
1918: * and in ports, old manuals get removed on update.
1919: */
1920: if (0 == use_all && MANDOC_FORM & src_form &&
1.38 schwarze 1921: '\0' != *psec) {
1.14 schwarze 1922: buf[0] = '\0';
1.49.2.1 schwarze 1923: strlcat(buf, dir, PATH_MAX);
1.14 schwarze 1924: p = strrchr(buf, '/');
1.38 schwarze 1925: if ('\0' != *parch && NULL != p)
1.32 schwarze 1926: for (p--; p > buf; p--)
1927: if ('/' == *p)
1928: break;
1.14 schwarze 1929: if (NULL == p)
1930: p = buf;
1931: else
1932: p++;
1933: if (0 == strncmp("cat", p, 3))
1934: memcpy(p, "man", 3);
1.49.2.1 schwarze 1935: strlcat(buf, "/", PATH_MAX);
1936: sz = strlcat(buf, fn, PATH_MAX);
1937: if (sz >= PATH_MAX) {
1.49.2.2 schwarze 1938: if (warnings) fprintf(stderr,
1939: "%s/%s: path too long\n",
1940: dir, fn);
1.5 kristaps 1941: continue;
1.14 schwarze 1942: }
1943: q = strrchr(buf, '.');
1944: if (NULL != q && p < q++) {
1945: *q = '\0';
1.49.2.1 schwarze 1946: sz = strlcat(buf, psec, PATH_MAX);
1947: if (sz >= PATH_MAX) {
1.49.2.2 schwarze 1948: if (warnings) fprintf(stderr,
1949: "%s/%s: path too long\n",
1950: dir, fn);
1.14 schwarze 1951: continue;
1952: }
1.35 kristaps 1953: if (0 == access(buf, R_OK))
1.14 schwarze 1954: continue;
1955: }
1.5 kristaps 1956: }
1.4 kristaps 1957:
1.38 schwarze 1958: buf[0] = '\0';
1.35 kristaps 1959: assert('.' == dir[0]);
1.38 schwarze 1960: if ('/' == dir[1]) {
1.49.2.1 schwarze 1961: strlcat(buf, dir + 2, PATH_MAX);
1962: strlcat(buf, "/", PATH_MAX);
1.38 schwarze 1963: }
1.49.2.1 schwarze 1964: sz = strlcat(buf, fn, PATH_MAX);
1965: if (sz >= PATH_MAX) {
1.49.2.2 schwarze 1966: if (warnings) fprintf(stderr,
1967: "%s/%s: path too long\n", dir, fn);
1.14 schwarze 1968: continue;
1.5 kristaps 1969: }
1970:
1.4 kristaps 1971: nof = mandoc_calloc(1, sizeof(struct of));
1.5 kristaps 1972: nof->fname = mandoc_strdup(buf);
1.38 schwarze 1973: nof->sec = mandoc_strdup(psec);
1974: nof->arch = mandoc_strdup(parch);
1.14 schwarze 1975: nof->src_form = src_form;
1.12 schwarze 1976:
1977: /*
1978: * Remember the file name without the extension,
1979: * to be used as the page title in the database.
1980: */
1981:
1982: if (NULL != suffix)
1983: *suffix = '\0';
1984: nof->title = mandoc_strdup(fn);
1.5 kristaps 1985:
1.14 schwarze 1986: /*
1987: * Add the structure to the list.
1988: */
1.41 kristaps 1989:
1.4 kristaps 1990: if (NULL == *of) {
1991: *of = nof;
1992: (*of)->first = nof;
1993: } else {
1.5 kristaps 1994: nof->first = (*of)->first;
1.4 kristaps 1995: (*of)->next = nof;
1996: *of = nof;
1997: }
1998: }
1999:
1.7 schwarze 2000: closedir(d);
1.4 kristaps 2001: }
2002:
2003: static void
2004: ofile_free(struct of *of)
2005: {
2006: struct of *nof;
2007:
1.41 kristaps 2008: if (NULL != of)
2009: of = of->first;
2010:
2011: while (NULL != of) {
1.4 kristaps 2012: nof = of->next;
2013: free(of->fname);
1.12 schwarze 2014: free(of->sec);
2015: free(of->arch);
2016: free(of->title);
1.4 kristaps 2017: free(of);
2018: of = nof;
2019: }
1.1 kristaps 2020: }
CVSweb