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