]>
git.cameronkatri.com Git - mandoc.git/blob - mandoc-db.c
1 /* $Id: mandoc-db.c,v 1.3 2011/04/03 14:18:29 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 MDOC_ARGS DB *db, \
56 DBT *key, size_t *ksz, \
58 const struct mdoc_node *n
60 static void dbt_append(DBT
*, size_t *, const char *);
61 static void dbt_appendb(DBT
*, size_t *,
62 const void *, size_t);
63 static void dbt_init(DBT
*, size_t *);
64 static void version(void);
65 static void usage(void);
66 static void pmdoc(DB
*, const char *,
67 DBT
*, size_t *, DBT
*,
68 const char *, struct mdoc
*);
69 static void pmdoc_node(MDOC_ARGS
);
70 static void pmdoc_Fd(MDOC_ARGS
);
71 static void pmdoc_In(MDOC_ARGS
);
72 static void pmdoc_Fn(MDOC_ARGS
);
73 static void pmdoc_Fo(MDOC_ARGS
);
74 static void pmdoc_Nm(MDOC_ARGS
);
75 static void pmdoc_Vt(MDOC_ARGS
);
77 typedef void (*pmdoc_nf
)(MDOC_ARGS
);
79 static const char *progname
;
81 static const pmdoc_nf mdocs
[MDOC_MAX
] = {
207 main(int argc
, char *argv
[])
209 struct mparse
*mp
; /* parse sequence */
210 struct mdoc
*mdoc
; /* resulting mdoc */
212 const char *dir
; /* result dir (default: cwd) */
213 char ibuf
[MAXPATHLEN
], /* index fname */
214 ibbuf
[MAXPATHLEN
], /* index backup fname */
215 fbuf
[MAXPATHLEN
], /* btree fname */
216 fbbuf
[MAXPATHLEN
]; /* btree backup fname */
218 DB
*index
, /* index database */
219 *db
; /* keyword database */
220 DBT rkey
, rval
, /* recno entries */
221 key
, val
; /* persistent keyword entries */
222 size_t ksz
; /* entry buffer size */
224 BTREEINFO info
; /* btree configuration */
229 progname
= strrchr(argv
[0], '/');
230 if (progname
== NULL
)
237 while (-1 != (c
= getopt(argc
, argv
, "d:V")))
244 return((int)MANDOCLEVEL_OK
);
247 return((int)MANDOCLEVEL_BADARG
);
254 * Set up temporary file-names into which we're going to write
255 * all of our data (both for the index and database). These
256 * will be securely renamed to the real file-names after we've
257 * written all of our data.
260 ibuf
[0] = ibuf
[MAXPATHLEN
- 2] =
261 ibbuf
[0] = ibbuf
[MAXPATHLEN
- 2] =
262 fbuf
[0] = fbuf
[MAXPATHLEN
- 2] =
263 fbbuf
[0] = fbbuf
[MAXPATHLEN
- 2] = '\0';
265 strlcat(fbuf
, dir
, MAXPATHLEN
);
266 strlcat(fbuf
, MANDOC_DB
, MAXPATHLEN
);
268 strlcat(fbbuf
, fbuf
, MAXPATHLEN
);
269 strlcat(fbbuf
, "~", MAXPATHLEN
);
271 strlcat(ibuf
, dir
, MAXPATHLEN
);
272 strlcat(ibuf
, MANDOC_IDX
, MAXPATHLEN
);
274 strlcat(ibbuf
, ibuf
, MAXPATHLEN
);
275 strlcat(ibbuf
, "~", MAXPATHLEN
);
277 if ('\0' != fbuf
[MAXPATHLEN
- 2] ||
278 '\0' != fbbuf
[MAXPATHLEN
- 2] ||
279 '\0' != ibuf
[MAXPATHLEN
- 2] ||
280 '\0' != ibbuf
[MAXPATHLEN
- 2]) {
281 fprintf(stderr
, "%s: Path too long\n", progname
);
282 exit((int)MANDOCLEVEL_SYSERR
);
286 * For the keyword database, open a BTREE database that allows
287 * duplicates. For the index database, use a standard RECNO
291 memset(&info
, 0, sizeof(BTREEINFO
));
293 db
= dbopen(fbbuf
, MANDOC_FLAGS
, 0644, DB_BTREE
, &info
);
297 exit((int)MANDOCLEVEL_SYSERR
);
300 index
= dbopen(ibbuf
, MANDOC_FLAGS
, 0644, DB_RECNO
, NULL
);
305 exit((int)MANDOCLEVEL_SYSERR
);
309 * Try parsing the manuals given on the command line. If we
310 * totally fail, then just keep on going. Take resulting trees
311 * and push them down into the database code.
312 * Use the auto-parser and don't report any errors.
315 mp
= mparse_alloc(MPARSE_AUTO
, MANDOCLEVEL_FATAL
, NULL
, NULL
);
317 memset(&key
, 0, sizeof(DBT
));
318 memset(&val
, 0, sizeof(DBT
));
319 memset(&rkey
, 0, sizeof(DBT
));
320 memset(&rval
, 0, sizeof(DBT
));
322 val
.size
= sizeof(vbuf
);
324 rkey
.size
= sizeof(recno_t
);
329 while (NULL
!= (fn
= *argv
++)) {
332 if (mparse_readfd(mp
, -1, fn
) >= MANDOCLEVEL_FATAL
)
335 mparse_result(mp
, &mdoc
, NULL
);
341 rval
.size
= strlen(fn
) + 1;
343 if (-1 == (*index
->put
)(index
, &rkey
, &rval
, 0)) {
348 memset(val
.data
, 0, sizeof(uint32_t));
349 memcpy(val
.data
+ 4, &rec
, sizeof(uint32_t));
351 pmdoc(db
, fbbuf
, &key
, &ksz
, &val
, fn
, mdoc
);
356 (*index
->close
)(index
);
362 /* Atomically replace the file with our temporary one. */
364 if (-1 == rename(fbbuf
, fbuf
))
366 if (-1 == rename(ibbuf
, ibuf
))
369 return((int)MANDOCLEVEL_OK
);
373 * Initialise the stored database key whose data buffer is shared
374 * between uses (as the key must sometimes be constructed from an array
378 dbt_init(DBT
*key
, size_t *ksz
)
382 assert(0 == key
->size
);
383 assert(NULL
== key
->data
);
384 key
->data
= mandoc_malloc(MANDOC_BUFSZ
);
392 * Append a binary value to a database entry. This can be invoked
393 * multiple times; the buffer is automatically resized.
396 dbt_appendb(DBT
*key
, size_t *ksz
, const void *cp
, size_t sz
)
401 /* Overshoot by MANDOC_BUFSZ. */
403 while (key
->size
+ sz
>= *ksz
) {
404 *ksz
= key
->size
+ sz
+ MANDOC_BUFSZ
;
405 key
->data
= mandoc_realloc(key
->data
, *ksz
);
408 memcpy(key
->data
+ (int)key
->size
, cp
, sz
);
413 * Append a nil-terminated string to the database entry. This can be
414 * invoked multiple times. The database entry will be nil-terminated as
415 * well; if invoked multiple times, a space is put between strings.
418 dbt_append(DBT
*key
, size_t *ksz
, const char *cp
)
422 if (0 == (sz
= strlen(cp
)))
428 ((char *)key
->data
)[(int)key
->size
- 1] = ' ';
430 dbt_appendb(key
, ksz
, cp
, sz
+ 1);
438 const char *start
, *end
;
442 if (SEC_SYNOPSIS
!= n
->sec
)
444 if (NULL
== (n
= n
->child
) || MDOC_TEXT
!= n
->type
)
446 if (strcmp("#include", n
->string
))
448 if (NULL
== (n
= n
->next
) || MDOC_TEXT
!= n
->type
)
455 if (0 == (sz
= strlen(start
)))
458 end
= &start
[(int)sz
- 1];
463 dbt_appendb(key
, ksz
, start
, end
- start
+ 1);
464 dbt_appendb(key
, ksz
, &nil
, 1);
466 fl
= MANDOC_INCLUDES
;
467 memcpy(val
->data
, &fl
, 4);
476 if (SEC_SYNOPSIS
!= n
->sec
)
478 if (NULL
== n
->child
|| MDOC_TEXT
!= n
->child
->type
)
481 dbt_append(key
, ksz
, n
->child
->string
);
482 fl
= MANDOC_INCLUDES
;
483 memcpy(val
->data
, &fl
, 4);
493 if (SEC_SYNOPSIS
!= n
->sec
)
495 if (NULL
== n
->child
|| MDOC_TEXT
!= n
->child
->type
)
498 /* .Fn "struct type *arg" "foo" */
500 cp
= strrchr(n
->child
->string
, ' ');
502 cp
= n
->child
->string
;
504 /* Ignore pointers. */
509 dbt_append(key
, ksz
, cp
);
510 fl
= MANDOC_FUNCTION
;
511 memcpy(val
->data
, &fl
, 4);
519 const char *start
, *end
;
523 if (SEC_SYNOPSIS
!= n
->sec
)
525 if (MDOC_Vt
== n
->tok
&& MDOC_BODY
!= n
->type
)
527 if (NULL
== n
->child
|| MDOC_TEXT
!= n
->child
->type
)
531 * Strip away leading '*' and trailing ';'.
534 start
= n
->last
->string
;
536 while ('*' == *start
)
539 if (0 == (sz
= strlen(start
)))
542 end
= &start
[sz
- 1];
543 while (end
> start
&& ';' == *end
)
550 dbt_appendb(key
, ksz
, start
, end
- start
+ 1);
551 dbt_appendb(key
, ksz
, &nil
, 1);
552 fl
= MANDOC_VARIABLE
;
553 memcpy(val
->data
, &fl
, 4);
562 if (SEC_SYNOPSIS
!= n
->sec
|| MDOC_HEAD
!= n
->type
)
564 if (NULL
== n
->child
|| MDOC_TEXT
!= n
->child
->type
)
567 dbt_append(key
, ksz
, n
->child
->string
);
568 fl
= MANDOC_FUNCTION
;
569 memcpy(val
->data
, &fl
, 4);
578 if (SEC_NAME
== n
->sec
) {
579 for (n
= n
->child
; n
; n
= n
->next
) {
580 if (MDOC_TEXT
!= n
->type
)
582 dbt_append(key
, ksz
, n
->string
);
585 memcpy(val
->data
, &fl
, 4);
587 } else if (SEC_SYNOPSIS
!= n
->sec
|| MDOC_HEAD
!= n
->type
)
590 for (n
= n
->child
; n
; n
= n
->next
) {
591 if (MDOC_TEXT
!= n
->type
)
593 dbt_append(key
, ksz
, n
->string
);
597 memcpy(val
->data
, &fl
, 4);
601 * Call out to per-macro handlers after clearing the persistent database
602 * key. If the macro sets the database key, flush it to the database.
605 pmdoc_node(MDOC_ARGS
)
621 if (NULL
== mdocs
[n
->tok
])
625 (*mdocs
[n
->tok
])(db
, dbn
, key
, ksz
, val
, n
);
629 if (0 == (*db
->put
)(db
, key
, val
, 0))
633 exit((int)MANDOCLEVEL_SYSERR
);
639 pmdoc_node(db
, dbn
, key
, ksz
, val
, n
->child
);
640 pmdoc_node(db
, dbn
, key
, ksz
, val
, n
->next
);
644 pmdoc(DB
*db
, const char *dbn
,
645 DBT
*key
, size_t *ksz
, DBT
*val
,
646 const char *path
, struct mdoc
*m
)
649 pmdoc_node(db
, dbn
, key
, ksz
, val
, mdoc_node(m
));
656 printf("%s %s\n", progname
, VERSION
);
663 fprintf(stderr
, "usage: %s "