]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
1 /* $Id: main.c,v 1.213 2015/01/13 23:17:52 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>
34 #include "mandoc_aux.h"
39 #include "mansearch.h"
41 #if !defined(__GNUC__) || (__GNUC__ < 2)
43 # define __attribute__(x)
45 #endif /* !defined(__GNUC__) || (__GNUC__ < 2) */
56 typedef void (*out_mdoc
)(void *, const struct mdoc
*);
57 typedef void (*out_man
)(void *, const struct man
*);
58 typedef void (*out_free
)(void *);
61 OUTT_ASCII
= 0, /* -Tascii */
62 OUTT_LOCALE
, /* -Tlocale */
63 OUTT_UTF8
, /* -Tutf8 */
64 OUTT_TREE
, /* -Ttree */
66 OUTT_HTML
, /* -Thtml */
67 OUTT_LINT
, /* -Tlint */
74 struct mchars
*mchars
; /* character table */
75 enum mandoclevel wlevel
; /* ignore messages below this */
76 int wstop
; /* stop after a file with a warning */
77 enum outt outtype
; /* which output to use */
78 out_mdoc outmdoc
; /* mdoc output ptr */
79 out_man outman
; /* man output ptr */
80 out_free outfree
; /* free output ptr */
81 void *outdata
; /* data for output */
82 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 *);
94 static int koptions(int *, char *);
96 int mandocdb(int, char**);
98 static int moptions(int *, char *);
99 static void mmsg(enum mandocerr
, enum mandoclevel
,
100 const char *, int, int, const char *);
101 static void parse(struct curparse
*, int,
102 const char *, enum mandoclevel
*);
104 static enum mandoclevel
passthrough(const char *, int, int);
106 static void spawn_pager(void);
107 static int toptions(struct curparse
*, char *);
108 static void usage(enum argmode
) __attribute__((noreturn
));
109 static void version(void) __attribute__((noreturn
));
110 static int woptions(struct curparse
*, char *);
112 static const int sec_prios
[] = {1, 4, 5, 8, 6, 3, 7, 2, 9};
113 static char help_arg
[] = "help";
114 static char *help_argv
[] = {help_arg
, NULL
};
115 static const char *progname
;
119 main(int argc
, char *argv
[])
121 struct curparse curp
;
122 struct mansearch search
;
123 struct manpaths paths
;
128 struct manpage
*res
, *resp
;
129 char *conf_file
, *defpaths
;
131 int prio
, best_prio
, synopsis_only
;
135 enum outmode outmode
;
142 progname
= strrchr(argv
[0], '/');
143 if (progname
== NULL
)
149 if (strcmp(progname
, BINM_MAKEWHATIS
) == 0)
150 return(mandocdb(argc
, argv
));
153 /* Search options. */
155 memset(&paths
, 0, sizeof(struct manpaths
));
157 conf_file
= defpaths
= NULL
;
161 memset(&search
, 0, sizeof(struct mansearch
));
162 search
.outkey
= "Nd";
164 if (strcmp(progname
, BINM_MAN
) == 0)
165 search
.argmode
= ARG_NAME
;
166 else if (strcmp(progname
, BINM_APROPOS
) == 0)
167 search
.argmode
= ARG_EXPR
;
168 else if (strcmp(progname
, BINM_WHATIS
) == 0)
169 search
.argmode
= ARG_WORD
;
170 else if (strncmp(progname
, "help", 4) == 0)
171 search
.argmode
= ARG_NAME
;
173 search
.argmode
= ARG_FILE
;
175 /* Parser and formatter options. */
177 memset(&curp
, 0, sizeof(struct curparse
));
178 curp
.outtype
= OUTT_LOCALE
;
179 curp
.wlevel
= MANDOCLEVEL_FATAL
;
180 options
= MPARSE_SO
| MPARSE_UTF8
| MPARSE_LATIN1
;
188 outmode
= OUTMODE_DEF
;
190 while (-1 != (c
= getopt(argc
, argv
,
191 "aC:cfhI:iK:klM:m:O:S:s:T:VW:w"))) {
194 outmode
= OUTMODE_ALL
;
205 search
.argmode
= ARG_WORD
;
208 (void)strlcat(curp
.outopts
, "synopsis,", BUFSIZ
);
213 outmode
= OUTMODE_ALL
;
216 if (strncmp(optarg
, "os=", 3)) {
218 "%s: -I %s: Bad argument\n",
220 return((int)MANDOCLEVEL_BADARG
);
224 "%s: -I %s: Duplicate argument\n",
226 return((int)MANDOCLEVEL_BADARG
);
228 defos
= mandoc_strdup(optarg
+ 3);
231 outmode
= OUTMODE_INT
;
234 if ( ! koptions(&options
, optarg
))
235 return((int)MANDOCLEVEL_BADARG
);
238 search
.argmode
= ARG_EXPR
;
241 search
.argmode
= ARG_FILE
;
242 outmode
= OUTMODE_ALL
;
253 search
.outkey
= optarg
;
254 (void)strlcat(curp
.outopts
, optarg
, BUFSIZ
);
255 (void)strlcat(curp
.outopts
, ",", BUFSIZ
);
258 search
.arch
= optarg
;
264 if ( ! toptions(&curp
, optarg
))
265 return((int)MANDOCLEVEL_BADARG
);
268 if ( ! woptions(&curp
, optarg
))
269 return((int)MANDOCLEVEL_BADARG
);
272 outmode
= OUTMODE_FLN
;
284 usage(search
.argmode
);
286 /* Postprocess options. */
288 if (outmode
== OUTMODE_DEF
) {
289 switch (search
.argmode
) {
291 outmode
= OUTMODE_ALL
;
295 outmode
= OUTMODE_ONE
;
298 outmode
= OUTMODE_LST
;
303 /* Parse arguments. */
313 * and for a man(1) section argument without -s.
316 if (search
.argmode
== ARG_NAME
) {
317 if (*progname
== 'h') {
322 } else if (argc
> 1 &&
323 ((uc
= argv
[0]) != NULL
) &&
324 ((isdigit(uc
[0]) && (uc
[1] == '\0' ||
325 (isalpha(uc
[1]) && uc
[2] == '\0'))) ||
326 (uc
[0] == 'n' && uc
[1] == '\0'))) {
335 /* man(1), whatis(1), apropos(1) */
337 if (search
.argmode
!= ARG_FILE
) {
340 usage(search
.argmode
);
342 if (search
.argmode
== ARG_NAME
&&
343 outmode
== OUTMODE_ONE
)
344 search
.firstmatch
= 1;
346 /* Access the mandoc database. */
348 manpath_parse(&paths
, conf_file
, defpaths
, auxpaths
);
350 if( ! mansearch(&search
, &paths
, argc
, argv
, &res
, &sz
))
351 usage(search
.argmode
);
353 if (sz
== 0 && search
.argmode
== ARG_NAME
)
354 fs_search(&search
, &paths
, argc
, argv
, &res
, &sz
);
357 rc
= MANDOCLEVEL_BADARG
;
362 * For standard man(1) and -a output mode,
363 * prepare for copying filename pointers
364 * into the program parameter array.
367 if (outmode
== OUTMODE_ONE
) {
370 } else if (outmode
== OUTMODE_ALL
)
373 /* Iterate all matching manuals. */
376 for (i
= 0; i
< sz
; i
++) {
377 if (outmode
== OUTMODE_FLN
)
379 else if (outmode
== OUTMODE_LST
)
380 printf("%s - %s\n", res
[i
].names
,
381 res
[i
].output
== NULL
? "" :
383 else if (outmode
== OUTMODE_ONE
) {
384 /* Search for the best section. */
385 isec
= strcspn(res
[i
].file
, "123456789");
386 sec
= res
[i
].file
[isec
];
389 prio
= sec_prios
[sec
- '1'];
390 if (prio
>= best_prio
)
398 * For man(1), -a and -i output mode, fall through
399 * to the main mandoc(1) code iterating files
400 * and running the parsers on each of them.
403 if (outmode
== OUTMODE_FLN
|| outmode
== OUTMODE_LST
)
406 fputs("mandoc: database support not compiled in\n",
408 return((int)MANDOCLEVEL_BADARG
);
414 if (search
.argmode
== ARG_FILE
&& ! moptions(&options
, auxpaths
))
415 return((int)MANDOCLEVEL_BADARG
);
417 curp
.mchars
= mchars_alloc();
418 curp
.mp
= mparse_alloc(options
, curp
.wlevel
, mmsg
,
422 * Conditionally start up the lookaside buffer before parsing.
424 if (OUTT_MAN
== curp
.outtype
)
425 mparse_keep(curp
.mp
);
428 if (use_pager
&& isatty(STDOUT_FILENO
))
430 parse(&curp
, STDIN_FILENO
, "<stdin>", &rc
);
434 rc
= mparse_open(curp
.mp
, &fd
,
436 resp
!= NULL
? resp
->file
:
441 if (use_pager
&& isatty(STDOUT_FILENO
))
448 parse(&curp
, fd
, *argv
, &rc
);
450 else if (resp
->form
& FORM_SRC
) {
451 /* For .so only; ignore failure. */
452 chdir(paths
.paths
[resp
->ipath
]);
453 parse(&curp
, fd
, resp
->file
, &rc
);
455 rc
= passthrough(resp
->file
, fd
,
459 if (mparse_wait(curp
.mp
) != MANDOCLEVEL_OK
)
460 rc
= MANDOCLEVEL_SYSERR
;
462 if (argc
> 1 && curp
.outtype
<= OUTT_UTF8
)
463 ascii_sepline(curp
.outdata
);
466 if (MANDOCLEVEL_OK
!= rc
&& curp
.wstop
)
476 mparse_reset(curp
.mp
);
480 (*curp
.outfree
)(curp
.outdata
);
481 mparse_free(curp
.mp
);
482 mchars_free(curp
.mchars
);
486 if (search
.argmode
!= ARG_FILE
) {
487 manpath_free(&paths
);
488 mansearch_free(res
, sz
);
502 printf("mandoc %s\n", VERSION
);
503 exit((int)MANDOCLEVEL_OK
);
507 usage(enum argmode argmode
)
512 fputs("usage: mandoc [-acfhklV] [-Ios=name] "
513 "[-Kencoding] [-mformat] [-Ooption]\n"
514 "\t [-Toutput] [-Wlevel] [file ...]\n", stderr
);
517 fputs("usage: man [-acfhklVw] [-C file] [-I os=name] "
518 "[-K encoding] [-M path] [-m path]\n"
519 "\t [-O option=value] [-S subsection] [-s section] "
520 "[-T output] [-W level]\n"
521 "\t [section] name ...\n", stderr
);
524 fputs("usage: whatis [-acfhklVw] [-C file] "
525 "[-M path] [-m path] [-O outkey] [-S arch]\n"
526 "\t [-s section] name ...\n", stderr
);
529 fputs("usage: apropos [-acfhklVw] [-C file] "
530 "[-M path] [-m path] [-O outkey] [-S arch]\n"
531 "\t [-s section] expression ...\n", stderr
);
534 exit((int)MANDOCLEVEL_BADARG
);
539 fs_lookup(const struct manpaths
*paths
, size_t ipath
,
540 const char *sec
, const char *arch
, const char *name
,
541 struct manpage
**res
, size_t *ressz
)
543 struct manpage
*page
;
547 mandoc_asprintf(&file
, "%s/man%s/%s.%s",
548 paths
->paths
[ipath
], sec
, name
, sec
);
549 if (access(file
, R_OK
) != -1) {
555 mandoc_asprintf(&file
, "%s/cat%s/%s.0",
556 paths
->paths
[ipath
], sec
, name
);
557 if (access(file
, R_OK
) != -1) {
564 mandoc_asprintf(&file
, "%s/man%s/%s/%s.%s",
565 paths
->paths
[ipath
], sec
, arch
, name
, sec
);
566 if (access(file
, R_OK
) != -1) {
575 fprintf(stderr
, "%s: outdated mandoc.db lacks %s(%s) entry,\n"
576 " consider running # makewhatis %s\n",
577 progname
, name
, sec
, paths
->paths
[ipath
]);
579 *res
= mandoc_reallocarray(*res
, ++*ressz
, sizeof(struct manpage
));
580 page
= *res
+ (*ressz
- 1);
585 page
->bits
= NAME_FILE
& NAME_MASK
;
586 page
->sec
= (*sec
>= '1' && *sec
<= '9') ? *sec
- '1' + 1 : 10;
592 fs_search(const struct mansearch
*cfg
, const struct manpaths
*paths
,
593 int argc
, char **argv
, struct manpage
**res
, size_t *ressz
)
595 const char *const sections
[] =
596 {"1", "8", "6", "2", "3", "3p", "5", "7", "4", "9"};
597 const size_t nsec
= sizeof(sections
)/sizeof(sections
[0]);
599 size_t ipath
, isec
, lastsz
;
601 assert(cfg
->argmode
== ARG_NAME
);
606 for (ipath
= 0; ipath
< paths
->sz
; ipath
++) {
607 if (cfg
->sec
!= NULL
) {
608 if (fs_lookup(paths
, ipath
, cfg
->sec
,
609 cfg
->arch
, *argv
, res
, ressz
) &&
612 } else for (isec
= 0; isec
< nsec
; isec
++)
613 if (fs_lookup(paths
, ipath
, sections
[isec
],
614 cfg
->arch
, *argv
, res
, ressz
) &&
618 if (*ressz
== lastsz
)
620 "%s: No entry for %s in the manual.\n",
630 parse(struct curparse
*curp
, int fd
, const char *file
,
631 enum mandoclevel
*level
)
637 /* Begin by parsing the file itself. */
642 rc
= mparse_readfd(curp
->mp
, fd
, file
);
644 /* Stop immediately if the parse has failed. */
646 if (MANDOCLEVEL_FATAL
<= rc
)
650 * With -Wstop and warnings or errors of at least the requested
651 * level, do not produce output.
654 if (MANDOCLEVEL_OK
!= rc
&& curp
->wstop
)
657 /* If unset, allocate output dev now (if applicable). */
659 if ( ! (curp
->outman
&& curp
->outmdoc
)) {
660 switch (curp
->outtype
) {
662 curp
->outdata
= html_alloc(curp
->mchars
,
664 curp
->outfree
= html_free
;
667 curp
->outdata
= utf8_alloc(curp
->mchars
,
669 curp
->outfree
= ascii_free
;
672 curp
->outdata
= locale_alloc(curp
->mchars
,
674 curp
->outfree
= ascii_free
;
677 curp
->outdata
= ascii_alloc(curp
->mchars
,
679 curp
->outfree
= ascii_free
;
682 curp
->outdata
= pdf_alloc(curp
->mchars
,
684 curp
->outfree
= pspdf_free
;
687 curp
->outdata
= ps_alloc(curp
->mchars
,
689 curp
->outfree
= pspdf_free
;
695 switch (curp
->outtype
) {
697 curp
->outman
= html_man
;
698 curp
->outmdoc
= html_mdoc
;
701 curp
->outman
= tree_man
;
702 curp
->outmdoc
= tree_mdoc
;
705 curp
->outmdoc
= man_mdoc
;
706 curp
->outman
= man_man
;
717 curp
->outman
= terminal_man
;
718 curp
->outmdoc
= terminal_mdoc
;
725 mparse_result(curp
->mp
, &mdoc
, &man
, NULL
);
727 /* Execute the out device, if it exists. */
729 if (man
&& curp
->outman
)
730 (*curp
->outman
)(curp
->outdata
, man
);
731 if (mdoc
&& curp
->outmdoc
)
732 (*curp
->outmdoc
)(curp
->outdata
, mdoc
);
740 static enum mandoclevel
741 passthrough(const char *file
, int fd
, int synopsis_only
)
743 const char synb
[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
744 const char synr
[] = "SYNOPSIS";
755 if ((stream
= fdopen(fd
, "r")) == NULL
) {
762 while ((line
= fgetln(stream
, &len
)) != NULL
) {
765 if ( ! isspace((unsigned char)*line
))
768 isspace((unsigned char)*line
)) {
773 if ((len
== sizeof(synb
) &&
774 ! strncmp(line
, synb
, len
- 1)) ||
775 (len
== sizeof(synr
) &&
776 ! strncmp(line
, synr
, len
- 1)))
781 for (off
= 0; off
< len
; off
+= nw
)
782 if ((nw
= write(STDOUT_FILENO
, line
+ off
,
783 len
- off
)) == -1 || nw
== 0) {
790 if (ferror(stream
)) {
798 return(MANDOCLEVEL_OK
);
801 fprintf(stderr
, "%s: %s: SYSERR: %s: %s",
802 progname
, file
, syscall
, strerror(errno
));
803 return(MANDOCLEVEL_SYSERR
);
808 koptions(int *options
, char *arg
)
811 if ( ! strcmp(arg
, "utf-8")) {
812 *options
|= MPARSE_UTF8
;
813 *options
&= ~MPARSE_LATIN1
;
814 } else if ( ! strcmp(arg
, "iso-8859-1")) {
815 *options
|= MPARSE_LATIN1
;
816 *options
&= ~MPARSE_UTF8
;
817 } else if ( ! strcmp(arg
, "us-ascii")) {
818 *options
&= ~(MPARSE_UTF8
| MPARSE_LATIN1
);
820 fprintf(stderr
, "%s: -K %s: Bad argument\n",
828 moptions(int *options
, char *arg
)
833 else if (0 == strcmp(arg
, "doc"))
834 *options
|= MPARSE_MDOC
;
835 else if (0 == strcmp(arg
, "andoc"))
837 else if (0 == strcmp(arg
, "an"))
838 *options
|= MPARSE_MAN
;
840 fprintf(stderr
, "%s: -m %s: Bad argument\n",
849 toptions(struct curparse
*curp
, char *arg
)
852 if (0 == strcmp(arg
, "ascii"))
853 curp
->outtype
= OUTT_ASCII
;
854 else if (0 == strcmp(arg
, "lint")) {
855 curp
->outtype
= OUTT_LINT
;
856 curp
->wlevel
= MANDOCLEVEL_WARNING
;
857 } else if (0 == strcmp(arg
, "tree"))
858 curp
->outtype
= OUTT_TREE
;
859 else if (0 == strcmp(arg
, "man"))
860 curp
->outtype
= OUTT_MAN
;
861 else if (0 == strcmp(arg
, "html"))
862 curp
->outtype
= OUTT_HTML
;
863 else if (0 == strcmp(arg
, "utf8"))
864 curp
->outtype
= OUTT_UTF8
;
865 else if (0 == strcmp(arg
, "locale"))
866 curp
->outtype
= OUTT_LOCALE
;
867 else if (0 == strcmp(arg
, "xhtml"))
868 curp
->outtype
= OUTT_HTML
;
869 else if (0 == strcmp(arg
, "ps"))
870 curp
->outtype
= OUTT_PS
;
871 else if (0 == strcmp(arg
, "pdf"))
872 curp
->outtype
= OUTT_PDF
;
874 fprintf(stderr
, "%s: -T %s: Bad argument\n",
883 woptions(struct curparse
*curp
, char *arg
)
897 switch (getsubopt(&arg
, UNCONST(toks
), &v
)) {
904 curp
->wlevel
= MANDOCLEVEL_WARNING
;
907 curp
->wlevel
= MANDOCLEVEL_ERROR
;
910 curp
->wlevel
= MANDOCLEVEL_FATAL
;
913 fprintf(stderr
, "%s: -W %s: Bad argument\n",
923 mmsg(enum mandocerr t
, enum mandoclevel lvl
,
924 const char *file
, int line
, int col
, const char *msg
)
926 const char *mparse_msg
;
928 fprintf(stderr
, "%s: %s:", progname
, file
);
931 fprintf(stderr
, "%d:%d:", line
, col
+ 1);
933 fprintf(stderr
, " %s", mparse_strlevel(lvl
));
935 if (NULL
!= (mparse_msg
= mparse_strerror(t
)))
936 fprintf(stderr
, ": %s", mparse_msg
);
939 fprintf(stderr
, ": %s", msg
);
947 #define MAX_PAGER_ARGS 16
948 char *argv
[MAX_PAGER_ARGS
];
954 if (pipe(fildes
) == -1) {
955 fprintf(stderr
, "%s: pipe: %s\n",
956 progname
, strerror(errno
));
962 fprintf(stderr
, "%s: fork: %s\n",
963 progname
, strerror(errno
));
964 exit((int)MANDOCLEVEL_SYSERR
);
967 if (dup2(fildes
[1], STDOUT_FILENO
) == -1) {
968 fprintf(stderr
, "%s: dup output: %s\n",
969 progname
, strerror(errno
));
970 exit((int)MANDOCLEVEL_SYSERR
);
977 /* The original process becomes the pager. */
980 if (dup2(fildes
[0], STDIN_FILENO
) == -1) {
981 fprintf(stderr
, "%s: dup input: %s\n",
982 progname
, strerror(errno
));
983 exit((int)MANDOCLEVEL_SYSERR
);
986 pager
= getenv("MANPAGER");
987 if (pager
== NULL
|| *pager
== '\0')
988 pager
= getenv("PAGER");
989 if (pager
== NULL
|| *pager
== '\0')
990 pager
= "/usr/bin/more -s";
991 cp
= mandoc_strdup(pager
);
994 * Parse the pager command into words.
995 * Intentionally do not do anything fancy here.
999 while (argc
+ 1 < MAX_PAGER_ARGS
) {
1001 cp
= strchr(cp
, ' ');
1012 /* Hand over to the pager. */
1014 execvp(argv
[0], argv
);
1015 fprintf(stderr
, "%s: exec: %s\n",
1016 progname
, strerror(errno
));
1017 exit((int)MANDOCLEVEL_SYSERR
);