]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
1 /* $Id: main.c,v 1.238 2015/04/29 11:04:17 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 AUTHORS DISCLAIM ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS 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 */
37 #include "mandoc_aux.h"
44 #include "mansearch.h"
46 #if !defined(__GNUC__) || (__GNUC__ < 2)
48 # define __attribute__(x)
50 #endif /* !defined(__GNUC__) || (__GNUC__ < 2) */
61 typedef void (*out_mdoc
)(void *, const struct roff_man
*);
62 typedef void (*out_man
)(void *, const struct roff_man
*);
63 typedef void (*out_free
)(void *);
66 OUTT_ASCII
= 0, /* -Tascii */
67 OUTT_LOCALE
, /* -Tlocale */
68 OUTT_UTF8
, /* -Tutf8 */
69 OUTT_TREE
, /* -Ttree */
71 OUTT_HTML
, /* -Thtml */
72 OUTT_LINT
, /* -Tlint */
79 struct mchars
*mchars
; /* character table */
80 enum mandoclevel wlevel
; /* ignore messages below this */
81 int wstop
; /* stop after a file with a warning */
82 enum outt outtype
; /* which output to use */
83 out_mdoc outmdoc
; /* mdoc output ptr */
84 out_man outman
; /* man output ptr */
85 out_free outfree
; /* free output ptr */
86 void *outdata
; /* data for output */
87 struct manoutput
*outopts
; /* output options */
90 static int fs_lookup(const struct manpaths
*,
91 size_t ipath
, const char *,
92 const char *, const char *,
93 struct manpage
**, size_t *);
94 static void fs_search(const struct mansearch
*,
95 const struct manpaths
*, int, char**,
96 struct manpage
**, size_t *);
97 static void handle_sigpipe(int);
98 static int koptions(int *, char *);
100 int mandocdb(int, char**);
102 static int moptions(int *, char *);
103 static void mmsg(enum mandocerr
, enum mandoclevel
,
104 const char *, int, int, const char *);
105 static void parse(struct curparse
*, int, const char *);
106 static void passthrough(const char *, int, int);
107 static pid_t
spawn_pager(void);
108 static int toptions(struct curparse
*, char *);
109 static void usage(enum argmode
) __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
;
116 static enum mandoclevel rc
;
120 main(int argc
, char *argv
[])
123 struct curparse curp
;
124 struct mansearch search
;
128 struct manpage
*res
, *resp
;
129 char *conf_file
, *defpaths
;
133 enum mandoclevel rctmp
;
134 enum outmode outmode
;
139 pid_t pager_pid
; /* 0: don't use; 1: not yet spawned. */
143 else if ((progname
= strrchr(argv
[0], '/')) == NULL
)
149 if (strcmp(progname
, BINM_MAKEWHATIS
) == 0)
150 return(mandocdb(argc
, argv
));
153 /* Search options. */
155 memset(&conf
, 0, sizeof(conf
));
156 conf_file
= defpaths
= NULL
;
159 memset(&search
, 0, sizeof(struct mansearch
));
160 search
.outkey
= "Nd";
162 if (strcmp(progname
, BINM_MAN
) == 0)
163 search
.argmode
= ARG_NAME
;
164 else if (strcmp(progname
, BINM_APROPOS
) == 0)
165 search
.argmode
= ARG_EXPR
;
166 else if (strcmp(progname
, BINM_WHATIS
) == 0)
167 search
.argmode
= ARG_WORD
;
168 else if (strncmp(progname
, "help", 4) == 0)
169 search
.argmode
= ARG_NAME
;
171 search
.argmode
= ARG_FILE
;
173 /* Parser and formatter options. */
175 memset(&curp
, 0, sizeof(struct curparse
));
176 curp
.outtype
= OUTT_LOCALE
;
177 curp
.wlevel
= MANDOCLEVEL_BADARG
;
178 curp
.outopts
= &conf
.output
;
179 options
= MPARSE_SO
| MPARSE_UTF8
| MPARSE_LATIN1
;
184 outmode
= OUTMODE_DEF
;
186 while (-1 != (c
= getopt(argc
, argv
,
187 "aC:cfhI:iK:klM:m:O:S:s:T:VW:w"))) {
190 outmode
= OUTMODE_ALL
;
199 search
.argmode
= ARG_WORD
;
202 conf
.output
.synopsisonly
= 1;
204 outmode
= OUTMODE_ALL
;
207 if (strncmp(optarg
, "os=", 3)) {
209 "%s: -I %s: Bad argument\n",
211 return((int)MANDOCLEVEL_BADARG
);
215 "%s: -I %s: Duplicate argument\n",
217 return((int)MANDOCLEVEL_BADARG
);
219 defos
= mandoc_strdup(optarg
+ 3);
222 outmode
= OUTMODE_INT
;
225 if ( ! koptions(&options
, optarg
))
226 return((int)MANDOCLEVEL_BADARG
);
229 search
.argmode
= ARG_EXPR
;
232 search
.argmode
= ARG_FILE
;
233 outmode
= OUTMODE_ALL
;
242 search
.outkey
= optarg
;
243 while (optarg
!= NULL
)
244 manconf_output(&conf
.output
,
245 strsep(&optarg
, ","));
248 search
.arch
= optarg
;
254 if ( ! toptions(&curp
, optarg
))
255 return((int)MANDOCLEVEL_BADARG
);
258 if ( ! woptions(&curp
, optarg
))
259 return((int)MANDOCLEVEL_BADARG
);
262 outmode
= OUTMODE_FLN
;
271 usage(search
.argmode
);
273 /* Postprocess options. */
275 if (outmode
== OUTMODE_DEF
) {
276 switch (search
.argmode
) {
278 outmode
= OUTMODE_ALL
;
282 outmode
= OUTMODE_ONE
;
285 outmode
= OUTMODE_LST
;
290 /* Parse arguments. */
300 * and for a man(1) section argument without -s.
303 if (search
.argmode
== ARG_NAME
) {
304 if (*progname
== 'h') {
309 } else if (argc
> 1 &&
310 ((uc
= (unsigned char *)argv
[0]) != NULL
) &&
311 ((isdigit(uc
[0]) && (uc
[1] == '\0' ||
312 (isalpha(uc
[1]) && uc
[2] == '\0'))) ||
313 (uc
[0] == 'n' && uc
[1] == '\0'))) {
314 search
.sec
= (char *)uc
;
318 if (search
.arch
== NULL
)
319 search
.arch
= getenv("MACHINE");
321 if (search
.arch
== NULL
)
322 search
.arch
= MACHINE
;
328 /* man(1), whatis(1), apropos(1) */
330 if (search
.argmode
!= ARG_FILE
) {
332 usage(search
.argmode
);
334 if (search
.argmode
== ARG_NAME
&&
335 outmode
== OUTMODE_ONE
)
336 search
.firstmatch
= 1;
338 /* Access the mandoc database. */
340 manconf_parse(&conf
, conf_file
, defpaths
, auxpaths
);
343 if ( ! mansearch(&search
, &conf
.manpath
,
344 argc
, argv
, &res
, &sz
))
345 usage(search
.argmode
);
347 if (search
.argmode
!= ARG_NAME
) {
348 fputs("mandoc: database support not compiled in\n",
350 return((int)MANDOCLEVEL_BADARG
);
356 if (search
.argmode
== ARG_NAME
)
357 fs_search(&search
, &conf
.manpath
,
358 argc
, argv
, &res
, &sz
);
361 "%s: nothing appropriate\n",
366 rc
= MANDOCLEVEL_BADARG
;
371 * For standard man(1) and -a output mode,
372 * prepare for copying filename pointers
373 * into the program parameter array.
376 if (outmode
== OUTMODE_ONE
) {
379 } else if (outmode
== OUTMODE_ALL
)
382 /* Iterate all matching manuals. */
385 for (i
= 0; i
< sz
; i
++) {
386 if (outmode
== OUTMODE_FLN
)
388 else if (outmode
== OUTMODE_LST
)
389 printf("%s - %s\n", res
[i
].names
,
390 res
[i
].output
== NULL
? "" :
392 else if (outmode
== OUTMODE_ONE
) {
393 /* Search for the best section. */
394 isec
= strcspn(res
[i
].file
, "123456789");
395 sec
= res
[i
].file
[isec
];
398 prio
= sec_prios
[sec
- '1'];
399 if (prio
>= best_prio
)
407 * For man(1), -a and -i output mode, fall through
408 * to the main mandoc(1) code iterating files
409 * and running the parsers on each of them.
412 if (outmode
== OUTMODE_FLN
|| outmode
== OUTMODE_LST
)
418 if (search
.argmode
== ARG_FILE
&& ! moptions(&options
, auxpaths
))
419 return((int)MANDOCLEVEL_BADARG
);
421 curp
.mchars
= mchars_alloc();
422 curp
.mp
= mparse_alloc(options
, curp
.wlevel
, mmsg
,
426 * Conditionally start up the lookaside buffer before parsing.
428 if (OUTT_MAN
== curp
.outtype
)
429 mparse_keep(curp
.mp
);
432 if (pager_pid
== 1 && isatty(STDOUT_FILENO
))
433 pager_pid
= spawn_pager();
434 parse(&curp
, STDIN_FILENO
, "<stdin>");
438 rctmp
= mparse_open(curp
.mp
, &fd
,
439 resp
!= NULL
? resp
->file
: *argv
);
444 if (pager_pid
== 1 && isatty(STDOUT_FILENO
))
445 pager_pid
= spawn_pager();
448 parse(&curp
, fd
, *argv
);
449 else if (resp
->form
& FORM_SRC
) {
450 /* For .so only; ignore failure. */
451 chdir(conf
.manpath
.paths
[resp
->ipath
]);
452 parse(&curp
, fd
, resp
->file
);
454 passthrough(resp
->file
, fd
,
455 conf
.output
.synopsisonly
);
457 rctmp
= mparse_wait(curp
.mp
);
461 if (argc
> 1 && curp
.outtype
<= OUTT_UTF8
)
462 ascii_sepline(curp
.outdata
);
465 if (MANDOCLEVEL_OK
!= rc
&& curp
.wstop
)
473 mparse_reset(curp
.mp
);
477 (*curp
.outfree
)(curp
.outdata
);
478 mparse_free(curp
.mp
);
479 mchars_free(curp
.mchars
);
482 if (search
.argmode
!= ARG_FILE
) {
485 mansearch_free(res
, sz
);
493 * If a pager is attached, flush the pipe leading to it
494 * and signal end of file such that the user can browse
495 * to the end. Then wait for the user to close the pager.
498 if (pager_pid
!= 0 && pager_pid
!= 1) {
500 waitpid(pager_pid
, NULL
, 0);
507 usage(enum argmode argmode
)
512 fputs("usage: mandoc [-acfhkl] [-I os=name] "
513 "[-K encoding] [-mformat] [-O option]\n"
514 "\t [-T output] [-W level] [file ...]\n", stderr
);
517 fputs("usage: man [-acfhklw] [-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 [-acfhklw] [-C file] "
525 "[-M path] [-m path] [-O outkey] [-S arch]\n"
526 "\t [-s section] name ...\n", stderr
);
529 fputs("usage: apropos [-acfhklw] [-C file] "
530 "[-M path] [-m path] [-O outkey] [-S arch]\n"
531 "\t [-s section] expression ...\n", stderr
);
534 exit((int)MANDOCLEVEL_BADARG
);
538 fs_lookup(const struct manpaths
*paths
, size_t ipath
,
539 const char *sec
, const char *arch
, const char *name
,
540 struct manpage
**res
, size_t *ressz
)
543 struct manpage
*page
;
548 mandoc_asprintf(&file
, "%s/man%s/%s.%s",
549 paths
->paths
[ipath
], sec
, name
, sec
);
550 if (access(file
, R_OK
) != -1)
554 mandoc_asprintf(&file
, "%s/cat%s/%s.0",
555 paths
->paths
[ipath
], sec
, name
);
556 if (access(file
, R_OK
) != -1) {
563 mandoc_asprintf(&file
, "%s/man%s/%s/%s.%s",
564 paths
->paths
[ipath
], sec
, arch
, name
, sec
);
565 if (access(file
, R_OK
) != -1)
570 mandoc_asprintf(&file
, "%s/man%s/%s.*",
571 paths
->paths
[ipath
], sec
, name
);
572 globres
= glob(file
, 0, NULL
, &globinfo
);
573 if (globres
!= 0 && globres
!= GLOB_NOMATCH
)
574 fprintf(stderr
, "%s: %s: glob: %s\n",
575 progname
, file
, strerror(errno
));
578 file
= mandoc_strdup(*globinfo
.gl_pathv
);
585 fprintf(stderr
, "%s: outdated mandoc.db lacks %s(%s) entry, run "
586 "makewhatis %s\n", progname
, name
, sec
, paths
->paths
[ipath
]);
588 *res
= mandoc_reallocarray(*res
, ++*ressz
, sizeof(struct manpage
));
589 page
= *res
+ (*ressz
- 1);
594 page
->bits
= NAME_FILE
& NAME_MASK
;
595 page
->sec
= (*sec
>= '1' && *sec
<= '9') ? *sec
- '1' + 1 : 10;
601 fs_search(const struct mansearch
*cfg
, const struct manpaths
*paths
,
602 int argc
, char **argv
, struct manpage
**res
, size_t *ressz
)
604 const char *const sections
[] =
605 {"1", "8", "6", "2", "3", "3p", "5", "7", "4", "9"};
606 const size_t nsec
= sizeof(sections
)/sizeof(sections
[0]);
608 size_t ipath
, isec
, lastsz
;
610 assert(cfg
->argmode
== ARG_NAME
);
615 for (ipath
= 0; ipath
< paths
->sz
; ipath
++) {
616 if (cfg
->sec
!= NULL
) {
617 if (fs_lookup(paths
, ipath
, cfg
->sec
,
618 cfg
->arch
, *argv
, res
, ressz
) &&
621 } else for (isec
= 0; isec
< nsec
; isec
++)
622 if (fs_lookup(paths
, ipath
, sections
[isec
],
623 cfg
->arch
, *argv
, res
, ressz
) &&
627 if (*ressz
== lastsz
)
629 "%s: No entry for %s in the manual.\n",
638 parse(struct curparse
*curp
, int fd
, const char *file
)
640 enum mandoclevel rctmp
;
641 struct roff_man
*man
;
643 /* Begin by parsing the file itself. */
648 rctmp
= mparse_readfd(curp
->mp
, fd
, file
);
653 * With -Wstop and warnings or errors of at least the requested
654 * level, do not produce output.
657 if (rctmp
!= MANDOCLEVEL_OK
&& curp
->wstop
)
660 /* If unset, allocate output dev now (if applicable). */
662 if ( ! (curp
->outman
&& curp
->outmdoc
)) {
663 switch (curp
->outtype
) {
665 curp
->outdata
= html_alloc(curp
->mchars
,
667 curp
->outfree
= html_free
;
670 curp
->outdata
= utf8_alloc(curp
->mchars
,
672 curp
->outfree
= ascii_free
;
675 curp
->outdata
= locale_alloc(curp
->mchars
,
677 curp
->outfree
= ascii_free
;
680 curp
->outdata
= ascii_alloc(curp
->mchars
,
682 curp
->outfree
= ascii_free
;
685 curp
->outdata
= pdf_alloc(curp
->mchars
,
687 curp
->outfree
= pspdf_free
;
690 curp
->outdata
= ps_alloc(curp
->mchars
,
692 curp
->outfree
= pspdf_free
;
698 switch (curp
->outtype
) {
700 curp
->outman
= html_man
;
701 curp
->outmdoc
= html_mdoc
;
704 curp
->outman
= tree_man
;
705 curp
->outmdoc
= tree_mdoc
;
708 curp
->outmdoc
= man_mdoc
;
709 curp
->outman
= man_man
;
720 curp
->outman
= terminal_man
;
721 curp
->outmdoc
= terminal_mdoc
;
728 mparse_result(curp
->mp
, &man
, NULL
);
730 /* Execute the out device, if it exists. */
734 if (curp
->outmdoc
!= NULL
&& man
->macroset
== MACROSET_MDOC
)
735 (*curp
->outmdoc
)(curp
->outdata
, man
);
736 if (curp
->outman
!= NULL
&& man
->macroset
== MACROSET_MAN
)
737 (*curp
->outman
)(curp
->outdata
, man
);
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
)) {
801 fprintf(stderr
, "%s: %s: SYSERR: %s: %s",
802 progname
, file
, syscall
, strerror(errno
));
803 if (rc
< MANDOCLEVEL_SYSERR
)
804 rc
= 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
)
898 switch (getsubopt(&arg
, UNCONST(toks
), &v
)) {
905 curp
->wlevel
= MANDOCLEVEL_WARNING
;
908 curp
->wlevel
= MANDOCLEVEL_ERROR
;
911 curp
->wlevel
= MANDOCLEVEL_UNSUPP
;
914 curp
->wlevel
= MANDOCLEVEL_BADARG
;
917 fprintf(stderr
, "%s: -W %s: Bad argument\n",
927 mmsg(enum mandocerr t
, enum mandoclevel lvl
,
928 const char *file
, int line
, int col
, const char *msg
)
930 const char *mparse_msg
;
932 fprintf(stderr
, "%s: %s:", progname
, file
);
935 fprintf(stderr
, "%d:%d:", line
, col
+ 1);
937 fprintf(stderr
, " %s", mparse_strlevel(lvl
));
939 if (NULL
!= (mparse_msg
= mparse_strerror(t
)))
940 fprintf(stderr
, ": %s", mparse_msg
);
943 fprintf(stderr
, ": %s", msg
);
949 handle_sigpipe(int signum
)
958 #define MAX_PAGER_ARGS 16
959 char *argv
[MAX_PAGER_ARGS
];
966 if (pipe(fildes
) == -1) {
967 fprintf(stderr
, "%s: pipe: %s\n",
968 progname
, strerror(errno
));
972 switch (pager_pid
= fork()) {
974 fprintf(stderr
, "%s: fork: %s\n",
975 progname
, strerror(errno
));
976 exit((int)MANDOCLEVEL_SYSERR
);
981 if (dup2(fildes
[1], STDOUT_FILENO
) == -1) {
982 fprintf(stderr
, "%s: dup output: %s\n",
983 progname
, strerror(errno
));
984 exit((int)MANDOCLEVEL_SYSERR
);
987 signal(SIGPIPE
, handle_sigpipe
);
991 /* The child process becomes the pager. */
994 if (dup2(fildes
[0], STDIN_FILENO
) == -1) {
995 fprintf(stderr
, "%s: dup input: %s\n",
996 progname
, strerror(errno
));
997 exit((int)MANDOCLEVEL_SYSERR
);
1001 pager
= getenv("MANPAGER");
1002 if (pager
== NULL
|| *pager
== '\0')
1003 pager
= getenv("PAGER");
1004 if (pager
== NULL
|| *pager
== '\0')
1006 cp
= mandoc_strdup(pager
);
1009 * Parse the pager command into words.
1010 * Intentionally do not do anything fancy here.
1014 while (argc
+ 1 < MAX_PAGER_ARGS
) {
1016 cp
= strchr(cp
, ' ');
1027 /* Hand over to the pager. */
1029 execvp(argv
[0], argv
);
1030 fprintf(stderr
, "%s: exec %s: %s\n",
1031 progname
, argv
[0], strerror(errno
));
1032 exit((int)MANDOCLEVEL_SYSERR
);