Annotation of mandoc/makewhatis.c, Revision 1.18
1.18 ! kristaps 1: /* $Id: makewhatis.c,v 1.17 2011/07/10 13:03:31 kristaps Exp $ */
1.1 kristaps 2: /*
3: * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4: *
5: * Permission to use, copy, modify, and distribute this software for any
6: * purpose with or without fee is hereby granted, provided that the above
7: * copyright notice and this permission notice appear in all copies.
8: *
9: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16: */
17: #ifdef HAVE_CONFIG_H
18: #include "config.h"
19: #endif
20:
21: #include <sys/param.h>
22:
23: #include <assert.h>
24: #include <fcntl.h>
25: #include <getopt.h>
26: #include <stdio.h>
27: #include <stdint.h>
28: #include <stdlib.h>
29: #include <string.h>
30:
1.10 kristaps 31: #ifdef __linux__
32: # include <db_185.h>
33: #else
34: # include <db.h>
35: #endif
36:
1.1 kristaps 37: #include "man.h"
38: #include "mdoc.h"
39: #include "mandoc.h"
40:
41: #define MANDOC_DB "mandoc.db"
42: #define MANDOC_IDX "mandoc.index"
43: #define MANDOC_BUFSZ BUFSIZ
44: #define MANDOC_FLAGS O_CREAT|O_TRUNC|O_RDWR
45:
1.5 kristaps 46: /* Bit-fields. See makewhatis.1. */
47:
1.10 kristaps 48: #define TYPE_NAME 0x01
49: #define TYPE_FUNCTION 0x02
50: #define TYPE_UTILITY 0x04
51: #define TYPE_INCLUDES 0x08
52: #define TYPE_VARIABLE 0x10
53: #define TYPE_STANDARD 0x20
54: #define TYPE_AUTHOR 0x40
55: #define TYPE_CONFIG 0x80
56: #define TYPE_DESC 0x100
1.11 kristaps 57: #define TYPE_XREF 0x200
1.12 kristaps 58: #define TYPE_PATH 0x400
1.15 kristaps 59: #define TYPE_ENV 0x800
1.16 kristaps 60: #define TYPE_ERR 0x1000
1.3 kristaps 61:
1.5 kristaps 62: /* Buffer for storing growable data. */
63:
1.3 kristaps 64: struct buf {
65: char *cp;
66: size_t len;
67: size_t size;
1.1 kristaps 68: };
69:
1.3 kristaps 70: #define MAN_ARGS DB *hash, \
71: struct buf *buf, \
1.5 kristaps 72: struct buf *dbuf, \
1.1 kristaps 73: const struct man_node *n
1.3 kristaps 74: #define MDOC_ARGS DB *hash, \
75: struct buf *buf, \
1.5 kristaps 76: struct buf *dbuf, \
1.3 kristaps 77: const struct mdoc_node *n, \
78: const struct mdoc_meta *m
1.1 kristaps 79:
1.13 kristaps 80: static void buf_appendmdoc(struct buf *,
1.14 kristaps 81: const struct mdoc_node *, int);
1.5 kristaps 82: static void buf_append(struct buf *, const char *);
83: static void buf_appendb(struct buf *,
1.1 kristaps 84: const void *, size_t);
85: static void dbt_put(DB *, const char *, DBT *, DBT *);
1.3 kristaps 86: static void hash_put(DB *, const struct buf *, int);
1.18 ! kristaps 87: static void hash_reset(DB **);
1.1 kristaps 88: static int pman_node(MAN_ARGS);
89: static void pmdoc_node(MDOC_ARGS);
90: static void pmdoc_An(MDOC_ARGS);
91: static void pmdoc_Cd(MDOC_ARGS);
1.16 kristaps 92: static void pmdoc_Er(MDOC_ARGS);
1.15 kristaps 93: static void pmdoc_Ev(MDOC_ARGS);
1.1 kristaps 94: static void pmdoc_Fd(MDOC_ARGS);
95: static void pmdoc_In(MDOC_ARGS);
96: static void pmdoc_Fn(MDOC_ARGS);
97: static void pmdoc_Fo(MDOC_ARGS);
98: static void pmdoc_Nd(MDOC_ARGS);
99: static void pmdoc_Nm(MDOC_ARGS);
1.12 kristaps 100: static void pmdoc_Pa(MDOC_ARGS);
1.1 kristaps 101: static void pmdoc_St(MDOC_ARGS);
102: static void pmdoc_Vt(MDOC_ARGS);
1.11 kristaps 103: static void pmdoc_Xr(MDOC_ARGS);
1.5 kristaps 104: static void usage(void);
1.1 kristaps 105:
106: typedef void (*pmdoc_nf)(MDOC_ARGS);
107:
108: static const pmdoc_nf mdocs[MDOC_MAX] = {
109: NULL, /* Ap */
110: NULL, /* Dd */
111: NULL, /* Dt */
112: NULL, /* Os */
113: NULL, /* Sh */
114: NULL, /* Ss */
115: NULL, /* Pp */
116: NULL, /* D1 */
117: NULL, /* Dl */
118: NULL, /* Bd */
119: NULL, /* Ed */
120: NULL, /* Bl */
121: NULL, /* El */
122: NULL, /* It */
123: NULL, /* Ad */
124: pmdoc_An, /* An */
125: NULL, /* Ar */
126: pmdoc_Cd, /* Cd */
127: NULL, /* Cm */
128: NULL, /* Dv */
1.16 kristaps 129: pmdoc_Er, /* Er */
1.15 kristaps 130: pmdoc_Ev, /* Ev */
1.1 kristaps 131: NULL, /* Ex */
132: NULL, /* Fa */
133: pmdoc_Fd, /* Fd */
134: NULL, /* Fl */
135: pmdoc_Fn, /* Fn */
136: NULL, /* Ft */
137: NULL, /* Ic */
138: pmdoc_In, /* In */
139: NULL, /* Li */
140: pmdoc_Nd, /* Nd */
141: pmdoc_Nm, /* Nm */
142: NULL, /* Op */
143: NULL, /* Ot */
1.12 kristaps 144: pmdoc_Pa, /* Pa */
1.1 kristaps 145: NULL, /* Rv */
146: pmdoc_St, /* St */
147: pmdoc_Vt, /* Va */
148: pmdoc_Vt, /* Vt */
1.11 kristaps 149: pmdoc_Xr, /* Xr */
1.1 kristaps 150: NULL, /* %A */
151: NULL, /* %B */
152: NULL, /* %D */
153: NULL, /* %I */
154: NULL, /* %J */
155: NULL, /* %N */
156: NULL, /* %O */
157: NULL, /* %P */
158: NULL, /* %R */
159: NULL, /* %T */
160: NULL, /* %V */
161: NULL, /* Ac */
162: NULL, /* Ao */
163: NULL, /* Aq */
164: NULL, /* At */
165: NULL, /* Bc */
166: NULL, /* Bf */
167: NULL, /* Bo */
168: NULL, /* Bq */
169: NULL, /* Bsx */
170: NULL, /* Bx */
171: NULL, /* Db */
172: NULL, /* Dc */
173: NULL, /* Do */
174: NULL, /* Dq */
175: NULL, /* Ec */
176: NULL, /* Ef */
177: NULL, /* Em */
178: NULL, /* Eo */
179: NULL, /* Fx */
180: NULL, /* Ms */
181: NULL, /* No */
182: NULL, /* Ns */
183: NULL, /* Nx */
184: NULL, /* Ox */
185: NULL, /* Pc */
186: NULL, /* Pf */
187: NULL, /* Po */
188: NULL, /* Pq */
189: NULL, /* Qc */
190: NULL, /* Ql */
191: NULL, /* Qo */
192: NULL, /* Qq */
193: NULL, /* Re */
194: NULL, /* Rs */
195: NULL, /* Sc */
196: NULL, /* So */
197: NULL, /* Sq */
198: NULL, /* Sm */
199: NULL, /* Sx */
200: NULL, /* Sy */
201: NULL, /* Tn */
202: NULL, /* Ux */
203: NULL, /* Xc */
204: NULL, /* Xo */
205: pmdoc_Fo, /* Fo */
206: NULL, /* Fc */
207: NULL, /* Oo */
208: NULL, /* Oc */
209: NULL, /* Bk */
210: NULL, /* Ek */
211: NULL, /* Bt */
212: NULL, /* Hf */
213: NULL, /* Fr */
214: NULL, /* Ud */
215: NULL, /* Lb */
216: NULL, /* Lp */
217: NULL, /* Lk */
218: NULL, /* Mt */
219: NULL, /* Brq */
220: NULL, /* Bro */
221: NULL, /* Brc */
222: NULL, /* %C */
223: NULL, /* Es */
224: NULL, /* En */
225: NULL, /* Dx */
226: NULL, /* %Q */
227: NULL, /* br */
228: NULL, /* sp */
229: NULL, /* %U */
230: NULL, /* Ta */
231: };
232:
1.5 kristaps 233: static const char *progname;
234:
1.1 kristaps 235: int
236: main(int argc, char *argv[])
237: {
238: struct mparse *mp; /* parse sequence */
239: struct mdoc *mdoc; /* resulting mdoc */
240: struct man *man; /* resulting man */
241: char *fn; /* current file being parsed */
242: const char *msec, /* manual section */
243: *mtitle, /* manual title */
244: *arch, /* manual architecture */
245: *dir; /* result dir (default: cwd) */
246: char ibuf[MAXPATHLEN], /* index fname */
247: fbuf[MAXPATHLEN], /* btree fname */
1.3 kristaps 248: vbuf[8]; /* stringified record number */
1.10 kristaps 249: int ch, seq, verb;
1.1 kristaps 250: DB *idx, /* index database */
1.3 kristaps 251: *db, /* keyword database */
252: *hash; /* temporary keyword hashtable */
1.5 kristaps 253: DBT key, val;
1.18 ! kristaps 254: enum mandocerr ec;
1.9 kristaps 255: size_t sv;
1.1 kristaps 256: BTREEINFO info; /* btree configuration */
257: recno_t rec; /* current record number */
1.5 kristaps 258: struct buf buf, /* keyword buffer */
259: dbuf; /* description buffer */
1.1 kristaps 260: extern int optind;
261: extern char *optarg;
262:
263: progname = strrchr(argv[0], '/');
264: if (progname == NULL)
265: progname = argv[0];
266: else
267: ++progname;
268:
269: dir = "";
1.10 kristaps 270: verb = 0;
1.18 ! kristaps 271: db = idx = NULL;
! 272: mp = NULL;
! 273: hash = NULL;
! 274: ec = MANDOCLEVEL_SYSERR;
! 275:
! 276: memset(&buf, 0, sizeof(struct buf));
! 277: memset(&dbuf, 0, sizeof(struct buf));
1.1 kristaps 278:
1.10 kristaps 279: while (-1 != (ch = getopt(argc, argv, "d:v")))
1.1 kristaps 280: switch (ch) {
281: case ('d'):
282: dir = optarg;
283: break;
1.10 kristaps 284: case ('v'):
285: verb++;
286: break;
1.1 kristaps 287: default:
288: usage();
289: return((int)MANDOCLEVEL_BADARG);
290: }
291:
292: argc -= optind;
293: argv += optind;
294:
295: ibuf[0] = ibuf[MAXPATHLEN - 2] =
1.18 ! kristaps 296: fbuf[0] = fbuf[MAXPATHLEN - 2] = '\0';
1.1 kristaps 297:
298: strlcat(fbuf, dir, MAXPATHLEN);
299: strlcat(fbuf, MANDOC_DB, MAXPATHLEN);
300:
301: strlcat(ibuf, dir, MAXPATHLEN);
302: strlcat(ibuf, MANDOC_IDX, MAXPATHLEN);
303:
304: if ('\0' != fbuf[MAXPATHLEN - 2] ||
1.18 ! kristaps 305: '\0' != ibuf[MAXPATHLEN - 2]) {
1.10 kristaps 306: fprintf(stderr, "%s: Path too long\n", dir);
1.18 ! kristaps 307: goto out;
1.1 kristaps 308: }
309:
310: /*
311: * For the keyword database, open a BTREE database that allows
1.3 kristaps 312: * duplicates.
313: * For the index database, use a standard RECNO database type.
1.1 kristaps 314: */
315:
316: memset(&info, 0, sizeof(BTREEINFO));
317: info.flags = R_DUP;
318:
1.18 ! kristaps 319: db = dbopen(fbuf, MANDOC_FLAGS, 0644, DB_BTREE, &info);
! 320: idx = dbopen(ibuf, MANDOC_FLAGS, 0644, DB_RECNO, NULL);
1.1 kristaps 321:
322: if (NULL == db) {
1.18 ! kristaps 323: perror(fbuf);
! 324: goto out;
! 325: } else if (NULL == db) {
! 326: perror(ibuf);
! 327: goto out;
1.1 kristaps 328: }
329:
330: /*
1.10 kristaps 331: * Try parsing each manual given on the command line.
332: * If we fail, then emit an error and keep on going.
333: * Take resulting trees and push them down into the database code.
1.1 kristaps 334: * Use the auto-parser and don't report any errors.
335: */
336:
337: mp = mparse_alloc(MPARSE_AUTO, MANDOCLEVEL_FATAL, NULL, NULL);
338:
1.5 kristaps 339: buf.size = dbuf.size = MANDOC_BUFSZ;
1.3 kristaps 340: buf.cp = mandoc_malloc(buf.size);
1.5 kristaps 341: dbuf.cp = mandoc_malloc(dbuf.size);
1.1 kristaps 342:
1.18 ! kristaps 343: rec = 1;
! 344:
1.1 kristaps 345: while (NULL != (fn = *argv++)) {
346: mparse_reset(mp);
1.18 ! kristaps 347: hash_reset(&hash);
1.1 kristaps 348:
349: if (mparse_readfd(mp, -1, fn) >= MANDOCLEVEL_FATAL) {
350: fprintf(stderr, "%s: Parse failure\n", fn);
351: continue;
352: }
1.10 kristaps 353:
1.1 kristaps 354: mparse_result(mp, &mdoc, &man);
355: if (NULL == mdoc && NULL == man)
356: continue;
357:
358: msec = NULL != mdoc ?
1.18 ! kristaps 359: mdoc_meta(mdoc)->msec : man_meta(man)->msec;
1.1 kristaps 360: mtitle = NULL != mdoc ?
1.18 ! kristaps 361: mdoc_meta(mdoc)->title : man_meta(man)->title;
! 362: arch = NULL != mdoc ?
! 363: mdoc_meta(mdoc)->arch : "";
1.1 kristaps 364:
365: /*
366: * The index record value consists of a nil-terminated
367: * filename, a nil-terminated manual section, and a
368: * nil-terminated description. Since the description
369: * may not be set, we set a sentinel to see if we're
370: * going to write a nil byte in its place.
371: */
372:
1.5 kristaps 373: dbuf.len = 0;
374: buf_appendb(&dbuf, fn, strlen(fn) + 1);
375: buf_appendb(&dbuf, msec, strlen(msec) + 1);
376: buf_appendb(&dbuf, mtitle, strlen(mtitle) + 1);
1.18 ! kristaps 377: buf_appendb(&dbuf, arch, strlen(arch) + 1);
1.1 kristaps 378:
1.5 kristaps 379: sv = dbuf.len;
1.1 kristaps 380:
381: /* Fix the record number in the btree value. */
382:
383: if (mdoc)
1.5 kristaps 384: pmdoc_node(hash, &buf, &dbuf,
385: mdoc_node(mdoc), mdoc_meta(mdoc));
1.1 kristaps 386: else
1.5 kristaps 387: pman_node(hash, &buf, &dbuf, man_node(man));
1.3 kristaps 388:
389: /*
390: * Copy from the in-memory hashtable of pending keywords
391: * into the database.
392: */
393:
394: memset(vbuf, 0, sizeof(uint32_t));
395: memcpy(vbuf + 4, &rec, sizeof(uint32_t));
396:
397: seq = R_FIRST;
398: while (0 == (ch = (*hash->seq)(hash, &key, &val, seq))) {
1.7 kristaps 399: seq = R_NEXT;
400:
1.3 kristaps 401: memcpy(vbuf, val.data, sizeof(uint32_t));
402: val.size = sizeof(vbuf);
403: val.data = vbuf;
1.7 kristaps 404:
1.10 kristaps 405: if (verb > 1)
1.18 ! kristaps 406: printf("%s: Keyword %s: 0x%x\n",
! 407: fn, (char *)key.data,
1.8 kristaps 408: *(int *)val.data);
409:
1.18 ! kristaps 410: dbt_put(db, fbuf, &key, &val);
1.5 kristaps 411:
1.3 kristaps 412: }
413: if (ch < 0) {
414: perror("hash");
415: exit((int)MANDOCLEVEL_SYSERR);
416: }
1.1 kristaps 417:
418: /*
1.3 kristaps 419: * Apply to the index. If we haven't had a description
420: * set, put an empty one in now.
1.1 kristaps 421: */
422:
1.5 kristaps 423: if (dbuf.len == sv)
424: buf_appendb(&dbuf, "", 1);
425:
426: key.data = &rec;
427: key.size = sizeof(recno_t);
1.1 kristaps 428:
1.5 kristaps 429: val.data = dbuf.cp;
430: val.size = dbuf.len;
1.1 kristaps 431:
1.10 kristaps 432: if (verb > 0)
433: printf("%s: Indexed\n", fn);
1.8 kristaps 434:
1.18 ! kristaps 435: dbt_put(idx, ibuf, &key, &val);
1.1 kristaps 436: rec++;
437: }
438:
1.18 ! kristaps 439: ec = MANDOCLEVEL_OK;
1.10 kristaps 440:
1.18 ! kristaps 441: out:
! 442: if (db)
! 443: (*db->close)(db);
! 444: if (idx)
! 445: (*idx->close)(idx);
1.8 kristaps 446: if (hash)
447: (*hash->close)(hash);
1.18 ! kristaps 448: if (mp)
! 449: mparse_free(mp);
1.1 kristaps 450:
1.3 kristaps 451: free(buf.cp);
1.5 kristaps 452: free(dbuf.cp);
1.1 kristaps 453:
1.18 ! kristaps 454: return((int)ec);
1.1 kristaps 455: }
456:
457: /*
1.5 kristaps 458: * Grow the buffer (if necessary) and copy in a binary string.
1.1 kristaps 459: */
460: static void
1.3 kristaps 461: buf_appendb(struct buf *buf, const void *cp, size_t sz)
462: {
463:
464: /* Overshoot by MANDOC_BUFSZ. */
465:
466: while (buf->len + sz >= buf->size) {
467: buf->size = buf->len + sz + MANDOC_BUFSZ;
468: buf->cp = mandoc_realloc(buf->cp, buf->size);
469: }
470:
471: memcpy(buf->cp + (int)buf->len, cp, sz);
472: buf->len += sz;
473: }
474:
1.1 kristaps 475: /*
1.5 kristaps 476: * Append a nil-terminated string to the buffer.
477: * This can be invoked multiple times.
478: * The buffer string will be nil-terminated.
479: * If invoked multiple times, a space is put between strings.
1.1 kristaps 480: */
481: static void
1.3 kristaps 482: buf_append(struct buf *buf, const char *cp)
483: {
484: size_t sz;
485:
486: if (0 == (sz = strlen(cp)))
487: return;
488:
489: if (buf->len)
490: buf->cp[(int)buf->len - 1] = ' ';
491:
492: buf_appendb(buf, cp, sz + 1);
493: }
494:
1.13 kristaps 495: /*
496: * Recursively add all text from a given node.
497: * This is optimised for general mdoc nodes in this context, which do
498: * not consist of subexpressions and having a recursive call for n->next
499: * would be wasteful.
1.14 kristaps 500: * The "f" variable should be 0 unless called from pmdoc_Nd for the
501: * description buffer, which does not start at the beginning of the
502: * buffer.
1.13 kristaps 503: */
504: static void
1.14 kristaps 505: buf_appendmdoc(struct buf *buf, const struct mdoc_node *n, int f)
1.13 kristaps 506: {
507:
508: for ( ; n; n = n->next) {
509: if (n->child)
1.14 kristaps 510: buf_appendmdoc(buf, n->child, f);
511:
512: if (MDOC_TEXT == n->type && f) {
513: f = 0;
514: buf_appendb(buf, n->string,
515: strlen(n->string) + 1);
516: } else if (MDOC_TEXT == n->type)
1.13 kristaps 517: buf_append(buf, n->string);
1.14 kristaps 518:
1.13 kristaps 519: }
520: }
521:
1.1 kristaps 522: /* ARGSUSED */
523: static void
524: pmdoc_An(MDOC_ARGS)
525: {
526:
527: if (SEC_AUTHORS != n->sec)
528: return;
529:
1.14 kristaps 530: buf_appendmdoc(buf, n->child, 0);
1.3 kristaps 531: hash_put(hash, buf, TYPE_AUTHOR);
1.18 ! kristaps 532: }
! 533:
! 534: static void
! 535: hash_reset(DB **db)
! 536: {
! 537: DB *hash;
! 538:
! 539: if (NULL != (hash = *db))
! 540: (*hash->close)(hash);
! 541:
! 542: *db = dbopen(NULL, MANDOC_FLAGS, 0644, DB_HASH, NULL);
! 543: if (NULL == *db) {
! 544: perror("hash");
! 545: exit((int)MANDOCLEVEL_SYSERR);
! 546: }
1.1 kristaps 547: }
548:
549: /* ARGSUSED */
550: static void
551: pmdoc_Fd(MDOC_ARGS)
552: {
553: const char *start, *end;
554: size_t sz;
555:
556: if (SEC_SYNOPSIS != n->sec)
557: return;
558: if (NULL == (n = n->child) || MDOC_TEXT != n->type)
559: return;
560:
561: /*
562: * Only consider those `Fd' macro fields that begin with an
563: * "inclusion" token (versus, e.g., #define).
564: */
565: if (strcmp("#include", n->string))
566: return;
567:
568: if (NULL == (n = n->next) || MDOC_TEXT != n->type)
569: return;
570:
571: /*
572: * Strip away the enclosing angle brackets and make sure we're
573: * not zero-length.
574: */
575:
576: start = n->string;
577: if ('<' == *start || '"' == *start)
578: start++;
579:
580: if (0 == (sz = strlen(start)))
581: return;
582:
583: end = &start[(int)sz - 1];
584: if ('>' == *end || '"' == *end)
585: end--;
586:
587: assert(end >= start);
588:
1.3 kristaps 589: buf_appendb(buf, start, (size_t)(end - start + 1));
590: buf_appendb(buf, "", 1);
591:
592: hash_put(hash, buf, TYPE_INCLUDES);
1.1 kristaps 593: }
594:
595: /* ARGSUSED */
596: static void
597: pmdoc_Cd(MDOC_ARGS)
598: {
599:
600: if (SEC_SYNOPSIS != n->sec)
601: return;
602:
1.14 kristaps 603: buf_appendmdoc(buf, n->child, 0);
1.3 kristaps 604: hash_put(hash, buf, TYPE_CONFIG);
1.1 kristaps 605: }
606:
607: /* ARGSUSED */
608: static void
609: pmdoc_In(MDOC_ARGS)
610: {
611:
612: if (SEC_SYNOPSIS != n->sec)
613: return;
614: if (NULL == n->child || MDOC_TEXT != n->child->type)
615: return;
616:
1.3 kristaps 617: buf_append(buf, n->child->string);
618: hash_put(hash, buf, TYPE_INCLUDES);
1.1 kristaps 619: }
620:
621: /* ARGSUSED */
622: static void
623: pmdoc_Fn(MDOC_ARGS)
624: {
625: const char *cp;
626:
627: if (SEC_SYNOPSIS != n->sec)
628: return;
629: if (NULL == n->child || MDOC_TEXT != n->child->type)
630: return;
631:
632: /* .Fn "struct type *arg" "foo" */
633:
634: cp = strrchr(n->child->string, ' ');
635: if (NULL == cp)
636: cp = n->child->string;
637:
638: /* Strip away pointer symbol. */
639:
640: while ('*' == *cp)
641: cp++;
642:
1.3 kristaps 643: buf_append(buf, cp);
644: hash_put(hash, buf, TYPE_FUNCTION);
1.1 kristaps 645: }
646:
647: /* ARGSUSED */
648: static void
649: pmdoc_St(MDOC_ARGS)
650: {
651:
652: if (SEC_STANDARDS != n->sec)
653: return;
654: if (NULL == n->child || MDOC_TEXT != n->child->type)
655: return;
656:
1.3 kristaps 657: buf_append(buf, n->child->string);
658: hash_put(hash, buf, TYPE_STANDARD);
1.11 kristaps 659: }
660:
661: /* ARGSUSED */
662: static void
663: pmdoc_Xr(MDOC_ARGS)
664: {
665:
666: if (NULL == (n = n->child))
667: return;
668:
669: buf_appendb(buf, n->string, strlen(n->string));
670:
671: if (NULL != (n = n->next)) {
672: buf_appendb(buf, ".", 1);
673: buf_appendb(buf, n->string, strlen(n->string) + 1);
674: } else
675: buf_appendb(buf, ".", 2);
676:
677: hash_put(hash, buf, TYPE_XREF);
1.1 kristaps 678: }
679:
680: /* ARGSUSED */
681: static void
682: pmdoc_Vt(MDOC_ARGS)
683: {
684: const char *start;
685: size_t sz;
686:
687: if (SEC_SYNOPSIS != n->sec)
688: return;
689: if (MDOC_Vt == n->tok && MDOC_BODY != n->type)
690: return;
691: if (NULL == n->last || MDOC_TEXT != n->last->type)
692: return;
693:
694: /*
695: * Strip away leading pointer symbol '*' and trailing ';'.
696: */
697:
698: start = n->last->string;
699:
700: while ('*' == *start)
701: start++;
702:
703: if (0 == (sz = strlen(start)))
704: return;
705:
706: if (';' == start[(int)sz - 1])
707: sz--;
708:
709: if (0 == sz)
710: return;
711:
1.3 kristaps 712: buf_appendb(buf, start, sz);
713: buf_appendb(buf, "", 1);
714: hash_put(hash, buf, TYPE_VARIABLE);
1.1 kristaps 715: }
716:
717: /* ARGSUSED */
718: static void
719: pmdoc_Fo(MDOC_ARGS)
720: {
721:
722: if (SEC_SYNOPSIS != n->sec || MDOC_HEAD != n->type)
723: return;
724: if (NULL == n->child || MDOC_TEXT != n->child->type)
725: return;
726:
1.3 kristaps 727: buf_append(buf, n->child->string);
728: hash_put(hash, buf, TYPE_FUNCTION);
1.1 kristaps 729: }
730:
731:
732: /* ARGSUSED */
733: static void
734: pmdoc_Nd(MDOC_ARGS)
735: {
1.6 kristaps 736:
1.13 kristaps 737: if (MDOC_BODY != n->type)
738: return;
1.6 kristaps 739:
1.14 kristaps 740: buf_appendmdoc(dbuf, n->child, 1);
741: buf_appendmdoc(buf, n->child, 0);
1.6 kristaps 742:
743: hash_put(hash, buf, TYPE_DESC);
1.16 kristaps 744: }
745:
746: /* ARGSUSED */
747: static void
748: pmdoc_Er(MDOC_ARGS)
749: {
750:
751: if (SEC_ERRORS != n->sec)
752: return;
753:
754: buf_appendmdoc(buf, n->child, 0);
755: hash_put(hash, buf, TYPE_ERR);
1.15 kristaps 756: }
757:
758: /* ARGSUSED */
759: static void
760: pmdoc_Ev(MDOC_ARGS)
761: {
762:
763: if (SEC_ENVIRONMENT != n->sec)
764: return;
765:
766: buf_appendmdoc(buf, n->child, 0);
767: hash_put(hash, buf, TYPE_ENV);
1.12 kristaps 768: }
769:
770: /* ARGSUSED */
771: static void
772: pmdoc_Pa(MDOC_ARGS)
773: {
774:
775: if (SEC_FILES != n->sec)
776: return;
777:
1.14 kristaps 778: buf_appendmdoc(buf, n->child, 0);
1.12 kristaps 779: hash_put(hash, buf, TYPE_PATH);
1.1 kristaps 780: }
781:
782: /* ARGSUSED */
783: static void
784: pmdoc_Nm(MDOC_ARGS)
785: {
786:
787: if (SEC_NAME == n->sec) {
1.14 kristaps 788: buf_appendmdoc(buf, n->child, 0);
1.3 kristaps 789: hash_put(hash, buf, TYPE_NAME);
1.1 kristaps 790: return;
791: } else if (SEC_SYNOPSIS != n->sec || MDOC_HEAD != n->type)
792: return;
793:
1.3 kristaps 794: if (NULL == n->child)
795: buf_append(buf, m->name);
796:
1.14 kristaps 797: buf_appendmdoc(buf, n->child, 0);
1.3 kristaps 798: hash_put(hash, buf, TYPE_UTILITY);
799: }
800:
801: static void
802: hash_put(DB *db, const struct buf *buf, int mask)
803: {
804: DBT key, val;
805: int rc;
806:
1.7 kristaps 807: if (buf->len < 2)
808: return;
809:
1.3 kristaps 810: key.data = buf->cp;
1.7 kristaps 811: key.size = buf->len;
1.3 kristaps 812:
813: if ((rc = (*db->get)(db, &key, &val, 0)) < 0) {
814: perror("hash");
815: exit((int)MANDOCLEVEL_SYSERR);
816: } else if (0 == rc)
817: mask |= *(int *)val.data;
818:
819: val.data = &mask;
820: val.size = sizeof(int);
1.1 kristaps 821:
1.3 kristaps 822: if ((rc = (*db->put)(db, &key, &val, 0)) < 0) {
823: perror("hash");
824: exit((int)MANDOCLEVEL_SYSERR);
825: }
1.1 kristaps 826: }
827:
828: static void
829: dbt_put(DB *db, const char *dbn, DBT *key, DBT *val)
830: {
831:
1.5 kristaps 832: assert(key->size);
1.1 kristaps 833: assert(val->size);
834:
835: if (0 == (*db->put)(db, key, val, 0))
836: return;
837:
838: perror(dbn);
839: exit((int)MANDOCLEVEL_SYSERR);
840: /* NOTREACHED */
841: }
842:
843: /*
844: * Call out to per-macro handlers after clearing the persistent database
845: * key. If the macro sets the database key, flush it to the database.
846: */
847: static void
848: pmdoc_node(MDOC_ARGS)
849: {
850:
851: if (NULL == n)
852: return;
853:
854: switch (n->type) {
855: case (MDOC_HEAD):
856: /* FALLTHROUGH */
857: case (MDOC_BODY):
858: /* FALLTHROUGH */
859: case (MDOC_TAIL):
860: /* FALLTHROUGH */
861: case (MDOC_BLOCK):
862: /* FALLTHROUGH */
863: case (MDOC_ELEM):
864: if (NULL == mdocs[n->tok])
865: break;
866:
1.3 kristaps 867: buf->len = 0;
1.5 kristaps 868: (*mdocs[n->tok])(hash, buf, dbuf, n, m);
1.1 kristaps 869: break;
870: default:
871: break;
872: }
873:
1.5 kristaps 874: pmdoc_node(hash, buf, dbuf, n->child, m);
875: pmdoc_node(hash, buf, dbuf, n->next, m);
1.1 kristaps 876: }
877:
878: static int
879: pman_node(MAN_ARGS)
880: {
881: const struct man_node *head, *body;
882: const char *start, *sv;
883: size_t sz;
884:
885: if (NULL == n)
886: return(0);
887:
888: /*
889: * We're only searching for one thing: the first text child in
890: * the BODY of a NAME section. Since we don't keep track of
891: * sections in -man, run some hoops to find out whether we're in
892: * the correct section or not.
893: */
894:
895: if (MAN_BODY == n->type && MAN_SH == n->tok) {
896: body = n;
897: assert(body->parent);
898: if (NULL != (head = body->parent->head) &&
899: 1 == head->nchild &&
900: NULL != (head = (head->child)) &&
901: MAN_TEXT == head->type &&
902: 0 == strcmp(head->string, "NAME") &&
903: NULL != (body = body->child) &&
904: MAN_TEXT == body->type) {
905:
906: assert(body->string);
907: start = sv = body->string;
908:
909: /*
910: * Go through a special heuristic dance here.
911: * This is why -man manuals are great!
912: * (I'm being sarcastic: my eyes are bleeding.)
913: * Conventionally, one or more manual names are
914: * comma-specified prior to a whitespace, then a
915: * dash, then a description. Try to puzzle out
916: * the name parts here.
917: */
918:
919: for ( ;; ) {
920: sz = strcspn(start, " ,");
921: if ('\0' == start[(int)sz])
922: break;
923:
1.3 kristaps 924: buf->len = 0;
925: buf_appendb(buf, start, sz);
926: buf_appendb(buf, "", 1);
1.1 kristaps 927:
1.3 kristaps 928: hash_put(hash, buf, TYPE_NAME);
1.1 kristaps 929:
930: if (' ' == start[(int)sz]) {
931: start += (int)sz + 1;
932: break;
933: }
934:
935: assert(',' == start[(int)sz]);
936: start += (int)sz + 1;
937: while (' ' == *start)
938: start++;
939: }
940:
1.17 kristaps 941: buf->len = 0;
942:
1.1 kristaps 943: if (sv == start) {
1.3 kristaps 944: buf_append(buf, start);
1.1 kristaps 945: return(1);
946: }
947:
948: while (' ' == *start)
949: start++;
950:
951: if (0 == strncmp(start, "-", 1))
952: start += 1;
953: else if (0 == strncmp(start, "\\-", 2))
954: start += 2;
955: else if (0 == strncmp(start, "\\(en", 4))
956: start += 4;
957: else if (0 == strncmp(start, "\\(em", 4))
958: start += 4;
959:
960: while (' ' == *start)
961: start++;
962:
1.6 kristaps 963: sz = strlen(start) + 1;
964: buf_appendb(dbuf, start, sz);
965: buf_appendb(buf, start, sz);
1.17 kristaps 966:
967: hash_put(hash, buf, TYPE_DESC);
1.1 kristaps 968: }
969: }
970:
1.5 kristaps 971: if (pman_node(hash, buf, dbuf, n->child))
1.1 kristaps 972: return(1);
1.5 kristaps 973: if (pman_node(hash, buf, dbuf, n->next))
1.1 kristaps 974: return(1);
975:
976: return(0);
977: }
978:
979: static void
980: usage(void)
981: {
982:
1.10 kristaps 983: fprintf(stderr, "usage: %s [-v] [-d path] [file...]\n",
984: progname);
1.1 kristaps 985: }
CVSweb