Annotation of mandoc/mandocdb.c, Revision 1.30
1.30 ! kristaps 1: /* $Id: mandocdb.c,v 1.29 2011/12/09 00:49:01 kristaps Exp $ */
1.1 kristaps 2: /*
3: * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.12 schwarze 4: * Copyright (c) 2011 Ingo Schwarze <schwarze@openbsd.org>
1.1 kristaps 5: *
6: * Permission to use, copy, modify, and distribute this software for any
7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
9: *
10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17: */
18: #ifdef HAVE_CONFIG_H
19: #include "config.h"
20: #endif
21:
22: #include <sys/param.h>
1.14 schwarze 23: #include <sys/types.h>
24: #include <sys/stat.h>
1.1 kristaps 25:
26: #include <assert.h>
1.4 kristaps 27: #include <dirent.h>
1.1 kristaps 28: #include <fcntl.h>
29: #include <getopt.h>
30: #include <stdio.h>
31: #include <stdint.h>
32: #include <stdlib.h>
33: #include <string.h>
1.17 schwarze 34: #include <unistd.h>
1.1 kristaps 35:
1.21 kristaps 36: #if defined(__linux__)
37: # include <endian.h>
1.1 kristaps 38: # include <db_185.h>
1.21 kristaps 39: #elif defined(__APPLE__)
40: # include <libkern/OSByteOrder.h>
41: # include <db.h>
1.1 kristaps 42: #else
43: # include <db.h>
44: #endif
45:
46: #include "man.h"
47: #include "mdoc.h"
48: #include "mandoc.h"
1.8 schwarze 49: #include "mandocdb.h"
1.10 kristaps 50: #include "manpath.h"
1.1 kristaps 51:
52: #define MANDOC_BUFSZ BUFSIZ
53: #define MANDOC_SLOP 1024
54:
1.14 schwarze 55: #define MANDOC_SRC 0x1
56: #define MANDOC_FORM 0x2
57:
1.5 kristaps 58: /* Tiny list for files. No need to bring in QUEUE. */
59:
1.3 kristaps 60: struct of {
1.5 kristaps 61: char *fname; /* heap-allocated */
1.12 schwarze 62: char *sec;
63: char *arch;
64: char *title;
1.14 schwarze 65: int src_form;
1.5 kristaps 66: struct of *next; /* NULL for last one */
67: struct of *first; /* first in list */
1.3 kristaps 68: };
69:
1.1 kristaps 70: /* Buffer for storing growable data. */
71:
72: struct buf {
73: char *cp;
1.5 kristaps 74: size_t len; /* current length */
75: size_t size; /* total buffer size */
1.1 kristaps 76: };
77:
78: /* Operation we're going to perform. */
79:
80: enum op {
81: OP_NEW = 0, /* new database */
1.5 kristaps 82: OP_UPDATE, /* delete/add entries in existing database */
1.1 kristaps 83: OP_DELETE /* delete entries from existing database */
84: };
85:
86: #define MAN_ARGS DB *hash, \
87: struct buf *buf, \
88: struct buf *dbuf, \
89: const struct man_node *n
90: #define MDOC_ARGS DB *hash, \
91: struct buf *buf, \
92: struct buf *dbuf, \
93: const struct mdoc_node *n, \
94: const struct mdoc_meta *m
95:
96: static void buf_appendmdoc(struct buf *,
97: const struct mdoc_node *, int);
98: static void buf_append(struct buf *, const char *);
99: static void buf_appendb(struct buf *,
100: const void *, size_t);
101: static void dbt_put(DB *, const char *, DBT *, DBT *);
1.9 kristaps 102: static void hash_put(DB *, const struct buf *, uint64_t);
1.1 kristaps 103: static void hash_reset(DB **);
1.3 kristaps 104: static void index_merge(const struct of *, struct mparse *,
1.16 schwarze 105: struct buf *, struct buf *, DB *,
106: DB *, const char *, DB *, const char *,
1.3 kristaps 107: recno_t, const recno_t *, size_t);
108: static void index_prune(const struct of *, DB *,
109: const char *, DB *, const char *,
1.27 schwarze 110: recno_t *, recno_t **, size_t *,
111: size_t *);
1.16 schwarze 112: static void ofile_argbuild(int, char *[], struct of **);
1.12 schwarze 113: static int ofile_dirbuild(const char *, const char *,
1.16 schwarze 114: const char *, int, struct of **);
1.4 kristaps 115: static void ofile_free(struct of *);
1.14 schwarze 116: static void pformatted(DB *, struct buf *, struct buf *,
117: const struct of *);
1.1 kristaps 118: static int pman_node(MAN_ARGS);
119: static void pmdoc_node(MDOC_ARGS);
1.25 schwarze 120: static int pmdoc_head(MDOC_ARGS);
121: static int pmdoc_body(MDOC_ARGS);
122: static int pmdoc_Fd(MDOC_ARGS);
123: static int pmdoc_In(MDOC_ARGS);
124: static int pmdoc_Fn(MDOC_ARGS);
125: static int pmdoc_Nd(MDOC_ARGS);
126: static int pmdoc_Nm(MDOC_ARGS);
127: static int pmdoc_Sh(MDOC_ARGS);
128: static int pmdoc_St(MDOC_ARGS);
129: static int pmdoc_Xr(MDOC_ARGS);
1.1 kristaps 130: static void usage(void);
131:
1.25 schwarze 132: #define MDOCF_CHILD 0x01 /* Automatically index child nodes. */
1.1 kristaps 133:
1.25 schwarze 134: struct mdoc_handler {
135: int (*fp)(MDOC_ARGS); /* Optional handler. */
136: uint64_t mask; /* Set unless handler returns 0. */
137: int flags; /* For use by pmdoc_node. */
138: };
139:
140: static const struct mdoc_handler mdocs[MDOC_MAX] = {
141: { NULL, 0, 0 }, /* Ap */
142: { NULL, 0, 0 }, /* Dd */
143: { NULL, 0, 0 }, /* Dt */
144: { NULL, 0, 0 }, /* Os */
145: { pmdoc_Sh, TYPE_Sh, MDOCF_CHILD }, /* Sh */
146: { pmdoc_head, TYPE_Ss, MDOCF_CHILD }, /* Ss */
147: { NULL, 0, 0 }, /* Pp */
148: { NULL, 0, 0 }, /* D1 */
149: { NULL, 0, 0 }, /* Dl */
150: { NULL, 0, 0 }, /* Bd */
151: { NULL, 0, 0 }, /* Ed */
152: { NULL, 0, 0 }, /* Bl */
153: { NULL, 0, 0 }, /* El */
154: { NULL, 0, 0 }, /* It */
155: { NULL, 0, 0 }, /* Ad */
156: { NULL, TYPE_An, MDOCF_CHILD }, /* An */
157: { NULL, TYPE_Ar, MDOCF_CHILD }, /* Ar */
158: { NULL, TYPE_Cd, MDOCF_CHILD }, /* Cd */
159: { NULL, TYPE_Cm, MDOCF_CHILD }, /* Cm */
160: { NULL, TYPE_Dv, MDOCF_CHILD }, /* Dv */
161: { NULL, TYPE_Er, MDOCF_CHILD }, /* Er */
162: { NULL, TYPE_Ev, MDOCF_CHILD }, /* Ev */
163: { NULL, 0, 0 }, /* Ex */
164: { NULL, TYPE_Fa, MDOCF_CHILD }, /* Fa */
165: { pmdoc_Fd, TYPE_In, 0 }, /* Fd */
166: { NULL, TYPE_Fl, MDOCF_CHILD }, /* Fl */
167: { pmdoc_Fn, 0, 0 }, /* Fn */
168: { NULL, TYPE_Ft, MDOCF_CHILD }, /* Ft */
169: { NULL, TYPE_Ic, MDOCF_CHILD }, /* Ic */
170: { pmdoc_In, TYPE_In, 0 }, /* In */
171: { NULL, TYPE_Li, MDOCF_CHILD }, /* Li */
172: { pmdoc_Nd, TYPE_Nd, MDOCF_CHILD }, /* Nd */
173: { pmdoc_Nm, TYPE_Nm, MDOCF_CHILD }, /* Nm */
174: { NULL, 0, 0 }, /* Op */
175: { NULL, 0, 0 }, /* Ot */
176: { NULL, TYPE_Pa, MDOCF_CHILD }, /* Pa */
177: { NULL, 0, 0 }, /* Rv */
178: { pmdoc_St, TYPE_St, 0 }, /* St */
179: { NULL, TYPE_Va, MDOCF_CHILD }, /* Va */
180: { pmdoc_body, TYPE_Va, MDOCF_CHILD }, /* Vt */
181: { pmdoc_Xr, TYPE_Xr, 0 }, /* Xr */
182: { NULL, 0, 0 }, /* %A */
183: { NULL, 0, 0 }, /* %B */
184: { NULL, 0, 0 }, /* %D */
185: { NULL, 0, 0 }, /* %I */
186: { NULL, 0, 0 }, /* %J */
187: { NULL, 0, 0 }, /* %N */
188: { NULL, 0, 0 }, /* %O */
189: { NULL, 0, 0 }, /* %P */
190: { NULL, 0, 0 }, /* %R */
191: { NULL, 0, 0 }, /* %T */
192: { NULL, 0, 0 }, /* %V */
193: { NULL, 0, 0 }, /* Ac */
194: { NULL, 0, 0 }, /* Ao */
195: { NULL, 0, 0 }, /* Aq */
196: { NULL, TYPE_At, MDOCF_CHILD }, /* At */
197: { NULL, 0, 0 }, /* Bc */
198: { NULL, 0, 0 }, /* Bf */
199: { NULL, 0, 0 }, /* Bo */
200: { NULL, 0, 0 }, /* Bq */
201: { NULL, TYPE_Bsx, MDOCF_CHILD }, /* Bsx */
202: { NULL, TYPE_Bx, MDOCF_CHILD }, /* Bx */
203: { NULL, 0, 0 }, /* Db */
204: { NULL, 0, 0 }, /* Dc */
205: { NULL, 0, 0 }, /* Do */
206: { NULL, 0, 0 }, /* Dq */
207: { NULL, 0, 0 }, /* Ec */
208: { NULL, 0, 0 }, /* Ef */
209: { NULL, TYPE_Em, MDOCF_CHILD }, /* Em */
210: { NULL, 0, 0 }, /* Eo */
211: { NULL, TYPE_Fx, MDOCF_CHILD }, /* Fx */
212: { NULL, TYPE_Ms, MDOCF_CHILD }, /* Ms */
213: { NULL, 0, 0 }, /* No */
214: { NULL, 0, 0 }, /* Ns */
215: { NULL, TYPE_Nx, MDOCF_CHILD }, /* Nx */
216: { NULL, TYPE_Ox, MDOCF_CHILD }, /* Ox */
217: { NULL, 0, 0 }, /* Pc */
218: { NULL, 0, 0 }, /* Pf */
219: { NULL, 0, 0 }, /* Po */
220: { NULL, 0, 0 }, /* Pq */
221: { NULL, 0, 0 }, /* Qc */
222: { NULL, 0, 0 }, /* Ql */
223: { NULL, 0, 0 }, /* Qo */
224: { NULL, 0, 0 }, /* Qq */
225: { NULL, 0, 0 }, /* Re */
226: { NULL, 0, 0 }, /* Rs */
227: { NULL, 0, 0 }, /* Sc */
228: { NULL, 0, 0 }, /* So */
229: { NULL, 0, 0 }, /* Sq */
230: { NULL, 0, 0 }, /* Sm */
231: { NULL, 0, 0 }, /* Sx */
232: { NULL, TYPE_Sy, MDOCF_CHILD }, /* Sy */
233: { NULL, TYPE_Tn, MDOCF_CHILD }, /* Tn */
234: { NULL, 0, 0 }, /* Ux */
235: { NULL, 0, 0 }, /* Xc */
236: { NULL, 0, 0 }, /* Xo */
237: { pmdoc_head, TYPE_Fn, 0 }, /* Fo */
238: { NULL, 0, 0 }, /* Fc */
239: { NULL, 0, 0 }, /* Oo */
240: { NULL, 0, 0 }, /* Oc */
241: { NULL, 0, 0 }, /* Bk */
242: { NULL, 0, 0 }, /* Ek */
243: { NULL, 0, 0 }, /* Bt */
244: { NULL, 0, 0 }, /* Hf */
245: { NULL, 0, 0 }, /* Fr */
246: { NULL, 0, 0 }, /* Ud */
247: { NULL, TYPE_Lb, MDOCF_CHILD }, /* Lb */
248: { NULL, 0, 0 }, /* Lp */
249: { NULL, TYPE_Lk, MDOCF_CHILD }, /* Lk */
250: { NULL, TYPE_Mt, MDOCF_CHILD }, /* Mt */
251: { NULL, 0, 0 }, /* Brq */
252: { NULL, 0, 0 }, /* Bro */
253: { NULL, 0, 0 }, /* Brc */
254: { NULL, 0, 0 }, /* %C */
255: { NULL, 0, 0 }, /* Es */
256: { NULL, 0, 0 }, /* En */
257: { NULL, TYPE_Dx, MDOCF_CHILD }, /* Dx */
258: { NULL, 0, 0 }, /* %Q */
259: { NULL, 0, 0 }, /* br */
260: { NULL, 0, 0 }, /* sp */
261: { NULL, 0, 0 }, /* %U */
262: { NULL, 0, 0 }, /* Ta */
1.1 kristaps 263: };
264:
265: static const char *progname;
1.16 schwarze 266: static int use_all; /* Use all directories and files. */
267: static int verb; /* Output verbosity level. */
1.1 kristaps 268:
269: int
270: main(int argc, char *argv[])
271: {
272: struct mparse *mp; /* parse sequence */
1.10 kristaps 273: struct manpaths dirs;
1.1 kristaps 274: enum op op; /* current operation */
1.5 kristaps 275: const char *dir;
1.26 kristaps 276: char *cp;
277: char pbuf[PATH_MAX],
278: ibuf[MAXPATHLEN], /* index fname */
1.3 kristaps 279: fbuf[MAXPATHLEN]; /* btree fname */
1.16 schwarze 280: int ch, i, flags;
1.1 kristaps 281: DB *idx, /* index database */
282: *db, /* keyword database */
283: *hash; /* temporary keyword hashtable */
284: BTREEINFO info; /* btree configuration */
1.12 schwarze 285: recno_t maxrec; /* last record number in the index */
286: recno_t *recs; /* the numbers of all empty records */
1.5 kristaps 287: size_t sz1, sz2,
1.12 schwarze 288: recsz, /* number of allocated slots in recs */
289: reccur; /* current number of empty records */
1.1 kristaps 290: struct buf buf, /* keyword buffer */
291: dbuf; /* description buffer */
1.5 kristaps 292: struct of *of; /* list of files for processing */
1.1 kristaps 293: extern int optind;
294: extern char *optarg;
295:
296: progname = strrchr(argv[0], '/');
297: if (progname == NULL)
298: progname = argv[0];
299: else
300: ++progname;
301:
1.10 kristaps 302: memset(&dirs, 0, sizeof(struct manpaths));
303:
1.5 kristaps 304: verb = 0;
1.12 schwarze 305: use_all = 0;
1.4 kristaps 306: of = NULL;
1.1 kristaps 307: db = idx = NULL;
308: mp = NULL;
309: hash = NULL;
310: recs = NULL;
311: recsz = reccur = 0;
312: maxrec = 0;
313: op = OP_NEW;
1.5 kristaps 314: dir = NULL;
1.1 kristaps 315:
1.12 schwarze 316: while (-1 != (ch = getopt(argc, argv, "ad:u:v")))
1.1 kristaps 317: switch (ch) {
1.12 schwarze 318: case ('a'):
319: use_all = 1;
320: break;
1.5 kristaps 321: case ('d'):
322: dir = optarg;
323: op = OP_UPDATE;
324: break;
325: case ('u'):
326: dir = optarg;
327: op = OP_DELETE;
328: break;
329: case ('v'):
330: verb++;
331: break;
1.1 kristaps 332: default:
333: usage();
334: return((int)MANDOCLEVEL_BADARG);
335: }
336:
337: argc -= optind;
338: argv += optind;
339:
1.4 kristaps 340: memset(&info, 0, sizeof(BTREEINFO));
341: info.flags = R_DUP;
1.1 kristaps 342:
1.4 kristaps 343: mp = mparse_alloc(MPARSE_AUTO, MANDOCLEVEL_FATAL, NULL, NULL);
1.1 kristaps 344:
1.5 kristaps 345: memset(&buf, 0, sizeof(struct buf));
346: memset(&dbuf, 0, sizeof(struct buf));
1.1 kristaps 347:
1.4 kristaps 348: buf.size = dbuf.size = MANDOC_BUFSZ;
1.1 kristaps 349:
1.4 kristaps 350: buf.cp = mandoc_malloc(buf.size);
351: dbuf.cp = mandoc_malloc(dbuf.size);
1.1 kristaps 352:
1.5 kristaps 353: flags = OP_NEW == op ? O_CREAT|O_TRUNC|O_RDWR : O_CREAT|O_RDWR;
354:
355: if (OP_UPDATE == op || OP_DELETE == op) {
356: ibuf[0] = fbuf[0] = '\0';
357:
358: strlcat(fbuf, dir, MAXPATHLEN);
359: strlcat(fbuf, "/", MAXPATHLEN);
360: sz1 = strlcat(fbuf, MANDOC_DB, MAXPATHLEN);
361:
362: strlcat(ibuf, dir, MAXPATHLEN);
363: strlcat(ibuf, "/", MAXPATHLEN);
364: sz2 = strlcat(ibuf, MANDOC_IDX, MAXPATHLEN);
365:
366: if (sz1 >= MAXPATHLEN || sz2 >= MAXPATHLEN) {
367: fprintf(stderr, "%s: Path too long\n", dir);
368: exit((int)MANDOCLEVEL_BADARG);
369: }
370:
371: db = dbopen(fbuf, flags, 0644, DB_BTREE, &info);
372: idx = dbopen(ibuf, flags, 0644, DB_RECNO, NULL);
373:
374: if (NULL == db) {
375: perror(fbuf);
376: exit((int)MANDOCLEVEL_SYSERR);
1.12 schwarze 377: } else if (NULL == idx) {
1.5 kristaps 378: perror(ibuf);
379: exit((int)MANDOCLEVEL_SYSERR);
380: }
381:
382: if (verb > 2) {
383: printf("%s: Opened\n", fbuf);
384: printf("%s: Opened\n", ibuf);
385: }
386:
1.16 schwarze 387: ofile_argbuild(argc, argv, &of);
1.5 kristaps 388: if (NULL == of)
389: goto out;
390:
391: of = of->first;
392:
1.16 schwarze 393: index_prune(of, db, fbuf, idx, ibuf,
1.27 schwarze 394: &maxrec, &recs, &recsz, &reccur);
1.5 kristaps 395:
1.17 schwarze 396: /*
397: * Go to the root of the respective manual tree
398: * such that .so links work. In case of failure,
399: * just prod on, even though .so links won't work.
400: */
401:
402: if (OP_UPDATE == op) {
403: chdir(dir);
1.13 schwarze 404: index_merge(of, mp, &dbuf, &buf, hash,
1.16 schwarze 405: db, fbuf, idx, ibuf,
406: maxrec, recs, reccur);
1.17 schwarze 407: }
1.5 kristaps 408:
409: goto out;
410: }
411:
1.10 kristaps 412: /*
413: * Configure the directories we're going to scan.
414: * If we have command-line arguments, use them.
415: * If not, we use man(1)'s method (see mandocdb.8).
416: */
417:
418: if (argc > 0) {
1.26 kristaps 419: dirs.paths = mandoc_calloc(argc, sizeof(char *));
1.10 kristaps 420: dirs.sz = argc;
1.26 kristaps 421: for (i = 0; i < argc; i++) {
422: if (NULL == (cp = realpath(argv[i], pbuf))) {
423: perror(argv[i]);
424: goto out;
425: }
426: dirs.paths[i] = mandoc_strdup(cp);
427: }
1.10 kristaps 428: } else
1.11 kristaps 429: manpath_parse(&dirs, NULL, NULL);
1.10 kristaps 430:
431: for (i = 0; i < dirs.sz; i++) {
1.5 kristaps 432: ibuf[0] = fbuf[0] = '\0';
1.1 kristaps 433:
1.10 kristaps 434: strlcat(fbuf, dirs.paths[i], MAXPATHLEN);
1.5 kristaps 435: strlcat(fbuf, "/", MAXPATHLEN);
436: sz1 = strlcat(fbuf, MANDOC_DB, MAXPATHLEN);
1.1 kristaps 437:
1.10 kristaps 438: strlcat(ibuf, dirs.paths[i], MAXPATHLEN);
1.5 kristaps 439: strlcat(ibuf, "/", MAXPATHLEN);
440: sz2 = strlcat(ibuf, MANDOC_IDX, MAXPATHLEN);
1.1 kristaps 441:
1.5 kristaps 442: if (sz1 >= MAXPATHLEN || sz2 >= MAXPATHLEN) {
1.13 schwarze 443: fprintf(stderr, "%s: Path too long\n",
1.10 kristaps 444: dirs.paths[i]);
1.5 kristaps 445: exit((int)MANDOCLEVEL_BADARG);
1.4 kristaps 446: }
1.3 kristaps 447:
1.13 schwarze 448: if (db)
449: (*db->close)(db);
450: if (idx)
451: (*idx->close)(idx);
452:
1.4 kristaps 453: db = dbopen(fbuf, flags, 0644, DB_BTREE, &info);
454: idx = dbopen(ibuf, flags, 0644, DB_RECNO, NULL);
1.3 kristaps 455:
1.4 kristaps 456: if (NULL == db) {
457: perror(fbuf);
1.5 kristaps 458: exit((int)MANDOCLEVEL_SYSERR);
1.12 schwarze 459: } else if (NULL == idx) {
1.4 kristaps 460: perror(ibuf);
1.5 kristaps 461: exit((int)MANDOCLEVEL_SYSERR);
462: }
463:
464: if (verb > 2) {
465: printf("%s: Truncated\n", fbuf);
466: printf("%s: Truncated\n", ibuf);
1.4 kristaps 467: }
1.1 kristaps 468:
1.4 kristaps 469: ofile_free(of);
470: of = NULL;
1.1 kristaps 471:
1.12 schwarze 472: if ( ! ofile_dirbuild(dirs.paths[i], NULL, NULL,
1.16 schwarze 473: 0, &of))
1.5 kristaps 474: exit((int)MANDOCLEVEL_SYSERR);
1.1 kristaps 475:
1.5 kristaps 476: if (NULL == of)
477: continue;
1.1 kristaps 478:
1.5 kristaps 479: of = of->first;
1.1 kristaps 480:
1.17 schwarze 481: /*
482: * Go to the root of the respective manual tree
483: * such that .so links work. In case of failure,
484: * just prod on, even though .so links won't work.
485: */
486:
487: chdir(dirs.paths[i]);
1.13 schwarze 488: index_merge(of, mp, &dbuf, &buf, hash, db, fbuf,
1.16 schwarze 489: idx, ibuf, maxrec, recs, reccur);
1.4 kristaps 490: }
1.3 kristaps 491:
1.5 kristaps 492: out:
1.3 kristaps 493: if (db)
494: (*db->close)(db);
495: if (idx)
496: (*idx->close)(idx);
497: if (hash)
498: (*hash->close)(hash);
499: if (mp)
500: mparse_free(mp);
501:
1.10 kristaps 502: manpath_free(&dirs);
1.4 kristaps 503: ofile_free(of);
1.3 kristaps 504: free(buf.cp);
505: free(dbuf.cp);
506: free(recs);
507:
1.5 kristaps 508: return(MANDOCLEVEL_OK);
1.3 kristaps 509: }
510:
511: void
512: index_merge(const struct of *of, struct mparse *mp,
1.16 schwarze 513: struct buf *dbuf, struct buf *buf, DB *hash,
514: DB *db, const char *dbf, DB *idx, const char *idxf,
1.3 kristaps 515: recno_t maxrec, const recno_t *recs, size_t reccur)
516: {
517: recno_t rec;
518: int ch;
519: DBT key, val;
520: struct mdoc *mdoc;
521: struct man *man;
522: const char *fn, *msec, *mtitle, *arch;
523: size_t sv;
524: unsigned seq;
1.9 kristaps 525: struct db_val vbuf;
1.3 kristaps 526:
527: for (rec = 0; of; of = of->next) {
528: fn = of->fname;
1.14 schwarze 529:
530: /*
531: * Reclaim an empty index record, if available.
532: */
533:
1.3 kristaps 534: if (reccur > 0) {
535: --reccur;
536: rec = recs[(int)reccur];
537: } else if (maxrec > 0) {
538: rec = maxrec;
539: maxrec = 0;
1.1 kristaps 540: } else
541: rec++;
542:
543: mparse_reset(mp);
544: hash_reset(&hash);
1.14 schwarze 545: mdoc = NULL;
546: man = NULL;
1.1 kristaps 547:
1.14 schwarze 548: /*
549: * Try interpreting the file as mdoc(7) or man(7)
550: * source code, unless it is already known to be
551: * formatted. Fall back to formatted mode.
552: */
553:
554: if ((MANDOC_SRC & of->src_form ||
555: ! (MANDOC_FORM & of->src_form)) &&
556: MANDOCLEVEL_FATAL > mparse_readfd(mp, -1, fn))
557: mparse_result(mp, &mdoc, &man);
558:
559: if (NULL != mdoc) {
560: msec = mdoc_meta(mdoc)->msec;
561: arch = mdoc_meta(mdoc)->arch;
562: mtitle = mdoc_meta(mdoc)->title;
563: } else if (NULL != man) {
564: msec = man_meta(man)->msec;
565: arch = NULL;
566: mtitle = man_meta(man)->title;
567: } else {
568: msec = of->sec;
569: arch = of->arch;
570: mtitle = of->title;
1.1 kristaps 571: }
572:
1.12 schwarze 573: /*
574: * By default, skip a file if the manual section
575: * and architecture given in the file disagree
576: * with the directory where the file is located.
577: */
578:
579: if (0 == use_all) {
580: assert(of->sec);
581: assert(msec);
582: if (strcmp(msec, of->sec))
583: continue;
584:
585: if (NULL == arch) {
586: if (NULL != of->arch)
587: continue;
588: } else if (NULL == of->arch ||
589: strcmp(arch, of->arch))
590: continue;
591: }
592:
1.1 kristaps 593: if (NULL == arch)
594: arch = "";
595:
596: /*
1.12 schwarze 597: * By default, skip a file if the title given
598: * in the file disagrees with the file name.
599: * If both agree, use the file name as the title,
600: * because the one in the file usually is all caps.
601: */
602:
603: assert(of->title);
604: assert(mtitle);
605:
606: if (0 == strcasecmp(mtitle, of->title))
607: mtitle = of->title;
608: else if (0 == use_all)
609: continue;
610:
611: /*
1.1 kristaps 612: * The index record value consists of a nil-terminated
613: * filename, a nil-terminated manual section, and a
614: * nil-terminated description. Since the description
615: * may not be set, we set a sentinel to see if we're
616: * going to write a nil byte in its place.
617: */
618:
1.3 kristaps 619: dbuf->len = 0;
1.15 schwarze 620: buf_append(dbuf, mdoc ? "mdoc" : (man ? "man" : "cat"));
1.3 kristaps 621: buf_appendb(dbuf, fn, strlen(fn) + 1);
622: buf_appendb(dbuf, msec, strlen(msec) + 1);
623: buf_appendb(dbuf, mtitle, strlen(mtitle) + 1);
624: buf_appendb(dbuf, arch, strlen(arch) + 1);
1.1 kristaps 625:
1.3 kristaps 626: sv = dbuf->len;
1.1 kristaps 627:
628: /* Fix the record number in the btree value. */
629:
630: if (mdoc)
1.3 kristaps 631: pmdoc_node(hash, buf, dbuf,
1.1 kristaps 632: mdoc_node(mdoc), mdoc_meta(mdoc));
1.14 schwarze 633: else if (man)
1.3 kristaps 634: pman_node(hash, buf, dbuf, man_node(man));
1.14 schwarze 635: else
636: pformatted(hash, buf, dbuf, of);
1.1 kristaps 637:
638: /*
639: * Copy from the in-memory hashtable of pending keywords
640: * into the database.
641: */
642:
1.20 kristaps 643: vbuf.rec = htobe32(rec);
1.1 kristaps 644: seq = R_FIRST;
645: while (0 == (ch = (*hash->seq)(hash, &key, &val, seq))) {
646: seq = R_NEXT;
1.20 kristaps 647: vbuf.mask = htobe64(*(uint64_t *)val.data);
1.9 kristaps 648: val.size = sizeof(struct db_val);
649: val.data = &vbuf;
1.3 kristaps 650: dbt_put(db, dbf, &key, &val);
1.1 kristaps 651: }
652: if (ch < 0) {
653: perror("hash");
654: exit((int)MANDOCLEVEL_SYSERR);
655: }
656:
657: /*
658: * Apply to the index. If we haven't had a description
659: * set, put an empty one in now.
660: */
661:
1.3 kristaps 662: if (dbuf->len == sv)
663: buf_appendb(dbuf, "", 1);
1.1 kristaps 664:
665: key.data = &rec;
666: key.size = sizeof(recno_t);
667:
1.3 kristaps 668: val.data = dbuf->cp;
669: val.size = dbuf->len;
1.1 kristaps 670:
1.5 kristaps 671: if (verb)
672: printf("%s: Added index\n", fn);
1.18 kristaps 673:
1.3 kristaps 674: dbt_put(idx, idxf, &key, &val);
675: }
676: }
677:
678: /*
679: * Scan through all entries in the index file `idx' and prune those
680: * entries in `ofile'.
681: * Pruning consists of removing from `db', then invalidating the entry
682: * in `idx' (zeroing its value size).
683: */
684: static void
685: index_prune(const struct of *ofile, DB *db, const char *dbf,
1.27 schwarze 686: DB *idx, const char *idxf, recno_t *maxrec,
687: recno_t **recs, size_t *recsz, size_t *reccur)
1.3 kristaps 688: {
689: const struct of *of;
1.18 kristaps 690: const char *fn, *cp;
1.9 kristaps 691: struct db_val *vbuf;
1.3 kristaps 692: unsigned seq, sseq;
693: DBT key, val;
694: int ch;
695:
1.27 schwarze 696: *reccur = 0;
1.3 kristaps 697: seq = R_FIRST;
698: while (0 == (ch = (*idx->seq)(idx, &key, &val, seq))) {
699: seq = R_NEXT;
700: *maxrec = *(recno_t *)key.data;
1.18 kristaps 701: cp = val.data;
702:
703: /* Deleted records are zero-sized. Skip them. */
704:
705: if (0 == val.size)
706: goto cont;
707:
708: /*
709: * Make sure we're sane.
710: * Read past our mdoc/man/cat type to the next string,
711: * then make sure it's bounded by a NUL.
712: * Failing any of these, we go into our error handler.
713: */
714:
715: if (NULL == (fn = memchr(cp, '\0', val.size)))
716: break;
717: if (++fn - cp >= (int)val.size)
718: break;
719: if (NULL == memchr(fn, '\0', val.size - (fn - cp)))
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.3 kristaps 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.18 kristaps 734: /*
735: * Search through the keyword database, throwing out all
736: * references to our file.
737: */
738:
1.3 kristaps 739: sseq = R_FIRST;
740: while (0 == (ch = (*db->seq)(db, &key, &val, sseq))) {
741: sseq = R_NEXT;
1.18 kristaps 742: if (sizeof(struct db_val) != val.size)
743: break;
744:
1.9 kristaps 745: vbuf = val.data;
1.20 kristaps 746: if (*maxrec != betoh32(vbuf->rec))
1.3 kristaps 747: continue;
1.18 kristaps 748:
749: if ((ch = (*db->del)(db, &key, R_CURSOR)) < 0)
1.3 kristaps 750: break;
751: }
1.18 kristaps 752:
1.3 kristaps 753: if (ch < 0) {
754: perror(dbf);
755: exit((int)MANDOCLEVEL_SYSERR);
1.18 kristaps 756: } else if (1 != ch) {
757: fprintf(stderr, "%s: Corrupt database\n", dbf);
758: exit((int)MANDOCLEVEL_SYSERR);
1.3 kristaps 759: }
1.1 kristaps 760:
1.5 kristaps 761: if (verb)
762: printf("%s: Deleted index\n", fn);
1.1 kristaps 763:
1.3 kristaps 764: val.size = 0;
765: ch = (*idx->put)(idx, &key, &val, R_CURSOR);
1.1 kristaps 766:
1.18 kristaps 767: if (ch < 0)
768: break;
769: cont:
1.27 schwarze 770: if (*reccur >= *recsz) {
1.3 kristaps 771: *recsz += MANDOC_SLOP;
772: *recs = mandoc_realloc
773: (*recs, *recsz * sizeof(recno_t));
774: }
1.1 kristaps 775:
1.27 schwarze 776: (*recs)[(int)*reccur] = *maxrec;
777: (*reccur)++;
1.3 kristaps 778: }
1.18 kristaps 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.3 kristaps 788: (*maxrec)++;
1.1 kristaps 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.5 kristaps 864: *db = dbopen(NULL, O_CREAT|O_RDWR, 0644, DB_HASH, NULL);
1.1 kristaps 865: if (NULL == *db) {
866: perror("hash");
867: exit((int)MANDOCLEVEL_SYSERR);
868: }
869: }
870:
871: /* ARGSUSED */
1.25 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 kristaps 889: pmdoc_Fd(MDOC_ARGS)
890: {
891: const char *start, *end;
892: size_t sz;
1.25 schwarze 893:
1.1 kristaps 894: if (SEC_SYNOPSIS != n->sec)
1.25 schwarze 895: return(0);
1.1 kristaps 896: if (NULL == (n = n->child) || MDOC_TEXT != n->type)
1.25 schwarze 897: return(0);
1.1 kristaps 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.25 schwarze 904: return(0);
1.1 kristaps 905:
906: if (NULL == (n = n->next) || MDOC_TEXT != n->type)
1.25 schwarze 907: return(0);
1.1 kristaps 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.25 schwarze 919: return(0);
1.1 kristaps 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.25 schwarze 929: return(1);
1.1 kristaps 930: }
931:
932: /* ARGSUSED */
1.25 schwarze 933: static int
934: pmdoc_In(MDOC_ARGS)
1.1 kristaps 935: {
936:
937: if (NULL == n->child || MDOC_TEXT != n->child->type)
1.25 schwarze 938: return(0);
1.1 kristaps 939:
940: buf_append(buf, n->child->string);
1.25 schwarze 941: return(1);
1.1 kristaps 942: }
943:
944: /* ARGSUSED */
1.25 schwarze 945: static int
1.1 kristaps 946: pmdoc_Fn(MDOC_ARGS)
947: {
1.25 schwarze 948: struct mdoc_node *nn;
1.1 kristaps 949: const char *cp;
950:
1.25 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 kristaps 957:
1.25 schwarze 958: cp = strrchr(nn->string, ' ');
1.1 kristaps 959: if (NULL == cp)
1.25 schwarze 960: cp = nn->string;
1.1 kristaps 961:
962: /* Strip away pointer symbol. */
963:
964: while ('*' == *cp)
965: cp++;
966:
1.25 schwarze 967: /* Store the function name. */
968:
1.1 kristaps 969: buf_append(buf, cp);
1.8 schwarze 970: hash_put(hash, buf, TYPE_Fn);
1.25 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 kristaps 992: }
993:
994: /* ARGSUSED */
1.25 schwarze 995: static int
1.1 kristaps 996: pmdoc_St(MDOC_ARGS)
997: {
1.25 schwarze 998:
1.1 kristaps 999: if (NULL == n->child || MDOC_TEXT != n->child->type)
1.25 schwarze 1000: return(0);
1.1 kristaps 1001:
1002: buf_append(buf, n->child->string);
1.25 schwarze 1003: return(1);
1.1 kristaps 1004: }
1005:
1006: /* ARGSUSED */
1.25 schwarze 1007: static int
1.1 kristaps 1008: pmdoc_Xr(MDOC_ARGS)
1009: {
1010:
1011: if (NULL == (n = n->child))
1.25 schwarze 1012: return(0);
1.1 kristaps 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.25 schwarze 1022: return(1);
1.1 kristaps 1023: }
1024:
1025: /* ARGSUSED */
1.25 schwarze 1026: static int
1.1 kristaps 1027: pmdoc_Nd(MDOC_ARGS)
1028: {
1029:
1030: if (MDOC_BODY != n->type)
1.25 schwarze 1031: return(0);
1.1 kristaps 1032:
1033: buf_appendmdoc(dbuf, n->child, 1);
1.25 schwarze 1034: return(1);
1.1 kristaps 1035: }
1036:
1037: /* ARGSUSED */
1.25 schwarze 1038: static int
1039: pmdoc_Nm(MDOC_ARGS)
1.1 kristaps 1040: {
1041:
1.25 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 kristaps 1046:
1.25 schwarze 1047: if (NULL == n->child)
1048: buf_append(buf, m->name);
1.1 kristaps 1049:
1.25 schwarze 1050: return(1);
1.1 kristaps 1051: }
1052:
1053: /* ARGSUSED */
1.25 schwarze 1054: static int
1055: pmdoc_Sh(MDOC_ARGS)
1.1 kristaps 1056: {
1057:
1.25 schwarze 1058: return(SEC_CUSTOM == n->sec && MDOC_HEAD == n->type);
1.1 kristaps 1059: }
1060:
1061: static void
1.9 kristaps 1062: hash_put(DB *db, const struct buf *buf, uint64_t mask)
1.1 kristaps 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 kristaps 1077: mask |= *(uint64_t *)val.data;
1.1 kristaps 1078:
1079: val.data = &mask;
1.9 kristaps 1080: val.size = sizeof(uint64_t);
1.1 kristaps 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.25 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 kristaps 1134: break;
1135:
1.25 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 kristaps 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.8 schwarze 1213: hash_put(hash, buf, TYPE_Nm);
1.1 kristaps 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.8 schwarze 1252: hash_put(hash, buf, TYPE_Nd);
1.1 kristaps 1253: }
1254: }
1255:
1.7 schwarze 1256: for (n = n->child; n; n = n->next)
1257: if (pman_node(hash, buf, dbuf, n))
1258: return(1);
1.1 kristaps 1259:
1260: return(0);
1261: }
1262:
1.14 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.28 kristaps 1291: /* Skip to first blank line. */
1.14 schwarze 1292:
1.28 kristaps 1293: while (NULL != (line = fgetln(stream, &len)))
1294: if (len && '\n' == *line)
1295: break;
1296:
1297: /*
1298: * Skip to first section header.
1299: * This happens when text is flush-left.
1300: */
1301:
1302: while (NULL != (line = fgetln(stream, &len)))
1303: if (len && '\n' != *line && ' ' != *line)
1304: break;
1.14 schwarze 1305:
1306: /*
1.28 kristaps 1307: * If no page content can be found or the input line is
1308: * malformed (zer-length or has no trailing newline), reuse the
1309: * page title as the page description.
1.14 schwarze 1310: */
1311:
1.28 kristaps 1312: line = fgetln(stream, &len);
1313: if (NULL == line || len == 0 || '\n' != line[(int)len - 1]) {
1.14 schwarze 1314: buf_appendb(dbuf, buf->cp, buf->size);
1315: hash_put(hash, buf, TYPE_Nd);
1316: fclose(stream);
1317: return;
1318: }
1319:
1.28 kristaps 1320: line[(int)--len] = '\0';
1321:
1322: /*
1323: * Skip to the last dash.
1324: * Use the remaining line as the description (no more than 70
1325: * bytes).
1.14 schwarze 1326: */
1327:
1.30 ! kristaps 1328: if (NULL != (p = strstr(line, "- "))) {
! 1329: for (p += 2; ' ' == *p || '\b' == *p; p++)
1.28 kristaps 1330: /* Skip to next word. */ ;
1331: } else
1.14 schwarze 1332: p = line;
1.28 kristaps 1333:
1334: if ((plen = strlen(p)) > 70) {
1335: plen = 70;
1336: p[plen] = '\0';
1.29 kristaps 1337: }
1338:
1339: /* Strip backspace-encoding from line. */
1340:
1341: while (NULL != (line = memchr(p, '\b', plen))) {
1342: len = line - p;
1343: if (0 == len) {
1344: memmove(line, line + 1, plen--);
1345: continue;
1346: }
1347: memmove(line - 1, line + 1, plen - len);
1348: plen -= 2;
1.14 schwarze 1349: }
1350:
1.28 kristaps 1351: buf_appendb(dbuf, p, plen + 1);
1.14 schwarze 1352: buf->len = 0;
1.28 kristaps 1353: buf_appendb(buf, p, plen + 1);
1.14 schwarze 1354: hash_put(hash, buf, TYPE_Nd);
1.28 kristaps 1355: fclose(stream);
1.14 schwarze 1356: }
1357:
1.5 kristaps 1358: static void
1.16 schwarze 1359: ofile_argbuild(int argc, char *argv[], struct of **of)
1.5 kristaps 1360: {
1.12 schwarze 1361: char buf[MAXPATHLEN];
1362: char *sec, *arch, *title, *p;
1.14 schwarze 1363: int i, src_form;
1.5 kristaps 1364: struct of *nof;
1365:
1366: for (i = 0; i < argc; i++) {
1.12 schwarze 1367:
1368: /*
1369: * Try to infer the manual section, architecture and
1370: * page title from the path, assuming it looks like
1.14 schwarze 1371: * man*[/<arch>]/<title>.<section> or
1372: * cat<section>[/<arch>]/<title>.0
1.12 schwarze 1373: */
1374:
1375: if (strlcpy(buf, argv[i], sizeof(buf)) >= sizeof(buf)) {
1376: fprintf(stderr, "%s: Path too long\n", argv[i]);
1377: continue;
1378: }
1379: sec = arch = title = NULL;
1.14 schwarze 1380: src_form = 0;
1.12 schwarze 1381: p = strrchr(buf, '\0');
1382: while (p-- > buf) {
1383: if (NULL == sec && '.' == *p) {
1384: sec = p + 1;
1385: *p = '\0';
1.14 schwarze 1386: if ('0' == *sec)
1387: src_form |= MANDOC_FORM;
1388: else if ('1' <= *sec && '9' >= *sec)
1389: src_form |= MANDOC_SRC;
1.12 schwarze 1390: continue;
1391: }
1392: if ('/' != *p)
1393: continue;
1394: if (NULL == title) {
1395: title = p + 1;
1396: *p = '\0';
1397: continue;
1398: }
1.24 schwarze 1399: if (0 == strncmp("man", p + 1, 3))
1.14 schwarze 1400: src_form |= MANDOC_SRC;
1.24 schwarze 1401: else if (0 == strncmp("cat", p + 1, 3))
1.14 schwarze 1402: src_form |= MANDOC_FORM;
1.24 schwarze 1403: else
1.12 schwarze 1404: arch = p + 1;
1405: break;
1406: }
1407: if (NULL == title)
1408: title = buf;
1409:
1410: /*
1411: * Build the file structure.
1412: */
1413:
1.5 kristaps 1414: nof = mandoc_calloc(1, sizeof(struct of));
1.12 schwarze 1415: nof->fname = mandoc_strdup(argv[i]);
1416: if (NULL != sec)
1417: nof->sec = mandoc_strdup(sec);
1418: if (NULL != arch)
1419: nof->arch = mandoc_strdup(arch);
1420: nof->title = mandoc_strdup(title);
1.14 schwarze 1421: nof->src_form = src_form;
1.12 schwarze 1422:
1423: /*
1424: * Add the structure to the list.
1425: */
1426:
1.5 kristaps 1427: if (verb > 2)
1428: printf("%s: Scheduling\n", argv[i]);
1429: if (NULL == *of) {
1430: *of = nof;
1431: (*of)->first = nof;
1432: } else {
1433: nof->first = (*of)->first;
1434: (*of)->next = nof;
1435: *of = nof;
1436: }
1437: }
1438: }
1439:
1.4 kristaps 1440: /*
1441: * Recursively build up a list of files to parse.
1442: * We use this instead of ftw() and so on because I don't want global
1443: * variables hanging around.
1444: * This ignores the mandoc.db and mandoc.index files, but assumes that
1445: * everything else is a manual.
1446: * Pass in a pointer to a NULL structure for the first invocation.
1447: */
1448: static int
1.12 schwarze 1449: ofile_dirbuild(const char *dir, const char* psec, const char *parch,
1.16 schwarze 1450: int p_src_form, struct of **of)
1.4 kristaps 1451: {
1.5 kristaps 1452: char buf[MAXPATHLEN];
1.14 schwarze 1453: struct stat sb;
1.5 kristaps 1454: size_t sz;
1.4 kristaps 1455: DIR *d;
1.12 schwarze 1456: const char *fn, *sec, *arch;
1.14 schwarze 1457: char *p, *q, *suffix;
1.4 kristaps 1458: struct of *nof;
1459: struct dirent *dp;
1.14 schwarze 1460: int src_form;
1.4 kristaps 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.12 schwarze 1469:
1470: if ('.' == *fn)
1471: continue;
1472:
1.14 schwarze 1473: src_form = p_src_form;
1474:
1.4 kristaps 1475: if (DT_DIR == dp->d_type) {
1.12 schwarze 1476: sec = psec;
1477: arch = parch;
1478:
1479: /*
1480: * By default, only use directories called:
1.14 schwarze 1481: * man<section>/[<arch>/] or
1482: * cat<section>/[<arch>/]
1.12 schwarze 1483: */
1484:
1485: if (NULL == sec) {
1.14 schwarze 1486: if(0 == strncmp("man", fn, 3)) {
1487: src_form |= MANDOC_SRC;
1.12 schwarze 1488: sec = fn + 3;
1.14 schwarze 1489: } else if (0 == strncmp("cat", fn, 3)) {
1490: src_form |= MANDOC_FORM;
1491: sec = fn + 3;
1492: } else if (use_all)
1.12 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.5 kristaps 1500: continue;
1501:
1502: buf[0] = '\0';
1503: strlcat(buf, dir, MAXPATHLEN);
1504: strlcat(buf, "/", MAXPATHLEN);
1505: sz = strlcat(buf, fn, MAXPATHLEN);
1506:
1.12 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:
1515: if ( ! ofile_dirbuild(buf, sec, arch,
1.16 schwarze 1516: src_form, of))
1.12 schwarze 1517: return(0);
1518: }
1519: if (DT_REG != dp->d_type ||
1520: (NULL == psec && !use_all) ||
1521: !strcmp(MANDOC_DB, fn) ||
1522: !strcmp(MANDOC_IDX, fn))
1523: continue;
1524:
1525: /*
1526: * By default, skip files where the file name suffix
1527: * does not agree with the section directory
1528: * they are located in.
1529: */
1530:
1531: suffix = strrchr(fn, '.');
1532: if (0 == use_all) {
1533: if (NULL == suffix)
1.5 kristaps 1534: continue;
1.14 schwarze 1535: if ((MANDOC_SRC & src_form &&
1536: strcmp(suffix + 1, psec)) ||
1537: (MANDOC_FORM & src_form &&
1538: strcmp(suffix + 1, "0")))
1539: continue;
1540: }
1541: if (NULL != suffix) {
1542: if ('0' == suffix[1])
1543: src_form |= MANDOC_FORM;
1544: else if ('1' <= suffix[1] && '9' >= suffix[1])
1545: src_form |= MANDOC_SRC;
1546: }
1547:
1548:
1549: /*
1550: * Skip formatted manuals if a source version is
1551: * available. Ignore the age: it is very unlikely
1552: * that people install newer formatted base manuals
1553: * when they used to have source manuals before,
1554: * and in ports, old manuals get removed on update.
1555: */
1556: if (0 == use_all && MANDOC_FORM & src_form &&
1557: NULL != psec) {
1558: buf[0] = '\0';
1559: strlcat(buf, dir, MAXPATHLEN);
1560: p = strrchr(buf, '/');
1561: if (NULL == p)
1562: p = buf;
1563: else
1564: p++;
1565: if (0 == strncmp("cat", p, 3))
1566: memcpy(p, "man", 3);
1567: strlcat(buf, "/", MAXPATHLEN);
1568: sz = strlcat(buf, fn, MAXPATHLEN);
1569: if (sz >= MAXPATHLEN) {
1570: fprintf(stderr, "%s: Path too long\n", buf);
1.5 kristaps 1571: continue;
1.14 schwarze 1572: }
1573: q = strrchr(buf, '.');
1574: if (NULL != q && p < q++) {
1575: *q = '\0';
1576: sz = strlcat(buf, psec, MAXPATHLEN);
1577: if (sz >= MAXPATHLEN) {
1578: fprintf(stderr,
1579: "%s: Path too long\n", buf);
1580: continue;
1581: }
1582: if (0 == stat(buf, &sb))
1583: continue;
1584: }
1.5 kristaps 1585: }
1.4 kristaps 1586:
1.5 kristaps 1587: buf[0] = '\0';
1588: strlcat(buf, dir, MAXPATHLEN);
1589: strlcat(buf, "/", MAXPATHLEN);
1.6 schwarze 1590: sz = strlcat(buf, fn, MAXPATHLEN);
1.5 kristaps 1591: if (sz >= MAXPATHLEN) {
1592: fprintf(stderr, "%s: Path too long\n", dir);
1.14 schwarze 1593: continue;
1.5 kristaps 1594: }
1595:
1.4 kristaps 1596: nof = mandoc_calloc(1, sizeof(struct of));
1.5 kristaps 1597: nof->fname = mandoc_strdup(buf);
1.12 schwarze 1598: if (NULL != psec)
1599: nof->sec = mandoc_strdup(psec);
1600: if (NULL != parch)
1601: nof->arch = mandoc_strdup(parch);
1.14 schwarze 1602: nof->src_form = src_form;
1.12 schwarze 1603:
1604: /*
1605: * Remember the file name without the extension,
1606: * to be used as the page title in the database.
1607: */
1608:
1609: if (NULL != suffix)
1610: *suffix = '\0';
1611: nof->title = mandoc_strdup(fn);
1.5 kristaps 1612:
1.14 schwarze 1613: /*
1614: * Add the structure to the list.
1615: */
1616:
1.5 kristaps 1617: if (verb > 2)
1618: printf("%s: Scheduling\n", buf);
1.4 kristaps 1619: if (NULL == *of) {
1620: *of = nof;
1621: (*of)->first = nof;
1622: } else {
1.5 kristaps 1623: nof->first = (*of)->first;
1.4 kristaps 1624: (*of)->next = nof;
1625: *of = nof;
1626: }
1627: }
1628:
1.7 schwarze 1629: closedir(d);
1.4 kristaps 1630: return(1);
1631: }
1632:
1633: static void
1634: ofile_free(struct of *of)
1635: {
1636: struct of *nof;
1637:
1638: while (of) {
1639: nof = of->next;
1640: free(of->fname);
1.12 schwarze 1641: free(of->sec);
1642: free(of->arch);
1643: free(of->title);
1.4 kristaps 1644: free(of);
1645: of = nof;
1646: }
1647: }
1648:
1.1 kristaps 1649: static void
1650: usage(void)
1651: {
1652:
1.5 kristaps 1653: fprintf(stderr, "usage: %s [-v] "
1654: "[-d dir [files...] |"
1655: " -u dir [files...] |"
1656: " dir...]\n", progname);
1.1 kristaps 1657: }
CVSweb