]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
1 /* $Id: main.c,v 1.199 2014/11/11 19:04:55 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 *);
86 static int moptions(int *, char *);
87 static void mmsg(enum mandocerr
, enum mandoclevel
,
88 const char *, int, int, const char *);
89 static void parse(struct curparse
*, int,
90 const char *, enum mandoclevel
*);
91 static enum mandoclevel
passthrough(const char *, int, int);
92 static void spawn_pager(void);
93 static int toptions(struct curparse
*, char *);
94 static void usage(enum argmode
) __attribute__((noreturn
));
95 static void version(void) __attribute__((noreturn
));
96 static int woptions(struct curparse
*, char *);
98 static const int sec_prios
[] = {1, 4, 5, 8, 6, 3, 7, 2, 9};
99 static const char *progname
;
103 main(int argc
, char *argv
[])
105 struct curparse curp
;
106 struct mansearch search
;
107 struct manpaths paths
;
108 char *conf_file
, *defpaths
, *auxpaths
;
111 struct manpage
*res
, *resp
;
117 enum outmode outmode
;
126 progname
= strrchr(argv
[0], '/');
127 if (progname
== NULL
)
132 /* Search options. */
134 memset(&paths
, 0, sizeof(struct manpaths
));
135 conf_file
= defpaths
= auxpaths
= NULL
;
137 memset(&search
, 0, sizeof(struct mansearch
));
138 search
.outkey
= "Nd";
140 if (strcmp(progname
, "man") == 0)
141 search
.argmode
= ARG_NAME
;
142 else if (strncmp(progname
, "apropos", 7) == 0)
143 search
.argmode
= ARG_EXPR
;
144 else if (strncmp(progname
, "whatis", 6) == 0)
145 search
.argmode
= ARG_WORD
;
147 search
.argmode
= ARG_FILE
;
149 /* Parser and formatter options. */
151 memset(&curp
, 0, sizeof(struct curparse
));
152 curp
.outtype
= OUTT_ASCII
;
153 curp
.wlevel
= MANDOCLEVEL_FATAL
;
154 options
= MPARSE_SO
| MPARSE_UTF8
| MPARSE_LATIN1
;
160 outmode
= OUTMODE_DEF
;
162 while (-1 != (c
= getopt(argc
, argv
,
163 "aC:cfhI:iK:klM:m:O:S:s:T:VW:w"))) {
166 outmode
= OUTMODE_ALL
;
175 search
.argmode
= ARG_WORD
;
178 (void)strlcat(curp
.outopts
, "synopsis,", BUFSIZ
);
181 outmode
= OUTMODE_ALL
;
184 if (strncmp(optarg
, "os=", 3)) {
186 "%s: -I%s: Bad argument\n",
188 return((int)MANDOCLEVEL_BADARG
);
192 "%s: -I%s: Duplicate argument\n",
194 return((int)MANDOCLEVEL_BADARG
);
196 defos
= mandoc_strdup(optarg
+ 3);
199 outmode
= OUTMODE_INT
;
202 if ( ! koptions(&options
, optarg
))
203 return((int)MANDOCLEVEL_BADARG
);
206 search
.argmode
= ARG_EXPR
;
209 search
.argmode
= ARG_FILE
;
210 outmode
= OUTMODE_ALL
;
219 search
.outkey
= optarg
;
220 (void)strlcat(curp
.outopts
, optarg
, BUFSIZ
);
221 (void)strlcat(curp
.outopts
, ",", BUFSIZ
);
224 search
.arch
= optarg
;
230 if ( ! toptions(&curp
, optarg
))
231 return((int)MANDOCLEVEL_BADARG
);
234 if ( ! woptions(&curp
, optarg
))
235 return((int)MANDOCLEVEL_BADARG
);
238 outmode
= OUTMODE_FLN
;
250 usage(search
.argmode
);
252 /* Postprocess options. */
254 if (outmode
== OUTMODE_DEF
) {
255 switch (search
.argmode
) {
257 outmode
= OUTMODE_ALL
;
261 outmode
= OUTMODE_ONE
;
264 outmode
= OUTMODE_LST
;
269 /* Parse arguments. */
277 /* Quirk for a man(1) section argument without -s. */
279 if (search
.argmode
== ARG_NAME
&&
281 isdigit((unsigned char)argv
[0][0]) &&
282 (argv
[0][1] == '\0' || !strcmp(argv
[0], "3p"))) {
283 search
.sec
= argv
[0];
290 /* man(1), whatis(1), apropos(1) */
292 if (search
.argmode
!= ARG_FILE
) {
295 usage(search
.argmode
);
297 if (search
.argmode
== ARG_NAME
&&
298 outmode
== OUTMODE_ONE
)
299 search
.firstmatch
= 1;
301 /* Access the mandoc database. */
303 manpath_parse(&paths
, conf_file
, defpaths
, auxpaths
);
305 if( ! mansearch(&search
, &paths
, argc
, argv
, &res
, &sz
))
306 usage(search
.argmode
);
310 if (search
.argmode
== ARG_NAME
)
311 fprintf(stderr
, "%s: No entry for %s "
312 "in the manual.\n", progname
, argv
[0]);
313 rc
= MANDOCLEVEL_BADARG
;
318 * For standard man(1) and -a output mode,
319 * prepare for copying filename pointers
320 * into the program parameter array.
323 if (outmode
== OUTMODE_ONE
) {
326 } else if (outmode
== OUTMODE_ALL
)
329 /* Iterate all matching manuals. */
331 for (i
= 0; i
< sz
; i
++) {
332 if (outmode
== OUTMODE_FLN
)
334 else if (outmode
== OUTMODE_LST
)
335 printf("%s - %s\n", res
[i
].names
,
336 res
[i
].output
== NULL
? "" :
338 else if (outmode
== OUTMODE_ONE
) {
339 /* Search for the best section. */
340 isec
= strcspn(res
[i
].file
, "123456789");
341 sec
= res
[i
].file
[isec
];
344 prio
= sec_prios
[sec
- '1'];
345 if (prio
>= best_prio
)
353 * For man(1), -a and -i output mode, fall through
354 * to the main mandoc(1) code iterating files
355 * and running the parsers on each of them.
358 if (outmode
== OUTMODE_FLN
|| outmode
== OUTMODE_LST
)
361 fputs("mandoc: database support not compiled in\n",
363 return((int)MANDOCLEVEL_BADARG
);
369 if ( ! moptions(&options
, auxpaths
))
370 return((int)MANDOCLEVEL_BADARG
);
372 if (use_pager
&& isatty(STDOUT_FILENO
))
375 curp
.mchars
= mchars_alloc();
376 curp
.mp
= mparse_alloc(options
, curp
.wlevel
, mmsg
,
380 * Conditionally start up the lookaside buffer before parsing.
382 if (OUTT_MAN
== curp
.outtype
)
383 mparse_keep(curp
.mp
);
386 parse(&curp
, STDIN_FILENO
, "<stdin>", &rc
);
391 rc
= mparse_open(curp
.mp
, &fd
, resp
->file
,
395 else if (resp
->form
& FORM_SRC
) {
396 /* For .so only; ignore failure. */
397 chdir(paths
.paths
[resp
->ipath
]);
398 parse(&curp
, fd
, resp
->file
, &rc
);
400 rc
= passthrough(resp
->file
, fd
,
406 rc
= mparse_open(curp
.mp
, &fd
, *argv
++,
409 parse(&curp
, fd
, argv
[-1], &rc
);
413 mparse_wait(curp
.mp
, child_pid
) != MANDOCLEVEL_OK
)
414 rc
= MANDOCLEVEL_SYSERR
;
416 if (MANDOCLEVEL_OK
!= rc
&& curp
.wstop
)
422 (*curp
.outfree
)(curp
.outdata
);
423 mparse_free(curp
.mp
);
424 mchars_free(curp
.mchars
);
428 if (search
.argmode
!= ARG_FILE
) {
429 manpath_free(&paths
);
430 mansearch_free(res
, sz
);
444 printf("mandoc %s\n", VERSION
);
445 exit((int)MANDOCLEVEL_OK
);
449 usage(enum argmode argmode
)
454 fputs("usage: mandoc [-acfhklV] [-Ios=name] "
455 "[-Kencoding] [-mformat] [-Ooption]\n"
456 "\t [-Toutput] [-Wlevel] [file ...]\n", stderr
);
459 fputs("usage: man [-acfhklVw] [-C file] "
460 "[-M path] [-m path] [-S arch] [-s section]\n"
461 "\t [section] name ...\n", stderr
);
464 fputs("usage: whatis [-acfhklVw] [-C file] "
465 "[-M path] [-m path] [-O outkey] [-S arch]\n"
466 "\t [-s section] name ...\n", stderr
);
469 fputs("usage: apropos [-acfhklVw] [-C file] "
470 "[-M path] [-m path] [-O outkey] [-S arch]\n"
471 "\t [-s section] expression ...\n", stderr
);
474 exit((int)MANDOCLEVEL_BADARG
);
478 parse(struct curparse
*curp
, int fd
, const char *file
,
479 enum mandoclevel
*level
)
485 /* Begin by parsing the file itself. */
490 rc
= mparse_readfd(curp
->mp
, fd
, file
);
492 /* Stop immediately if the parse has failed. */
494 if (MANDOCLEVEL_FATAL
<= rc
)
498 * With -Wstop and warnings or errors of at least the requested
499 * level, do not produce output.
502 if (MANDOCLEVEL_OK
!= rc
&& curp
->wstop
)
505 /* If unset, allocate output dev now (if applicable). */
507 if ( ! (curp
->outman
&& curp
->outmdoc
)) {
508 switch (curp
->outtype
) {
510 curp
->outdata
= html_alloc(curp
->mchars
,
512 curp
->outfree
= html_free
;
515 curp
->outdata
= utf8_alloc(curp
->mchars
,
517 curp
->outfree
= ascii_free
;
520 curp
->outdata
= locale_alloc(curp
->mchars
,
522 curp
->outfree
= ascii_free
;
525 curp
->outdata
= ascii_alloc(curp
->mchars
,
527 curp
->outfree
= ascii_free
;
530 curp
->outdata
= pdf_alloc(curp
->mchars
,
532 curp
->outfree
= pspdf_free
;
535 curp
->outdata
= ps_alloc(curp
->mchars
,
537 curp
->outfree
= pspdf_free
;
543 switch (curp
->outtype
) {
545 curp
->outman
= html_man
;
546 curp
->outmdoc
= html_mdoc
;
549 curp
->outman
= tree_man
;
550 curp
->outmdoc
= tree_mdoc
;
553 curp
->outmdoc
= man_mdoc
;
554 curp
->outman
= man_man
;
565 curp
->outman
= terminal_man
;
566 curp
->outmdoc
= terminal_mdoc
;
573 mparse_result(curp
->mp
, &mdoc
, &man
, NULL
);
575 /* Execute the out device, if it exists. */
577 if (man
&& curp
->outman
)
578 (*curp
->outman
)(curp
->outdata
, man
);
579 if (mdoc
&& curp
->outmdoc
)
580 (*curp
->outmdoc
)(curp
->outdata
, mdoc
);
584 mparse_reset(curp
->mp
);
590 static enum mandoclevel
591 passthrough(const char *file
, int fd
, int synopsis_only
)
593 const char synb
[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
594 const char synr
[] = "SYNOPSIS";
603 if ((stream
= fdopen(fd
, "r")) == NULL
) {
610 while ((line
= fgetln(stream
, &len
)) != NULL
) {
613 if ( ! isspace((unsigned char)*line
))
616 isspace((unsigned char)*line
)) {
621 if ((len
== sizeof(synb
) &&
622 ! strncmp(line
, synb
, len
- 1)) ||
623 (len
== sizeof(synr
) &&
624 ! strncmp(line
, synr
, len
- 1)))
629 for (off
= 0; off
< len
; off
+= nw
)
630 if ((nw
= write(STDOUT_FILENO
, line
+ off
,
631 len
- off
)) == -1 || nw
== 0) {
638 if (ferror(stream
)) {
646 return(MANDOCLEVEL_OK
);
649 fprintf(stderr
, "%s: %s: SYSERR: %s: %s",
650 progname
, file
, syscall
, strerror(errno
));
651 return(MANDOCLEVEL_SYSERR
);
655 koptions(int *options
, char *arg
)
658 if ( ! strcmp(arg
, "utf-8")) {
659 *options
|= MPARSE_UTF8
;
660 *options
&= ~MPARSE_LATIN1
;
661 } else if ( ! strcmp(arg
, "iso-8859-1")) {
662 *options
|= MPARSE_LATIN1
;
663 *options
&= ~MPARSE_UTF8
;
664 } else if ( ! strcmp(arg
, "us-ascii")) {
665 *options
&= ~(MPARSE_UTF8
| MPARSE_LATIN1
);
667 fprintf(stderr
, "%s: -K%s: Bad argument\n",
675 moptions(int *options
, char *arg
)
680 else if (0 == strcmp(arg
, "doc"))
681 *options
|= MPARSE_MDOC
;
682 else if (0 == strcmp(arg
, "andoc"))
684 else if (0 == strcmp(arg
, "an"))
685 *options
|= MPARSE_MAN
;
687 fprintf(stderr
, "%s: -m%s: Bad argument\n",
696 toptions(struct curparse
*curp
, char *arg
)
699 if (0 == strcmp(arg
, "ascii"))
700 curp
->outtype
= OUTT_ASCII
;
701 else if (0 == strcmp(arg
, "lint")) {
702 curp
->outtype
= OUTT_LINT
;
703 curp
->wlevel
= MANDOCLEVEL_WARNING
;
704 } else if (0 == strcmp(arg
, "tree"))
705 curp
->outtype
= OUTT_TREE
;
706 else if (0 == strcmp(arg
, "man"))
707 curp
->outtype
= OUTT_MAN
;
708 else if (0 == strcmp(arg
, "html"))
709 curp
->outtype
= OUTT_HTML
;
710 else if (0 == strcmp(arg
, "utf8"))
711 curp
->outtype
= OUTT_UTF8
;
712 else if (0 == strcmp(arg
, "locale"))
713 curp
->outtype
= OUTT_LOCALE
;
714 else if (0 == strcmp(arg
, "xhtml"))
715 curp
->outtype
= OUTT_HTML
;
716 else if (0 == strcmp(arg
, "ps"))
717 curp
->outtype
= OUTT_PS
;
718 else if (0 == strcmp(arg
, "pdf"))
719 curp
->outtype
= OUTT_PDF
;
721 fprintf(stderr
, "%s: -T%s: Bad argument\n",
730 woptions(struct curparse
*curp
, char *arg
)
744 switch (getsubopt(&arg
, UNCONST(toks
), &v
)) {
751 curp
->wlevel
= MANDOCLEVEL_WARNING
;
754 curp
->wlevel
= MANDOCLEVEL_ERROR
;
757 curp
->wlevel
= MANDOCLEVEL_FATAL
;
760 fprintf(stderr
, "%s: -W%s: Bad argument\n",
770 mmsg(enum mandocerr t
, enum mandoclevel lvl
,
771 const char *file
, int line
, int col
, const char *msg
)
773 const char *mparse_msg
;
775 fprintf(stderr
, "%s: %s:", progname
, file
);
778 fprintf(stderr
, "%d:%d:", line
, col
+ 1);
780 fprintf(stderr
, " %s", mparse_strlevel(lvl
));
782 if (NULL
!= (mparse_msg
= mparse_strerror(t
)))
783 fprintf(stderr
, ": %s", mparse_msg
);
786 fprintf(stderr
, ": %s", msg
);
794 #define MAX_PAGER_ARGS 16
795 char *argv
[MAX_PAGER_ARGS
];
801 if (pipe(fildes
) == -1) {
802 fprintf(stderr
, "%s: pipe: %s\n",
803 progname
, strerror(errno
));
809 fprintf(stderr
, "%s: fork: %s\n",
810 progname
, strerror(errno
));
811 exit((int)MANDOCLEVEL_SYSERR
);
814 if (dup2(fildes
[1], STDOUT_FILENO
) == -1) {
815 fprintf(stderr
, "%s: dup output: %s\n",
816 progname
, strerror(errno
));
817 exit((int)MANDOCLEVEL_SYSERR
);
824 /* The original process becomes the pager. */
827 if (dup2(fildes
[0], STDIN_FILENO
) == -1) {
828 fprintf(stderr
, "%s: dup input: %s\n",
829 progname
, strerror(errno
));
830 exit((int)MANDOCLEVEL_SYSERR
);
833 pager
= getenv("MANPAGER");
834 if (pager
== NULL
|| *pager
== '\0')
835 pager
= getenv("PAGER");
836 if (pager
== NULL
|| *pager
== '\0')
837 pager
= "/usr/bin/more -s";
838 cp
= mandoc_strdup(pager
);
841 * Parse the pager command into words.
842 * Intentionally do not do anything fancy here.
846 while (argc
+ 1 < MAX_PAGER_ARGS
) {
848 cp
= strchr(cp
, ' ');
859 /* Hand over to the pager. */
861 execvp(argv
[0], argv
);
862 fprintf(stderr
, "%s: exec: %s\n",
863 progname
, strerror(errno
));
864 exit((int)MANDOCLEVEL_SYSERR
);