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