]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
1 /* $Id: main.c,v 1.228 2015/03/27 16:36:31 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 */
38 #include "mandoc_aux.h"
43 #include "mansearch.h"
45 #if !defined(__GNUC__) || (__GNUC__ < 2)
47 # define __attribute__(x)
49 #endif /* !defined(__GNUC__) || (__GNUC__ < 2) */
60 typedef void (*out_mdoc
)(void *, const struct mdoc
*);
61 typedef void (*out_man
)(void *, const struct man
*);
62 typedef void (*out_free
)(void *);
65 OUTT_ASCII
= 0, /* -Tascii */
66 OUTT_LOCALE
, /* -Tlocale */
67 OUTT_UTF8
, /* -Tutf8 */
68 OUTT_TREE
, /* -Ttree */
70 OUTT_HTML
, /* -Thtml */
71 OUTT_LINT
, /* -Tlint */
78 struct mchars
*mchars
; /* character table */
79 enum mandoclevel wlevel
; /* ignore messages below this */
80 int wstop
; /* stop after a file with a warning */
81 enum outt outtype
; /* which output to use */
82 out_mdoc outmdoc
; /* mdoc output ptr */
83 out_man outman
; /* man output ptr */
84 out_free outfree
; /* free output ptr */
85 void *outdata
; /* data for output */
86 char outopts
[BUFSIZ
]; /* buf of output opts */
89 static int fs_lookup(const struct manpaths
*,
90 size_t ipath
, const char *,
91 const char *, const char *,
92 struct manpage
**, size_t *);
93 static void fs_search(const struct mansearch
*,
94 const struct manpaths
*, int, char**,
95 struct manpage
**, size_t *);
96 static void handle_sigpipe(int);
97 static int koptions(int *, char *);
99 int mandocdb(int, char**);
101 static int moptions(int *, char *);
102 static void mmsg(enum mandocerr
, enum mandoclevel
,
103 const char *, int, int, const char *);
104 static void parse(struct curparse
*, int, const char *);
105 static void passthrough(const char *, int, int);
106 static pid_t
spawn_pager(void);
107 static int toptions(struct curparse
*, char *);
108 static void usage(enum argmode
) __attribute__((noreturn
));
109 static int woptions(struct curparse
*, char *);
111 static const int sec_prios
[] = {1, 4, 5, 8, 6, 3, 7, 2, 9};
112 static char help_arg
[] = "help";
113 static char *help_argv
[] = {help_arg
, NULL
};
114 static const char *progname
;
115 static enum mandoclevel rc
;
119 main(int argc
, char *argv
[])
121 struct curparse curp
;
122 struct mansearch search
;
123 struct manpaths paths
;
127 struct manpage
*res
, *resp
;
128 char *conf_file
, *defpaths
;
130 int prio
, best_prio
, synopsis_only
;
132 enum mandoclevel rctmp
;
133 enum outmode outmode
;
138 pid_t pager_pid
; /* 0: don't use; 1: not yet spawned. */
142 else if ((progname
= strrchr(argv
[0], '/')) == NULL
)
148 if (strcmp(progname
, BINM_MAKEWHATIS
) == 0)
149 return(mandocdb(argc
, argv
));
152 /* Search options. */
154 memset(&paths
, 0, sizeof(struct manpaths
));
155 conf_file
= defpaths
= NULL
;
158 memset(&search
, 0, sizeof(struct mansearch
));
159 search
.outkey
= "Nd";
161 if (strcmp(progname
, BINM_MAN
) == 0)
162 search
.argmode
= ARG_NAME
;
163 else if (strcmp(progname
, BINM_APROPOS
) == 0)
164 search
.argmode
= ARG_EXPR
;
165 else if (strcmp(progname
, BINM_WHATIS
) == 0)
166 search
.argmode
= ARG_WORD
;
167 else if (strncmp(progname
, "help", 4) == 0)
168 search
.argmode
= ARG_NAME
;
170 search
.argmode
= ARG_FILE
;
172 /* Parser and formatter options. */
174 memset(&curp
, 0, sizeof(struct curparse
));
175 curp
.outtype
= OUTT_LOCALE
;
176 curp
.wlevel
= MANDOCLEVEL_BADARG
;
177 options
= MPARSE_SO
| MPARSE_UTF8
| MPARSE_LATIN1
;
183 outmode
= OUTMODE_DEF
;
185 while (-1 != (c
= getopt(argc
, argv
,
186 "aC:cfhI:iK:klM:m:O:S:s:T:VW:w"))) {
189 outmode
= OUTMODE_ALL
;
198 search
.argmode
= ARG_WORD
;
201 (void)strlcat(curp
.outopts
, "synopsis,", BUFSIZ
);
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 (void)strlcat(curp
.outopts
, optarg
, BUFSIZ
);
244 (void)strlcat(curp
.outopts
, ",", BUFSIZ
);
247 search
.arch
= optarg
;
253 if ( ! toptions(&curp
, optarg
))
254 return((int)MANDOCLEVEL_BADARG
);
257 if ( ! woptions(&curp
, optarg
))
258 return((int)MANDOCLEVEL_BADARG
);
261 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
= (unsigned char *)argv
[0]) != NULL
) &&
310 ((isdigit(uc
[0]) && (uc
[1] == '\0' ||
311 (isalpha(uc
[1]) && uc
[2] == '\0'))) ||
312 (uc
[0] == 'n' && uc
[1] == '\0'))) {
313 search
.sec
= (char *)uc
;
317 if (search
.arch
== NULL
)
318 search
.arch
= getenv("MACHINE");
320 if (search
.arch
== NULL
)
321 search
.arch
= MACHINE
;
327 /* man(1), whatis(1), apropos(1) */
329 if (search
.argmode
!= ARG_FILE
) {
331 usage(search
.argmode
);
333 if (search
.argmode
== ARG_NAME
&&
334 outmode
== OUTMODE_ONE
)
335 search
.firstmatch
= 1;
337 /* Access the mandoc database. */
339 manpath_parse(&paths
, conf_file
, defpaths
, auxpaths
);
342 if( ! mansearch(&search
, &paths
, argc
, argv
, &res
, &sz
))
343 usage(search
.argmode
);
345 if (search
.argmode
!= ARG_NAME
) {
346 fputs("mandoc: database support not compiled in\n",
348 return((int)MANDOCLEVEL_BADARG
);
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
)
409 if (search
.argmode
== ARG_FILE
&& ! moptions(&options
, auxpaths
))
410 return((int)MANDOCLEVEL_BADARG
);
412 curp
.mchars
= mchars_alloc();
413 curp
.mp
= mparse_alloc(options
, curp
.wlevel
, mmsg
,
417 * Conditionally start up the lookaside buffer before parsing.
419 if (OUTT_MAN
== curp
.outtype
)
420 mparse_keep(curp
.mp
);
423 if (pager_pid
== 1 && isatty(STDOUT_FILENO
))
424 pager_pid
= spawn_pager();
425 parse(&curp
, STDIN_FILENO
, "<stdin>");
429 rctmp
= mparse_open(curp
.mp
, &fd
,
430 resp
!= NULL
? resp
->file
: *argv
);
435 if (pager_pid
== 1 && isatty(STDOUT_FILENO
))
436 pager_pid
= spawn_pager();
439 parse(&curp
, fd
, *argv
);
440 else if (resp
->form
& FORM_SRC
) {
441 /* For .so only; ignore failure. */
442 chdir(paths
.paths
[resp
->ipath
]);
443 parse(&curp
, fd
, resp
->file
);
445 passthrough(resp
->file
, fd
, synopsis_only
);
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
);
483 * If a pager is attached, flush the pipe leading to it
484 * and signal end of file such that the user can browse
485 * to the end. Then wait for the user to close the pager.
488 if (pager_pid
!= 0 && pager_pid
!= 1) {
490 waitpid(pager_pid
, NULL
, 0);
497 usage(enum argmode argmode
)
502 fputs("usage: mandoc [-acfhkl] [-I os=name] "
503 "[-K encoding] [-mformat] [-O option]\n"
504 "\t [-T output] [-W level] [file ...]\n", stderr
);
507 fputs("usage: man [-acfhklw] [-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 [-acfhklw] [-C file] "
515 "[-M path] [-m path] [-O outkey] [-S arch]\n"
516 "\t [-s section] name ...\n", stderr
);
519 fputs("usage: apropos [-acfhklw] [-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
)
533 struct manpage
*page
;
538 mandoc_asprintf(&file
, "%s/man%s/%s.%s",
539 paths
->paths
[ipath
], sec
, name
, sec
);
540 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)
560 mandoc_asprintf(&file
, "%s/man%s/%s.*",
561 paths
->paths
[ipath
], sec
, name
);
562 globres
= glob(file
, 0, NULL
, &globinfo
);
563 if (globres
!= 0 && globres
!= GLOB_NOMATCH
)
564 fprintf(stderr
, "%s: %s: glob: %s\n",
565 progname
, file
, strerror(errno
));
568 file
= mandoc_strdup(*globinfo
.gl_pathv
);
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
]);
580 *res
= mandoc_reallocarray(*res
, ++*ressz
, sizeof(struct manpage
));
581 page
= *res
+ (*ressz
- 1);
586 page
->bits
= NAME_FILE
& NAME_MASK
;
587 page
->sec
= (*sec
>= '1' && *sec
<= '9') ? *sec
- '1' + 1 : 10;
593 fs_search(const struct mansearch
*cfg
, const struct manpaths
*paths
,
594 int argc
, char **argv
, struct manpage
**res
, size_t *ressz
)
596 const char *const sections
[] =
597 {"1", "8", "6", "2", "3", "3p", "5", "7", "4", "9"};
598 const size_t nsec
= sizeof(sections
)/sizeof(sections
[0]);
600 size_t ipath
, isec
, lastsz
;
602 assert(cfg
->argmode
== ARG_NAME
);
607 for (ipath
= 0; ipath
< paths
->sz
; ipath
++) {
608 if (cfg
->sec
!= NULL
) {
609 if (fs_lookup(paths
, ipath
, cfg
->sec
,
610 cfg
->arch
, *argv
, res
, ressz
) &&
613 } else for (isec
= 0; isec
< nsec
; isec
++)
614 if (fs_lookup(paths
, ipath
, sections
[isec
],
615 cfg
->arch
, *argv
, res
, ressz
) &&
619 if (*ressz
== lastsz
)
621 "%s: No entry for %s in the manual.\n",
630 parse(struct curparse
*curp
, int fd
, const char *file
)
632 enum mandoclevel rctmp
;
636 /* Begin by parsing the file itself. */
641 rctmp
= mparse_readfd(curp
->mp
, fd
, file
);
646 * With -Wstop and warnings or errors of at least the requested
647 * level, do not produce output.
650 if (rctmp
!= MANDOCLEVEL_OK
&& curp
->wstop
)
653 /* If unset, allocate output dev now (if applicable). */
655 if ( ! (curp
->outman
&& curp
->outmdoc
)) {
656 switch (curp
->outtype
) {
658 curp
->outdata
= html_alloc(curp
->mchars
,
660 curp
->outfree
= html_free
;
663 curp
->outdata
= utf8_alloc(curp
->mchars
,
665 curp
->outfree
= ascii_free
;
668 curp
->outdata
= locale_alloc(curp
->mchars
,
670 curp
->outfree
= ascii_free
;
673 curp
->outdata
= ascii_alloc(curp
->mchars
,
675 curp
->outfree
= ascii_free
;
678 curp
->outdata
= pdf_alloc(curp
->mchars
,
680 curp
->outfree
= pspdf_free
;
683 curp
->outdata
= ps_alloc(curp
->mchars
,
685 curp
->outfree
= pspdf_free
;
691 switch (curp
->outtype
) {
693 curp
->outman
= html_man
;
694 curp
->outmdoc
= html_mdoc
;
697 curp
->outman
= tree_man
;
698 curp
->outmdoc
= tree_mdoc
;
701 curp
->outmdoc
= man_mdoc
;
702 curp
->outman
= man_man
;
713 curp
->outman
= terminal_man
;
714 curp
->outmdoc
= terminal_mdoc
;
721 mparse_result(curp
->mp
, &mdoc
, &man
, NULL
);
723 /* Execute the out device, if it exists. */
725 if (man
&& curp
->outman
)
726 (*curp
->outman
)(curp
->outdata
, man
);
727 if (mdoc
&& curp
->outmdoc
)
728 (*curp
->outmdoc
)(curp
->outdata
, mdoc
);
732 passthrough(const char *file
, int fd
, int synopsis_only
)
734 const char synb
[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
735 const char synr
[] = "SYNOPSIS";
746 if ((stream
= fdopen(fd
, "r")) == NULL
) {
753 while ((line
= fgetln(stream
, &len
)) != NULL
) {
756 if ( ! isspace((unsigned char)*line
))
759 isspace((unsigned char)*line
)) {
764 if ((len
== sizeof(synb
) &&
765 ! strncmp(line
, synb
, len
- 1)) ||
766 (len
== sizeof(synr
) &&
767 ! strncmp(line
, synr
, len
- 1)))
772 for (off
= 0; off
< len
; off
+= nw
)
773 if ((nw
= write(STDOUT_FILENO
, line
+ off
,
774 len
- off
)) == -1 || nw
== 0) {
781 if (ferror(stream
)) {
792 fprintf(stderr
, "%s: %s: SYSERR: %s: %s",
793 progname
, file
, syscall
, strerror(errno
));
794 if (rc
< MANDOCLEVEL_SYSERR
)
795 rc
= MANDOCLEVEL_SYSERR
;
799 koptions(int *options
, char *arg
)
802 if ( ! strcmp(arg
, "utf-8")) {
803 *options
|= MPARSE_UTF8
;
804 *options
&= ~MPARSE_LATIN1
;
805 } else if ( ! strcmp(arg
, "iso-8859-1")) {
806 *options
|= MPARSE_LATIN1
;
807 *options
&= ~MPARSE_UTF8
;
808 } else if ( ! strcmp(arg
, "us-ascii")) {
809 *options
&= ~(MPARSE_UTF8
| MPARSE_LATIN1
);
811 fprintf(stderr
, "%s: -K %s: Bad argument\n",
819 moptions(int *options
, char *arg
)
824 else if (0 == strcmp(arg
, "doc"))
825 *options
|= MPARSE_MDOC
;
826 else if (0 == strcmp(arg
, "andoc"))
828 else if (0 == strcmp(arg
, "an"))
829 *options
|= MPARSE_MAN
;
831 fprintf(stderr
, "%s: -m %s: Bad argument\n",
840 toptions(struct curparse
*curp
, char *arg
)
843 if (0 == strcmp(arg
, "ascii"))
844 curp
->outtype
= OUTT_ASCII
;
845 else if (0 == strcmp(arg
, "lint")) {
846 curp
->outtype
= OUTT_LINT
;
847 curp
->wlevel
= MANDOCLEVEL_WARNING
;
848 } else if (0 == strcmp(arg
, "tree"))
849 curp
->outtype
= OUTT_TREE
;
850 else if (0 == strcmp(arg
, "man"))
851 curp
->outtype
= OUTT_MAN
;
852 else if (0 == strcmp(arg
, "html"))
853 curp
->outtype
= OUTT_HTML
;
854 else if (0 == strcmp(arg
, "utf8"))
855 curp
->outtype
= OUTT_UTF8
;
856 else if (0 == strcmp(arg
, "locale"))
857 curp
->outtype
= OUTT_LOCALE
;
858 else if (0 == strcmp(arg
, "xhtml"))
859 curp
->outtype
= OUTT_HTML
;
860 else if (0 == strcmp(arg
, "ps"))
861 curp
->outtype
= OUTT_PS
;
862 else if (0 == strcmp(arg
, "pdf"))
863 curp
->outtype
= OUTT_PDF
;
865 fprintf(stderr
, "%s: -T %s: Bad argument\n",
874 woptions(struct curparse
*curp
, char *arg
)
889 switch (getsubopt(&arg
, UNCONST(toks
), &v
)) {
896 curp
->wlevel
= MANDOCLEVEL_WARNING
;
899 curp
->wlevel
= MANDOCLEVEL_ERROR
;
902 curp
->wlevel
= MANDOCLEVEL_UNSUPP
;
905 curp
->wlevel
= MANDOCLEVEL_BADARG
;
908 fprintf(stderr
, "%s: -W %s: Bad argument\n",
918 mmsg(enum mandocerr t
, enum mandoclevel lvl
,
919 const char *file
, int line
, int col
, const char *msg
)
921 const char *mparse_msg
;
923 fprintf(stderr
, "%s: %s:", progname
, file
);
926 fprintf(stderr
, "%d:%d:", line
, col
+ 1);
928 fprintf(stderr
, " %s", mparse_strlevel(lvl
));
930 if (NULL
!= (mparse_msg
= mparse_strerror(t
)))
931 fprintf(stderr
, ": %s", mparse_msg
);
934 fprintf(stderr
, ": %s", msg
);
940 handle_sigpipe(int signum
)
949 #define MAX_PAGER_ARGS 16
950 char *argv
[MAX_PAGER_ARGS
];
957 if (pipe(fildes
) == -1) {
958 fprintf(stderr
, "%s: pipe: %s\n",
959 progname
, strerror(errno
));
963 switch (pager_pid
= fork()) {
965 fprintf(stderr
, "%s: fork: %s\n",
966 progname
, strerror(errno
));
967 exit((int)MANDOCLEVEL_SYSERR
);
972 if (dup2(fildes
[1], STDOUT_FILENO
) == -1) {
973 fprintf(stderr
, "%s: dup output: %s\n",
974 progname
, strerror(errno
));
975 exit((int)MANDOCLEVEL_SYSERR
);
978 signal(SIGPIPE
, handle_sigpipe
);
982 /* The child process becomes the pager. */
985 if (dup2(fildes
[0], STDIN_FILENO
) == -1) {
986 fprintf(stderr
, "%s: dup input: %s\n",
987 progname
, strerror(errno
));
988 exit((int)MANDOCLEVEL_SYSERR
);
992 pager
= getenv("MANPAGER");
993 if (pager
== NULL
|| *pager
== '\0')
994 pager
= getenv("PAGER");
995 if (pager
== NULL
|| *pager
== '\0')
996 pager
= "/usr/bin/more -s";
997 cp
= mandoc_strdup(pager
);
1000 * Parse the pager command into words.
1001 * Intentionally do not do anything fancy here.
1005 while (argc
+ 1 < MAX_PAGER_ARGS
) {
1007 cp
= strchr(cp
, ' ');
1018 /* Hand over to the pager. */
1020 execvp(argv
[0], argv
);
1021 fprintf(stderr
, "%s: exec: %s\n",
1022 progname
, strerror(errno
));
1023 exit((int)MANDOCLEVEL_SYSERR
);