]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
e949afd38523c3309219685199927cb294879885
1 /* $Id: main.c,v 1.185 2014/08/22 18:07:15 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>
33 #include "mandoc_aux.h"
38 #include "mansearch.h"
40 #if !defined(__GNUC__) || (__GNUC__ < 2)
42 # define __attribute__(x)
44 #endif /* !defined(__GNUC__) || (__GNUC__ < 2) */
55 typedef void (*out_mdoc
)(void *, const struct mdoc
*);
56 typedef void (*out_man
)(void *, const struct man
*);
57 typedef void (*out_free
)(void *);
60 OUTT_ASCII
= 0, /* -Tascii */
61 OUTT_LOCALE
, /* -Tlocale */
62 OUTT_UTF8
, /* -Tutf8 */
63 OUTT_TREE
, /* -Ttree */
65 OUTT_HTML
, /* -Thtml */
66 OUTT_XHTML
, /* -Txhtml */
67 OUTT_LINT
, /* -Tlint */
74 enum mandoclevel wlevel
; /* ignore messages below this */
75 int wstop
; /* stop after a file with a warning */
76 enum outt outtype
; /* which output to use */
77 out_mdoc outmdoc
; /* mdoc output ptr */
78 out_man outman
; /* man output ptr */
79 out_free outfree
; /* free output ptr */
80 void *outdata
; /* data for output */
81 char outopts
[BUFSIZ
]; /* buf of output opts */
84 static int moptions(int *, char *);
85 static void mmsg(enum mandocerr
, enum mandoclevel
,
86 const char *, int, int, const char *);
87 static void parse(struct curparse
*, int,
88 const char *, enum mandoclevel
*);
89 static void spawn_pager(void);
90 static int toptions(struct curparse
*, char *);
91 static void usage(enum argmode
) __attribute__((noreturn
));
92 static void version(void) __attribute__((noreturn
));
93 static int woptions(struct curparse
*, char *);
95 static const int sec_prios
[] = {1, 4, 5, 8, 6, 3, 7, 2, 9};
96 static const char *progname
;
100 main(int argc
, char *argv
[])
102 struct curparse curp
;
103 struct mansearch search
;
104 struct manpaths paths
;
105 char *conf_file
, *defpaths
, *auxpaths
;
115 enum outmode outmode
;
121 progname
= strrchr(argv
[0], '/');
122 if (progname
== NULL
)
127 /* Search options. */
129 memset(&paths
, 0, sizeof(struct manpaths
));
130 conf_file
= defpaths
= auxpaths
= NULL
;
132 memset(&search
, 0, sizeof(struct mansearch
));
133 search
.outkey
= "Nd";
135 if (strcmp(progname
, "man") == 0)
136 search
.argmode
= ARG_NAME
;
137 else if (strncmp(progname
, "apropos", 7) == 0)
138 search
.argmode
= ARG_EXPR
;
139 else if (strncmp(progname
, "whatis", 6) == 0)
140 search
.argmode
= ARG_WORD
;
142 search
.argmode
= ARG_FILE
;
144 /* Parser and formatter options. */
146 memset(&curp
, 0, sizeof(struct curparse
));
147 curp
.outtype
= OUTT_ASCII
;
148 curp
.wlevel
= MANDOCLEVEL_FATAL
;
154 outmode
= OUTMODE_DEF
;
156 while (-1 != (c
= getopt(argc
, argv
, "aC:cfI:ikM:m:O:S:s:T:VW:w"))) {
159 outmode
= OUTMODE_ALL
;
168 search
.argmode
= ARG_WORD
;
171 if (strncmp(optarg
, "os=", 3)) {
173 "%s: -I%s: Bad argument\n",
175 return((int)MANDOCLEVEL_BADARG
);
179 "%s: -I%s: Duplicate argument\n",
181 return((int)MANDOCLEVEL_BADARG
);
183 defos
= mandoc_strdup(optarg
+ 3);
186 outmode
= OUTMODE_INT
;
189 search
.argmode
= ARG_EXPR
;
198 search
.outkey
= optarg
;
199 (void)strlcat(curp
.outopts
, optarg
, BUFSIZ
);
200 (void)strlcat(curp
.outopts
, ",", BUFSIZ
);
203 search
.arch
= optarg
;
209 if ( ! toptions(&curp
, optarg
))
210 return((int)MANDOCLEVEL_BADARG
);
213 if ( ! woptions(&curp
, optarg
))
214 return((int)MANDOCLEVEL_BADARG
);
217 outmode
= OUTMODE_FLN
;
229 usage(search
.argmode
);
231 /* Postprocess options. */
233 if (outmode
== OUTMODE_DEF
) {
234 switch (search
.argmode
) {
236 outmode
= OUTMODE_ALL
;
240 outmode
= OUTMODE_ONE
;
243 outmode
= OUTMODE_LST
;
248 /* Parse arguments. */
256 /* Quirk for a man(1) section argument without -s. */
258 if (search
.argmode
== ARG_NAME
&&
260 isdigit((unsigned char)argv
[0][0]) &&
261 (argv
[0][1] == '\0' || !strcmp(argv
[0], "3p"))) {
262 search
.sec
= argv
[0];
269 /* man(1), whatis(1), apropos(1) */
271 if (search
.argmode
!= ARG_FILE
) {
274 usage(search
.argmode
);
276 /* Access the mandoc database. */
278 manpath_parse(&paths
, conf_file
, defpaths
, auxpaths
);
280 if( ! mansearch(&search
, &paths
, argc
, argv
, &res
, &sz
))
281 usage(search
.argmode
);
282 manpath_free(&paths
);
285 * For standard man(1) and -a output mode,
286 * prepare for copying filename pointers
287 * into the program parameter array.
290 if (outmode
== OUTMODE_ONE
) {
292 argv
[0] = res
[0].file
;
295 } else if (outmode
== OUTMODE_ALL
) {
297 argv
= auxargv
= mandoc_reallocarray(
298 NULL
, sz
+ 1, sizeof(char *));
302 /* Iterate all matching manuals. */
304 for (i
= 0; i
< sz
; i
++) {
305 if (outmode
== OUTMODE_FLN
)
307 else if (outmode
== OUTMODE_LST
)
308 printf("%s - %s\n", res
[i
].names
,
309 res
[i
].output
== NULL
? "" :
311 else if (outmode
== OUTMODE_ALL
)
312 argv
[i
] = res
[i
].file
;
314 /* Search for the best section. */
315 isec
= strcspn(res
[i
].file
, "123456789");
316 sec
= res
[i
].file
[isec
];
319 prio
= sec_prios
[sec
- '1'];
320 if (prio
>= best_prio
)
323 argv
[0] = res
[i
].file
;
328 * For man(1), -a and -i output mode, fall through
329 * to the main mandoc(1) code iterating files
330 * and running the parsers on each of them.
333 if (outmode
== OUTMODE_FLN
|| outmode
== OUTMODE_LST
)
336 fputs("mandoc: database support not compiled in\n",
338 return((int)MANDOCLEVEL_BADARG
);
344 if ( ! moptions(&options
, auxpaths
))
345 return((int)MANDOCLEVEL_BADARG
);
347 if (use_pager
&& isatty(STDOUT_FILENO
))
350 curp
.mp
= mparse_alloc(options
, curp
.wlevel
, mmsg
, defos
);
353 * Conditionally start up the lookaside buffer before parsing.
355 if (OUTT_MAN
== curp
.outtype
)
356 mparse_keep(curp
.mp
);
359 parse(&curp
, STDIN_FILENO
, "<stdin>", &rc
);
362 parse(&curp
, -1, *argv
, &rc
);
363 if (MANDOCLEVEL_OK
!= rc
&& curp
.wstop
)
369 (*curp
.outfree
)(curp
.outdata
);
371 mparse_free(curp
.mp
);
375 if (search
.argmode
!= ARG_FILE
) {
376 mansearch_free(res
, sz
);
391 printf("mandoc %s\n", VERSION
);
392 exit((int)MANDOCLEVEL_OK
);
396 usage(enum argmode argmode
)
401 fputs("usage: mandoc [-V] [-Ios=name] [-mformat]"
402 " [-Ooption] [-Toutput] [-Wlevel]\n"
403 "\t [file ...]\n", stderr
);
406 fputs("usage: man [-acfhkVw] [-C file] "
407 "[-M path] [-m path] [-S arch] [-s section]\n"
408 "\t [section] name ...\n", stderr
);
411 fputs("usage: whatis [-V] [-C file] [-M path] [-m path] "
412 "[-S arch] [-s section] name ...\n", stderr
);
415 fputs("usage: apropos [-V] [-C file] [-M path] [-m path] "
416 "[-O outkey] [-S arch]\n"
417 "\t [-s section] expression ...\n", stderr
);
420 exit((int)MANDOCLEVEL_BADARG
);
424 parse(struct curparse
*curp
, int fd
, const char *file
,
425 enum mandoclevel
*level
)
431 /* Begin by parsing the file itself. */
436 rc
= mparse_readfd(curp
->mp
, fd
, file
);
438 /* Stop immediately if the parse has failed. */
440 if (MANDOCLEVEL_FATAL
<= rc
)
444 * With -Wstop and warnings or errors of at least the requested
445 * level, do not produce output.
448 if (MANDOCLEVEL_OK
!= rc
&& curp
->wstop
)
451 /* If unset, allocate output dev now (if applicable). */
453 if ( ! (curp
->outman
&& curp
->outmdoc
)) {
454 switch (curp
->outtype
) {
456 curp
->outdata
= xhtml_alloc(curp
->outopts
);
457 curp
->outfree
= html_free
;
460 curp
->outdata
= html_alloc(curp
->outopts
);
461 curp
->outfree
= html_free
;
464 curp
->outdata
= utf8_alloc(curp
->outopts
);
465 curp
->outfree
= ascii_free
;
468 curp
->outdata
= locale_alloc(curp
->outopts
);
469 curp
->outfree
= ascii_free
;
472 curp
->outdata
= ascii_alloc(curp
->outopts
);
473 curp
->outfree
= ascii_free
;
476 curp
->outdata
= pdf_alloc(curp
->outopts
);
477 curp
->outfree
= pspdf_free
;
480 curp
->outdata
= ps_alloc(curp
->outopts
);
481 curp
->outfree
= pspdf_free
;
487 switch (curp
->outtype
) {
491 curp
->outman
= html_man
;
492 curp
->outmdoc
= html_mdoc
;
495 curp
->outman
= tree_man
;
496 curp
->outmdoc
= tree_mdoc
;
499 curp
->outmdoc
= man_mdoc
;
500 curp
->outman
= man_man
;
511 curp
->outman
= terminal_man
;
512 curp
->outmdoc
= terminal_mdoc
;
519 mparse_result(curp
->mp
, &mdoc
, &man
, NULL
);
521 /* Execute the out device, if it exists. */
523 if (man
&& curp
->outman
)
524 (*curp
->outman
)(curp
->outdata
, man
);
525 if (mdoc
&& curp
->outmdoc
)
526 (*curp
->outmdoc
)(curp
->outdata
, mdoc
);
530 mparse_reset(curp
->mp
);
537 moptions(int *options
, char *arg
)
542 else if (0 == strcmp(arg
, "doc"))
543 *options
|= MPARSE_MDOC
;
544 else if (0 == strcmp(arg
, "andoc"))
546 else if (0 == strcmp(arg
, "an"))
547 *options
|= MPARSE_MAN
;
549 fprintf(stderr
, "%s: -m%s: Bad argument\n",
558 toptions(struct curparse
*curp
, char *arg
)
561 if (0 == strcmp(arg
, "ascii"))
562 curp
->outtype
= OUTT_ASCII
;
563 else if (0 == strcmp(arg
, "lint")) {
564 curp
->outtype
= OUTT_LINT
;
565 curp
->wlevel
= MANDOCLEVEL_WARNING
;
566 } else if (0 == strcmp(arg
, "tree"))
567 curp
->outtype
= OUTT_TREE
;
568 else if (0 == strcmp(arg
, "man"))
569 curp
->outtype
= OUTT_MAN
;
570 else if (0 == strcmp(arg
, "html"))
571 curp
->outtype
= OUTT_HTML
;
572 else if (0 == strcmp(arg
, "utf8"))
573 curp
->outtype
= OUTT_UTF8
;
574 else if (0 == strcmp(arg
, "locale"))
575 curp
->outtype
= OUTT_LOCALE
;
576 else if (0 == strcmp(arg
, "xhtml"))
577 curp
->outtype
= OUTT_XHTML
;
578 else if (0 == strcmp(arg
, "ps"))
579 curp
->outtype
= OUTT_PS
;
580 else if (0 == strcmp(arg
, "pdf"))
581 curp
->outtype
= OUTT_PDF
;
583 fprintf(stderr
, "%s: -T%s: Bad argument\n",
592 woptions(struct curparse
*curp
, char *arg
)
606 switch (getsubopt(&arg
, UNCONST(toks
), &v
)) {
613 curp
->wlevel
= MANDOCLEVEL_WARNING
;
616 curp
->wlevel
= MANDOCLEVEL_ERROR
;
619 curp
->wlevel
= MANDOCLEVEL_FATAL
;
622 fprintf(stderr
, "%s: -W%s: Bad argument\n",
632 mmsg(enum mandocerr t
, enum mandoclevel lvl
,
633 const char *file
, int line
, int col
, const char *msg
)
635 const char *mparse_msg
;
637 fprintf(stderr
, "%s: %s:", progname
, file
);
640 fprintf(stderr
, "%d:%d:", line
, col
+ 1);
642 fprintf(stderr
, " %s", mparse_strlevel(lvl
));
644 if (NULL
!= (mparse_msg
= mparse_strerror(t
)))
645 fprintf(stderr
, ": %s", mparse_msg
);
648 fprintf(stderr
, ": %s", msg
);
656 #define MAX_PAGER_ARGS 16
657 char *argv
[MAX_PAGER_ARGS
];
663 if (pipe(fildes
) == -1) {
664 fprintf(stderr
, "%s: pipe: %s\n",
665 progname
, strerror(errno
));
671 fprintf(stderr
, "%s: fork: %s\n",
672 progname
, strerror(errno
));
673 exit((int)MANDOCLEVEL_SYSERR
);
676 if (dup2(fildes
[1], STDOUT_FILENO
) == -1) {
677 fprintf(stderr
, "%s: dup output: %s\n",
678 progname
, strerror(errno
));
679 exit((int)MANDOCLEVEL_SYSERR
);
686 /* The original process becomes the pager. */
689 if (dup2(fildes
[0], STDIN_FILENO
) == -1) {
690 fprintf(stderr
, "%s: dup input: %s\n",
691 progname
, strerror(errno
));
692 exit((int)MANDOCLEVEL_SYSERR
);
695 pager
= getenv("MANPAGER");
696 if (pager
== NULL
|| *pager
== '\0')
697 pager
= getenv("PAGER");
698 if (pager
== NULL
|| *pager
== '\0')
699 pager
= "/usr/bin/more -s";
700 cp
= mandoc_strdup(pager
);
703 * Parse the pager command into words.
704 * Intentionally do not do anything fancy here.
708 while (argc
+ 1 < MAX_PAGER_ARGS
) {
710 cp
= strchr(cp
, ' ');
721 /* Hand over to the pager. */
723 execvp(argv
[0], argv
);
724 fprintf(stderr
, "%s: exec: %s\n",
725 progname
, strerror(errno
));
726 exit((int)MANDOCLEVEL_SYSERR
);