]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
e0380e945e1dac2f8e2f411e44f352408983ee63
1 /* $Id: main.c,v 1.187 2014/08/23 22:26:06 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 moptions(int *, char *);
86 static void mmsg(enum mandocerr
, enum mandoclevel
,
87 const char *, int, int, const char *);
88 static void parse(struct curparse
*, int,
89 const char *, enum mandoclevel
*);
90 static enum mandoclevel
passthrough(const char *);
91 static void spawn_pager(void);
92 static int toptions(struct curparse
*, char *);
93 static void usage(enum argmode
) __attribute__((noreturn
));
94 static void version(void) __attribute__((noreturn
));
95 static int woptions(struct curparse
*, char *);
97 static const int sec_prios
[] = {1, 4, 5, 8, 6, 3, 7, 2, 9};
98 static const char *progname
;
102 main(int argc
, char *argv
[])
104 struct curparse curp
;
105 struct mansearch search
;
106 struct manpaths paths
;
107 char *conf_file
, *defpaths
, *auxpaths
;
110 struct manpage
*res
, *resp
;
116 enum outmode outmode
;
122 progname
= strrchr(argv
[0], '/');
123 if (progname
== NULL
)
128 /* Search options. */
130 memset(&paths
, 0, sizeof(struct manpaths
));
131 conf_file
= defpaths
= auxpaths
= NULL
;
133 memset(&search
, 0, sizeof(struct mansearch
));
134 search
.outkey
= "Nd";
136 if (strcmp(progname
, "man") == 0)
137 search
.argmode
= ARG_NAME
;
138 else if (strncmp(progname
, "apropos", 7) == 0)
139 search
.argmode
= ARG_EXPR
;
140 else if (strncmp(progname
, "whatis", 6) == 0)
141 search
.argmode
= ARG_WORD
;
143 search
.argmode
= ARG_FILE
;
145 /* Parser and formatter options. */
147 memset(&curp
, 0, sizeof(struct curparse
));
148 curp
.outtype
= OUTT_ASCII
;
149 curp
.wlevel
= MANDOCLEVEL_FATAL
;
155 outmode
= OUTMODE_DEF
;
157 while (-1 != (c
= getopt(argc
, argv
, "aC:cfI:ikM:m:O:S:s:T:VW:w"))) {
160 outmode
= OUTMODE_ALL
;
169 search
.argmode
= ARG_WORD
;
172 if (strncmp(optarg
, "os=", 3)) {
174 "%s: -I%s: Bad argument\n",
176 return((int)MANDOCLEVEL_BADARG
);
180 "%s: -I%s: Duplicate argument\n",
182 return((int)MANDOCLEVEL_BADARG
);
184 defos
= mandoc_strdup(optarg
+ 3);
187 outmode
= OUTMODE_INT
;
190 search
.argmode
= ARG_EXPR
;
199 search
.outkey
= optarg
;
200 (void)strlcat(curp
.outopts
, optarg
, BUFSIZ
);
201 (void)strlcat(curp
.outopts
, ",", BUFSIZ
);
204 search
.arch
= optarg
;
210 if ( ! toptions(&curp
, optarg
))
211 return((int)MANDOCLEVEL_BADARG
);
214 if ( ! woptions(&curp
, optarg
))
215 return((int)MANDOCLEVEL_BADARG
);
218 outmode
= OUTMODE_FLN
;
230 usage(search
.argmode
);
232 /* Postprocess options. */
234 if (outmode
== OUTMODE_DEF
) {
235 switch (search
.argmode
) {
237 outmode
= OUTMODE_ALL
;
241 outmode
= OUTMODE_ONE
;
244 outmode
= OUTMODE_LST
;
249 /* Parse arguments. */
257 /* Quirk for a man(1) section argument without -s. */
259 if (search
.argmode
== ARG_NAME
&&
261 isdigit((unsigned char)argv
[0][0]) &&
262 (argv
[0][1] == '\0' || !strcmp(argv
[0], "3p"))) {
263 search
.sec
= argv
[0];
270 /* man(1), whatis(1), apropos(1) */
272 if (search
.argmode
!= ARG_FILE
) {
275 usage(search
.argmode
);
277 /* Access the mandoc database. */
279 manpath_parse(&paths
, conf_file
, defpaths
, auxpaths
);
281 if( ! mansearch(&search
, &paths
, argc
, argv
, &res
, &sz
))
282 usage(search
.argmode
);
283 manpath_free(&paths
);
287 if (search
.argmode
== ARG_NAME
)
288 fprintf(stderr
, "%s: No entry for %s "
289 "in the manual.\n", progname
, argv
[0]);
290 rc
= MANDOCLEVEL_BADARG
;
295 * For standard man(1) and -a output mode,
296 * prepare for copying filename pointers
297 * into the program parameter array.
300 if (outmode
== OUTMODE_ONE
) {
303 } else if (outmode
== OUTMODE_ALL
)
306 /* Iterate all matching manuals. */
308 for (i
= 0; i
< sz
; i
++) {
309 if (outmode
== OUTMODE_FLN
)
311 else if (outmode
== OUTMODE_LST
)
312 printf("%s - %s\n", res
[i
].names
,
313 res
[i
].output
== NULL
? "" :
315 else if (outmode
== OUTMODE_ONE
) {
316 /* Search for the best section. */
317 isec
= strcspn(res
[i
].file
, "123456789");
318 sec
= res
[i
].file
[isec
];
321 prio
= sec_prios
[sec
- '1'];
322 if (prio
>= best_prio
)
330 * For man(1), -a and -i output mode, fall through
331 * to the main mandoc(1) code iterating files
332 * and running the parsers on each of them.
335 if (outmode
== OUTMODE_FLN
|| outmode
== OUTMODE_LST
)
338 fputs("mandoc: database support not compiled in\n",
340 return((int)MANDOCLEVEL_BADARG
);
346 if ( ! moptions(&options
, auxpaths
))
347 return((int)MANDOCLEVEL_BADARG
);
349 if (use_pager
&& isatty(STDOUT_FILENO
))
352 curp
.mp
= mparse_alloc(options
, curp
.wlevel
, mmsg
, defos
);
355 * Conditionally start up the lookaside buffer before parsing.
357 if (OUTT_MAN
== curp
.outtype
)
358 mparse_keep(curp
.mp
);
361 parse(&curp
, STDIN_FILENO
, "<stdin>", &rc
);
367 parse(&curp
, -1, resp
->file
, &rc
);
369 rc
= passthrough(resp
->file
);
373 parse(&curp
, -1, *argv
++, &rc
);
374 if (MANDOCLEVEL_OK
!= rc
&& curp
.wstop
)
380 (*curp
.outfree
)(curp
.outdata
);
382 mparse_free(curp
.mp
);
386 if (search
.argmode
!= ARG_FILE
) {
387 mansearch_free(res
, sz
);
401 printf("mandoc %s\n", VERSION
);
402 exit((int)MANDOCLEVEL_OK
);
406 usage(enum argmode argmode
)
411 fputs("usage: mandoc [-V] [-Ios=name] [-mformat]"
412 " [-Ooption] [-Toutput] [-Wlevel]\n"
413 "\t [file ...]\n", stderr
);
416 fputs("usage: man [-acfhkVw] [-C file] "
417 "[-M path] [-m path] [-S arch] [-s section]\n"
418 "\t [section] name ...\n", stderr
);
421 fputs("usage: whatis [-V] [-C file] [-M path] [-m path] "
422 "[-S arch] [-s section] name ...\n", stderr
);
425 fputs("usage: apropos [-V] [-C file] [-M path] [-m path] "
426 "[-O outkey] [-S arch]\n"
427 "\t [-s section] expression ...\n", stderr
);
430 exit((int)MANDOCLEVEL_BADARG
);
434 parse(struct curparse
*curp
, int fd
, const char *file
,
435 enum mandoclevel
*level
)
441 /* Begin by parsing the file itself. */
446 rc
= mparse_readfd(curp
->mp
, fd
, file
);
448 /* Stop immediately if the parse has failed. */
450 if (MANDOCLEVEL_FATAL
<= rc
)
454 * With -Wstop and warnings or errors of at least the requested
455 * level, do not produce output.
458 if (MANDOCLEVEL_OK
!= rc
&& curp
->wstop
)
461 /* If unset, allocate output dev now (if applicable). */
463 if ( ! (curp
->outman
&& curp
->outmdoc
)) {
464 switch (curp
->outtype
) {
466 curp
->outdata
= xhtml_alloc(curp
->outopts
);
467 curp
->outfree
= html_free
;
470 curp
->outdata
= html_alloc(curp
->outopts
);
471 curp
->outfree
= html_free
;
474 curp
->outdata
= utf8_alloc(curp
->outopts
);
475 curp
->outfree
= ascii_free
;
478 curp
->outdata
= locale_alloc(curp
->outopts
);
479 curp
->outfree
= ascii_free
;
482 curp
->outdata
= ascii_alloc(curp
->outopts
);
483 curp
->outfree
= ascii_free
;
486 curp
->outdata
= pdf_alloc(curp
->outopts
);
487 curp
->outfree
= pspdf_free
;
490 curp
->outdata
= ps_alloc(curp
->outopts
);
491 curp
->outfree
= pspdf_free
;
497 switch (curp
->outtype
) {
501 curp
->outman
= html_man
;
502 curp
->outmdoc
= html_mdoc
;
505 curp
->outman
= tree_man
;
506 curp
->outmdoc
= tree_mdoc
;
509 curp
->outmdoc
= man_mdoc
;
510 curp
->outman
= man_man
;
521 curp
->outman
= terminal_man
;
522 curp
->outmdoc
= terminal_mdoc
;
529 mparse_result(curp
->mp
, &mdoc
, &man
, NULL
);
531 /* Execute the out device, if it exists. */
533 if (man
&& curp
->outman
)
534 (*curp
->outman
)(curp
->outdata
, man
);
535 if (mdoc
&& curp
->outmdoc
)
536 (*curp
->outmdoc
)(curp
->outdata
, mdoc
);
540 mparse_reset(curp
->mp
);
546 static enum mandoclevel
547 passthrough(const char *file
)
554 fd
= open(file
, O_RDONLY
);
560 while ((nr
= read(fd
, buf
, BUFSIZ
)) != -1 && nr
!= 0)
561 for (off
= 0; off
< nr
; off
+= nw
)
562 if ((nw
= write(STDOUT_FILENO
, buf
+ off
,
563 (size_t)(nr
- off
))) == -1 || nw
== 0) {
570 return(MANDOCLEVEL_OK
);
575 fprintf(stderr
, "%s: %s: SYSERR: %s: %s",
576 progname
, file
, syscall
, strerror(errno
));
577 return(MANDOCLEVEL_SYSERR
);
581 moptions(int *options
, char *arg
)
586 else if (0 == strcmp(arg
, "doc"))
587 *options
|= MPARSE_MDOC
;
588 else if (0 == strcmp(arg
, "andoc"))
590 else if (0 == strcmp(arg
, "an"))
591 *options
|= MPARSE_MAN
;
593 fprintf(stderr
, "%s: -m%s: Bad argument\n",
602 toptions(struct curparse
*curp
, char *arg
)
605 if (0 == strcmp(arg
, "ascii"))
606 curp
->outtype
= OUTT_ASCII
;
607 else if (0 == strcmp(arg
, "lint")) {
608 curp
->outtype
= OUTT_LINT
;
609 curp
->wlevel
= MANDOCLEVEL_WARNING
;
610 } else if (0 == strcmp(arg
, "tree"))
611 curp
->outtype
= OUTT_TREE
;
612 else if (0 == strcmp(arg
, "man"))
613 curp
->outtype
= OUTT_MAN
;
614 else if (0 == strcmp(arg
, "html"))
615 curp
->outtype
= OUTT_HTML
;
616 else if (0 == strcmp(arg
, "utf8"))
617 curp
->outtype
= OUTT_UTF8
;
618 else if (0 == strcmp(arg
, "locale"))
619 curp
->outtype
= OUTT_LOCALE
;
620 else if (0 == strcmp(arg
, "xhtml"))
621 curp
->outtype
= OUTT_XHTML
;
622 else if (0 == strcmp(arg
, "ps"))
623 curp
->outtype
= OUTT_PS
;
624 else if (0 == strcmp(arg
, "pdf"))
625 curp
->outtype
= OUTT_PDF
;
627 fprintf(stderr
, "%s: -T%s: Bad argument\n",
636 woptions(struct curparse
*curp
, char *arg
)
650 switch (getsubopt(&arg
, UNCONST(toks
), &v
)) {
657 curp
->wlevel
= MANDOCLEVEL_WARNING
;
660 curp
->wlevel
= MANDOCLEVEL_ERROR
;
663 curp
->wlevel
= MANDOCLEVEL_FATAL
;
666 fprintf(stderr
, "%s: -W%s: Bad argument\n",
676 mmsg(enum mandocerr t
, enum mandoclevel lvl
,
677 const char *file
, int line
, int col
, const char *msg
)
679 const char *mparse_msg
;
681 fprintf(stderr
, "%s: %s:", progname
, file
);
684 fprintf(stderr
, "%d:%d:", line
, col
+ 1);
686 fprintf(stderr
, " %s", mparse_strlevel(lvl
));
688 if (NULL
!= (mparse_msg
= mparse_strerror(t
)))
689 fprintf(stderr
, ": %s", mparse_msg
);
692 fprintf(stderr
, ": %s", msg
);
700 #define MAX_PAGER_ARGS 16
701 char *argv
[MAX_PAGER_ARGS
];
707 if (pipe(fildes
) == -1) {
708 fprintf(stderr
, "%s: pipe: %s\n",
709 progname
, strerror(errno
));
715 fprintf(stderr
, "%s: fork: %s\n",
716 progname
, strerror(errno
));
717 exit((int)MANDOCLEVEL_SYSERR
);
720 if (dup2(fildes
[1], STDOUT_FILENO
) == -1) {
721 fprintf(stderr
, "%s: dup output: %s\n",
722 progname
, strerror(errno
));
723 exit((int)MANDOCLEVEL_SYSERR
);
730 /* The original process becomes the pager. */
733 if (dup2(fildes
[0], STDIN_FILENO
) == -1) {
734 fprintf(stderr
, "%s: dup input: %s\n",
735 progname
, strerror(errno
));
736 exit((int)MANDOCLEVEL_SYSERR
);
739 pager
= getenv("MANPAGER");
740 if (pager
== NULL
|| *pager
== '\0')
741 pager
= getenv("PAGER");
742 if (pager
== NULL
|| *pager
== '\0')
743 pager
= "/usr/bin/more -s";
744 cp
= mandoc_strdup(pager
);
747 * Parse the pager command into words.
748 * Intentionally do not do anything fancy here.
752 while (argc
+ 1 < MAX_PAGER_ARGS
) {
754 cp
= strchr(cp
, ' ');
765 /* Hand over to the pager. */
767 execvp(argv
[0], argv
);
768 fprintf(stderr
, "%s: exec: %s\n",
769 progname
, strerror(errno
));
770 exit((int)MANDOCLEVEL_SYSERR
);