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