Annotation of src/usr.bin/mandoc/mandocdb.c, Revision 1.24
1.24 ! schwarze 1: /* $Id: mandocdb.c,v 1.23 2011/12/10 16:53:38 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: /*
1.24 ! schwarze 519: * Try interpreting the file as mdoc(7) or man(7)
! 520: * source code, unless it is already known to be
! 521: * formatted. Fall back to formatted mode.
1.11 schwarze 522: */
523:
1.1 schwarze 524: mparse_reset(mp);
1.11 schwarze 525: mdoc = NULL;
526: man = NULL;
1.1 schwarze 527:
1.11 schwarze 528: if ((MANDOC_SRC & of->src_form ||
529: ! (MANDOC_FORM & of->src_form)) &&
530: MANDOCLEVEL_FATAL > mparse_readfd(mp, -1, fn))
531: mparse_result(mp, &mdoc, &man);
532:
533: if (NULL != mdoc) {
534: msec = mdoc_meta(mdoc)->msec;
535: arch = mdoc_meta(mdoc)->arch;
536: mtitle = mdoc_meta(mdoc)->title;
537: } else if (NULL != man) {
538: msec = man_meta(man)->msec;
539: arch = NULL;
540: mtitle = man_meta(man)->title;
541: } else {
542: msec = of->sec;
543: arch = of->arch;
544: mtitle = of->title;
1.1 schwarze 545: }
546:
1.6 schwarze 547: /*
1.8 schwarze 548: * By default, skip a file if the manual section
549: * and architecture given in the file disagree
550: * with the directory where the file is located.
1.6 schwarze 551: */
552:
553: if (0 == use_all) {
554: assert(of->sec);
555: assert(msec);
1.23 schwarze 556: if (strcasecmp(msec, of->sec))
1.6 schwarze 557: continue;
558:
559: if (NULL == arch) {
560: if (NULL != of->arch)
561: continue;
562: } else if (NULL == of->arch ||
1.23 schwarze 563: strcasecmp(arch, of->arch))
1.6 schwarze 564: continue;
565: }
566:
1.1 schwarze 567: if (NULL == arch)
568: arch = "";
569:
570: /*
1.8 schwarze 571: * By default, skip a file if the title given
572: * in the file disagrees with the file name.
573: * If both agree, use the file name as the title,
574: * because the one in the file usually is all caps.
1.6 schwarze 575: */
576:
577: assert(of->title);
578: assert(mtitle);
579:
580: if (0 == strcasecmp(mtitle, of->title))
581: mtitle = of->title;
582: else if (0 == use_all)
583: continue;
584:
585: /*
1.1 schwarze 586: * The index record value consists of a nil-terminated
587: * filename, a nil-terminated manual section, and a
588: * nil-terminated description. Since the description
589: * may not be set, we set a sentinel to see if we're
590: * going to write a nil byte in its place.
591: */
592:
1.2 schwarze 593: dbuf->len = 0;
1.12 schwarze 594: buf_append(dbuf, mdoc ? "mdoc" : (man ? "man" : "cat"));
1.2 schwarze 595: buf_appendb(dbuf, fn, strlen(fn) + 1);
596: buf_appendb(dbuf, msec, strlen(msec) + 1);
597: buf_appendb(dbuf, mtitle, strlen(mtitle) + 1);
598: buf_appendb(dbuf, arch, strlen(arch) + 1);
1.1 schwarze 599:
1.2 schwarze 600: sv = dbuf->len;
1.1 schwarze 601:
1.24 ! schwarze 602: /*
! 603: * Collect keyword/mask pairs.
! 604: * Each pair will become a new btree node.
! 605: */
1.1 schwarze 606:
1.24 ! schwarze 607: hash_reset(&hash);
1.1 schwarze 608: if (mdoc)
1.2 schwarze 609: pmdoc_node(hash, buf, dbuf,
1.1 schwarze 610: mdoc_node(mdoc), mdoc_meta(mdoc));
1.11 schwarze 611: else if (man)
1.2 schwarze 612: pman_node(hash, buf, dbuf, man_node(man));
1.11 schwarze 613: else
614: pformatted(hash, buf, dbuf, of);
1.1 schwarze 615:
616: /*
1.24 ! schwarze 617: * Reclaim an empty index record, if available.
! 618: * Use its record number for all new btree nodes.
1.1 schwarze 619: */
620:
1.24 ! schwarze 621: if (reccur > 0) {
! 622: --reccur;
! 623: rec = recs[(int)reccur];
! 624: } else if (maxrec > 0) {
! 625: rec = maxrec;
! 626: maxrec = 0;
! 627: } else
! 628: rec++;
1.17 schwarze 629: vbuf.rec = htobe32(rec);
1.24 ! schwarze 630:
! 631: /*
! 632: * Copy from the in-memory hashtable of pending
! 633: * keyword/mask pairs into the database.
! 634: */
! 635:
1.1 schwarze 636: seq = R_FIRST;
637: while (0 == (ch = (*hash->seq)(hash, &key, &val, seq))) {
638: seq = R_NEXT;
1.17 schwarze 639: vbuf.mask = htobe64(*(uint64_t *)val.data);
1.9 schwarze 640: val.size = sizeof(struct db_val);
641: val.data = &vbuf;
1.2 schwarze 642: dbt_put(db, dbf, &key, &val);
1.1 schwarze 643: }
644: if (ch < 0) {
645: perror("hash");
646: exit((int)MANDOCLEVEL_SYSERR);
647: }
648:
649: /*
650: * Apply to the index. If we haven't had a description
651: * set, put an empty one in now.
652: */
653:
1.2 schwarze 654: if (dbuf->len == sv)
655: buf_appendb(dbuf, "", 1);
1.1 schwarze 656:
657: key.data = &rec;
658: key.size = sizeof(recno_t);
659:
1.2 schwarze 660: val.data = dbuf->cp;
661: val.size = dbuf->len;
1.1 schwarze 662:
1.2 schwarze 663: if (verb)
1.1 schwarze 664: printf("%s: Added index\n", fn);
1.16 schwarze 665:
1.2 schwarze 666: dbt_put(idx, idxf, &key, &val);
667: }
668: }
669:
670: /*
671: * Scan through all entries in the index file `idx' and prune those
672: * entries in `ofile'.
673: * Pruning consists of removing from `db', then invalidating the entry
674: * in `idx' (zeroing its value size).
675: */
676: static void
677: index_prune(const struct of *ofile, DB *db, const char *dbf,
1.20 schwarze 678: DB *idx, const char *idxf, recno_t *maxrec,
679: recno_t **recs, size_t *recsz, size_t *reccur)
1.2 schwarze 680: {
681: const struct of *of;
1.16 schwarze 682: const char *fn, *cp;
1.9 schwarze 683: struct db_val *vbuf;
1.2 schwarze 684: unsigned seq, sseq;
685: DBT key, val;
686: int ch;
687:
1.20 schwarze 688: *reccur = 0;
1.2 schwarze 689: seq = R_FIRST;
690: while (0 == (ch = (*idx->seq)(idx, &key, &val, seq))) {
691: seq = R_NEXT;
692: *maxrec = *(recno_t *)key.data;
1.16 schwarze 693: cp = val.data;
694:
695: /* Deleted records are zero-sized. Skip them. */
696:
697: if (0 == val.size)
698: goto cont;
699:
700: /*
701: * Make sure we're sane.
702: * Read past our mdoc/man/cat type to the next string,
703: * then make sure it's bounded by a NUL.
704: * Failing any of these, we go into our error handler.
705: */
706:
707: if (NULL == (fn = memchr(cp, '\0', val.size)))
708: break;
709: if (++fn - cp >= (int)val.size)
710: break;
711: if (NULL == memchr(fn, '\0', val.size - (fn - cp)))
712: break;
713:
714: /*
715: * Search for the file in those we care about.
716: * XXX: build this into a tree. Too slow.
717: */
1.2 schwarze 718:
719: for (of = ofile; of; of = of->next)
720: if (0 == strcmp(fn, of->fname))
721: break;
722:
723: if (NULL == of)
724: continue;
725:
1.16 schwarze 726: /*
727: * Search through the keyword database, throwing out all
728: * references to our file.
729: */
730:
1.2 schwarze 731: sseq = R_FIRST;
732: while (0 == (ch = (*db->seq)(db, &key, &val, sseq))) {
733: sseq = R_NEXT;
1.16 schwarze 734: if (sizeof(struct db_val) != val.size)
735: break;
736:
1.9 schwarze 737: vbuf = val.data;
1.17 schwarze 738: if (*maxrec != betoh32(vbuf->rec))
1.2 schwarze 739: continue;
1.16 schwarze 740:
741: if ((ch = (*db->del)(db, &key, R_CURSOR)) < 0)
1.2 schwarze 742: break;
743: }
1.16 schwarze 744:
1.2 schwarze 745: if (ch < 0) {
746: perror(dbf);
747: exit((int)MANDOCLEVEL_SYSERR);
1.16 schwarze 748: } else if (1 != ch) {
749: fprintf(stderr, "%s: Corrupt database\n", dbf);
750: exit((int)MANDOCLEVEL_SYSERR);
1.2 schwarze 751: }
1.1 schwarze 752:
1.2 schwarze 753: if (verb)
754: printf("%s: Deleted index\n", fn);
1.1 schwarze 755:
1.2 schwarze 756: val.size = 0;
757: ch = (*idx->put)(idx, &key, &val, R_CURSOR);
1.1 schwarze 758:
1.16 schwarze 759: if (ch < 0)
760: break;
761: cont:
1.20 schwarze 762: if (*reccur >= *recsz) {
1.2 schwarze 763: *recsz += MANDOC_SLOP;
764: *recs = mandoc_realloc
765: (*recs, *recsz * sizeof(recno_t));
766: }
1.1 schwarze 767:
1.20 schwarze 768: (*recs)[(int)*reccur] = *maxrec;
769: (*reccur)++;
1.2 schwarze 770: }
1.16 schwarze 771:
772: if (ch < 0) {
773: perror(idxf);
774: exit((int)MANDOCLEVEL_SYSERR);
775: } else if (1 != ch) {
776: fprintf(stderr, "%s: Corrupt index\n", idxf);
777: exit((int)MANDOCLEVEL_SYSERR);
778: }
779:
1.2 schwarze 780: (*maxrec)++;
1.1 schwarze 781: }
782:
783: /*
784: * Grow the buffer (if necessary) and copy in a binary string.
785: */
786: static void
787: buf_appendb(struct buf *buf, const void *cp, size_t sz)
788: {
789:
790: /* Overshoot by MANDOC_BUFSZ. */
791:
792: while (buf->len + sz >= buf->size) {
793: buf->size = buf->len + sz + MANDOC_BUFSZ;
794: buf->cp = mandoc_realloc(buf->cp, buf->size);
795: }
796:
797: memcpy(buf->cp + (int)buf->len, cp, sz);
798: buf->len += sz;
799: }
800:
801: /*
802: * Append a nil-terminated string to the buffer.
803: * This can be invoked multiple times.
804: * The buffer string will be nil-terminated.
805: * If invoked multiple times, a space is put between strings.
806: */
807: static void
808: buf_append(struct buf *buf, const char *cp)
809: {
810: size_t sz;
811:
812: if (0 == (sz = strlen(cp)))
813: return;
814:
815: if (buf->len)
816: buf->cp[(int)buf->len - 1] = ' ';
817:
818: buf_appendb(buf, cp, sz + 1);
819: }
820:
821: /*
822: * Recursively add all text from a given node.
823: * This is optimised for general mdoc nodes in this context, which do
824: * not consist of subexpressions and having a recursive call for n->next
825: * would be wasteful.
826: * The "f" variable should be 0 unless called from pmdoc_Nd for the
827: * description buffer, which does not start at the beginning of the
828: * buffer.
829: */
830: static void
831: buf_appendmdoc(struct buf *buf, const struct mdoc_node *n, int f)
832: {
833:
834: for ( ; n; n = n->next) {
835: if (n->child)
836: buf_appendmdoc(buf, n->child, f);
837:
838: if (MDOC_TEXT == n->type && f) {
839: f = 0;
840: buf_appendb(buf, n->string,
841: strlen(n->string) + 1);
842: } else if (MDOC_TEXT == n->type)
843: buf_append(buf, n->string);
844:
845: }
846: }
847:
848: static void
849: hash_reset(DB **db)
850: {
851: DB *hash;
852:
853: if (NULL != (hash = *db))
854: (*hash->close)(hash);
855:
1.2 schwarze 856: *db = dbopen(NULL, O_CREAT|O_RDWR, 0644, DB_HASH, NULL);
1.1 schwarze 857: if (NULL == *db) {
858: perror("hash");
859: exit((int)MANDOCLEVEL_SYSERR);
860: }
861: }
862:
863: /* ARGSUSED */
1.19 schwarze 864: static int
865: pmdoc_head(MDOC_ARGS)
866: {
867:
868: return(MDOC_HEAD == n->type);
869: }
870:
871: /* ARGSUSED */
872: static int
873: pmdoc_body(MDOC_ARGS)
874: {
875:
876: return(MDOC_BODY == n->type);
877: }
878:
879: /* ARGSUSED */
880: static int
1.1 schwarze 881: pmdoc_Fd(MDOC_ARGS)
882: {
883: const char *start, *end;
884: size_t sz;
1.19 schwarze 885:
1.1 schwarze 886: if (SEC_SYNOPSIS != n->sec)
1.19 schwarze 887: return(0);
1.1 schwarze 888: if (NULL == (n = n->child) || MDOC_TEXT != n->type)
1.19 schwarze 889: return(0);
1.1 schwarze 890:
891: /*
892: * Only consider those `Fd' macro fields that begin with an
893: * "inclusion" token (versus, e.g., #define).
894: */
895: if (strcmp("#include", n->string))
1.19 schwarze 896: return(0);
1.1 schwarze 897:
898: if (NULL == (n = n->next) || MDOC_TEXT != n->type)
1.19 schwarze 899: return(0);
1.1 schwarze 900:
901: /*
902: * Strip away the enclosing angle brackets and make sure we're
903: * not zero-length.
904: */
905:
906: start = n->string;
907: if ('<' == *start || '"' == *start)
908: start++;
909:
910: if (0 == (sz = strlen(start)))
1.19 schwarze 911: return(0);
1.1 schwarze 912:
913: end = &start[(int)sz - 1];
914: if ('>' == *end || '"' == *end)
915: end--;
916:
917: assert(end >= start);
918:
919: buf_appendb(buf, start, (size_t)(end - start + 1));
920: buf_appendb(buf, "", 1);
1.19 schwarze 921: return(1);
1.1 schwarze 922: }
923:
924: /* ARGSUSED */
1.19 schwarze 925: static int
926: pmdoc_In(MDOC_ARGS)
1.1 schwarze 927: {
928:
929: if (NULL == n->child || MDOC_TEXT != n->child->type)
1.19 schwarze 930: return(0);
1.1 schwarze 931:
932: buf_append(buf, n->child->string);
1.19 schwarze 933: return(1);
1.1 schwarze 934: }
935:
936: /* ARGSUSED */
1.19 schwarze 937: static int
1.1 schwarze 938: pmdoc_Fn(MDOC_ARGS)
939: {
1.19 schwarze 940: struct mdoc_node *nn;
1.1 schwarze 941: const char *cp;
942:
1.19 schwarze 943: nn = n->child;
944:
945: if (NULL == nn || MDOC_TEXT != nn->type)
946: return(0);
947:
948: /* .Fn "struct type *name" "char *arg" */
1.1 schwarze 949:
1.19 schwarze 950: cp = strrchr(nn->string, ' ');
1.1 schwarze 951: if (NULL == cp)
1.19 schwarze 952: cp = nn->string;
1.1 schwarze 953:
954: /* Strip away pointer symbol. */
955:
956: while ('*' == *cp)
957: cp++;
958:
1.19 schwarze 959: /* Store the function name. */
960:
1.1 schwarze 961: buf_append(buf, cp);
1.5 schwarze 962: hash_put(hash, buf, TYPE_Fn);
1.19 schwarze 963:
964: /* Store the function type. */
965:
966: if (nn->string < cp) {
967: buf->len = 0;
968: buf_appendb(buf, nn->string, cp - nn->string);
969: buf_appendb(buf, "", 1);
970: hash_put(hash, buf, TYPE_Ft);
971: }
972:
973: /* Store the arguments. */
974:
975: for (nn = nn->next; nn; nn = nn->next) {
976: if (MDOC_TEXT != nn->type)
977: continue;
978: buf->len = 0;
979: buf_append(buf, nn->string);
980: hash_put(hash, buf, TYPE_Fa);
981: }
982:
983: return(0);
1.1 schwarze 984: }
985:
986: /* ARGSUSED */
1.19 schwarze 987: static int
1.1 schwarze 988: pmdoc_St(MDOC_ARGS)
989: {
1.19 schwarze 990:
1.1 schwarze 991: if (NULL == n->child || MDOC_TEXT != n->child->type)
1.19 schwarze 992: return(0);
1.1 schwarze 993:
994: buf_append(buf, n->child->string);
1.19 schwarze 995: return(1);
1.1 schwarze 996: }
997:
998: /* ARGSUSED */
1.19 schwarze 999: static int
1.1 schwarze 1000: pmdoc_Xr(MDOC_ARGS)
1001: {
1002:
1003: if (NULL == (n = n->child))
1.19 schwarze 1004: return(0);
1.1 schwarze 1005:
1006: buf_appendb(buf, n->string, strlen(n->string));
1007:
1008: if (NULL != (n = n->next)) {
1009: buf_appendb(buf, ".", 1);
1010: buf_appendb(buf, n->string, strlen(n->string) + 1);
1011: } else
1012: buf_appendb(buf, ".", 2);
1013:
1.19 schwarze 1014: return(1);
1.1 schwarze 1015: }
1016:
1017: /* ARGSUSED */
1.19 schwarze 1018: static int
1.1 schwarze 1019: pmdoc_Nd(MDOC_ARGS)
1020: {
1021:
1022: if (MDOC_BODY != n->type)
1.19 schwarze 1023: return(0);
1.1 schwarze 1024:
1025: buf_appendmdoc(dbuf, n->child, 1);
1.19 schwarze 1026: return(1);
1.1 schwarze 1027: }
1028:
1029: /* ARGSUSED */
1.19 schwarze 1030: static int
1031: pmdoc_Nm(MDOC_ARGS)
1.1 schwarze 1032: {
1033:
1.19 schwarze 1034: if (SEC_NAME == n->sec)
1035: return(1);
1036: else if (SEC_SYNOPSIS != n->sec || MDOC_HEAD != n->type)
1037: return(0);
1.1 schwarze 1038:
1.19 schwarze 1039: if (NULL == n->child)
1040: buf_append(buf, m->name);
1.1 schwarze 1041:
1.19 schwarze 1042: return(1);
1.1 schwarze 1043: }
1044:
1045: /* ARGSUSED */
1.19 schwarze 1046: static int
1047: pmdoc_Sh(MDOC_ARGS)
1.1 schwarze 1048: {
1049:
1.19 schwarze 1050: return(SEC_CUSTOM == n->sec && MDOC_HEAD == n->type);
1.1 schwarze 1051: }
1052:
1053: static void
1.9 schwarze 1054: hash_put(DB *db, const struct buf *buf, uint64_t mask)
1.1 schwarze 1055: {
1056: DBT key, val;
1057: int rc;
1058:
1059: if (buf->len < 2)
1060: return;
1061:
1062: key.data = buf->cp;
1063: key.size = buf->len;
1064:
1065: if ((rc = (*db->get)(db, &key, &val, 0)) < 0) {
1066: perror("hash");
1067: exit((int)MANDOCLEVEL_SYSERR);
1068: } else if (0 == rc)
1.9 schwarze 1069: mask |= *(uint64_t *)val.data;
1.1 schwarze 1070:
1071: val.data = &mask;
1.9 schwarze 1072: val.size = sizeof(uint64_t);
1.1 schwarze 1073:
1074: if ((rc = (*db->put)(db, &key, &val, 0)) < 0) {
1075: perror("hash");
1076: exit((int)MANDOCLEVEL_SYSERR);
1077: }
1078: }
1079:
1080: static void
1081: dbt_put(DB *db, const char *dbn, DBT *key, DBT *val)
1082: {
1083:
1084: assert(key->size);
1085: assert(val->size);
1086:
1087: if (0 == (*db->put)(db, key, val, 0))
1088: return;
1089:
1090: perror(dbn);
1091: exit((int)MANDOCLEVEL_SYSERR);
1092: /* NOTREACHED */
1093: }
1094:
1095: /*
1096: * Call out to per-macro handlers after clearing the persistent database
1097: * key. If the macro sets the database key, flush it to the database.
1098: */
1099: static void
1100: pmdoc_node(MDOC_ARGS)
1101: {
1102:
1103: if (NULL == n)
1104: return;
1105:
1106: switch (n->type) {
1107: case (MDOC_HEAD):
1108: /* FALLTHROUGH */
1109: case (MDOC_BODY):
1110: /* FALLTHROUGH */
1111: case (MDOC_TAIL):
1112: /* FALLTHROUGH */
1113: case (MDOC_BLOCK):
1114: /* FALLTHROUGH */
1115: case (MDOC_ELEM):
1.19 schwarze 1116: buf->len = 0;
1117:
1118: /*
1119: * Both NULL handlers and handlers returning true
1120: * request using the data. Only skip the element
1121: * when the handler returns false.
1122: */
1123:
1124: if (NULL != mdocs[n->tok].fp &&
1125: 0 == (*mdocs[n->tok].fp)(hash, buf, dbuf, n, m))
1.1 schwarze 1126: break;
1127:
1.19 schwarze 1128: /*
1129: * For many macros, use the text from all children.
1130: * Set zero flags for macros not needing this.
1131: * In that case, the handler must fill the buffer.
1132: */
1133:
1134: if (MDOCF_CHILD & mdocs[n->tok].flags)
1135: buf_appendmdoc(buf, n->child, 0);
1136:
1137: /*
1138: * Cover the most common case:
1139: * Automatically stage one string per element.
1140: * Set a zero mask for macros not needing this.
1141: * Additional staging can be done in the handler.
1142: */
1143:
1144: if (mdocs[n->tok].mask)
1145: hash_put(hash, buf, mdocs[n->tok].mask);
1.1 schwarze 1146: break;
1147: default:
1148: break;
1149: }
1150:
1151: pmdoc_node(hash, buf, dbuf, n->child, m);
1152: pmdoc_node(hash, buf, dbuf, n->next, m);
1153: }
1154:
1155: static int
1156: pman_node(MAN_ARGS)
1157: {
1158: const struct man_node *head, *body;
1159: const char *start, *sv;
1160: size_t sz;
1161:
1162: if (NULL == n)
1163: return(0);
1164:
1165: /*
1166: * We're only searching for one thing: the first text child in
1167: * the BODY of a NAME section. Since we don't keep track of
1168: * sections in -man, run some hoops to find out whether we're in
1169: * the correct section or not.
1170: */
1171:
1172: if (MAN_BODY == n->type && MAN_SH == n->tok) {
1173: body = n;
1174: assert(body->parent);
1175: if (NULL != (head = body->parent->head) &&
1176: 1 == head->nchild &&
1177: NULL != (head = (head->child)) &&
1178: MAN_TEXT == head->type &&
1179: 0 == strcmp(head->string, "NAME") &&
1180: NULL != (body = body->child) &&
1181: MAN_TEXT == body->type) {
1182:
1183: assert(body->string);
1184: start = sv = body->string;
1185:
1186: /*
1187: * Go through a special heuristic dance here.
1188: * This is why -man manuals are great!
1189: * (I'm being sarcastic: my eyes are bleeding.)
1190: * Conventionally, one or more manual names are
1191: * comma-specified prior to a whitespace, then a
1192: * dash, then a description. Try to puzzle out
1193: * the name parts here.
1194: */
1195:
1196: for ( ;; ) {
1197: sz = strcspn(start, " ,");
1198: if ('\0' == start[(int)sz])
1199: break;
1200:
1201: buf->len = 0;
1202: buf_appendb(buf, start, sz);
1203: buf_appendb(buf, "", 1);
1204:
1.5 schwarze 1205: hash_put(hash, buf, TYPE_Nm);
1.1 schwarze 1206:
1207: if (' ' == start[(int)sz]) {
1208: start += (int)sz + 1;
1209: break;
1210: }
1211:
1212: assert(',' == start[(int)sz]);
1213: start += (int)sz + 1;
1214: while (' ' == *start)
1215: start++;
1216: }
1217:
1218: buf->len = 0;
1219:
1220: if (sv == start) {
1221: buf_append(buf, start);
1222: return(1);
1223: }
1224:
1225: while (' ' == *start)
1226: start++;
1227:
1228: if (0 == strncmp(start, "-", 1))
1229: start += 1;
1230: else if (0 == strncmp(start, "\\-", 2))
1231: start += 2;
1232: else if (0 == strncmp(start, "\\(en", 4))
1233: start += 4;
1234: else if (0 == strncmp(start, "\\(em", 4))
1235: start += 4;
1236:
1237: while (' ' == *start)
1238: start++;
1239:
1240: sz = strlen(start) + 1;
1241: buf_appendb(dbuf, start, sz);
1242: buf_appendb(buf, start, sz);
1243:
1.5 schwarze 1244: hash_put(hash, buf, TYPE_Nd);
1.1 schwarze 1245: }
1246: }
1247:
1.4 schwarze 1248: for (n = n->child; n; n = n->next)
1249: if (pman_node(hash, buf, dbuf, n))
1250: return(1);
1.1 schwarze 1251:
1252: return(0);
1253: }
1254:
1.11 schwarze 1255: /*
1256: * Parse a formatted manual page.
1257: * By necessity, this involves rather crude guesswork.
1258: */
1259: static void
1260: pformatted(DB *hash, struct buf *buf, struct buf *dbuf,
1261: const struct of *of)
1262: {
1263: FILE *stream;
1264: char *line, *p;
1265: size_t len, plen;
1266:
1267: if (NULL == (stream = fopen(of->fname, "r"))) {
1268: perror(of->fname);
1269: return;
1270: }
1271:
1272: /*
1273: * Always use the title derived from the filename up front,
1274: * do not even try to find it in the file. This also makes
1275: * sure we don't end up with an orphan index record, even if
1276: * the file content turns out to be completely unintelligible.
1277: */
1278:
1279: buf->len = 0;
1280: buf_append(buf, of->title);
1281: hash_put(hash, buf, TYPE_Nm);
1282:
1.22 schwarze 1283: /* Skip to first blank line. */
1.11 schwarze 1284:
1.22 schwarze 1285: while (NULL != (line = fgetln(stream, &len)))
1286: if ('\n' == *line)
1287: break;
1288:
1289: /*
1290: * Assume the first line that is not indented
1291: * is the first section header. Skip to it.
1292: */
1293:
1294: while (NULL != (line = fgetln(stream, &len)))
1295: if ('\n' != *line && ' ' != *line)
1296: break;
1.11 schwarze 1297:
1298: /*
1.22 schwarze 1299: * If no page content can be found, or the input line
1300: * is already the next section header, or there is no
1301: * trailing newline, reuse the page title as the page
1302: * description.
1.11 schwarze 1303: */
1304:
1.22 schwarze 1305: line = fgetln(stream, &len);
1306: if (NULL == line || ' ' != *line || '\n' != line[(int)len - 1]) {
1.11 schwarze 1307: buf_appendb(dbuf, buf->cp, buf->size);
1308: hash_put(hash, buf, TYPE_Nd);
1309: fclose(stream);
1310: return;
1311: }
1.22 schwarze 1312:
1313: line[(int)--len] = '\0';
1.11 schwarze 1314:
1315: /*
1.22 schwarze 1316: * Skip to the first dash.
1317: * Use the remaining line as the description (no more than 70
1318: * bytes).
1.11 schwarze 1319: */
1320:
1.22 schwarze 1321: if (NULL != (p = strstr(line, "- "))) {
1322: for (p += 2; ' ' == *p || '\b' == *p; p++)
1323: /* Skip to next word. */ ;
1324: } else
1.11 schwarze 1325: p = line;
1.22 schwarze 1326:
1327: if ((plen = strlen(p)) > 70) {
1328: plen = 70;
1329: p[plen] = '\0';
1.11 schwarze 1330: }
1331:
1.22 schwarze 1332: /* Strip backspace-encoding from line. */
1333:
1334: while (NULL != (line = memchr(p, '\b', plen))) {
1335: len = line - p;
1336: if (0 == len) {
1337: memmove(line, line + 1, plen--);
1338: continue;
1339: }
1340: memmove(line - 1, line + 1, plen - len);
1341: plen -= 2;
1342: }
1.11 schwarze 1343:
1.22 schwarze 1344: buf_appendb(dbuf, p, plen + 1);
1.11 schwarze 1345: buf->len = 0;
1.22 schwarze 1346: buf_appendb(buf, p, plen + 1);
1.11 schwarze 1347: hash_put(hash, buf, TYPE_Nd);
1.22 schwarze 1348: fclose(stream);
1.11 schwarze 1349: }
1350:
1.1 schwarze 1351: static void
1.13 schwarze 1352: ofile_argbuild(int argc, char *argv[], struct of **of)
1.2 schwarze 1353: {
1.6 schwarze 1354: char buf[MAXPATHLEN];
1355: char *sec, *arch, *title, *p;
1.11 schwarze 1356: int i, src_form;
1.2 schwarze 1357: struct of *nof;
1358:
1359: for (i = 0; i < argc; i++) {
1.6 schwarze 1360:
1361: /*
1.8 schwarze 1362: * Try to infer the manual section, architecture and
1363: * page title from the path, assuming it looks like
1.11 schwarze 1364: * man*[/<arch>]/<title>.<section> or
1365: * cat<section>[/<arch>]/<title>.0
1.6 schwarze 1366: */
1367:
1368: if (strlcpy(buf, argv[i], sizeof(buf)) >= sizeof(buf)) {
1369: fprintf(stderr, "%s: Path too long\n", argv[i]);
1370: continue;
1371: }
1372: sec = arch = title = NULL;
1.11 schwarze 1373: src_form = 0;
1.6 schwarze 1374: p = strrchr(buf, '\0');
1375: while (p-- > buf) {
1376: if (NULL == sec && '.' == *p) {
1377: sec = p + 1;
1378: *p = '\0';
1.11 schwarze 1379: if ('0' == *sec)
1380: src_form |= MANDOC_FORM;
1381: else if ('1' <= *sec && '9' >= *sec)
1382: src_form |= MANDOC_SRC;
1.6 schwarze 1383: continue;
1384: }
1385: if ('/' != *p)
1386: continue;
1387: if (NULL == title) {
1388: title = p + 1;
1389: *p = '\0';
1390: continue;
1391: }
1.18 schwarze 1392: if (0 == strncmp("man", p + 1, 3))
1.11 schwarze 1393: src_form |= MANDOC_SRC;
1.18 schwarze 1394: else if (0 == strncmp("cat", p + 1, 3))
1.11 schwarze 1395: src_form |= MANDOC_FORM;
1.18 schwarze 1396: else
1.6 schwarze 1397: arch = p + 1;
1398: break;
1399: }
1400: if (NULL == title)
1401: title = buf;
1402:
1403: /*
1404: * Build the file structure.
1405: */
1406:
1.2 schwarze 1407: nof = mandoc_calloc(1, sizeof(struct of));
1.6 schwarze 1408: nof->fname = mandoc_strdup(argv[i]);
1409: if (NULL != sec)
1410: nof->sec = mandoc_strdup(sec);
1411: if (NULL != arch)
1412: nof->arch = mandoc_strdup(arch);
1413: nof->title = mandoc_strdup(title);
1.11 schwarze 1414: nof->src_form = src_form;
1.6 schwarze 1415:
1416: /*
1417: * Add the structure to the list.
1418: */
1419:
1.2 schwarze 1420: if (verb > 2)
1421: printf("%s: Scheduling\n", argv[i]);
1422: if (NULL == *of) {
1423: *of = nof;
1424: (*of)->first = nof;
1425: } else {
1426: nof->first = (*of)->first;
1427: (*of)->next = nof;
1428: *of = nof;
1429: }
1430: }
1431: }
1432:
1433: /*
1434: * Recursively build up a list of files to parse.
1435: * We use this instead of ftw() and so on because I don't want global
1436: * variables hanging around.
1437: * This ignores the mandoc.db and mandoc.index files, but assumes that
1438: * everything else is a manual.
1439: * Pass in a pointer to a NULL structure for the first invocation.
1440: */
1441: static int
1.6 schwarze 1442: ofile_dirbuild(const char *dir, const char* psec, const char *parch,
1.13 schwarze 1443: int p_src_form, struct of **of)
1.2 schwarze 1444: {
1445: char buf[MAXPATHLEN];
1.11 schwarze 1446: struct stat sb;
1.2 schwarze 1447: size_t sz;
1448: DIR *d;
1.6 schwarze 1449: const char *fn, *sec, *arch;
1.11 schwarze 1450: char *p, *q, *suffix;
1.2 schwarze 1451: struct of *nof;
1452: struct dirent *dp;
1.11 schwarze 1453: int src_form;
1.2 schwarze 1454:
1455: if (NULL == (d = opendir(dir))) {
1456: perror(dir);
1457: return(0);
1458: }
1459:
1460: while (NULL != (dp = readdir(d))) {
1461: fn = dp->d_name;
1.6 schwarze 1462:
1463: if ('.' == *fn)
1464: continue;
1465:
1.11 schwarze 1466: src_form = p_src_form;
1467:
1.2 schwarze 1468: if (DT_DIR == dp->d_type) {
1.6 schwarze 1469: sec = psec;
1470: arch = parch;
1471:
1472: /*
1.8 schwarze 1473: * By default, only use directories called:
1.11 schwarze 1474: * man<section>/[<arch>/] or
1475: * cat<section>/[<arch>/]
1.6 schwarze 1476: */
1477:
1478: if (NULL == sec) {
1.11 schwarze 1479: if(0 == strncmp("man", fn, 3)) {
1480: src_form |= MANDOC_SRC;
1.6 schwarze 1481: sec = fn + 3;
1.11 schwarze 1482: } else if (0 == strncmp("cat", fn, 3)) {
1483: src_form |= MANDOC_FORM;
1484: sec = fn + 3;
1485: } else if (use_all)
1.6 schwarze 1486: sec = fn;
1487: else
1488: continue;
1489: } else if (NULL == arch && (use_all ||
1490: NULL == strchr(fn, '.')))
1491: arch = fn;
1492: else if (0 == use_all)
1.2 schwarze 1493: continue;
1494:
1495: buf[0] = '\0';
1496: strlcat(buf, dir, MAXPATHLEN);
1497: strlcat(buf, "/", MAXPATHLEN);
1498: sz = strlcat(buf, fn, MAXPATHLEN);
1499:
1.6 schwarze 1500: if (MAXPATHLEN <= sz) {
1501: fprintf(stderr, "%s: Path too long\n", dir);
1502: return(0);
1503: }
1504:
1505: if (verb > 2)
1506: printf("%s: Scanning\n", buf);
1507:
1508: if ( ! ofile_dirbuild(buf, sec, arch,
1.13 schwarze 1509: src_form, of))
1.6 schwarze 1510: return(0);
1511: }
1512: if (DT_REG != dp->d_type ||
1513: (NULL == psec && !use_all) ||
1514: !strcmp(MANDOC_DB, fn) ||
1515: !strcmp(MANDOC_IDX, fn))
1516: continue;
1517:
1518: /*
1.8 schwarze 1519: * By default, skip files where the file name suffix
1520: * does not agree with the section directory
1521: * they are located in.
1.6 schwarze 1522: */
1523:
1524: suffix = strrchr(fn, '.');
1525: if (0 == use_all) {
1526: if (NULL == suffix)
1.2 schwarze 1527: continue;
1.11 schwarze 1528: if ((MANDOC_SRC & src_form &&
1529: strcmp(suffix + 1, psec)) ||
1530: (MANDOC_FORM & src_form &&
1531: strcmp(suffix + 1, "0")))
1532: continue;
1533: }
1534: if (NULL != suffix) {
1535: if ('0' == suffix[1])
1536: src_form |= MANDOC_FORM;
1537: else if ('1' <= suffix[1] && '9' >= suffix[1])
1538: src_form |= MANDOC_SRC;
1539: }
1540:
1541:
1542: /*
1543: * Skip formatted manuals if a source version is
1544: * available. Ignore the age: it is very unlikely
1545: * that people install newer formatted base manuals
1546: * when they used to have source manuals before,
1547: * and in ports, old manuals get removed on update.
1548: */
1549: if (0 == use_all && MANDOC_FORM & src_form &&
1550: NULL != psec) {
1551: buf[0] = '\0';
1552: strlcat(buf, dir, MAXPATHLEN);
1553: p = strrchr(buf, '/');
1.23 schwarze 1554: if (NULL != parch && NULL != p)
1555: for (p--; p > buf; p--)
1556: if ('/' == *p)
1557: break;
1.11 schwarze 1558: if (NULL == p)
1559: p = buf;
1560: else
1561: p++;
1562: if (0 == strncmp("cat", p, 3))
1563: memcpy(p, "man", 3);
1564: strlcat(buf, "/", MAXPATHLEN);
1565: sz = strlcat(buf, fn, MAXPATHLEN);
1566: if (sz >= MAXPATHLEN) {
1567: fprintf(stderr, "%s: Path too long\n", buf);
1.2 schwarze 1568: continue;
1.11 schwarze 1569: }
1570: q = strrchr(buf, '.');
1571: if (NULL != q && p < q++) {
1572: *q = '\0';
1573: sz = strlcat(buf, psec, MAXPATHLEN);
1574: if (sz >= MAXPATHLEN) {
1575: fprintf(stderr,
1576: "%s: Path too long\n", buf);
1577: continue;
1578: }
1579: if (0 == stat(buf, &sb))
1580: continue;
1581: }
1.2 schwarze 1582: }
1583:
1584: buf[0] = '\0';
1585: strlcat(buf, dir, MAXPATHLEN);
1586: strlcat(buf, "/", MAXPATHLEN);
1587: sz = strlcat(buf, fn, MAXPATHLEN);
1588: if (sz >= MAXPATHLEN) {
1589: fprintf(stderr, "%s: Path too long\n", dir);
1.11 schwarze 1590: continue;
1.2 schwarze 1591: }
1592:
1593: nof = mandoc_calloc(1, sizeof(struct of));
1594: nof->fname = mandoc_strdup(buf);
1.6 schwarze 1595: if (NULL != psec)
1596: nof->sec = mandoc_strdup(psec);
1597: if (NULL != parch)
1598: nof->arch = mandoc_strdup(parch);
1.11 schwarze 1599: nof->src_form = src_form;
1.8 schwarze 1600:
1601: /*
1602: * Remember the file name without the extension,
1603: * to be used as the page title in the database.
1604: */
1605:
1.6 schwarze 1606: if (NULL != suffix)
1607: *suffix = '\0';
1608: nof->title = mandoc_strdup(fn);
1.2 schwarze 1609:
1.11 schwarze 1610: /*
1611: * Add the structure to the list.
1612: */
1613:
1.2 schwarze 1614: if (verb > 2)
1615: printf("%s: Scheduling\n", buf);
1616: if (NULL == *of) {
1617: *of = nof;
1618: (*of)->first = nof;
1619: } else {
1620: nof->first = (*of)->first;
1621: (*of)->next = nof;
1622: *of = nof;
1623: }
1624: }
1625:
1.4 schwarze 1626: closedir(d);
1.2 schwarze 1627: return(1);
1628: }
1629:
1630: static void
1631: ofile_free(struct of *of)
1632: {
1633: struct of *nof;
1634:
1635: while (of) {
1636: nof = of->next;
1637: free(of->fname);
1.6 schwarze 1638: free(of->sec);
1639: free(of->arch);
1640: free(of->title);
1.2 schwarze 1641: free(of);
1642: of = nof;
1643: }
1644: }
1645:
1646: static void
1.1 schwarze 1647: usage(void)
1648: {
1649:
1.2 schwarze 1650: fprintf(stderr, "usage: %s [-v] "
1651: "[-d dir [files...] |"
1652: " -u dir [files...] |"
1653: " dir...]\n", progname);
1.1 schwarze 1654: }