]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
1 /* $Id: main.c,v 1.193 2014/10/18 15:57:34 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 *, int);
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
;
124 progname
= strrchr(argv
[0], '/');
125 if (progname
== NULL
)
130 /* Search options. */
132 memset(&paths
, 0, sizeof(struct manpaths
));
133 conf_file
= defpaths
= auxpaths
= NULL
;
135 memset(&search
, 0, sizeof(struct mansearch
));
136 search
.outkey
= "Nd";
138 if (strcmp(progname
, "man") == 0)
139 search
.argmode
= ARG_NAME
;
140 else if (strncmp(progname
, "apropos", 7) == 0)
141 search
.argmode
= ARG_EXPR
;
142 else if (strncmp(progname
, "whatis", 6) == 0)
143 search
.argmode
= ARG_WORD
;
145 search
.argmode
= ARG_FILE
;
147 /* Parser and formatter options. */
149 memset(&curp
, 0, sizeof(struct curparse
));
150 curp
.outtype
= OUTT_ASCII
;
151 curp
.wlevel
= MANDOCLEVEL_FATAL
;
157 outmode
= OUTMODE_DEF
;
159 while (-1 != (c
= getopt(argc
, argv
, "aC:cfhI:iklM:m:O:S:s:T:VW:w"))) {
162 outmode
= OUTMODE_ALL
;
171 search
.argmode
= ARG_WORD
;
174 (void)strlcat(curp
.outopts
, "synopsis,", BUFSIZ
);
175 outmode
= OUTMODE_ALL
;
178 if (strncmp(optarg
, "os=", 3)) {
180 "%s: -I%s: Bad argument\n",
182 return((int)MANDOCLEVEL_BADARG
);
186 "%s: -I%s: Duplicate argument\n",
188 return((int)MANDOCLEVEL_BADARG
);
190 defos
= mandoc_strdup(optarg
+ 3);
193 outmode
= OUTMODE_INT
;
196 search
.argmode
= ARG_EXPR
;
199 search
.argmode
= ARG_FILE
;
200 outmode
= OUTMODE_ALL
;
209 search
.outkey
= optarg
;
210 (void)strlcat(curp
.outopts
, optarg
, BUFSIZ
);
211 (void)strlcat(curp
.outopts
, ",", BUFSIZ
);
214 search
.arch
= optarg
;
220 if ( ! toptions(&curp
, optarg
))
221 return((int)MANDOCLEVEL_BADARG
);
224 if ( ! woptions(&curp
, optarg
))
225 return((int)MANDOCLEVEL_BADARG
);
228 outmode
= OUTMODE_FLN
;
240 usage(search
.argmode
);
242 /* Postprocess options. */
244 if (outmode
== OUTMODE_DEF
) {
245 switch (search
.argmode
) {
247 outmode
= OUTMODE_ALL
;
251 outmode
= OUTMODE_ONE
;
254 outmode
= OUTMODE_LST
;
259 /* Parse arguments. */
267 /* Quirk for a man(1) section argument without -s. */
269 if (search
.argmode
== ARG_NAME
&&
271 isdigit((unsigned char)argv
[0][0]) &&
272 (argv
[0][1] == '\0' || !strcmp(argv
[0], "3p"))) {
273 search
.sec
= argv
[0];
280 /* man(1), whatis(1), apropos(1) */
282 if (search
.argmode
!= ARG_FILE
) {
285 usage(search
.argmode
);
287 /* Access the mandoc database. */
289 manpath_parse(&paths
, conf_file
, defpaths
, auxpaths
);
291 if( ! mansearch(&search
, &paths
, argc
, argv
, &res
, &sz
))
292 usage(search
.argmode
);
296 if (search
.argmode
== ARG_NAME
)
297 fprintf(stderr
, "%s: No entry for %s "
298 "in the manual.\n", progname
, argv
[0]);
299 rc
= MANDOCLEVEL_BADARG
;
304 * For standard man(1) and -a output mode,
305 * prepare for copying filename pointers
306 * into the program parameter array.
309 if (outmode
== OUTMODE_ONE
) {
312 } else if (outmode
== OUTMODE_ALL
)
315 /* Iterate all matching manuals. */
317 for (i
= 0; i
< sz
; i
++) {
318 if (outmode
== OUTMODE_FLN
)
320 else if (outmode
== OUTMODE_LST
)
321 printf("%s - %s\n", res
[i
].names
,
322 res
[i
].output
== NULL
? "" :
324 else if (outmode
== OUTMODE_ONE
) {
325 /* Search for the best section. */
326 isec
= strcspn(res
[i
].file
, "123456789");
327 sec
= res
[i
].file
[isec
];
330 prio
= sec_prios
[sec
- '1'];
331 if (prio
>= best_prio
)
339 * For man(1), -a and -i output mode, fall through
340 * to the main mandoc(1) code iterating files
341 * and running the parsers on each of them.
344 if (outmode
== OUTMODE_FLN
|| outmode
== OUTMODE_LST
)
347 fputs("mandoc: database support not compiled in\n",
349 return((int)MANDOCLEVEL_BADARG
);
355 if ( ! moptions(&options
, auxpaths
))
356 return((int)MANDOCLEVEL_BADARG
);
358 if (use_pager
&& isatty(STDOUT_FILENO
))
361 curp
.mp
= mparse_alloc(options
, curp
.wlevel
, mmsg
, defos
);
364 * Conditionally start up the lookaside buffer before parsing.
366 if (OUTT_MAN
== curp
.outtype
)
367 mparse_keep(curp
.mp
);
370 parse(&curp
, STDIN_FILENO
, "<stdin>", &rc
);
375 rc
= mparse_open(curp
.mp
, &fd
, resp
->file
,
379 else if (resp
->form
& FORM_SRC
) {
380 /* For .so only; ignore failure. */
381 chdir(paths
.paths
[resp
->ipath
]);
382 parse(&curp
, fd
, resp
->file
, &rc
);
384 rc
= passthrough(resp
->file
, fd
);
389 rc
= mparse_open(curp
.mp
, &fd
, *argv
++,
392 parse(&curp
, fd
, argv
[-1], &rc
);
396 mparse_wait(curp
.mp
, child_pid
) != MANDOCLEVEL_OK
)
397 rc
= MANDOCLEVEL_SYSERR
;
399 if (MANDOCLEVEL_OK
!= rc
&& curp
.wstop
)
405 (*curp
.outfree
)(curp
.outdata
);
407 mparse_free(curp
.mp
);
411 if (search
.argmode
!= ARG_FILE
) {
412 manpath_free(&paths
);
413 mansearch_free(res
, sz
);
427 printf("mandoc %s\n", VERSION
);
428 exit((int)MANDOCLEVEL_OK
);
432 usage(enum argmode argmode
)
437 fputs("usage: mandoc [-acfhklV] [-Ios=name] "
438 "[-mformat] [-Ooption] [-Toutput] [-Wlevel]\n"
439 "\t [file ...]\n", stderr
);
442 fputs("usage: man [-acfhklVw] [-C file] "
443 "[-M path] [-m path] [-S arch] [-s section]\n"
444 "\t [section] name ...\n", stderr
);
447 fputs("usage: whatis [-acfhklVw] [-C file] "
448 "[-M path] [-m path] [-O outkey] [-S arch]\n"
449 "\t [-s section] name ...\n", stderr
);
452 fputs("usage: apropos [-acfhklVw] [-C file] "
453 "[-M path] [-m path] [-O outkey] [-S arch]\n"
454 "\t [-s section] expression ...\n", stderr
);
457 exit((int)MANDOCLEVEL_BADARG
);
461 parse(struct curparse
*curp
, int fd
, const char *file
,
462 enum mandoclevel
*level
)
468 /* Begin by parsing the file itself. */
473 rc
= mparse_readfd(curp
->mp
, fd
, file
);
475 /* Stop immediately if the parse has failed. */
477 if (MANDOCLEVEL_FATAL
<= rc
)
481 * With -Wstop and warnings or errors of at least the requested
482 * level, do not produce output.
485 if (MANDOCLEVEL_OK
!= rc
&& curp
->wstop
)
488 /* If unset, allocate output dev now (if applicable). */
490 if ( ! (curp
->outman
&& curp
->outmdoc
)) {
491 switch (curp
->outtype
) {
493 curp
->outdata
= xhtml_alloc(curp
->outopts
);
494 curp
->outfree
= html_free
;
497 curp
->outdata
= html_alloc(curp
->outopts
);
498 curp
->outfree
= html_free
;
501 curp
->outdata
= utf8_alloc(curp
->outopts
);
502 curp
->outfree
= ascii_free
;
505 curp
->outdata
= locale_alloc(curp
->outopts
);
506 curp
->outfree
= ascii_free
;
509 curp
->outdata
= ascii_alloc(curp
->outopts
);
510 curp
->outfree
= ascii_free
;
513 curp
->outdata
= pdf_alloc(curp
->outopts
);
514 curp
->outfree
= pspdf_free
;
517 curp
->outdata
= ps_alloc(curp
->outopts
);
518 curp
->outfree
= pspdf_free
;
524 switch (curp
->outtype
) {
528 curp
->outman
= html_man
;
529 curp
->outmdoc
= html_mdoc
;
532 curp
->outman
= tree_man
;
533 curp
->outmdoc
= tree_mdoc
;
536 curp
->outmdoc
= man_mdoc
;
537 curp
->outman
= man_man
;
548 curp
->outman
= terminal_man
;
549 curp
->outmdoc
= terminal_mdoc
;
556 mparse_result(curp
->mp
, &mdoc
, &man
, NULL
);
558 /* Execute the out device, if it exists. */
560 if (man
&& curp
->outman
)
561 (*curp
->outman
)(curp
->outdata
, man
);
562 if (mdoc
&& curp
->outmdoc
)
563 (*curp
->outmdoc
)(curp
->outdata
, mdoc
);
567 mparse_reset(curp
->mp
);
573 static enum mandoclevel
574 passthrough(const char *file
, int fd
)
580 while ((nr
= read(fd
, buf
, BUFSIZ
)) != -1 && nr
!= 0)
581 for (off
= 0; off
< nr
; off
+= nw
)
582 if ((nw
= write(STDOUT_FILENO
, buf
+ off
,
583 (size_t)(nr
- off
))) == -1 || nw
== 0) {
592 return(MANDOCLEVEL_OK
);
596 fprintf(stderr
, "%s: %s: SYSERR: %s: %s",
597 progname
, file
, syscall
, strerror(errno
));
598 return(MANDOCLEVEL_SYSERR
);
602 moptions(int *options
, char *arg
)
607 else if (0 == strcmp(arg
, "doc"))
608 *options
|= MPARSE_MDOC
;
609 else if (0 == strcmp(arg
, "andoc"))
611 else if (0 == strcmp(arg
, "an"))
612 *options
|= MPARSE_MAN
;
614 fprintf(stderr
, "%s: -m%s: Bad argument\n",
623 toptions(struct curparse
*curp
, char *arg
)
626 if (0 == strcmp(arg
, "ascii"))
627 curp
->outtype
= OUTT_ASCII
;
628 else if (0 == strcmp(arg
, "lint")) {
629 curp
->outtype
= OUTT_LINT
;
630 curp
->wlevel
= MANDOCLEVEL_WARNING
;
631 } else if (0 == strcmp(arg
, "tree"))
632 curp
->outtype
= OUTT_TREE
;
633 else if (0 == strcmp(arg
, "man"))
634 curp
->outtype
= OUTT_MAN
;
635 else if (0 == strcmp(arg
, "html"))
636 curp
->outtype
= OUTT_HTML
;
637 else if (0 == strcmp(arg
, "utf8"))
638 curp
->outtype
= OUTT_UTF8
;
639 else if (0 == strcmp(arg
, "locale"))
640 curp
->outtype
= OUTT_LOCALE
;
641 else if (0 == strcmp(arg
, "xhtml"))
642 curp
->outtype
= OUTT_XHTML
;
643 else if (0 == strcmp(arg
, "ps"))
644 curp
->outtype
= OUTT_PS
;
645 else if (0 == strcmp(arg
, "pdf"))
646 curp
->outtype
= OUTT_PDF
;
648 fprintf(stderr
, "%s: -T%s: Bad argument\n",
657 woptions(struct curparse
*curp
, char *arg
)
671 switch (getsubopt(&arg
, UNCONST(toks
), &v
)) {
678 curp
->wlevel
= MANDOCLEVEL_WARNING
;
681 curp
->wlevel
= MANDOCLEVEL_ERROR
;
684 curp
->wlevel
= MANDOCLEVEL_FATAL
;
687 fprintf(stderr
, "%s: -W%s: Bad argument\n",
697 mmsg(enum mandocerr t
, enum mandoclevel lvl
,
698 const char *file
, int line
, int col
, const char *msg
)
700 const char *mparse_msg
;
702 fprintf(stderr
, "%s: %s:", progname
, file
);
705 fprintf(stderr
, "%d:%d:", line
, col
+ 1);
707 fprintf(stderr
, " %s", mparse_strlevel(lvl
));
709 if (NULL
!= (mparse_msg
= mparse_strerror(t
)))
710 fprintf(stderr
, ": %s", mparse_msg
);
713 fprintf(stderr
, ": %s", msg
);
721 #define MAX_PAGER_ARGS 16
722 char *argv
[MAX_PAGER_ARGS
];
728 if (pipe(fildes
) == -1) {
729 fprintf(stderr
, "%s: pipe: %s\n",
730 progname
, strerror(errno
));
736 fprintf(stderr
, "%s: fork: %s\n",
737 progname
, strerror(errno
));
738 exit((int)MANDOCLEVEL_SYSERR
);
741 if (dup2(fildes
[1], STDOUT_FILENO
) == -1) {
742 fprintf(stderr
, "%s: dup output: %s\n",
743 progname
, strerror(errno
));
744 exit((int)MANDOCLEVEL_SYSERR
);
751 /* The original process becomes the pager. */
754 if (dup2(fildes
[0], STDIN_FILENO
) == -1) {
755 fprintf(stderr
, "%s: dup input: %s\n",
756 progname
, strerror(errno
));
757 exit((int)MANDOCLEVEL_SYSERR
);
760 pager
= getenv("MANPAGER");
761 if (pager
== NULL
|| *pager
== '\0')
762 pager
= getenv("PAGER");
763 if (pager
== NULL
|| *pager
== '\0')
764 pager
= "/usr/bin/more -s";
765 cp
= mandoc_strdup(pager
);
768 * Parse the pager command into words.
769 * Intentionally do not do anything fancy here.
773 while (argc
+ 1 < MAX_PAGER_ARGS
) {
775 cp
= strchr(cp
, ' ');
786 /* Hand over to the pager. */
788 execvp(argv
[0], argv
);
789 fprintf(stderr
, "%s: exec: %s\n",
790 progname
, strerror(errno
));
791 exit((int)MANDOCLEVEL_SYSERR
);