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