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