Annotation of src/usr.bin/mandoc/mandocdb.c, Revision 1.21
1.21 ! schwarze 1: /* $Id: mandocdb.c,v 1.20 2011/12/08 02:14:00 schwarze Exp $ */
1.1 schwarze 2: /*
3: * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.10 schwarze 4: * Copyright (c) 2011 Ingo Schwarze <schwarze@openbsd.org>
1.1 schwarze 5: *
6: * Permission to use, copy, modify, and distribute this software for any
7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
9: *
10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17: */
18: #include <sys/param.h>
1.11 schwarze 19: #include <sys/types.h>
20: #include <sys/stat.h>
1.1 schwarze 21:
22: #include <assert.h>
1.2 schwarze 23: #include <dirent.h>
1.1 schwarze 24: #include <fcntl.h>
25: #include <getopt.h>
26: #include <stdio.h>
27: #include <stdint.h>
28: #include <stdlib.h>
29: #include <string.h>
1.14 schwarze 30: #include <unistd.h>
1.1 schwarze 31: #include <db.h>
32:
33: #include "man.h"
34: #include "mdoc.h"
35: #include "mandoc.h"
1.5 schwarze 36: #include "mandocdb.h"
1.10 schwarze 37: #include "manpath.h"
1.1 schwarze 38:
39: #define MANDOC_BUFSZ BUFSIZ
40: #define MANDOC_SLOP 1024
41:
1.11 schwarze 42: #define MANDOC_SRC 0x1
43: #define MANDOC_FORM 0x2
44:
1.2 schwarze 45: /* Tiny list for files. No need to bring in QUEUE. */
46:
47: struct of {
48: char *fname; /* heap-allocated */
1.6 schwarze 49: char *sec;
50: char *arch;
51: char *title;
1.11 schwarze 52: int src_form;
1.2 schwarze 53: struct of *next; /* NULL for last one */
54: struct of *first; /* first in list */
55: };
56:
1.1 schwarze 57: /* Buffer for storing growable data. */
58:
59: struct buf {
60: char *cp;
1.2 schwarze 61: size_t len; /* current length */
62: size_t size; /* total buffer size */
1.1 schwarze 63: };
64:
65: /* Operation we're going to perform. */
66:
67: enum op {
68: OP_NEW = 0, /* new database */
1.2 schwarze 69: OP_UPDATE, /* delete/add entries in existing database */
1.1 schwarze 70: OP_DELETE /* delete entries from existing database */
71: };
72:
73: #define MAN_ARGS DB *hash, \
74: struct buf *buf, \
75: struct buf *dbuf, \
76: const struct man_node *n
77: #define MDOC_ARGS DB *hash, \
78: struct buf *buf, \
79: struct buf *dbuf, \
80: const struct mdoc_node *n, \
81: const struct mdoc_meta *m
82:
83: static void buf_appendmdoc(struct buf *,
84: const struct mdoc_node *, int);
85: static void buf_append(struct buf *, const char *);
86: static void buf_appendb(struct buf *,
87: const void *, size_t);
88: static void dbt_put(DB *, const char *, DBT *, DBT *);
1.9 schwarze 89: static void hash_put(DB *, const struct buf *, uint64_t);
1.1 schwarze 90: static void hash_reset(DB **);
1.2 schwarze 91: static void index_merge(const struct of *, struct mparse *,
1.13 schwarze 92: struct buf *, struct buf *, DB *,
93: DB *, const char *, DB *, const char *,
1.2 schwarze 94: recno_t, const recno_t *, size_t);
95: static void index_prune(const struct of *, DB *,
96: const char *, DB *, const char *,
1.20 schwarze 97: recno_t *, recno_t **, size_t *,
98: size_t *);
1.13 schwarze 99: static void ofile_argbuild(int, char *[], struct of **);
1.6 schwarze 100: static int ofile_dirbuild(const char *, const char *,
1.13 schwarze 101: const char *, int, struct of **);
1.2 schwarze 102: static void ofile_free(struct of *);
1.11 schwarze 103: static void pformatted(DB *, struct buf *, struct buf *,
104: const struct of *);
1.1 schwarze 105: static int pman_node(MAN_ARGS);
106: static void pmdoc_node(MDOC_ARGS);
1.19 schwarze 107: static int pmdoc_head(MDOC_ARGS);
108: static int pmdoc_body(MDOC_ARGS);
109: static int pmdoc_Fd(MDOC_ARGS);
110: static int pmdoc_In(MDOC_ARGS);
111: static int pmdoc_Fn(MDOC_ARGS);
112: static int pmdoc_Nd(MDOC_ARGS);
113: static int pmdoc_Nm(MDOC_ARGS);
114: static int pmdoc_Sh(MDOC_ARGS);
115: static int pmdoc_St(MDOC_ARGS);
116: static int pmdoc_Xr(MDOC_ARGS);
1.1 schwarze 117: static void usage(void);
118:
1.19 schwarze 119: #define MDOCF_CHILD 0x01 /* Automatically index child nodes. */
1.1 schwarze 120:
1.19 schwarze 121: struct mdoc_handler {
122: int (*fp)(MDOC_ARGS); /* Optional handler. */
123: uint64_t mask; /* Set unless handler returns 0. */
124: int flags; /* For use by pmdoc_node. */
125: };
126:
127: static const struct mdoc_handler mdocs[MDOC_MAX] = {
128: { NULL, 0, 0 }, /* Ap */
129: { NULL, 0, 0 }, /* Dd */
130: { NULL, 0, 0 }, /* Dt */
131: { NULL, 0, 0 }, /* Os */
132: { pmdoc_Sh, TYPE_Sh, MDOCF_CHILD }, /* Sh */
133: { pmdoc_head, TYPE_Ss, MDOCF_CHILD }, /* Ss */
134: { NULL, 0, 0 }, /* Pp */
135: { NULL, 0, 0 }, /* D1 */
136: { NULL, 0, 0 }, /* Dl */
137: { NULL, 0, 0 }, /* Bd */
138: { NULL, 0, 0 }, /* Ed */
139: { NULL, 0, 0 }, /* Bl */
140: { NULL, 0, 0 }, /* El */
141: { NULL, 0, 0 }, /* It */
142: { NULL, 0, 0 }, /* Ad */
143: { NULL, TYPE_An, MDOCF_CHILD }, /* An */
144: { NULL, TYPE_Ar, MDOCF_CHILD }, /* Ar */
145: { NULL, TYPE_Cd, MDOCF_CHILD }, /* Cd */
146: { NULL, TYPE_Cm, MDOCF_CHILD }, /* Cm */
147: { NULL, TYPE_Dv, MDOCF_CHILD }, /* Dv */
148: { NULL, TYPE_Er, MDOCF_CHILD }, /* Er */
149: { NULL, TYPE_Ev, MDOCF_CHILD }, /* Ev */
150: { NULL, 0, 0 }, /* Ex */
151: { NULL, TYPE_Fa, MDOCF_CHILD }, /* Fa */
152: { pmdoc_Fd, TYPE_In, 0 }, /* Fd */
153: { NULL, TYPE_Fl, MDOCF_CHILD }, /* Fl */
154: { pmdoc_Fn, 0, 0 }, /* Fn */
155: { NULL, TYPE_Ft, MDOCF_CHILD }, /* Ft */
156: { NULL, TYPE_Ic, MDOCF_CHILD }, /* Ic */
157: { pmdoc_In, TYPE_In, 0 }, /* In */
158: { NULL, TYPE_Li, MDOCF_CHILD }, /* Li */
159: { pmdoc_Nd, TYPE_Nd, MDOCF_CHILD }, /* Nd */
160: { pmdoc_Nm, TYPE_Nm, MDOCF_CHILD }, /* Nm */
161: { NULL, 0, 0 }, /* Op */
162: { NULL, 0, 0 }, /* Ot */
163: { NULL, TYPE_Pa, MDOCF_CHILD }, /* Pa */
164: { NULL, 0, 0 }, /* Rv */
165: { pmdoc_St, TYPE_St, 0 }, /* St */
166: { NULL, TYPE_Va, MDOCF_CHILD }, /* Va */
167: { pmdoc_body, TYPE_Va, MDOCF_CHILD }, /* Vt */
168: { pmdoc_Xr, TYPE_Xr, 0 }, /* Xr */
169: { NULL, 0, 0 }, /* %A */
170: { NULL, 0, 0 }, /* %B */
171: { NULL, 0, 0 }, /* %D */
172: { NULL, 0, 0 }, /* %I */
173: { NULL, 0, 0 }, /* %J */
174: { NULL, 0, 0 }, /* %N */
175: { NULL, 0, 0 }, /* %O */
176: { NULL, 0, 0 }, /* %P */
177: { NULL, 0, 0 }, /* %R */
178: { NULL, 0, 0 }, /* %T */
179: { NULL, 0, 0 }, /* %V */
180: { NULL, 0, 0 }, /* Ac */
181: { NULL, 0, 0 }, /* Ao */
182: { NULL, 0, 0 }, /* Aq */
183: { NULL, TYPE_At, MDOCF_CHILD }, /* At */
184: { NULL, 0, 0 }, /* Bc */
185: { NULL, 0, 0 }, /* Bf */
186: { NULL, 0, 0 }, /* Bo */
187: { NULL, 0, 0 }, /* Bq */
188: { NULL, TYPE_Bsx, MDOCF_CHILD }, /* Bsx */
189: { NULL, TYPE_Bx, MDOCF_CHILD }, /* Bx */
190: { NULL, 0, 0 }, /* Db */
191: { NULL, 0, 0 }, /* Dc */
192: { NULL, 0, 0 }, /* Do */
193: { NULL, 0, 0 }, /* Dq */
194: { NULL, 0, 0 }, /* Ec */
195: { NULL, 0, 0 }, /* Ef */
196: { NULL, TYPE_Em, MDOCF_CHILD }, /* Em */
197: { NULL, 0, 0 }, /* Eo */
198: { NULL, TYPE_Fx, MDOCF_CHILD }, /* Fx */
199: { NULL, TYPE_Ms, MDOCF_CHILD }, /* Ms */
200: { NULL, 0, 0 }, /* No */
201: { NULL, 0, 0 }, /* Ns */
202: { NULL, TYPE_Nx, MDOCF_CHILD }, /* Nx */
203: { NULL, TYPE_Ox, MDOCF_CHILD }, /* Ox */
204: { NULL, 0, 0 }, /* Pc */
205: { NULL, 0, 0 }, /* Pf */
206: { NULL, 0, 0 }, /* Po */
207: { NULL, 0, 0 }, /* Pq */
208: { NULL, 0, 0 }, /* Qc */
209: { NULL, 0, 0 }, /* Ql */
210: { NULL, 0, 0 }, /* Qo */
211: { NULL, 0, 0 }, /* Qq */
212: { NULL, 0, 0 }, /* Re */
213: { NULL, 0, 0 }, /* Rs */
214: { NULL, 0, 0 }, /* Sc */
215: { NULL, 0, 0 }, /* So */
216: { NULL, 0, 0 }, /* Sq */
217: { NULL, 0, 0 }, /* Sm */
218: { NULL, 0, 0 }, /* Sx */
219: { NULL, TYPE_Sy, MDOCF_CHILD }, /* Sy */
220: { NULL, TYPE_Tn, MDOCF_CHILD }, /* Tn */
221: { NULL, 0, 0 }, /* Ux */
222: { NULL, 0, 0 }, /* Xc */
223: { NULL, 0, 0 }, /* Xo */
224: { pmdoc_head, TYPE_Fn, 0 }, /* Fo */
225: { NULL, 0, 0 }, /* Fc */
226: { NULL, 0, 0 }, /* Oo */
227: { NULL, 0, 0 }, /* Oc */
228: { NULL, 0, 0 }, /* Bk */
229: { NULL, 0, 0 }, /* Ek */
230: { NULL, 0, 0 }, /* Bt */
231: { NULL, 0, 0 }, /* Hf */
232: { NULL, 0, 0 }, /* Fr */
233: { NULL, 0, 0 }, /* Ud */
234: { NULL, TYPE_Lb, MDOCF_CHILD }, /* Lb */
235: { NULL, 0, 0 }, /* Lp */
236: { NULL, TYPE_Lk, MDOCF_CHILD }, /* Lk */
237: { NULL, TYPE_Mt, MDOCF_CHILD }, /* Mt */
238: { NULL, 0, 0 }, /* Brq */
239: { NULL, 0, 0 }, /* Bro */
240: { NULL, 0, 0 }, /* Brc */
241: { NULL, 0, 0 }, /* %C */
242: { NULL, 0, 0 }, /* Es */
243: { NULL, 0, 0 }, /* En */
244: { NULL, TYPE_Dx, MDOCF_CHILD }, /* Dx */
245: { NULL, 0, 0 }, /* %Q */
246: { NULL, 0, 0 }, /* br */
247: { NULL, 0, 0 }, /* sp */
248: { NULL, 0, 0 }, /* %U */
249: { NULL, 0, 0 }, /* Ta */
1.1 schwarze 250: };
251:
252: static const char *progname;
1.13 schwarze 253: static int use_all; /* Use all directories and files. */
254: static int verb; /* Output verbosity level. */
1.1 schwarze 255:
256: int
1.3 schwarze 257: mandocdb(int argc, char *argv[])
1.1 schwarze 258: {
259: struct mparse *mp; /* parse sequence */
1.10 schwarze 260: struct manpaths dirs;
1.1 schwarze 261: enum op op; /* current operation */
1.2 schwarze 262: const char *dir;
1.21 ! schwarze 263: char *cp;
! 264: char pbuf[PATH_MAX],
! 265: ibuf[MAXPATHLEN], /* index fname */
1.2 schwarze 266: fbuf[MAXPATHLEN]; /* btree fname */
1.13 schwarze 267: int ch, i, flags;
1.1 schwarze 268: DB *idx, /* index database */
269: *db, /* keyword database */
270: *hash; /* temporary keyword hashtable */
271: BTREEINFO info; /* btree configuration */
1.8 schwarze 272: recno_t maxrec; /* last record number in the index */
273: recno_t *recs; /* the numbers of all empty records */
1.2 schwarze 274: size_t sz1, sz2,
1.8 schwarze 275: recsz, /* number of allocated slots in recs */
276: reccur; /* current number of empty records */
1.1 schwarze 277: struct buf buf, /* keyword buffer */
278: dbuf; /* description buffer */
1.2 schwarze 279: struct of *of; /* list of files for processing */
1.1 schwarze 280: extern int optind;
281: extern char *optarg;
282:
283: progname = strrchr(argv[0], '/');
284: if (progname == NULL)
285: progname = argv[0];
286: else
287: ++progname;
288:
1.10 schwarze 289: memset(&dirs, 0, sizeof(struct manpaths));
290:
1.1 schwarze 291: verb = 0;
1.6 schwarze 292: use_all = 0;
1.2 schwarze 293: of = NULL;
1.1 schwarze 294: db = idx = NULL;
295: mp = NULL;
296: hash = NULL;
297: recs = NULL;
298: recsz = reccur = 0;
299: maxrec = 0;
300: op = OP_NEW;
1.2 schwarze 301: dir = NULL;
1.1 schwarze 302:
1.6 schwarze 303: while (-1 != (ch = getopt(argc, argv, "ad:u:v")))
1.1 schwarze 304: switch (ch) {
1.6 schwarze 305: case ('a'):
306: use_all = 1;
307: break;
1.1 schwarze 308: case ('d'):
309: dir = optarg;
1.2 schwarze 310: op = OP_UPDATE;
1.1 schwarze 311: break;
1.2 schwarze 312: case ('u'):
313: dir = optarg;
1.1 schwarze 314: op = OP_DELETE;
315: break;
316: case ('v'):
317: verb++;
318: break;
319: default:
320: usage();
321: return((int)MANDOCLEVEL_BADARG);
322: }
323:
324: argc -= optind;
325: argv += optind;
326:
1.2 schwarze 327: memset(&info, 0, sizeof(BTREEINFO));
1.17 schwarze 328: info.lorder = 4321;
1.2 schwarze 329: info.flags = R_DUP;
330:
331: mp = mparse_alloc(MPARSE_AUTO, MANDOCLEVEL_FATAL, NULL, NULL);
332:
333: memset(&buf, 0, sizeof(struct buf));
334: memset(&dbuf, 0, sizeof(struct buf));
335:
336: buf.size = dbuf.size = MANDOC_BUFSZ;
337:
338: buf.cp = mandoc_malloc(buf.size);
339: dbuf.cp = mandoc_malloc(dbuf.size);
340:
341: flags = OP_NEW == op ? O_CREAT|O_TRUNC|O_RDWR : O_CREAT|O_RDWR;
1.1 schwarze 342:
1.2 schwarze 343: if (OP_UPDATE == op || OP_DELETE == op) {
344: ibuf[0] = fbuf[0] = '\0';
1.1 schwarze 345:
1.2 schwarze 346: strlcat(fbuf, dir, MAXPATHLEN);
347: strlcat(fbuf, "/", MAXPATHLEN);
348: sz1 = strlcat(fbuf, MANDOC_DB, MAXPATHLEN);
349:
350: strlcat(ibuf, dir, MAXPATHLEN);
351: strlcat(ibuf, "/", MAXPATHLEN);
352: sz2 = strlcat(ibuf, MANDOC_IDX, MAXPATHLEN);
353:
354: if (sz1 >= MAXPATHLEN || sz2 >= MAXPATHLEN) {
355: fprintf(stderr, "%s: Path too long\n", dir);
356: exit((int)MANDOCLEVEL_BADARG);
357: }
1.1 schwarze 358:
1.2 schwarze 359: db = dbopen(fbuf, flags, 0644, DB_BTREE, &info);
360: idx = dbopen(ibuf, flags, 0644, DB_RECNO, NULL);
1.1 schwarze 361:
1.2 schwarze 362: if (NULL == db) {
363: perror(fbuf);
364: exit((int)MANDOCLEVEL_SYSERR);
1.8 schwarze 365: } else if (NULL == idx) {
1.2 schwarze 366: perror(ibuf);
367: exit((int)MANDOCLEVEL_SYSERR);
368: }
1.1 schwarze 369:
1.2 schwarze 370: if (verb > 2) {
371: printf("%s: Opened\n", fbuf);
372: printf("%s: Opened\n", ibuf);
373: }
1.1 schwarze 374:
1.13 schwarze 375: ofile_argbuild(argc, argv, &of);
1.2 schwarze 376: if (NULL == of)
377: goto out;
378:
379: of = of->first;
380:
1.13 schwarze 381: index_prune(of, db, fbuf, idx, ibuf,
1.20 schwarze 382: &maxrec, &recs, &recsz, &reccur);
1.2 schwarze 383:
1.15 schwarze 384: /*
385: * Go to the root of the respective manual tree
386: * such that .so links work. In case of failure,
387: * just prod on, even though .so links won't work.
388: */
389:
1.14 schwarze 390: if (OP_UPDATE == op) {
391: chdir(dir);
1.10 schwarze 392: index_merge(of, mp, &dbuf, &buf, hash,
1.13 schwarze 393: db, fbuf, idx, ibuf,
394: maxrec, recs, reccur);
1.14 schwarze 395: }
1.1 schwarze 396:
397: goto out;
398: }
399:
1.10 schwarze 400: /*
401: * Configure the directories we're going to scan.
402: * If we have command-line arguments, use them.
403: * If not, we use man(1)'s method (see mandocdb.8).
404: */
405:
406: if (argc > 0) {
1.21 ! schwarze 407: dirs.paths = mandoc_calloc(argc, sizeof(char *));
1.10 schwarze 408: dirs.sz = argc;
1.21 ! schwarze 409: for (i = 0; i < argc; i++) {
! 410: if (NULL == (cp = realpath(argv[i], pbuf))) {
! 411: perror(argv[i]);
! 412: goto out;
! 413: }
! 414: dirs.paths[i] = mandoc_strdup(cp);
! 415: }
1.10 schwarze 416: } else
417: manpath_parse(&dirs, NULL, NULL);
1.7 schwarze 418:
1.10 schwarze 419: for (i = 0; i < dirs.sz; i++) {
1.2 schwarze 420: ibuf[0] = fbuf[0] = '\0';
421:
1.10 schwarze 422: strlcat(fbuf, dirs.paths[i], MAXPATHLEN);
1.2 schwarze 423: strlcat(fbuf, "/", MAXPATHLEN);
424: sz1 = strlcat(fbuf, MANDOC_DB, MAXPATHLEN);
425:
1.10 schwarze 426: strlcat(ibuf, dirs.paths[i], MAXPATHLEN);
1.2 schwarze 427: strlcat(ibuf, "/", MAXPATHLEN);
428: sz2 = strlcat(ibuf, MANDOC_IDX, MAXPATHLEN);
429:
430: if (sz1 >= MAXPATHLEN || sz2 >= MAXPATHLEN) {
1.10 schwarze 431: fprintf(stderr, "%s: Path too long\n",
432: dirs.paths[i]);
1.2 schwarze 433: exit((int)MANDOCLEVEL_BADARG);
434: }
435:
1.7 schwarze 436: if (db)
437: (*db->close)(db);
438: if (idx)
439: (*idx->close)(idx);
440:
1.2 schwarze 441: db = dbopen(fbuf, flags, 0644, DB_BTREE, &info);
442: idx = dbopen(ibuf, flags, 0644, DB_RECNO, NULL);
1.1 schwarze 443:
1.2 schwarze 444: if (NULL == db) {
445: perror(fbuf);
446: exit((int)MANDOCLEVEL_SYSERR);
1.8 schwarze 447: } else if (NULL == idx) {
1.2 schwarze 448: perror(ibuf);
449: exit((int)MANDOCLEVEL_SYSERR);
450: }
1.1 schwarze 451:
1.2 schwarze 452: if (verb > 2) {
453: printf("%s: Truncated\n", fbuf);
454: printf("%s: Truncated\n", ibuf);
455: }
1.1 schwarze 456:
1.2 schwarze 457: ofile_free(of);
458: of = NULL;
1.1 schwarze 459:
1.10 schwarze 460: if ( ! ofile_dirbuild(dirs.paths[i], NULL, NULL,
1.13 schwarze 461: 0, &of))
1.2 schwarze 462: exit((int)MANDOCLEVEL_SYSERR);
1.1 schwarze 463:
1.2 schwarze 464: if (NULL == of)
465: continue;
1.1 schwarze 466:
1.2 schwarze 467: of = of->first;
1.15 schwarze 468:
469: /*
470: * Go to the root of the respective manual tree
471: * such that .so links work. In case of failure,
472: * just prod on, even though .so links won't work.
473: */
1.1 schwarze 474:
1.14 schwarze 475: chdir(dirs.paths[i]);
1.10 schwarze 476: index_merge(of, mp, &dbuf, &buf, hash, db, fbuf,
1.13 schwarze 477: idx, ibuf, maxrec, recs, reccur);
1.1 schwarze 478: }
479:
1.2 schwarze 480: out:
481: if (db)
482: (*db->close)(db);
483: if (idx)
484: (*idx->close)(idx);
485: if (hash)
486: (*hash->close)(hash);
487: if (mp)
488: mparse_free(mp);
1.1 schwarze 489:
1.10 schwarze 490: manpath_free(&dirs);
1.2 schwarze 491: ofile_free(of);
492: free(buf.cp);
493: free(dbuf.cp);
494: free(recs);
1.1 schwarze 495:
1.2 schwarze 496: return(MANDOCLEVEL_OK);
497: }
1.1 schwarze 498:
1.2 schwarze 499: void
500: index_merge(const struct of *of, struct mparse *mp,
1.13 schwarze 501: struct buf *dbuf, struct buf *buf, DB *hash,
502: DB *db, const char *dbf, DB *idx, const char *idxf,
1.2 schwarze 503: recno_t maxrec, const recno_t *recs, size_t reccur)
504: {
505: recno_t rec;
506: int ch;
507: DBT key, val;
508: struct mdoc *mdoc;
509: struct man *man;
510: const char *fn, *msec, *mtitle, *arch;
511: size_t sv;
512: unsigned seq;
1.9 schwarze 513: struct db_val vbuf;
1.1 schwarze 514:
1.2 schwarze 515: for (rec = 0; of; of = of->next) {
516: fn = of->fname;
1.11 schwarze 517:
518: /*
519: * Reclaim an empty index record, if available.
520: */
521:
1.2 schwarze 522: if (reccur > 0) {
523: --reccur;
524: rec = recs[(int)reccur];
525: } else if (maxrec > 0) {
526: rec = maxrec;
527: maxrec = 0;
1.1 schwarze 528: } else
529: rec++;
530:
531: mparse_reset(mp);
532: hash_reset(&hash);
1.11 schwarze 533: mdoc = NULL;
534: man = NULL;
1.1 schwarze 535:
1.11 schwarze 536: /*
537: * Try interpreting the file as mdoc(7) or man(7)
538: * source code, unless it is already known to be
539: * formatted. Fall back to formatted mode.
540: */
541:
542: if ((MANDOC_SRC & of->src_form ||
543: ! (MANDOC_FORM & of->src_form)) &&
544: MANDOCLEVEL_FATAL > mparse_readfd(mp, -1, fn))
545: mparse_result(mp, &mdoc, &man);
546:
547: if (NULL != mdoc) {
548: msec = mdoc_meta(mdoc)->msec;
549: arch = mdoc_meta(mdoc)->arch;
550: mtitle = mdoc_meta(mdoc)->title;
551: } else if (NULL != man) {
552: msec = man_meta(man)->msec;
553: arch = NULL;
554: mtitle = man_meta(man)->title;
555: } else {
556: msec = of->sec;
557: arch = of->arch;
558: mtitle = of->title;
1.1 schwarze 559: }
560:
1.6 schwarze 561: /*
1.8 schwarze 562: * By default, skip a file if the manual section
563: * and architecture given in the file disagree
564: * with the directory where the file is located.
1.6 schwarze 565: */
566:
567: if (0 == use_all) {
568: assert(of->sec);
569: assert(msec);
570: if (strcmp(msec, of->sec))
571: continue;
572:
573: if (NULL == arch) {
574: if (NULL != of->arch)
575: continue;
576: } else if (NULL == of->arch ||
577: strcmp(arch, of->arch))
578: continue;
579: }
580:
1.1 schwarze 581: if (NULL == arch)
582: arch = "";
583:
584: /*
1.8 schwarze 585: * By default, skip a file if the title given
586: * in the file disagrees with the file name.
587: * If both agree, use the file name as the title,
588: * because the one in the file usually is all caps.
1.6 schwarze 589: */
590:
591: assert(of->title);
592: assert(mtitle);
593:
594: if (0 == strcasecmp(mtitle, of->title))
595: mtitle = of->title;
596: else if (0 == use_all)
597: continue;
598:
599: /*
1.1 schwarze 600: * The index record value consists of a nil-terminated
601: * filename, a nil-terminated manual section, and a
602: * nil-terminated description. Since the description
603: * may not be set, we set a sentinel to see if we're
604: * going to write a nil byte in its place.
605: */
606:
1.2 schwarze 607: dbuf->len = 0;
1.12 schwarze 608: buf_append(dbuf, mdoc ? "mdoc" : (man ? "man" : "cat"));
1.2 schwarze 609: buf_appendb(dbuf, fn, strlen(fn) + 1);
610: buf_appendb(dbuf, msec, strlen(msec) + 1);
611: buf_appendb(dbuf, mtitle, strlen(mtitle) + 1);
612: buf_appendb(dbuf, arch, strlen(arch) + 1);
1.1 schwarze 613:
1.2 schwarze 614: sv = dbuf->len;
1.1 schwarze 615:
616: /* Fix the record number in the btree value. */
617:
618: if (mdoc)
1.2 schwarze 619: pmdoc_node(hash, buf, dbuf,
1.1 schwarze 620: mdoc_node(mdoc), mdoc_meta(mdoc));
1.11 schwarze 621: else if (man)
1.2 schwarze 622: pman_node(hash, buf, dbuf, man_node(man));
1.11 schwarze 623: else
624: pformatted(hash, buf, dbuf, of);
1.1 schwarze 625:
626: /*
627: * Copy from the in-memory hashtable of pending keywords
628: * into the database.
629: */
630:
1.17 schwarze 631: vbuf.rec = htobe32(rec);
1.1 schwarze 632: seq = R_FIRST;
633: while (0 == (ch = (*hash->seq)(hash, &key, &val, seq))) {
634: seq = R_NEXT;
1.17 schwarze 635: vbuf.mask = htobe64(*(uint64_t *)val.data);
1.9 schwarze 636: val.size = sizeof(struct db_val);
637: val.data = &vbuf;
1.2 schwarze 638: dbt_put(db, dbf, &key, &val);
1.1 schwarze 639: }
640: if (ch < 0) {
641: perror("hash");
642: exit((int)MANDOCLEVEL_SYSERR);
643: }
644:
645: /*
646: * Apply to the index. If we haven't had a description
647: * set, put an empty one in now.
648: */
649:
1.2 schwarze 650: if (dbuf->len == sv)
651: buf_appendb(dbuf, "", 1);
1.1 schwarze 652:
653: key.data = &rec;
654: key.size = sizeof(recno_t);
655:
1.2 schwarze 656: val.data = dbuf->cp;
657: val.size = dbuf->len;
1.1 schwarze 658:
1.2 schwarze 659: if (verb)
1.1 schwarze 660: printf("%s: Added index\n", fn);
1.16 schwarze 661:
1.2 schwarze 662: dbt_put(idx, idxf, &key, &val);
663: }
664: }
665:
666: /*
667: * Scan through all entries in the index file `idx' and prune those
668: * entries in `ofile'.
669: * Pruning consists of removing from `db', then invalidating the entry
670: * in `idx' (zeroing its value size).
671: */
672: static void
673: index_prune(const struct of *ofile, DB *db, const char *dbf,
1.20 schwarze 674: DB *idx, const char *idxf, recno_t *maxrec,
675: recno_t **recs, size_t *recsz, size_t *reccur)
1.2 schwarze 676: {
677: const struct of *of;
1.16 schwarze 678: const char *fn, *cp;
1.9 schwarze 679: struct db_val *vbuf;
1.2 schwarze 680: unsigned seq, sseq;
681: DBT key, val;
682: int ch;
683:
1.20 schwarze 684: *reccur = 0;
1.2 schwarze 685: seq = R_FIRST;
686: while (0 == (ch = (*idx->seq)(idx, &key, &val, seq))) {
687: seq = R_NEXT;
688: *maxrec = *(recno_t *)key.data;
1.16 schwarze 689: cp = val.data;
690:
691: /* Deleted records are zero-sized. Skip them. */
692:
693: if (0 == val.size)
694: goto cont;
695:
696: /*
697: * Make sure we're sane.
698: * Read past our mdoc/man/cat type to the next string,
699: * then make sure it's bounded by a NUL.
700: * Failing any of these, we go into our error handler.
701: */
702:
703: if (NULL == (fn = memchr(cp, '\0', val.size)))
704: break;
705: if (++fn - cp >= (int)val.size)
706: break;
707: if (NULL == memchr(fn, '\0', val.size - (fn - cp)))
708: break;
709:
710: /*
711: * Search for the file in those we care about.
712: * XXX: build this into a tree. Too slow.
713: */
1.2 schwarze 714:
715: for (of = ofile; of; of = of->next)
716: if (0 == strcmp(fn, of->fname))
717: break;
718:
719: if (NULL == of)
720: continue;
721:
1.16 schwarze 722: /*
723: * Search through the keyword database, throwing out all
724: * references to our file.
725: */
726:
1.2 schwarze 727: sseq = R_FIRST;
728: while (0 == (ch = (*db->seq)(db, &key, &val, sseq))) {
729: sseq = R_NEXT;
1.16 schwarze 730: if (sizeof(struct db_val) != val.size)
731: break;
732:
1.9 schwarze 733: vbuf = val.data;
1.17 schwarze 734: if (*maxrec != betoh32(vbuf->rec))
1.2 schwarze 735: continue;
1.16 schwarze 736:
737: if ((ch = (*db->del)(db, &key, R_CURSOR)) < 0)
1.2 schwarze 738: break;
739: }
1.16 schwarze 740:
1.2 schwarze 741: if (ch < 0) {
742: perror(dbf);
743: exit((int)MANDOCLEVEL_SYSERR);
1.16 schwarze 744: } else if (1 != ch) {
745: fprintf(stderr, "%s: Corrupt database\n", dbf);
746: exit((int)MANDOCLEVEL_SYSERR);
1.2 schwarze 747: }
1.1 schwarze 748:
1.2 schwarze 749: if (verb)
750: printf("%s: Deleted index\n", fn);
1.1 schwarze 751:
1.2 schwarze 752: val.size = 0;
753: ch = (*idx->put)(idx, &key, &val, R_CURSOR);
1.1 schwarze 754:
1.16 schwarze 755: if (ch < 0)
756: break;
757: cont:
1.20 schwarze 758: if (*reccur >= *recsz) {
1.2 schwarze 759: *recsz += MANDOC_SLOP;
760: *recs = mandoc_realloc
761: (*recs, *recsz * sizeof(recno_t));
762: }
1.1 schwarze 763:
1.20 schwarze 764: (*recs)[(int)*reccur] = *maxrec;
765: (*reccur)++;
1.2 schwarze 766: }
1.16 schwarze 767:
768: if (ch < 0) {
769: perror(idxf);
770: exit((int)MANDOCLEVEL_SYSERR);
771: } else if (1 != ch) {
772: fprintf(stderr, "%s: Corrupt index\n", idxf);
773: exit((int)MANDOCLEVEL_SYSERR);
774: }
775:
1.2 schwarze 776: (*maxrec)++;
1.1 schwarze 777: }
778:
779: /*
780: * Grow the buffer (if necessary) and copy in a binary string.
781: */
782: static void
783: buf_appendb(struct buf *buf, const void *cp, size_t sz)
784: {
785:
786: /* Overshoot by MANDOC_BUFSZ. */
787:
788: while (buf->len + sz >= buf->size) {
789: buf->size = buf->len + sz + MANDOC_BUFSZ;
790: buf->cp = mandoc_realloc(buf->cp, buf->size);
791: }
792:
793: memcpy(buf->cp + (int)buf->len, cp, sz);
794: buf->len += sz;
795: }
796:
797: /*
798: * Append a nil-terminated string to the buffer.
799: * This can be invoked multiple times.
800: * The buffer string will be nil-terminated.
801: * If invoked multiple times, a space is put between strings.
802: */
803: static void
804: buf_append(struct buf *buf, const char *cp)
805: {
806: size_t sz;
807:
808: if (0 == (sz = strlen(cp)))
809: return;
810:
811: if (buf->len)
812: buf->cp[(int)buf->len - 1] = ' ';
813:
814: buf_appendb(buf, cp, sz + 1);
815: }
816:
817: /*
818: * Recursively add all text from a given node.
819: * This is optimised for general mdoc nodes in this context, which do
820: * not consist of subexpressions and having a recursive call for n->next
821: * would be wasteful.
822: * The "f" variable should be 0 unless called from pmdoc_Nd for the
823: * description buffer, which does not start at the beginning of the
824: * buffer.
825: */
826: static void
827: buf_appendmdoc(struct buf *buf, const struct mdoc_node *n, int f)
828: {
829:
830: for ( ; n; n = n->next) {
831: if (n->child)
832: buf_appendmdoc(buf, n->child, f);
833:
834: if (MDOC_TEXT == n->type && f) {
835: f = 0;
836: buf_appendb(buf, n->string,
837: strlen(n->string) + 1);
838: } else if (MDOC_TEXT == n->type)
839: buf_append(buf, n->string);
840:
841: }
842: }
843:
844: static void
845: hash_reset(DB **db)
846: {
847: DB *hash;
848:
849: if (NULL != (hash = *db))
850: (*hash->close)(hash);
851:
1.2 schwarze 852: *db = dbopen(NULL, O_CREAT|O_RDWR, 0644, DB_HASH, NULL);
1.1 schwarze 853: if (NULL == *db) {
854: perror("hash");
855: exit((int)MANDOCLEVEL_SYSERR);
856: }
857: }
858:
859: /* ARGSUSED */
1.19 schwarze 860: static int
861: pmdoc_head(MDOC_ARGS)
862: {
863:
864: return(MDOC_HEAD == n->type);
865: }
866:
867: /* ARGSUSED */
868: static int
869: pmdoc_body(MDOC_ARGS)
870: {
871:
872: return(MDOC_BODY == n->type);
873: }
874:
875: /* ARGSUSED */
876: static int
1.1 schwarze 877: pmdoc_Fd(MDOC_ARGS)
878: {
879: const char *start, *end;
880: size_t sz;
1.19 schwarze 881:
1.1 schwarze 882: if (SEC_SYNOPSIS != n->sec)
1.19 schwarze 883: return(0);
1.1 schwarze 884: if (NULL == (n = n->child) || MDOC_TEXT != n->type)
1.19 schwarze 885: return(0);
1.1 schwarze 886:
887: /*
888: * Only consider those `Fd' macro fields that begin with an
889: * "inclusion" token (versus, e.g., #define).
890: */
891: if (strcmp("#include", n->string))
1.19 schwarze 892: return(0);
1.1 schwarze 893:
894: if (NULL == (n = n->next) || MDOC_TEXT != n->type)
1.19 schwarze 895: return(0);
1.1 schwarze 896:
897: /*
898: * Strip away the enclosing angle brackets and make sure we're
899: * not zero-length.
900: */
901:
902: start = n->string;
903: if ('<' == *start || '"' == *start)
904: start++;
905:
906: if (0 == (sz = strlen(start)))
1.19 schwarze 907: return(0);
1.1 schwarze 908:
909: end = &start[(int)sz - 1];
910: if ('>' == *end || '"' == *end)
911: end--;
912:
913: assert(end >= start);
914:
915: buf_appendb(buf, start, (size_t)(end - start + 1));
916: buf_appendb(buf, "", 1);
1.19 schwarze 917: return(1);
1.1 schwarze 918: }
919:
920: /* ARGSUSED */
1.19 schwarze 921: static int
922: pmdoc_In(MDOC_ARGS)
1.1 schwarze 923: {
924:
925: if (NULL == n->child || MDOC_TEXT != n->child->type)
1.19 schwarze 926: return(0);
1.1 schwarze 927:
928: buf_append(buf, n->child->string);
1.19 schwarze 929: return(1);
1.1 schwarze 930: }
931:
932: /* ARGSUSED */
1.19 schwarze 933: static int
1.1 schwarze 934: pmdoc_Fn(MDOC_ARGS)
935: {
1.19 schwarze 936: struct mdoc_node *nn;
1.1 schwarze 937: const char *cp;
938:
1.19 schwarze 939: nn = n->child;
940:
941: if (NULL == nn || MDOC_TEXT != nn->type)
942: return(0);
943:
944: /* .Fn "struct type *name" "char *arg" */
1.1 schwarze 945:
1.19 schwarze 946: cp = strrchr(nn->string, ' ');
1.1 schwarze 947: if (NULL == cp)
1.19 schwarze 948: cp = nn->string;
1.1 schwarze 949:
950: /* Strip away pointer symbol. */
951:
952: while ('*' == *cp)
953: cp++;
954:
1.19 schwarze 955: /* Store the function name. */
956:
1.1 schwarze 957: buf_append(buf, cp);
1.5 schwarze 958: hash_put(hash, buf, TYPE_Fn);
1.19 schwarze 959:
960: /* Store the function type. */
961:
962: if (nn->string < cp) {
963: buf->len = 0;
964: buf_appendb(buf, nn->string, cp - nn->string);
965: buf_appendb(buf, "", 1);
966: hash_put(hash, buf, TYPE_Ft);
967: }
968:
969: /* Store the arguments. */
970:
971: for (nn = nn->next; nn; nn = nn->next) {
972: if (MDOC_TEXT != nn->type)
973: continue;
974: buf->len = 0;
975: buf_append(buf, nn->string);
976: hash_put(hash, buf, TYPE_Fa);
977: }
978:
979: return(0);
1.1 schwarze 980: }
981:
982: /* ARGSUSED */
1.19 schwarze 983: static int
1.1 schwarze 984: pmdoc_St(MDOC_ARGS)
985: {
1.19 schwarze 986:
1.1 schwarze 987: if (NULL == n->child || MDOC_TEXT != n->child->type)
1.19 schwarze 988: return(0);
1.1 schwarze 989:
990: buf_append(buf, n->child->string);
1.19 schwarze 991: return(1);
1.1 schwarze 992: }
993:
994: /* ARGSUSED */
1.19 schwarze 995: static int
1.1 schwarze 996: pmdoc_Xr(MDOC_ARGS)
997: {
998:
999: if (NULL == (n = n->child))
1.19 schwarze 1000: return(0);
1.1 schwarze 1001:
1002: buf_appendb(buf, n->string, strlen(n->string));
1003:
1004: if (NULL != (n = n->next)) {
1005: buf_appendb(buf, ".", 1);
1006: buf_appendb(buf, n->string, strlen(n->string) + 1);
1007: } else
1008: buf_appendb(buf, ".", 2);
1009:
1.19 schwarze 1010: return(1);
1.1 schwarze 1011: }
1012:
1013: /* ARGSUSED */
1.19 schwarze 1014: static int
1.1 schwarze 1015: pmdoc_Nd(MDOC_ARGS)
1016: {
1017:
1018: if (MDOC_BODY != n->type)
1.19 schwarze 1019: return(0);
1.1 schwarze 1020:
1021: buf_appendmdoc(dbuf, n->child, 1);
1.19 schwarze 1022: return(1);
1.1 schwarze 1023: }
1024:
1025: /* ARGSUSED */
1.19 schwarze 1026: static int
1027: pmdoc_Nm(MDOC_ARGS)
1.1 schwarze 1028: {
1029:
1.19 schwarze 1030: if (SEC_NAME == n->sec)
1031: return(1);
1032: else if (SEC_SYNOPSIS != n->sec || MDOC_HEAD != n->type)
1033: return(0);
1.1 schwarze 1034:
1.19 schwarze 1035: if (NULL == n->child)
1036: buf_append(buf, m->name);
1.1 schwarze 1037:
1.19 schwarze 1038: return(1);
1.1 schwarze 1039: }
1040:
1041: /* ARGSUSED */
1.19 schwarze 1042: static int
1043: pmdoc_Sh(MDOC_ARGS)
1.1 schwarze 1044: {
1045:
1.19 schwarze 1046: return(SEC_CUSTOM == n->sec && MDOC_HEAD == n->type);
1.1 schwarze 1047: }
1048:
1049: static void
1.9 schwarze 1050: hash_put(DB *db, const struct buf *buf, uint64_t mask)
1.1 schwarze 1051: {
1052: DBT key, val;
1053: int rc;
1054:
1055: if (buf->len < 2)
1056: return;
1057:
1058: key.data = buf->cp;
1059: key.size = buf->len;
1060:
1061: if ((rc = (*db->get)(db, &key, &val, 0)) < 0) {
1062: perror("hash");
1063: exit((int)MANDOCLEVEL_SYSERR);
1064: } else if (0 == rc)
1.9 schwarze 1065: mask |= *(uint64_t *)val.data;
1.1 schwarze 1066:
1067: val.data = &mask;
1.9 schwarze 1068: val.size = sizeof(uint64_t);
1.1 schwarze 1069:
1070: if ((rc = (*db->put)(db, &key, &val, 0)) < 0) {
1071: perror("hash");
1072: exit((int)MANDOCLEVEL_SYSERR);
1073: }
1074: }
1075:
1076: static void
1077: dbt_put(DB *db, const char *dbn, DBT *key, DBT *val)
1078: {
1079:
1080: assert(key->size);
1081: assert(val->size);
1082:
1083: if (0 == (*db->put)(db, key, val, 0))
1084: return;
1085:
1086: perror(dbn);
1087: exit((int)MANDOCLEVEL_SYSERR);
1088: /* NOTREACHED */
1089: }
1090:
1091: /*
1092: * Call out to per-macro handlers after clearing the persistent database
1093: * key. If the macro sets the database key, flush it to the database.
1094: */
1095: static void
1096: pmdoc_node(MDOC_ARGS)
1097: {
1098:
1099: if (NULL == n)
1100: return;
1101:
1102: switch (n->type) {
1103: case (MDOC_HEAD):
1104: /* FALLTHROUGH */
1105: case (MDOC_BODY):
1106: /* FALLTHROUGH */
1107: case (MDOC_TAIL):
1108: /* FALLTHROUGH */
1109: case (MDOC_BLOCK):
1110: /* FALLTHROUGH */
1111: case (MDOC_ELEM):
1.19 schwarze 1112: buf->len = 0;
1113:
1114: /*
1115: * Both NULL handlers and handlers returning true
1116: * request using the data. Only skip the element
1117: * when the handler returns false.
1118: */
1119:
1120: if (NULL != mdocs[n->tok].fp &&
1121: 0 == (*mdocs[n->tok].fp)(hash, buf, dbuf, n, m))
1.1 schwarze 1122: break;
1123:
1.19 schwarze 1124: /*
1125: * For many macros, use the text from all children.
1126: * Set zero flags for macros not needing this.
1127: * In that case, the handler must fill the buffer.
1128: */
1129:
1130: if (MDOCF_CHILD & mdocs[n->tok].flags)
1131: buf_appendmdoc(buf, n->child, 0);
1132:
1133: /*
1134: * Cover the most common case:
1135: * Automatically stage one string per element.
1136: * Set a zero mask for macros not needing this.
1137: * Additional staging can be done in the handler.
1138: */
1139:
1140: if (mdocs[n->tok].mask)
1141: hash_put(hash, buf, mdocs[n->tok].mask);
1.1 schwarze 1142: break;
1143: default:
1144: break;
1145: }
1146:
1147: pmdoc_node(hash, buf, dbuf, n->child, m);
1148: pmdoc_node(hash, buf, dbuf, n->next, m);
1149: }
1150:
1151: static int
1152: pman_node(MAN_ARGS)
1153: {
1154: const struct man_node *head, *body;
1155: const char *start, *sv;
1156: size_t sz;
1157:
1158: if (NULL == n)
1159: return(0);
1160:
1161: /*
1162: * We're only searching for one thing: the first text child in
1163: * the BODY of a NAME section. Since we don't keep track of
1164: * sections in -man, run some hoops to find out whether we're in
1165: * the correct section or not.
1166: */
1167:
1168: if (MAN_BODY == n->type && MAN_SH == n->tok) {
1169: body = n;
1170: assert(body->parent);
1171: if (NULL != (head = body->parent->head) &&
1172: 1 == head->nchild &&
1173: NULL != (head = (head->child)) &&
1174: MAN_TEXT == head->type &&
1175: 0 == strcmp(head->string, "NAME") &&
1176: NULL != (body = body->child) &&
1177: MAN_TEXT == body->type) {
1178:
1179: assert(body->string);
1180: start = sv = body->string;
1181:
1182: /*
1183: * Go through a special heuristic dance here.
1184: * This is why -man manuals are great!
1185: * (I'm being sarcastic: my eyes are bleeding.)
1186: * Conventionally, one or more manual names are
1187: * comma-specified prior to a whitespace, then a
1188: * dash, then a description. Try to puzzle out
1189: * the name parts here.
1190: */
1191:
1192: for ( ;; ) {
1193: sz = strcspn(start, " ,");
1194: if ('\0' == start[(int)sz])
1195: break;
1196:
1197: buf->len = 0;
1198: buf_appendb(buf, start, sz);
1199: buf_appendb(buf, "", 1);
1200:
1.5 schwarze 1201: hash_put(hash, buf, TYPE_Nm);
1.1 schwarze 1202:
1203: if (' ' == start[(int)sz]) {
1204: start += (int)sz + 1;
1205: break;
1206: }
1207:
1208: assert(',' == start[(int)sz]);
1209: start += (int)sz + 1;
1210: while (' ' == *start)
1211: start++;
1212: }
1213:
1214: buf->len = 0;
1215:
1216: if (sv == start) {
1217: buf_append(buf, start);
1218: return(1);
1219: }
1220:
1221: while (' ' == *start)
1222: start++;
1223:
1224: if (0 == strncmp(start, "-", 1))
1225: start += 1;
1226: else if (0 == strncmp(start, "\\-", 2))
1227: start += 2;
1228: else if (0 == strncmp(start, "\\(en", 4))
1229: start += 4;
1230: else if (0 == strncmp(start, "\\(em", 4))
1231: start += 4;
1232:
1233: while (' ' == *start)
1234: start++;
1235:
1236: sz = strlen(start) + 1;
1237: buf_appendb(dbuf, start, sz);
1238: buf_appendb(buf, start, sz);
1239:
1.5 schwarze 1240: hash_put(hash, buf, TYPE_Nd);
1.1 schwarze 1241: }
1242: }
1243:
1.4 schwarze 1244: for (n = n->child; n; n = n->next)
1245: if (pman_node(hash, buf, dbuf, n))
1246: return(1);
1.1 schwarze 1247:
1248: return(0);
1249: }
1250:
1.11 schwarze 1251: /*
1252: * Parse a formatted manual page.
1253: * By necessity, this involves rather crude guesswork.
1254: */
1255: static void
1256: pformatted(DB *hash, struct buf *buf, struct buf *dbuf,
1257: const struct of *of)
1258: {
1259: FILE *stream;
1260: char *line, *p;
1261: size_t len, plen;
1262:
1263: if (NULL == (stream = fopen(of->fname, "r"))) {
1264: perror(of->fname);
1265: return;
1266: }
1267:
1268: /*
1269: * Always use the title derived from the filename up front,
1270: * do not even try to find it in the file. This also makes
1271: * sure we don't end up with an orphan index record, even if
1272: * the file content turns out to be completely unintelligible.
1273: */
1274:
1275: buf->len = 0;
1276: buf_append(buf, of->title);
1277: hash_put(hash, buf, TYPE_Nm);
1278:
1279: while (NULL != (line = fgetln(stream, &len)) && '\n' != *line)
1280: /* Skip to first blank line. */ ;
1281:
1282: while (NULL != (line = fgetln(stream, &len)) &&
1283: ('\n' == *line || ' ' == *line))
1284: /* Skip to first section header. */ ;
1285:
1286: /*
1287: * If no page content can be found,
1288: * reuse the page title as the page description.
1289: */
1290:
1291: if (NULL == (line = fgetln(stream, &len))) {
1292: buf_appendb(dbuf, buf->cp, buf->size);
1293: hash_put(hash, buf, TYPE_Nd);
1294: fclose(stream);
1295: return;
1296: }
1297: fclose(stream);
1298:
1299: /*
1300: * If there is a dash, skip to the text following it.
1301: */
1302:
1303: for (p = line, plen = len; plen; p++, plen--)
1304: if ('-' == *p)
1305: break;
1306: for ( ; plen; p++, plen--)
1307: if ('-' != *p && ' ' != *p && 8 != *p)
1308: break;
1309: if (0 == plen) {
1310: p = line;
1311: plen = len;
1312: }
1313:
1314: /*
1315: * Copy the rest of the line, but no more than 70 bytes.
1316: */
1317:
1318: if (70 < plen)
1319: plen = 70;
1320: p[plen-1] = '\0';
1321: buf_appendb(dbuf, p, plen);
1322: buf->len = 0;
1323: buf_appendb(buf, p, plen);
1324: hash_put(hash, buf, TYPE_Nd);
1325: }
1326:
1.1 schwarze 1327: static void
1.13 schwarze 1328: ofile_argbuild(int argc, char *argv[], struct of **of)
1.2 schwarze 1329: {
1.6 schwarze 1330: char buf[MAXPATHLEN];
1331: char *sec, *arch, *title, *p;
1.11 schwarze 1332: int i, src_form;
1.2 schwarze 1333: struct of *nof;
1334:
1335: for (i = 0; i < argc; i++) {
1.6 schwarze 1336:
1337: /*
1.8 schwarze 1338: * Try to infer the manual section, architecture and
1339: * page title from the path, assuming it looks like
1.11 schwarze 1340: * man*[/<arch>]/<title>.<section> or
1341: * cat<section>[/<arch>]/<title>.0
1.6 schwarze 1342: */
1343:
1344: if (strlcpy(buf, argv[i], sizeof(buf)) >= sizeof(buf)) {
1345: fprintf(stderr, "%s: Path too long\n", argv[i]);
1346: continue;
1347: }
1348: sec = arch = title = NULL;
1.11 schwarze 1349: src_form = 0;
1.6 schwarze 1350: p = strrchr(buf, '\0');
1351: while (p-- > buf) {
1352: if (NULL == sec && '.' == *p) {
1353: sec = p + 1;
1354: *p = '\0';
1.11 schwarze 1355: if ('0' == *sec)
1356: src_form |= MANDOC_FORM;
1357: else if ('1' <= *sec && '9' >= *sec)
1358: src_form |= MANDOC_SRC;
1.6 schwarze 1359: continue;
1360: }
1361: if ('/' != *p)
1362: continue;
1363: if (NULL == title) {
1364: title = p + 1;
1365: *p = '\0';
1366: continue;
1367: }
1.18 schwarze 1368: if (0 == strncmp("man", p + 1, 3))
1.11 schwarze 1369: src_form |= MANDOC_SRC;
1.18 schwarze 1370: else if (0 == strncmp("cat", p + 1, 3))
1.11 schwarze 1371: src_form |= MANDOC_FORM;
1.18 schwarze 1372: else
1.6 schwarze 1373: arch = p + 1;
1374: break;
1375: }
1376: if (NULL == title)
1377: title = buf;
1378:
1379: /*
1380: * Build the file structure.
1381: */
1382:
1.2 schwarze 1383: nof = mandoc_calloc(1, sizeof(struct of));
1.6 schwarze 1384: nof->fname = mandoc_strdup(argv[i]);
1385: if (NULL != sec)
1386: nof->sec = mandoc_strdup(sec);
1387: if (NULL != arch)
1388: nof->arch = mandoc_strdup(arch);
1389: nof->title = mandoc_strdup(title);
1.11 schwarze 1390: nof->src_form = src_form;
1.6 schwarze 1391:
1392: /*
1393: * Add the structure to the list.
1394: */
1395:
1.2 schwarze 1396: if (verb > 2)
1397: printf("%s: Scheduling\n", argv[i]);
1398: if (NULL == *of) {
1399: *of = nof;
1400: (*of)->first = nof;
1401: } else {
1402: nof->first = (*of)->first;
1403: (*of)->next = nof;
1404: *of = nof;
1405: }
1406: }
1407: }
1408:
1409: /*
1410: * Recursively build up a list of files to parse.
1411: * We use this instead of ftw() and so on because I don't want global
1412: * variables hanging around.
1413: * This ignores the mandoc.db and mandoc.index files, but assumes that
1414: * everything else is a manual.
1415: * Pass in a pointer to a NULL structure for the first invocation.
1416: */
1417: static int
1.6 schwarze 1418: ofile_dirbuild(const char *dir, const char* psec, const char *parch,
1.13 schwarze 1419: int p_src_form, struct of **of)
1.2 schwarze 1420: {
1421: char buf[MAXPATHLEN];
1.11 schwarze 1422: struct stat sb;
1.2 schwarze 1423: size_t sz;
1424: DIR *d;
1.6 schwarze 1425: const char *fn, *sec, *arch;
1.11 schwarze 1426: char *p, *q, *suffix;
1.2 schwarze 1427: struct of *nof;
1428: struct dirent *dp;
1.11 schwarze 1429: int src_form;
1.2 schwarze 1430:
1431: if (NULL == (d = opendir(dir))) {
1432: perror(dir);
1433: return(0);
1434: }
1435:
1436: while (NULL != (dp = readdir(d))) {
1437: fn = dp->d_name;
1.6 schwarze 1438:
1439: if ('.' == *fn)
1440: continue;
1441:
1.11 schwarze 1442: src_form = p_src_form;
1443:
1.2 schwarze 1444: if (DT_DIR == dp->d_type) {
1.6 schwarze 1445: sec = psec;
1446: arch = parch;
1447:
1448: /*
1.8 schwarze 1449: * By default, only use directories called:
1.11 schwarze 1450: * man<section>/[<arch>/] or
1451: * cat<section>/[<arch>/]
1.6 schwarze 1452: */
1453:
1454: if (NULL == sec) {
1.11 schwarze 1455: if(0 == strncmp("man", fn, 3)) {
1456: src_form |= MANDOC_SRC;
1.6 schwarze 1457: sec = fn + 3;
1.11 schwarze 1458: } else if (0 == strncmp("cat", fn, 3)) {
1459: src_form |= MANDOC_FORM;
1460: sec = fn + 3;
1461: } else if (use_all)
1.6 schwarze 1462: sec = fn;
1463: else
1464: continue;
1465: } else if (NULL == arch && (use_all ||
1466: NULL == strchr(fn, '.')))
1467: arch = fn;
1468: else if (0 == use_all)
1.2 schwarze 1469: continue;
1470:
1471: buf[0] = '\0';
1472: strlcat(buf, dir, MAXPATHLEN);
1473: strlcat(buf, "/", MAXPATHLEN);
1474: sz = strlcat(buf, fn, MAXPATHLEN);
1475:
1.6 schwarze 1476: if (MAXPATHLEN <= sz) {
1477: fprintf(stderr, "%s: Path too long\n", dir);
1478: return(0);
1479: }
1480:
1481: if (verb > 2)
1482: printf("%s: Scanning\n", buf);
1483:
1484: if ( ! ofile_dirbuild(buf, sec, arch,
1.13 schwarze 1485: src_form, of))
1.6 schwarze 1486: return(0);
1487: }
1488: if (DT_REG != dp->d_type ||
1489: (NULL == psec && !use_all) ||
1490: !strcmp(MANDOC_DB, fn) ||
1491: !strcmp(MANDOC_IDX, fn))
1492: continue;
1493:
1494: /*
1.8 schwarze 1495: * By default, skip files where the file name suffix
1496: * does not agree with the section directory
1497: * they are located in.
1.6 schwarze 1498: */
1499:
1500: suffix = strrchr(fn, '.');
1501: if (0 == use_all) {
1502: if (NULL == suffix)
1.2 schwarze 1503: continue;
1.11 schwarze 1504: if ((MANDOC_SRC & src_form &&
1505: strcmp(suffix + 1, psec)) ||
1506: (MANDOC_FORM & src_form &&
1507: strcmp(suffix + 1, "0")))
1508: continue;
1509: }
1510: if (NULL != suffix) {
1511: if ('0' == suffix[1])
1512: src_form |= MANDOC_FORM;
1513: else if ('1' <= suffix[1] && '9' >= suffix[1])
1514: src_form |= MANDOC_SRC;
1515: }
1516:
1517:
1518: /*
1519: * Skip formatted manuals if a source version is
1520: * available. Ignore the age: it is very unlikely
1521: * that people install newer formatted base manuals
1522: * when they used to have source manuals before,
1523: * and in ports, old manuals get removed on update.
1524: */
1525: if (0 == use_all && MANDOC_FORM & src_form &&
1526: NULL != psec) {
1527: buf[0] = '\0';
1528: strlcat(buf, dir, MAXPATHLEN);
1529: p = strrchr(buf, '/');
1530: if (NULL == p)
1531: p = buf;
1532: else
1533: p++;
1534: if (0 == strncmp("cat", p, 3))
1535: memcpy(p, "man", 3);
1536: strlcat(buf, "/", MAXPATHLEN);
1537: sz = strlcat(buf, fn, MAXPATHLEN);
1538: if (sz >= MAXPATHLEN) {
1539: fprintf(stderr, "%s: Path too long\n", buf);
1.2 schwarze 1540: continue;
1.11 schwarze 1541: }
1542: q = strrchr(buf, '.');
1543: if (NULL != q && p < q++) {
1544: *q = '\0';
1545: sz = strlcat(buf, psec, MAXPATHLEN);
1546: if (sz >= MAXPATHLEN) {
1547: fprintf(stderr,
1548: "%s: Path too long\n", buf);
1549: continue;
1550: }
1551: if (0 == stat(buf, &sb))
1552: continue;
1553: }
1.2 schwarze 1554: }
1555:
1556: buf[0] = '\0';
1557: strlcat(buf, dir, MAXPATHLEN);
1558: strlcat(buf, "/", MAXPATHLEN);
1559: sz = strlcat(buf, fn, MAXPATHLEN);
1560: if (sz >= MAXPATHLEN) {
1561: fprintf(stderr, "%s: Path too long\n", dir);
1.11 schwarze 1562: continue;
1.2 schwarze 1563: }
1564:
1565: nof = mandoc_calloc(1, sizeof(struct of));
1566: nof->fname = mandoc_strdup(buf);
1.6 schwarze 1567: if (NULL != psec)
1568: nof->sec = mandoc_strdup(psec);
1569: if (NULL != parch)
1570: nof->arch = mandoc_strdup(parch);
1.11 schwarze 1571: nof->src_form = src_form;
1.8 schwarze 1572:
1573: /*
1574: * Remember the file name without the extension,
1575: * to be used as the page title in the database.
1576: */
1577:
1.6 schwarze 1578: if (NULL != suffix)
1579: *suffix = '\0';
1580: nof->title = mandoc_strdup(fn);
1.2 schwarze 1581:
1.11 schwarze 1582: /*
1583: * Add the structure to the list.
1584: */
1585:
1.2 schwarze 1586: if (verb > 2)
1587: printf("%s: Scheduling\n", buf);
1588: if (NULL == *of) {
1589: *of = nof;
1590: (*of)->first = nof;
1591: } else {
1592: nof->first = (*of)->first;
1593: (*of)->next = nof;
1594: *of = nof;
1595: }
1596: }
1597:
1.4 schwarze 1598: closedir(d);
1.2 schwarze 1599: return(1);
1600: }
1601:
1602: static void
1603: ofile_free(struct of *of)
1604: {
1605: struct of *nof;
1606:
1607: while (of) {
1608: nof = of->next;
1609: free(of->fname);
1.6 schwarze 1610: free(of->sec);
1611: free(of->arch);
1612: free(of->title);
1.2 schwarze 1613: free(of);
1614: of = nof;
1615: }
1616: }
1617:
1618: static void
1.1 schwarze 1619: usage(void)
1620: {
1621:
1.2 schwarze 1622: fprintf(stderr, "usage: %s [-v] "
1623: "[-d dir [files...] |"
1624: " -u dir [files...] |"
1625: " dir...]\n", progname);
1.1 schwarze 1626: }