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