]>
git.cameronkatri.com Git - mandoc.git/blob - makewhatis.c
1 /* $Id: makewhatis.c,v 1.15 2011/07/01 12:02:44 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>
41 #define MANDOC_DB "mandoc.db"
42 #define MANDOC_IDX "mandoc.index"
43 #define MANDOC_BUFSZ BUFSIZ
44 #define MANDOC_FLAGS O_CREAT|O_TRUNC|O_RDWR
46 /* Bit-fields. See makewhatis.1. */
48 #define TYPE_NAME 0x01
49 #define TYPE_FUNCTION 0x02
50 #define TYPE_UTILITY 0x04
51 #define TYPE_INCLUDES 0x08
52 #define TYPE_VARIABLE 0x10
53 #define TYPE_STANDARD 0x20
54 #define TYPE_AUTHOR 0x40
55 #define TYPE_CONFIG 0x80
56 #define TYPE_DESC 0x100
57 #define TYPE_XREF 0x200
58 #define TYPE_PATH 0x400
59 #define TYPE_ENV 0x800
61 /* Buffer for storing growable data. */
69 #define MAN_ARGS DB *hash, \
72 const struct man_node *n
73 #define MDOC_ARGS DB *hash, \
76 const struct mdoc_node *n, \
77 const struct mdoc_meta *m
79 static void buf_appendmdoc(struct buf
*,
80 const struct mdoc_node
*, int);
81 static void buf_append(struct buf
*, const char *);
82 static void buf_appendb(struct buf
*,
83 const void *, size_t);
84 static void dbt_put(DB
*, const char *, DBT
*, DBT
*);
85 static void hash_put(DB
*, const struct buf
*, int);
86 static int pman_node(MAN_ARGS
);
87 static void pmdoc_node(MDOC_ARGS
);
88 static void pmdoc_An(MDOC_ARGS
);
89 static void pmdoc_Cd(MDOC_ARGS
);
90 static void pmdoc_Ev(MDOC_ARGS
);
91 static void pmdoc_Fd(MDOC_ARGS
);
92 static void pmdoc_In(MDOC_ARGS
);
93 static void pmdoc_Fn(MDOC_ARGS
);
94 static void pmdoc_Fo(MDOC_ARGS
);
95 static void pmdoc_Nd(MDOC_ARGS
);
96 static void pmdoc_Nm(MDOC_ARGS
);
97 static void pmdoc_Pa(MDOC_ARGS
);
98 static void pmdoc_St(MDOC_ARGS
);
99 static void pmdoc_Vt(MDOC_ARGS
);
100 static void pmdoc_Xr(MDOC_ARGS
);
101 static void usage(void);
103 typedef void (*pmdoc_nf
)(MDOC_ARGS
);
105 static const pmdoc_nf mdocs
[MDOC_MAX
] = {
230 static const char *progname
;
233 main(int argc
, char *argv
[])
235 struct mparse
*mp
; /* parse sequence */
236 struct mdoc
*mdoc
; /* resulting mdoc */
237 struct man
*man
; /* resulting man */
238 char *fn
; /* current file being parsed */
239 const char *msec
, /* manual section */
240 *mtitle
, /* manual title */
241 *arch
, /* manual architecture */
242 *dir
; /* result dir (default: cwd) */
243 char ibuf
[MAXPATHLEN
], /* index fname */
244 ibbuf
[MAXPATHLEN
], /* index backup fname */
245 fbuf
[MAXPATHLEN
], /* btree fname */
246 fbbuf
[MAXPATHLEN
], /* btree backup fname */
247 vbuf
[8]; /* stringified record number */
249 DB
*idx
, /* index database */
250 *db
, /* keyword database */
251 *hash
; /* temporary keyword hashtable */
254 BTREEINFO info
; /* btree configuration */
255 recno_t rec
; /* current record number */
256 struct buf buf
, /* keyword buffer */
257 dbuf
; /* description buffer */
261 progname
= strrchr(argv
[0], '/');
262 if (progname
== NULL
)
270 while (-1 != (ch
= getopt(argc
, argv
, "d:v")))
280 return((int)MANDOCLEVEL_BADARG
);
287 * Set up temporary file-names into which we're going to write
288 * all of our data (both for the index and database). These
289 * will be securely renamed to the real file-names after we've
290 * written all of our data.
293 ibuf
[0] = ibuf
[MAXPATHLEN
- 2] =
294 ibbuf
[0] = ibbuf
[MAXPATHLEN
- 2] =
295 fbuf
[0] = fbuf
[MAXPATHLEN
- 2] =
296 fbbuf
[0] = fbbuf
[MAXPATHLEN
- 2] = '\0';
298 strlcat(fbuf
, dir
, MAXPATHLEN
);
299 strlcat(fbuf
, MANDOC_DB
, MAXPATHLEN
);
301 strlcat(fbbuf
, fbuf
, MAXPATHLEN
);
302 strlcat(fbbuf
, "~", MAXPATHLEN
);
304 strlcat(ibuf
, dir
, MAXPATHLEN
);
305 strlcat(ibuf
, MANDOC_IDX
, MAXPATHLEN
);
307 strlcat(ibbuf
, ibuf
, MAXPATHLEN
);
308 strlcat(ibbuf
, "~", MAXPATHLEN
);
310 if ('\0' != fbuf
[MAXPATHLEN
- 2] ||
311 '\0' != fbbuf
[MAXPATHLEN
- 2] ||
312 '\0' != ibuf
[MAXPATHLEN
- 2] ||
313 '\0' != ibbuf
[MAXPATHLEN
- 2]) {
314 fprintf(stderr
, "%s: Path too long\n", dir
);
315 exit((int)MANDOCLEVEL_SYSERR
);
319 * For the keyword database, open a BTREE database that allows
321 * For the index database, use a standard RECNO database type.
324 memset(&info
, 0, sizeof(BTREEINFO
));
326 db
= dbopen(fbbuf
, MANDOC_FLAGS
, 0644, DB_BTREE
, &info
);
330 exit((int)MANDOCLEVEL_SYSERR
);
333 idx
= dbopen(ibbuf
, MANDOC_FLAGS
, 0644, DB_RECNO
, NULL
);
338 exit((int)MANDOCLEVEL_SYSERR
);
342 * Try parsing each manual given on the command line.
343 * If we fail, then emit an error and keep on going.
344 * Take resulting trees and push them down into the database code.
345 * Use the auto-parser and don't report any errors.
348 mp
= mparse_alloc(MPARSE_AUTO
, MANDOCLEVEL_FATAL
, NULL
, NULL
);
353 memset(&buf
, 0, sizeof(struct buf
));
354 memset(&dbuf
, 0, sizeof(struct buf
));
356 buf
.size
= dbuf
.size
= MANDOC_BUFSZ
;
358 buf
.cp
= mandoc_malloc(buf
.size
);
359 dbuf
.cp
= mandoc_malloc(dbuf
.size
);
361 while (NULL
!= (fn
= *argv
++)) {
364 /* Initialise the in-memory hash of keywords. */
367 (*hash
->close
)(hash
);
369 hash
= dbopen(NULL
, MANDOC_FLAGS
, 0644, DB_HASH
, NULL
);
373 exit((int)MANDOCLEVEL_SYSERR
);
376 /* Parse and get (non-empty) AST. */
378 if (mparse_readfd(mp
, -1, fn
) >= MANDOCLEVEL_FATAL
) {
379 fprintf(stderr
, "%s: Parse failure\n", fn
);
383 mparse_result(mp
, &mdoc
, &man
);
385 if (NULL
== mdoc
&& NULL
== man
)
388 msec
= NULL
!= mdoc
?
389 mdoc_meta(mdoc
)->msec
:
391 mtitle
= NULL
!= mdoc
?
392 mdoc_meta(mdoc
)->title
:
393 man_meta(man
)->title
;
394 arch
= NULL
!= mdoc
? mdoc_meta(mdoc
)->arch
: NULL
;
397 * The index record value consists of a nil-terminated
398 * filename, a nil-terminated manual section, and a
399 * nil-terminated description. Since the description
400 * may not be set, we set a sentinel to see if we're
401 * going to write a nil byte in its place.
405 buf_appendb(&dbuf
, fn
, strlen(fn
) + 1);
406 buf_appendb(&dbuf
, msec
, strlen(msec
) + 1);
407 buf_appendb(&dbuf
, mtitle
, strlen(mtitle
) + 1);
408 buf_appendb(&dbuf
, arch
? arch
: "",
409 arch
? strlen(arch
) + 1 : 1);
413 /* Fix the record number in the btree value. */
416 pmdoc_node(hash
, &buf
, &dbuf
,
417 mdoc_node(mdoc
), mdoc_meta(mdoc
));
419 pman_node(hash
, &buf
, &dbuf
, man_node(man
));
422 * Copy from the in-memory hashtable of pending keywords
426 memset(vbuf
, 0, sizeof(uint32_t));
427 memcpy(vbuf
+ 4, &rec
, sizeof(uint32_t));
430 while (0 == (ch
= (*hash
->seq
)(hash
, &key
, &val
, seq
))) {
433 memcpy(vbuf
, val
.data
, sizeof(uint32_t));
434 val
.size
= sizeof(vbuf
);
438 printf("%s: Keyword %s (%zu): 0x%x\n",
439 fn
, (char *)key
.data
, key
.size
,
442 dbt_put(db
, fbbuf
, &key
, &val
);
448 exit((int)MANDOCLEVEL_SYSERR
);
452 * Apply to the index. If we haven't had a description
453 * set, put an empty one in now.
457 buf_appendb(&dbuf
, "", 1);
460 key
.size
= sizeof(recno_t
);
466 printf("%s: Indexed\n", fn
);
468 dbt_put(idx
, ibbuf
, &key
, &val
);
476 (*hash
->close
)(hash
);
483 /* Atomically replace the file with our temporary one. */
485 if (-1 == rename(fbbuf
, fbuf
))
487 if (-1 == rename(ibbuf
, ibuf
))
490 return((int)MANDOCLEVEL_OK
);
494 * Grow the buffer (if necessary) and copy in a binary string.
497 buf_appendb(struct buf
*buf
, const void *cp
, size_t sz
)
500 /* Overshoot by MANDOC_BUFSZ. */
502 while (buf
->len
+ sz
>= buf
->size
) {
503 buf
->size
= buf
->len
+ sz
+ MANDOC_BUFSZ
;
504 buf
->cp
= mandoc_realloc(buf
->cp
, buf
->size
);
507 memcpy(buf
->cp
+ (int)buf
->len
, cp
, sz
);
512 * Append a nil-terminated string to the buffer.
513 * This can be invoked multiple times.
514 * The buffer string will be nil-terminated.
515 * If invoked multiple times, a space is put between strings.
518 buf_append(struct buf
*buf
, const char *cp
)
522 if (0 == (sz
= strlen(cp
)))
526 buf
->cp
[(int)buf
->len
- 1] = ' ';
528 buf_appendb(buf
, cp
, sz
+ 1);
532 * Recursively add all text from a given node.
533 * This is optimised for general mdoc nodes in this context, which do
534 * not consist of subexpressions and having a recursive call for n->next
536 * The "f" variable should be 0 unless called from pmdoc_Nd for the
537 * description buffer, which does not start at the beginning of the
541 buf_appendmdoc(struct buf
*buf
, const struct mdoc_node
*n
, int f
)
544 for ( ; n
; n
= n
->next
) {
546 buf_appendmdoc(buf
, n
->child
, f
);
548 if (MDOC_TEXT
== n
->type
&& f
) {
550 buf_appendb(buf
, n
->string
,
551 strlen(n
->string
) + 1);
552 } else if (MDOC_TEXT
== n
->type
)
553 buf_append(buf
, n
->string
);
563 if (SEC_AUTHORS
!= n
->sec
)
566 buf_appendmdoc(buf
, n
->child
, 0);
567 hash_put(hash
, buf
, TYPE_AUTHOR
);
574 const char *start
, *end
;
577 if (SEC_SYNOPSIS
!= n
->sec
)
579 if (NULL
== (n
= n
->child
) || MDOC_TEXT
!= n
->type
)
583 * Only consider those `Fd' macro fields that begin with an
584 * "inclusion" token (versus, e.g., #define).
586 if (strcmp("#include", n
->string
))
589 if (NULL
== (n
= n
->next
) || MDOC_TEXT
!= n
->type
)
593 * Strip away the enclosing angle brackets and make sure we're
598 if ('<' == *start
|| '"' == *start
)
601 if (0 == (sz
= strlen(start
)))
604 end
= &start
[(int)sz
- 1];
605 if ('>' == *end
|| '"' == *end
)
608 assert(end
>= start
);
610 buf_appendb(buf
, start
, (size_t)(end
- start
+ 1));
611 buf_appendb(buf
, "", 1);
613 hash_put(hash
, buf
, TYPE_INCLUDES
);
621 if (SEC_SYNOPSIS
!= n
->sec
)
624 buf_appendmdoc(buf
, n
->child
, 0);
625 hash_put(hash
, buf
, TYPE_CONFIG
);
633 if (SEC_SYNOPSIS
!= n
->sec
)
635 if (NULL
== n
->child
|| MDOC_TEXT
!= n
->child
->type
)
638 buf_append(buf
, n
->child
->string
);
639 hash_put(hash
, buf
, TYPE_INCLUDES
);
648 if (SEC_SYNOPSIS
!= n
->sec
)
650 if (NULL
== n
->child
|| MDOC_TEXT
!= n
->child
->type
)
653 /* .Fn "struct type *arg" "foo" */
655 cp
= strrchr(n
->child
->string
, ' ');
657 cp
= n
->child
->string
;
659 /* Strip away pointer symbol. */
665 hash_put(hash
, buf
, TYPE_FUNCTION
);
673 if (SEC_STANDARDS
!= n
->sec
)
675 if (NULL
== n
->child
|| MDOC_TEXT
!= n
->child
->type
)
678 buf_append(buf
, n
->child
->string
);
679 hash_put(hash
, buf
, TYPE_STANDARD
);
687 if (NULL
== (n
= n
->child
))
690 buf_appendb(buf
, n
->string
, strlen(n
->string
));
692 if (NULL
!= (n
= n
->next
)) {
693 buf_appendb(buf
, ".", 1);
694 buf_appendb(buf
, n
->string
, strlen(n
->string
) + 1);
696 buf_appendb(buf
, ".", 2);
698 hash_put(hash
, buf
, TYPE_XREF
);
708 if (SEC_SYNOPSIS
!= n
->sec
)
710 if (MDOC_Vt
== n
->tok
&& MDOC_BODY
!= n
->type
)
712 if (NULL
== n
->last
|| MDOC_TEXT
!= n
->last
->type
)
716 * Strip away leading pointer symbol '*' and trailing ';'.
719 start
= n
->last
->string
;
721 while ('*' == *start
)
724 if (0 == (sz
= strlen(start
)))
727 if (';' == start
[(int)sz
- 1])
733 buf_appendb(buf
, start
, sz
);
734 buf_appendb(buf
, "", 1);
735 hash_put(hash
, buf
, TYPE_VARIABLE
);
743 if (SEC_SYNOPSIS
!= n
->sec
|| MDOC_HEAD
!= n
->type
)
745 if (NULL
== n
->child
|| MDOC_TEXT
!= n
->child
->type
)
748 buf_append(buf
, n
->child
->string
);
749 hash_put(hash
, buf
, TYPE_FUNCTION
);
758 if (MDOC_BODY
!= n
->type
)
761 buf_appendmdoc(dbuf
, n
->child
, 1);
762 buf_appendmdoc(buf
, n
->child
, 0);
764 hash_put(hash
, buf
, TYPE_DESC
);
772 if (SEC_ENVIRONMENT
!= n
->sec
)
775 buf_appendmdoc(buf
, n
->child
, 0);
776 hash_put(hash
, buf
, TYPE_ENV
);
784 if (SEC_FILES
!= n
->sec
)
787 buf_appendmdoc(buf
, n
->child
, 0);
788 hash_put(hash
, buf
, TYPE_PATH
);
796 if (SEC_NAME
== n
->sec
) {
797 buf_appendmdoc(buf
, n
->child
, 0);
798 hash_put(hash
, buf
, TYPE_NAME
);
800 } else if (SEC_SYNOPSIS
!= n
->sec
|| MDOC_HEAD
!= n
->type
)
803 if (NULL
== n
->child
)
804 buf_append(buf
, m
->name
);
806 buf_appendmdoc(buf
, n
->child
, 0);
807 hash_put(hash
, buf
, TYPE_UTILITY
);
811 hash_put(DB
*db
, const struct buf
*buf
, int mask
)
822 if ((rc
= (*db
->get
)(db
, &key
, &val
, 0)) < 0) {
824 exit((int)MANDOCLEVEL_SYSERR
);
826 mask
|= *(int *)val
.data
;
829 val
.size
= sizeof(int);
831 if ((rc
= (*db
->put
)(db
, &key
, &val
, 0)) < 0) {
833 exit((int)MANDOCLEVEL_SYSERR
);
838 dbt_put(DB
*db
, const char *dbn
, DBT
*key
, DBT
*val
)
844 if (0 == (*db
->put
)(db
, key
, val
, 0))
848 exit((int)MANDOCLEVEL_SYSERR
);
853 * Call out to per-macro handlers after clearing the persistent database
854 * key. If the macro sets the database key, flush it to the database.
857 pmdoc_node(MDOC_ARGS
)
873 if (NULL
== mdocs
[n
->tok
])
877 (*mdocs
[n
->tok
])(hash
, buf
, dbuf
, n
, m
);
883 pmdoc_node(hash
, buf
, dbuf
, n
->child
, m
);
884 pmdoc_node(hash
, buf
, dbuf
, n
->next
, m
);
890 const struct man_node
*head
, *body
;
891 const char *start
, *sv
;
898 * We're only searching for one thing: the first text child in
899 * the BODY of a NAME section. Since we don't keep track of
900 * sections in -man, run some hoops to find out whether we're in
901 * the correct section or not.
904 if (MAN_BODY
== n
->type
&& MAN_SH
== n
->tok
) {
906 assert(body
->parent
);
907 if (NULL
!= (head
= body
->parent
->head
) &&
909 NULL
!= (head
= (head
->child
)) &&
910 MAN_TEXT
== head
->type
&&
911 0 == strcmp(head
->string
, "NAME") &&
912 NULL
!= (body
= body
->child
) &&
913 MAN_TEXT
== body
->type
) {
915 assert(body
->string
);
916 start
= sv
= body
->string
;
919 * Go through a special heuristic dance here.
920 * This is why -man manuals are great!
921 * (I'm being sarcastic: my eyes are bleeding.)
922 * Conventionally, one or more manual names are
923 * comma-specified prior to a whitespace, then a
924 * dash, then a description. Try to puzzle out
925 * the name parts here.
929 sz
= strcspn(start
, " ,");
930 if ('\0' == start
[(int)sz
])
934 buf_appendb(buf
, start
, sz
);
935 buf_appendb(buf
, "", 1);
937 hash_put(hash
, buf
, TYPE_NAME
);
939 if (' ' == start
[(int)sz
]) {
940 start
+= (int)sz
+ 1;
944 assert(',' == start
[(int)sz
]);
945 start
+= (int)sz
+ 1;
946 while (' ' == *start
)
952 buf_append(buf
, start
);
956 while (' ' == *start
)
959 if (0 == strncmp(start
, "-", 1))
961 else if (0 == strncmp(start
, "\\-", 2))
963 else if (0 == strncmp(start
, "\\(en", 4))
965 else if (0 == strncmp(start
, "\\(em", 4))
968 while (' ' == *start
)
971 sz
= strlen(start
) + 1;
972 buf_appendb(dbuf
, start
, sz
);
973 buf_appendb(buf
, start
, sz
);
977 if (pman_node(hash
, buf
, dbuf
, n
->child
))
979 if (pman_node(hash
, buf
, dbuf
, n
->next
))
989 fprintf(stderr
, "usage: %s [-v] [-d path] [file...]\n",