]> git.cameronkatri.com Git - mandoc.git/blob - mandoc-db.c
Let mandoc-db grok `St' tokens.
[mandoc.git] / mandoc-db.c
1 /* $Id: mandoc-db.c,v 1.13 2011/04/28 10:44: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 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20
21 #include <sys/param.h>
22
23 #include <assert.h>
24 #ifdef __linux__
25 # include <db_185.h>
26 #else
27 # include <db.h>
28 #endif
29 #include <fcntl.h>
30 #include <getopt.h>
31 #include <stdio.h>
32 #include <stdint.h>
33 #include <stdlib.h>
34 #include <string.h>
35
36 #include "man.h"
37 #include "mdoc.h"
38 #include "mandoc.h"
39
40 #define MANDOC_DB "mandoc.db"
41 #define MANDOC_IDX "mandoc.index"
42 #define MANDOC_BUFSZ BUFSIZ
43 #define MANDOC_FLAGS O_CREAT|O_TRUNC|O_RDWR
44
45 enum type {
46 MANDOC_NONE = 0,
47 MANDOC_NAME,
48 MANDOC_FUNCTION,
49 MANDOC_UTILITY,
50 MANDOC_INCLUDES,
51 MANDOC_VARIABLE,
52 MANDOC_STANDARD
53 };
54
55 #define MAN_ARGS DB *db, \
56 const char *dbn, \
57 DBT *key, size_t *ksz, \
58 DBT *val, \
59 DBT *rval, size_t *rsz, \
60 const struct man_node *n
61 #define MDOC_ARGS DB *db, \
62 const char *dbn, \
63 DBT *key, size_t *ksz, \
64 DBT *val, \
65 DBT *rval, size_t *rsz, \
66 const struct mdoc_node *n
67
68 static void dbt_append(DBT *, size_t *, const char *);
69 static void dbt_appendb(DBT *, size_t *,
70 const void *, size_t);
71 static void dbt_init(DBT *, size_t *);
72 static void dbt_put(DB *, const char *, DBT *, DBT *);
73 static void usage(void);
74 static void pman(DB *, const char *, DBT *, size_t *,
75 DBT *, DBT *, size_t *, struct man *);
76 static int pman_node(MAN_ARGS);
77 static void pmdoc(DB *, const char *, DBT *, size_t *,
78 DBT *, DBT *, size_t *, struct mdoc *);
79 static void pmdoc_node(MDOC_ARGS);
80 static void pmdoc_Fd(MDOC_ARGS);
81 static void pmdoc_In(MDOC_ARGS);
82 static void pmdoc_Fn(MDOC_ARGS);
83 static void pmdoc_Fo(MDOC_ARGS);
84 static void pmdoc_Nd(MDOC_ARGS);
85 static void pmdoc_Nm(MDOC_ARGS);
86 static void pmdoc_St(MDOC_ARGS);
87 static void pmdoc_Vt(MDOC_ARGS);
88
89 typedef void (*pmdoc_nf)(MDOC_ARGS);
90
91 static const char *progname;
92
93 static const pmdoc_nf mdocs[MDOC_MAX] = {
94 NULL, /* Ap */
95 NULL, /* Dd */
96 NULL, /* Dt */
97 NULL, /* Os */
98 NULL, /* Sh */
99 NULL, /* Ss */
100 NULL, /* Pp */
101 NULL, /* D1 */
102 NULL, /* Dl */
103 NULL, /* Bd */
104 NULL, /* Ed */
105 NULL, /* Bl */
106 NULL, /* El */
107 NULL, /* It */
108 NULL, /* Ad */
109 NULL, /* An */
110 NULL, /* Ar */
111 NULL, /* Cd */
112 NULL, /* Cm */
113 NULL, /* Dv */
114 NULL, /* Er */
115 NULL, /* Ev */
116 NULL, /* Ex */
117 NULL, /* Fa */
118 pmdoc_Fd, /* Fd */
119 NULL, /* Fl */
120 pmdoc_Fn, /* Fn */
121 NULL, /* Ft */
122 NULL, /* Ic */
123 pmdoc_In, /* In */
124 NULL, /* Li */
125 pmdoc_Nd, /* Nd */
126 pmdoc_Nm, /* Nm */
127 NULL, /* Op */
128 NULL, /* Ot */
129 NULL, /* Pa */
130 NULL, /* Rv */
131 pmdoc_St, /* St */
132 pmdoc_Vt, /* Va */
133 pmdoc_Vt, /* Vt */
134 NULL, /* Xr */
135 NULL, /* %A */
136 NULL, /* %B */
137 NULL, /* %D */
138 NULL, /* %I */
139 NULL, /* %J */
140 NULL, /* %N */
141 NULL, /* %O */
142 NULL, /* %P */
143 NULL, /* %R */
144 NULL, /* %T */
145 NULL, /* %V */
146 NULL, /* Ac */
147 NULL, /* Ao */
148 NULL, /* Aq */
149 NULL, /* At */
150 NULL, /* Bc */
151 NULL, /* Bf */
152 NULL, /* Bo */
153 NULL, /* Bq */
154 NULL, /* Bsx */
155 NULL, /* Bx */
156 NULL, /* Db */
157 NULL, /* Dc */
158 NULL, /* Do */
159 NULL, /* Dq */
160 NULL, /* Ec */
161 NULL, /* Ef */
162 NULL, /* Em */
163 NULL, /* Eo */
164 NULL, /* Fx */
165 NULL, /* Ms */
166 NULL, /* No */
167 NULL, /* Ns */
168 NULL, /* Nx */
169 NULL, /* Ox */
170 NULL, /* Pc */
171 NULL, /* Pf */
172 NULL, /* Po */
173 NULL, /* Pq */
174 NULL, /* Qc */
175 NULL, /* Ql */
176 NULL, /* Qo */
177 NULL, /* Qq */
178 NULL, /* Re */
179 NULL, /* Rs */
180 NULL, /* Sc */
181 NULL, /* So */
182 NULL, /* Sq */
183 NULL, /* Sm */
184 NULL, /* Sx */
185 NULL, /* Sy */
186 NULL, /* Tn */
187 NULL, /* Ux */
188 NULL, /* Xc */
189 NULL, /* Xo */
190 pmdoc_Fo, /* Fo */
191 NULL, /* Fc */
192 NULL, /* Oo */
193 NULL, /* Oc */
194 NULL, /* Bk */
195 NULL, /* Ek */
196 NULL, /* Bt */
197 NULL, /* Hf */
198 NULL, /* Fr */
199 NULL, /* Ud */
200 NULL, /* Lb */
201 NULL, /* Lp */
202 NULL, /* Lk */
203 NULL, /* Mt */
204 NULL, /* Brq */
205 NULL, /* Bro */
206 NULL, /* Brc */
207 NULL, /* %C */
208 NULL, /* Es */
209 NULL, /* En */
210 NULL, /* Dx */
211 NULL, /* %Q */
212 NULL, /* br */
213 NULL, /* sp */
214 NULL, /* %U */
215 NULL, /* Ta */
216 };
217
218 int
219 main(int argc, char *argv[])
220 {
221 struct mparse *mp; /* parse sequence */
222 struct mdoc *mdoc; /* resulting mdoc */
223 struct man *man; /* resulting man */
224 char *fn; /* current file being parsed */
225 const char *msec, /* manual section */
226 *dir; /* result dir (default: cwd) */
227 char ibuf[MAXPATHLEN], /* index fname */
228 ibbuf[MAXPATHLEN], /* index backup fname */
229 fbuf[MAXPATHLEN], /* btree fname */
230 fbbuf[MAXPATHLEN]; /* btree backup fname */
231 int ch;
232 DB *idx, /* index database */
233 *db; /* keyword database */
234 DBT rkey, rval, /* recno entries */
235 key, val; /* persistent keyword entries */
236 size_t sv,
237 ksz, rsz; /* entry buffer size */
238 char vbuf[8]; /* stringified record number */
239 BTREEINFO info; /* btree configuration */
240 recno_t rec; /* current record number */
241 extern int optind;
242 extern char *optarg;
243
244 progname = strrchr(argv[0], '/');
245 if (progname == NULL)
246 progname = argv[0];
247 else
248 ++progname;
249
250 dir = "";
251
252 while (-1 != (ch = getopt(argc, argv, "d:")))
253 switch (ch) {
254 case ('d'):
255 dir = optarg;
256 break;
257 default:
258 usage();
259 return((int)MANDOCLEVEL_BADARG);
260 }
261
262 argc -= optind;
263 argv += optind;
264
265 /*
266 * Set up temporary file-names into which we're going to write
267 * all of our data (both for the index and database). These
268 * will be securely renamed to the real file-names after we've
269 * written all of our data.
270 */
271
272 ibuf[0] = ibuf[MAXPATHLEN - 2] =
273 ibbuf[0] = ibbuf[MAXPATHLEN - 2] =
274 fbuf[0] = fbuf[MAXPATHLEN - 2] =
275 fbbuf[0] = fbbuf[MAXPATHLEN - 2] = '\0';
276
277 strlcat(fbuf, dir, MAXPATHLEN);
278 strlcat(fbuf, MANDOC_DB, MAXPATHLEN);
279
280 strlcat(fbbuf, fbuf, MAXPATHLEN);
281 strlcat(fbbuf, "~", MAXPATHLEN);
282
283 strlcat(ibuf, dir, MAXPATHLEN);
284 strlcat(ibuf, MANDOC_IDX, MAXPATHLEN);
285
286 strlcat(ibbuf, ibuf, MAXPATHLEN);
287 strlcat(ibbuf, "~", MAXPATHLEN);
288
289 if ('\0' != fbuf[MAXPATHLEN - 2] ||
290 '\0' != fbbuf[MAXPATHLEN - 2] ||
291 '\0' != ibuf[MAXPATHLEN - 2] ||
292 '\0' != ibbuf[MAXPATHLEN - 2]) {
293 fprintf(stderr, "%s: Path too long\n", progname);
294 exit((int)MANDOCLEVEL_SYSERR);
295 }
296
297 /*
298 * For the keyword database, open a BTREE database that allows
299 * duplicates. For the index database, use a standard RECNO
300 * database type.
301 */
302
303 memset(&info, 0, sizeof(BTREEINFO));
304 info.flags = R_DUP;
305 db = dbopen(fbbuf, MANDOC_FLAGS, 0644, DB_BTREE, &info);
306
307 if (NULL == db) {
308 perror(fbbuf);
309 exit((int)MANDOCLEVEL_SYSERR);
310 }
311
312 idx = dbopen(ibbuf, MANDOC_FLAGS, 0644, DB_RECNO, NULL);
313
314 if (NULL == db) {
315 perror(ibbuf);
316 (*db->close)(db);
317 exit((int)MANDOCLEVEL_SYSERR);
318 }
319
320 /*
321 * Try parsing the manuals given on the command line. If we
322 * totally fail, then just keep on going. Take resulting trees
323 * and push them down into the database code.
324 * Use the auto-parser and don't report any errors.
325 */
326
327 mp = mparse_alloc(MPARSE_AUTO, MANDOCLEVEL_FATAL, NULL, NULL);
328
329 memset(&key, 0, sizeof(DBT));
330 memset(&val, 0, sizeof(DBT));
331 memset(&rkey, 0, sizeof(DBT));
332 memset(&rval, 0, sizeof(DBT));
333
334 val.size = sizeof(vbuf);
335 val.data = vbuf;
336 rkey.size = sizeof(recno_t);
337
338 rec = 1;
339 ksz = rsz = 0;
340
341 while (NULL != (fn = *argv++)) {
342 mparse_reset(mp);
343
344 /* Parse and get (non-empty) AST. */
345
346 if (mparse_readfd(mp, -1, fn) >= MANDOCLEVEL_FATAL) {
347 fprintf(stderr, "%s: Parse failure\n", fn);
348 continue;
349 }
350 mparse_result(mp, &mdoc, &man);
351 if (NULL == mdoc && NULL == man)
352 continue;
353
354 /* Manual section: can be empty string. */
355
356 msec = NULL != mdoc ?
357 mdoc_meta(mdoc)->msec :
358 man_meta(man)->msec;
359
360 assert(msec);
361
362 /*
363 * The index record value consists of a nil-terminated
364 * filename, a nil-terminated manual section, and a
365 * nil-terminated description. Since the description
366 * may not be set, we set a sentinel to see if we're
367 * going to write a nil byte in its place.
368 */
369
370 dbt_init(&rval, &rsz);
371 dbt_appendb(&rval, &rsz, fn, strlen(fn) + 1);
372 dbt_appendb(&rval, &rsz, msec, strlen(msec) + 1);
373 sv = rval.size;
374
375 /* Fix the record number in the btree value. */
376
377 memset(val.data, 0, sizeof(uint32_t));
378 memcpy(val.data + 4, &rec, sizeof(uint32_t));
379
380 if (mdoc)
381 pmdoc(db, fbbuf, &key, &ksz,
382 &val, &rval, &rsz, mdoc);
383 else
384 pman(db, fbbuf, &key, &ksz,
385 &val, &rval, &rsz, man);
386
387 /*
388 * Apply this to the index. If we haven't had a
389 * description set, put an empty one in now.
390 */
391
392 if (rval.size == sv)
393 dbt_appendb(&rval, &rsz, "", 1);
394
395 rkey.data = &rec;
396 dbt_put(idx, ibbuf, &rkey, &rval);
397
398 printf("Indexed: %s\n", fn);
399 rec++;
400 }
401
402 (*db->close)(db);
403 (*idx->close)(idx);
404
405 mparse_free(mp);
406
407 free(key.data);
408 free(rval.data);
409
410 /* Atomically replace the file with our temporary one. */
411
412 if (-1 == rename(fbbuf, fbuf))
413 perror(fbuf);
414 if (-1 == rename(ibbuf, ibuf))
415 perror(fbuf);
416
417 return((int)MANDOCLEVEL_OK);
418 }
419
420 /*
421 * Initialise the stored database key whose data buffer is shared
422 * between uses (as the key must sometimes be constructed from an array
423 * of
424 */
425 static void
426 dbt_init(DBT *key, size_t *ksz)
427 {
428
429 if (0 == *ksz) {
430 assert(0 == key->size);
431 assert(NULL == key->data);
432 key->data = mandoc_malloc(MANDOC_BUFSZ);
433 *ksz = MANDOC_BUFSZ;
434 }
435
436 key->size = 0;
437 }
438
439 /*
440 * Append a binary value to a database entry. This can be invoked
441 * multiple times; the buffer is automatically resized.
442 */
443 static void
444 dbt_appendb(DBT *key, size_t *ksz, const void *cp, size_t sz)
445 {
446
447 assert(key->data);
448
449 /* Overshoot by MANDOC_BUFSZ. */
450
451 while (key->size + sz >= *ksz) {
452 *ksz = key->size + sz + MANDOC_BUFSZ;
453 key->data = mandoc_realloc(key->data, *ksz);
454 }
455
456 memcpy(key->data + (int)key->size, cp, sz);
457 key->size += sz;
458 }
459
460 /*
461 * Append a nil-terminated string to the database entry. This can be
462 * invoked multiple times. The database entry will be nil-terminated as
463 * well; if invoked multiple times, a space is put between strings.
464 */
465 static void
466 dbt_append(DBT *key, size_t *ksz, const char *cp)
467 {
468 size_t sz;
469
470 if (0 == (sz = strlen(cp)))
471 return;
472
473 assert(key->data);
474
475 if (key->size)
476 ((char *)key->data)[(int)key->size - 1] = ' ';
477
478 dbt_appendb(key, ksz, cp, sz + 1);
479 }
480
481 /* ARGSUSED */
482 static void
483 pmdoc_Fd(MDOC_ARGS)
484 {
485 uint32_t fl;
486 const char *start, *end;
487 size_t sz;
488 const char nil = '\0';
489
490 if (SEC_SYNOPSIS != n->sec)
491 return;
492 if (NULL == (n = n->child) || MDOC_TEXT != n->type)
493 return;
494
495 /*
496 * Only consider those `Fd' macro fields that begin with an
497 * "inclusion" token (versus, e.g., #define).
498 */
499 if (strcmp("#include", n->string))
500 return;
501
502 if (NULL == (n = n->next) || MDOC_TEXT != n->type)
503 return;
504
505 /*
506 * Strip away the enclosing angle brackets and make sure we're
507 * not zero-length.
508 */
509
510 start = n->string;
511 if ('<' == *start || '"' == *start)
512 start++;
513
514 if (0 == (sz = strlen(start)))
515 return;
516
517 end = &start[(int)sz - 1];
518 if ('>' == *end || '"' == *end)
519 end--;
520
521 dbt_appendb(key, ksz, start, end - start + 1);
522 dbt_appendb(key, ksz, &nil, 1);
523
524 fl = MANDOC_INCLUDES;
525 memcpy(val->data, &fl, 4);
526 }
527
528 /* ARGSUSED */
529 static void
530 pmdoc_In(MDOC_ARGS)
531 {
532 uint32_t fl;
533
534 if (SEC_SYNOPSIS != n->sec)
535 return;
536 if (NULL == n->child || MDOC_TEXT != n->child->type)
537 return;
538
539 dbt_append(key, ksz, n->child->string);
540 fl = MANDOC_INCLUDES;
541 memcpy(val->data, &fl, 4);
542 }
543
544 /* ARGSUSED */
545 static void
546 pmdoc_Fn(MDOC_ARGS)
547 {
548 uint32_t fl;
549 const char *cp;
550
551 if (SEC_SYNOPSIS != n->sec)
552 return;
553 if (NULL == n->child || MDOC_TEXT != n->child->type)
554 return;
555
556 /* .Fn "struct type *arg" "foo" */
557
558 cp = strrchr(n->child->string, ' ');
559 if (NULL == cp)
560 cp = n->child->string;
561
562 /* Strip away pointer symbol. */
563
564 while ('*' == *cp)
565 cp++;
566
567 dbt_append(key, ksz, cp);
568 fl = MANDOC_FUNCTION;
569 memcpy(val->data, &fl, 4);
570 }
571
572 /* ARGSUSED */
573 static void
574 pmdoc_St(MDOC_ARGS)
575 {
576 uint32_t fl;
577
578 if (SEC_STANDARDS != n->sec)
579 return;
580 if (NULL == n->child || MDOC_TEXT != n->child->type)
581 return;
582
583 dbt_append(key, ksz, n->child->string);
584 fl = MANDOC_STANDARD;
585 memcpy(val->data, &fl, 4);
586 }
587
588 /* ARGSUSED */
589 static void
590 pmdoc_Vt(MDOC_ARGS)
591 {
592 uint32_t fl;
593 const char *start, *end;
594 size_t sz;
595 const char nil = '\0';
596
597 if (SEC_SYNOPSIS != n->sec)
598 return;
599 if (MDOC_Vt == n->tok && MDOC_BODY != n->type)
600 return;
601 if (NULL == n->last || MDOC_TEXT != n->last->type)
602 return;
603
604 /*
605 * Strip away leading pointer symbol '*' and trailing ';'.
606 */
607
608 start = n->last->string;
609
610 while ('*' == *start)
611 start++;
612
613 if (0 == (sz = strlen(start)))
614 return;
615
616 end = &start[sz - 1];
617 while (end > start && ';' == *end)
618 end--;
619
620 if (end == start)
621 return;
622
623 dbt_appendb(key, ksz, start, end - start + 1);
624 dbt_appendb(key, ksz, &nil, 1);
625 fl = MANDOC_VARIABLE;
626 memcpy(val->data, &fl, 4);
627 }
628
629 /* ARGSUSED */
630 static void
631 pmdoc_Fo(MDOC_ARGS)
632 {
633 uint32_t fl;
634
635 if (SEC_SYNOPSIS != n->sec || MDOC_HEAD != n->type)
636 return;
637 if (NULL == n->child || MDOC_TEXT != n->child->type)
638 return;
639
640 dbt_append(key, ksz, n->child->string);
641 fl = MANDOC_FUNCTION;
642 memcpy(val->data, &fl, 4);
643 }
644
645
646 /* ARGSUSED */
647 static void
648 pmdoc_Nd(MDOC_ARGS)
649 {
650 int first;
651
652 for (first = 1, n = n->child; n; n = n->next) {
653 if (MDOC_TEXT != n->type)
654 continue;
655 if (first)
656 dbt_appendb(rval, rsz, n->string, strlen(n->string) + 1);
657 else
658 dbt_append(rval, rsz, n->string);
659
660 first = 0;
661 }
662 }
663
664 /* ARGSUSED */
665 static void
666 pmdoc_Nm(MDOC_ARGS)
667 {
668 uint32_t fl;
669
670 if (SEC_NAME == n->sec) {
671 for (n = n->child; n; n = n->next) {
672 if (MDOC_TEXT != n->type)
673 continue;
674 dbt_append(key, ksz, n->string);
675 }
676 fl = MANDOC_NAME;
677 memcpy(val->data, &fl, 4);
678 return;
679 } else if (SEC_SYNOPSIS != n->sec || MDOC_HEAD != n->type)
680 return;
681
682 for (n = n->child; n; n = n->next) {
683 if (MDOC_TEXT != n->type)
684 continue;
685 dbt_append(key, ksz, n->string);
686 }
687
688 fl = MANDOC_UTILITY;
689 memcpy(val->data, &fl, 4);
690 }
691
692 static void
693 dbt_put(DB *db, const char *dbn, DBT *key, DBT *val)
694 {
695
696 if (0 == key->size)
697 return;
698
699 assert(key->data);
700 assert(val->size);
701 assert(val->data);
702
703 if (0 == (*db->put)(db, key, val, 0))
704 return;
705
706 perror(dbn);
707 exit((int)MANDOCLEVEL_SYSERR);
708 /* NOTREACHED */
709 }
710
711 /*
712 * Call out to per-macro handlers after clearing the persistent database
713 * key. If the macro sets the database key, flush it to the database.
714 */
715 static void
716 pmdoc_node(MDOC_ARGS)
717 {
718
719 if (NULL == n)
720 return;
721
722 switch (n->type) {
723 case (MDOC_HEAD):
724 /* FALLTHROUGH */
725 case (MDOC_BODY):
726 /* FALLTHROUGH */
727 case (MDOC_TAIL):
728 /* FALLTHROUGH */
729 case (MDOC_BLOCK):
730 /* FALLTHROUGH */
731 case (MDOC_ELEM):
732 if (NULL == mdocs[n->tok])
733 break;
734
735 dbt_init(key, ksz);
736 (*mdocs[n->tok])(db, dbn, key, ksz, val, rval, rsz, n);
737
738 dbt_put(db, dbn, key, val);
739 break;
740 default:
741 break;
742 }
743
744 pmdoc_node(db, dbn, key, ksz, val, rval, rsz, n->child);
745 pmdoc_node(db, dbn, key, ksz, val, rval, rsz, n->next);
746 }
747
748 static int
749 pman_node(MAN_ARGS)
750 {
751 const struct man_node *head, *body;
752 const char *start, *sv;
753 const char nil = '\0';
754 size_t sz;
755 uint32_t fl;
756
757 if (NULL == n)
758 return(0);
759
760 /*
761 * We're only searching for one thing: the first text child in
762 * the BODY of a NAME section. Since we don't keep track of
763 * sections in -man, run some hoops to find out whether we're in
764 * the correct section or not.
765 */
766
767 if (MAN_BODY == n->type && MAN_SH == n->tok) {
768 body = n;
769 assert(body->parent);
770 if (NULL != (head = body->parent->head) &&
771 1 == head->nchild &&
772 NULL != (head = (head->child)) &&
773 MAN_TEXT == head->type &&
774 0 == strcmp(head->string, "NAME") &&
775 NULL != (body = body->child) &&
776 MAN_TEXT == body->type) {
777
778 fl = MANDOC_NAME;
779 memcpy(val->data, &fl, 4);
780
781 assert(body->string);
782 start = sv = body->string;
783
784 /*
785 * Go through a special heuristic dance here.
786 * This is why -man manuals are great!
787 * Conventionally, one or more manual names are
788 * comma-specified prior to a whitespace, then a
789 * dash, then a description. Try to puzzle out
790 * the name parts here.
791 */
792
793 for ( ;; ) {
794 sz = strcspn(start, " ,");
795 if ('\0' == start[(int)sz])
796 break;
797
798 dbt_init(key, ksz);
799 dbt_appendb(key, ksz, start, sz);
800 dbt_appendb(key, ksz, &nil, 1);
801
802 dbt_put(db, dbn, key, val);
803
804 if (' ' == start[(int)sz]) {
805 start += (int)sz + 1;
806 break;
807 }
808
809 assert(',' == start[(int)sz]);
810 start += (int)sz + 1;
811 while (' ' == *start)
812 start++;
813 }
814
815 if (sv == start) {
816 dbt_init(key, ksz);
817 dbt_append(key, ksz, start);
818 return(1);
819 }
820
821 while (' ' == *start)
822 start++;
823
824 if ('\\' == *start && '-' == *(start + 1))
825 start += 2;
826 else if ('-' == *start)
827 start++;
828
829 while (' ' == *start)
830 start++;
831
832 dbt_appendb(rval, rsz, start, strlen(start) + 1);
833 }
834 }
835
836 if (pman_node(db, dbn, key, ksz, val, rval, rsz, n->child))
837 return(1);
838 if (pman_node(db, dbn, key, ksz, val, rval, rsz, n->next))
839 return(1);
840
841 return(0);
842 }
843
844 static void
845 pman(DB *db, const char *dbn, DBT *key, size_t *ksz,
846 DBT *val, DBT *rval, size_t *rsz, struct man *m)
847 {
848
849 pman_node(db, dbn, key, ksz, val, rval, rsz, man_node(m));
850 }
851
852
853 static void
854 pmdoc(DB *db, const char *dbn, DBT *key, size_t *ksz,
855 DBT *val, DBT *rval, size_t *rsz, struct mdoc *m)
856 {
857
858 pmdoc_node(db, dbn, key, ksz, val, rval, rsz, mdoc_node(m));
859 }
860
861 static void
862 usage(void)
863 {
864
865 fprintf(stderr, "usage: %s "
866 "[-d path] "
867 "[file...]\n",
868 progname);
869 }