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