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