]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
1 /* $Id: main.c,v 1.244 2015/07/28 18:38:55 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 AUTHORS DISCLAIM ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS 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 */
37 #include "mandoc_aux.h"
45 #include "mansearch.h"
47 #if !defined(__GNUC__) || (__GNUC__ < 2)
49 # define __attribute__(x)
51 #endif /* !defined(__GNUC__) || (__GNUC__ < 2) */
62 typedef void (*out_mdoc
)(void *, const struct roff_man
*);
63 typedef void (*out_man
)(void *, const struct roff_man
*);
64 typedef void (*out_free
)(void *);
67 OUTT_ASCII
= 0, /* -Tascii */
68 OUTT_LOCALE
, /* -Tlocale */
69 OUTT_UTF8
, /* -Tutf8 */
70 OUTT_TREE
, /* -Ttree */
72 OUTT_HTML
, /* -Thtml */
73 OUTT_LINT
, /* -Tlint */
80 struct mchars
*mchars
; /* character table */
81 enum mandoclevel wlevel
; /* ignore messages below this */
82 int wstop
; /* stop after a file with a warning */
83 enum outt outtype
; /* which output to use */
84 out_mdoc outmdoc
; /* mdoc output ptr */
85 out_man outman
; /* man output ptr */
86 out_free outfree
; /* free output ptr */
87 void *outdata
; /* data for output */
88 struct manoutput
*outopts
; /* output options */
91 static int fs_lookup(const struct manpaths
*,
92 size_t ipath
, const char *,
93 const char *, const char *,
94 struct manpage
**, size_t *);
95 static void fs_search(const struct mansearch
*,
96 const struct manpaths
*, int, char**,
97 struct manpage
**, size_t *);
98 static int koptions(int *, char *);
100 int mandocdb(int, char**);
102 static int moptions(int *, char *);
103 static void mmsg(enum mandocerr
, enum mandoclevel
,
104 const char *, int, int, const char *);
105 static void parse(struct curparse
*, int, const char *);
106 static void passthrough(const char *, int, int);
107 static pid_t
spawn_pager(struct tag_files
*);
108 static int toptions(struct curparse
*, char *);
109 static void usage(enum argmode
) __attribute__((noreturn
));
110 static int woptions(struct curparse
*, char *);
112 static const int sec_prios
[] = {1, 4, 5, 8, 6, 3, 7, 2, 9};
113 static char help_arg
[] = "help";
114 static char *help_argv
[] = {help_arg
, NULL
};
115 static const char *progname
;
116 static enum mandoclevel rc
;
120 main(int argc
, char *argv
[])
123 struct curparse curp
;
124 struct mansearch search
;
125 struct tag_files
*tag_files
;
129 struct manpage
*res
, *resp
;
130 char *conf_file
, *defpaths
;
134 enum mandoclevel rctmp
;
135 enum outmode outmode
;
144 else if ((progname
= strrchr(argv
[0], '/')) == NULL
)
150 if (strcmp(progname
, BINM_MAKEWHATIS
) == 0)
151 return(mandocdb(argc
, argv
));
154 /* Search options. */
156 memset(&conf
, 0, sizeof(conf
));
157 conf_file
= defpaths
= NULL
;
160 memset(&search
, 0, sizeof(struct mansearch
));
161 search
.outkey
= "Nd";
163 if (strcmp(progname
, BINM_MAN
) == 0)
164 search
.argmode
= ARG_NAME
;
165 else if (strcmp(progname
, BINM_APROPOS
) == 0)
166 search
.argmode
= ARG_EXPR
;
167 else if (strcmp(progname
, BINM_WHATIS
) == 0)
168 search
.argmode
= ARG_WORD
;
169 else if (strncmp(progname
, "help", 4) == 0)
170 search
.argmode
= ARG_NAME
;
172 search
.argmode
= ARG_FILE
;
174 /* Parser and formatter options. */
176 memset(&curp
, 0, sizeof(struct curparse
));
177 curp
.outtype
= OUTT_LOCALE
;
178 curp
.wlevel
= MANDOCLEVEL_BADARG
;
179 curp
.outopts
= &conf
.output
;
180 options
= MPARSE_SO
| MPARSE_UTF8
| MPARSE_LATIN1
;
186 outmode
= OUTMODE_DEF
;
188 while (-1 != (c
= getopt(argc
, argv
,
189 "aC:cfhI:iK:klM:m:O:S:s:T:VW:w"))) {
192 outmode
= OUTMODE_ALL
;
201 search
.argmode
= ARG_WORD
;
204 conf
.output
.synopsisonly
= 1;
206 outmode
= OUTMODE_ALL
;
209 if (strncmp(optarg
, "os=", 3)) {
211 "%s: -I %s: Bad argument\n",
213 return((int)MANDOCLEVEL_BADARG
);
217 "%s: -I %s: Duplicate argument\n",
219 return((int)MANDOCLEVEL_BADARG
);
221 defos
= mandoc_strdup(optarg
+ 3);
224 outmode
= OUTMODE_INT
;
227 if ( ! koptions(&options
, optarg
))
228 return((int)MANDOCLEVEL_BADARG
);
231 search
.argmode
= ARG_EXPR
;
234 search
.argmode
= ARG_FILE
;
235 outmode
= OUTMODE_ALL
;
244 search
.outkey
= optarg
;
245 while (optarg
!= NULL
)
246 manconf_output(&conf
.output
,
247 strsep(&optarg
, ","));
250 search
.arch
= optarg
;
256 if ( ! toptions(&curp
, optarg
))
257 return((int)MANDOCLEVEL_BADARG
);
260 if ( ! woptions(&curp
, optarg
))
261 return((int)MANDOCLEVEL_BADARG
);
264 outmode
= OUTMODE_FLN
;
273 usage(search
.argmode
);
275 /* Postprocess options. */
277 if (outmode
== OUTMODE_DEF
) {
278 switch (search
.argmode
) {
280 outmode
= OUTMODE_ALL
;
284 outmode
= OUTMODE_ONE
;
287 outmode
= OUTMODE_LST
;
292 /* Parse arguments. */
302 * and for a man(1) section argument without -s.
305 if (search
.argmode
== ARG_NAME
) {
306 if (*progname
== 'h') {
311 } else if (argc
> 1 &&
312 ((uc
= (unsigned char *)argv
[0]) != NULL
) &&
313 ((isdigit(uc
[0]) && (uc
[1] == '\0' ||
314 (isalpha(uc
[1]) && uc
[2] == '\0'))) ||
315 (uc
[0] == 'n' && uc
[1] == '\0'))) {
316 search
.sec
= (char *)uc
;
320 if (search
.arch
== NULL
)
321 search
.arch
= getenv("MACHINE");
323 if (search
.arch
== NULL
)
324 search
.arch
= MACHINE
;
330 /* man(1), whatis(1), apropos(1) */
332 if (search
.argmode
!= ARG_FILE
) {
334 usage(search
.argmode
);
336 if (search
.argmode
== ARG_NAME
&&
337 outmode
== OUTMODE_ONE
)
338 search
.firstmatch
= 1;
340 /* Access the mandoc database. */
342 manconf_parse(&conf
, conf_file
, defpaths
, auxpaths
);
345 if ( ! mansearch(&search
, &conf
.manpath
,
346 argc
, argv
, &res
, &sz
))
347 usage(search
.argmode
);
349 if (search
.argmode
!= ARG_NAME
) {
350 fputs("mandoc: database support not compiled in\n",
352 return((int)MANDOCLEVEL_BADARG
);
358 if (search
.argmode
== ARG_NAME
)
359 fs_search(&search
, &conf
.manpath
,
360 argc
, argv
, &res
, &sz
);
363 "%s: nothing appropriate\n",
368 rc
= MANDOCLEVEL_BADARG
;
373 * For standard man(1) and -a output mode,
374 * prepare for copying filename pointers
375 * into the program parameter array.
378 if (outmode
== OUTMODE_ONE
) {
381 } else if (outmode
== OUTMODE_ALL
)
384 /* Iterate all matching manuals. */
387 for (i
= 0; i
< sz
; i
++) {
388 if (outmode
== OUTMODE_FLN
)
390 else if (outmode
== OUTMODE_LST
)
391 printf("%s - %s\n", res
[i
].names
,
392 res
[i
].output
== NULL
? "" :
394 else if (outmode
== OUTMODE_ONE
) {
395 /* Search for the best section. */
396 isec
= strcspn(res
[i
].file
, "123456789");
397 sec
= res
[i
].file
[isec
];
400 prio
= sec_prios
[sec
- '1'];
401 if (prio
>= best_prio
)
409 * For man(1), -a and -i output mode, fall through
410 * to the main mandoc(1) code iterating files
411 * and running the parsers on each of them.
414 if (outmode
== OUTMODE_FLN
|| outmode
== OUTMODE_LST
)
420 if (search
.argmode
== ARG_FILE
&& ! moptions(&options
, auxpaths
))
421 return((int)MANDOCLEVEL_BADARG
);
423 if (use_pager
&& ! isatty(STDOUT_FILENO
))
426 curp
.mchars
= mchars_alloc();
427 curp
.mp
= mparse_alloc(options
, curp
.wlevel
, mmsg
,
431 * Conditionally start up the lookaside buffer before parsing.
433 if (OUTT_MAN
== curp
.outtype
)
434 mparse_keep(curp
.mp
);
438 tag_files
= tag_init();
439 parse(&curp
, STDIN_FILENO
, "<stdin>");
443 rctmp
= mparse_open(curp
.mp
, &fd
,
444 resp
!= NULL
? resp
->file
: *argv
);
450 tag_files
= tag_init();
455 parse(&curp
, fd
, *argv
);
456 else if (resp
->form
& FORM_SRC
) {
457 /* For .so only; ignore failure. */
458 chdir(conf
.manpath
.paths
[resp
->ipath
]);
459 parse(&curp
, fd
, resp
->file
);
461 passthrough(resp
->file
, fd
,
462 conf
.output
.synopsisonly
);
464 if (argc
> 1 && curp
.outtype
<= OUTT_UTF8
)
465 ascii_sepline(curp
.outdata
);
468 if (MANDOCLEVEL_OK
!= rc
&& curp
.wstop
)
476 mparse_reset(curp
.mp
);
480 (*curp
.outfree
)(curp
.outdata
);
481 mparse_free(curp
.mp
);
482 mchars_free(curp
.mchars
);
485 if (search
.argmode
!= ARG_FILE
) {
488 mansearch_free(res
, sz
);
496 * When using a pager, finish writing both temporary files,
497 * fork it, wait for the user to close it, and clean up.
500 if (tag_files
!= NULL
) {
503 waitpid(spawn_pager(tag_files
), NULL
, 0);
511 usage(enum argmode argmode
)
516 fputs("usage: mandoc [-acfhkl] [-I os=name] "
517 "[-K encoding] [-mformat] [-O option]\n"
518 "\t [-T output] [-W level] [file ...]\n", stderr
);
521 fputs("usage: man [-acfhklw] [-C file] [-I os=name] "
522 "[-K encoding] [-M path] [-m path]\n"
523 "\t [-O option=value] [-S subsection] [-s section] "
524 "[-T output] [-W level]\n"
525 "\t [section] name ...\n", stderr
);
528 fputs("usage: whatis [-acfhklw] [-C file] "
529 "[-M path] [-m path] [-O outkey] [-S arch]\n"
530 "\t [-s section] name ...\n", stderr
);
533 fputs("usage: apropos [-acfhklw] [-C file] "
534 "[-M path] [-m path] [-O outkey] [-S arch]\n"
535 "\t [-s section] expression ...\n", stderr
);
538 exit((int)MANDOCLEVEL_BADARG
);
542 fs_lookup(const struct manpaths
*paths
, size_t ipath
,
543 const char *sec
, const char *arch
, const char *name
,
544 struct manpage
**res
, size_t *ressz
)
547 struct manpage
*page
;
552 mandoc_asprintf(&file
, "%s/man%s/%s.%s",
553 paths
->paths
[ipath
], sec
, name
, sec
);
554 if (access(file
, R_OK
) != -1)
558 mandoc_asprintf(&file
, "%s/cat%s/%s.0",
559 paths
->paths
[ipath
], sec
, name
);
560 if (access(file
, R_OK
) != -1) {
567 mandoc_asprintf(&file
, "%s/man%s/%s/%s.%s",
568 paths
->paths
[ipath
], sec
, arch
, name
, sec
);
569 if (access(file
, R_OK
) != -1)
574 mandoc_asprintf(&file
, "%s/man%s/%s.[01-9]*",
575 paths
->paths
[ipath
], sec
, name
);
576 globres
= glob(file
, 0, NULL
, &globinfo
);
577 if (globres
!= 0 && globres
!= GLOB_NOMATCH
)
578 fprintf(stderr
, "%s: %s: glob: %s\n",
579 progname
, file
, strerror(errno
));
582 file
= mandoc_strdup(*globinfo
.gl_pathv
);
589 fprintf(stderr
, "%s: outdated mandoc.db lacks %s(%s) entry, run "
590 "makewhatis %s\n", progname
, name
, sec
, paths
->paths
[ipath
]);
592 *res
= mandoc_reallocarray(*res
, ++*ressz
, sizeof(struct manpage
));
593 page
= *res
+ (*ressz
- 1);
598 page
->bits
= NAME_FILE
& NAME_MASK
;
599 page
->sec
= (*sec
>= '1' && *sec
<= '9') ? *sec
- '1' + 1 : 10;
605 fs_search(const struct mansearch
*cfg
, const struct manpaths
*paths
,
606 int argc
, char **argv
, struct manpage
**res
, size_t *ressz
)
608 const char *const sections
[] =
609 {"1", "8", "6", "2", "3", "3p", "5", "7", "4", "9"};
610 const size_t nsec
= sizeof(sections
)/sizeof(sections
[0]);
612 size_t ipath
, isec
, lastsz
;
614 assert(cfg
->argmode
== ARG_NAME
);
619 for (ipath
= 0; ipath
< paths
->sz
; ipath
++) {
620 if (cfg
->sec
!= NULL
) {
621 if (fs_lookup(paths
, ipath
, cfg
->sec
,
622 cfg
->arch
, *argv
, res
, ressz
) &&
625 } else for (isec
= 0; isec
< nsec
; isec
++)
626 if (fs_lookup(paths
, ipath
, sections
[isec
],
627 cfg
->arch
, *argv
, res
, ressz
) &&
631 if (*ressz
== lastsz
)
633 "%s: No entry for %s in the manual.\n",
642 parse(struct curparse
*curp
, int fd
, const char *file
)
644 enum mandoclevel rctmp
;
645 struct roff_man
*man
;
647 /* Begin by parsing the file itself. */
652 rctmp
= mparse_readfd(curp
->mp
, fd
, file
);
657 * With -Wstop and warnings or errors of at least the requested
658 * level, do not produce output.
661 if (rctmp
!= MANDOCLEVEL_OK
&& curp
->wstop
)
664 /* If unset, allocate output dev now (if applicable). */
666 if ( ! (curp
->outman
&& curp
->outmdoc
)) {
667 switch (curp
->outtype
) {
669 curp
->outdata
= html_alloc(curp
->mchars
,
671 curp
->outfree
= html_free
;
674 curp
->outdata
= utf8_alloc(curp
->mchars
,
676 curp
->outfree
= ascii_free
;
679 curp
->outdata
= locale_alloc(curp
->mchars
,
681 curp
->outfree
= ascii_free
;
684 curp
->outdata
= ascii_alloc(curp
->mchars
,
686 curp
->outfree
= ascii_free
;
689 curp
->outdata
= pdf_alloc(curp
->mchars
,
691 curp
->outfree
= pspdf_free
;
694 curp
->outdata
= ps_alloc(curp
->mchars
,
696 curp
->outfree
= pspdf_free
;
702 switch (curp
->outtype
) {
704 curp
->outman
= html_man
;
705 curp
->outmdoc
= html_mdoc
;
708 curp
->outman
= tree_man
;
709 curp
->outmdoc
= tree_mdoc
;
712 curp
->outmdoc
= man_mdoc
;
713 curp
->outman
= man_man
;
724 curp
->outman
= terminal_man
;
725 curp
->outmdoc
= terminal_mdoc
;
732 mparse_result(curp
->mp
, &man
, NULL
);
734 /* Execute the out device, if it exists. */
738 if (curp
->outmdoc
!= NULL
&& man
->macroset
== MACROSET_MDOC
)
739 (*curp
->outmdoc
)(curp
->outdata
, man
);
740 if (curp
->outman
!= NULL
&& man
->macroset
== MACROSET_MAN
)
741 (*curp
->outman
)(curp
->outdata
, man
);
745 passthrough(const char *file
, int fd
, int synopsis_only
)
747 const char synb
[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
748 const char synr
[] = "SYNOPSIS";
759 if ((stream
= fdopen(fd
, "r")) == NULL
) {
766 while ((line
= fgetln(stream
, &len
)) != NULL
) {
769 if ( ! isspace((unsigned char)*line
))
772 isspace((unsigned char)*line
)) {
777 if ((len
== sizeof(synb
) &&
778 ! strncmp(line
, synb
, len
- 1)) ||
779 (len
== sizeof(synr
) &&
780 ! strncmp(line
, synr
, len
- 1)))
785 for (off
= 0; off
< len
; off
+= nw
)
786 if ((nw
= write(STDOUT_FILENO
, line
+ off
,
787 len
- off
)) == -1 || nw
== 0) {
794 if (ferror(stream
)) {
805 fprintf(stderr
, "%s: %s: SYSERR: %s: %s",
806 progname
, file
, syscall
, strerror(errno
));
807 if (rc
< MANDOCLEVEL_SYSERR
)
808 rc
= MANDOCLEVEL_SYSERR
;
812 koptions(int *options
, char *arg
)
815 if ( ! strcmp(arg
, "utf-8")) {
816 *options
|= MPARSE_UTF8
;
817 *options
&= ~MPARSE_LATIN1
;
818 } else if ( ! strcmp(arg
, "iso-8859-1")) {
819 *options
|= MPARSE_LATIN1
;
820 *options
&= ~MPARSE_UTF8
;
821 } else if ( ! strcmp(arg
, "us-ascii")) {
822 *options
&= ~(MPARSE_UTF8
| MPARSE_LATIN1
);
824 fprintf(stderr
, "%s: -K %s: Bad argument\n",
832 moptions(int *options
, char *arg
)
837 else if (0 == strcmp(arg
, "doc"))
838 *options
|= MPARSE_MDOC
;
839 else if (0 == strcmp(arg
, "andoc"))
841 else if (0 == strcmp(arg
, "an"))
842 *options
|= MPARSE_MAN
;
844 fprintf(stderr
, "%s: -m %s: Bad argument\n",
853 toptions(struct curparse
*curp
, char *arg
)
856 if (0 == strcmp(arg
, "ascii"))
857 curp
->outtype
= OUTT_ASCII
;
858 else if (0 == strcmp(arg
, "lint")) {
859 curp
->outtype
= OUTT_LINT
;
860 curp
->wlevel
= MANDOCLEVEL_WARNING
;
861 } else if (0 == strcmp(arg
, "tree"))
862 curp
->outtype
= OUTT_TREE
;
863 else if (0 == strcmp(arg
, "man"))
864 curp
->outtype
= OUTT_MAN
;
865 else if (0 == strcmp(arg
, "html"))
866 curp
->outtype
= OUTT_HTML
;
867 else if (0 == strcmp(arg
, "utf8"))
868 curp
->outtype
= OUTT_UTF8
;
869 else if (0 == strcmp(arg
, "locale"))
870 curp
->outtype
= OUTT_LOCALE
;
871 else if (0 == strcmp(arg
, "xhtml"))
872 curp
->outtype
= OUTT_HTML
;
873 else if (0 == strcmp(arg
, "ps"))
874 curp
->outtype
= OUTT_PS
;
875 else if (0 == strcmp(arg
, "pdf"))
876 curp
->outtype
= OUTT_PDF
;
878 fprintf(stderr
, "%s: -T %s: Bad argument\n",
887 woptions(struct curparse
*curp
, char *arg
)
902 switch (getsubopt(&arg
, UNCONST(toks
), &v
)) {
909 curp
->wlevel
= MANDOCLEVEL_WARNING
;
912 curp
->wlevel
= MANDOCLEVEL_ERROR
;
915 curp
->wlevel
= MANDOCLEVEL_UNSUPP
;
918 curp
->wlevel
= MANDOCLEVEL_BADARG
;
921 fprintf(stderr
, "%s: -W %s: Bad argument\n",
931 mmsg(enum mandocerr t
, enum mandoclevel lvl
,
932 const char *file
, int line
, int col
, const char *msg
)
934 const char *mparse_msg
;
936 fprintf(stderr
, "%s: %s:", progname
, file
);
939 fprintf(stderr
, "%d:%d:", line
, col
+ 1);
941 fprintf(stderr
, " %s", mparse_strlevel(lvl
));
943 if (NULL
!= (mparse_msg
= mparse_strerror(t
)))
944 fprintf(stderr
, ": %s", mparse_msg
);
947 fprintf(stderr
, ": %s", msg
);
953 spawn_pager(struct tag_files
*tag_files
)
955 #define MAX_PAGER_ARGS 16
956 char *argv
[MAX_PAGER_ARGS
];
963 pager
= getenv("MANPAGER");
964 if (pager
== NULL
|| *pager
== '\0')
965 pager
= getenv("PAGER");
966 if (pager
== NULL
|| *pager
== '\0')
968 cp
= mandoc_strdup(pager
);
971 * Parse the pager command into words.
972 * Intentionally do not do anything fancy here.
976 while (argc
+ 4 < MAX_PAGER_ARGS
) {
978 cp
= strchr(cp
, ' ');
988 /* For more(1) and less(1), use the tag file. */
990 if ((cmdlen
= strlen(argv
[0])) >= 4) {
991 cp
= argv
[0] + cmdlen
- 4;
992 if (strcmp(cp
, "less") == 0 || strcmp(cp
, "more") == 0) {
993 argv
[argc
++] = mandoc_strdup("-T");
994 argv
[argc
++] = tag_files
->tfn
;
997 argv
[argc
++] = tag_files
->ofn
;
1000 switch (pager_pid
= fork()) {
1002 fprintf(stderr
, "%s: fork: %s\n",
1003 progname
, strerror(errno
));
1004 exit((int)MANDOCLEVEL_SYSERR
);
1011 /* The child process becomes the pager. */
1013 if (dup2(tag_files
->ofd
, STDOUT_FILENO
) == -1) {
1014 fprintf(stderr
, "pager: stdout: %s\n", strerror(errno
));
1015 exit((int)MANDOCLEVEL_SYSERR
);
1017 close(tag_files
->ofd
);
1018 close(tag_files
->tfd
);
1019 execvp(argv
[0], argv
);
1020 fprintf(stderr
, "%s: exec %s: %s\n",
1021 progname
, argv
[0], strerror(errno
));
1022 exit((int)MANDOCLEVEL_SYSERR
);