]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
1 /* $Id: main.c,v 1.194 2014/10/25 01:03:52 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_XHTML
, /* -Txhtml */
68 OUTT_LINT
, /* -Tlint */
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);
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
;
125 progname
= strrchr(argv
[0], '/');
126 if (progname
== NULL
)
131 /* Search options. */
133 memset(&paths
, 0, sizeof(struct manpaths
));
134 conf_file
= defpaths
= auxpaths
= NULL
;
136 memset(&search
, 0, sizeof(struct mansearch
));
137 search
.outkey
= "Nd";
139 if (strcmp(progname
, "man") == 0)
140 search
.argmode
= ARG_NAME
;
141 else if (strncmp(progname
, "apropos", 7) == 0)
142 search
.argmode
= ARG_EXPR
;
143 else if (strncmp(progname
, "whatis", 6) == 0)
144 search
.argmode
= ARG_WORD
;
146 search
.argmode
= ARG_FILE
;
148 /* Parser and formatter options. */
150 memset(&curp
, 0, sizeof(struct curparse
));
151 curp
.outtype
= OUTT_ASCII
;
152 curp
.wlevel
= MANDOCLEVEL_FATAL
;
153 options
= MPARSE_SO
| MPARSE_UTF8
| MPARSE_LATIN1
;
158 outmode
= OUTMODE_DEF
;
160 while (-1 != (c
= getopt(argc
, argv
,
161 "aC:cfhI:iK:klM:m:O:S:s:T:VW:w"))) {
164 outmode
= OUTMODE_ALL
;
173 search
.argmode
= ARG_WORD
;
176 (void)strlcat(curp
.outopts
, "synopsis,", BUFSIZ
);
177 outmode
= OUTMODE_ALL
;
180 if (strncmp(optarg
, "os=", 3)) {
182 "%s: -I%s: Bad argument\n",
184 return((int)MANDOCLEVEL_BADARG
);
188 "%s: -I%s: Duplicate argument\n",
190 return((int)MANDOCLEVEL_BADARG
);
192 defos
= mandoc_strdup(optarg
+ 3);
195 outmode
= OUTMODE_INT
;
198 if ( ! koptions(&options
, optarg
))
199 return((int)MANDOCLEVEL_BADARG
);
202 search
.argmode
= ARG_EXPR
;
205 search
.argmode
= ARG_FILE
;
206 outmode
= OUTMODE_ALL
;
215 search
.outkey
= optarg
;
216 (void)strlcat(curp
.outopts
, optarg
, BUFSIZ
);
217 (void)strlcat(curp
.outopts
, ",", BUFSIZ
);
220 search
.arch
= optarg
;
226 if ( ! toptions(&curp
, optarg
))
227 return((int)MANDOCLEVEL_BADARG
);
230 if ( ! woptions(&curp
, optarg
))
231 return((int)MANDOCLEVEL_BADARG
);
234 outmode
= OUTMODE_FLN
;
246 usage(search
.argmode
);
248 /* Postprocess options. */
250 if (outmode
== OUTMODE_DEF
) {
251 switch (search
.argmode
) {
253 outmode
= OUTMODE_ALL
;
257 outmode
= OUTMODE_ONE
;
260 outmode
= OUTMODE_LST
;
265 /* Parse arguments. */
273 /* Quirk for a man(1) section argument without -s. */
275 if (search
.argmode
== ARG_NAME
&&
277 isdigit((unsigned char)argv
[0][0]) &&
278 (argv
[0][1] == '\0' || !strcmp(argv
[0], "3p"))) {
279 search
.sec
= argv
[0];
286 /* man(1), whatis(1), apropos(1) */
288 if (search
.argmode
!= ARG_FILE
) {
291 usage(search
.argmode
);
293 /* Access the mandoc database. */
295 manpath_parse(&paths
, conf_file
, defpaths
, auxpaths
);
297 if( ! mansearch(&search
, &paths
, argc
, argv
, &res
, &sz
))
298 usage(search
.argmode
);
302 if (search
.argmode
== ARG_NAME
)
303 fprintf(stderr
, "%s: No entry for %s "
304 "in the manual.\n", progname
, argv
[0]);
305 rc
= MANDOCLEVEL_BADARG
;
310 * For standard man(1) and -a output mode,
311 * prepare for copying filename pointers
312 * into the program parameter array.
315 if (outmode
== OUTMODE_ONE
) {
318 } else if (outmode
== OUTMODE_ALL
)
321 /* Iterate all matching manuals. */
323 for (i
= 0; i
< sz
; i
++) {
324 if (outmode
== OUTMODE_FLN
)
326 else if (outmode
== OUTMODE_LST
)
327 printf("%s - %s\n", res
[i
].names
,
328 res
[i
].output
== NULL
? "" :
330 else if (outmode
== OUTMODE_ONE
) {
331 /* Search for the best section. */
332 isec
= strcspn(res
[i
].file
, "123456789");
333 sec
= res
[i
].file
[isec
];
336 prio
= sec_prios
[sec
- '1'];
337 if (prio
>= best_prio
)
345 * For man(1), -a and -i output mode, fall through
346 * to the main mandoc(1) code iterating files
347 * and running the parsers on each of them.
350 if (outmode
== OUTMODE_FLN
|| outmode
== OUTMODE_LST
)
353 fputs("mandoc: database support not compiled in\n",
355 return((int)MANDOCLEVEL_BADARG
);
361 if ( ! moptions(&options
, auxpaths
))
362 return((int)MANDOCLEVEL_BADARG
);
364 if (use_pager
&& isatty(STDOUT_FILENO
))
367 curp
.mp
= mparse_alloc(options
, curp
.wlevel
, mmsg
, defos
);
370 * Conditionally start up the lookaside buffer before parsing.
372 if (OUTT_MAN
== curp
.outtype
)
373 mparse_keep(curp
.mp
);
376 parse(&curp
, STDIN_FILENO
, "<stdin>", &rc
);
381 rc
= mparse_open(curp
.mp
, &fd
, resp
->file
,
385 else if (resp
->form
& FORM_SRC
) {
386 /* For .so only; ignore failure. */
387 chdir(paths
.paths
[resp
->ipath
]);
388 parse(&curp
, fd
, resp
->file
, &rc
);
390 rc
= passthrough(resp
->file
, fd
);
395 rc
= mparse_open(curp
.mp
, &fd
, *argv
++,
398 parse(&curp
, fd
, argv
[-1], &rc
);
402 mparse_wait(curp
.mp
, child_pid
) != MANDOCLEVEL_OK
)
403 rc
= MANDOCLEVEL_SYSERR
;
405 if (MANDOCLEVEL_OK
!= rc
&& curp
.wstop
)
411 (*curp
.outfree
)(curp
.outdata
);
413 mparse_free(curp
.mp
);
417 if (search
.argmode
!= ARG_FILE
) {
418 manpath_free(&paths
);
419 mansearch_free(res
, sz
);
433 printf("mandoc %s\n", VERSION
);
434 exit((int)MANDOCLEVEL_OK
);
438 usage(enum argmode argmode
)
443 fputs("usage: mandoc [-acfhklV] [-Ios=name] "
444 "[-mformat] [-Ooption] [-Toutput] [-Wlevel]\n"
445 "\t [file ...]\n", stderr
);
448 fputs("usage: man [-acfhklVw] [-C file] "
449 "[-M path] [-m path] [-S arch] [-s section]\n"
450 "\t [section] name ...\n", stderr
);
453 fputs("usage: whatis [-acfhklVw] [-C file] "
454 "[-M path] [-m path] [-O outkey] [-S arch]\n"
455 "\t [-s section] name ...\n", stderr
);
458 fputs("usage: apropos [-acfhklVw] [-C file] "
459 "[-M path] [-m path] [-O outkey] [-S arch]\n"
460 "\t [-s section] expression ...\n", stderr
);
463 exit((int)MANDOCLEVEL_BADARG
);
467 parse(struct curparse
*curp
, int fd
, const char *file
,
468 enum mandoclevel
*level
)
474 /* Begin by parsing the file itself. */
479 rc
= mparse_readfd(curp
->mp
, fd
, file
);
481 /* Stop immediately if the parse has failed. */
483 if (MANDOCLEVEL_FATAL
<= rc
)
487 * With -Wstop and warnings or errors of at least the requested
488 * level, do not produce output.
491 if (MANDOCLEVEL_OK
!= rc
&& curp
->wstop
)
494 /* If unset, allocate output dev now (if applicable). */
496 if ( ! (curp
->outman
&& curp
->outmdoc
)) {
497 switch (curp
->outtype
) {
499 curp
->outdata
= xhtml_alloc(curp
->outopts
);
500 curp
->outfree
= html_free
;
503 curp
->outdata
= html_alloc(curp
->outopts
);
504 curp
->outfree
= html_free
;
507 curp
->outdata
= utf8_alloc(curp
->outopts
);
508 curp
->outfree
= ascii_free
;
511 curp
->outdata
= locale_alloc(curp
->outopts
);
512 curp
->outfree
= ascii_free
;
515 curp
->outdata
= ascii_alloc(curp
->outopts
);
516 curp
->outfree
= ascii_free
;
519 curp
->outdata
= pdf_alloc(curp
->outopts
);
520 curp
->outfree
= pspdf_free
;
523 curp
->outdata
= ps_alloc(curp
->outopts
);
524 curp
->outfree
= pspdf_free
;
530 switch (curp
->outtype
) {
534 curp
->outman
= html_man
;
535 curp
->outmdoc
= html_mdoc
;
538 curp
->outman
= tree_man
;
539 curp
->outmdoc
= tree_mdoc
;
542 curp
->outmdoc
= man_mdoc
;
543 curp
->outman
= man_man
;
554 curp
->outman
= terminal_man
;
555 curp
->outmdoc
= terminal_mdoc
;
562 mparse_result(curp
->mp
, &mdoc
, &man
, NULL
);
564 /* Execute the out device, if it exists. */
566 if (man
&& curp
->outman
)
567 (*curp
->outman
)(curp
->outdata
, man
);
568 if (mdoc
&& curp
->outmdoc
)
569 (*curp
->outmdoc
)(curp
->outdata
, mdoc
);
573 mparse_reset(curp
->mp
);
579 static enum mandoclevel
580 passthrough(const char *file
, int fd
)
586 while ((nr
= read(fd
, buf
, BUFSIZ
)) != -1 && nr
!= 0)
587 for (off
= 0; off
< nr
; off
+= nw
)
588 if ((nw
= write(STDOUT_FILENO
, buf
+ off
,
589 (size_t)(nr
- off
))) == -1 || nw
== 0) {
598 return(MANDOCLEVEL_OK
);
602 fprintf(stderr
, "%s: %s: SYSERR: %s: %s",
603 progname
, file
, syscall
, strerror(errno
));
604 return(MANDOCLEVEL_SYSERR
);
608 koptions(int *options
, char *arg
)
611 if ( ! strcmp(arg
, "utf-8")) {
612 *options
|= MPARSE_UTF8
;
613 *options
&= ~MPARSE_LATIN1
;
614 } else if ( ! strcmp(arg
, "iso-8859-1")) {
615 *options
|= MPARSE_LATIN1
;
616 *options
&= ~MPARSE_UTF8
;
617 } else if ( ! strcmp(arg
, "us-ascii")) {
618 *options
&= ~(MPARSE_UTF8
| MPARSE_LATIN1
);
620 fprintf(stderr
, "%s: -K%s: Bad argument\n",
628 moptions(int *options
, char *arg
)
633 else if (0 == strcmp(arg
, "doc"))
634 *options
|= MPARSE_MDOC
;
635 else if (0 == strcmp(arg
, "andoc"))
637 else if (0 == strcmp(arg
, "an"))
638 *options
|= MPARSE_MAN
;
640 fprintf(stderr
, "%s: -m%s: Bad argument\n",
649 toptions(struct curparse
*curp
, char *arg
)
652 if (0 == strcmp(arg
, "ascii"))
653 curp
->outtype
= OUTT_ASCII
;
654 else if (0 == strcmp(arg
, "lint")) {
655 curp
->outtype
= OUTT_LINT
;
656 curp
->wlevel
= MANDOCLEVEL_WARNING
;
657 } else if (0 == strcmp(arg
, "tree"))
658 curp
->outtype
= OUTT_TREE
;
659 else if (0 == strcmp(arg
, "man"))
660 curp
->outtype
= OUTT_MAN
;
661 else if (0 == strcmp(arg
, "html"))
662 curp
->outtype
= OUTT_HTML
;
663 else if (0 == strcmp(arg
, "utf8"))
664 curp
->outtype
= OUTT_UTF8
;
665 else if (0 == strcmp(arg
, "locale"))
666 curp
->outtype
= OUTT_LOCALE
;
667 else if (0 == strcmp(arg
, "xhtml"))
668 curp
->outtype
= OUTT_XHTML
;
669 else if (0 == strcmp(arg
, "ps"))
670 curp
->outtype
= OUTT_PS
;
671 else if (0 == strcmp(arg
, "pdf"))
672 curp
->outtype
= OUTT_PDF
;
674 fprintf(stderr
, "%s: -T%s: Bad argument\n",
683 woptions(struct curparse
*curp
, char *arg
)
697 switch (getsubopt(&arg
, UNCONST(toks
), &v
)) {
704 curp
->wlevel
= MANDOCLEVEL_WARNING
;
707 curp
->wlevel
= MANDOCLEVEL_ERROR
;
710 curp
->wlevel
= MANDOCLEVEL_FATAL
;
713 fprintf(stderr
, "%s: -W%s: Bad argument\n",
723 mmsg(enum mandocerr t
, enum mandoclevel lvl
,
724 const char *file
, int line
, int col
, const char *msg
)
726 const char *mparse_msg
;
728 fprintf(stderr
, "%s: %s:", progname
, file
);
731 fprintf(stderr
, "%d:%d:", line
, col
+ 1);
733 fprintf(stderr
, " %s", mparse_strlevel(lvl
));
735 if (NULL
!= (mparse_msg
= mparse_strerror(t
)))
736 fprintf(stderr
, ": %s", mparse_msg
);
739 fprintf(stderr
, ": %s", msg
);
747 #define MAX_PAGER_ARGS 16
748 char *argv
[MAX_PAGER_ARGS
];
754 if (pipe(fildes
) == -1) {
755 fprintf(stderr
, "%s: pipe: %s\n",
756 progname
, strerror(errno
));
762 fprintf(stderr
, "%s: fork: %s\n",
763 progname
, strerror(errno
));
764 exit((int)MANDOCLEVEL_SYSERR
);
767 if (dup2(fildes
[1], STDOUT_FILENO
) == -1) {
768 fprintf(stderr
, "%s: dup output: %s\n",
769 progname
, strerror(errno
));
770 exit((int)MANDOCLEVEL_SYSERR
);
777 /* The original process becomes the pager. */
780 if (dup2(fildes
[0], STDIN_FILENO
) == -1) {
781 fprintf(stderr
, "%s: dup input: %s\n",
782 progname
, strerror(errno
));
783 exit((int)MANDOCLEVEL_SYSERR
);
786 pager
= getenv("MANPAGER");
787 if (pager
== NULL
|| *pager
== '\0')
788 pager
= getenv("PAGER");
789 if (pager
== NULL
|| *pager
== '\0')
790 pager
= "/usr/bin/more -s";
791 cp
= mandoc_strdup(pager
);
794 * Parse the pager command into words.
795 * Intentionally do not do anything fancy here.
799 while (argc
+ 1 < MAX_PAGER_ARGS
) {
801 cp
= strchr(cp
, ' ');
812 /* Hand over to the pager. */
814 execvp(argv
[0], argv
);
815 fprintf(stderr
, "%s: exec: %s\n",
816 progname
, strerror(errno
));
817 exit((int)MANDOCLEVEL_SYSERR
);