Annotation of mandoc/makewhatis.c, Revision 1.9
1.9 ! kristaps 1: /* $Id: makewhatis.c,v 1.8 2011/06/22 09:00:14 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
1.6 kristaps 55: #define TYPE_DESC 0x100
1.3 kristaps 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.9 ! kristaps 244: size_t sv;
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.
1.1 kristaps 309: */
310:
311: memset(&info, 0, sizeof(BTREEINFO));
312: info.flags = R_DUP;
313: db = dbopen(fbbuf, MANDOC_FLAGS, 0644, DB_BTREE, &info);
314:
315: if (NULL == db) {
316: perror(fbbuf);
317: exit((int)MANDOCLEVEL_SYSERR);
318: }
319:
320: idx = dbopen(ibbuf, MANDOC_FLAGS, 0644, DB_RECNO, NULL);
321:
322: if (NULL == db) {
323: perror(ibbuf);
324: (*db->close)(db);
325: exit((int)MANDOCLEVEL_SYSERR);
326: }
327:
328: /*
329: * Try parsing the manuals given on the command line. If we
330: * totally fail, then just keep on going. Take resulting trees
331: * and push them down into the database code.
332: * Use the auto-parser and don't report any errors.
333: */
334:
335: mp = mparse_alloc(MPARSE_AUTO, MANDOCLEVEL_FATAL, NULL, NULL);
336:
337: rec = 1;
1.9 ! kristaps 338: hash = NULL;
1.3 kristaps 339:
340: memset(&buf, 0, sizeof(struct buf));
1.5 kristaps 341: memset(&dbuf, 0, sizeof(struct buf));
342:
343: buf.size = dbuf.size = MANDOC_BUFSZ;
1.3 kristaps 344:
345: buf.cp = mandoc_malloc(buf.size);
1.5 kristaps 346: dbuf.cp = mandoc_malloc(dbuf.size);
1.1 kristaps 347:
348: while (NULL != (fn = *argv++)) {
349: mparse_reset(mp);
350:
1.8 kristaps 351: if (hash)
352: (*hash->close)(hash);
353:
354: hash = dbopen(NULL, MANDOC_FLAGS, 0644, DB_HASH, NULL);
355:
356: if (NULL == hash) {
357: perror("hash");
358: exit((int)MANDOCLEVEL_SYSERR);
359: }
360:
1.1 kristaps 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))) {
1.7 kristaps 419: seq = R_NEXT;
420:
1.3 kristaps 421: memcpy(vbuf, val.data, sizeof(uint32_t));
422: val.size = sizeof(vbuf);
423: val.data = vbuf;
1.7 kristaps 424:
1.8 kristaps 425: printf("Added: %s (%zu): 0x%x\n",
426: (char *)key.data, key.size,
427: *(int *)val.data);
428:
1.3 kristaps 429: dbt_put(db, fbbuf, &key, &val);
1.5 kristaps 430:
1.3 kristaps 431: }
432:
433: if (ch < 0) {
434: perror("hash");
435: exit((int)MANDOCLEVEL_SYSERR);
436: }
1.1 kristaps 437:
438: /*
1.3 kristaps 439: * Apply to the index. If we haven't had a description
440: * set, put an empty one in now.
1.1 kristaps 441: */
442:
1.5 kristaps 443: if (dbuf.len == sv)
444: buf_appendb(&dbuf, "", 1);
445:
446: key.data = &rec;
447: key.size = sizeof(recno_t);
1.1 kristaps 448:
1.5 kristaps 449: val.data = dbuf.cp;
450: val.size = dbuf.len;
1.1 kristaps 451:
1.8 kristaps 452: printf("Indexed: %s\n", fn);
453:
1.5 kristaps 454: dbt_put(idx, ibbuf, &key, &val);
1.1 kristaps 455: rec++;
456: }
457:
458: (*db->close)(db);
459: (*idx->close)(idx);
1.8 kristaps 460: if (hash)
461: (*hash->close)(hash);
1.1 kristaps 462:
463: mparse_free(mp);
464:
1.3 kristaps 465: free(buf.cp);
1.5 kristaps 466: free(dbuf.cp);
1.1 kristaps 467:
468: /* Atomically replace the file with our temporary one. */
469:
470: if (-1 == rename(fbbuf, fbuf))
471: perror(fbuf);
472: if (-1 == rename(ibbuf, ibuf))
473: perror(fbuf);
474:
475: return((int)MANDOCLEVEL_OK);
476: }
477:
478: /*
1.5 kristaps 479: * Grow the buffer (if necessary) and copy in a binary string.
1.1 kristaps 480: */
481: static void
1.3 kristaps 482: buf_appendb(struct buf *buf, const void *cp, size_t sz)
483: {
484:
485: /* Overshoot by MANDOC_BUFSZ. */
486:
487: while (buf->len + sz >= buf->size) {
488: buf->size = buf->len + sz + MANDOC_BUFSZ;
489: buf->cp = mandoc_realloc(buf->cp, buf->size);
490: }
491:
492: memcpy(buf->cp + (int)buf->len, cp, sz);
493: buf->len += sz;
494: }
495:
1.1 kristaps 496: /*
1.5 kristaps 497: * Append a nil-terminated string to the buffer.
498: * This can be invoked multiple times.
499: * The buffer string will be nil-terminated.
500: * If invoked multiple times, a space is put between strings.
1.1 kristaps 501: */
502: static void
1.3 kristaps 503: buf_append(struct buf *buf, const char *cp)
504: {
505: size_t sz;
506:
507: if (0 == (sz = strlen(cp)))
508: return;
509:
510: if (buf->len)
511: buf->cp[(int)buf->len - 1] = ' ';
512:
513: buf_appendb(buf, cp, sz + 1);
514: }
515:
1.1 kristaps 516: /* ARGSUSED */
517: static void
518: pmdoc_An(MDOC_ARGS)
519: {
520:
521: if (SEC_AUTHORS != n->sec)
522: return;
523:
524: for (n = n->child; n; n = n->next)
525: if (MDOC_TEXT == n->type)
1.3 kristaps 526: buf_append(buf, n->string);
1.1 kristaps 527:
1.3 kristaps 528: hash_put(hash, buf, TYPE_AUTHOR);
1.1 kristaps 529: }
530:
531: /* ARGSUSED */
532: static void
533: pmdoc_Fd(MDOC_ARGS)
534: {
535: const char *start, *end;
536: size_t sz;
537:
538: if (SEC_SYNOPSIS != n->sec)
539: return;
540: if (NULL == (n = n->child) || MDOC_TEXT != n->type)
541: return;
542:
543: /*
544: * Only consider those `Fd' macro fields that begin with an
545: * "inclusion" token (versus, e.g., #define).
546: */
547: if (strcmp("#include", n->string))
548: return;
549:
550: if (NULL == (n = n->next) || MDOC_TEXT != n->type)
551: return;
552:
553: /*
554: * Strip away the enclosing angle brackets and make sure we're
555: * not zero-length.
556: */
557:
558: start = n->string;
559: if ('<' == *start || '"' == *start)
560: start++;
561:
562: if (0 == (sz = strlen(start)))
563: return;
564:
565: end = &start[(int)sz - 1];
566: if ('>' == *end || '"' == *end)
567: end--;
568:
569: assert(end >= start);
570:
1.3 kristaps 571: buf_appendb(buf, start, (size_t)(end - start + 1));
572: buf_appendb(buf, "", 1);
573:
574: hash_put(hash, buf, TYPE_INCLUDES);
1.1 kristaps 575: }
576:
577: /* ARGSUSED */
578: static void
579: pmdoc_Cd(MDOC_ARGS)
580: {
581:
582: if (SEC_SYNOPSIS != n->sec)
583: return;
584:
585: for (n = n->child; n; n = n->next)
586: if (MDOC_TEXT == n->type)
1.3 kristaps 587: buf_append(buf, n->string);
1.1 kristaps 588:
1.3 kristaps 589: hash_put(hash, buf, TYPE_CONFIG);
1.1 kristaps 590: }
591:
592: /* ARGSUSED */
593: static void
594: pmdoc_In(MDOC_ARGS)
595: {
596:
597: if (SEC_SYNOPSIS != n->sec)
598: return;
599: if (NULL == n->child || MDOC_TEXT != n->child->type)
600: return;
601:
1.3 kristaps 602: buf_append(buf, n->child->string);
603: hash_put(hash, buf, TYPE_INCLUDES);
1.1 kristaps 604: }
605:
606: /* ARGSUSED */
607: static void
608: pmdoc_Fn(MDOC_ARGS)
609: {
610: const char *cp;
611:
612: if (SEC_SYNOPSIS != n->sec)
613: return;
614: if (NULL == n->child || MDOC_TEXT != n->child->type)
615: return;
616:
617: /* .Fn "struct type *arg" "foo" */
618:
619: cp = strrchr(n->child->string, ' ');
620: if (NULL == cp)
621: cp = n->child->string;
622:
623: /* Strip away pointer symbol. */
624:
625: while ('*' == *cp)
626: cp++;
627:
1.3 kristaps 628: buf_append(buf, cp);
629: hash_put(hash, buf, TYPE_FUNCTION);
1.1 kristaps 630: }
631:
632: /* ARGSUSED */
633: static void
634: pmdoc_St(MDOC_ARGS)
635: {
636:
637: if (SEC_STANDARDS != n->sec)
638: return;
639: if (NULL == n->child || MDOC_TEXT != n->child->type)
640: return;
641:
1.3 kristaps 642: buf_append(buf, n->child->string);
643: hash_put(hash, buf, TYPE_STANDARD);
1.1 kristaps 644: }
645:
646: /* ARGSUSED */
647: static void
648: pmdoc_Vt(MDOC_ARGS)
649: {
650: const char *start;
651: size_t sz;
652:
653: if (SEC_SYNOPSIS != n->sec)
654: return;
655: if (MDOC_Vt == n->tok && MDOC_BODY != n->type)
656: return;
657: if (NULL == n->last || MDOC_TEXT != n->last->type)
658: return;
659:
660: /*
661: * Strip away leading pointer symbol '*' and trailing ';'.
662: */
663:
664: start = n->last->string;
665:
666: while ('*' == *start)
667: start++;
668:
669: if (0 == (sz = strlen(start)))
670: return;
671:
672: if (';' == start[(int)sz - 1])
673: sz--;
674:
675: if (0 == sz)
676: return;
677:
1.3 kristaps 678: buf_appendb(buf, start, sz);
679: buf_appendb(buf, "", 1);
680: hash_put(hash, buf, TYPE_VARIABLE);
1.1 kristaps 681: }
682:
683: /* ARGSUSED */
684: static void
685: pmdoc_Fo(MDOC_ARGS)
686: {
687:
688: if (SEC_SYNOPSIS != n->sec || MDOC_HEAD != n->type)
689: return;
690: if (NULL == n->child || MDOC_TEXT != n->child->type)
691: return;
692:
1.3 kristaps 693: buf_append(buf, n->child->string);
694: hash_put(hash, buf, TYPE_FUNCTION);
1.1 kristaps 695: }
696:
697:
698: /* ARGSUSED */
699: static void
700: pmdoc_Nd(MDOC_ARGS)
701: {
702: int first;
1.6 kristaps 703: size_t sz;
1.1 kristaps 704:
705: for (first = 1, n = n->child; n; n = n->next) {
706: if (MDOC_TEXT != n->type)
707: continue;
1.6 kristaps 708:
709: if (first) {
710: sz = strlen(n->string) + 1;
711: buf_appendb(dbuf, n->string, sz);
712: buf_appendb(buf, n->string, sz);
713: } else {
1.5 kristaps 714: buf_append(dbuf, n->string);
1.6 kristaps 715: buf_append(buf, n->string);
716: }
717:
1.1 kristaps 718: first = 0;
719: }
1.6 kristaps 720:
721: hash_put(hash, buf, TYPE_DESC);
1.1 kristaps 722: }
723:
724: /* ARGSUSED */
725: static void
726: pmdoc_Nm(MDOC_ARGS)
727: {
728:
729: if (SEC_NAME == n->sec) {
1.3 kristaps 730: for (n = n->child; n; n = n->next)
731: if (MDOC_TEXT == n->type)
732: buf_append(buf, n->string);
733: hash_put(hash, buf, TYPE_NAME);
1.1 kristaps 734: return;
735: } else if (SEC_SYNOPSIS != n->sec || MDOC_HEAD != n->type)
736: return;
737:
1.3 kristaps 738: if (NULL == n->child)
739: buf_append(buf, m->name);
740:
741: for (n = n->child; n; n = n->next)
742: if (MDOC_TEXT == n->type)
743: buf_append(buf, n->string);
744:
745: hash_put(hash, buf, TYPE_UTILITY);
746: }
747:
748: static void
749: hash_put(DB *db, const struct buf *buf, int mask)
750: {
751: DBT key, val;
752: int rc;
753:
1.7 kristaps 754: if (buf->len < 2)
755: return;
756:
1.3 kristaps 757: key.data = buf->cp;
1.7 kristaps 758: key.size = buf->len;
1.3 kristaps 759:
760: if ((rc = (*db->get)(db, &key, &val, 0)) < 0) {
761: perror("hash");
762: exit((int)MANDOCLEVEL_SYSERR);
763: } else if (0 == rc)
764: mask |= *(int *)val.data;
765:
766: val.data = &mask;
767: val.size = sizeof(int);
1.1 kristaps 768:
1.3 kristaps 769: if ((rc = (*db->put)(db, &key, &val, 0)) < 0) {
770: perror("hash");
771: exit((int)MANDOCLEVEL_SYSERR);
772: }
1.1 kristaps 773: }
774:
775: static void
776: dbt_put(DB *db, const char *dbn, DBT *key, DBT *val)
777: {
778:
1.5 kristaps 779: assert(key->size);
1.1 kristaps 780: assert(val->size);
781:
782: if (0 == (*db->put)(db, key, val, 0))
783: return;
784:
785: perror(dbn);
786: exit((int)MANDOCLEVEL_SYSERR);
787: /* NOTREACHED */
788: }
789:
790: /*
791: * Call out to per-macro handlers after clearing the persistent database
792: * key. If the macro sets the database key, flush it to the database.
793: */
794: static void
795: pmdoc_node(MDOC_ARGS)
796: {
797:
798: if (NULL == n)
799: return;
800:
801: switch (n->type) {
802: case (MDOC_HEAD):
803: /* FALLTHROUGH */
804: case (MDOC_BODY):
805: /* FALLTHROUGH */
806: case (MDOC_TAIL):
807: /* FALLTHROUGH */
808: case (MDOC_BLOCK):
809: /* FALLTHROUGH */
810: case (MDOC_ELEM):
811: if (NULL == mdocs[n->tok])
812: break;
813:
1.3 kristaps 814: buf->len = 0;
1.5 kristaps 815: (*mdocs[n->tok])(hash, buf, dbuf, n, m);
1.1 kristaps 816: break;
817: default:
818: break;
819: }
820:
1.5 kristaps 821: pmdoc_node(hash, buf, dbuf, n->child, m);
822: pmdoc_node(hash, buf, dbuf, n->next, m);
1.1 kristaps 823: }
824:
825: static int
826: pman_node(MAN_ARGS)
827: {
828: const struct man_node *head, *body;
829: const char *start, *sv;
830: size_t sz;
831:
832: if (NULL == n)
833: return(0);
834:
835: /*
836: * We're only searching for one thing: the first text child in
837: * the BODY of a NAME section. Since we don't keep track of
838: * sections in -man, run some hoops to find out whether we're in
839: * the correct section or not.
840: */
841:
842: if (MAN_BODY == n->type && MAN_SH == n->tok) {
843: body = n;
844: assert(body->parent);
845: if (NULL != (head = body->parent->head) &&
846: 1 == head->nchild &&
847: NULL != (head = (head->child)) &&
848: MAN_TEXT == head->type &&
849: 0 == strcmp(head->string, "NAME") &&
850: NULL != (body = body->child) &&
851: MAN_TEXT == body->type) {
852:
853: assert(body->string);
854: start = sv = body->string;
855:
856: /*
857: * Go through a special heuristic dance here.
858: * This is why -man manuals are great!
859: * (I'm being sarcastic: my eyes are bleeding.)
860: * Conventionally, one or more manual names are
861: * comma-specified prior to a whitespace, then a
862: * dash, then a description. Try to puzzle out
863: * the name parts here.
864: */
865:
866: for ( ;; ) {
867: sz = strcspn(start, " ,");
868: if ('\0' == start[(int)sz])
869: break;
870:
1.3 kristaps 871: buf->len = 0;
872: buf_appendb(buf, start, sz);
873: buf_appendb(buf, "", 1);
1.1 kristaps 874:
1.3 kristaps 875: hash_put(hash, buf, TYPE_NAME);
1.1 kristaps 876:
877: if (' ' == start[(int)sz]) {
878: start += (int)sz + 1;
879: break;
880: }
881:
882: assert(',' == start[(int)sz]);
883: start += (int)sz + 1;
884: while (' ' == *start)
885: start++;
886: }
887:
888: if (sv == start) {
1.3 kristaps 889: buf->len = 0;
890: buf_append(buf, start);
1.1 kristaps 891: return(1);
892: }
893:
894: while (' ' == *start)
895: start++;
896:
897: if (0 == strncmp(start, "-", 1))
898: start += 1;
899: else if (0 == strncmp(start, "\\-", 2))
900: start += 2;
901: else if (0 == strncmp(start, "\\(en", 4))
902: start += 4;
903: else if (0 == strncmp(start, "\\(em", 4))
904: start += 4;
905:
906: while (' ' == *start)
907: start++;
908:
1.6 kristaps 909: sz = strlen(start) + 1;
910: buf_appendb(dbuf, start, sz);
911: buf_appendb(buf, start, sz);
1.1 kristaps 912: }
913: }
914:
1.5 kristaps 915: if (pman_node(hash, buf, dbuf, n->child))
1.1 kristaps 916: return(1);
1.5 kristaps 917: if (pman_node(hash, buf, dbuf, n->next))
1.1 kristaps 918: return(1);
919:
920: return(0);
921: }
922:
923: static void
924: usage(void)
925: {
926:
1.5 kristaps 927: fprintf(stderr, "usage: %s [-d path] [file...]\n", progname);
1.1 kristaps 928: }
CVSweb