]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
1 /* $Id: main.c,v 1.218 2015/02/03 21:16:02 schwarze Exp $ */
3 * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010-2012, 2014, 2015 Ingo Schwarze <schwarze@openbsd.org>
5 * Copyright (c) 2010 Joerg Sonnenberger <joerg@netbsd.org>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 #include <sys/types.h>
22 #include <sys/param.h> /* MACHINE */
35 #include "mandoc_aux.h"
40 #include "mansearch.h"
42 #if !defined(__GNUC__) || (__GNUC__ < 2)
44 # define __attribute__(x)
46 #endif /* !defined(__GNUC__) || (__GNUC__ < 2) */
57 typedef void (*out_mdoc
)(void *, const struct mdoc
*);
58 typedef void (*out_man
)(void *, const struct man
*);
59 typedef void (*out_free
)(void *);
62 OUTT_ASCII
= 0, /* -Tascii */
63 OUTT_LOCALE
, /* -Tlocale */
64 OUTT_UTF8
, /* -Tutf8 */
65 OUTT_TREE
, /* -Ttree */
67 OUTT_HTML
, /* -Thtml */
68 OUTT_LINT
, /* -Tlint */
75 struct mchars
*mchars
; /* character table */
76 enum mandoclevel wlevel
; /* ignore messages below this */
77 int wstop
; /* stop after a file with a warning */
78 enum outt outtype
; /* which output to use */
79 out_mdoc outmdoc
; /* mdoc output ptr */
80 out_man outman
; /* man output ptr */
81 out_free outfree
; /* free output ptr */
82 void *outdata
; /* data for output */
83 char outopts
[BUFSIZ
]; /* buf of output opts */
86 static int fs_lookup(const struct manpaths
*,
87 size_t ipath
, const char *,
88 const char *, const char *,
89 struct manpage
**, size_t *);
90 static void fs_search(const struct mansearch
*,
91 const struct manpaths
*, int, char**,
92 struct manpage
**, size_t *);
93 static int koptions(int *, char *);
95 int mandocdb(int, char**);
97 static int moptions(int *, char *);
98 static void mmsg(enum mandocerr
, enum mandoclevel
,
99 const char *, int, int, const char *);
100 static void parse(struct curparse
*, int,
101 const char *, enum mandoclevel
*);
102 static enum mandoclevel
passthrough(const char *, int, int);
103 static void spawn_pager(void);
104 static int toptions(struct curparse
*, char *);
105 static void usage(enum argmode
) __attribute__((noreturn
));
106 static void version(void) __attribute__((noreturn
));
107 static int woptions(struct curparse
*, char *);
109 static const int sec_prios
[] = {1, 4, 5, 8, 6, 3, 7, 2, 9};
110 static char help_arg
[] = "help";
111 static char *help_argv
[] = {help_arg
, NULL
};
112 static const char *progname
;
116 main(int argc
, char *argv
[])
118 struct curparse curp
;
119 struct mansearch search
;
120 struct manpaths paths
;
124 struct manpage
*res
, *resp
;
125 char *conf_file
, *defpaths
;
127 int prio
, best_prio
, synopsis_only
;
129 enum mandoclevel rc
, rctmp
;
130 enum outmode outmode
;
137 progname
= strrchr(argv
[0], '/');
138 if (progname
== NULL
)
144 if (strcmp(progname
, BINM_MAKEWHATIS
) == 0)
145 return(mandocdb(argc
, argv
));
148 /* Search options. */
150 memset(&paths
, 0, sizeof(struct manpaths
));
151 conf_file
= defpaths
= NULL
;
154 memset(&search
, 0, sizeof(struct mansearch
));
155 search
.outkey
= "Nd";
157 if (strcmp(progname
, BINM_MAN
) == 0)
158 search
.argmode
= ARG_NAME
;
159 else if (strcmp(progname
, BINM_APROPOS
) == 0)
160 search
.argmode
= ARG_EXPR
;
161 else if (strcmp(progname
, BINM_WHATIS
) == 0)
162 search
.argmode
= ARG_WORD
;
163 else if (strncmp(progname
, "help", 4) == 0)
164 search
.argmode
= ARG_NAME
;
166 search
.argmode
= ARG_FILE
;
168 /* Parser and formatter options. */
170 memset(&curp
, 0, sizeof(struct curparse
));
171 curp
.outtype
= OUTT_LOCALE
;
172 curp
.wlevel
= MANDOCLEVEL_BADARG
;
173 options
= MPARSE_SO
| MPARSE_UTF8
| MPARSE_LATIN1
;
179 outmode
= OUTMODE_DEF
;
181 while (-1 != (c
= getopt(argc
, argv
,
182 "aC:cfhI:iK:klM:m:O:S:s:T:VW:w"))) {
185 outmode
= OUTMODE_ALL
;
194 search
.argmode
= ARG_WORD
;
197 (void)strlcat(curp
.outopts
, "synopsis,", BUFSIZ
);
200 outmode
= OUTMODE_ALL
;
203 if (strncmp(optarg
, "os=", 3)) {
205 "%s: -I %s: Bad argument\n",
207 return((int)MANDOCLEVEL_BADARG
);
211 "%s: -I %s: Duplicate argument\n",
213 return((int)MANDOCLEVEL_BADARG
);
215 defos
= mandoc_strdup(optarg
+ 3);
218 outmode
= OUTMODE_INT
;
221 if ( ! koptions(&options
, optarg
))
222 return((int)MANDOCLEVEL_BADARG
);
225 search
.argmode
= ARG_EXPR
;
228 search
.argmode
= ARG_FILE
;
229 outmode
= OUTMODE_ALL
;
238 search
.outkey
= optarg
;
239 (void)strlcat(curp
.outopts
, optarg
, BUFSIZ
);
240 (void)strlcat(curp
.outopts
, ",", BUFSIZ
);
243 search
.arch
= optarg
;
249 if ( ! toptions(&curp
, optarg
))
250 return((int)MANDOCLEVEL_BADARG
);
253 if ( ! woptions(&curp
, optarg
))
254 return((int)MANDOCLEVEL_BADARG
);
257 outmode
= OUTMODE_FLN
;
269 usage(search
.argmode
);
271 /* Postprocess options. */
273 if (outmode
== OUTMODE_DEF
) {
274 switch (search
.argmode
) {
276 outmode
= OUTMODE_ALL
;
280 outmode
= OUTMODE_ONE
;
283 outmode
= OUTMODE_LST
;
288 /* Parse arguments. */
296 * and for a man(1) section argument without -s.
299 if (search
.argmode
== ARG_NAME
) {
300 if (*progname
== 'h') {
305 } else if (argc
> 1 &&
306 ((uc
= argv
[0]) != NULL
) &&
307 ((isdigit(uc
[0]) && (uc
[1] == '\0' ||
308 (isalpha(uc
[1]) && uc
[2] == '\0'))) ||
309 (uc
[0] == 'n' && uc
[1] == '\0'))) {
314 if (search
.arch
== NULL
)
315 search
.arch
= getenv("MACHINE");
316 if (search
.arch
== NULL
)
317 search
.arch
= MACHINE
;
322 /* man(1), whatis(1), apropos(1) */
324 if (search
.argmode
!= ARG_FILE
) {
326 usage(search
.argmode
);
328 if (search
.argmode
== ARG_NAME
&&
329 outmode
== OUTMODE_ONE
)
330 search
.firstmatch
= 1;
332 /* Access the mandoc database. */
334 manpath_parse(&paths
, conf_file
, defpaths
, auxpaths
);
337 if( ! mansearch(&search
, &paths
, argc
, argv
, &res
, &sz
))
338 usage(search
.argmode
);
340 if (search
.argmode
!= ARG_NAME
) {
341 fputs("mandoc: database support not compiled in\n",
343 return((int)MANDOCLEVEL_BADARG
);
348 if (sz
== 0 && search
.argmode
== ARG_NAME
)
349 fs_search(&search
, &paths
, argc
, argv
, &res
, &sz
);
352 rc
= MANDOCLEVEL_BADARG
;
357 * For standard man(1) and -a output mode,
358 * prepare for copying filename pointers
359 * into the program parameter array.
362 if (outmode
== OUTMODE_ONE
) {
365 } else if (outmode
== OUTMODE_ALL
)
368 /* Iterate all matching manuals. */
371 for (i
= 0; i
< sz
; i
++) {
372 if (outmode
== OUTMODE_FLN
)
374 else if (outmode
== OUTMODE_LST
)
375 printf("%s - %s\n", res
[i
].names
,
376 res
[i
].output
== NULL
? "" :
378 else if (outmode
== OUTMODE_ONE
) {
379 /* Search for the best section. */
380 isec
= strcspn(res
[i
].file
, "123456789");
381 sec
= res
[i
].file
[isec
];
384 prio
= sec_prios
[sec
- '1'];
385 if (prio
>= best_prio
)
393 * For man(1), -a and -i output mode, fall through
394 * to the main mandoc(1) code iterating files
395 * and running the parsers on each of them.
398 if (outmode
== OUTMODE_FLN
|| outmode
== OUTMODE_LST
)
404 if (search
.argmode
== ARG_FILE
&& ! moptions(&options
, auxpaths
))
405 return((int)MANDOCLEVEL_BADARG
);
407 curp
.mchars
= mchars_alloc();
408 curp
.mp
= mparse_alloc(options
, curp
.wlevel
, mmsg
,
412 * Conditionally start up the lookaside buffer before parsing.
414 if (OUTT_MAN
== curp
.outtype
)
415 mparse_keep(curp
.mp
);
418 if (use_pager
&& isatty(STDOUT_FILENO
))
420 parse(&curp
, STDIN_FILENO
, "<stdin>", &rc
);
424 rctmp
= mparse_open(curp
.mp
, &fd
,
425 resp
!= NULL
? resp
->file
: *argv
);
430 if (use_pager
&& isatty(STDOUT_FILENO
))
435 parse(&curp
, fd
, *argv
, &rc
);
436 else if (resp
->form
& FORM_SRC
) {
437 /* For .so only; ignore failure. */
438 chdir(paths
.paths
[resp
->ipath
]);
439 parse(&curp
, fd
, resp
->file
, &rc
);
441 rctmp
= passthrough(resp
->file
, fd
,
447 rctmp
= mparse_wait(curp
.mp
);
451 if (argc
> 1 && curp
.outtype
<= OUTT_UTF8
)
452 ascii_sepline(curp
.outdata
);
455 if (MANDOCLEVEL_OK
!= rc
&& curp
.wstop
)
463 mparse_reset(curp
.mp
);
467 (*curp
.outfree
)(curp
.outdata
);
468 mparse_free(curp
.mp
);
469 mchars_free(curp
.mchars
);
472 if (search
.argmode
!= ARG_FILE
) {
473 manpath_free(&paths
);
475 mansearch_free(res
, sz
);
489 printf("mandoc %s\n", VERSION
);
490 exit((int)MANDOCLEVEL_OK
);
494 usage(enum argmode argmode
)
499 fputs("usage: mandoc [-acfhklV] [-Ios=name] "
500 "[-Kencoding] [-mformat] [-Ooption]\n"
501 "\t [-Toutput] [-Wlevel] [file ...]\n", stderr
);
504 fputs("usage: man [-acfhklVw] [-C file] [-I os=name] "
505 "[-K encoding] [-M path] [-m path]\n"
506 "\t [-O option=value] [-S subsection] [-s section] "
507 "[-T output] [-W level]\n"
508 "\t [section] name ...\n", stderr
);
511 fputs("usage: whatis [-acfhklVw] [-C file] "
512 "[-M path] [-m path] [-O outkey] [-S arch]\n"
513 "\t [-s section] name ...\n", stderr
);
516 fputs("usage: apropos [-acfhklVw] [-C file] "
517 "[-M path] [-m path] [-O outkey] [-S arch]\n"
518 "\t [-s section] expression ...\n", stderr
);
521 exit((int)MANDOCLEVEL_BADARG
);
525 fs_lookup(const struct manpaths
*paths
, size_t ipath
,
526 const char *sec
, const char *arch
, const char *name
,
527 struct manpage
**res
, size_t *ressz
)
529 struct manpage
*page
;
533 mandoc_asprintf(&file
, "%s/man%s/%s.%s",
534 paths
->paths
[ipath
], sec
, name
, sec
);
535 if (access(file
, R_OK
) != -1) {
541 mandoc_asprintf(&file
, "%s/cat%s/%s.0",
542 paths
->paths
[ipath
], sec
, name
);
543 if (access(file
, R_OK
) != -1) {
550 mandoc_asprintf(&file
, "%s/man%s/%s/%s.%s",
551 paths
->paths
[ipath
], sec
, arch
, name
, sec
);
552 if (access(file
, R_OK
) != -1) {
562 fprintf(stderr
, "%s: outdated mandoc.db lacks %s(%s) entry,\n"
563 " consider running # makewhatis %s\n",
564 progname
, name
, sec
, paths
->paths
[ipath
]);
567 *res
= mandoc_reallocarray(*res
, ++*ressz
, sizeof(struct manpage
));
568 page
= *res
+ (*ressz
- 1);
573 page
->bits
= NAME_FILE
& NAME_MASK
;
574 page
->sec
= (*sec
>= '1' && *sec
<= '9') ? *sec
- '1' + 1 : 10;
580 fs_search(const struct mansearch
*cfg
, const struct manpaths
*paths
,
581 int argc
, char **argv
, struct manpage
**res
, size_t *ressz
)
583 const char *const sections
[] =
584 {"1", "8", "6", "2", "3", "3p", "5", "7", "4", "9"};
585 const size_t nsec
= sizeof(sections
)/sizeof(sections
[0]);
587 size_t ipath
, isec
, lastsz
;
589 assert(cfg
->argmode
== ARG_NAME
);
594 for (ipath
= 0; ipath
< paths
->sz
; ipath
++) {
595 if (cfg
->sec
!= NULL
) {
596 if (fs_lookup(paths
, ipath
, cfg
->sec
,
597 cfg
->arch
, *argv
, res
, ressz
) &&
600 } else for (isec
= 0; isec
< nsec
; isec
++)
601 if (fs_lookup(paths
, ipath
, sections
[isec
],
602 cfg
->arch
, *argv
, res
, ressz
) &&
606 if (*ressz
== lastsz
)
608 "%s: No entry for %s in the manual.\n",
617 parse(struct curparse
*curp
, int fd
, const char *file
,
618 enum mandoclevel
*level
)
624 /* Begin by parsing the file itself. */
629 rc
= mparse_readfd(curp
->mp
, fd
, file
);
632 * With -Wstop and warnings or errors of at least the requested
633 * level, do not produce output.
636 if (MANDOCLEVEL_OK
!= rc
&& curp
->wstop
)
639 /* If unset, allocate output dev now (if applicable). */
641 if ( ! (curp
->outman
&& curp
->outmdoc
)) {
642 switch (curp
->outtype
) {
644 curp
->outdata
= html_alloc(curp
->mchars
,
646 curp
->outfree
= html_free
;
649 curp
->outdata
= utf8_alloc(curp
->mchars
,
651 curp
->outfree
= ascii_free
;
654 curp
->outdata
= locale_alloc(curp
->mchars
,
656 curp
->outfree
= ascii_free
;
659 curp
->outdata
= ascii_alloc(curp
->mchars
,
661 curp
->outfree
= ascii_free
;
664 curp
->outdata
= pdf_alloc(curp
->mchars
,
666 curp
->outfree
= pspdf_free
;
669 curp
->outdata
= ps_alloc(curp
->mchars
,
671 curp
->outfree
= pspdf_free
;
677 switch (curp
->outtype
) {
679 curp
->outman
= html_man
;
680 curp
->outmdoc
= html_mdoc
;
683 curp
->outman
= tree_man
;
684 curp
->outmdoc
= tree_mdoc
;
687 curp
->outmdoc
= man_mdoc
;
688 curp
->outman
= man_man
;
699 curp
->outman
= terminal_man
;
700 curp
->outmdoc
= terminal_mdoc
;
707 mparse_result(curp
->mp
, &mdoc
, &man
, NULL
);
709 /* Execute the out device, if it exists. */
711 if (man
&& curp
->outman
)
712 (*curp
->outman
)(curp
->outdata
, man
);
713 if (mdoc
&& curp
->outmdoc
)
714 (*curp
->outmdoc
)(curp
->outdata
, mdoc
);
721 static enum mandoclevel
722 passthrough(const char *file
, int fd
, int synopsis_only
)
724 const char synb
[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
725 const char synr
[] = "SYNOPSIS";
736 if ((stream
= fdopen(fd
, "r")) == NULL
) {
743 while ((line
= fgetln(stream
, &len
)) != NULL
) {
746 if ( ! isspace((unsigned char)*line
))
749 isspace((unsigned char)*line
)) {
754 if ((len
== sizeof(synb
) &&
755 ! strncmp(line
, synb
, len
- 1)) ||
756 (len
== sizeof(synr
) &&
757 ! strncmp(line
, synr
, len
- 1)))
762 for (off
= 0; off
< len
; off
+= nw
)
763 if ((nw
= write(STDOUT_FILENO
, line
+ off
,
764 len
- off
)) == -1 || nw
== 0) {
771 if (ferror(stream
)) {
779 return(MANDOCLEVEL_OK
);
782 fprintf(stderr
, "%s: %s: SYSERR: %s: %s",
783 progname
, file
, syscall
, strerror(errno
));
784 return(MANDOCLEVEL_SYSERR
);
788 koptions(int *options
, char *arg
)
791 if ( ! strcmp(arg
, "utf-8")) {
792 *options
|= MPARSE_UTF8
;
793 *options
&= ~MPARSE_LATIN1
;
794 } else if ( ! strcmp(arg
, "iso-8859-1")) {
795 *options
|= MPARSE_LATIN1
;
796 *options
&= ~MPARSE_UTF8
;
797 } else if ( ! strcmp(arg
, "us-ascii")) {
798 *options
&= ~(MPARSE_UTF8
| MPARSE_LATIN1
);
800 fprintf(stderr
, "%s: -K %s: Bad argument\n",
808 moptions(int *options
, char *arg
)
813 else if (0 == strcmp(arg
, "doc"))
814 *options
|= MPARSE_MDOC
;
815 else if (0 == strcmp(arg
, "andoc"))
817 else if (0 == strcmp(arg
, "an"))
818 *options
|= MPARSE_MAN
;
820 fprintf(stderr
, "%s: -m %s: Bad argument\n",
829 toptions(struct curparse
*curp
, char *arg
)
832 if (0 == strcmp(arg
, "ascii"))
833 curp
->outtype
= OUTT_ASCII
;
834 else if (0 == strcmp(arg
, "lint")) {
835 curp
->outtype
= OUTT_LINT
;
836 curp
->wlevel
= MANDOCLEVEL_WARNING
;
837 } else if (0 == strcmp(arg
, "tree"))
838 curp
->outtype
= OUTT_TREE
;
839 else if (0 == strcmp(arg
, "man"))
840 curp
->outtype
= OUTT_MAN
;
841 else if (0 == strcmp(arg
, "html"))
842 curp
->outtype
= OUTT_HTML
;
843 else if (0 == strcmp(arg
, "utf8"))
844 curp
->outtype
= OUTT_UTF8
;
845 else if (0 == strcmp(arg
, "locale"))
846 curp
->outtype
= OUTT_LOCALE
;
847 else if (0 == strcmp(arg
, "xhtml"))
848 curp
->outtype
= OUTT_HTML
;
849 else if (0 == strcmp(arg
, "ps"))
850 curp
->outtype
= OUTT_PS
;
851 else if (0 == strcmp(arg
, "pdf"))
852 curp
->outtype
= OUTT_PDF
;
854 fprintf(stderr
, "%s: -T %s: Bad argument\n",
863 woptions(struct curparse
*curp
, char *arg
)
878 switch (getsubopt(&arg
, UNCONST(toks
), &v
)) {
885 curp
->wlevel
= MANDOCLEVEL_WARNING
;
888 curp
->wlevel
= MANDOCLEVEL_ERROR
;
891 curp
->wlevel
= MANDOCLEVEL_UNSUPP
;
894 curp
->wlevel
= MANDOCLEVEL_BADARG
;
897 fprintf(stderr
, "%s: -W %s: Bad argument\n",
907 mmsg(enum mandocerr t
, enum mandoclevel lvl
,
908 const char *file
, int line
, int col
, const char *msg
)
910 const char *mparse_msg
;
912 fprintf(stderr
, "%s: %s:", progname
, file
);
915 fprintf(stderr
, "%d:%d:", line
, col
+ 1);
917 fprintf(stderr
, " %s", mparse_strlevel(lvl
));
919 if (NULL
!= (mparse_msg
= mparse_strerror(t
)))
920 fprintf(stderr
, ": %s", mparse_msg
);
923 fprintf(stderr
, ": %s", msg
);
931 #define MAX_PAGER_ARGS 16
932 char *argv
[MAX_PAGER_ARGS
];
938 if (pipe(fildes
) == -1) {
939 fprintf(stderr
, "%s: pipe: %s\n",
940 progname
, strerror(errno
));
946 fprintf(stderr
, "%s: fork: %s\n",
947 progname
, strerror(errno
));
948 exit((int)MANDOCLEVEL_SYSERR
);
951 if (dup2(fildes
[1], STDOUT_FILENO
) == -1) {
952 fprintf(stderr
, "%s: dup output: %s\n",
953 progname
, strerror(errno
));
954 exit((int)MANDOCLEVEL_SYSERR
);
961 /* The original process becomes the pager. */
964 if (dup2(fildes
[0], STDIN_FILENO
) == -1) {
965 fprintf(stderr
, "%s: dup input: %s\n",
966 progname
, strerror(errno
));
967 exit((int)MANDOCLEVEL_SYSERR
);
970 pager
= getenv("MANPAGER");
971 if (pager
== NULL
|| *pager
== '\0')
972 pager
= getenv("PAGER");
973 if (pager
== NULL
|| *pager
== '\0')
974 pager
= "/usr/bin/more -s";
975 cp
= mandoc_strdup(pager
);
978 * Parse the pager command into words.
979 * Intentionally do not do anything fancy here.
983 while (argc
+ 1 < MAX_PAGER_ARGS
) {
985 cp
= strchr(cp
, ' ');
996 /* Hand over to the pager. */
998 execvp(argv
[0], argv
);
999 fprintf(stderr
, "%s: exec: %s\n",
1000 progname
, strerror(errno
));
1001 exit((int)MANDOCLEVEL_SYSERR
);