]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
1 /* $Id: main.c,v 1.201 2014/12/02 11:31:51 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_LINT
, /* -Tlint */
74 struct mchars
*mchars
; /* character table */
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, 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_LOCALE
;
152 curp
.wlevel
= MANDOCLEVEL_FATAL
;
153 options
= MPARSE_SO
| MPARSE_UTF8
| MPARSE_LATIN1
;
159 outmode
= OUTMODE_DEF
;
161 while (-1 != (c
= getopt(argc
, argv
,
162 "aC:cfhI:iK:klM:m:O:S:s:T:VW:w"))) {
165 outmode
= OUTMODE_ALL
;
174 search
.argmode
= ARG_WORD
;
177 (void)strlcat(curp
.outopts
, "synopsis,", BUFSIZ
);
180 outmode
= OUTMODE_ALL
;
183 if (strncmp(optarg
, "os=", 3)) {
185 "%s: -I%s: Bad argument\n",
187 return((int)MANDOCLEVEL_BADARG
);
191 "%s: -I%s: Duplicate argument\n",
193 return((int)MANDOCLEVEL_BADARG
);
195 defos
= mandoc_strdup(optarg
+ 3);
198 outmode
= OUTMODE_INT
;
201 if ( ! koptions(&options
, optarg
))
202 return((int)MANDOCLEVEL_BADARG
);
205 search
.argmode
= ARG_EXPR
;
208 search
.argmode
= ARG_FILE
;
209 outmode
= OUTMODE_ALL
;
218 search
.outkey
= optarg
;
219 (void)strlcat(curp
.outopts
, optarg
, BUFSIZ
);
220 (void)strlcat(curp
.outopts
, ",", BUFSIZ
);
223 search
.arch
= optarg
;
229 if ( ! toptions(&curp
, optarg
))
230 return((int)MANDOCLEVEL_BADARG
);
233 if ( ! woptions(&curp
, optarg
))
234 return((int)MANDOCLEVEL_BADARG
);
237 outmode
= OUTMODE_FLN
;
249 usage(search
.argmode
);
251 /* Postprocess options. */
253 if (outmode
== OUTMODE_DEF
) {
254 switch (search
.argmode
) {
256 outmode
= OUTMODE_ALL
;
260 outmode
= OUTMODE_ONE
;
263 outmode
= OUTMODE_LST
;
268 /* Parse arguments. */
276 /* Quirk for a man(1) section argument without -s. */
278 if (search
.argmode
== ARG_NAME
&&
280 isdigit((unsigned char)argv
[0][0]) &&
281 (argv
[0][1] == '\0' || !strcmp(argv
[0], "3p"))) {
282 search
.sec
= argv
[0];
289 /* man(1), whatis(1), apropos(1) */
291 if (search
.argmode
!= ARG_FILE
) {
294 usage(search
.argmode
);
296 if (search
.argmode
== ARG_NAME
&&
297 outmode
== OUTMODE_ONE
)
298 search
.firstmatch
= 1;
300 /* Access the mandoc database. */
302 manpath_parse(&paths
, conf_file
, defpaths
, auxpaths
);
304 if( ! mansearch(&search
, &paths
, argc
, argv
, &res
, &sz
))
305 usage(search
.argmode
);
309 if (search
.argmode
== ARG_NAME
)
310 fprintf(stderr
, "%s: No entry for %s "
311 "in the manual.\n", progname
, argv
[0]);
312 rc
= MANDOCLEVEL_BADARG
;
317 * For standard man(1) and -a output mode,
318 * prepare for copying filename pointers
319 * into the program parameter array.
322 if (outmode
== OUTMODE_ONE
) {
325 } else if (outmode
== OUTMODE_ALL
)
328 /* Iterate all matching manuals. */
330 for (i
= 0; i
< sz
; i
++) {
331 if (outmode
== OUTMODE_FLN
)
333 else if (outmode
== OUTMODE_LST
)
334 printf("%s - %s\n", res
[i
].names
,
335 res
[i
].output
== NULL
? "" :
337 else if (outmode
== OUTMODE_ONE
) {
338 /* Search for the best section. */
339 isec
= strcspn(res
[i
].file
, "123456789");
340 sec
= res
[i
].file
[isec
];
343 prio
= sec_prios
[sec
- '1'];
344 if (prio
>= best_prio
)
352 * For man(1), -a and -i output mode, fall through
353 * to the main mandoc(1) code iterating files
354 * and running the parsers on each of them.
357 if (outmode
== OUTMODE_FLN
|| outmode
== OUTMODE_LST
)
360 fputs("mandoc: database support not compiled in\n",
362 return((int)MANDOCLEVEL_BADARG
);
368 if ( ! moptions(&options
, auxpaths
))
369 return((int)MANDOCLEVEL_BADARG
);
371 if (use_pager
&& isatty(STDOUT_FILENO
))
374 curp
.mchars
= mchars_alloc();
375 curp
.mp
= mparse_alloc(options
, curp
.wlevel
, mmsg
,
379 * Conditionally start up the lookaside buffer before parsing.
381 if (OUTT_MAN
== curp
.outtype
)
382 mparse_keep(curp
.mp
);
385 parse(&curp
, STDIN_FILENO
, "<stdin>", &rc
);
390 rc
= mparse_open(curp
.mp
, &fd
, resp
->file
);
393 else if (resp
->form
& FORM_SRC
) {
394 /* For .so only; ignore failure. */
395 chdir(paths
.paths
[resp
->ipath
]);
396 parse(&curp
, fd
, resp
->file
, &rc
);
398 rc
= passthrough(resp
->file
, fd
,
404 rc
= mparse_open(curp
.mp
, &fd
, *argv
++);
406 parse(&curp
, fd
, argv
[-1], &rc
);
409 if (mparse_wait(curp
.mp
) != MANDOCLEVEL_OK
)
410 rc
= MANDOCLEVEL_SYSERR
;
412 if (MANDOCLEVEL_OK
!= rc
&& curp
.wstop
)
418 (*curp
.outfree
)(curp
.outdata
);
419 mparse_free(curp
.mp
);
420 mchars_free(curp
.mchars
);
424 if (search
.argmode
!= ARG_FILE
) {
425 manpath_free(&paths
);
426 mansearch_free(res
, sz
);
440 printf("mandoc %s\n", VERSION
);
441 exit((int)MANDOCLEVEL_OK
);
445 usage(enum argmode argmode
)
450 fputs("usage: mandoc [-acfhklV] [-Ios=name] "
451 "[-Kencoding] [-mformat] [-Ooption]\n"
452 "\t [-Toutput] [-Wlevel] [file ...]\n", stderr
);
455 fputs("usage: man [-acfhklVw] [-C file] "
456 "[-M path] [-m path] [-S arch] [-s section]\n"
457 "\t [section] name ...\n", stderr
);
460 fputs("usage: whatis [-acfhklVw] [-C file] "
461 "[-M path] [-m path] [-O outkey] [-S arch]\n"
462 "\t [-s section] name ...\n", stderr
);
465 fputs("usage: apropos [-acfhklVw] [-C file] "
466 "[-M path] [-m path] [-O outkey] [-S arch]\n"
467 "\t [-s section] expression ...\n", stderr
);
470 exit((int)MANDOCLEVEL_BADARG
);
474 parse(struct curparse
*curp
, int fd
, const char *file
,
475 enum mandoclevel
*level
)
481 /* Begin by parsing the file itself. */
486 rc
= mparse_readfd(curp
->mp
, fd
, file
);
488 /* Stop immediately if the parse has failed. */
490 if (MANDOCLEVEL_FATAL
<= rc
)
494 * With -Wstop and warnings or errors of at least the requested
495 * level, do not produce output.
498 if (MANDOCLEVEL_OK
!= rc
&& curp
->wstop
)
501 /* If unset, allocate output dev now (if applicable). */
503 if ( ! (curp
->outman
&& curp
->outmdoc
)) {
504 switch (curp
->outtype
) {
506 curp
->outdata
= html_alloc(curp
->mchars
,
508 curp
->outfree
= html_free
;
511 curp
->outdata
= utf8_alloc(curp
->mchars
,
513 curp
->outfree
= ascii_free
;
516 curp
->outdata
= locale_alloc(curp
->mchars
,
518 curp
->outfree
= ascii_free
;
521 curp
->outdata
= ascii_alloc(curp
->mchars
,
523 curp
->outfree
= ascii_free
;
526 curp
->outdata
= pdf_alloc(curp
->mchars
,
528 curp
->outfree
= pspdf_free
;
531 curp
->outdata
= ps_alloc(curp
->mchars
,
533 curp
->outfree
= pspdf_free
;
539 switch (curp
->outtype
) {
541 curp
->outman
= html_man
;
542 curp
->outmdoc
= html_mdoc
;
545 curp
->outman
= tree_man
;
546 curp
->outmdoc
= tree_mdoc
;
549 curp
->outmdoc
= man_mdoc
;
550 curp
->outman
= man_man
;
561 curp
->outman
= terminal_man
;
562 curp
->outmdoc
= terminal_mdoc
;
569 mparse_result(curp
->mp
, &mdoc
, &man
, NULL
);
571 /* Execute the out device, if it exists. */
573 if (man
&& curp
->outman
)
574 (*curp
->outman
)(curp
->outdata
, man
);
575 if (mdoc
&& curp
->outmdoc
)
576 (*curp
->outmdoc
)(curp
->outdata
, mdoc
);
580 mparse_reset(curp
->mp
);
586 static enum mandoclevel
587 passthrough(const char *file
, int fd
, int synopsis_only
)
589 const char synb
[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
590 const char synr
[] = "SYNOPSIS";
599 if ((stream
= fdopen(fd
, "r")) == NULL
) {
606 while ((line
= fgetln(stream
, &len
)) != NULL
) {
609 if ( ! isspace((unsigned char)*line
))
612 isspace((unsigned char)*line
)) {
617 if ((len
== sizeof(synb
) &&
618 ! strncmp(line
, synb
, len
- 1)) ||
619 (len
== sizeof(synr
) &&
620 ! strncmp(line
, synr
, len
- 1)))
625 for (off
= 0; off
< len
; off
+= nw
)
626 if ((nw
= write(STDOUT_FILENO
, line
+ off
,
627 len
- off
)) == -1 || nw
== 0) {
634 if (ferror(stream
)) {
642 return(MANDOCLEVEL_OK
);
645 fprintf(stderr
, "%s: %s: SYSERR: %s: %s",
646 progname
, file
, syscall
, strerror(errno
));
647 return(MANDOCLEVEL_SYSERR
);
651 koptions(int *options
, char *arg
)
654 if ( ! strcmp(arg
, "utf-8")) {
655 *options
|= MPARSE_UTF8
;
656 *options
&= ~MPARSE_LATIN1
;
657 } else if ( ! strcmp(arg
, "iso-8859-1")) {
658 *options
|= MPARSE_LATIN1
;
659 *options
&= ~MPARSE_UTF8
;
660 } else if ( ! strcmp(arg
, "us-ascii")) {
661 *options
&= ~(MPARSE_UTF8
| MPARSE_LATIN1
);
663 fprintf(stderr
, "%s: -K%s: Bad argument\n",
671 moptions(int *options
, char *arg
)
676 else if (0 == strcmp(arg
, "doc"))
677 *options
|= MPARSE_MDOC
;
678 else if (0 == strcmp(arg
, "andoc"))
680 else if (0 == strcmp(arg
, "an"))
681 *options
|= MPARSE_MAN
;
683 fprintf(stderr
, "%s: -m%s: Bad argument\n",
692 toptions(struct curparse
*curp
, char *arg
)
695 if (0 == strcmp(arg
, "ascii"))
696 curp
->outtype
= OUTT_ASCII
;
697 else if (0 == strcmp(arg
, "lint")) {
698 curp
->outtype
= OUTT_LINT
;
699 curp
->wlevel
= MANDOCLEVEL_WARNING
;
700 } else if (0 == strcmp(arg
, "tree"))
701 curp
->outtype
= OUTT_TREE
;
702 else if (0 == strcmp(arg
, "man"))
703 curp
->outtype
= OUTT_MAN
;
704 else if (0 == strcmp(arg
, "html"))
705 curp
->outtype
= OUTT_HTML
;
706 else if (0 == strcmp(arg
, "utf8"))
707 curp
->outtype
= OUTT_UTF8
;
708 else if (0 == strcmp(arg
, "locale"))
709 curp
->outtype
= OUTT_LOCALE
;
710 else if (0 == strcmp(arg
, "xhtml"))
711 curp
->outtype
= OUTT_HTML
;
712 else if (0 == strcmp(arg
, "ps"))
713 curp
->outtype
= OUTT_PS
;
714 else if (0 == strcmp(arg
, "pdf"))
715 curp
->outtype
= OUTT_PDF
;
717 fprintf(stderr
, "%s: -T%s: Bad argument\n",
726 woptions(struct curparse
*curp
, char *arg
)
740 switch (getsubopt(&arg
, UNCONST(toks
), &v
)) {
747 curp
->wlevel
= MANDOCLEVEL_WARNING
;
750 curp
->wlevel
= MANDOCLEVEL_ERROR
;
753 curp
->wlevel
= MANDOCLEVEL_FATAL
;
756 fprintf(stderr
, "%s: -W%s: Bad argument\n",
766 mmsg(enum mandocerr t
, enum mandoclevel lvl
,
767 const char *file
, int line
, int col
, const char *msg
)
769 const char *mparse_msg
;
771 fprintf(stderr
, "%s: %s:", progname
, file
);
774 fprintf(stderr
, "%d:%d:", line
, col
+ 1);
776 fprintf(stderr
, " %s", mparse_strlevel(lvl
));
778 if (NULL
!= (mparse_msg
= mparse_strerror(t
)))
779 fprintf(stderr
, ": %s", mparse_msg
);
782 fprintf(stderr
, ": %s", msg
);
790 #define MAX_PAGER_ARGS 16
791 char *argv
[MAX_PAGER_ARGS
];
797 if (pipe(fildes
) == -1) {
798 fprintf(stderr
, "%s: pipe: %s\n",
799 progname
, strerror(errno
));
805 fprintf(stderr
, "%s: fork: %s\n",
806 progname
, strerror(errno
));
807 exit((int)MANDOCLEVEL_SYSERR
);
810 if (dup2(fildes
[1], STDOUT_FILENO
) == -1) {
811 fprintf(stderr
, "%s: dup output: %s\n",
812 progname
, strerror(errno
));
813 exit((int)MANDOCLEVEL_SYSERR
);
820 /* The original process becomes the pager. */
823 if (dup2(fildes
[0], STDIN_FILENO
) == -1) {
824 fprintf(stderr
, "%s: dup input: %s\n",
825 progname
, strerror(errno
));
826 exit((int)MANDOCLEVEL_SYSERR
);
829 pager
= getenv("MANPAGER");
830 if (pager
== NULL
|| *pager
== '\0')
831 pager
= getenv("PAGER");
832 if (pager
== NULL
|| *pager
== '\0')
833 pager
= "/usr/bin/more -s";
834 cp
= mandoc_strdup(pager
);
837 * Parse the pager command into words.
838 * Intentionally do not do anything fancy here.
842 while (argc
+ 1 < MAX_PAGER_ARGS
) {
844 cp
= strchr(cp
, ' ');
855 /* Hand over to the pager. */
857 execvp(argv
[0], argv
);
858 fprintf(stderr
, "%s: exec: %s\n",
859 progname
, strerror(errno
));
860 exit((int)MANDOCLEVEL_SYSERR
);