]>
git.cameronkatri.com Git - mandoc.git/blob - mandoc-db.c
1 /* $Id: mandoc-db.c,v 1.16 2011/05/03 10:08:09 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
55 #define MAN_ARGS DB *db, \
57 DBT *key, size_t *ksz, \
59 DBT *rval, size_t *rsz, \
60 const struct man_node *n
61 #define MDOC_ARGS DB *db, \
63 DBT *key, size_t *ksz, \
65 DBT *rval, size_t *rsz, \
66 const struct mdoc_node *n
68 static void dbt_append(DBT
*, size_t *, const char *);
69 static void dbt_appendb(DBT
*, size_t *,
70 const void *, size_t);
71 static void dbt_init(DBT
*, size_t *);
72 static void dbt_put(DB
*, const char *, DBT
*, DBT
*);
73 static void usage(void);
74 static void pman(DB
*, const char *, DBT
*, size_t *,
75 DBT
*, DBT
*, size_t *, struct man
*);
76 static int pman_node(MAN_ARGS
);
77 static void pmdoc(DB
*, const char *, DBT
*, size_t *,
78 DBT
*, DBT
*, size_t *, struct mdoc
*);
79 static void pmdoc_node(MDOC_ARGS
);
80 static void pmdoc_Fd(MDOC_ARGS
);
81 static void pmdoc_In(MDOC_ARGS
);
82 static void pmdoc_Fn(MDOC_ARGS
);
83 static void pmdoc_Fo(MDOC_ARGS
);
84 static void pmdoc_Nd(MDOC_ARGS
);
85 static void pmdoc_Nm(MDOC_ARGS
);
86 static void pmdoc_St(MDOC_ARGS
);
87 static void pmdoc_Vt(MDOC_ARGS
);
89 typedef void (*pmdoc_nf
)(MDOC_ARGS
);
91 static const char *progname
;
93 static const pmdoc_nf mdocs
[MDOC_MAX
] = {
219 main(int argc
, char *argv
[])
221 struct mparse
*mp
; /* parse sequence */
222 struct mdoc
*mdoc
; /* resulting mdoc */
223 struct man
*man
; /* resulting man */
224 char *fn
; /* current file being parsed */
225 const char *msec
, /* manual section */
226 *mtitle
, /* manual title */
227 *dir
; /* result dir (default: cwd) */
228 char ibuf
[MAXPATHLEN
], /* index fname */
229 ibbuf
[MAXPATHLEN
], /* index backup fname */
230 fbuf
[MAXPATHLEN
], /* btree fname */
231 fbbuf
[MAXPATHLEN
]; /* btree backup fname */
233 DB
*idx
, /* index database */
234 *db
; /* keyword database */
235 DBT rkey
, rval
, /* recno entries */
236 key
, val
; /* persistent keyword entries */
238 ksz
, rsz
; /* entry buffer size */
239 char vbuf
[8]; /* stringified record number */
240 BTREEINFO info
; /* btree configuration */
241 recno_t rec
; /* current record number */
245 progname
= strrchr(argv
[0], '/');
246 if (progname
== NULL
)
253 while (-1 != (ch
= getopt(argc
, argv
, "d:")))
260 return((int)MANDOCLEVEL_BADARG
);
267 * Set up temporary file-names into which we're going to write
268 * all of our data (both for the index and database). These
269 * will be securely renamed to the real file-names after we've
270 * written all of our data.
273 ibuf
[0] = ibuf
[MAXPATHLEN
- 2] =
274 ibbuf
[0] = ibbuf
[MAXPATHLEN
- 2] =
275 fbuf
[0] = fbuf
[MAXPATHLEN
- 2] =
276 fbbuf
[0] = fbbuf
[MAXPATHLEN
- 2] = '\0';
278 strlcat(fbuf
, dir
, MAXPATHLEN
);
279 strlcat(fbuf
, MANDOC_DB
, MAXPATHLEN
);
281 strlcat(fbbuf
, fbuf
, MAXPATHLEN
);
282 strlcat(fbbuf
, "~", MAXPATHLEN
);
284 strlcat(ibuf
, dir
, MAXPATHLEN
);
285 strlcat(ibuf
, MANDOC_IDX
, MAXPATHLEN
);
287 strlcat(ibbuf
, ibuf
, MAXPATHLEN
);
288 strlcat(ibbuf
, "~", MAXPATHLEN
);
290 if ('\0' != fbuf
[MAXPATHLEN
- 2] ||
291 '\0' != fbbuf
[MAXPATHLEN
- 2] ||
292 '\0' != ibuf
[MAXPATHLEN
- 2] ||
293 '\0' != ibbuf
[MAXPATHLEN
- 2]) {
294 fprintf(stderr
, "%s: Path too long\n", progname
);
295 exit((int)MANDOCLEVEL_SYSERR
);
299 * For the keyword database, open a BTREE database that allows
300 * duplicates. For the index database, use a standard RECNO
304 memset(&info
, 0, sizeof(BTREEINFO
));
306 db
= dbopen(fbbuf
, MANDOC_FLAGS
, 0644, DB_BTREE
, &info
);
310 exit((int)MANDOCLEVEL_SYSERR
);
313 idx
= dbopen(ibbuf
, MANDOC_FLAGS
, 0644, DB_RECNO
, NULL
);
318 exit((int)MANDOCLEVEL_SYSERR
);
322 * Try parsing the manuals given on the command line. If we
323 * totally fail, then just keep on going. Take resulting trees
324 * and push them down into the database code.
325 * Use the auto-parser and don't report any errors.
328 mp
= mparse_alloc(MPARSE_AUTO
, MANDOCLEVEL_FATAL
, NULL
, NULL
);
330 memset(&key
, 0, sizeof(DBT
));
331 memset(&val
, 0, sizeof(DBT
));
332 memset(&rkey
, 0, sizeof(DBT
));
333 memset(&rval
, 0, sizeof(DBT
));
335 val
.size
= sizeof(vbuf
);
337 rkey
.size
= sizeof(recno_t
);
342 while (NULL
!= (fn
= *argv
++)) {
345 /* Parse and get (non-empty) AST. */
347 if (mparse_readfd(mp
, -1, fn
) >= MANDOCLEVEL_FATAL
) {
348 fprintf(stderr
, "%s: Parse failure\n", fn
);
351 mparse_result(mp
, &mdoc
, &man
);
352 if (NULL
== mdoc
&& NULL
== man
)
355 /* Manual section: can be empty string. */
357 msec
= NULL
!= mdoc
?
358 mdoc_meta(mdoc
)->msec
:
360 mtitle
= NULL
!= mdoc
?
361 mdoc_meta(mdoc
)->title
:
362 man_meta(man
)->title
;
368 * The index record value consists of a nil-terminated
369 * filename, a nil-terminated manual section, and a
370 * nil-terminated description. Since the description
371 * may not be set, we set a sentinel to see if we're
372 * going to write a nil byte in its place.
375 dbt_init(&rval
, &rsz
);
376 dbt_appendb(&rval
, &rsz
, fn
, strlen(fn
) + 1);
377 dbt_appendb(&rval
, &rsz
, msec
, strlen(msec
) + 1);
378 dbt_appendb(&rval
, &rsz
, mtitle
, strlen(mtitle
) + 1);
381 /* Fix the record number in the btree value. */
383 memset(val
.data
, 0, sizeof(uint32_t));
384 memcpy(val
.data
+ 4, &rec
, sizeof(uint32_t));
387 pmdoc(db
, fbbuf
, &key
, &ksz
,
388 &val
, &rval
, &rsz
, mdoc
);
390 pman(db
, fbbuf
, &key
, &ksz
,
391 &val
, &rval
, &rsz
, man
);
394 * Apply this to the index. If we haven't had a
395 * description set, put an empty one in now.
399 dbt_appendb(&rval
, &rsz
, "", 1);
402 dbt_put(idx
, ibbuf
, &rkey
, &rval
);
404 printf("Indexed: %s\n", fn
);
416 /* Atomically replace the file with our temporary one. */
418 if (-1 == rename(fbbuf
, fbuf
))
420 if (-1 == rename(ibbuf
, ibuf
))
423 return((int)MANDOCLEVEL_OK
);
427 * Initialise the stored database key whose data buffer is shared
428 * between uses (as the key must sometimes be constructed from an array
432 dbt_init(DBT
*key
, size_t *ksz
)
436 assert(0 == key
->size
);
437 assert(NULL
== key
->data
);
438 key
->data
= mandoc_malloc(MANDOC_BUFSZ
);
446 * Append a binary value to a database entry. This can be invoked
447 * multiple times; the buffer is automatically resized.
450 dbt_appendb(DBT
*key
, size_t *ksz
, const void *cp
, size_t sz
)
455 /* Overshoot by MANDOC_BUFSZ. */
457 while (key
->size
+ sz
>= *ksz
) {
458 *ksz
= key
->size
+ sz
+ MANDOC_BUFSZ
;
459 key
->data
= mandoc_realloc(key
->data
, *ksz
);
463 dstp
= key
->data
+ (int)key
->size
;
465 while (NULL
!= (endp
= memchr(cp
, '\\', sz
))) {
467 memcpy(dstp
, cp
, ssz
);
474 /* FIXME: expects nil-terminated string! */
475 esc
= mandoc_escape((const char **)&endp
, NULL
, NULL
);
479 /* Nil-terminate this point. */
483 case (ESCAPE_PREDEF
):
485 case (ESCAPE_SPECIAL
):
494 memcpy(dstp
, cp
, ssz
);
504 memcpy(key
->data
+ (int)key
->size
, cp
, sz
);
509 * Append a nil-terminated string to the database entry. This can be
510 * invoked multiple times. The database entry will be nil-terminated as
511 * well; if invoked multiple times, a space is put between strings.
514 dbt_append(DBT
*key
, size_t *ksz
, const char *cp
)
518 if (0 == (sz
= strlen(cp
)))
524 ((char *)key
->data
)[(int)key
->size
- 1] = ' ';
526 dbt_appendb(key
, ksz
, cp
, sz
+ 1);
534 const char *start
, *end
;
537 if (SEC_SYNOPSIS
!= n
->sec
)
539 if (NULL
== (n
= n
->child
) || MDOC_TEXT
!= n
->type
)
543 * Only consider those `Fd' macro fields that begin with an
544 * "inclusion" token (versus, e.g., #define).
546 if (strcmp("#include", n
->string
))
549 if (NULL
== (n
= n
->next
) || MDOC_TEXT
!= n
->type
)
553 * Strip away the enclosing angle brackets and make sure we're
558 if ('<' == *start
|| '"' == *start
)
561 if (0 == (sz
= strlen(start
)))
564 end
= &start
[(int)sz
- 1];
565 if ('>' == *end
|| '"' == *end
)
568 dbt_appendb(key
, ksz
, start
, end
- start
+ 1);
569 dbt_appendb(key
, ksz
, "", 1);
571 fl
= MANDOC_INCLUDES
;
572 memcpy(val
->data
, &fl
, 4);
581 if (SEC_SYNOPSIS
!= n
->sec
)
583 if (NULL
== n
->child
|| MDOC_TEXT
!= n
->child
->type
)
586 dbt_append(key
, ksz
, n
->child
->string
);
587 fl
= MANDOC_INCLUDES
;
588 memcpy(val
->data
, &fl
, 4);
598 if (SEC_SYNOPSIS
!= n
->sec
)
600 if (NULL
== n
->child
|| MDOC_TEXT
!= n
->child
->type
)
603 /* .Fn "struct type *arg" "foo" */
605 cp
= strrchr(n
->child
->string
, ' ');
607 cp
= n
->child
->string
;
609 /* Strip away pointer symbol. */
614 dbt_append(key
, ksz
, cp
);
615 fl
= MANDOC_FUNCTION
;
616 memcpy(val
->data
, &fl
, 4);
625 if (SEC_STANDARDS
!= n
->sec
)
627 if (NULL
== n
->child
|| MDOC_TEXT
!= n
->child
->type
)
630 dbt_append(key
, ksz
, n
->child
->string
);
631 fl
= MANDOC_STANDARD
;
632 memcpy(val
->data
, &fl
, 4);
643 if (SEC_SYNOPSIS
!= n
->sec
)
645 if (MDOC_Vt
== n
->tok
&& MDOC_BODY
!= n
->type
)
647 if (NULL
== n
->last
|| MDOC_TEXT
!= n
->last
->type
)
651 * Strip away leading pointer symbol '*' and trailing ';'.
654 start
= n
->last
->string
;
656 while ('*' == *start
)
659 if (0 == (sz
= strlen(start
)))
662 if (';' == start
[sz
- 1])
668 dbt_appendb(key
, ksz
, start
, sz
);
669 dbt_appendb(key
, ksz
, "", 1);
671 fl
= MANDOC_VARIABLE
;
672 memcpy(val
->data
, &fl
, 4);
681 if (SEC_SYNOPSIS
!= n
->sec
|| MDOC_HEAD
!= n
->type
)
683 if (NULL
== n
->child
|| MDOC_TEXT
!= n
->child
->type
)
686 dbt_append(key
, ksz
, n
->child
->string
);
687 fl
= MANDOC_FUNCTION
;
688 memcpy(val
->data
, &fl
, 4);
698 for (first
= 1, n
= n
->child
; n
; n
= n
->next
) {
699 if (MDOC_TEXT
!= n
->type
)
702 dbt_appendb(rval
, rsz
, n
->string
, strlen(n
->string
) + 1);
704 dbt_append(rval
, rsz
, n
->string
);
715 if (SEC_NAME
== n
->sec
) {
716 for (n
= n
->child
; n
; n
= n
->next
) {
717 if (MDOC_TEXT
!= n
->type
)
719 dbt_append(key
, ksz
, n
->string
);
722 memcpy(val
->data
, &fl
, 4);
724 } else if (SEC_SYNOPSIS
!= n
->sec
|| MDOC_HEAD
!= n
->type
)
727 for (n
= n
->child
; n
; n
= n
->next
) {
728 if (MDOC_TEXT
!= n
->type
)
730 dbt_append(key
, ksz
, n
->string
);
734 memcpy(val
->data
, &fl
, 4);
738 dbt_put(DB
*db
, const char *dbn
, DBT
*key
, DBT
*val
)
748 if (0 == (*db
->put
)(db
, key
, val
, 0))
752 exit((int)MANDOCLEVEL_SYSERR
);
757 * Call out to per-macro handlers after clearing the persistent database
758 * key. If the macro sets the database key, flush it to the database.
761 pmdoc_node(MDOC_ARGS
)
777 if (NULL
== mdocs
[n
->tok
])
782 (*mdocs
[n
->tok
])(db
, dbn
, key
, ksz
, val
, rval
, rsz
, n
);
783 dbt_put(db
, dbn
, key
, val
);
789 pmdoc_node(db
, dbn
, key
, ksz
, val
, rval
, rsz
, n
->child
);
790 pmdoc_node(db
, dbn
, key
, ksz
, val
, rval
, rsz
, n
->next
);
796 const struct man_node
*head
, *body
;
797 const char *start
, *sv
;
805 * We're only searching for one thing: the first text child in
806 * the BODY of a NAME section. Since we don't keep track of
807 * sections in -man, run some hoops to find out whether we're in
808 * the correct section or not.
811 if (MAN_BODY
== n
->type
&& MAN_SH
== n
->tok
) {
813 assert(body
->parent
);
814 if (NULL
!= (head
= body
->parent
->head
) &&
816 NULL
!= (head
= (head
->child
)) &&
817 MAN_TEXT
== head
->type
&&
818 0 == strcmp(head
->string
, "NAME") &&
819 NULL
!= (body
= body
->child
) &&
820 MAN_TEXT
== body
->type
) {
823 memcpy(val
->data
, &fl
, 4);
825 assert(body
->string
);
826 start
= sv
= body
->string
;
829 * Go through a special heuristic dance here.
830 * This is why -man manuals are great!
831 * Conventionally, one or more manual names are
832 * comma-specified prior to a whitespace, then a
833 * dash, then a description. Try to puzzle out
834 * the name parts here.
838 sz
= strcspn(start
, " ,");
839 if ('\0' == start
[(int)sz
])
843 dbt_appendb(key
, ksz
, start
, sz
);
844 dbt_appendb(key
, ksz
, "", 1);
846 dbt_put(db
, dbn
, key
, val
);
848 if (' ' == start
[(int)sz
]) {
849 start
+= (int)sz
+ 1;
853 assert(',' == start
[(int)sz
]);
854 start
+= (int)sz
+ 1;
855 while (' ' == *start
)
861 dbt_append(key
, ksz
, start
);
865 while (' ' == *start
)
868 if ('\\' == *start
&& '-' == *(start
+ 1))
870 else if ('-' == *start
)
873 while (' ' == *start
)
876 dbt_appendb(rval
, rsz
, start
, strlen(start
) + 1);
880 if (pman_node(db
, dbn
, key
, ksz
, val
, rval
, rsz
, n
->child
))
882 if (pman_node(db
, dbn
, key
, ksz
, val
, rval
, rsz
, n
->next
))
889 pman(DB
*db
, const char *dbn
, DBT
*key
, size_t *ksz
,
890 DBT
*val
, DBT
*rval
, size_t *rsz
, struct man
*m
)
893 pman_node(db
, dbn
, key
, ksz
, val
, rval
, rsz
, man_node(m
));
898 pmdoc(DB
*db
, const char *dbn
, DBT
*key
, size_t *ksz
,
899 DBT
*val
, DBT
*rval
, size_t *rsz
, struct mdoc
*m
)
902 pmdoc_node(db
, dbn
, key
, ksz
, val
, rval
, rsz
, mdoc_node(m
));
909 fprintf(stderr
, "usage: %s "