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