]>
git.cameronkatri.com Git - mandoc.git/blob - mandoc-db.c
1 /* $Id: mandoc-db.c,v 1.8 2011/04/05 14:10:52 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 *,
72 DBT
*, size_t *, DBT
*,
73 const char *, struct man
*);
74 static int pman_node(MAN_ARGS
);
75 static void pmdoc(DB
*, const char *,
76 DBT
*, size_t *, DBT
*,
77 const char *, struct mdoc
*);
78 static void pmdoc_node(MDOC_ARGS
);
79 static void pmdoc_Fd(MDOC_ARGS
);
80 static void pmdoc_In(MDOC_ARGS
);
81 static void pmdoc_Fn(MDOC_ARGS
);
82 static void pmdoc_Fo(MDOC_ARGS
);
83 static void pmdoc_Nm(MDOC_ARGS
);
84 static void pmdoc_Vt(MDOC_ARGS
);
86 typedef void (*pmdoc_nf
)(MDOC_ARGS
);
88 static const char *progname
;
90 static const pmdoc_nf mdocs
[MDOC_MAX
] = {
216 main(int argc
, char *argv
[])
218 struct mparse
*mp
; /* parse sequence */
219 struct mdoc
*mdoc
; /* resulting mdoc */
220 struct man
*man
; /* resulting man */
222 const char *dir
; /* result dir (default: cwd) */
223 char ibuf
[MAXPATHLEN
], /* index fname */
224 ibbuf
[MAXPATHLEN
], /* index backup fname */
225 fbuf
[MAXPATHLEN
], /* btree fname */
226 fbbuf
[MAXPATHLEN
]; /* btree backup fname */
228 DB
*index
, /* index database */
229 *db
; /* keyword database */
230 DBT rkey
, rval
, /* recno entries */
231 key
, val
; /* persistent keyword entries */
232 size_t ksz
; /* entry buffer size */
234 BTREEINFO info
; /* btree configuration */
239 progname
= strrchr(argv
[0], '/');
240 if (progname
== NULL
)
247 while (-1 != (c
= getopt(argc
, argv
, "d:")))
254 return((int)MANDOCLEVEL_BADARG
);
261 * Set up temporary file-names into which we're going to write
262 * all of our data (both for the index and database). These
263 * will be securely renamed to the real file-names after we've
264 * written all of our data.
267 ibuf
[0] = ibuf
[MAXPATHLEN
- 2] =
268 ibbuf
[0] = ibbuf
[MAXPATHLEN
- 2] =
269 fbuf
[0] = fbuf
[MAXPATHLEN
- 2] =
270 fbbuf
[0] = fbbuf
[MAXPATHLEN
- 2] = '\0';
272 strlcat(fbuf
, dir
, MAXPATHLEN
);
273 strlcat(fbuf
, MANDOC_DB
, MAXPATHLEN
);
275 strlcat(fbbuf
, fbuf
, MAXPATHLEN
);
276 strlcat(fbbuf
, "~", MAXPATHLEN
);
278 strlcat(ibuf
, dir
, MAXPATHLEN
);
279 strlcat(ibuf
, MANDOC_IDX
, MAXPATHLEN
);
281 strlcat(ibbuf
, ibuf
, MAXPATHLEN
);
282 strlcat(ibbuf
, "~", MAXPATHLEN
);
284 if ('\0' != fbuf
[MAXPATHLEN
- 2] ||
285 '\0' != fbbuf
[MAXPATHLEN
- 2] ||
286 '\0' != ibuf
[MAXPATHLEN
- 2] ||
287 '\0' != ibbuf
[MAXPATHLEN
- 2]) {
288 fprintf(stderr
, "%s: Path too long\n", progname
);
289 exit((int)MANDOCLEVEL_SYSERR
);
293 * For the keyword database, open a BTREE database that allows
294 * duplicates. For the index database, use a standard RECNO
298 memset(&info
, 0, sizeof(BTREEINFO
));
300 db
= dbopen(fbbuf
, MANDOC_FLAGS
, 0644, DB_BTREE
, &info
);
304 exit((int)MANDOCLEVEL_SYSERR
);
307 index
= dbopen(ibbuf
, MANDOC_FLAGS
, 0644, DB_RECNO
, NULL
);
312 exit((int)MANDOCLEVEL_SYSERR
);
316 * Try parsing the manuals given on the command line. If we
317 * totally fail, then just keep on going. Take resulting trees
318 * and push them down into the database code.
319 * Use the auto-parser and don't report any errors.
322 mp
= mparse_alloc(MPARSE_AUTO
, MANDOCLEVEL_FATAL
, NULL
, NULL
);
324 memset(&key
, 0, sizeof(DBT
));
325 memset(&val
, 0, sizeof(DBT
));
326 memset(&rkey
, 0, sizeof(DBT
));
327 memset(&rval
, 0, sizeof(DBT
));
329 val
.size
= sizeof(vbuf
);
331 rkey
.size
= sizeof(recno_t
);
336 while (NULL
!= (fn
= *argv
++)) {
339 if (mparse_readfd(mp
, -1, fn
) >= MANDOCLEVEL_FATAL
) {
340 fprintf(stderr
, "%s: Parse failure\n", fn
);
344 mparse_result(mp
, &mdoc
, &man
);
345 if (NULL
== mdoc
&& NULL
== man
)
350 rval
.size
= strlen(fn
) + 1;
352 if (-1 == (*index
->put
)(index
, &rkey
, &rval
, 0)) {
357 memset(val
.data
, 0, sizeof(uint32_t));
358 memcpy(val
.data
+ 4, &rec
, sizeof(uint32_t));
361 pmdoc(db
, fbbuf
, &key
, &ksz
, &val
, fn
, mdoc
);
363 pman(db
, fbbuf
, &key
, &ksz
, &val
, fn
, man
);
368 (*index
->close
)(index
);
374 /* Atomically replace the file with our temporary one. */
376 if (-1 == rename(fbbuf
, fbuf
))
378 if (-1 == rename(ibbuf
, ibuf
))
381 return((int)MANDOCLEVEL_OK
);
385 * Initialise the stored database key whose data buffer is shared
386 * between uses (as the key must sometimes be constructed from an array
390 dbt_init(DBT
*key
, size_t *ksz
)
394 assert(0 == key
->size
);
395 assert(NULL
== key
->data
);
396 key
->data
= mandoc_malloc(MANDOC_BUFSZ
);
404 * Append a binary value to a database entry. This can be invoked
405 * multiple times; the buffer is automatically resized.
408 dbt_appendb(DBT
*key
, size_t *ksz
, const void *cp
, size_t sz
)
413 /* Overshoot by MANDOC_BUFSZ. */
415 while (key
->size
+ sz
>= *ksz
) {
416 *ksz
= key
->size
+ sz
+ MANDOC_BUFSZ
;
417 key
->data
= mandoc_realloc(key
->data
, *ksz
);
420 memcpy(key
->data
+ (int)key
->size
, cp
, sz
);
425 * Append a nil-terminated string to the database entry. This can be
426 * invoked multiple times. The database entry will be nil-terminated as
427 * well; if invoked multiple times, a space is put between strings.
430 dbt_append(DBT
*key
, size_t *ksz
, const char *cp
)
434 if (0 == (sz
= strlen(cp
)))
440 ((char *)key
->data
)[(int)key
->size
- 1] = ' ';
442 dbt_appendb(key
, ksz
, cp
, sz
+ 1);
450 const char *start
, *end
;
454 if (SEC_SYNOPSIS
!= n
->sec
)
456 if (NULL
== (n
= n
->child
) || MDOC_TEXT
!= n
->type
)
460 * Only consider those `Fd' macro fields that begin with an
461 * "inclusion" token (versus, e.g., #define).
463 if (strcmp("#include", n
->string
))
466 if (NULL
== (n
= n
->next
) || MDOC_TEXT
!= n
->type
)
470 * Strip away the enclosing angle brackets and make sure we're
475 if ('<' == *start
|| '"' == *start
)
478 if (0 == (sz
= strlen(start
)))
481 end
= &start
[(int)sz
- 1];
482 if ('>' == *end
|| '"' == *end
)
486 dbt_appendb(key
, ksz
, start
, end
- start
+ 1);
487 dbt_appendb(key
, ksz
, &nil
, 1);
489 fl
= MANDOC_INCLUDES
;
490 memcpy(val
->data
, &fl
, 4);
499 if (SEC_SYNOPSIS
!= n
->sec
)
501 if (NULL
== n
->child
|| MDOC_TEXT
!= n
->child
->type
)
504 dbt_append(key
, ksz
, n
->child
->string
);
505 fl
= MANDOC_INCLUDES
;
506 memcpy(val
->data
, &fl
, 4);
516 if (SEC_SYNOPSIS
!= n
->sec
)
518 if (NULL
== n
->child
|| MDOC_TEXT
!= n
->child
->type
)
521 /* .Fn "struct type *arg" "foo" */
523 cp
= strrchr(n
->child
->string
, ' ');
525 cp
= n
->child
->string
;
527 /* Strip away pointer symbol. */
532 dbt_append(key
, ksz
, cp
);
533 fl
= MANDOC_FUNCTION
;
534 memcpy(val
->data
, &fl
, 4);
542 const char *start
, *end
;
546 if (SEC_SYNOPSIS
!= n
->sec
)
548 if (MDOC_Vt
== n
->tok
&& MDOC_BODY
!= n
->type
)
550 if (NULL
== n
->last
|| MDOC_TEXT
!= n
->last
->type
)
554 * Strip away leading pointer symbol '*' and trailing ';'.
557 start
= n
->last
->string
;
559 while ('*' == *start
)
562 if (0 == (sz
= strlen(start
)))
565 end
= &start
[sz
- 1];
566 while (end
> start
&& ';' == *end
)
573 dbt_appendb(key
, ksz
, start
, end
- start
+ 1);
574 dbt_appendb(key
, ksz
, &nil
, 1);
575 fl
= MANDOC_VARIABLE
;
576 memcpy(val
->data
, &fl
, 4);
585 if (SEC_SYNOPSIS
!= n
->sec
|| MDOC_HEAD
!= n
->type
)
587 if (NULL
== n
->child
|| MDOC_TEXT
!= n
->child
->type
)
590 dbt_append(key
, ksz
, n
->child
->string
);
591 fl
= MANDOC_FUNCTION
;
592 memcpy(val
->data
, &fl
, 4);
601 if (SEC_NAME
== n
->sec
) {
602 for (n
= n
->child
; n
; n
= n
->next
) {
603 if (MDOC_TEXT
!= n
->type
)
605 dbt_append(key
, ksz
, n
->string
);
608 memcpy(val
->data
, &fl
, 4);
610 } else if (SEC_SYNOPSIS
!= n
->sec
|| MDOC_HEAD
!= n
->type
)
613 for (n
= n
->child
; n
; n
= n
->next
) {
614 if (MDOC_TEXT
!= n
->type
)
616 dbt_append(key
, ksz
, n
->string
);
620 memcpy(val
->data
, &fl
, 4);
624 dbt_put(DB
*db
, const char *dbn
, DBT
*key
, DBT
*val
)
631 assert(8 == val
->size
);
634 if (0 == (*db
->put
)(db
, key
, val
, 0))
638 exit((int)MANDOCLEVEL_SYSERR
);
643 * Call out to per-macro handlers after clearing the persistent database
644 * key. If the macro sets the database key, flush it to the database.
647 pmdoc_node(MDOC_ARGS
)
663 if (NULL
== mdocs
[n
->tok
])
667 (*mdocs
[n
->tok
])(db
, dbn
, key
, ksz
, val
, n
);
669 dbt_put(db
, dbn
, key
, val
);
675 pmdoc_node(db
, dbn
, key
, ksz
, val
, n
->child
);
676 pmdoc_node(db
, dbn
, key
, ksz
, val
, n
->next
);
682 const struct man_node
*head
, *body
;
692 * We're only searching for one thing: the first text child in
693 * the BODY of a NAME section. Since we don't keep track of
694 * sections in -man, run some hoops to find out whether we're in
695 * the correct section or not.
698 if (MAN_BODY
== n
->type
&& MAN_SH
== n
->tok
) {
700 assert(body
->parent
);
701 if (NULL
!= (head
= body
->parent
->head
) &&
703 NULL
!= (head
= (head
->child
)) &&
704 MAN_TEXT
== head
->type
&&
705 0 == strcmp(head
->string
, "NAME") &&
706 NULL
!= (body
= body
->child
) &&
707 MAN_TEXT
== body
->type
) {
711 memcpy(val
->data
, &fl
, 4);
713 start
= body
->string
;
716 * Go through a special heuristic dance here.
717 * This is why -man manuals are great!
718 * Conventionally, one or more manual names are
719 * comma-specified prior to a whitespace, then a
720 * dash, then a description. Try to puzzle out
721 * the name parts here.
725 sz
= strcspn(start
, " ,");
726 if ('\0' == start
[(int)sz
])
730 dbt_appendb(key
, ksz
, start
, sz
);
731 dbt_appendb(key
, ksz
, &nil
, 1);
733 dbt_put(db
, dbn
, key
, val
);
735 if (' ' == start
[(int)sz
])
738 assert(',' == start
[(int)sz
]);
739 start
+= (int)sz
+ 1;
740 while (' ' == *start
)
748 if (pman_node(db
, dbn
, key
, ksz
, val
, n
->child
))
750 if (pman_node(db
, dbn
, key
, ksz
, val
, n
->next
))
757 pman(DB
*db
, const char *dbn
,
758 DBT
*key
, size_t *ksz
, DBT
*val
,
759 const char *path
, struct man
*m
)
762 pman_node(db
, dbn
, key
, ksz
, val
, man_node(m
));
767 pmdoc(DB
*db
, const char *dbn
,
768 DBT
*key
, size_t *ksz
, DBT
*val
,
769 const char *path
, struct mdoc
*m
)
772 pmdoc_node(db
, dbn
, key
, ksz
, val
, mdoc_node(m
));
779 fprintf(stderr
, "usage: %s "