]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
1 /* $Id: main.c,v 1.188 2014/08/30 18:08:10 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:iklM: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
;
193 search
.argmode
= ARG_FILE
;
194 outmode
= OUTMODE_ALL
;
203 search
.outkey
= optarg
;
204 (void)strlcat(curp
.outopts
, optarg
, BUFSIZ
);
205 (void)strlcat(curp
.outopts
, ",", BUFSIZ
);
208 search
.arch
= optarg
;
214 if ( ! toptions(&curp
, optarg
))
215 return((int)MANDOCLEVEL_BADARG
);
218 if ( ! woptions(&curp
, optarg
))
219 return((int)MANDOCLEVEL_BADARG
);
222 outmode
= OUTMODE_FLN
;
234 usage(search
.argmode
);
236 /* Postprocess options. */
238 if (outmode
== OUTMODE_DEF
) {
239 switch (search
.argmode
) {
241 outmode
= OUTMODE_ALL
;
245 outmode
= OUTMODE_ONE
;
248 outmode
= OUTMODE_LST
;
253 /* Parse arguments. */
261 /* Quirk for a man(1) section argument without -s. */
263 if (search
.argmode
== ARG_NAME
&&
265 isdigit((unsigned char)argv
[0][0]) &&
266 (argv
[0][1] == '\0' || !strcmp(argv
[0], "3p"))) {
267 search
.sec
= argv
[0];
274 /* man(1), whatis(1), apropos(1) */
276 if (search
.argmode
!= ARG_FILE
) {
279 usage(search
.argmode
);
281 /* Access the mandoc database. */
283 manpath_parse(&paths
, conf_file
, defpaths
, auxpaths
);
285 if( ! mansearch(&search
, &paths
, argc
, argv
, &res
, &sz
))
286 usage(search
.argmode
);
287 manpath_free(&paths
);
291 if (search
.argmode
== ARG_NAME
)
292 fprintf(stderr
, "%s: No entry for %s "
293 "in the manual.\n", progname
, argv
[0]);
294 rc
= MANDOCLEVEL_BADARG
;
299 * For standard man(1) and -a output mode,
300 * prepare for copying filename pointers
301 * into the program parameter array.
304 if (outmode
== OUTMODE_ONE
) {
307 } else if (outmode
== OUTMODE_ALL
)
310 /* Iterate all matching manuals. */
312 for (i
= 0; i
< sz
; i
++) {
313 if (outmode
== OUTMODE_FLN
)
315 else if (outmode
== OUTMODE_LST
)
316 printf("%s - %s\n", res
[i
].names
,
317 res
[i
].output
== NULL
? "" :
319 else if (outmode
== OUTMODE_ONE
) {
320 /* Search for the best section. */
321 isec
= strcspn(res
[i
].file
, "123456789");
322 sec
= res
[i
].file
[isec
];
325 prio
= sec_prios
[sec
- '1'];
326 if (prio
>= best_prio
)
334 * For man(1), -a and -i output mode, fall through
335 * to the main mandoc(1) code iterating files
336 * and running the parsers on each of them.
339 if (outmode
== OUTMODE_FLN
|| outmode
== OUTMODE_LST
)
342 fputs("mandoc: database support not compiled in\n",
344 return((int)MANDOCLEVEL_BADARG
);
350 if ( ! moptions(&options
, auxpaths
))
351 return((int)MANDOCLEVEL_BADARG
);
353 if (use_pager
&& isatty(STDOUT_FILENO
))
356 curp
.mp
= mparse_alloc(options
, curp
.wlevel
, mmsg
, defos
);
359 * Conditionally start up the lookaside buffer before parsing.
361 if (OUTT_MAN
== curp
.outtype
)
362 mparse_keep(curp
.mp
);
365 parse(&curp
, STDIN_FILENO
, "<stdin>", &rc
);
371 parse(&curp
, -1, resp
->file
, &rc
);
373 rc
= passthrough(resp
->file
);
377 parse(&curp
, -1, *argv
++, &rc
);
378 if (MANDOCLEVEL_OK
!= rc
&& curp
.wstop
)
384 (*curp
.outfree
)(curp
.outdata
);
386 mparse_free(curp
.mp
);
390 if (search
.argmode
!= ARG_FILE
) {
391 mansearch_free(res
, sz
);
405 printf("mandoc %s\n", VERSION
);
406 exit((int)MANDOCLEVEL_OK
);
410 usage(enum argmode argmode
)
415 fputs("usage: mandoc [-acfklV] [-Ios=name] "
416 "[-mformat] [-Ooption] [-Toutput] [-Wlevel]\n"
417 "\t [file ...]\n", stderr
);
420 fputs("usage: man [-acfhklVw] [-C file] "
421 "[-M path] [-m path] [-S arch] [-s section]\n"
422 "\t [section] name ...\n", stderr
);
425 fputs("usage: whatis [-acfklVw] [-C file] "
426 "[-M path] [-m path] [-O outkey] [-S arch]\n"
427 "\t [-s section] name ...\n", stderr
);
430 fputs("usage: apropos [-acfklVw] [-C file] "
431 "[-M path] [-m path] [-O outkey] [-S arch]\n"
432 "\t [-s section] expression ...\n", stderr
);
435 exit((int)MANDOCLEVEL_BADARG
);
439 parse(struct curparse
*curp
, int fd
, const char *file
,
440 enum mandoclevel
*level
)
446 /* Begin by parsing the file itself. */
451 rc
= mparse_readfd(curp
->mp
, fd
, file
);
453 /* Stop immediately if the parse has failed. */
455 if (MANDOCLEVEL_FATAL
<= rc
)
459 * With -Wstop and warnings or errors of at least the requested
460 * level, do not produce output.
463 if (MANDOCLEVEL_OK
!= rc
&& curp
->wstop
)
466 /* If unset, allocate output dev now (if applicable). */
468 if ( ! (curp
->outman
&& curp
->outmdoc
)) {
469 switch (curp
->outtype
) {
471 curp
->outdata
= xhtml_alloc(curp
->outopts
);
472 curp
->outfree
= html_free
;
475 curp
->outdata
= html_alloc(curp
->outopts
);
476 curp
->outfree
= html_free
;
479 curp
->outdata
= utf8_alloc(curp
->outopts
);
480 curp
->outfree
= ascii_free
;
483 curp
->outdata
= locale_alloc(curp
->outopts
);
484 curp
->outfree
= ascii_free
;
487 curp
->outdata
= ascii_alloc(curp
->outopts
);
488 curp
->outfree
= ascii_free
;
491 curp
->outdata
= pdf_alloc(curp
->outopts
);
492 curp
->outfree
= pspdf_free
;
495 curp
->outdata
= ps_alloc(curp
->outopts
);
496 curp
->outfree
= pspdf_free
;
502 switch (curp
->outtype
) {
506 curp
->outman
= html_man
;
507 curp
->outmdoc
= html_mdoc
;
510 curp
->outman
= tree_man
;
511 curp
->outmdoc
= tree_mdoc
;
514 curp
->outmdoc
= man_mdoc
;
515 curp
->outman
= man_man
;
526 curp
->outman
= terminal_man
;
527 curp
->outmdoc
= terminal_mdoc
;
534 mparse_result(curp
->mp
, &mdoc
, &man
, NULL
);
536 /* Execute the out device, if it exists. */
538 if (man
&& curp
->outman
)
539 (*curp
->outman
)(curp
->outdata
, man
);
540 if (mdoc
&& curp
->outmdoc
)
541 (*curp
->outmdoc
)(curp
->outdata
, mdoc
);
545 mparse_reset(curp
->mp
);
551 static enum mandoclevel
552 passthrough(const char *file
)
559 fd
= open(file
, O_RDONLY
);
565 while ((nr
= read(fd
, buf
, BUFSIZ
)) != -1 && nr
!= 0)
566 for (off
= 0; off
< nr
; off
+= nw
)
567 if ((nw
= write(STDOUT_FILENO
, buf
+ off
,
568 (size_t)(nr
- off
))) == -1 || nw
== 0) {
575 return(MANDOCLEVEL_OK
);
580 fprintf(stderr
, "%s: %s: SYSERR: %s: %s",
581 progname
, file
, syscall
, strerror(errno
));
582 return(MANDOCLEVEL_SYSERR
);
586 moptions(int *options
, char *arg
)
591 else if (0 == strcmp(arg
, "doc"))
592 *options
|= MPARSE_MDOC
;
593 else if (0 == strcmp(arg
, "andoc"))
595 else if (0 == strcmp(arg
, "an"))
596 *options
|= MPARSE_MAN
;
598 fprintf(stderr
, "%s: -m%s: Bad argument\n",
607 toptions(struct curparse
*curp
, char *arg
)
610 if (0 == strcmp(arg
, "ascii"))
611 curp
->outtype
= OUTT_ASCII
;
612 else if (0 == strcmp(arg
, "lint")) {
613 curp
->outtype
= OUTT_LINT
;
614 curp
->wlevel
= MANDOCLEVEL_WARNING
;
615 } else if (0 == strcmp(arg
, "tree"))
616 curp
->outtype
= OUTT_TREE
;
617 else if (0 == strcmp(arg
, "man"))
618 curp
->outtype
= OUTT_MAN
;
619 else if (0 == strcmp(arg
, "html"))
620 curp
->outtype
= OUTT_HTML
;
621 else if (0 == strcmp(arg
, "utf8"))
622 curp
->outtype
= OUTT_UTF8
;
623 else if (0 == strcmp(arg
, "locale"))
624 curp
->outtype
= OUTT_LOCALE
;
625 else if (0 == strcmp(arg
, "xhtml"))
626 curp
->outtype
= OUTT_XHTML
;
627 else if (0 == strcmp(arg
, "ps"))
628 curp
->outtype
= OUTT_PS
;
629 else if (0 == strcmp(arg
, "pdf"))
630 curp
->outtype
= OUTT_PDF
;
632 fprintf(stderr
, "%s: -T%s: Bad argument\n",
641 woptions(struct curparse
*curp
, char *arg
)
655 switch (getsubopt(&arg
, UNCONST(toks
), &v
)) {
662 curp
->wlevel
= MANDOCLEVEL_WARNING
;
665 curp
->wlevel
= MANDOCLEVEL_ERROR
;
668 curp
->wlevel
= MANDOCLEVEL_FATAL
;
671 fprintf(stderr
, "%s: -W%s: Bad argument\n",
681 mmsg(enum mandocerr t
, enum mandoclevel lvl
,
682 const char *file
, int line
, int col
, const char *msg
)
684 const char *mparse_msg
;
686 fprintf(stderr
, "%s: %s:", progname
, file
);
689 fprintf(stderr
, "%d:%d:", line
, col
+ 1);
691 fprintf(stderr
, " %s", mparse_strlevel(lvl
));
693 if (NULL
!= (mparse_msg
= mparse_strerror(t
)))
694 fprintf(stderr
, ": %s", mparse_msg
);
697 fprintf(stderr
, ": %s", msg
);
705 #define MAX_PAGER_ARGS 16
706 char *argv
[MAX_PAGER_ARGS
];
712 if (pipe(fildes
) == -1) {
713 fprintf(stderr
, "%s: pipe: %s\n",
714 progname
, strerror(errno
));
720 fprintf(stderr
, "%s: fork: %s\n",
721 progname
, strerror(errno
));
722 exit((int)MANDOCLEVEL_SYSERR
);
725 if (dup2(fildes
[1], STDOUT_FILENO
) == -1) {
726 fprintf(stderr
, "%s: dup output: %s\n",
727 progname
, strerror(errno
));
728 exit((int)MANDOCLEVEL_SYSERR
);
735 /* The original process becomes the pager. */
738 if (dup2(fildes
[0], STDIN_FILENO
) == -1) {
739 fprintf(stderr
, "%s: dup input: %s\n",
740 progname
, strerror(errno
));
741 exit((int)MANDOCLEVEL_SYSERR
);
744 pager
= getenv("MANPAGER");
745 if (pager
== NULL
|| *pager
== '\0')
746 pager
= getenv("PAGER");
747 if (pager
== NULL
|| *pager
== '\0')
748 pager
= "/usr/bin/more -s";
749 cp
= mandoc_strdup(pager
);
752 * Parse the pager command into words.
753 * Intentionally do not do anything fancy here.
757 while (argc
+ 1 < MAX_PAGER_ARGS
) {
759 cp
= strchr(cp
, ' ');
770 /* Hand over to the pager. */
772 execvp(argv
[0], argv
);
773 fprintf(stderr
, "%s: exec: %s\n",
774 progname
, strerror(errno
));
775 exit((int)MANDOCLEVEL_SYSERR
);