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