]>
git.cameronkatri.com Git - mandoc.git/blob - mandoc-db.c
1 /* $Id: mandoc-db.c,v 1.18 2011/05/04 08:21:17 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
57 #define MAN_ARGS DB *db, \
59 DBT *key, size_t *ksz, \
61 DBT *rval, size_t *rsz, \
62 const struct man_node *n
63 #define MDOC_ARGS DB *db, \
65 DBT *key, size_t *ksz, \
67 DBT *rval, size_t *rsz, \
68 const struct mdoc_node *n
70 static void dbt_append(DBT
*, size_t *, const char *);
71 static void dbt_appendb(DBT
*, size_t *,
72 const void *, size_t);
73 static void dbt_init(DBT
*, size_t *);
74 static void dbt_put(DB
*, const char *, DBT
*, DBT
*);
75 static void usage(void);
76 static void pman(DB
*, const char *, DBT
*, size_t *,
77 DBT
*, DBT
*, size_t *, struct man
*);
78 static int pman_node(MAN_ARGS
);
79 static void pmdoc(DB
*, const char *, DBT
*, size_t *,
80 DBT
*, DBT
*, size_t *, struct mdoc
*);
81 static void pmdoc_node(MDOC_ARGS
);
82 static void pmdoc_An(MDOC_ARGS
);
83 static void pmdoc_Cd(MDOC_ARGS
);
84 static void pmdoc_Fd(MDOC_ARGS
);
85 static void pmdoc_In(MDOC_ARGS
);
86 static void pmdoc_Fn(MDOC_ARGS
);
87 static void pmdoc_Fo(MDOC_ARGS
);
88 static void pmdoc_Nd(MDOC_ARGS
);
89 static void pmdoc_Nm(MDOC_ARGS
);
90 static void pmdoc_St(MDOC_ARGS
);
91 static void pmdoc_Vt(MDOC_ARGS
);
93 typedef void (*pmdoc_nf
)(MDOC_ARGS
);
95 static const char *progname
;
97 static const pmdoc_nf mdocs
[MDOC_MAX
] = {
223 main(int argc
, char *argv
[])
225 struct mparse
*mp
; /* parse sequence */
226 struct mdoc
*mdoc
; /* resulting mdoc */
227 struct man
*man
; /* resulting man */
228 char *fn
; /* current file being parsed */
229 const char *msec
, /* manual section */
230 *mtitle
, /* manual title */
231 *dir
; /* result dir (default: cwd) */
232 char ibuf
[MAXPATHLEN
], /* index fname */
233 ibbuf
[MAXPATHLEN
], /* index backup fname */
234 fbuf
[MAXPATHLEN
], /* btree fname */
235 fbbuf
[MAXPATHLEN
]; /* btree backup fname */
237 DB
*idx
, /* index database */
238 *db
; /* keyword database */
239 DBT rkey
, rval
, /* recno entries */
240 key
, val
; /* persistent keyword entries */
242 ksz
, rsz
; /* entry buffer size */
243 char vbuf
[8]; /* stringified record number */
244 BTREEINFO info
; /* btree configuration */
245 recno_t rec
; /* current record number */
249 progname
= strrchr(argv
[0], '/');
250 if (progname
== NULL
)
257 while (-1 != (ch
= getopt(argc
, argv
, "d:")))
264 return((int)MANDOCLEVEL_BADARG
);
271 * Set up temporary file-names into which we're going to write
272 * all of our data (both for the index and database). These
273 * will be securely renamed to the real file-names after we've
274 * written all of our data.
277 ibuf
[0] = ibuf
[MAXPATHLEN
- 2] =
278 ibbuf
[0] = ibbuf
[MAXPATHLEN
- 2] =
279 fbuf
[0] = fbuf
[MAXPATHLEN
- 2] =
280 fbbuf
[0] = fbbuf
[MAXPATHLEN
- 2] = '\0';
282 strlcat(fbuf
, dir
, MAXPATHLEN
);
283 strlcat(fbuf
, MANDOC_DB
, MAXPATHLEN
);
285 strlcat(fbbuf
, fbuf
, MAXPATHLEN
);
286 strlcat(fbbuf
, "~", MAXPATHLEN
);
288 strlcat(ibuf
, dir
, MAXPATHLEN
);
289 strlcat(ibuf
, MANDOC_IDX
, MAXPATHLEN
);
291 strlcat(ibbuf
, ibuf
, MAXPATHLEN
);
292 strlcat(ibbuf
, "~", MAXPATHLEN
);
294 if ('\0' != fbuf
[MAXPATHLEN
- 2] ||
295 '\0' != fbbuf
[MAXPATHLEN
- 2] ||
296 '\0' != ibuf
[MAXPATHLEN
- 2] ||
297 '\0' != ibbuf
[MAXPATHLEN
- 2]) {
298 fprintf(stderr
, "%s: Path too long\n", progname
);
299 exit((int)MANDOCLEVEL_SYSERR
);
303 * For the keyword database, open a BTREE database that allows
304 * duplicates. For the index database, use a standard RECNO
308 memset(&info
, 0, sizeof(BTREEINFO
));
310 db
= dbopen(fbbuf
, MANDOC_FLAGS
, 0644, DB_BTREE
, &info
);
314 exit((int)MANDOCLEVEL_SYSERR
);
317 idx
= dbopen(ibbuf
, MANDOC_FLAGS
, 0644, DB_RECNO
, NULL
);
322 exit((int)MANDOCLEVEL_SYSERR
);
326 * Try parsing the manuals given on the command line. If we
327 * totally fail, then just keep on going. Take resulting trees
328 * and push them down into the database code.
329 * Use the auto-parser and don't report any errors.
332 mp
= mparse_alloc(MPARSE_AUTO
, MANDOCLEVEL_FATAL
, NULL
, NULL
);
334 memset(&key
, 0, sizeof(DBT
));
335 memset(&val
, 0, sizeof(DBT
));
336 memset(&rkey
, 0, sizeof(DBT
));
337 memset(&rval
, 0, sizeof(DBT
));
339 val
.size
= sizeof(vbuf
);
341 rkey
.size
= sizeof(recno_t
);
346 while (NULL
!= (fn
= *argv
++)) {
349 /* Parse and get (non-empty) AST. */
351 if (mparse_readfd(mp
, -1, fn
) >= MANDOCLEVEL_FATAL
) {
352 fprintf(stderr
, "%s: Parse failure\n", fn
);
355 mparse_result(mp
, &mdoc
, &man
);
356 if (NULL
== mdoc
&& NULL
== man
)
359 /* Manual section: can be empty string. */
361 msec
= NULL
!= mdoc
?
362 mdoc_meta(mdoc
)->msec
:
364 mtitle
= NULL
!= mdoc
?
365 mdoc_meta(mdoc
)->title
:
366 man_meta(man
)->title
;
372 * The index record value consists of a nil-terminated
373 * filename, a nil-terminated manual section, and a
374 * nil-terminated description. Since the description
375 * may not be set, we set a sentinel to see if we're
376 * going to write a nil byte in its place.
379 dbt_init(&rval
, &rsz
);
380 dbt_appendb(&rval
, &rsz
, fn
, strlen(fn
) + 1);
381 dbt_appendb(&rval
, &rsz
, msec
, strlen(msec
) + 1);
382 dbt_appendb(&rval
, &rsz
, mtitle
, strlen(mtitle
) + 1);
385 /* Fix the record number in the btree value. */
387 memset(val
.data
, 0, sizeof(uint32_t));
388 memcpy(val
.data
+ 4, &rec
, sizeof(uint32_t));
391 pmdoc(db
, fbbuf
, &key
, &ksz
,
392 &val
, &rval
, &rsz
, mdoc
);
394 pman(db
, fbbuf
, &key
, &ksz
,
395 &val
, &rval
, &rsz
, man
);
398 * Apply this to the index. If we haven't had a
399 * description set, put an empty one in now.
403 dbt_appendb(&rval
, &rsz
, "", 1);
406 dbt_put(idx
, ibbuf
, &rkey
, &rval
);
408 printf("Indexed: %s\n", fn
);
420 /* Atomically replace the file with our temporary one. */
422 if (-1 == rename(fbbuf
, fbuf
))
424 if (-1 == rename(ibbuf
, ibuf
))
427 return((int)MANDOCLEVEL_OK
);
431 * Initialise the stored database key whose data buffer is shared
432 * between uses (as the key must sometimes be constructed from an array
436 dbt_init(DBT
*key
, size_t *ksz
)
440 assert(0 == key
->size
);
441 assert(NULL
== key
->data
);
442 key
->data
= mandoc_malloc(MANDOC_BUFSZ
);
450 * Append a binary value to a database entry. This can be invoked
451 * multiple times; the buffer is automatically resized.
454 dbt_appendb(DBT
*key
, size_t *ksz
, const void *cp
, size_t sz
)
459 /* Overshoot by MANDOC_BUFSZ. */
461 while (key
->size
+ sz
>= *ksz
) {
462 *ksz
= key
->size
+ sz
+ MANDOC_BUFSZ
;
463 key
->data
= mandoc_realloc(key
->data
, *ksz
);
467 dstp
= key
->data
+ (int)key
->size
;
469 while (NULL
!= (endp
= memchr(cp
, '\\', sz
))) {
471 memcpy(dstp
, cp
, ssz
);
478 /* FIXME: expects nil-terminated string! */
479 esc
= mandoc_escape((const char **)&endp
, NULL
, NULL
);
483 /* Nil-terminate this point. */
487 case (ESCAPE_PREDEF
):
489 case (ESCAPE_SPECIAL
):
498 memcpy(dstp
, cp
, ssz
);
508 memcpy(key
->data
+ (int)key
->size
, cp
, sz
);
513 * Append a nil-terminated string to the database entry. This can be
514 * invoked multiple times. The database entry will be nil-terminated as
515 * well; if invoked multiple times, a space is put between strings.
518 dbt_append(DBT
*key
, size_t *ksz
, const char *cp
)
522 if (0 == (sz
= strlen(cp
)))
528 ((char *)key
->data
)[(int)key
->size
- 1] = ' ';
530 dbt_appendb(key
, ksz
, cp
, sz
+ 1);
539 if (SEC_AUTHORS
!= n
->sec
)
542 for (n
= n
->child
; n
; n
= n
->next
)
543 if (MDOC_TEXT
== n
->type
)
544 dbt_append(key
, ksz
, n
->string
);
547 memcpy(val
->data
, &fl
, 4);
555 const char *start
, *end
;
558 if (SEC_SYNOPSIS
!= n
->sec
)
560 if (NULL
== (n
= n
->child
) || MDOC_TEXT
!= n
->type
)
564 * Only consider those `Fd' macro fields that begin with an
565 * "inclusion" token (versus, e.g., #define).
567 if (strcmp("#include", n
->string
))
570 if (NULL
== (n
= n
->next
) || MDOC_TEXT
!= n
->type
)
574 * Strip away the enclosing angle brackets and make sure we're
579 if ('<' == *start
|| '"' == *start
)
582 if (0 == (sz
= strlen(start
)))
585 end
= &start
[(int)sz
- 1];
586 if ('>' == *end
|| '"' == *end
)
589 dbt_appendb(key
, ksz
, start
, end
- start
+ 1);
590 dbt_appendb(key
, ksz
, "", 1);
592 fl
= MANDOC_INCLUDES
;
593 memcpy(val
->data
, &fl
, 4);
602 if (SEC_SYNOPSIS
!= n
->sec
)
605 for (n
= n
->child
; n
; n
= n
->next
)
606 if (MDOC_TEXT
== n
->type
)
607 dbt_append(key
, ksz
, n
->string
);
610 memcpy(val
->data
, &fl
, 4);
619 if (SEC_SYNOPSIS
!= n
->sec
)
621 if (NULL
== n
->child
|| MDOC_TEXT
!= n
->child
->type
)
624 dbt_append(key
, ksz
, n
->child
->string
);
625 fl
= MANDOC_INCLUDES
;
626 memcpy(val
->data
, &fl
, 4);
636 if (SEC_SYNOPSIS
!= n
->sec
)
638 if (NULL
== n
->child
|| MDOC_TEXT
!= n
->child
->type
)
641 /* .Fn "struct type *arg" "foo" */
643 cp
= strrchr(n
->child
->string
, ' ');
645 cp
= n
->child
->string
;
647 /* Strip away pointer symbol. */
652 dbt_append(key
, ksz
, cp
);
653 fl
= MANDOC_FUNCTION
;
654 memcpy(val
->data
, &fl
, 4);
663 if (SEC_STANDARDS
!= n
->sec
)
665 if (NULL
== n
->child
|| MDOC_TEXT
!= n
->child
->type
)
668 dbt_append(key
, ksz
, n
->child
->string
);
669 fl
= MANDOC_STANDARD
;
670 memcpy(val
->data
, &fl
, 4);
681 if (SEC_SYNOPSIS
!= n
->sec
)
683 if (MDOC_Vt
== n
->tok
&& MDOC_BODY
!= n
->type
)
685 if (NULL
== n
->last
|| MDOC_TEXT
!= n
->last
->type
)
689 * Strip away leading pointer symbol '*' and trailing ';'.
692 start
= n
->last
->string
;
694 while ('*' == *start
)
697 if (0 == (sz
= strlen(start
)))
700 if (';' == start
[sz
- 1])
706 dbt_appendb(key
, ksz
, start
, sz
);
707 dbt_appendb(key
, ksz
, "", 1);
709 fl
= MANDOC_VARIABLE
;
710 memcpy(val
->data
, &fl
, 4);
719 if (SEC_SYNOPSIS
!= n
->sec
|| MDOC_HEAD
!= n
->type
)
721 if (NULL
== n
->child
|| MDOC_TEXT
!= n
->child
->type
)
724 dbt_append(key
, ksz
, n
->child
->string
);
725 fl
= MANDOC_FUNCTION
;
726 memcpy(val
->data
, &fl
, 4);
736 for (first
= 1, n
= n
->child
; n
; n
= n
->next
) {
737 if (MDOC_TEXT
!= n
->type
)
740 dbt_appendb(rval
, rsz
, n
->string
, strlen(n
->string
) + 1);
742 dbt_append(rval
, rsz
, n
->string
);
753 if (SEC_NAME
== n
->sec
) {
754 for (n
= n
->child
; n
; n
= n
->next
) {
755 if (MDOC_TEXT
!= n
->type
)
757 dbt_append(key
, ksz
, n
->string
);
760 memcpy(val
->data
, &fl
, 4);
762 } else if (SEC_SYNOPSIS
!= n
->sec
|| MDOC_HEAD
!= n
->type
)
765 for (n
= n
->child
; n
; n
= n
->next
) {
766 if (MDOC_TEXT
!= n
->type
)
768 dbt_append(key
, ksz
, n
->string
);
772 memcpy(val
->data
, &fl
, 4);
776 dbt_put(DB
*db
, const char *dbn
, DBT
*key
, DBT
*val
)
786 if (0 == (*db
->put
)(db
, key
, val
, 0))
790 exit((int)MANDOCLEVEL_SYSERR
);
795 * Call out to per-macro handlers after clearing the persistent database
796 * key. If the macro sets the database key, flush it to the database.
799 pmdoc_node(MDOC_ARGS
)
815 if (NULL
== mdocs
[n
->tok
])
820 (*mdocs
[n
->tok
])(db
, dbn
, key
, ksz
, val
, rval
, rsz
, n
);
821 dbt_put(db
, dbn
, key
, val
);
827 pmdoc_node(db
, dbn
, key
, ksz
, val
, rval
, rsz
, n
->child
);
828 pmdoc_node(db
, dbn
, key
, ksz
, val
, rval
, rsz
, n
->next
);
834 const struct man_node
*head
, *body
;
835 const char *start
, *sv
;
843 * We're only searching for one thing: the first text child in
844 * the BODY of a NAME section. Since we don't keep track of
845 * sections in -man, run some hoops to find out whether we're in
846 * the correct section or not.
849 if (MAN_BODY
== n
->type
&& MAN_SH
== n
->tok
) {
851 assert(body
->parent
);
852 if (NULL
!= (head
= body
->parent
->head
) &&
854 NULL
!= (head
= (head
->child
)) &&
855 MAN_TEXT
== head
->type
&&
856 0 == strcmp(head
->string
, "NAME") &&
857 NULL
!= (body
= body
->child
) &&
858 MAN_TEXT
== body
->type
) {
861 memcpy(val
->data
, &fl
, 4);
863 assert(body
->string
);
864 start
= sv
= body
->string
;
867 * Go through a special heuristic dance here.
868 * This is why -man manuals are great!
869 * Conventionally, one or more manual names are
870 * comma-specified prior to a whitespace, then a
871 * dash, then a description. Try to puzzle out
872 * the name parts here.
876 sz
= strcspn(start
, " ,");
877 if ('\0' == start
[(int)sz
])
881 dbt_appendb(key
, ksz
, start
, sz
);
882 dbt_appendb(key
, ksz
, "", 1);
884 dbt_put(db
, dbn
, key
, val
);
886 if (' ' == start
[(int)sz
]) {
887 start
+= (int)sz
+ 1;
891 assert(',' == start
[(int)sz
]);
892 start
+= (int)sz
+ 1;
893 while (' ' == *start
)
899 dbt_append(key
, ksz
, start
);
903 while (' ' == *start
)
906 if ('\\' == *start
&& '-' == *(start
+ 1))
908 else if ('-' == *start
)
911 while (' ' == *start
)
914 dbt_appendb(rval
, rsz
, start
, strlen(start
) + 1);
918 if (pman_node(db
, dbn
, key
, ksz
, val
, rval
, rsz
, n
->child
))
920 if (pman_node(db
, dbn
, key
, ksz
, val
, rval
, rsz
, n
->next
))
927 pman(DB
*db
, const char *dbn
, DBT
*key
, size_t *ksz
,
928 DBT
*val
, DBT
*rval
, size_t *rsz
, struct man
*m
)
931 pman_node(db
, dbn
, key
, ksz
, val
, rval
, rsz
, man_node(m
));
936 pmdoc(DB
*db
, const char *dbn
, DBT
*key
, size_t *ksz
,
937 DBT
*val
, DBT
*rval
, size_t *rsz
, struct mdoc
*m
)
940 pmdoc_node(db
, dbn
, key
, ksz
, val
, rval
, rsz
, mdoc_node(m
));
947 fprintf(stderr
, "usage: %s "