]>
git.cameronkatri.com Git - mandoc.git/blob - mandoc-db.c
1 /* $Id: mandoc-db.c,v 1.6 2011/04/05 13:09:33 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 usage(void);
70 static void pman(DB
*, const char *,
71 DBT
*, size_t *, DBT
*,
72 const char *, struct man
*);
73 static int pman_node(MAN_ARGS
);
74 static void pmdoc(DB
*, const char *,
75 DBT
*, size_t *, DBT
*,
76 const char *, struct mdoc
*);
77 static void pmdoc_node(MDOC_ARGS
);
78 static void pmdoc_Fd(MDOC_ARGS
);
79 static void pmdoc_In(MDOC_ARGS
);
80 static void pmdoc_Fn(MDOC_ARGS
);
81 static void pmdoc_Fo(MDOC_ARGS
);
82 static void pmdoc_Nm(MDOC_ARGS
);
83 static void pmdoc_Vt(MDOC_ARGS
);
85 typedef void (*pmdoc_nf
)(MDOC_ARGS
);
87 static const char *progname
;
89 static const pmdoc_nf mdocs
[MDOC_MAX
] = {
215 main(int argc
, char *argv
[])
217 struct mparse
*mp
; /* parse sequence */
218 struct mdoc
*mdoc
; /* resulting mdoc */
219 struct man
*man
; /* resulting man */
221 const char *dir
; /* result dir (default: cwd) */
222 char ibuf
[MAXPATHLEN
], /* index fname */
223 ibbuf
[MAXPATHLEN
], /* index backup fname */
224 fbuf
[MAXPATHLEN
], /* btree fname */
225 fbbuf
[MAXPATHLEN
]; /* btree backup fname */
227 DB
*index
, /* index database */
228 *db
; /* keyword database */
229 DBT rkey
, rval
, /* recno entries */
230 key
, val
; /* persistent keyword entries */
231 size_t ksz
; /* entry buffer size */
233 BTREEINFO info
; /* btree configuration */
238 progname
= strrchr(argv
[0], '/');
239 if (progname
== NULL
)
246 while (-1 != (c
= getopt(argc
, argv
, "d:")))
253 return((int)MANDOCLEVEL_BADARG
);
260 * Set up temporary file-names into which we're going to write
261 * all of our data (both for the index and database). These
262 * will be securely renamed to the real file-names after we've
263 * written all of our data.
266 ibuf
[0] = ibuf
[MAXPATHLEN
- 2] =
267 ibbuf
[0] = ibbuf
[MAXPATHLEN
- 2] =
268 fbuf
[0] = fbuf
[MAXPATHLEN
- 2] =
269 fbbuf
[0] = fbbuf
[MAXPATHLEN
- 2] = '\0';
271 strlcat(fbuf
, dir
, MAXPATHLEN
);
272 strlcat(fbuf
, MANDOC_DB
, MAXPATHLEN
);
274 strlcat(fbbuf
, fbuf
, MAXPATHLEN
);
275 strlcat(fbbuf
, "~", MAXPATHLEN
);
277 strlcat(ibuf
, dir
, MAXPATHLEN
);
278 strlcat(ibuf
, MANDOC_IDX
, MAXPATHLEN
);
280 strlcat(ibbuf
, ibuf
, MAXPATHLEN
);
281 strlcat(ibbuf
, "~", MAXPATHLEN
);
283 if ('\0' != fbuf
[MAXPATHLEN
- 2] ||
284 '\0' != fbbuf
[MAXPATHLEN
- 2] ||
285 '\0' != ibuf
[MAXPATHLEN
- 2] ||
286 '\0' != ibbuf
[MAXPATHLEN
- 2]) {
287 fprintf(stderr
, "%s: Path too long\n", progname
);
288 exit((int)MANDOCLEVEL_SYSERR
);
292 * For the keyword database, open a BTREE database that allows
293 * duplicates. For the index database, use a standard RECNO
297 memset(&info
, 0, sizeof(BTREEINFO
));
299 db
= dbopen(fbbuf
, MANDOC_FLAGS
, 0644, DB_BTREE
, &info
);
303 exit((int)MANDOCLEVEL_SYSERR
);
306 index
= dbopen(ibbuf
, MANDOC_FLAGS
, 0644, DB_RECNO
, NULL
);
311 exit((int)MANDOCLEVEL_SYSERR
);
315 * Try parsing the manuals given on the command line. If we
316 * totally fail, then just keep on going. Take resulting trees
317 * and push them down into the database code.
318 * Use the auto-parser and don't report any errors.
321 mp
= mparse_alloc(MPARSE_AUTO
, MANDOCLEVEL_FATAL
, NULL
, NULL
);
323 memset(&key
, 0, sizeof(DBT
));
324 memset(&val
, 0, sizeof(DBT
));
325 memset(&rkey
, 0, sizeof(DBT
));
326 memset(&rval
, 0, sizeof(DBT
));
328 val
.size
= sizeof(vbuf
);
330 rkey
.size
= sizeof(recno_t
);
335 while (NULL
!= (fn
= *argv
++)) {
338 if (mparse_readfd(mp
, -1, fn
) >= MANDOCLEVEL_FATAL
) {
339 fprintf(stderr
, "%s: Parse failure\n", fn
);
343 mparse_result(mp
, &mdoc
, &man
);
344 if (NULL
== mdoc
&& NULL
== man
)
349 rval
.size
= strlen(fn
) + 1;
351 if (-1 == (*index
->put
)(index
, &rkey
, &rval
, 0)) {
356 memset(val
.data
, 0, sizeof(uint32_t));
357 memcpy(val
.data
+ 4, &rec
, sizeof(uint32_t));
360 pmdoc(db
, fbbuf
, &key
, &ksz
, &val
, fn
, mdoc
);
362 pman(db
, fbbuf
, &key
, &ksz
, &val
, fn
, man
);
367 (*index
->close
)(index
);
373 /* Atomically replace the file with our temporary one. */
375 if (-1 == rename(fbbuf
, fbuf
))
377 if (-1 == rename(ibbuf
, ibuf
))
380 return((int)MANDOCLEVEL_OK
);
384 * Initialise the stored database key whose data buffer is shared
385 * between uses (as the key must sometimes be constructed from an array
389 dbt_init(DBT
*key
, size_t *ksz
)
393 assert(0 == key
->size
);
394 assert(NULL
== key
->data
);
395 key
->data
= mandoc_malloc(MANDOC_BUFSZ
);
403 * Append a binary value to a database entry. This can be invoked
404 * multiple times; the buffer is automatically resized.
407 dbt_appendb(DBT
*key
, size_t *ksz
, const void *cp
, size_t sz
)
412 /* Overshoot by MANDOC_BUFSZ. */
414 while (key
->size
+ sz
>= *ksz
) {
415 *ksz
= key
->size
+ sz
+ MANDOC_BUFSZ
;
416 key
->data
= mandoc_realloc(key
->data
, *ksz
);
419 memcpy(key
->data
+ (int)key
->size
, cp
, sz
);
424 * Append a nil-terminated string to the database entry. This can be
425 * invoked multiple times. The database entry will be nil-terminated as
426 * well; if invoked multiple times, a space is put between strings.
429 dbt_append(DBT
*key
, size_t *ksz
, const char *cp
)
433 if (0 == (sz
= strlen(cp
)))
439 ((char *)key
->data
)[(int)key
->size
- 1] = ' ';
441 dbt_appendb(key
, ksz
, cp
, sz
+ 1);
449 const char *start
, *end
;
453 if (SEC_SYNOPSIS
!= n
->sec
)
455 if (NULL
== (n
= n
->child
) || MDOC_TEXT
!= n
->type
)
459 * Only consider those `Fd' macro fields that begin with an
460 * "inclusion" token (versus, e.g., #define).
462 if (strcmp("#include", n
->string
))
465 if (NULL
== (n
= n
->next
) || MDOC_TEXT
!= n
->type
)
469 * Strip away the enclosing angle brackets and make sure we're
474 if ('<' == *start
|| '"' == *start
)
477 if (0 == (sz
= strlen(start
)))
480 end
= &start
[(int)sz
- 1];
481 if ('>' == *end
|| '"' == *end
)
485 dbt_appendb(key
, ksz
, start
, end
- start
+ 1);
486 dbt_appendb(key
, ksz
, &nil
, 1);
488 fl
= MANDOC_INCLUDES
;
489 memcpy(val
->data
, &fl
, 4);
498 if (SEC_SYNOPSIS
!= n
->sec
)
500 if (NULL
== n
->child
|| MDOC_TEXT
!= n
->child
->type
)
503 dbt_append(key
, ksz
, n
->child
->string
);
504 fl
= MANDOC_INCLUDES
;
505 memcpy(val
->data
, &fl
, 4);
515 if (SEC_SYNOPSIS
!= n
->sec
)
517 if (NULL
== n
->child
|| MDOC_TEXT
!= n
->child
->type
)
520 /* .Fn "struct type *arg" "foo" */
522 cp
= strrchr(n
->child
->string
, ' ');
524 cp
= n
->child
->string
;
526 /* Strip away pointer symbol. */
531 dbt_append(key
, ksz
, cp
);
532 fl
= MANDOC_FUNCTION
;
533 memcpy(val
->data
, &fl
, 4);
541 const char *start
, *end
;
545 if (SEC_SYNOPSIS
!= n
->sec
)
547 if (MDOC_Vt
== n
->tok
&& MDOC_BODY
!= n
->type
)
549 if (NULL
== n
->child
|| MDOC_TEXT
!= n
->child
->type
)
553 * Strip away leading pointer symbol '*' and trailing ';'.
556 start
= n
->last
->string
;
558 while ('*' == *start
)
561 if (0 == (sz
= strlen(start
)))
564 end
= &start
[sz
- 1];
565 while (end
> start
&& ';' == *end
)
572 dbt_appendb(key
, ksz
, start
, end
- start
+ 1);
573 dbt_appendb(key
, ksz
, &nil
, 1);
574 fl
= MANDOC_VARIABLE
;
575 memcpy(val
->data
, &fl
, 4);
584 if (SEC_SYNOPSIS
!= n
->sec
|| MDOC_HEAD
!= n
->type
)
586 if (NULL
== n
->child
|| MDOC_TEXT
!= n
->child
->type
)
589 dbt_append(key
, ksz
, n
->child
->string
);
590 fl
= MANDOC_FUNCTION
;
591 memcpy(val
->data
, &fl
, 4);
600 if (SEC_NAME
== n
->sec
) {
601 for (n
= n
->child
; n
; n
= n
->next
) {
602 if (MDOC_TEXT
!= n
->type
)
604 dbt_append(key
, ksz
, n
->string
);
607 memcpy(val
->data
, &fl
, 4);
609 } else if (SEC_SYNOPSIS
!= n
->sec
|| MDOC_HEAD
!= n
->type
)
612 for (n
= n
->child
; n
; n
= n
->next
) {
613 if (MDOC_TEXT
!= n
->type
)
615 dbt_append(key
, ksz
, n
->string
);
619 memcpy(val
->data
, &fl
, 4);
623 * Call out to per-macro handlers after clearing the persistent database
624 * key. If the macro sets the database key, flush it to the database.
627 pmdoc_node(MDOC_ARGS
)
643 if (NULL
== mdocs
[n
->tok
])
647 (*mdocs
[n
->tok
])(db
, dbn
, key
, ksz
, val
, n
);
651 if (0 == (*db
->put
)(db
, key
, val
, 0))
655 exit((int)MANDOCLEVEL_SYSERR
);
661 pmdoc_node(db
, dbn
, key
, ksz
, val
, n
->child
);
662 pmdoc_node(db
, dbn
, key
, ksz
, val
, n
->next
);
668 const struct man_node
*head
, *body
;
669 const char *end
, *start
;
677 * We're only searching for one thing: the first text child in
678 * the BODY of a NAME section. Since we don't keep track of
679 * sections in -man, run some hoops to find out whether we're in
680 * the correct section or not.
683 if (MAN_BODY
== n
->type
&& MAN_SH
== n
->tok
) {
685 assert(body
->parent
);
686 if (NULL
!= (head
= body
->parent
->head
) &&
688 NULL
!= (head
= (head
->child
)) &&
689 MAN_TEXT
== head
->type
&&
690 0 == strcmp(head
->string
, "NAME") &&
691 NULL
!= (body
= body
->child
) &&
692 MAN_TEXT
== body
->type
) {
695 start
= body
->string
;
696 if (NULL
== (end
= strchr(start
, ' ')))
697 end
= start
+ strlen(start
);
700 dbt_appendb(key
, ksz
, start
, end
- start
+ 1);
701 dbt_appendb(key
, ksz
, &nil
, 1);
703 memcpy(val
->data
, &fl
, 4);
708 if (pman_node(db
, dbn
, key
, ksz
, val
, n
->child
))
710 if (pman_node(db
, dbn
, key
, ksz
, val
, n
->next
))
717 pman(DB
*db
, const char *dbn
,
718 DBT
*key
, size_t *ksz
, DBT
*val
,
719 const char *path
, struct man
*m
)
722 pman_node(db
, dbn
, key
, ksz
, val
, man_node(m
));
727 pmdoc(DB
*db
, const char *dbn
,
728 DBT
*key
, size_t *ksz
, DBT
*val
,
729 const char *path
, struct mdoc
*m
)
732 pmdoc_node(db
, dbn
, key
, ksz
, val
, mdoc_node(m
));
739 fprintf(stderr
, "usage: %s "