Annotation of mandoc/makewhatis.c, Revision 1.21
1.21 ! kristaps 1: /* $Id: makewhatis.c,v 1.20 2011/07/12 10:03: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:
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
1.21 ! kristaps 45: #define MANDOC_SLOP 1024
1.1 kristaps 46:
1.5 kristaps 47: /* Bit-fields. See makewhatis.1. */
48:
1.10 kristaps 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
1.11 kristaps 58: #define TYPE_XREF 0x200
1.12 kristaps 59: #define TYPE_PATH 0x400
1.15 kristaps 60: #define TYPE_ENV 0x800
1.16 kristaps 61: #define TYPE_ERR 0x1000
1.3 kristaps 62:
1.5 kristaps 63: /* Buffer for storing growable data. */
64:
1.3 kristaps 65: struct buf {
66: char *cp;
67: size_t len;
68: size_t size;
1.1 kristaps 69: };
70:
1.19 kristaps 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:
1.3 kristaps 79: #define MAN_ARGS DB *hash, \
80: struct buf *buf, \
1.5 kristaps 81: struct buf *dbuf, \
1.1 kristaps 82: const struct man_node *n
1.3 kristaps 83: #define MDOC_ARGS DB *hash, \
84: struct buf *buf, \
1.5 kristaps 85: struct buf *dbuf, \
1.3 kristaps 86: const struct mdoc_node *n, \
87: const struct mdoc_meta *m
1.1 kristaps 88:
1.13 kristaps 89: static void buf_appendmdoc(struct buf *,
1.14 kristaps 90: const struct mdoc_node *, int);
1.5 kristaps 91: static void buf_append(struct buf *, const char *);
92: static void buf_appendb(struct buf *,
1.1 kristaps 93: const void *, size_t);
94: static void dbt_put(DB *, const char *, DBT *, DBT *);
1.3 kristaps 95: static void hash_put(DB *, const struct buf *, int);
1.18 kristaps 96: static void hash_reset(DB **);
1.1 kristaps 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);
1.16 kristaps 101: static void pmdoc_Er(MDOC_ARGS);
1.15 kristaps 102: static void pmdoc_Ev(MDOC_ARGS);
1.1 kristaps 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);
1.12 kristaps 109: static void pmdoc_Pa(MDOC_ARGS);
1.1 kristaps 110: static void pmdoc_St(MDOC_ARGS);
111: static void pmdoc_Vt(MDOC_ARGS);
1.11 kristaps 112: static void pmdoc_Xr(MDOC_ARGS);
1.5 kristaps 113: static void usage(void);
1.1 kristaps 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 */
1.16 kristaps 138: pmdoc_Er, /* Er */
1.15 kristaps 139: pmdoc_Ev, /* Ev */
1.1 kristaps 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 */
1.12 kristaps 153: pmdoc_Pa, /* Pa */
1.1 kristaps 154: NULL, /* Rv */
155: pmdoc_St, /* St */
156: pmdoc_Vt, /* Va */
157: pmdoc_Vt, /* Vt */
1.11 kristaps 158: pmdoc_Xr, /* Xr */
1.1 kristaps 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:
1.5 kristaps 242: static const char *progname;
243:
1.1 kristaps 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 */
1.21 ! kristaps 250: enum op op; /* current operation */
1.1 kristaps 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 */
1.3 kristaps 258: vbuf[8]; /* stringified record number */
1.21 ! kristaps 259: int ch, seq, sseq, verb, i;
1.1 kristaps 260: DB *idx, /* index database */
1.3 kristaps 261: *db, /* keyword database */
262: *hash; /* temporary keyword hashtable */
1.5 kristaps 263: DBT key, val;
1.21 ! kristaps 264: enum mandoclevel ec; /* exit status */
1.9 kristaps 265: size_t sv;
1.1 kristaps 266: BTREEINFO info; /* btree configuration */
1.21 ! kristaps 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 */
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;
1.21 ! kristaps 289: recsz = reccur = 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
1.21 ! kristaps 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.
1.19 kristaps 367: */
368:
1.21 ! kristaps 369: if (OP_DELETE == op || OP_UPDATE == op) {
1.19 kristaps 370: seq = R_FIRST;
371: while (0 == (ch = (*idx->seq)(idx, &key, &val, seq))) {
372: seq = R_NEXT;
373: maxrec = *(recno_t *)key.data;
1.21 ! kristaps 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)
1.19 kristaps 391: continue;
1.21 ! kristaps 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++;
1.19 kristaps 429: }
430: }
431: maxrec++;
1.21 ! kristaps 432: }
! 433:
! 434: if (OP_DELETE == op) {
! 435: ec = MANDOCLEVEL_OK;
! 436: goto out;
! 437: }
1.19 kristaps 438:
439: /*
440: * Add records to the database.
1.10 kristaps 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.
1.1 kristaps 444: * Use the auto-parser and don't report any errors.
445: */
446:
447: mp = mparse_alloc(MPARSE_AUTO, MANDOCLEVEL_FATAL, NULL, NULL);
448:
1.5 kristaps 449: buf.size = dbuf.size = MANDOC_BUFSZ;
1.3 kristaps 450: buf.cp = mandoc_malloc(buf.size);
1.5 kristaps 451: dbuf.cp = mandoc_malloc(dbuf.size);
1.1 kristaps 452:
1.19 kristaps 453: for (rec = 0, i = 0; i < argc; i++) {
454: fn = argv[i];
455: if (OP_UPDATE == op) {
1.21 ! kristaps 456: if (reccur > 0) {
! 457: --reccur;
! 458: rec = recs[(int)reccur];
1.19 kristaps 459: } else if (maxrec > 0) {
460: rec = maxrec;
461: maxrec = 0;
462: } else
463: rec++;
464: } else
465: rec++;
1.18 kristaps 466:
1.1 kristaps 467: mparse_reset(mp);
1.18 kristaps 468: hash_reset(&hash);
1.1 kristaps 469:
470: if (mparse_readfd(mp, -1, fn) >= MANDOCLEVEL_FATAL) {
471: fprintf(stderr, "%s: Parse failure\n", fn);
472: continue;
473: }
1.10 kristaps 474:
1.1 kristaps 475: mparse_result(mp, &mdoc, &man);
476: if (NULL == mdoc && NULL == man)
477: continue;
478:
479: msec = NULL != mdoc ?
1.18 kristaps 480: mdoc_meta(mdoc)->msec : man_meta(man)->msec;
1.1 kristaps 481: mtitle = NULL != mdoc ?
1.18 kristaps 482: mdoc_meta(mdoc)->title : man_meta(man)->title;
1.19 kristaps 483: arch = NULL != mdoc ? mdoc_meta(mdoc)->arch : NULL;
484:
485: if (NULL == arch)
486: arch = "";
1.1 kristaps 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:
1.5 kristaps 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);
1.18 kristaps 500: buf_appendb(&dbuf, arch, strlen(arch) + 1);
1.1 kristaps 501:
1.5 kristaps 502: sv = dbuf.len;
1.1 kristaps 503:
504: /* Fix the record number in the btree value. */
505:
506: if (mdoc)
1.5 kristaps 507: pmdoc_node(hash, &buf, &dbuf,
508: mdoc_node(mdoc), mdoc_meta(mdoc));
1.1 kristaps 509: else
1.5 kristaps 510: pman_node(hash, &buf, &dbuf, man_node(man));
1.3 kristaps 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))) {
1.7 kristaps 522: seq = R_NEXT;
523:
1.3 kristaps 524: memcpy(vbuf, val.data, sizeof(uint32_t));
525: val.size = sizeof(vbuf);
526: val.data = vbuf;
1.7 kristaps 527:
1.10 kristaps 528: if (verb > 1)
1.21 ! kristaps 529: printf("%s: Added keyword: %s, 0x%x\n",
1.18 kristaps 530: fn, (char *)key.data,
1.8 kristaps 531: *(int *)val.data);
1.18 kristaps 532: dbt_put(db, fbuf, &key, &val);
1.3 kristaps 533: }
534: if (ch < 0) {
535: perror("hash");
536: exit((int)MANDOCLEVEL_SYSERR);
537: }
1.1 kristaps 538:
539: /*
1.3 kristaps 540: * Apply to the index. If we haven't had a description
541: * set, put an empty one in now.
1.1 kristaps 542: */
543:
1.5 kristaps 544: if (dbuf.len == sv)
545: buf_appendb(&dbuf, "", 1);
546:
547: key.data = &rec;
548: key.size = sizeof(recno_t);
1.1 kristaps 549:
1.5 kristaps 550: val.data = dbuf.cp;
551: val.size = dbuf.len;
1.1 kristaps 552:
1.10 kristaps 553: if (verb > 0)
1.21 ! kristaps 554: printf("%s: Added index\n", fn);
1.8 kristaps 555:
1.18 kristaps 556: dbt_put(idx, ibuf, &key, &val);
1.1 kristaps 557: }
558:
1.18 kristaps 559: ec = MANDOCLEVEL_OK;
560: out:
561: if (db)
562: (*db->close)(db);
563: if (idx)
564: (*idx->close)(idx);
1.8 kristaps 565: if (hash)
566: (*hash->close)(hash);
1.18 kristaps 567: if (mp)
568: mparse_free(mp);
1.1 kristaps 569:
1.3 kristaps 570: free(buf.cp);
1.5 kristaps 571: free(dbuf.cp);
1.19 kristaps 572: free(recs);
1.1 kristaps 573:
1.18 kristaps 574: return((int)ec);
1.19 kristaps 575: }
576:
1.1 kristaps 577: /*
1.5 kristaps 578: * Grow the buffer (if necessary) and copy in a binary string.
1.1 kristaps 579: */
580: static void
1.3 kristaps 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:
1.1 kristaps 595: /*
1.5 kristaps 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.
1.1 kristaps 600: */
601: static void
1.3 kristaps 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:
1.13 kristaps 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.
1.14 kristaps 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.
1.13 kristaps 623: */
624: static void
1.14 kristaps 625: buf_appendmdoc(struct buf *buf, const struct mdoc_node *n, int f)
1.13 kristaps 626: {
627:
628: for ( ; n; n = n->next) {
629: if (n->child)
1.14 kristaps 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)
1.13 kristaps 637: buf_append(buf, n->string);
1.14 kristaps 638:
1.13 kristaps 639: }
640: }
641:
1.1 kristaps 642: /* ARGSUSED */
643: static void
644: pmdoc_An(MDOC_ARGS)
645: {
646:
647: if (SEC_AUTHORS != n->sec)
648: return;
649:
1.14 kristaps 650: buf_appendmdoc(buf, n->child, 0);
1.3 kristaps 651: hash_put(hash, buf, TYPE_AUTHOR);
1.18 kristaps 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: }
1.1 kristaps 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:
1.3 kristaps 709: buf_appendb(buf, start, (size_t)(end - start + 1));
710: buf_appendb(buf, "", 1);
711:
712: hash_put(hash, buf, TYPE_INCLUDES);
1.1 kristaps 713: }
714:
715: /* ARGSUSED */
716: static void
717: pmdoc_Cd(MDOC_ARGS)
718: {
719:
720: if (SEC_SYNOPSIS != n->sec)
721: return;
722:
1.14 kristaps 723: buf_appendmdoc(buf, n->child, 0);
1.3 kristaps 724: hash_put(hash, buf, TYPE_CONFIG);
1.1 kristaps 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:
1.3 kristaps 737: buf_append(buf, n->child->string);
738: hash_put(hash, buf, TYPE_INCLUDES);
1.1 kristaps 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:
1.3 kristaps 763: buf_append(buf, cp);
764: hash_put(hash, buf, TYPE_FUNCTION);
1.1 kristaps 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:
1.3 kristaps 777: buf_append(buf, n->child->string);
778: hash_put(hash, buf, TYPE_STANDARD);
1.11 kristaps 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);
1.1 kristaps 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:
1.3 kristaps 832: buf_appendb(buf, start, sz);
833: buf_appendb(buf, "", 1);
834: hash_put(hash, buf, TYPE_VARIABLE);
1.1 kristaps 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:
1.3 kristaps 847: buf_append(buf, n->child->string);
848: hash_put(hash, buf, TYPE_FUNCTION);
1.1 kristaps 849: }
850:
851:
852: /* ARGSUSED */
853: static void
854: pmdoc_Nd(MDOC_ARGS)
855: {
1.6 kristaps 856:
1.13 kristaps 857: if (MDOC_BODY != n->type)
858: return;
1.6 kristaps 859:
1.14 kristaps 860: buf_appendmdoc(dbuf, n->child, 1);
861: buf_appendmdoc(buf, n->child, 0);
1.6 kristaps 862:
863: hash_put(hash, buf, TYPE_DESC);
1.16 kristaps 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);
1.15 kristaps 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);
1.12 kristaps 888: }
889:
890: /* ARGSUSED */
891: static void
892: pmdoc_Pa(MDOC_ARGS)
893: {
894:
895: if (SEC_FILES != n->sec)
896: return;
897:
1.14 kristaps 898: buf_appendmdoc(buf, n->child, 0);
1.12 kristaps 899: hash_put(hash, buf, TYPE_PATH);
1.1 kristaps 900: }
901:
902: /* ARGSUSED */
903: static void
904: pmdoc_Nm(MDOC_ARGS)
905: {
906:
907: if (SEC_NAME == n->sec) {
1.14 kristaps 908: buf_appendmdoc(buf, n->child, 0);
1.3 kristaps 909: hash_put(hash, buf, TYPE_NAME);
1.1 kristaps 910: return;
911: } else if (SEC_SYNOPSIS != n->sec || MDOC_HEAD != n->type)
912: return;
913:
1.3 kristaps 914: if (NULL == n->child)
915: buf_append(buf, m->name);
916:
1.14 kristaps 917: buf_appendmdoc(buf, n->child, 0);
1.3 kristaps 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:
1.7 kristaps 927: if (buf->len < 2)
928: return;
929:
1.3 kristaps 930: key.data = buf->cp;
1.7 kristaps 931: key.size = buf->len;
1.3 kristaps 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);
1.1 kristaps 941:
1.3 kristaps 942: if ((rc = (*db->put)(db, &key, &val, 0)) < 0) {
943: perror("hash");
944: exit((int)MANDOCLEVEL_SYSERR);
945: }
1.1 kristaps 946: }
947:
948: static void
949: dbt_put(DB *db, const char *dbn, DBT *key, DBT *val)
950: {
951:
1.5 kristaps 952: assert(key->size);
1.1 kristaps 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:
1.3 kristaps 987: buf->len = 0;
1.5 kristaps 988: (*mdocs[n->tok])(hash, buf, dbuf, n, m);
1.1 kristaps 989: break;
990: default:
991: break;
992: }
993:
1.5 kristaps 994: pmdoc_node(hash, buf, dbuf, n->child, m);
995: pmdoc_node(hash, buf, dbuf, n->next, m);
1.1 kristaps 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:
1.3 kristaps 1044: buf->len = 0;
1045: buf_appendb(buf, start, sz);
1046: buf_appendb(buf, "", 1);
1.1 kristaps 1047:
1.3 kristaps 1048: hash_put(hash, buf, TYPE_NAME);
1.1 kristaps 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:
1.17 kristaps 1061: buf->len = 0;
1062:
1.1 kristaps 1063: if (sv == start) {
1.3 kristaps 1064: buf_append(buf, start);
1.1 kristaps 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:
1.6 kristaps 1083: sz = strlen(start) + 1;
1084: buf_appendb(dbuf, start, sz);
1085: buf_appendb(buf, start, sz);
1.17 kristaps 1086:
1087: hash_put(hash, buf, TYPE_DESC);
1.1 kristaps 1088: }
1089: }
1090:
1.5 kristaps 1091: if (pman_node(hash, buf, dbuf, n->child))
1.1 kristaps 1092: return(1);
1.5 kristaps 1093: if (pman_node(hash, buf, dbuf, n->next))
1.1 kristaps 1094: return(1);
1095:
1096: return(0);
1097: }
1098:
1099: static void
1100: usage(void)
1101: {
1102:
1.19 kristaps 1103: fprintf(stderr, "usage: %s [-ruv] [-d path] [file...]\n",
1.10 kristaps 1104: progname);
1.1 kristaps 1105: }
CVSweb