]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
1 /* $Id: main.c,v 1.315 2018/12/30 00:49:55 schwarze Exp $ */
3 * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010-2012, 2014-2018 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/ioctl.h>
23 #include <sys/param.h> /* MACHINE */
24 #include <sys/termios.h>
46 #include "mandoc_aux.h"
48 #include "mandoc_xr.h"
52 #include "mandoc_parse.h"
56 #include "mansearch.h"
67 OUTT_ASCII
= 0, /* -Tascii */
68 OUTT_LOCALE
, /* -Tlocale */
69 OUTT_UTF8
, /* -Tutf8 */
70 OUTT_TREE
, /* -Ttree */
72 OUTT_HTML
, /* -Thtml */
73 OUTT_MARKDOWN
, /* -Tmarkdown */
74 OUTT_LINT
, /* -Tlint */
81 struct manoutput
*outopts
; /* output options */
82 void *outdata
; /* data for output */
83 char *os_s
; /* operating system for display */
84 int wstop
; /* stop after a file with a warning */
85 enum mandoc_os os_e
; /* check base system conventions */
86 enum outt outtype
; /* which output to use */
90 int mandocdb(int, char *[]);
92 static void check_xr(void);
93 static int fs_lookup(const struct manpaths
*,
94 size_t ipath
, const char *,
95 const char *, const char *,
96 struct manpage
**, size_t *);
97 static int fs_search(const struct mansearch
*,
98 const struct manpaths
*, int, char**,
99 struct manpage
**, size_t *);
100 static int koptions(int *, char *);
101 static void moptions(int *, char *);
102 static void outdata_alloc(struct curparse
*);
103 static void parse(struct curparse
*, int, const char *);
104 static void passthrough(const char *, int, int);
105 static pid_t
spawn_pager(struct tag_files
*);
106 static int toptions(struct curparse
*, char *);
107 static void usage(enum argmode
) __attribute__((__noreturn__
));
108 static int woptions(struct curparse
*, char *);
110 static const int sec_prios
[] = {1, 4, 5, 8, 6, 3, 7, 2, 9};
111 static char help_arg
[] = "help";
112 static char *help_argv
[] = {help_arg
, NULL
};
116 main(int argc
, char *argv
[])
119 struct mansearch search
;
120 struct curparse curp
;
122 struct tag_files
*tag_files
;
123 struct manpage
*res
, *resp
;
124 const char *progname
, *sec
, *thisarg
;
125 char *conf_file
, *defpaths
, *auxpaths
;
130 enum outmode outmode
;
137 pid_t pager_pid
, tc_pgid
, man_pgid
, pid
;
140 progname
= getprogname();
143 progname
= mandoc_strdup("mandoc");
144 else if ((progname
= strrchr(argv
[0], '/')) == NULL
)
148 setprogname(progname
);
151 if (strncmp(progname
, "mandocdb", 8) == 0 ||
152 strcmp(progname
, BINM_MAKEWHATIS
) == 0)
153 return mandocdb(argc
, argv
);
156 if (pledge("stdio rpath tmppath tty proc exec", NULL
) == -1)
157 err((int)MANDOCLEVEL_SYSERR
, "pledge");
160 #if HAVE_SANDBOX_INIT
161 if (sandbox_init(kSBXProfileNoInternet
, SANDBOX_NAMED
, NULL
) == -1)
162 errx((int)MANDOCLEVEL_SYSERR
, "sandbox_init");
165 /* Search options. */
167 memset(&conf
, 0, sizeof(conf
));
168 conf_file
= defpaths
= NULL
;
171 memset(&search
, 0, sizeof(struct mansearch
));
172 search
.outkey
= "Nd";
175 if (strcmp(progname
, BINM_MAN
) == 0)
176 search
.argmode
= ARG_NAME
;
177 else if (strcmp(progname
, BINM_APROPOS
) == 0)
178 search
.argmode
= ARG_EXPR
;
179 else if (strcmp(progname
, BINM_WHATIS
) == 0)
180 search
.argmode
= ARG_WORD
;
181 else if (strncmp(progname
, "help", 4) == 0)
182 search
.argmode
= ARG_NAME
;
184 search
.argmode
= ARG_FILE
;
186 /* Parser and formatter options. */
188 memset(&curp
, 0, sizeof(struct curparse
));
189 curp
.outtype
= OUTT_LOCALE
;
190 curp
.outopts
= &conf
.output
;
191 options
= MPARSE_SO
| MPARSE_UTF8
| MPARSE_LATIN1
;
196 outmode
= OUTMODE_DEF
;
198 while ((c
= getopt(argc
, argv
,
199 "aC:cfhI:iK:klM:m:O:S:s:T:VW:w")) != -1) {
200 if (c
== 'i' && search
.argmode
== ARG_EXPR
) {
206 outmode
= OUTMODE_ALL
;
215 search
.argmode
= ARG_WORD
;
218 conf
.output
.synopsisonly
= 1;
220 outmode
= OUTMODE_ALL
;
223 if (strncmp(optarg
, "os=", 3)) {
224 warnx("-I %s: Bad argument", optarg
);
225 return (int)MANDOCLEVEL_BADARG
;
227 if (curp
.os_s
!= NULL
) {
228 warnx("-I %s: Duplicate argument", optarg
);
229 return (int)MANDOCLEVEL_BADARG
;
231 curp
.os_s
= mandoc_strdup(optarg
+ 3);
234 if ( ! koptions(&options
, optarg
))
235 return (int)MANDOCLEVEL_BADARG
;
238 search
.argmode
= ARG_EXPR
;
241 search
.argmode
= ARG_FILE
;
242 outmode
= OUTMODE_ALL
;
254 search
.arch
= optarg
;
260 if ( ! toptions(&curp
, optarg
))
261 return (int)MANDOCLEVEL_BADARG
;
264 if ( ! woptions(&curp
, optarg
))
265 return (int)MANDOCLEVEL_BADARG
;
268 outmode
= OUTMODE_FLN
;
277 usage(search
.argmode
);
279 /* Postprocess options. */
281 if (outmode
== OUTMODE_DEF
) {
282 switch (search
.argmode
) {
284 outmode
= OUTMODE_ALL
;
288 outmode
= OUTMODE_ONE
;
291 outmode
= OUTMODE_LST
;
297 if (outmode
== OUTMODE_LST
)
298 search
.outkey
= oarg
;
300 while (oarg
!= NULL
) {
302 if (manconf_output(&conf
.output
,
303 strsep(&oarg
, ","), 0) == 0)
305 warnx("-O %s: Bad argument", thisarg
);
306 return (int)MANDOCLEVEL_BADARG
;
311 if (curp
.outtype
!= OUTT_TREE
|| !curp
.outopts
->noval
)
312 options
|= MPARSE_VALIDATE
;
314 if (outmode
== OUTMODE_FLN
||
315 outmode
== OUTMODE_LST
||
316 !isatty(STDOUT_FILENO
))
320 (conf
.output
.width
== 0 || conf
.output
.indent
== 0) &&
321 ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &ws
) != -1 &&
323 if (conf
.output
.width
== 0 && ws
.ws_col
< 79)
324 conf
.output
.width
= ws
.ws_col
- 1;
325 if (conf
.output
.indent
== 0 && ws
.ws_col
< 66)
326 conf
.output
.indent
= 3;
331 if (pledge("stdio rpath", NULL
) == -1)
332 err((int)MANDOCLEVEL_SYSERR
, "pledge");
335 /* Parse arguments. */
345 * and for a man(1) section argument without -s.
348 if (search
.argmode
== ARG_NAME
) {
349 if (*progname
== 'h') {
354 } else if (argc
> 1 &&
355 ((uc
= (unsigned char *)argv
[0]) != NULL
) &&
356 ((isdigit(uc
[0]) && (uc
[1] == '\0' ||
357 (isalpha(uc
[1]) && uc
[2] == '\0'))) ||
358 (uc
[0] == 'n' && uc
[1] == '\0'))) {
359 search
.sec
= (char *)uc
;
363 if (search
.arch
== NULL
)
364 search
.arch
= getenv("MACHINE");
366 if (search
.arch
== NULL
)
367 search
.arch
= MACHINE
;
371 /* man(1), whatis(1), apropos(1) */
373 if (search
.argmode
!= ARG_FILE
) {
374 if (search
.argmode
== ARG_NAME
&&
375 outmode
== OUTMODE_ONE
)
376 search
.firstmatch
= 1;
378 /* Access the mandoc database. */
380 manconf_parse(&conf
, conf_file
, defpaths
, auxpaths
);
381 if ( ! mansearch(&search
, &conf
.manpath
,
382 argc
, argv
, &res
, &sz
))
383 usage(search
.argmode
);
385 if (sz
== 0 && search
.argmode
== ARG_NAME
)
386 fs_search(&search
, &conf
.manpath
,
387 argc
, argv
, &res
, &sz
);
389 if (search
.argmode
== ARG_NAME
) {
390 for (c
= 0; c
< argc
; c
++) {
391 if (strchr(argv
[c
], '/') == NULL
)
393 if (access(argv
[c
], R_OK
) == -1) {
397 res
= mandoc_reallocarray(res
,
398 sz
+ 1, sizeof(*res
));
399 res
[sz
].file
= mandoc_strdup(argv
[c
]);
400 res
[sz
].names
= NULL
;
401 res
[sz
].output
= NULL
;
402 res
[sz
].ipath
= SIZE_MAX
;
404 res
[sz
].form
= FORM_SRC
;
410 if (search
.argmode
!= ARG_NAME
)
411 warnx("nothing appropriate");
412 mandoc_msg_setrc(MANDOCLEVEL_BADARG
);
417 * For standard man(1) and -a output mode,
418 * prepare for copying filename pointers
419 * into the program parameter array.
422 if (outmode
== OUTMODE_ONE
) {
425 } else if (outmode
== OUTMODE_ALL
)
428 /* Iterate all matching manuals. */
431 for (i
= 0; i
< sz
; i
++) {
432 if (outmode
== OUTMODE_FLN
)
434 else if (outmode
== OUTMODE_LST
)
435 printf("%s - %s\n", res
[i
].names
,
436 res
[i
].output
== NULL
? "" :
438 else if (outmode
== OUTMODE_ONE
) {
439 /* Search for the best section. */
441 sec
+= strcspn(sec
, "123456789");
444 prio
= sec_prios
[sec
[0] - '1'];
447 if (prio
>= best_prio
)
455 * For man(1), -a and -i output mode, fall through
456 * to the main mandoc(1) code iterating files
457 * and running the parsers on each of them.
460 if (outmode
== OUTMODE_FLN
|| outmode
== OUTMODE_LST
)
468 if (pledge("stdio rpath tmppath tty proc exec", NULL
) == -1)
469 err((int)MANDOCLEVEL_SYSERR
, "pledge");
471 if (pledge("stdio rpath", NULL
) == -1)
472 err((int)MANDOCLEVEL_SYSERR
, "pledge");
476 if (search
.argmode
== ARG_FILE
)
477 moptions(&options
, auxpaths
);
480 curp
.mp
= mparse_alloc(options
, curp
.os_e
, curp
.os_s
);
484 tag_files
= tag_init();
486 mandoc_msg_setinfilename(thisarg
);
487 parse(&curp
, STDIN_FILENO
, thisarg
);
488 mandoc_msg_setinfilename(NULL
);
492 * Remember the original working directory, if possible.
493 * This will be needed if some names on the command line
494 * are page names and some are relative file names.
495 * Do not error out if the current directory is not
496 * readable: Maybe it won't be needed after all.
498 startdir
= open(".", O_RDONLY
| O_DIRECTORY
);
503 * Changing directories is not needed in ARG_FILE mode.
504 * Do it on a best-effort basis. Even in case of
505 * failure, some functionality may still work.
508 if (resp
->ipath
!= SIZE_MAX
)
509 (void)chdir(conf
.manpath
.paths
[resp
->ipath
]);
510 else if (startdir
!= -1)
511 (void)fchdir(startdir
);
512 thisarg
= resp
->file
;
516 fd
= mparse_open(curp
.mp
, thisarg
);
520 tag_files
= tag_init();
521 if (conf
.output
.tag
!= NULL
&&
522 tag_files
->tagname
== NULL
)
524 *conf
.output
.tag
!= '\0' ?
525 conf
.output
.tag
: *argv
;
528 mandoc_msg_setinfilename(thisarg
);
529 if (resp
== NULL
|| resp
->form
== FORM_SRC
)
530 parse(&curp
, fd
, thisarg
);
532 passthrough(resp
->file
, fd
,
533 conf
.output
.synopsisonly
);
534 mandoc_msg_setinfilename(NULL
);
536 if (ferror(stdout
)) {
537 if (tag_files
!= NULL
) {
538 warn("%s", tag_files
->ofn
);
543 mandoc_msg_setrc(MANDOCLEVEL_SYSERR
);
547 if (argc
> 1 && curp
.outtype
<= OUTT_UTF8
) {
548 if (curp
.outdata
== NULL
)
549 outdata_alloc(&curp
);
550 terminal_sepline(curp
.outdata
);
553 mandoc_msg(MANDOCERR_FILE
, 0, 0,
554 "%s", strerror(errno
));
556 if (curp
.wstop
&& mandoc_msg_getrc() != MANDOCLEVEL_OK
)
564 mparse_reset(curp
.mp
);
566 if (startdir
!= -1) {
567 (void)fchdir(startdir
);
571 if (curp
.outdata
!= NULL
) {
572 switch (curp
.outtype
) {
574 html_free(curp
.outdata
);
579 ascii_free(curp
.outdata
);
583 pspdf_free(curp
.outdata
);
590 mparse_free(curp
.mp
);
594 if (search
.argmode
!= ARG_FILE
) {
596 mansearch_free(res
, sz
);
602 * When using a pager, finish writing both temporary files,
603 * fork it, wait for the user to close it, and clean up.
606 if (tag_files
!= NULL
) {
609 man_pgid
= getpgid(0);
610 tag_files
->tcpgid
= man_pgid
== getpid() ?
611 getpgid(getppid()) : man_pgid
;
616 /* Stop here until moved to the foreground. */
618 tc_pgid
= tcgetpgrp(tag_files
->ofd
);
619 if (tc_pgid
!= man_pgid
) {
620 if (tc_pgid
== pager_pid
) {
621 (void)tcsetpgrp(tag_files
->ofd
,
623 if (signum
== SIGTTIN
)
626 tag_files
->tcpgid
= tc_pgid
;
631 /* Once in the foreground, activate the pager. */
634 (void)tcsetpgrp(tag_files
->ofd
, pager_pid
);
635 kill(pager_pid
, SIGCONT
);
637 pager_pid
= spawn_pager(tag_files
);
639 /* Wait for the pager to stop or exit. */
641 while ((pid
= waitpid(pager_pid
, &status
,
642 WUNTRACED
)) == -1 && errno
== EINTR
)
647 mandoc_msg_setrc(MANDOCLEVEL_SYSERR
);
650 if (!WIFSTOPPED(status
))
653 signum
= WSTOPSIG(status
);
657 return (int)mandoc_msg_getrc();
661 usage(enum argmode argmode
)
666 fputs("usage: mandoc [-ac] [-I os=name] "
667 "[-K encoding] [-mdoc | -man] [-O options]\n"
668 "\t [-T output] [-W level] [file ...]\n", stderr
);
671 fputs("usage: man [-acfhklw] [-C file] [-M path] "
672 "[-m path] [-S subsection]\n"
673 "\t [[-s] section] name ...\n", stderr
);
676 fputs("usage: whatis [-afk] [-C file] "
677 "[-M path] [-m path] [-O outkey] [-S arch]\n"
678 "\t [-s section] name ...\n", stderr
);
681 fputs("usage: apropos [-afk] [-C file] "
682 "[-M path] [-m path] [-O outkey] [-S arch]\n"
683 "\t [-s section] expression ...\n", stderr
);
686 exit((int)MANDOCLEVEL_BADARG
);
690 fs_lookup(const struct manpaths
*paths
, size_t ipath
,
691 const char *sec
, const char *arch
, const char *name
,
692 struct manpage
**res
, size_t *ressz
)
695 struct manpage
*page
;
701 mandoc_asprintf(&file
, "%s/man%s/%s.%s",
702 paths
->paths
[ipath
], sec
, name
, sec
);
703 if (access(file
, R_OK
) != -1)
707 mandoc_asprintf(&file
, "%s/cat%s/%s.0",
708 paths
->paths
[ipath
], sec
, name
);
709 if (access(file
, R_OK
) != -1) {
716 mandoc_asprintf(&file
, "%s/man%s/%s/%s.%s",
717 paths
->paths
[ipath
], sec
, arch
, name
, sec
);
718 if (access(file
, R_OK
) != -1)
723 mandoc_asprintf(&file
, "%s/man%s/%s.[01-9]*",
724 paths
->paths
[ipath
], sec
, name
);
725 globres
= glob(file
, 0, NULL
, &globinfo
);
726 if (globres
!= 0 && globres
!= GLOB_NOMATCH
)
727 warn("%s: glob", file
);
730 file
= mandoc_strdup(*globinfo
.gl_pathv
);
734 if (res
!= NULL
|| ipath
+ 1 != paths
->sz
)
737 mandoc_asprintf(&file
, "%s.%s", name
, sec
);
738 globres
= access(file
, R_OK
);
740 return globres
!= -1;
743 warnx("outdated mandoc.db lacks %s(%s) entry, run %s %s",
744 name
, sec
, BINM_MAKEWHATIS
, paths
->paths
[ipath
]);
749 *res
= mandoc_reallocarray(*res
, ++*ressz
, sizeof(struct manpage
));
750 page
= *res
+ (*ressz
- 1);
755 page
->sec
= (*sec
>= '1' && *sec
<= '9') ? *sec
- '1' + 1 : 10;
761 fs_search(const struct mansearch
*cfg
, const struct manpaths
*paths
,
762 int argc
, char **argv
, struct manpage
**res
, size_t *ressz
)
764 const char *const sections
[] =
765 {"1", "8", "6", "2", "3", "5", "7", "4", "9", "3p"};
766 const size_t nsec
= sizeof(sections
)/sizeof(sections
[0]);
768 size_t ipath
, isec
, lastsz
;
770 assert(cfg
->argmode
== ARG_NAME
);
776 for (ipath
= 0; ipath
< paths
->sz
; ipath
++) {
777 if (cfg
->sec
!= NULL
) {
778 if (fs_lookup(paths
, ipath
, cfg
->sec
,
779 cfg
->arch
, *argv
, res
, ressz
) &&
782 } else for (isec
= 0; isec
< nsec
; isec
++)
783 if (fs_lookup(paths
, ipath
, sections
[isec
],
784 cfg
->arch
, *argv
, res
, ressz
) &&
788 if (res
!= NULL
&& *ressz
== lastsz
&&
789 strchr(*argv
, '/') == NULL
) {
790 if (cfg
->sec
== NULL
)
791 warnx("No entry for %s in the manual.",
794 warnx("No entry for %s in section %s "
795 "of the manual.", *argv
, cfg
->sec
);
805 parse(struct curparse
*curp
, int fd
, const char *file
)
807 struct roff_meta
*meta
;
809 /* Begin by parsing the file itself. */
814 mparse_readfd(curp
->mp
, fd
, file
);
815 if (fd
!= STDIN_FILENO
)
819 * With -Wstop and warnings or errors of at least the requested
820 * level, do not produce output.
823 if (curp
->wstop
&& mandoc_msg_getrc() != MANDOCLEVEL_OK
)
826 if (curp
->outdata
== NULL
)
830 meta
= mparse_result(curp
->mp
);
832 /* Execute the out device, if it exists. */
834 if (meta
->macroset
== MACROSET_MDOC
) {
835 switch (curp
->outtype
) {
837 html_mdoc(curp
->outdata
, meta
);
840 tree_mdoc(curp
->outdata
, meta
);
843 man_mdoc(curp
->outdata
, meta
);
850 terminal_mdoc(curp
->outdata
, meta
);
853 markdown_mdoc(curp
->outdata
, meta
);
859 if (meta
->macroset
== MACROSET_MAN
) {
860 switch (curp
->outtype
) {
862 html_man(curp
->outdata
, meta
);
865 tree_man(curp
->outdata
, meta
);
868 mparse_copy(curp
->mp
);
875 terminal_man(curp
->outdata
, meta
);
881 if (mandoc_msg_getmin() < MANDOCERR_STYLE
)
888 static struct manpaths paths
;
889 struct mansearch search
;
890 struct mandoc_xr
*xr
;
894 manpath_base(&paths
);
896 for (xr
= mandoc_xr_get(); xr
!= NULL
; xr
= xr
->next
) {
900 search
.sec
= xr
->sec
;
901 search
.outkey
= NULL
;
902 search
.argmode
= ARG_NAME
;
903 search
.firstmatch
= 1;
904 if (mansearch(&search
, &paths
, 1, &xr
->name
, NULL
, &sz
))
906 if (fs_search(&search
, &paths
, 1, &xr
->name
, NULL
, &sz
))
909 mandoc_msg(MANDOCERR_XR_BAD
, xr
->line
,
910 xr
->pos
+ 1, "Xr %s %s", xr
->name
, xr
->sec
);
912 mandoc_msg(MANDOCERR_XR_BAD
, xr
->line
,
913 xr
->pos
+ 1, "Xr %s %s (%d times)",
914 xr
->name
, xr
->sec
, xr
->count
);
919 outdata_alloc(struct curparse
*curp
)
921 switch (curp
->outtype
) {
923 curp
->outdata
= html_alloc(curp
->outopts
);
926 curp
->outdata
= utf8_alloc(curp
->outopts
);
929 curp
->outdata
= locale_alloc(curp
->outopts
);
932 curp
->outdata
= ascii_alloc(curp
->outopts
);
935 curp
->outdata
= pdf_alloc(curp
->outopts
);
938 curp
->outdata
= ps_alloc(curp
->outopts
);
946 passthrough(const char *file
, int fd
, int synopsis_only
)
948 const char synb
[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
949 const char synr
[] = "SYNOPSIS";
955 ssize_t len
, written
;
961 if (fflush(stdout
) == EOF
) {
966 if ((stream
= fdopen(fd
, "r")) == NULL
) {
973 while ((len
= getline(&line
, &linesz
, stream
)) != -1) {
977 if ( ! isspace((unsigned char)*cp
))
979 while (isspace((unsigned char)*cp
)) {
984 if (strcmp(cp
, synb
) == 0 ||
985 strcmp(cp
, synr
) == 0)
990 for (; len
> 0; len
-= written
) {
991 if ((written
= write(STDOUT_FILENO
, cp
, len
)) != -1)
999 if (ferror(stream
)) {
1001 syscall
= "getline";
1012 warn("%s: SYSERR: %s", file
, syscall
);
1013 mandoc_msg_setrc(MANDOCLEVEL_SYSERR
);
1017 koptions(int *options
, char *arg
)
1020 if ( ! strcmp(arg
, "utf-8")) {
1021 *options
|= MPARSE_UTF8
;
1022 *options
&= ~MPARSE_LATIN1
;
1023 } else if ( ! strcmp(arg
, "iso-8859-1")) {
1024 *options
|= MPARSE_LATIN1
;
1025 *options
&= ~MPARSE_UTF8
;
1026 } else if ( ! strcmp(arg
, "us-ascii")) {
1027 *options
&= ~(MPARSE_UTF8
| MPARSE_LATIN1
);
1029 warnx("-K %s: Bad argument", arg
);
1036 moptions(int *options
, char *arg
)
1041 if (strcmp(arg
, "doc") == 0)
1042 *options
|= MPARSE_MDOC
;
1043 else if (strcmp(arg
, "an") == 0)
1044 *options
|= MPARSE_MAN
;
1048 toptions(struct curparse
*curp
, char *arg
)
1051 if (0 == strcmp(arg
, "ascii"))
1052 curp
->outtype
= OUTT_ASCII
;
1053 else if (0 == strcmp(arg
, "lint")) {
1054 curp
->outtype
= OUTT_LINT
;
1055 mandoc_msg_setoutfile(stdout
);
1056 mandoc_msg_setmin(MANDOCERR_BASE
);
1057 } else if (0 == strcmp(arg
, "tree"))
1058 curp
->outtype
= OUTT_TREE
;
1059 else if (0 == strcmp(arg
, "man"))
1060 curp
->outtype
= OUTT_MAN
;
1061 else if (0 == strcmp(arg
, "html"))
1062 curp
->outtype
= OUTT_HTML
;
1063 else if (0 == strcmp(arg
, "markdown"))
1064 curp
->outtype
= OUTT_MARKDOWN
;
1065 else if (0 == strcmp(arg
, "utf8"))
1066 curp
->outtype
= OUTT_UTF8
;
1067 else if (0 == strcmp(arg
, "locale"))
1068 curp
->outtype
= OUTT_LOCALE
;
1069 else if (0 == strcmp(arg
, "ps"))
1070 curp
->outtype
= OUTT_PS
;
1071 else if (0 == strcmp(arg
, "pdf"))
1072 curp
->outtype
= OUTT_PDF
;
1074 warnx("-T %s: Bad argument", arg
);
1082 woptions(struct curparse
*curp
, char *arg
)
1085 const char *toks
[11];
1091 toks
[4] = "warning";
1095 toks
[8] = "openbsd";
1101 switch (getsubopt(&arg
, (char * const *)toks
, &v
)) {
1107 mandoc_msg_setmin(MANDOCERR_BASE
);
1110 mandoc_msg_setmin(MANDOCERR_STYLE
);
1113 mandoc_msg_setmin(MANDOCERR_WARNING
);
1116 mandoc_msg_setmin(MANDOCERR_ERROR
);
1119 mandoc_msg_setmin(MANDOCERR_UNSUPP
);
1122 mandoc_msg_setmin(MANDOCERR_MAX
);
1125 mandoc_msg_setmin(MANDOCERR_BASE
);
1126 curp
->os_e
= MANDOC_OS_OPENBSD
;
1129 mandoc_msg_setmin(MANDOCERR_BASE
);
1130 curp
->os_e
= MANDOC_OS_NETBSD
;
1133 warnx("-W %s: Bad argument", o
);
1141 spawn_pager(struct tag_files
*tag_files
)
1143 const struct timespec timeout
= { 0, 100000000 }; /* 0.1s */
1144 #define MAX_PAGER_ARGS 16
1145 char *argv
[MAX_PAGER_ARGS
];
1152 pager
= getenv("MANPAGER");
1153 if (pager
== NULL
|| *pager
== '\0')
1154 pager
= getenv("PAGER");
1155 if (pager
== NULL
|| *pager
== '\0')
1157 cp
= mandoc_strdup(pager
);
1160 * Parse the pager command into words.
1161 * Intentionally do not do anything fancy here.
1165 while (argc
+ 5 < MAX_PAGER_ARGS
) {
1167 cp
= strchr(cp
, ' ');
1177 /* For less(1), use the tag file. */
1180 if ((cmdlen
= strlen(argv
[0])) >= 4) {
1181 cp
= argv
[0] + cmdlen
- 4;
1182 if (strcmp(cp
, "less") == 0) {
1183 argv
[argc
++] = mandoc_strdup("-T");
1184 argv
[argc
++] = tag_files
->tfn
;
1185 if (tag_files
->tagname
!= NULL
) {
1186 argv
[argc
++] = mandoc_strdup("-t");
1187 argv
[argc
++] = tag_files
->tagname
;
1193 argv
[argc
++] = tag_files
->ofn
;
1196 switch (pager_pid
= fork()) {
1198 err((int)MANDOCLEVEL_SYSERR
, "fork");
1202 (void)setpgid(pager_pid
, 0);
1203 (void)tcsetpgrp(tag_files
->ofd
, pager_pid
);
1205 if (pledge("stdio rpath tmppath tty proc", NULL
) == -1)
1206 err((int)MANDOCLEVEL_SYSERR
, "pledge");
1208 tag_files
->pager_pid
= pager_pid
;
1212 /* The child process becomes the pager. */
1214 if (dup2(tag_files
->ofd
, STDOUT_FILENO
) == -1)
1215 err((int)MANDOCLEVEL_SYSERR
, "pager stdout");
1216 close(tag_files
->ofd
);
1217 assert(tag_files
->tfd
== -1);
1219 /* Do not start the pager before controlling the terminal. */
1221 while (tcgetpgrp(STDOUT_FILENO
) != getpid())
1222 nanosleep(&timeout
, NULL
);
1224 execvp(argv
[0], argv
);
1225 err((int)MANDOCLEVEL_SYSERR
, "exec %s", argv
[0]);