]>
git.cameronkatri.com Git - mandoc.git/blob - mandoc-db.c
1 /* $Id: mandoc-db.c,v 1.13 2011/04/28 10:44:02 kristaps Exp $ */
3 * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
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.
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.
21 #include <sys/param.h>
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
55 #define MAN_ARGS DB *db, \
57 DBT *key, size_t *ksz, \
59 DBT *rval, size_t *rsz, \
60 const struct man_node *n
61 #define MDOC_ARGS DB *db, \
63 DBT *key, size_t *ksz, \
65 DBT *rval, size_t *rsz, \
66 const struct mdoc_node *n
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
);
89 typedef void (*pmdoc_nf
)(MDOC_ARGS
);
91 static const char *progname
;
93 static const pmdoc_nf mdocs
[MDOC_MAX
] = {
219 main(int argc
, char *argv
[])
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 */
232 DB
*idx
, /* index database */
233 *db
; /* keyword database */
234 DBT rkey
, rval
, /* recno entries */
235 key
, val
; /* persistent keyword entries */
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 */
244 progname
= strrchr(argv
[0], '/');
245 if (progname
== NULL
)
252 while (-1 != (ch
= getopt(argc
, argv
, "d:")))
259 return((int)MANDOCLEVEL_BADARG
);
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.
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';
277 strlcat(fbuf
, dir
, MAXPATHLEN
);
278 strlcat(fbuf
, MANDOC_DB
, MAXPATHLEN
);
280 strlcat(fbbuf
, fbuf
, MAXPATHLEN
);
281 strlcat(fbbuf
, "~", MAXPATHLEN
);
283 strlcat(ibuf
, dir
, MAXPATHLEN
);
284 strlcat(ibuf
, MANDOC_IDX
, MAXPATHLEN
);
286 strlcat(ibbuf
, ibuf
, MAXPATHLEN
);
287 strlcat(ibbuf
, "~", MAXPATHLEN
);
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
);
298 * For the keyword database, open a BTREE database that allows
299 * duplicates. For the index database, use a standard RECNO
303 memset(&info
, 0, sizeof(BTREEINFO
));
305 db
= dbopen(fbbuf
, MANDOC_FLAGS
, 0644, DB_BTREE
, &info
);
309 exit((int)MANDOCLEVEL_SYSERR
);
312 idx
= dbopen(ibbuf
, MANDOC_FLAGS
, 0644, DB_RECNO
, NULL
);
317 exit((int)MANDOCLEVEL_SYSERR
);
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.
327 mp
= mparse_alloc(MPARSE_AUTO
, MANDOCLEVEL_FATAL
, NULL
, NULL
);
329 memset(&key
, 0, sizeof(DBT
));
330 memset(&val
, 0, sizeof(DBT
));
331 memset(&rkey
, 0, sizeof(DBT
));
332 memset(&rval
, 0, sizeof(DBT
));
334 val
.size
= sizeof(vbuf
);
336 rkey
.size
= sizeof(recno_t
);
341 while (NULL
!= (fn
= *argv
++)) {
344 /* Parse and get (non-empty) AST. */
346 if (mparse_readfd(mp
, -1, fn
) >= MANDOCLEVEL_FATAL
) {
347 fprintf(stderr
, "%s: Parse failure\n", fn
);
350 mparse_result(mp
, &mdoc
, &man
);
351 if (NULL
== mdoc
&& NULL
== man
)
354 /* Manual section: can be empty string. */
356 msec
= NULL
!= mdoc
?
357 mdoc_meta(mdoc
)->msec
:
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.
370 dbt_init(&rval
, &rsz
);
371 dbt_appendb(&rval
, &rsz
, fn
, strlen(fn
) + 1);
372 dbt_appendb(&rval
, &rsz
, msec
, strlen(msec
) + 1);
375 /* Fix the record number in the btree value. */
377 memset(val
.data
, 0, sizeof(uint32_t));
378 memcpy(val
.data
+ 4, &rec
, sizeof(uint32_t));
381 pmdoc(db
, fbbuf
, &key
, &ksz
,
382 &val
, &rval
, &rsz
, mdoc
);
384 pman(db
, fbbuf
, &key
, &ksz
,
385 &val
, &rval
, &rsz
, man
);
388 * Apply this to the index. If we haven't had a
389 * description set, put an empty one in now.
393 dbt_appendb(&rval
, &rsz
, "", 1);
396 dbt_put(idx
, ibbuf
, &rkey
, &rval
);
398 printf("Indexed: %s\n", fn
);
410 /* Atomically replace the file with our temporary one. */
412 if (-1 == rename(fbbuf
, fbuf
))
414 if (-1 == rename(ibbuf
, ibuf
))
417 return((int)MANDOCLEVEL_OK
);
421 * Initialise the stored database key whose data buffer is shared
422 * between uses (as the key must sometimes be constructed from an array
426 dbt_init(DBT
*key
, size_t *ksz
)
430 assert(0 == key
->size
);
431 assert(NULL
== key
->data
);
432 key
->data
= mandoc_malloc(MANDOC_BUFSZ
);
440 * Append a binary value to a database entry. This can be invoked
441 * multiple times; the buffer is automatically resized.
444 dbt_appendb(DBT
*key
, size_t *ksz
, const void *cp
, size_t sz
)
449 /* Overshoot by MANDOC_BUFSZ. */
451 while (key
->size
+ sz
>= *ksz
) {
452 *ksz
= key
->size
+ sz
+ MANDOC_BUFSZ
;
453 key
->data
= mandoc_realloc(key
->data
, *ksz
);
456 memcpy(key
->data
+ (int)key
->size
, cp
, sz
);
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.
466 dbt_append(DBT
*key
, size_t *ksz
, const char *cp
)
470 if (0 == (sz
= strlen(cp
)))
476 ((char *)key
->data
)[(int)key
->size
- 1] = ' ';
478 dbt_appendb(key
, ksz
, cp
, sz
+ 1);
486 const char *start
, *end
;
488 const char nil
= '\0';
490 if (SEC_SYNOPSIS
!= n
->sec
)
492 if (NULL
== (n
= n
->child
) || MDOC_TEXT
!= n
->type
)
496 * Only consider those `Fd' macro fields that begin with an
497 * "inclusion" token (versus, e.g., #define).
499 if (strcmp("#include", n
->string
))
502 if (NULL
== (n
= n
->next
) || MDOC_TEXT
!= n
->type
)
506 * Strip away the enclosing angle brackets and make sure we're
511 if ('<' == *start
|| '"' == *start
)
514 if (0 == (sz
= strlen(start
)))
517 end
= &start
[(int)sz
- 1];
518 if ('>' == *end
|| '"' == *end
)
521 dbt_appendb(key
, ksz
, start
, end
- start
+ 1);
522 dbt_appendb(key
, ksz
, &nil
, 1);
524 fl
= MANDOC_INCLUDES
;
525 memcpy(val
->data
, &fl
, 4);
534 if (SEC_SYNOPSIS
!= n
->sec
)
536 if (NULL
== n
->child
|| MDOC_TEXT
!= n
->child
->type
)
539 dbt_append(key
, ksz
, n
->child
->string
);
540 fl
= MANDOC_INCLUDES
;
541 memcpy(val
->data
, &fl
, 4);
551 if (SEC_SYNOPSIS
!= n
->sec
)
553 if (NULL
== n
->child
|| MDOC_TEXT
!= n
->child
->type
)
556 /* .Fn "struct type *arg" "foo" */
558 cp
= strrchr(n
->child
->string
, ' ');
560 cp
= n
->child
->string
;
562 /* Strip away pointer symbol. */
567 dbt_append(key
, ksz
, cp
);
568 fl
= MANDOC_FUNCTION
;
569 memcpy(val
->data
, &fl
, 4);
578 if (SEC_STANDARDS
!= n
->sec
)
580 if (NULL
== n
->child
|| MDOC_TEXT
!= n
->child
->type
)
583 dbt_append(key
, ksz
, n
->child
->string
);
584 fl
= MANDOC_STANDARD
;
585 memcpy(val
->data
, &fl
, 4);
593 const char *start
, *end
;
595 const char nil
= '\0';
597 if (SEC_SYNOPSIS
!= n
->sec
)
599 if (MDOC_Vt
== n
->tok
&& MDOC_BODY
!= n
->type
)
601 if (NULL
== n
->last
|| MDOC_TEXT
!= n
->last
->type
)
605 * Strip away leading pointer symbol '*' and trailing ';'.
608 start
= n
->last
->string
;
610 while ('*' == *start
)
613 if (0 == (sz
= strlen(start
)))
616 end
= &start
[sz
- 1];
617 while (end
> start
&& ';' == *end
)
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);
635 if (SEC_SYNOPSIS
!= n
->sec
|| MDOC_HEAD
!= n
->type
)
637 if (NULL
== n
->child
|| MDOC_TEXT
!= n
->child
->type
)
640 dbt_append(key
, ksz
, n
->child
->string
);
641 fl
= MANDOC_FUNCTION
;
642 memcpy(val
->data
, &fl
, 4);
652 for (first
= 1, n
= n
->child
; n
; n
= n
->next
) {
653 if (MDOC_TEXT
!= n
->type
)
656 dbt_appendb(rval
, rsz
, n
->string
, strlen(n
->string
) + 1);
658 dbt_append(rval
, rsz
, n
->string
);
670 if (SEC_NAME
== n
->sec
) {
671 for (n
= n
->child
; n
; n
= n
->next
) {
672 if (MDOC_TEXT
!= n
->type
)
674 dbt_append(key
, ksz
, n
->string
);
677 memcpy(val
->data
, &fl
, 4);
679 } else if (SEC_SYNOPSIS
!= n
->sec
|| MDOC_HEAD
!= n
->type
)
682 for (n
= n
->child
; n
; n
= n
->next
) {
683 if (MDOC_TEXT
!= n
->type
)
685 dbt_append(key
, ksz
, n
->string
);
689 memcpy(val
->data
, &fl
, 4);
693 dbt_put(DB
*db
, const char *dbn
, DBT
*key
, DBT
*val
)
703 if (0 == (*db
->put
)(db
, key
, val
, 0))
707 exit((int)MANDOCLEVEL_SYSERR
);
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.
716 pmdoc_node(MDOC_ARGS
)
732 if (NULL
== mdocs
[n
->tok
])
736 (*mdocs
[n
->tok
])(db
, dbn
, key
, ksz
, val
, rval
, rsz
, n
);
738 dbt_put(db
, dbn
, key
, val
);
744 pmdoc_node(db
, dbn
, key
, ksz
, val
, rval
, rsz
, n
->child
);
745 pmdoc_node(db
, dbn
, key
, ksz
, val
, rval
, rsz
, n
->next
);
751 const struct man_node
*head
, *body
;
752 const char *start
, *sv
;
753 const char nil
= '\0';
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.
767 if (MAN_BODY
== n
->type
&& MAN_SH
== n
->tok
) {
769 assert(body
->parent
);
770 if (NULL
!= (head
= body
->parent
->head
) &&
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
) {
779 memcpy(val
->data
, &fl
, 4);
781 assert(body
->string
);
782 start
= sv
= body
->string
;
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.
794 sz
= strcspn(start
, " ,");
795 if ('\0' == start
[(int)sz
])
799 dbt_appendb(key
, ksz
, start
, sz
);
800 dbt_appendb(key
, ksz
, &nil
, 1);
802 dbt_put(db
, dbn
, key
, val
);
804 if (' ' == start
[(int)sz
]) {
805 start
+= (int)sz
+ 1;
809 assert(',' == start
[(int)sz
]);
810 start
+= (int)sz
+ 1;
811 while (' ' == *start
)
817 dbt_append(key
, ksz
, start
);
821 while (' ' == *start
)
824 if ('\\' == *start
&& '-' == *(start
+ 1))
826 else if ('-' == *start
)
829 while (' ' == *start
)
832 dbt_appendb(rval
, rsz
, start
, strlen(start
) + 1);
836 if (pman_node(db
, dbn
, key
, ksz
, val
, rval
, rsz
, n
->child
))
838 if (pman_node(db
, dbn
, key
, ksz
, val
, rval
, rsz
, n
->next
))
845 pman(DB
*db
, const char *dbn
, DBT
*key
, size_t *ksz
,
846 DBT
*val
, DBT
*rval
, size_t *rsz
, struct man
*m
)
849 pman_node(db
, dbn
, key
, ksz
, val
, rval
, rsz
, man_node(m
));
854 pmdoc(DB
*db
, const char *dbn
, DBT
*key
, size_t *ksz
,
855 DBT
*val
, DBT
*rval
, size_t *rsz
, struct mdoc
*m
)
858 pmdoc_node(db
, dbn
, key
, ksz
, val
, rval
, rsz
, mdoc_node(m
));
865 fprintf(stderr
, "usage: %s "