]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
1 /* $Id: main.c,v 1.209 2014/12/21 14:49:28 schwarze Exp $ */
3 * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010, 2011, 2012, 2014 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 */
85 static int koptions(int *, char *);
87 int mandocdb(int, char**);
89 static int moptions(int *, char *);
90 static void mmsg(enum mandocerr
, enum mandoclevel
,
91 const char *, int, int, const char *);
92 static void parse(struct curparse
*, int,
93 const char *, enum mandoclevel
*);
95 static enum mandoclevel
passthrough(const char *, int, int);
97 static void spawn_pager(void);
98 static int toptions(struct curparse
*, char *);
99 static void usage(enum argmode
) __attribute__((noreturn
));
100 static void version(void) __attribute__((noreturn
));
101 static int woptions(struct curparse
*, char *);
103 static const int sec_prios
[] = {1, 4, 5, 8, 6, 3, 7, 2, 9};
104 static char help_arg
[] = "help";
105 static char *help_argv
[] = {help_arg
, NULL
};
106 static const char *progname
;
110 main(int argc
, char *argv
[])
112 struct curparse curp
;
113 struct mansearch search
;
114 struct manpaths paths
;
119 struct manpage
*res
, *resp
;
120 char *conf_file
, *defpaths
;
122 int prio
, best_prio
, synopsis_only
;
126 enum outmode outmode
;
133 progname
= strrchr(argv
[0], '/');
134 if (progname
== NULL
)
140 if (strcmp(progname
, BINM_MAKEWHATIS
) == 0)
141 return(mandocdb(argc
, argv
));
144 /* Search options. */
146 memset(&paths
, 0, sizeof(struct manpaths
));
148 conf_file
= defpaths
= NULL
;
152 memset(&search
, 0, sizeof(struct mansearch
));
153 search
.outkey
= "Nd";
155 if (strcmp(progname
, BINM_MAN
) == 0)
156 search
.argmode
= ARG_NAME
;
157 else if (strcmp(progname
, BINM_APROPOS
) == 0)
158 search
.argmode
= ARG_EXPR
;
159 else if (strcmp(progname
, BINM_WHATIS
) == 0)
160 search
.argmode
= ARG_WORD
;
161 else if (strncmp(progname
, "help", 4) == 0)
162 search
.argmode
= ARG_NAME
;
164 search
.argmode
= ARG_FILE
;
166 /* Parser and formatter options. */
168 memset(&curp
, 0, sizeof(struct curparse
));
169 curp
.outtype
= OUTT_LOCALE
;
170 curp
.wlevel
= MANDOCLEVEL_FATAL
;
171 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
;
196 search
.argmode
= ARG_WORD
;
199 (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
;
244 search
.outkey
= optarg
;
245 (void)strlcat(curp
.outopts
, optarg
, BUFSIZ
);
246 (void)strlcat(curp
.outopts
, ",", BUFSIZ
);
249 search
.arch
= optarg
;
255 if ( ! toptions(&curp
, optarg
))
256 return((int)MANDOCLEVEL_BADARG
);
259 if ( ! woptions(&curp
, optarg
))
260 return((int)MANDOCLEVEL_BADARG
);
263 outmode
= OUTMODE_FLN
;
275 usage(search
.argmode
);
277 /* Postprocess options. */
279 if (outmode
== OUTMODE_DEF
) {
280 switch (search
.argmode
) {
282 outmode
= OUTMODE_ALL
;
286 outmode
= OUTMODE_ONE
;
289 outmode
= OUTMODE_LST
;
294 /* Parse arguments. */
304 * and for a man(1) section argument without -s.
307 if (search
.argmode
== ARG_NAME
) {
308 if (*progname
== 'h') {
313 } else if (((uc
= argv
[0]) != NULL
) &&
314 ((isdigit(uc
[0]) && (uc
[1] == '\0' ||
315 (isalpha(uc
[1]) && uc
[2] == '\0'))) ||
316 (uc
[0] == 'n' && uc
[1] == '\0'))) {
325 /* man(1), whatis(1), apropos(1) */
327 if (search
.argmode
!= ARG_FILE
) {
330 usage(search
.argmode
);
332 if (search
.argmode
== ARG_NAME
&&
333 outmode
== OUTMODE_ONE
)
334 search
.firstmatch
= 1;
336 /* Access the mandoc database. */
338 manpath_parse(&paths
, conf_file
, defpaths
, auxpaths
);
340 if( ! mansearch(&search
, &paths
, argc
, argv
, &res
, &sz
))
341 usage(search
.argmode
);
345 if (search
.argmode
== ARG_NAME
)
346 fprintf(stderr
, "%s: No entry for %s "
347 "in the manual.\n", progname
, argv
[0]);
348 rc
= MANDOCLEVEL_BADARG
;
353 * For standard man(1) and -a output mode,
354 * prepare for copying filename pointers
355 * into the program parameter array.
358 if (outmode
== OUTMODE_ONE
) {
361 } else if (outmode
== OUTMODE_ALL
)
364 /* Iterate all matching manuals. */
366 for (i
= 0; i
< sz
; i
++) {
367 if (outmode
== OUTMODE_FLN
)
369 else if (outmode
== OUTMODE_LST
)
370 printf("%s - %s\n", res
[i
].names
,
371 res
[i
].output
== NULL
? "" :
373 else if (outmode
== OUTMODE_ONE
) {
374 /* Search for the best section. */
375 isec
= strcspn(res
[i
].file
, "123456789");
376 sec
= res
[i
].file
[isec
];
379 prio
= sec_prios
[sec
- '1'];
380 if (prio
>= best_prio
)
388 * For man(1), -a and -i output mode, fall through
389 * to the main mandoc(1) code iterating files
390 * and running the parsers on each of them.
393 if (outmode
== OUTMODE_FLN
|| outmode
== OUTMODE_LST
)
396 fputs("mandoc: database support not compiled in\n",
398 return((int)MANDOCLEVEL_BADARG
);
404 if (search
.argmode
== ARG_FILE
&& ! moptions(&options
, auxpaths
))
405 return((int)MANDOCLEVEL_BADARG
);
407 if (use_pager
&& isatty(STDOUT_FILENO
))
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 parse(&curp
, STDIN_FILENO
, "<stdin>", &rc
);
426 rc
= mparse_open(curp
.mp
, &fd
, resp
->file
);
429 else if (resp
->form
& FORM_SRC
) {
430 /* For .so only; ignore failure. */
431 chdir(paths
.paths
[resp
->ipath
]);
432 parse(&curp
, fd
, resp
->file
, &rc
);
434 rc
= passthrough(resp
->file
, fd
,
440 rc
= mparse_open(curp
.mp
, &fd
, *argv
++);
442 parse(&curp
, fd
, argv
[-1], &rc
);
445 if (mparse_wait(curp
.mp
) != MANDOCLEVEL_OK
)
446 rc
= MANDOCLEVEL_SYSERR
;
448 if (MANDOCLEVEL_OK
!= rc
&& curp
.wstop
)
454 (*curp
.outfree
)(curp
.outdata
);
455 mparse_free(curp
.mp
);
456 mchars_free(curp
.mchars
);
460 if (search
.argmode
!= ARG_FILE
) {
461 manpath_free(&paths
);
462 mansearch_free(res
, sz
);
476 printf("mandoc %s\n", VERSION
);
477 exit((int)MANDOCLEVEL_OK
);
481 usage(enum argmode argmode
)
486 fputs("usage: mandoc [-acfhklV] [-Ios=name] "
487 "[-Kencoding] [-mformat] [-Ooption]\n"
488 "\t [-Toutput] [-Wlevel] [file ...]\n", stderr
);
491 fputs("usage: man [-acfhklVw] [-C file] [-I os=name] "
492 "[-K encoding] [-M path] [-m path]\n"
493 "\t [-O option=value] [-S subsection] [-s section] "
494 "[-T output] [-W level]\n"
495 "\t [section] name ...\n", stderr
);
498 fputs("usage: whatis [-acfhklVw] [-C file] "
499 "[-M path] [-m path] [-O outkey] [-S arch]\n"
500 "\t [-s section] name ...\n", stderr
);
503 fputs("usage: apropos [-acfhklVw] [-C file] "
504 "[-M path] [-m path] [-O outkey] [-S arch]\n"
505 "\t [-s section] expression ...\n", stderr
);
508 exit((int)MANDOCLEVEL_BADARG
);
512 parse(struct curparse
*curp
, int fd
, const char *file
,
513 enum mandoclevel
*level
)
519 /* Begin by parsing the file itself. */
524 rc
= mparse_readfd(curp
->mp
, fd
, file
);
526 /* Stop immediately if the parse has failed. */
528 if (MANDOCLEVEL_FATAL
<= rc
)
532 * With -Wstop and warnings or errors of at least the requested
533 * level, do not produce output.
536 if (MANDOCLEVEL_OK
!= rc
&& curp
->wstop
)
539 /* If unset, allocate output dev now (if applicable). */
541 if ( ! (curp
->outman
&& curp
->outmdoc
)) {
542 switch (curp
->outtype
) {
544 curp
->outdata
= html_alloc(curp
->mchars
,
546 curp
->outfree
= html_free
;
549 curp
->outdata
= utf8_alloc(curp
->mchars
,
551 curp
->outfree
= ascii_free
;
554 curp
->outdata
= locale_alloc(curp
->mchars
,
556 curp
->outfree
= ascii_free
;
559 curp
->outdata
= ascii_alloc(curp
->mchars
,
561 curp
->outfree
= ascii_free
;
564 curp
->outdata
= pdf_alloc(curp
->mchars
,
566 curp
->outfree
= pspdf_free
;
569 curp
->outdata
= ps_alloc(curp
->mchars
,
571 curp
->outfree
= pspdf_free
;
577 switch (curp
->outtype
) {
579 curp
->outman
= html_man
;
580 curp
->outmdoc
= html_mdoc
;
583 curp
->outman
= tree_man
;
584 curp
->outmdoc
= tree_mdoc
;
587 curp
->outmdoc
= man_mdoc
;
588 curp
->outman
= man_man
;
599 curp
->outman
= terminal_man
;
600 curp
->outmdoc
= terminal_mdoc
;
607 mparse_result(curp
->mp
, &mdoc
, &man
, NULL
);
609 /* Execute the out device, if it exists. */
611 if (man
&& curp
->outman
)
612 (*curp
->outman
)(curp
->outdata
, man
);
613 if (mdoc
&& curp
->outmdoc
)
614 (*curp
->outmdoc
)(curp
->outdata
, mdoc
);
618 mparse_reset(curp
->mp
);
625 static enum mandoclevel
626 passthrough(const char *file
, int fd
, int synopsis_only
)
628 const char synb
[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
629 const char synr
[] = "SYNOPSIS";
638 if ((stream
= fdopen(fd
, "r")) == NULL
) {
645 while ((line
= fgetln(stream
, &len
)) != NULL
) {
648 if ( ! isspace((unsigned char)*line
))
651 isspace((unsigned char)*line
)) {
656 if ((len
== sizeof(synb
) &&
657 ! strncmp(line
, synb
, len
- 1)) ||
658 (len
== sizeof(synr
) &&
659 ! strncmp(line
, synr
, len
- 1)))
664 for (off
= 0; off
< len
; off
+= nw
)
665 if ((nw
= write(STDOUT_FILENO
, line
+ off
,
666 len
- off
)) == -1 || nw
== 0) {
673 if (ferror(stream
)) {
681 return(MANDOCLEVEL_OK
);
684 fprintf(stderr
, "%s: %s: SYSERR: %s: %s",
685 progname
, file
, syscall
, strerror(errno
));
686 return(MANDOCLEVEL_SYSERR
);
691 koptions(int *options
, char *arg
)
694 if ( ! strcmp(arg
, "utf-8")) {
695 *options
|= MPARSE_UTF8
;
696 *options
&= ~MPARSE_LATIN1
;
697 } else if ( ! strcmp(arg
, "iso-8859-1")) {
698 *options
|= MPARSE_LATIN1
;
699 *options
&= ~MPARSE_UTF8
;
700 } else if ( ! strcmp(arg
, "us-ascii")) {
701 *options
&= ~(MPARSE_UTF8
| MPARSE_LATIN1
);
703 fprintf(stderr
, "%s: -K %s: Bad argument\n",
711 moptions(int *options
, char *arg
)
716 else if (0 == strcmp(arg
, "doc"))
717 *options
|= MPARSE_MDOC
;
718 else if (0 == strcmp(arg
, "andoc"))
720 else if (0 == strcmp(arg
, "an"))
721 *options
|= MPARSE_MAN
;
723 fprintf(stderr
, "%s: -m %s: Bad argument\n",
732 toptions(struct curparse
*curp
, char *arg
)
735 if (0 == strcmp(arg
, "ascii"))
736 curp
->outtype
= OUTT_ASCII
;
737 else if (0 == strcmp(arg
, "lint")) {
738 curp
->outtype
= OUTT_LINT
;
739 curp
->wlevel
= MANDOCLEVEL_WARNING
;
740 } else if (0 == strcmp(arg
, "tree"))
741 curp
->outtype
= OUTT_TREE
;
742 else if (0 == strcmp(arg
, "man"))
743 curp
->outtype
= OUTT_MAN
;
744 else if (0 == strcmp(arg
, "html"))
745 curp
->outtype
= OUTT_HTML
;
746 else if (0 == strcmp(arg
, "utf8"))
747 curp
->outtype
= OUTT_UTF8
;
748 else if (0 == strcmp(arg
, "locale"))
749 curp
->outtype
= OUTT_LOCALE
;
750 else if (0 == strcmp(arg
, "xhtml"))
751 curp
->outtype
= OUTT_HTML
;
752 else if (0 == strcmp(arg
, "ps"))
753 curp
->outtype
= OUTT_PS
;
754 else if (0 == strcmp(arg
, "pdf"))
755 curp
->outtype
= OUTT_PDF
;
757 fprintf(stderr
, "%s: -T %s: Bad argument\n",
766 woptions(struct curparse
*curp
, char *arg
)
780 switch (getsubopt(&arg
, UNCONST(toks
), &v
)) {
787 curp
->wlevel
= MANDOCLEVEL_WARNING
;
790 curp
->wlevel
= MANDOCLEVEL_ERROR
;
793 curp
->wlevel
= MANDOCLEVEL_FATAL
;
796 fprintf(stderr
, "%s: -W %s: Bad argument\n",
806 mmsg(enum mandocerr t
, enum mandoclevel lvl
,
807 const char *file
, int line
, int col
, const char *msg
)
809 const char *mparse_msg
;
811 fprintf(stderr
, "%s: %s:", progname
, file
);
814 fprintf(stderr
, "%d:%d:", line
, col
+ 1);
816 fprintf(stderr
, " %s", mparse_strlevel(lvl
));
818 if (NULL
!= (mparse_msg
= mparse_strerror(t
)))
819 fprintf(stderr
, ": %s", mparse_msg
);
822 fprintf(stderr
, ": %s", msg
);
830 #define MAX_PAGER_ARGS 16
831 char *argv
[MAX_PAGER_ARGS
];
837 if (pipe(fildes
) == -1) {
838 fprintf(stderr
, "%s: pipe: %s\n",
839 progname
, strerror(errno
));
845 fprintf(stderr
, "%s: fork: %s\n",
846 progname
, strerror(errno
));
847 exit((int)MANDOCLEVEL_SYSERR
);
850 if (dup2(fildes
[1], STDOUT_FILENO
) == -1) {
851 fprintf(stderr
, "%s: dup output: %s\n",
852 progname
, strerror(errno
));
853 exit((int)MANDOCLEVEL_SYSERR
);
860 /* The original process becomes the pager. */
863 if (dup2(fildes
[0], STDIN_FILENO
) == -1) {
864 fprintf(stderr
, "%s: dup input: %s\n",
865 progname
, strerror(errno
));
866 exit((int)MANDOCLEVEL_SYSERR
);
869 pager
= getenv("MANPAGER");
870 if (pager
== NULL
|| *pager
== '\0')
871 pager
= getenv("PAGER");
872 if (pager
== NULL
|| *pager
== '\0')
873 pager
= "/usr/bin/more -s";
874 cp
= mandoc_strdup(pager
);
877 * Parse the pager command into words.
878 * Intentionally do not do anything fancy here.
882 while (argc
+ 1 < MAX_PAGER_ARGS
) {
884 cp
= strchr(cp
, ' ');
895 /* Hand over to the pager. */
897 execvp(argv
[0], argv
);
898 fprintf(stderr
, "%s: exec: %s\n",
899 progname
, strerror(errno
));
900 exit((int)MANDOCLEVEL_SYSERR
);