]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
1 /* $Id: main.c,v 1.220 2015/02/10 08:05:30 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
;
139 else if ((progname
= strrchr(argv
[0], '/')) == NULL
)
145 if (strcmp(progname
, BINM_MAKEWHATIS
) == 0)
146 return(mandocdb(argc
, argv
));
149 /* Search options. */
151 memset(&paths
, 0, sizeof(struct manpaths
));
152 conf_file
= defpaths
= NULL
;
155 memset(&search
, 0, sizeof(struct mansearch
));
156 search
.outkey
= "Nd";
158 if (strcmp(progname
, BINM_MAN
) == 0)
159 search
.argmode
= ARG_NAME
;
160 else if (strcmp(progname
, BINM_APROPOS
) == 0)
161 search
.argmode
= ARG_EXPR
;
162 else if (strcmp(progname
, BINM_WHATIS
) == 0)
163 search
.argmode
= ARG_WORD
;
164 else if (strncmp(progname
, "help", 4) == 0)
165 search
.argmode
= ARG_NAME
;
167 search
.argmode
= ARG_FILE
;
169 /* Parser and formatter options. */
171 memset(&curp
, 0, sizeof(struct curparse
));
172 curp
.outtype
= OUTT_LOCALE
;
173 curp
.wlevel
= MANDOCLEVEL_BADARG
;
174 options
= MPARSE_SO
| MPARSE_UTF8
| MPARSE_LATIN1
;
180 outmode
= OUTMODE_DEF
;
182 while (-1 != (c
= getopt(argc
, argv
,
183 "aC:cfhI:iK:klM:m:O:S:s:T:VW:w"))) {
186 outmode
= OUTMODE_ALL
;
195 search
.argmode
= ARG_WORD
;
198 (void)strlcat(curp
.outopts
, "synopsis,", BUFSIZ
);
201 outmode
= OUTMODE_ALL
;
204 if (strncmp(optarg
, "os=", 3)) {
206 "%s: -I %s: Bad argument\n",
208 return((int)MANDOCLEVEL_BADARG
);
212 "%s: -I %s: Duplicate argument\n",
214 return((int)MANDOCLEVEL_BADARG
);
216 defos
= mandoc_strdup(optarg
+ 3);
219 outmode
= OUTMODE_INT
;
222 if ( ! koptions(&options
, optarg
))
223 return((int)MANDOCLEVEL_BADARG
);
226 search
.argmode
= ARG_EXPR
;
229 search
.argmode
= ARG_FILE
;
230 outmode
= OUTMODE_ALL
;
239 search
.outkey
= optarg
;
240 (void)strlcat(curp
.outopts
, optarg
, BUFSIZ
);
241 (void)strlcat(curp
.outopts
, ",", BUFSIZ
);
244 search
.arch
= optarg
;
250 if ( ! toptions(&curp
, optarg
))
251 return((int)MANDOCLEVEL_BADARG
);
254 if ( ! woptions(&curp
, optarg
))
255 return((int)MANDOCLEVEL_BADARG
);
258 outmode
= OUTMODE_FLN
;
270 usage(search
.argmode
);
272 /* Postprocess options. */
274 if (outmode
== OUTMODE_DEF
) {
275 switch (search
.argmode
) {
277 outmode
= OUTMODE_ALL
;
281 outmode
= OUTMODE_ONE
;
284 outmode
= OUTMODE_LST
;
289 /* Parse arguments. */
299 * and for a man(1) section argument without -s.
302 if (search
.argmode
== ARG_NAME
) {
303 if (*progname
== 'h') {
308 } else if (argc
> 1 &&
309 ((uc
= argv
[0]) != NULL
) &&
310 ((isdigit(uc
[0]) && (uc
[1] == '\0' ||
311 (isalpha(uc
[1]) && uc
[2] == '\0'))) ||
312 (uc
[0] == 'n' && uc
[1] == '\0'))) {
317 if (search
.arch
== NULL
)
318 search
.arch
= getenv("MACHINE");
319 if (search
.arch
== NULL
)
320 search
.arch
= MACHINE
;
325 /* man(1), whatis(1), apropos(1) */
327 if (search
.argmode
!= ARG_FILE
) {
329 usage(search
.argmode
);
331 if (search
.argmode
== ARG_NAME
&&
332 outmode
== OUTMODE_ONE
)
333 search
.firstmatch
= 1;
335 /* Access the mandoc database. */
337 manpath_parse(&paths
, conf_file
, defpaths
, auxpaths
);
340 if( ! mansearch(&search
, &paths
, argc
, argv
, &res
, &sz
))
341 usage(search
.argmode
);
343 if (search
.argmode
!= ARG_NAME
) {
344 fputs("mandoc: database support not compiled in\n",
346 return((int)MANDOCLEVEL_BADARG
);
351 if (sz
== 0 && search
.argmode
== ARG_NAME
)
352 fs_search(&search
, &paths
, argc
, argv
, &res
, &sz
);
355 rc
= MANDOCLEVEL_BADARG
;
360 * For standard man(1) and -a output mode,
361 * prepare for copying filename pointers
362 * into the program parameter array.
365 if (outmode
== OUTMODE_ONE
) {
368 } else if (outmode
== OUTMODE_ALL
)
371 /* Iterate all matching manuals. */
374 for (i
= 0; i
< sz
; i
++) {
375 if (outmode
== OUTMODE_FLN
)
377 else if (outmode
== OUTMODE_LST
)
378 printf("%s - %s\n", res
[i
].names
,
379 res
[i
].output
== NULL
? "" :
381 else if (outmode
== OUTMODE_ONE
) {
382 /* Search for the best section. */
383 isec
= strcspn(res
[i
].file
, "123456789");
384 sec
= res
[i
].file
[isec
];
387 prio
= sec_prios
[sec
- '1'];
388 if (prio
>= best_prio
)
396 * For man(1), -a and -i output mode, fall through
397 * to the main mandoc(1) code iterating files
398 * and running the parsers on each of them.
401 if (outmode
== OUTMODE_FLN
|| outmode
== OUTMODE_LST
)
407 if (search
.argmode
== ARG_FILE
&& ! moptions(&options
, auxpaths
))
408 return((int)MANDOCLEVEL_BADARG
);
410 curp
.mchars
= mchars_alloc();
411 curp
.mp
= mparse_alloc(options
, curp
.wlevel
, mmsg
,
415 * Conditionally start up the lookaside buffer before parsing.
417 if (OUTT_MAN
== curp
.outtype
)
418 mparse_keep(curp
.mp
);
421 if (use_pager
&& isatty(STDOUT_FILENO
))
423 parse(&curp
, STDIN_FILENO
, "<stdin>", &rc
);
427 rctmp
= mparse_open(curp
.mp
, &fd
,
428 resp
!= NULL
? resp
->file
: *argv
);
433 if (use_pager
&& isatty(STDOUT_FILENO
))
438 parse(&curp
, fd
, *argv
, &rc
);
439 else if (resp
->form
& FORM_SRC
) {
440 /* For .so only; ignore failure. */
441 chdir(paths
.paths
[resp
->ipath
]);
442 parse(&curp
, fd
, resp
->file
, &rc
);
444 rctmp
= passthrough(resp
->file
, fd
,
450 rctmp
= mparse_wait(curp
.mp
);
454 if (argc
> 1 && curp
.outtype
<= OUTT_UTF8
)
455 ascii_sepline(curp
.outdata
);
458 if (MANDOCLEVEL_OK
!= rc
&& curp
.wstop
)
466 mparse_reset(curp
.mp
);
470 (*curp
.outfree
)(curp
.outdata
);
471 mparse_free(curp
.mp
);
472 mchars_free(curp
.mchars
);
475 if (search
.argmode
!= ARG_FILE
) {
476 manpath_free(&paths
);
478 mansearch_free(res
, sz
);
492 printf("mandoc %s\n", VERSION
);
493 exit((int)MANDOCLEVEL_OK
);
497 usage(enum argmode argmode
)
502 fputs("usage: mandoc [-acfhklV] [-Ios=name] "
503 "[-Kencoding] [-mformat] [-Ooption]\n"
504 "\t [-Toutput] [-Wlevel] [file ...]\n", stderr
);
507 fputs("usage: man [-acfhklVw] [-C file] [-I os=name] "
508 "[-K encoding] [-M path] [-m path]\n"
509 "\t [-O option=value] [-S subsection] [-s section] "
510 "[-T output] [-W level]\n"
511 "\t [section] name ...\n", stderr
);
514 fputs("usage: whatis [-acfhklVw] [-C file] "
515 "[-M path] [-m path] [-O outkey] [-S arch]\n"
516 "\t [-s section] name ...\n", stderr
);
519 fputs("usage: apropos [-acfhklVw] [-C file] "
520 "[-M path] [-m path] [-O outkey] [-S arch]\n"
521 "\t [-s section] expression ...\n", stderr
);
524 exit((int)MANDOCLEVEL_BADARG
);
528 fs_lookup(const struct manpaths
*paths
, size_t ipath
,
529 const char *sec
, const char *arch
, const char *name
,
530 struct manpage
**res
, size_t *ressz
)
532 struct manpage
*page
;
536 mandoc_asprintf(&file
, "%s/man%s/%s.%s",
537 paths
->paths
[ipath
], sec
, name
, sec
);
538 if (access(file
, R_OK
) != -1) {
544 mandoc_asprintf(&file
, "%s/cat%s/%s.0",
545 paths
->paths
[ipath
], sec
, name
);
546 if (access(file
, R_OK
) != -1) {
553 mandoc_asprintf(&file
, "%s/man%s/%s/%s.%s",
554 paths
->paths
[ipath
], sec
, arch
, name
, sec
);
555 if (access(file
, R_OK
) != -1) {
565 fprintf(stderr
, "%s: outdated mandoc.db lacks %s(%s) entry,\n"
566 " consider running # makewhatis %s\n",
567 progname
, name
, sec
, paths
->paths
[ipath
]);
570 *res
= mandoc_reallocarray(*res
, ++*ressz
, sizeof(struct manpage
));
571 page
= *res
+ (*ressz
- 1);
576 page
->bits
= NAME_FILE
& NAME_MASK
;
577 page
->sec
= (*sec
>= '1' && *sec
<= '9') ? *sec
- '1' + 1 : 10;
583 fs_search(const struct mansearch
*cfg
, const struct manpaths
*paths
,
584 int argc
, char **argv
, struct manpage
**res
, size_t *ressz
)
586 const char *const sections
[] =
587 {"1", "8", "6", "2", "3", "3p", "5", "7", "4", "9"};
588 const size_t nsec
= sizeof(sections
)/sizeof(sections
[0]);
590 size_t ipath
, isec
, lastsz
;
592 assert(cfg
->argmode
== ARG_NAME
);
597 for (ipath
= 0; ipath
< paths
->sz
; ipath
++) {
598 if (cfg
->sec
!= NULL
) {
599 if (fs_lookup(paths
, ipath
, cfg
->sec
,
600 cfg
->arch
, *argv
, res
, ressz
) &&
603 } else for (isec
= 0; isec
< nsec
; isec
++)
604 if (fs_lookup(paths
, ipath
, sections
[isec
],
605 cfg
->arch
, *argv
, res
, ressz
) &&
609 if (*ressz
== lastsz
)
611 "%s: No entry for %s in the manual.\n",
620 parse(struct curparse
*curp
, int fd
, const char *file
,
621 enum mandoclevel
*level
)
627 /* Begin by parsing the file itself. */
632 rc
= mparse_readfd(curp
->mp
, fd
, file
);
635 * With -Wstop and warnings or errors of at least the requested
636 * level, do not produce output.
639 if (MANDOCLEVEL_OK
!= rc
&& curp
->wstop
)
642 /* If unset, allocate output dev now (if applicable). */
644 if ( ! (curp
->outman
&& curp
->outmdoc
)) {
645 switch (curp
->outtype
) {
647 curp
->outdata
= html_alloc(curp
->mchars
,
649 curp
->outfree
= html_free
;
652 curp
->outdata
= utf8_alloc(curp
->mchars
,
654 curp
->outfree
= ascii_free
;
657 curp
->outdata
= locale_alloc(curp
->mchars
,
659 curp
->outfree
= ascii_free
;
662 curp
->outdata
= ascii_alloc(curp
->mchars
,
664 curp
->outfree
= ascii_free
;
667 curp
->outdata
= pdf_alloc(curp
->mchars
,
669 curp
->outfree
= pspdf_free
;
672 curp
->outdata
= ps_alloc(curp
->mchars
,
674 curp
->outfree
= pspdf_free
;
680 switch (curp
->outtype
) {
682 curp
->outman
= html_man
;
683 curp
->outmdoc
= html_mdoc
;
686 curp
->outman
= tree_man
;
687 curp
->outmdoc
= tree_mdoc
;
690 curp
->outmdoc
= man_mdoc
;
691 curp
->outman
= man_man
;
702 curp
->outman
= terminal_man
;
703 curp
->outmdoc
= terminal_mdoc
;
710 mparse_result(curp
->mp
, &mdoc
, &man
, NULL
);
712 /* Execute the out device, if it exists. */
714 if (man
&& curp
->outman
)
715 (*curp
->outman
)(curp
->outdata
, man
);
716 if (mdoc
&& curp
->outmdoc
)
717 (*curp
->outmdoc
)(curp
->outdata
, mdoc
);
724 static enum mandoclevel
725 passthrough(const char *file
, int fd
, int synopsis_only
)
727 const char synb
[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
728 const char synr
[] = "SYNOPSIS";
739 if ((stream
= fdopen(fd
, "r")) == NULL
) {
746 while ((line
= fgetln(stream
, &len
)) != NULL
) {
749 if ( ! isspace((unsigned char)*line
))
752 isspace((unsigned char)*line
)) {
757 if ((len
== sizeof(synb
) &&
758 ! strncmp(line
, synb
, len
- 1)) ||
759 (len
== sizeof(synr
) &&
760 ! strncmp(line
, synr
, len
- 1)))
765 for (off
= 0; off
< len
; off
+= nw
)
766 if ((nw
= write(STDOUT_FILENO
, line
+ off
,
767 len
- off
)) == -1 || nw
== 0) {
774 if (ferror(stream
)) {
782 return(MANDOCLEVEL_OK
);
785 fprintf(stderr
, "%s: %s: SYSERR: %s: %s",
786 progname
, file
, syscall
, strerror(errno
));
787 return(MANDOCLEVEL_SYSERR
);
791 koptions(int *options
, char *arg
)
794 if ( ! strcmp(arg
, "utf-8")) {
795 *options
|= MPARSE_UTF8
;
796 *options
&= ~MPARSE_LATIN1
;
797 } else if ( ! strcmp(arg
, "iso-8859-1")) {
798 *options
|= MPARSE_LATIN1
;
799 *options
&= ~MPARSE_UTF8
;
800 } else if ( ! strcmp(arg
, "us-ascii")) {
801 *options
&= ~(MPARSE_UTF8
| MPARSE_LATIN1
);
803 fprintf(stderr
, "%s: -K %s: Bad argument\n",
811 moptions(int *options
, char *arg
)
816 else if (0 == strcmp(arg
, "doc"))
817 *options
|= MPARSE_MDOC
;
818 else if (0 == strcmp(arg
, "andoc"))
820 else if (0 == strcmp(arg
, "an"))
821 *options
|= MPARSE_MAN
;
823 fprintf(stderr
, "%s: -m %s: Bad argument\n",
832 toptions(struct curparse
*curp
, char *arg
)
835 if (0 == strcmp(arg
, "ascii"))
836 curp
->outtype
= OUTT_ASCII
;
837 else if (0 == strcmp(arg
, "lint")) {
838 curp
->outtype
= OUTT_LINT
;
839 curp
->wlevel
= MANDOCLEVEL_WARNING
;
840 } else if (0 == strcmp(arg
, "tree"))
841 curp
->outtype
= OUTT_TREE
;
842 else if (0 == strcmp(arg
, "man"))
843 curp
->outtype
= OUTT_MAN
;
844 else if (0 == strcmp(arg
, "html"))
845 curp
->outtype
= OUTT_HTML
;
846 else if (0 == strcmp(arg
, "utf8"))
847 curp
->outtype
= OUTT_UTF8
;
848 else if (0 == strcmp(arg
, "locale"))
849 curp
->outtype
= OUTT_LOCALE
;
850 else if (0 == strcmp(arg
, "xhtml"))
851 curp
->outtype
= OUTT_HTML
;
852 else if (0 == strcmp(arg
, "ps"))
853 curp
->outtype
= OUTT_PS
;
854 else if (0 == strcmp(arg
, "pdf"))
855 curp
->outtype
= OUTT_PDF
;
857 fprintf(stderr
, "%s: -T %s: Bad argument\n",
866 woptions(struct curparse
*curp
, char *arg
)
881 switch (getsubopt(&arg
, UNCONST(toks
), &v
)) {
888 curp
->wlevel
= MANDOCLEVEL_WARNING
;
891 curp
->wlevel
= MANDOCLEVEL_ERROR
;
894 curp
->wlevel
= MANDOCLEVEL_UNSUPP
;
897 curp
->wlevel
= MANDOCLEVEL_BADARG
;
900 fprintf(stderr
, "%s: -W %s: Bad argument\n",
910 mmsg(enum mandocerr t
, enum mandoclevel lvl
,
911 const char *file
, int line
, int col
, const char *msg
)
913 const char *mparse_msg
;
915 fprintf(stderr
, "%s: %s:", progname
, file
);
918 fprintf(stderr
, "%d:%d:", line
, col
+ 1);
920 fprintf(stderr
, " %s", mparse_strlevel(lvl
));
922 if (NULL
!= (mparse_msg
= mparse_strerror(t
)))
923 fprintf(stderr
, ": %s", mparse_msg
);
926 fprintf(stderr
, ": %s", msg
);
934 #define MAX_PAGER_ARGS 16
935 char *argv
[MAX_PAGER_ARGS
];
941 if (pipe(fildes
) == -1) {
942 fprintf(stderr
, "%s: pipe: %s\n",
943 progname
, strerror(errno
));
949 fprintf(stderr
, "%s: fork: %s\n",
950 progname
, strerror(errno
));
951 exit((int)MANDOCLEVEL_SYSERR
);
954 if (dup2(fildes
[1], STDOUT_FILENO
) == -1) {
955 fprintf(stderr
, "%s: dup output: %s\n",
956 progname
, strerror(errno
));
957 exit((int)MANDOCLEVEL_SYSERR
);
964 /* The original process becomes the pager. */
967 if (dup2(fildes
[0], STDIN_FILENO
) == -1) {
968 fprintf(stderr
, "%s: dup input: %s\n",
969 progname
, strerror(errno
));
970 exit((int)MANDOCLEVEL_SYSERR
);
973 pager
= getenv("MANPAGER");
974 if (pager
== NULL
|| *pager
== '\0')
975 pager
= getenv("PAGER");
976 if (pager
== NULL
|| *pager
== '\0')
977 pager
= "/usr/bin/more -s";
978 cp
= mandoc_strdup(pager
);
981 * Parse the pager command into words.
982 * Intentionally do not do anything fancy here.
986 while (argc
+ 1 < MAX_PAGER_ARGS
) {
988 cp
= strchr(cp
, ' ');
999 /* Hand over to the pager. */
1001 execvp(argv
[0], argv
);
1002 fprintf(stderr
, "%s: exec: %s\n",
1003 progname
, strerror(errno
));
1004 exit((int)MANDOCLEVEL_SYSERR
);