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