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