1 /* $Id: mandoc-db.c,v 1.5 2011/04/04 16:49:03 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 usage(void);
65 static void pmdoc(DB
*, const char *,
66 DBT
*, size_t *, DBT
*,
67 const char *, struct mdoc
*);
68 static void pmdoc_node(MDOC_ARGS
);
69 static void pmdoc_Fd(MDOC_ARGS
);
70 static void pmdoc_In(MDOC_ARGS
);
71 static void pmdoc_Fn(MDOC_ARGS
);
72 static void pmdoc_Fo(MDOC_ARGS
);
73 static void pmdoc_Nm(MDOC_ARGS
);
74 static void pmdoc_Vt(MDOC_ARGS
);
76 typedef void (*pmdoc_nf
)(MDOC_ARGS
);
78 static const char *progname
;
80 static const pmdoc_nf mdocs
[MDOC_MAX
] = {
206 main(int argc
, char *argv
[])
208 struct mparse
*mp
; /* parse sequence */
209 struct mdoc
*mdoc
; /* resulting mdoc */
211 const char *dir
; /* result dir (default: cwd) */
212 char ibuf
[MAXPATHLEN
], /* index fname */
213 ibbuf
[MAXPATHLEN
], /* index backup fname */
214 fbuf
[MAXPATHLEN
], /* btree fname */
215 fbbuf
[MAXPATHLEN
]; /* btree backup fname */
217 DB
*index
, /* index database */
218 *db
; /* keyword database */
219 DBT rkey
, rval
, /* recno entries */
220 key
, val
; /* persistent keyword entries */
221 size_t ksz
; /* entry buffer size */
223 BTREEINFO info
; /* btree configuration */
228 progname
= strrchr(argv
[0], '/');
229 if (progname
== NULL
)
236 while (-1 != (c
= getopt(argc
, argv
, "d:")))
243 return((int)MANDOCLEVEL_BADARG
);
250 * Set up temporary file-names into which we're going to write
251 * all of our data (both for the index and database). These
252 * will be securely renamed to the real file-names after we've
253 * written all of our data.
256 ibuf
[0] = ibuf
[MAXPATHLEN
- 2] =
257 ibbuf
[0] = ibbuf
[MAXPATHLEN
- 2] =
258 fbuf
[0] = fbuf
[MAXPATHLEN
- 2] =
259 fbbuf
[0] = fbbuf
[MAXPATHLEN
- 2] = '\0';
261 strlcat(fbuf
, dir
, MAXPATHLEN
);
262 strlcat(fbuf
, MANDOC_DB
, MAXPATHLEN
);
264 strlcat(fbbuf
, fbuf
, MAXPATHLEN
);
265 strlcat(fbbuf
, "~", MAXPATHLEN
);
267 strlcat(ibuf
, dir
, MAXPATHLEN
);
268 strlcat(ibuf
, MANDOC_IDX
, MAXPATHLEN
);
270 strlcat(ibbuf
, ibuf
, MAXPATHLEN
);
271 strlcat(ibbuf
, "~", MAXPATHLEN
);
273 if ('\0' != fbuf
[MAXPATHLEN
- 2] ||
274 '\0' != fbbuf
[MAXPATHLEN
- 2] ||
275 '\0' != ibuf
[MAXPATHLEN
- 2] ||
276 '\0' != ibbuf
[MAXPATHLEN
- 2]) {
277 fprintf(stderr
, "%s: Path too long\n", progname
);
278 exit((int)MANDOCLEVEL_SYSERR
);
282 * For the keyword database, open a BTREE database that allows
283 * duplicates. For the index database, use a standard RECNO
287 memset(&info
, 0, sizeof(BTREEINFO
));
289 db
= dbopen(fbbuf
, MANDOC_FLAGS
, 0644, DB_BTREE
, &info
);
293 exit((int)MANDOCLEVEL_SYSERR
);
296 index
= dbopen(ibbuf
, MANDOC_FLAGS
, 0644, DB_RECNO
, NULL
);
301 exit((int)MANDOCLEVEL_SYSERR
);
305 * Try parsing the manuals given on the command line. If we
306 * totally fail, then just keep on going. Take resulting trees
307 * and push them down into the database code.
308 * Use the auto-parser and don't report any errors.
311 mp
= mparse_alloc(MPARSE_AUTO
, MANDOCLEVEL_FATAL
, NULL
, NULL
);
313 memset(&key
, 0, sizeof(DBT
));
314 memset(&val
, 0, sizeof(DBT
));
315 memset(&rkey
, 0, sizeof(DBT
));
316 memset(&rval
, 0, sizeof(DBT
));
318 val
.size
= sizeof(vbuf
);
320 rkey
.size
= sizeof(recno_t
);
325 while (NULL
!= (fn
= *argv
++)) {
328 if (mparse_readfd(mp
, -1, fn
) >= MANDOCLEVEL_FATAL
) {
329 fprintf(stderr
, "%s: Parse failure\n", fn
);
333 mparse_result(mp
, &mdoc
, NULL
);
339 rval
.size
= strlen(fn
) + 1;
341 if (-1 == (*index
->put
)(index
, &rkey
, &rval
, 0)) {
346 memset(val
.data
, 0, sizeof(uint32_t));
347 memcpy(val
.data
+ 4, &rec
, sizeof(uint32_t));
349 pmdoc(db
, fbbuf
, &key
, &ksz
, &val
, fn
, mdoc
);
354 (*index
->close
)(index
);
360 /* Atomically replace the file with our temporary one. */
362 if (-1 == rename(fbbuf
, fbuf
))
364 if (-1 == rename(ibbuf
, ibuf
))
367 return((int)MANDOCLEVEL_OK
);
371 * Initialise the stored database key whose data buffer is shared
372 * between uses (as the key must sometimes be constructed from an array
376 dbt_init(DBT
*key
, size_t *ksz
)
380 assert(0 == key
->size
);
381 assert(NULL
== key
->data
);
382 key
->data
= mandoc_malloc(MANDOC_BUFSZ
);
390 * Append a binary value to a database entry. This can be invoked
391 * multiple times; the buffer is automatically resized.
394 dbt_appendb(DBT
*key
, size_t *ksz
, const void *cp
, size_t sz
)
399 /* Overshoot by MANDOC_BUFSZ. */
401 while (key
->size
+ sz
>= *ksz
) {
402 *ksz
= key
->size
+ sz
+ MANDOC_BUFSZ
;
403 key
->data
= mandoc_realloc(key
->data
, *ksz
);
406 memcpy(key
->data
+ (int)key
->size
, cp
, sz
);
411 * Append a nil-terminated string to the database entry. This can be
412 * invoked multiple times. The database entry will be nil-terminated as
413 * well; if invoked multiple times, a space is put between strings.
416 dbt_append(DBT
*key
, size_t *ksz
, const char *cp
)
420 if (0 == (sz
= strlen(cp
)))
426 ((char *)key
->data
)[(int)key
->size
- 1] = ' ';
428 dbt_appendb(key
, ksz
, cp
, sz
+ 1);
436 const char *start
, *end
;
440 if (SEC_SYNOPSIS
!= n
->sec
)
442 if (NULL
== (n
= n
->child
) || MDOC_TEXT
!= n
->type
)
446 * Only consider those `Fd' macro fields that begin with an
447 * "inclusion" token (versus, e.g., #define).
449 if (strcmp("#include", n
->string
))
452 if (NULL
== (n
= n
->next
) || MDOC_TEXT
!= n
->type
)
456 * Strip away the enclosing angle brackets and make sure we're
461 if ('<' == *start
|| '"' == *start
)
464 if (0 == (sz
= strlen(start
)))
467 end
= &start
[(int)sz
- 1];
468 if ('>' == *end
|| '"' == *end
)
472 dbt_appendb(key
, ksz
, start
, end
- start
+ 1);
473 dbt_appendb(key
, ksz
, &nil
, 1);
475 fl
= MANDOC_INCLUDES
;
476 memcpy(val
->data
, &fl
, 4);
485 if (SEC_SYNOPSIS
!= n
->sec
)
487 if (NULL
== n
->child
|| MDOC_TEXT
!= n
->child
->type
)
490 dbt_append(key
, ksz
, n
->child
->string
);
491 fl
= MANDOC_INCLUDES
;
492 memcpy(val
->data
, &fl
, 4);
502 if (SEC_SYNOPSIS
!= n
->sec
)
504 if (NULL
== n
->child
|| MDOC_TEXT
!= n
->child
->type
)
507 /* .Fn "struct type *arg" "foo" */
509 cp
= strrchr(n
->child
->string
, ' ');
511 cp
= n
->child
->string
;
513 /* Strip away pointer symbol. */
518 dbt_append(key
, ksz
, cp
);
519 fl
= MANDOC_FUNCTION
;
520 memcpy(val
->data
, &fl
, 4);
528 const char *start
, *end
;
532 if (SEC_SYNOPSIS
!= n
->sec
)
534 if (MDOC_Vt
== n
->tok
&& MDOC_BODY
!= n
->type
)
536 if (NULL
== n
->child
|| MDOC_TEXT
!= n
->child
->type
)
540 * Strip away leading pointer symbol '*' and trailing ';'.
543 start
= n
->last
->string
;
545 while ('*' == *start
)
548 if (0 == (sz
= strlen(start
)))
551 end
= &start
[sz
- 1];
552 while (end
> start
&& ';' == *end
)
559 dbt_appendb(key
, ksz
, start
, end
- start
+ 1);
560 dbt_appendb(key
, ksz
, &nil
, 1);
561 fl
= MANDOC_VARIABLE
;
562 memcpy(val
->data
, &fl
, 4);
571 if (SEC_SYNOPSIS
!= n
->sec
|| MDOC_HEAD
!= n
->type
)
573 if (NULL
== n
->child
|| MDOC_TEXT
!= n
->child
->type
)
576 dbt_append(key
, ksz
, n
->child
->string
);
577 fl
= MANDOC_FUNCTION
;
578 memcpy(val
->data
, &fl
, 4);
587 if (SEC_NAME
== n
->sec
) {
588 for (n
= n
->child
; n
; n
= n
->next
) {
589 if (MDOC_TEXT
!= n
->type
)
591 dbt_append(key
, ksz
, n
->string
);
594 memcpy(val
->data
, &fl
, 4);
596 } else if (SEC_SYNOPSIS
!= n
->sec
|| MDOC_HEAD
!= n
->type
)
599 for (n
= n
->child
; n
; n
= n
->next
) {
600 if (MDOC_TEXT
!= n
->type
)
602 dbt_append(key
, ksz
, n
->string
);
606 memcpy(val
->data
, &fl
, 4);
610 * Call out to per-macro handlers after clearing the persistent database
611 * key. If the macro sets the database key, flush it to the database.
614 pmdoc_node(MDOC_ARGS
)
630 if (NULL
== mdocs
[n
->tok
])
634 (*mdocs
[n
->tok
])(db
, dbn
, key
, ksz
, val
, n
);
638 if (0 == (*db
->put
)(db
, key
, val
, 0))
642 exit((int)MANDOCLEVEL_SYSERR
);
648 pmdoc_node(db
, dbn
, key
, ksz
, val
, n
->child
);
649 pmdoc_node(db
, dbn
, key
, ksz
, val
, n
->next
);
653 pmdoc(DB
*db
, const char *dbn
,
654 DBT
*key
, size_t *ksz
, DBT
*val
,
655 const char *path
, struct mdoc
*m
)
658 pmdoc_node(db
, dbn
, key
, ksz
, val
, mdoc_node(m
));
665 fprintf(stderr
, "usage: %s "