]>
git.cameronkatri.com Git - mandoc.git/blob - mandoc-db.c
1 /* $Id: mandoc-db.c,v 1.9 2011/04/05 14:16:05 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
54 #define MAN_ARGS DB *db, \
56 DBT *key, size_t *ksz, \
58 const struct man_node *n
59 #define MDOC_ARGS DB *db, \
61 DBT *key, size_t *ksz, \
63 const struct mdoc_node *n
65 static void dbt_append(DBT
*, size_t *, const char *);
66 static void dbt_appendb(DBT
*, size_t *,
67 const void *, size_t);
68 static void dbt_init(DBT
*, size_t *);
69 static void dbt_put(DB
*, const char *, DBT
*, DBT
*);
70 static void usage(void);
71 static void pman(DB
*, const char *, DBT
*,
72 size_t *, DBT
*, struct man
*);
73 static int pman_node(MAN_ARGS
);
74 static void pmdoc(DB
*, const char *, DBT
*,
75 size_t *, DBT
*, struct mdoc
*);
76 static void pmdoc_node(MDOC_ARGS
);
77 static void pmdoc_Fd(MDOC_ARGS
);
78 static void pmdoc_In(MDOC_ARGS
);
79 static void pmdoc_Fn(MDOC_ARGS
);
80 static void pmdoc_Fo(MDOC_ARGS
);
81 static void pmdoc_Nm(MDOC_ARGS
);
82 static void pmdoc_Vt(MDOC_ARGS
);
84 typedef void (*pmdoc_nf
)(MDOC_ARGS
);
86 static const char *progname
;
88 static const pmdoc_nf mdocs
[MDOC_MAX
] = {
214 main(int argc
, char *argv
[])
216 struct mparse
*mp
; /* parse sequence */
217 struct mdoc
*mdoc
; /* resulting mdoc */
218 struct man
*man
; /* resulting man */
220 const char *dir
; /* result dir (default: cwd) */
221 char ibuf
[MAXPATHLEN
], /* index fname */
222 ibbuf
[MAXPATHLEN
], /* index backup fname */
223 fbuf
[MAXPATHLEN
], /* btree fname */
224 fbbuf
[MAXPATHLEN
]; /* btree backup fname */
226 DB
*idx
, /* index database */
227 *db
; /* keyword database */
228 DBT rkey
, rval
, /* recno entries */
229 key
, val
; /* persistent keyword entries */
230 size_t ksz
; /* entry buffer size */
232 BTREEINFO info
; /* btree configuration */
237 progname
= strrchr(argv
[0], '/');
238 if (progname
== NULL
)
245 while (-1 != (c
= getopt(argc
, argv
, "d:")))
252 return((int)MANDOCLEVEL_BADARG
);
259 * Set up temporary file-names into which we're going to write
260 * all of our data (both for the index and database). These
261 * will be securely renamed to the real file-names after we've
262 * written all of our data.
265 ibuf
[0] = ibuf
[MAXPATHLEN
- 2] =
266 ibbuf
[0] = ibbuf
[MAXPATHLEN
- 2] =
267 fbuf
[0] = fbuf
[MAXPATHLEN
- 2] =
268 fbbuf
[0] = fbbuf
[MAXPATHLEN
- 2] = '\0';
270 strlcat(fbuf
, dir
, MAXPATHLEN
);
271 strlcat(fbuf
, MANDOC_DB
, MAXPATHLEN
);
273 strlcat(fbbuf
, fbuf
, MAXPATHLEN
);
274 strlcat(fbbuf
, "~", MAXPATHLEN
);
276 strlcat(ibuf
, dir
, MAXPATHLEN
);
277 strlcat(ibuf
, MANDOC_IDX
, MAXPATHLEN
);
279 strlcat(ibbuf
, ibuf
, MAXPATHLEN
);
280 strlcat(ibbuf
, "~", MAXPATHLEN
);
282 if ('\0' != fbuf
[MAXPATHLEN
- 2] ||
283 '\0' != fbbuf
[MAXPATHLEN
- 2] ||
284 '\0' != ibuf
[MAXPATHLEN
- 2] ||
285 '\0' != ibbuf
[MAXPATHLEN
- 2]) {
286 fprintf(stderr
, "%s: Path too long\n", progname
);
287 exit((int)MANDOCLEVEL_SYSERR
);
291 * For the keyword database, open a BTREE database that allows
292 * duplicates. For the index database, use a standard RECNO
296 memset(&info
, 0, sizeof(BTREEINFO
));
298 db
= dbopen(fbbuf
, MANDOC_FLAGS
, 0644, DB_BTREE
, &info
);
302 exit((int)MANDOCLEVEL_SYSERR
);
305 idx
= dbopen(ibbuf
, MANDOC_FLAGS
, 0644, DB_RECNO
, NULL
);
310 exit((int)MANDOCLEVEL_SYSERR
);
314 * Try parsing the manuals given on the command line. If we
315 * totally fail, then just keep on going. Take resulting trees
316 * and push them down into the database code.
317 * Use the auto-parser and don't report any errors.
320 mp
= mparse_alloc(MPARSE_AUTO
, MANDOCLEVEL_FATAL
, NULL
, NULL
);
322 memset(&key
, 0, sizeof(DBT
));
323 memset(&val
, 0, sizeof(DBT
));
324 memset(&rkey
, 0, sizeof(DBT
));
325 memset(&rval
, 0, sizeof(DBT
));
327 val
.size
= sizeof(vbuf
);
329 rkey
.size
= sizeof(recno_t
);
334 while (NULL
!= (fn
= *argv
++)) {
337 if (mparse_readfd(mp
, -1, fn
) >= MANDOCLEVEL_FATAL
) {
338 fprintf(stderr
, "%s: Parse failure\n", fn
);
342 mparse_result(mp
, &mdoc
, &man
);
343 if (NULL
== mdoc
&& NULL
== man
)
348 rval
.size
= strlen(fn
) + 1;
350 if (-1 == (*idx
->put
)(idx
, &rkey
, &rval
, 0)) {
355 memset(val
.data
, 0, sizeof(uint32_t));
356 memcpy(val
.data
+ 4, &rec
, sizeof(uint32_t));
359 pmdoc(db
, fbbuf
, &key
, &ksz
, &val
, mdoc
);
361 pman(db
, fbbuf
, &key
, &ksz
, &val
, man
);
372 /* Atomically replace the file with our temporary one. */
374 if (-1 == rename(fbbuf
, fbuf
))
376 if (-1 == rename(ibbuf
, ibuf
))
379 return((int)MANDOCLEVEL_OK
);
383 * Initialise the stored database key whose data buffer is shared
384 * between uses (as the key must sometimes be constructed from an array
388 dbt_init(DBT
*key
, size_t *ksz
)
392 assert(0 == key
->size
);
393 assert(NULL
== key
->data
);
394 key
->data
= mandoc_malloc(MANDOC_BUFSZ
);
402 * Append a binary value to a database entry. This can be invoked
403 * multiple times; the buffer is automatically resized.
406 dbt_appendb(DBT
*key
, size_t *ksz
, const void *cp
, size_t sz
)
411 /* Overshoot by MANDOC_BUFSZ. */
413 while (key
->size
+ sz
>= *ksz
) {
414 *ksz
= key
->size
+ sz
+ MANDOC_BUFSZ
;
415 key
->data
= mandoc_realloc(key
->data
, *ksz
);
418 memcpy(key
->data
+ (int)key
->size
, cp
, sz
);
423 * Append a nil-terminated string to the database entry. This can be
424 * invoked multiple times. The database entry will be nil-terminated as
425 * well; if invoked multiple times, a space is put between strings.
428 dbt_append(DBT
*key
, size_t *ksz
, const char *cp
)
432 if (0 == (sz
= strlen(cp
)))
438 ((char *)key
->data
)[(int)key
->size
- 1] = ' ';
440 dbt_appendb(key
, ksz
, cp
, sz
+ 1);
448 const char *start
, *end
;
452 if (SEC_SYNOPSIS
!= n
->sec
)
454 if (NULL
== (n
= n
->child
) || MDOC_TEXT
!= n
->type
)
458 * Only consider those `Fd' macro fields that begin with an
459 * "inclusion" token (versus, e.g., #define).
461 if (strcmp("#include", n
->string
))
464 if (NULL
== (n
= n
->next
) || MDOC_TEXT
!= n
->type
)
468 * Strip away the enclosing angle brackets and make sure we're
473 if ('<' == *start
|| '"' == *start
)
476 if (0 == (sz
= strlen(start
)))
479 end
= &start
[(int)sz
- 1];
480 if ('>' == *end
|| '"' == *end
)
484 dbt_appendb(key
, ksz
, start
, end
- start
+ 1);
485 dbt_appendb(key
, ksz
, &nil
, 1);
487 fl
= MANDOC_INCLUDES
;
488 memcpy(val
->data
, &fl
, 4);
497 if (SEC_SYNOPSIS
!= n
->sec
)
499 if (NULL
== n
->child
|| MDOC_TEXT
!= n
->child
->type
)
502 dbt_append(key
, ksz
, n
->child
->string
);
503 fl
= MANDOC_INCLUDES
;
504 memcpy(val
->data
, &fl
, 4);
514 if (SEC_SYNOPSIS
!= n
->sec
)
516 if (NULL
== n
->child
|| MDOC_TEXT
!= n
->child
->type
)
519 /* .Fn "struct type *arg" "foo" */
521 cp
= strrchr(n
->child
->string
, ' ');
523 cp
= n
->child
->string
;
525 /* Strip away pointer symbol. */
530 dbt_append(key
, ksz
, cp
);
531 fl
= MANDOC_FUNCTION
;
532 memcpy(val
->data
, &fl
, 4);
540 const char *start
, *end
;
544 if (SEC_SYNOPSIS
!= n
->sec
)
546 if (MDOC_Vt
== n
->tok
&& MDOC_BODY
!= n
->type
)
548 if (NULL
== n
->last
|| MDOC_TEXT
!= n
->last
->type
)
552 * Strip away leading pointer symbol '*' and trailing ';'.
555 start
= n
->last
->string
;
557 while ('*' == *start
)
560 if (0 == (sz
= strlen(start
)))
563 end
= &start
[sz
- 1];
564 while (end
> start
&& ';' == *end
)
571 dbt_appendb(key
, ksz
, start
, end
- start
+ 1);
572 dbt_appendb(key
, ksz
, &nil
, 1);
573 fl
= MANDOC_VARIABLE
;
574 memcpy(val
->data
, &fl
, 4);
583 if (SEC_SYNOPSIS
!= n
->sec
|| MDOC_HEAD
!= n
->type
)
585 if (NULL
== n
->child
|| MDOC_TEXT
!= n
->child
->type
)
588 dbt_append(key
, ksz
, n
->child
->string
);
589 fl
= MANDOC_FUNCTION
;
590 memcpy(val
->data
, &fl
, 4);
599 if (SEC_NAME
== n
->sec
) {
600 for (n
= n
->child
; n
; n
= n
->next
) {
601 if (MDOC_TEXT
!= n
->type
)
603 dbt_append(key
, ksz
, n
->string
);
606 memcpy(val
->data
, &fl
, 4);
608 } else if (SEC_SYNOPSIS
!= n
->sec
|| MDOC_HEAD
!= n
->type
)
611 for (n
= n
->child
; n
; n
= n
->next
) {
612 if (MDOC_TEXT
!= n
->type
)
614 dbt_append(key
, ksz
, n
->string
);
618 memcpy(val
->data
, &fl
, 4);
622 dbt_put(DB
*db
, const char *dbn
, DBT
*key
, DBT
*val
)
629 assert(8 == val
->size
);
632 if (0 == (*db
->put
)(db
, key
, val
, 0))
636 exit((int)MANDOCLEVEL_SYSERR
);
641 * Call out to per-macro handlers after clearing the persistent database
642 * key. If the macro sets the database key, flush it to the database.
645 pmdoc_node(MDOC_ARGS
)
661 if (NULL
== mdocs
[n
->tok
])
665 (*mdocs
[n
->tok
])(db
, dbn
, key
, ksz
, val
, n
);
667 dbt_put(db
, dbn
, key
, val
);
673 pmdoc_node(db
, dbn
, key
, ksz
, val
, n
->child
);
674 pmdoc_node(db
, dbn
, key
, ksz
, val
, n
->next
);
680 const struct man_node
*head
, *body
;
690 * We're only searching for one thing: the first text child in
691 * the BODY of a NAME section. Since we don't keep track of
692 * sections in -man, run some hoops to find out whether we're in
693 * the correct section or not.
696 if (MAN_BODY
== n
->type
&& MAN_SH
== n
->tok
) {
698 assert(body
->parent
);
699 if (NULL
!= (head
= body
->parent
->head
) &&
701 NULL
!= (head
= (head
->child
)) &&
702 MAN_TEXT
== head
->type
&&
703 0 == strcmp(head
->string
, "NAME") &&
704 NULL
!= (body
= body
->child
) &&
705 MAN_TEXT
== body
->type
) {
709 memcpy(val
->data
, &fl
, 4);
711 start
= body
->string
;
714 * Go through a special heuristic dance here.
715 * This is why -man manuals are great!
716 * Conventionally, one or more manual names are
717 * comma-specified prior to a whitespace, then a
718 * dash, then a description. Try to puzzle out
719 * the name parts here.
723 sz
= strcspn(start
, " ,");
724 if ('\0' == start
[(int)sz
])
728 dbt_appendb(key
, ksz
, start
, sz
);
729 dbt_appendb(key
, ksz
, &nil
, 1);
731 dbt_put(db
, dbn
, key
, val
);
733 if (' ' == start
[(int)sz
])
736 assert(',' == start
[(int)sz
]);
737 start
+= (int)sz
+ 1;
738 while (' ' == *start
)
746 if (pman_node(db
, dbn
, key
, ksz
, val
, n
->child
))
748 if (pman_node(db
, dbn
, key
, ksz
, val
, n
->next
))
755 pman(DB
*db
, const char *dbn
, DBT
*key
,
756 size_t *ksz
, DBT
*val
, struct man
*m
)
759 pman_node(db
, dbn
, key
, ksz
, val
, man_node(m
));
764 pmdoc(DB
*db
, const char *dbn
, DBT
*key
,
765 size_t *ksz
, DBT
*val
, struct mdoc
*m
)
768 pmdoc_node(db
, dbn
, key
, ksz
, val
, mdoc_node(m
));
775 fprintf(stderr
, "usage: %s "