]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
1 /* $Id: main.c,v 1.217 2015/01/20 21:16:51 schwarze Exp $ */
3 * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010-2012, 2014, 2015 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>
22 #include <sys/param.h> /* MACHINE */
35 #include "mandoc_aux.h"
40 #include "mansearch.h"
42 #if !defined(__GNUC__) || (__GNUC__ < 2)
44 # define __attribute__(x)
46 #endif /* !defined(__GNUC__) || (__GNUC__ < 2) */
57 typedef void (*out_mdoc
)(void *, const struct mdoc
*);
58 typedef void (*out_man
)(void *, const struct man
*);
59 typedef void (*out_free
)(void *);
62 OUTT_ASCII
= 0, /* -Tascii */
63 OUTT_LOCALE
, /* -Tlocale */
64 OUTT_UTF8
, /* -Tutf8 */
65 OUTT_TREE
, /* -Ttree */
67 OUTT_HTML
, /* -Thtml */
68 OUTT_LINT
, /* -Tlint */
75 struct mchars
*mchars
; /* character table */
76 enum mandoclevel wlevel
; /* ignore messages below this */
77 int wstop
; /* stop after a file with a warning */
78 enum outt outtype
; /* which output to use */
79 out_mdoc outmdoc
; /* mdoc output ptr */
80 out_man outman
; /* man output ptr */
81 out_free outfree
; /* free output ptr */
82 void *outdata
; /* data for output */
83 char outopts
[BUFSIZ
]; /* buf of output opts */
87 static int fs_lookup(const struct manpaths
*,
88 size_t ipath
, const char *,
89 const char *, const char *,
90 struct manpage
**, size_t *);
91 static void fs_search(const struct mansearch
*,
92 const struct manpaths
*, int, char**,
93 struct manpage
**, size_t *);
95 static int koptions(int *, char *);
97 int mandocdb(int, char**);
99 static int moptions(int *, char *);
100 static void mmsg(enum mandocerr
, enum mandoclevel
,
101 const char *, int, int, const char *);
102 static void parse(struct curparse
*, int,
103 const char *, enum mandoclevel
*);
105 static enum mandoclevel
passthrough(const char *, int, int);
107 static void spawn_pager(void);
108 static int toptions(struct curparse
*, char *);
109 static void usage(enum argmode
) __attribute__((noreturn
));
110 static void version(void) __attribute__((noreturn
));
111 static int woptions(struct curparse
*, char *);
113 static const int sec_prios
[] = {1, 4, 5, 8, 6, 3, 7, 2, 9};
114 static char help_arg
[] = "help";
115 static char *help_argv
[] = {help_arg
, NULL
};
116 static const char *progname
;
120 main(int argc
, char *argv
[])
122 struct curparse curp
;
123 struct mansearch search
;
124 struct manpaths paths
;
129 struct manpage
*res
, *resp
;
130 char *conf_file
, *defpaths
;
132 int prio
, best_prio
, synopsis_only
;
135 enum mandoclevel rc
, rctmp
;
136 enum outmode outmode
;
143 progname
= strrchr(argv
[0], '/');
144 if (progname
== NULL
)
150 if (strcmp(progname
, BINM_MAKEWHATIS
) == 0)
151 return(mandocdb(argc
, argv
));
154 /* Search options. */
156 memset(&paths
, 0, sizeof(struct manpaths
));
158 conf_file
= defpaths
= NULL
;
162 memset(&search
, 0, sizeof(struct mansearch
));
163 search
.outkey
= "Nd";
165 if (strcmp(progname
, BINM_MAN
) == 0)
166 search
.argmode
= ARG_NAME
;
167 else if (strcmp(progname
, BINM_APROPOS
) == 0)
168 search
.argmode
= ARG_EXPR
;
169 else if (strcmp(progname
, BINM_WHATIS
) == 0)
170 search
.argmode
= ARG_WORD
;
171 else if (strncmp(progname
, "help", 4) == 0)
172 search
.argmode
= ARG_NAME
;
174 search
.argmode
= ARG_FILE
;
176 /* Parser and formatter options. */
178 memset(&curp
, 0, sizeof(struct curparse
));
179 curp
.outtype
= OUTT_LOCALE
;
180 curp
.wlevel
= MANDOCLEVEL_BADARG
;
181 options
= MPARSE_SO
| MPARSE_UTF8
| MPARSE_LATIN1
;
189 outmode
= OUTMODE_DEF
;
191 while (-1 != (c
= getopt(argc
, argv
,
192 "aC:cfhI:iK:klM:m:O:S:s:T:VW:w"))) {
195 outmode
= OUTMODE_ALL
;
206 search
.argmode
= ARG_WORD
;
209 (void)strlcat(curp
.outopts
, "synopsis,", BUFSIZ
);
214 outmode
= OUTMODE_ALL
;
217 if (strncmp(optarg
, "os=", 3)) {
219 "%s: -I %s: Bad argument\n",
221 return((int)MANDOCLEVEL_BADARG
);
225 "%s: -I %s: Duplicate argument\n",
227 return((int)MANDOCLEVEL_BADARG
);
229 defos
= mandoc_strdup(optarg
+ 3);
232 outmode
= OUTMODE_INT
;
235 if ( ! koptions(&options
, optarg
))
236 return((int)MANDOCLEVEL_BADARG
);
239 search
.argmode
= ARG_EXPR
;
242 search
.argmode
= ARG_FILE
;
243 outmode
= OUTMODE_ALL
;
254 search
.outkey
= optarg
;
255 (void)strlcat(curp
.outopts
, optarg
, BUFSIZ
);
256 (void)strlcat(curp
.outopts
, ",", BUFSIZ
);
259 search
.arch
= optarg
;
265 if ( ! toptions(&curp
, optarg
))
266 return((int)MANDOCLEVEL_BADARG
);
269 if ( ! woptions(&curp
, optarg
))
270 return((int)MANDOCLEVEL_BADARG
);
273 outmode
= OUTMODE_FLN
;
285 usage(search
.argmode
);
287 /* Postprocess options. */
289 if (outmode
== OUTMODE_DEF
) {
290 switch (search
.argmode
) {
292 outmode
= OUTMODE_ALL
;
296 outmode
= OUTMODE_ONE
;
299 outmode
= OUTMODE_LST
;
304 /* Parse arguments. */
314 * and for a man(1) section argument without -s.
317 if (search
.argmode
== ARG_NAME
) {
318 if (*progname
== 'h') {
323 } else if (argc
> 1 &&
324 ((uc
= argv
[0]) != NULL
) &&
325 ((isdigit(uc
[0]) && (uc
[1] == '\0' ||
326 (isalpha(uc
[1]) && uc
[2] == '\0'))) ||
327 (uc
[0] == 'n' && uc
[1] == '\0'))) {
332 if (search
.arch
== NULL
)
333 search
.arch
= getenv("MACHINE");
334 if (search
.arch
== NULL
)
335 search
.arch
= MACHINE
;
340 /* man(1), whatis(1), apropos(1) */
342 if (search
.argmode
!= ARG_FILE
) {
345 usage(search
.argmode
);
347 if (search
.argmode
== ARG_NAME
&&
348 outmode
== OUTMODE_ONE
)
349 search
.firstmatch
= 1;
351 /* Access the mandoc database. */
353 manpath_parse(&paths
, conf_file
, defpaths
, auxpaths
);
355 if( ! mansearch(&search
, &paths
, argc
, argv
, &res
, &sz
))
356 usage(search
.argmode
);
358 if (sz
== 0 && search
.argmode
== ARG_NAME
)
359 fs_search(&search
, &paths
, argc
, argv
, &res
, &sz
);
362 rc
= MANDOCLEVEL_BADARG
;
367 * For standard man(1) and -a output mode,
368 * prepare for copying filename pointers
369 * into the program parameter array.
372 if (outmode
== OUTMODE_ONE
) {
375 } else if (outmode
== OUTMODE_ALL
)
378 /* Iterate all matching manuals. */
381 for (i
= 0; i
< sz
; i
++) {
382 if (outmode
== OUTMODE_FLN
)
384 else if (outmode
== OUTMODE_LST
)
385 printf("%s - %s\n", res
[i
].names
,
386 res
[i
].output
== NULL
? "" :
388 else if (outmode
== OUTMODE_ONE
) {
389 /* Search for the best section. */
390 isec
= strcspn(res
[i
].file
, "123456789");
391 sec
= res
[i
].file
[isec
];
394 prio
= sec_prios
[sec
- '1'];
395 if (prio
>= best_prio
)
403 * For man(1), -a and -i output mode, fall through
404 * to the main mandoc(1) code iterating files
405 * and running the parsers on each of them.
408 if (outmode
== OUTMODE_FLN
|| outmode
== OUTMODE_LST
)
411 fputs("mandoc: database support not compiled in\n",
413 return((int)MANDOCLEVEL_BADARG
);
419 if (search
.argmode
== ARG_FILE
&& ! moptions(&options
, auxpaths
))
420 return((int)MANDOCLEVEL_BADARG
);
422 curp
.mchars
= mchars_alloc();
423 curp
.mp
= mparse_alloc(options
, curp
.wlevel
, mmsg
,
427 * Conditionally start up the lookaside buffer before parsing.
429 if (OUTT_MAN
== curp
.outtype
)
430 mparse_keep(curp
.mp
);
433 if (use_pager
&& isatty(STDOUT_FILENO
))
435 parse(&curp
, STDIN_FILENO
, "<stdin>", &rc
);
439 rctmp
= mparse_open(curp
.mp
, &fd
,
441 resp
!= NULL
? resp
->file
:
448 if (use_pager
&& isatty(STDOUT_FILENO
))
455 parse(&curp
, fd
, *argv
, &rc
);
457 else if (resp
->form
& FORM_SRC
) {
458 /* For .so only; ignore failure. */
459 chdir(paths
.paths
[resp
->ipath
]);
460 parse(&curp
, fd
, resp
->file
, &rc
);
462 rctmp
= passthrough(resp
->file
, fd
,
469 rctmp
= mparse_wait(curp
.mp
);
473 if (argc
> 1 && curp
.outtype
<= OUTT_UTF8
)
474 ascii_sepline(curp
.outdata
);
477 if (MANDOCLEVEL_OK
!= rc
&& curp
.wstop
)
487 mparse_reset(curp
.mp
);
491 (*curp
.outfree
)(curp
.outdata
);
492 mparse_free(curp
.mp
);
493 mchars_free(curp
.mchars
);
497 if (search
.argmode
!= ARG_FILE
) {
498 manpath_free(&paths
);
499 mansearch_free(res
, sz
);
513 printf("mandoc %s\n", VERSION
);
514 exit((int)MANDOCLEVEL_OK
);
518 usage(enum argmode argmode
)
523 fputs("usage: mandoc [-acfhklV] [-Ios=name] "
524 "[-Kencoding] [-mformat] [-Ooption]\n"
525 "\t [-Toutput] [-Wlevel] [file ...]\n", stderr
);
528 fputs("usage: man [-acfhklVw] [-C file] [-I os=name] "
529 "[-K encoding] [-M path] [-m path]\n"
530 "\t [-O option=value] [-S subsection] [-s section] "
531 "[-T output] [-W level]\n"
532 "\t [section] name ...\n", stderr
);
535 fputs("usage: whatis [-acfhklVw] [-C file] "
536 "[-M path] [-m path] [-O outkey] [-S arch]\n"
537 "\t [-s section] name ...\n", stderr
);
540 fputs("usage: apropos [-acfhklVw] [-C file] "
541 "[-M path] [-m path] [-O outkey] [-S arch]\n"
542 "\t [-s section] expression ...\n", stderr
);
545 exit((int)MANDOCLEVEL_BADARG
);
550 fs_lookup(const struct manpaths
*paths
, size_t ipath
,
551 const char *sec
, const char *arch
, const char *name
,
552 struct manpage
**res
, size_t *ressz
)
554 struct manpage
*page
;
558 mandoc_asprintf(&file
, "%s/man%s/%s.%s",
559 paths
->paths
[ipath
], sec
, name
, sec
);
560 if (access(file
, R_OK
) != -1) {
566 mandoc_asprintf(&file
, "%s/cat%s/%s.0",
567 paths
->paths
[ipath
], sec
, name
);
568 if (access(file
, R_OK
) != -1) {
575 mandoc_asprintf(&file
, "%s/man%s/%s/%s.%s",
576 paths
->paths
[ipath
], sec
, arch
, name
, sec
);
577 if (access(file
, R_OK
) != -1) {
586 fprintf(stderr
, "%s: outdated mandoc.db lacks %s(%s) entry,\n"
587 " consider running # makewhatis %s\n",
588 progname
, name
, sec
, paths
->paths
[ipath
]);
590 *res
= mandoc_reallocarray(*res
, ++*ressz
, sizeof(struct manpage
));
591 page
= *res
+ (*ressz
- 1);
596 page
->bits
= NAME_FILE
& NAME_MASK
;
597 page
->sec
= (*sec
>= '1' && *sec
<= '9') ? *sec
- '1' + 1 : 10;
603 fs_search(const struct mansearch
*cfg
, const struct manpaths
*paths
,
604 int argc
, char **argv
, struct manpage
**res
, size_t *ressz
)
606 const char *const sections
[] =
607 {"1", "8", "6", "2", "3", "3p", "5", "7", "4", "9"};
608 const size_t nsec
= sizeof(sections
)/sizeof(sections
[0]);
610 size_t ipath
, isec
, lastsz
;
612 assert(cfg
->argmode
== ARG_NAME
);
617 for (ipath
= 0; ipath
< paths
->sz
; ipath
++) {
618 if (cfg
->sec
!= NULL
) {
619 if (fs_lookup(paths
, ipath
, cfg
->sec
,
620 cfg
->arch
, *argv
, res
, ressz
) &&
623 } else for (isec
= 0; isec
< nsec
; isec
++)
624 if (fs_lookup(paths
, ipath
, sections
[isec
],
625 cfg
->arch
, *argv
, res
, ressz
) &&
629 if (*ressz
== lastsz
)
631 "%s: No entry for %s in the manual.\n",
641 parse(struct curparse
*curp
, int fd
, const char *file
,
642 enum mandoclevel
*level
)
648 /* Begin by parsing the file itself. */
653 rc
= mparse_readfd(curp
->mp
, fd
, file
);
656 * With -Wstop and warnings or errors of at least the requested
657 * level, do not produce output.
660 if (MANDOCLEVEL_OK
!= rc
&& curp
->wstop
)
663 /* If unset, allocate output dev now (if applicable). */
665 if ( ! (curp
->outman
&& curp
->outmdoc
)) {
666 switch (curp
->outtype
) {
668 curp
->outdata
= html_alloc(curp
->mchars
,
670 curp
->outfree
= html_free
;
673 curp
->outdata
= utf8_alloc(curp
->mchars
,
675 curp
->outfree
= ascii_free
;
678 curp
->outdata
= locale_alloc(curp
->mchars
,
680 curp
->outfree
= ascii_free
;
683 curp
->outdata
= ascii_alloc(curp
->mchars
,
685 curp
->outfree
= ascii_free
;
688 curp
->outdata
= pdf_alloc(curp
->mchars
,
690 curp
->outfree
= pspdf_free
;
693 curp
->outdata
= ps_alloc(curp
->mchars
,
695 curp
->outfree
= pspdf_free
;
701 switch (curp
->outtype
) {
703 curp
->outman
= html_man
;
704 curp
->outmdoc
= html_mdoc
;
707 curp
->outman
= tree_man
;
708 curp
->outmdoc
= tree_mdoc
;
711 curp
->outmdoc
= man_mdoc
;
712 curp
->outman
= man_man
;
723 curp
->outman
= terminal_man
;
724 curp
->outmdoc
= terminal_mdoc
;
731 mparse_result(curp
->mp
, &mdoc
, &man
, NULL
);
733 /* Execute the out device, if it exists. */
735 if (man
&& curp
->outman
)
736 (*curp
->outman
)(curp
->outdata
, man
);
737 if (mdoc
&& curp
->outmdoc
)
738 (*curp
->outmdoc
)(curp
->outdata
, mdoc
);
746 static enum mandoclevel
747 passthrough(const char *file
, int fd
, int synopsis_only
)
749 const char synb
[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
750 const char synr
[] = "SYNOPSIS";
761 if ((stream
= fdopen(fd
, "r")) == NULL
) {
768 while ((line
= fgetln(stream
, &len
)) != NULL
) {
771 if ( ! isspace((unsigned char)*line
))
774 isspace((unsigned char)*line
)) {
779 if ((len
== sizeof(synb
) &&
780 ! strncmp(line
, synb
, len
- 1)) ||
781 (len
== sizeof(synr
) &&
782 ! strncmp(line
, synr
, len
- 1)))
787 for (off
= 0; off
< len
; off
+= nw
)
788 if ((nw
= write(STDOUT_FILENO
, line
+ off
,
789 len
- off
)) == -1 || nw
== 0) {
796 if (ferror(stream
)) {
804 return(MANDOCLEVEL_OK
);
807 fprintf(stderr
, "%s: %s: SYSERR: %s: %s",
808 progname
, file
, syscall
, strerror(errno
));
809 return(MANDOCLEVEL_SYSERR
);
814 koptions(int *options
, char *arg
)
817 if ( ! strcmp(arg
, "utf-8")) {
818 *options
|= MPARSE_UTF8
;
819 *options
&= ~MPARSE_LATIN1
;
820 } else if ( ! strcmp(arg
, "iso-8859-1")) {
821 *options
|= MPARSE_LATIN1
;
822 *options
&= ~MPARSE_UTF8
;
823 } else if ( ! strcmp(arg
, "us-ascii")) {
824 *options
&= ~(MPARSE_UTF8
| MPARSE_LATIN1
);
826 fprintf(stderr
, "%s: -K %s: Bad argument\n",
834 moptions(int *options
, char *arg
)
839 else if (0 == strcmp(arg
, "doc"))
840 *options
|= MPARSE_MDOC
;
841 else if (0 == strcmp(arg
, "andoc"))
843 else if (0 == strcmp(arg
, "an"))
844 *options
|= MPARSE_MAN
;
846 fprintf(stderr
, "%s: -m %s: Bad argument\n",
855 toptions(struct curparse
*curp
, char *arg
)
858 if (0 == strcmp(arg
, "ascii"))
859 curp
->outtype
= OUTT_ASCII
;
860 else if (0 == strcmp(arg
, "lint")) {
861 curp
->outtype
= OUTT_LINT
;
862 curp
->wlevel
= MANDOCLEVEL_WARNING
;
863 } else if (0 == strcmp(arg
, "tree"))
864 curp
->outtype
= OUTT_TREE
;
865 else if (0 == strcmp(arg
, "man"))
866 curp
->outtype
= OUTT_MAN
;
867 else if (0 == strcmp(arg
, "html"))
868 curp
->outtype
= OUTT_HTML
;
869 else if (0 == strcmp(arg
, "utf8"))
870 curp
->outtype
= OUTT_UTF8
;
871 else if (0 == strcmp(arg
, "locale"))
872 curp
->outtype
= OUTT_LOCALE
;
873 else if (0 == strcmp(arg
, "xhtml"))
874 curp
->outtype
= OUTT_HTML
;
875 else if (0 == strcmp(arg
, "ps"))
876 curp
->outtype
= OUTT_PS
;
877 else if (0 == strcmp(arg
, "pdf"))
878 curp
->outtype
= OUTT_PDF
;
880 fprintf(stderr
, "%s: -T %s: Bad argument\n",
889 woptions(struct curparse
*curp
, char *arg
)
904 switch (getsubopt(&arg
, UNCONST(toks
), &v
)) {
911 curp
->wlevel
= MANDOCLEVEL_WARNING
;
914 curp
->wlevel
= MANDOCLEVEL_ERROR
;
917 curp
->wlevel
= MANDOCLEVEL_UNSUPP
;
920 curp
->wlevel
= MANDOCLEVEL_BADARG
;
923 fprintf(stderr
, "%s: -W %s: Bad argument\n",
933 mmsg(enum mandocerr t
, enum mandoclevel lvl
,
934 const char *file
, int line
, int col
, const char *msg
)
936 const char *mparse_msg
;
938 fprintf(stderr
, "%s: %s:", progname
, file
);
941 fprintf(stderr
, "%d:%d:", line
, col
+ 1);
943 fprintf(stderr
, " %s", mparse_strlevel(lvl
));
945 if (NULL
!= (mparse_msg
= mparse_strerror(t
)))
946 fprintf(stderr
, ": %s", mparse_msg
);
949 fprintf(stderr
, ": %s", msg
);
957 #define MAX_PAGER_ARGS 16
958 char *argv
[MAX_PAGER_ARGS
];
964 if (pipe(fildes
) == -1) {
965 fprintf(stderr
, "%s: pipe: %s\n",
966 progname
, strerror(errno
));
972 fprintf(stderr
, "%s: fork: %s\n",
973 progname
, strerror(errno
));
974 exit((int)MANDOCLEVEL_SYSERR
);
977 if (dup2(fildes
[1], STDOUT_FILENO
) == -1) {
978 fprintf(stderr
, "%s: dup output: %s\n",
979 progname
, strerror(errno
));
980 exit((int)MANDOCLEVEL_SYSERR
);
987 /* The original process becomes the pager. */
990 if (dup2(fildes
[0], STDIN_FILENO
) == -1) {
991 fprintf(stderr
, "%s: dup input: %s\n",
992 progname
, strerror(errno
));
993 exit((int)MANDOCLEVEL_SYSERR
);
996 pager
= getenv("MANPAGER");
997 if (pager
== NULL
|| *pager
== '\0')
998 pager
= getenv("PAGER");
999 if (pager
== NULL
|| *pager
== '\0')
1000 pager
= "/usr/bin/more -s";
1001 cp
= mandoc_strdup(pager
);
1004 * Parse the pager command into words.
1005 * Intentionally do not do anything fancy here.
1009 while (argc
+ 1 < MAX_PAGER_ARGS
) {
1011 cp
= strchr(cp
, ' ');
1022 /* Hand over to the pager. */
1024 execvp(argv
[0], argv
);
1025 fprintf(stderr
, "%s: exec: %s\n",
1026 progname
, strerror(errno
));
1027 exit((int)MANDOCLEVEL_SYSERR
);