]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
1 /* $Id: main.c,v 1.257 2015/11/07 17:58: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 */
39 #include "mandoc_aux.h"
47 #include "mansearch.h"
49 #if !defined(__GNUC__) || (__GNUC__ < 2)
51 # define __attribute__(x)
53 #endif /* !defined(__GNUC__) || (__GNUC__ < 2) */
65 OUTT_ASCII
= 0, /* -Tascii */
66 OUTT_LOCALE
, /* -Tlocale */
67 OUTT_UTF8
, /* -Tutf8 */
68 OUTT_TREE
, /* -Ttree */
70 OUTT_HTML
, /* -Thtml */
71 OUTT_LINT
, /* -Tlint */
78 enum mandoclevel wlevel
; /* ignore messages below this */
79 int wstop
; /* stop after a file with a warning */
80 enum outt outtype
; /* which output to use */
81 void *outdata
; /* data for output */
82 struct manoutput
*outopts
; /* output options */
85 static int fs_lookup(const struct manpaths
*,
86 size_t ipath
, const char *,
87 const char *, const char *,
88 struct manpage
**, size_t *);
89 static void fs_search(const struct mansearch
*,
90 const struct manpaths
*, int, char**,
91 struct manpage
**, size_t *);
92 static int koptions(int *, char *);
94 int mandocdb(int, char**);
96 static int moptions(int *, char *);
97 static void mmsg(enum mandocerr
, enum mandoclevel
,
98 const char *, int, int, const char *);
99 static void parse(struct curparse
*, int, const char *);
100 static void passthrough(const char *, int, int);
101 static pid_t
spawn_pager(struct tag_files
*);
102 static int toptions(struct curparse
*, char *);
103 static void usage(enum argmode
) __attribute__((noreturn
));
104 static int woptions(struct curparse
*, char *);
106 static const int sec_prios
[] = {1, 4, 5, 8, 6, 3, 7, 2, 9};
107 static char help_arg
[] = "help";
108 static char *help_argv
[] = {help_arg
, NULL
};
109 static enum mandoclevel rc
;
113 main(int argc
, char *argv
[])
116 struct curparse curp
;
117 struct mansearch search
;
118 struct tag_files
*tag_files
;
119 const char *progname
;
123 struct manpage
*res
, *resp
;
124 char *conf_file
, *defpaths
;
128 enum mandoclevel rctmp
;
129 enum outmode outmode
;
137 progname
= getprogname();
140 progname
= mandoc_strdup("mandoc");
141 else if ((progname
= strrchr(argv
[0], '/')) == NULL
)
145 setprogname(progname
);
149 if (strncmp(progname
, "mandocdb", 8) == 0 ||
150 strcmp(progname
, BINM_MAKEWHATIS
) == 0)
151 return mandocdb(argc
, argv
);
155 if (pledge("stdio rpath tmppath proc exec flock", NULL
) == -1)
156 err((int)MANDOCLEVEL_SYSERR
, "pledge");
159 /* Search options. */
161 memset(&conf
, 0, sizeof(conf
));
162 conf_file
= defpaths
= NULL
;
165 memset(&search
, 0, sizeof(struct mansearch
));
166 search
.outkey
= "Nd";
168 if (strcmp(progname
, BINM_MAN
) == 0)
169 search
.argmode
= ARG_NAME
;
170 else if (strcmp(progname
, BINM_APROPOS
) == 0)
171 search
.argmode
= ARG_EXPR
;
172 else if (strcmp(progname
, BINM_WHATIS
) == 0)
173 search
.argmode
= ARG_WORD
;
174 else if (strncmp(progname
, "help", 4) == 0)
175 search
.argmode
= ARG_NAME
;
177 search
.argmode
= ARG_FILE
;
179 /* Parser and formatter options. */
181 memset(&curp
, 0, sizeof(struct curparse
));
182 curp
.outtype
= OUTT_LOCALE
;
183 curp
.wlevel
= MANDOCLEVEL_BADARG
;
184 curp
.outopts
= &conf
.output
;
185 options
= MPARSE_SO
| MPARSE_UTF8
| MPARSE_LATIN1
;
191 outmode
= OUTMODE_DEF
;
193 while (-1 != (c
= getopt(argc
, argv
,
194 "aC:cfhI:iK:klM:m:O:S:s:T:VW:w"))) {
197 outmode
= OUTMODE_ALL
;
206 search
.argmode
= ARG_WORD
;
209 conf
.output
.synopsisonly
= 1;
211 outmode
= OUTMODE_ALL
;
214 if (strncmp(optarg
, "os=", 3)) {
215 warnx("-I %s: Bad argument", optarg
);
216 return (int)MANDOCLEVEL_BADARG
;
219 warnx("-I %s: Duplicate argument", optarg
);
220 return (int)MANDOCLEVEL_BADARG
;
222 defos
= mandoc_strdup(optarg
+ 3);
225 outmode
= OUTMODE_INT
;
228 if ( ! koptions(&options
, optarg
))
229 return (int)MANDOCLEVEL_BADARG
;
232 search
.argmode
= ARG_EXPR
;
235 search
.argmode
= ARG_FILE
;
236 outmode
= OUTMODE_ALL
;
245 search
.outkey
= optarg
;
246 while (optarg
!= NULL
)
247 manconf_output(&conf
.output
,
248 strsep(&optarg
, ","));
251 search
.arch
= optarg
;
257 if ( ! toptions(&curp
, optarg
))
258 return (int)MANDOCLEVEL_BADARG
;
261 if ( ! woptions(&curp
, optarg
))
262 return (int)MANDOCLEVEL_BADARG
;
265 outmode
= OUTMODE_FLN
;
274 usage(search
.argmode
);
276 /* Postprocess options. */
278 if (outmode
== OUTMODE_DEF
) {
279 switch (search
.argmode
) {
281 outmode
= OUTMODE_ALL
;
285 outmode
= OUTMODE_ONE
;
288 outmode
= OUTMODE_LST
;
293 if (outmode
== OUTMODE_FLN
||
294 outmode
== OUTMODE_LST
||
295 !isatty(STDOUT_FILENO
))
299 if (!use_pager
&& pledge("stdio rpath flock", NULL
) == -1)
300 err((int)MANDOCLEVEL_SYSERR
, "pledge");
303 /* Parse arguments. */
313 * and for a man(1) section argument without -s.
316 if (search
.argmode
== ARG_NAME
) {
317 if (*progname
== 'h') {
322 } else if (argc
> 1 &&
323 ((uc
= (unsigned char *)argv
[0]) != NULL
) &&
324 ((isdigit(uc
[0]) && (uc
[1] == '\0' ||
325 (isalpha(uc
[1]) && uc
[2] == '\0'))) ||
326 (uc
[0] == 'n' && uc
[1] == '\0'))) {
327 search
.sec
= (char *)uc
;
331 if (search
.arch
== NULL
)
332 search
.arch
= getenv("MACHINE");
334 if (search
.arch
== NULL
)
335 search
.arch
= MACHINE
;
341 /* man(1), whatis(1), apropos(1) */
343 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 manconf_parse(&conf
, conf_file
, defpaths
, auxpaths
);
356 if ( ! mansearch(&search
, &conf
.manpath
,
357 argc
, argv
, &res
, &sz
))
358 usage(search
.argmode
);
360 if (search
.argmode
!= ARG_NAME
) {
361 fputs("mandoc: database support not compiled in\n",
363 return (int)MANDOCLEVEL_BADARG
;
369 if (search
.argmode
== ARG_NAME
)
370 fs_search(&search
, &conf
.manpath
,
371 argc
, argv
, &res
, &sz
);
373 warnx("nothing appropriate");
377 rc
= MANDOCLEVEL_BADARG
;
382 * For standard man(1) and -a output mode,
383 * prepare for copying filename pointers
384 * into the program parameter array.
387 if (outmode
== OUTMODE_ONE
) {
390 } else if (outmode
== OUTMODE_ALL
)
393 /* Iterate all matching manuals. */
396 for (i
= 0; i
< sz
; i
++) {
397 if (outmode
== OUTMODE_FLN
)
399 else if (outmode
== OUTMODE_LST
)
400 printf("%s - %s\n", res
[i
].names
,
401 res
[i
].output
== NULL
? "" :
403 else if (outmode
== OUTMODE_ONE
) {
404 /* Search for the best section. */
405 isec
= strcspn(res
[i
].file
, "123456789");
406 sec
= res
[i
].file
[isec
];
409 prio
= sec_prios
[sec
- '1'];
410 if (prio
>= best_prio
)
418 * For man(1), -a and -i output mode, fall through
419 * to the main mandoc(1) code iterating files
420 * and running the parsers on each of them.
423 if (outmode
== OUTMODE_FLN
|| outmode
== OUTMODE_LST
)
430 if (pledge(use_pager
? "stdio rpath tmppath proc exec" :
431 "stdio rpath", NULL
) == -1)
432 err((int)MANDOCLEVEL_SYSERR
, "pledge");
435 if (search
.argmode
== ARG_FILE
&& ! moptions(&options
, auxpaths
))
436 return (int)MANDOCLEVEL_BADARG
;
439 curp
.mp
= mparse_alloc(options
, curp
.wlevel
, mmsg
, defos
);
442 * Conditionally start up the lookaside buffer before parsing.
444 if (OUTT_MAN
== curp
.outtype
)
445 mparse_keep(curp
.mp
);
449 tag_files
= tag_init();
450 parse(&curp
, STDIN_FILENO
, "<stdin>");
454 rctmp
= mparse_open(curp
.mp
, &fd
,
455 resp
!= NULL
? resp
->file
: *argv
);
461 tag_files
= tag_init();
466 parse(&curp
, fd
, *argv
);
467 else if (resp
->form
& FORM_SRC
) {
468 /* For .so only; ignore failure. */
469 chdir(conf
.manpath
.paths
[resp
->ipath
]);
470 parse(&curp
, fd
, resp
->file
);
472 passthrough(resp
->file
, fd
,
473 conf
.output
.synopsisonly
);
475 if (argc
> 1 && curp
.outtype
<= OUTT_UTF8
)
476 ascii_sepline(curp
.outdata
);
479 if (MANDOCLEVEL_OK
!= rc
&& curp
.wstop
)
487 mparse_reset(curp
.mp
);
490 if (curp
.outdata
!= NULL
) {
491 switch (curp
.outtype
) {
493 html_free(curp
.outdata
);
498 ascii_free(curp
.outdata
);
502 pspdf_free(curp
.outdata
);
508 mparse_free(curp
.mp
);
512 if (search
.argmode
!= ARG_FILE
) {
515 mansearch_free(res
, sz
);
523 * When using a pager, finish writing both temporary files,
524 * fork it, wait for the user to close it, and clean up.
527 if (tag_files
!= NULL
) {
530 waitpid(spawn_pager(tag_files
), NULL
, 0);
538 usage(enum argmode argmode
)
543 fputs("usage: mandoc [-acfhkl] [-I os=name] "
544 "[-K encoding] [-mformat] [-O option]\n"
545 "\t [-T output] [-W level] [file ...]\n", stderr
);
548 fputs("usage: man [-acfhklw] [-C file] [-I os=name] "
549 "[-K encoding] [-M path] [-m path]\n"
550 "\t [-O option=value] [-S subsection] [-s section] "
551 "[-T output] [-W level]\n"
552 "\t [section] name ...\n", stderr
);
555 fputs("usage: whatis [-acfhklw] [-C file] "
556 "[-M path] [-m path] [-O outkey] [-S arch]\n"
557 "\t [-s section] name ...\n", stderr
);
560 fputs("usage: apropos [-acfhklw] [-C file] "
561 "[-M path] [-m path] [-O outkey] [-S arch]\n"
562 "\t [-s section] expression ...\n", stderr
);
565 exit((int)MANDOCLEVEL_BADARG
);
569 fs_lookup(const struct manpaths
*paths
, size_t ipath
,
570 const char *sec
, const char *arch
, const char *name
,
571 struct manpage
**res
, size_t *ressz
)
574 struct manpage
*page
;
579 mandoc_asprintf(&file
, "%s/man%s/%s.%s",
580 paths
->paths
[ipath
], sec
, name
, sec
);
581 if (access(file
, R_OK
) != -1)
585 mandoc_asprintf(&file
, "%s/cat%s/%s.0",
586 paths
->paths
[ipath
], sec
, name
);
587 if (access(file
, R_OK
) != -1) {
594 mandoc_asprintf(&file
, "%s/man%s/%s/%s.%s",
595 paths
->paths
[ipath
], sec
, arch
, name
, sec
);
596 if (access(file
, R_OK
) != -1)
601 mandoc_asprintf(&file
, "%s/man%s/%s.[01-9]*",
602 paths
->paths
[ipath
], sec
, name
);
603 globres
= glob(file
, 0, NULL
, &globinfo
);
604 if (globres
!= 0 && globres
!= GLOB_NOMATCH
)
605 warn("%s: glob", file
);
608 file
= mandoc_strdup(*globinfo
.gl_pathv
);
615 warnx("outdated mandoc.db lacks %s(%s) entry, run makewhatis %s",
616 name
, sec
, paths
->paths
[ipath
]);
618 *res
= mandoc_reallocarray(*res
, ++*ressz
, sizeof(struct manpage
));
619 page
= *res
+ (*ressz
- 1);
624 page
->bits
= NAME_FILE
& NAME_MASK
;
625 page
->sec
= (*sec
>= '1' && *sec
<= '9') ? *sec
- '1' + 1 : 10;
631 fs_search(const struct mansearch
*cfg
, const struct manpaths
*paths
,
632 int argc
, char **argv
, struct manpage
**res
, size_t *ressz
)
634 const char *const sections
[] =
635 {"1", "8", "6", "2", "3", "3p", "5", "7", "4", "9"};
636 const size_t nsec
= sizeof(sections
)/sizeof(sections
[0]);
638 size_t ipath
, isec
, lastsz
;
640 assert(cfg
->argmode
== ARG_NAME
);
645 for (ipath
= 0; ipath
< paths
->sz
; ipath
++) {
646 if (cfg
->sec
!= NULL
) {
647 if (fs_lookup(paths
, ipath
, cfg
->sec
,
648 cfg
->arch
, *argv
, res
, ressz
) &&
651 } else for (isec
= 0; isec
< nsec
; isec
++)
652 if (fs_lookup(paths
, ipath
, sections
[isec
],
653 cfg
->arch
, *argv
, res
, ressz
) &&
657 if (*ressz
== lastsz
)
658 warnx("No entry for %s in the manual.", *argv
);
666 parse(struct curparse
*curp
, int fd
, const char *file
)
668 enum mandoclevel rctmp
;
669 struct roff_man
*man
;
671 /* Begin by parsing the file itself. */
676 rctmp
= mparse_readfd(curp
->mp
, fd
, file
);
681 * With -Wstop and warnings or errors of at least the requested
682 * level, do not produce output.
685 if (rctmp
!= MANDOCLEVEL_OK
&& curp
->wstop
)
688 /* If unset, allocate output dev now (if applicable). */
690 if (curp
->outdata
== NULL
) {
691 switch (curp
->outtype
) {
693 curp
->outdata
= html_alloc(curp
->outopts
);
696 curp
->outdata
= utf8_alloc(curp
->outopts
);
699 curp
->outdata
= locale_alloc(curp
->outopts
);
702 curp
->outdata
= ascii_alloc(curp
->outopts
);
705 curp
->outdata
= pdf_alloc(curp
->outopts
);
708 curp
->outdata
= ps_alloc(curp
->outopts
);
715 mparse_result(curp
->mp
, &man
, NULL
);
717 /* Execute the out device, if it exists. */
721 if (man
->macroset
== MACROSET_MDOC
) {
723 switch (curp
->outtype
) {
725 html_mdoc(curp
->outdata
, man
);
728 tree_mdoc(curp
->outdata
, man
);
731 man_mdoc(curp
->outdata
, man
);
738 terminal_mdoc(curp
->outdata
, man
);
744 if (man
->macroset
== MACROSET_MAN
) {
746 switch (curp
->outtype
) {
748 html_man(curp
->outdata
, man
);
751 tree_man(curp
->outdata
, man
);
754 man_man(curp
->outdata
, man
);
761 terminal_man(curp
->outdata
, man
);
770 passthrough(const char *file
, int fd
, int synopsis_only
)
772 const char synb
[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
773 const char synr
[] = "SYNOPSIS";
784 if ((stream
= fdopen(fd
, "r")) == NULL
) {
791 while (getline(&line
, &linesz
, stream
) != -1) {
795 if ( ! isspace((unsigned char)*cp
))
797 while (isspace((unsigned char)*cp
))
800 if (strcmp(cp
, synb
) == 0 ||
801 strcmp(cp
, synr
) == 0)
806 if (fputs(cp
, stdout
)) {
813 if (ferror(stream
)) {
826 warn("%s: SYSERR: %s", file
, syscall
);
827 if (rc
< MANDOCLEVEL_SYSERR
)
828 rc
= MANDOCLEVEL_SYSERR
;
832 koptions(int *options
, char *arg
)
835 if ( ! strcmp(arg
, "utf-8")) {
836 *options
|= MPARSE_UTF8
;
837 *options
&= ~MPARSE_LATIN1
;
838 } else if ( ! strcmp(arg
, "iso-8859-1")) {
839 *options
|= MPARSE_LATIN1
;
840 *options
&= ~MPARSE_UTF8
;
841 } else if ( ! strcmp(arg
, "us-ascii")) {
842 *options
&= ~(MPARSE_UTF8
| MPARSE_LATIN1
);
844 warnx("-K %s: Bad argument", arg
);
851 moptions(int *options
, char *arg
)
856 else if (0 == strcmp(arg
, "doc"))
857 *options
|= MPARSE_MDOC
;
858 else if (0 == strcmp(arg
, "andoc"))
860 else if (0 == strcmp(arg
, "an"))
861 *options
|= MPARSE_MAN
;
863 warnx("-m %s: Bad argument", arg
);
871 toptions(struct curparse
*curp
, char *arg
)
874 if (0 == strcmp(arg
, "ascii"))
875 curp
->outtype
= OUTT_ASCII
;
876 else if (0 == strcmp(arg
, "lint")) {
877 curp
->outtype
= OUTT_LINT
;
878 curp
->wlevel
= MANDOCLEVEL_WARNING
;
879 } else if (0 == strcmp(arg
, "tree"))
880 curp
->outtype
= OUTT_TREE
;
881 else if (0 == strcmp(arg
, "man"))
882 curp
->outtype
= OUTT_MAN
;
883 else if (0 == strcmp(arg
, "html"))
884 curp
->outtype
= OUTT_HTML
;
885 else if (0 == strcmp(arg
, "utf8"))
886 curp
->outtype
= OUTT_UTF8
;
887 else if (0 == strcmp(arg
, "locale"))
888 curp
->outtype
= OUTT_LOCALE
;
889 else if (0 == strcmp(arg
, "xhtml"))
890 curp
->outtype
= OUTT_HTML
;
891 else if (0 == strcmp(arg
, "ps"))
892 curp
->outtype
= OUTT_PS
;
893 else if (0 == strcmp(arg
, "pdf"))
894 curp
->outtype
= OUTT_PDF
;
896 warnx("-T %s: Bad argument", arg
);
904 woptions(struct curparse
*curp
, char *arg
)
919 switch (getsubopt(&arg
, UNCONST(toks
), &v
)) {
925 curp
->wlevel
= MANDOCLEVEL_WARNING
;
928 curp
->wlevel
= MANDOCLEVEL_ERROR
;
931 curp
->wlevel
= MANDOCLEVEL_UNSUPP
;
934 curp
->wlevel
= MANDOCLEVEL_BADARG
;
937 warnx("-W %s: Bad argument", o
);
946 mmsg(enum mandocerr t
, enum mandoclevel lvl
,
947 const char *file
, int line
, int col
, const char *msg
)
949 const char *mparse_msg
;
951 fprintf(stderr
, "%s: %s:", getprogname(), file
);
954 fprintf(stderr
, "%d:%d:", line
, col
+ 1);
956 fprintf(stderr
, " %s", mparse_strlevel(lvl
));
958 if (NULL
!= (mparse_msg
= mparse_strerror(t
)))
959 fprintf(stderr
, ": %s", mparse_msg
);
962 fprintf(stderr
, ": %s", msg
);
968 spawn_pager(struct tag_files
*tag_files
)
970 #define MAX_PAGER_ARGS 16
971 char *argv
[MAX_PAGER_ARGS
];
978 pager
= getenv("MANPAGER");
979 if (pager
== NULL
|| *pager
== '\0')
980 pager
= getenv("PAGER");
981 if (pager
== NULL
|| *pager
== '\0')
983 cp
= mandoc_strdup(pager
);
986 * Parse the pager command into words.
987 * Intentionally do not do anything fancy here.
991 while (argc
+ 4 < MAX_PAGER_ARGS
) {
993 cp
= strchr(cp
, ' ');
1003 /* For more(1) and less(1), use the tag file. */
1005 if ((cmdlen
= strlen(argv
[0])) >= 4) {
1006 cp
= argv
[0] + cmdlen
- 4;
1007 if (strcmp(cp
, "less") == 0 || strcmp(cp
, "more") == 0) {
1008 argv
[argc
++] = mandoc_strdup("-T");
1009 argv
[argc
++] = tag_files
->tfn
;
1012 argv
[argc
++] = tag_files
->ofn
;
1015 switch (pager_pid
= fork()) {
1017 err((int)MANDOCLEVEL_SYSERR
, "fork");
1022 if (pledge("stdio rpath tmppath", NULL
) == -1)
1023 err((int)MANDOCLEVEL_SYSERR
, "pledge");
1028 /* The child process becomes the pager. */
1030 if (dup2(tag_files
->ofd
, STDOUT_FILENO
) == -1)
1031 err((int)MANDOCLEVEL_SYSERR
, "pager stdout");
1032 close(tag_files
->ofd
);
1033 close(tag_files
->tfd
);
1034 execvp(argv
[0], argv
);
1035 err((int)MANDOCLEVEL_SYSERR
, "exec %s", argv
[0]);