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