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