]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
1 /* $Id: main.c,v 1.293 2017/06/24 14:38:32 schwarze Exp $ */
3 * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010-2012, 2014-2017 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 */
44 #include "mandoc_aux.h"
52 #include "mansearch.h"
63 OUTT_ASCII
= 0, /* -Tascii */
64 OUTT_LOCALE
, /* -Tlocale */
65 OUTT_UTF8
, /* -Tutf8 */
66 OUTT_TREE
, /* -Ttree */
68 OUTT_HTML
, /* -Thtml */
69 OUTT_MARKDOWN
, /* -Tmarkdown */
70 OUTT_LINT
, /* -Tlint */
77 struct manoutput
*outopts
; /* output options */
78 void *outdata
; /* data for output */
79 char *os_s
; /* operating system for display */
80 int wstop
; /* stop after a file with a warning */
81 enum mandocerr mmin
; /* ignore messages below this */
82 enum mandoc_os os_e
; /* check base system conventions */
83 enum outt outtype
; /* which output to use */
87 int mandocdb(int, char *[]);
89 static int fs_lookup(const struct manpaths
*,
90 size_t ipath
, const char *,
91 const char *, const char *,
92 struct manpage
**, size_t *);
93 static void fs_search(const struct mansearch
*,
94 const struct manpaths
*, int, char**,
95 struct manpage
**, size_t *);
96 static int koptions(int *, char *);
97 static void moptions(int *, char *);
98 static void mmsg(enum mandocerr
, enum mandoclevel
,
99 const char *, int, int, const char *);
100 static void outdata_alloc(struct curparse
*);
101 static void parse(struct curparse
*, int, const char *);
102 static void passthrough(const char *, int, int);
103 static pid_t
spawn_pager(struct tag_files
*);
104 static int toptions(struct curparse
*, char *);
105 static void usage(enum argmode
) __attribute__((__noreturn__
));
106 static int woptions(struct curparse
*, char *);
108 static const int sec_prios
[] = {1, 4, 5, 8, 6, 3, 7, 2, 9};
109 static char help_arg
[] = "help";
110 static char *help_argv
[] = {help_arg
, NULL
};
111 static enum mandoclevel rc
;
115 main(int argc
, char *argv
[])
118 struct mansearch search
;
119 struct curparse curp
;
120 struct tag_files
*tag_files
;
121 struct manpage
*res
, *resp
;
122 const char *progname
, *sec
, *thisarg
;
123 char *conf_file
, *defpaths
, *auxpaths
;
128 enum outmode outmode
;
135 pid_t pager_pid
, tc_pgid
, man_pgid
, pid
;
138 progname
= getprogname();
141 progname
= mandoc_strdup("mandoc");
142 else if ((progname
= strrchr(argv
[0], '/')) == NULL
)
146 setprogname(progname
);
149 if (strncmp(progname
, "mandocdb", 8) == 0 ||
150 strcmp(progname
, BINM_MAKEWHATIS
) == 0)
151 return mandocdb(argc
, argv
);
154 if (pledge("stdio rpath tmppath tty proc exec", NULL
) == -1)
155 err((int)MANDOCLEVEL_SYSERR
, "pledge");
158 #if HAVE_SANDBOX_INIT
159 if (sandbox_init(kSBXProfileNoInternet
, SANDBOX_NAMED
, NULL
) == -1)
160 errx((int)MANDOCLEVEL_SYSERR
, "sandbox_init");
163 /* Search options. */
165 memset(&conf
, 0, sizeof(conf
));
166 conf_file
= defpaths
= NULL
;
169 memset(&search
, 0, sizeof(struct mansearch
));
170 search
.outkey
= "Nd";
173 if (strcmp(progname
, BINM_MAN
) == 0)
174 search
.argmode
= ARG_NAME
;
175 else if (strcmp(progname
, BINM_APROPOS
) == 0)
176 search
.argmode
= ARG_EXPR
;
177 else if (strcmp(progname
, BINM_WHATIS
) == 0)
178 search
.argmode
= ARG_WORD
;
179 else if (strncmp(progname
, "help", 4) == 0)
180 search
.argmode
= ARG_NAME
;
182 search
.argmode
= ARG_FILE
;
184 /* Parser and formatter options. */
186 memset(&curp
, 0, sizeof(struct curparse
));
187 curp
.outtype
= OUTT_LOCALE
;
188 curp
.mmin
= MANDOCERR_MAX
;
189 curp
.outopts
= &conf
.output
;
190 options
= MPARSE_SO
| MPARSE_UTF8
| MPARSE_LATIN1
;
195 outmode
= OUTMODE_DEF
;
197 while ((c
= getopt(argc
, argv
,
198 "aC:cfhI:iK:klM:m:O:S:s:T:VW:w")) != -1) {
199 if (c
== 'i' && search
.argmode
== ARG_EXPR
) {
205 outmode
= OUTMODE_ALL
;
214 search
.argmode
= ARG_WORD
;
217 conf
.output
.synopsisonly
= 1;
219 outmode
= OUTMODE_ALL
;
222 if (strncmp(optarg
, "os=", 3)) {
223 warnx("-I %s: Bad argument", optarg
);
224 return (int)MANDOCLEVEL_BADARG
;
226 if (curp
.os_s
!= NULL
) {
227 warnx("-I %s: Duplicate argument", optarg
);
228 return (int)MANDOCLEVEL_BADARG
;
230 curp
.os_s
= mandoc_strdup(optarg
+ 3);
233 if ( ! koptions(&options
, optarg
))
234 return (int)MANDOCLEVEL_BADARG
;
237 search
.argmode
= ARG_EXPR
;
240 search
.argmode
= ARG_FILE
;
241 outmode
= OUTMODE_ALL
;
253 search
.arch
= optarg
;
259 if ( ! toptions(&curp
, optarg
))
260 return (int)MANDOCLEVEL_BADARG
;
263 if ( ! woptions(&curp
, optarg
))
264 return (int)MANDOCLEVEL_BADARG
;
267 outmode
= OUTMODE_FLN
;
276 usage(search
.argmode
);
278 /* Postprocess options. */
280 if (outmode
== OUTMODE_DEF
) {
281 switch (search
.argmode
) {
283 outmode
= OUTMODE_ALL
;
287 outmode
= OUTMODE_ONE
;
290 outmode
= OUTMODE_LST
;
296 if (outmode
== OUTMODE_LST
)
297 search
.outkey
= oarg
;
299 while (oarg
!= NULL
) {
301 if (manconf_output(&conf
.output
,
302 strsep(&oarg
, ","), 0) == 0)
304 warnx("-O %s: Bad argument", thisarg
);
305 return (int)MANDOCLEVEL_BADARG
;
310 if (outmode
== OUTMODE_FLN
||
311 outmode
== OUTMODE_LST
||
312 !isatty(STDOUT_FILENO
))
317 if (pledge("stdio rpath", NULL
) == -1)
318 err((int)MANDOCLEVEL_SYSERR
, "pledge");
321 /* Parse arguments. */
331 * and for a man(1) section argument without -s.
334 if (search
.argmode
== ARG_NAME
) {
335 if (*progname
== 'h') {
340 } else if (argc
> 1 &&
341 ((uc
= (unsigned char *)argv
[0]) != NULL
) &&
342 ((isdigit(uc
[0]) && (uc
[1] == '\0' ||
343 (isalpha(uc
[1]) && uc
[2] == '\0'))) ||
344 (uc
[0] == 'n' && uc
[1] == '\0'))) {
345 search
.sec
= (char *)uc
;
349 if (search
.arch
== NULL
)
350 search
.arch
= getenv("MACHINE");
352 if (search
.arch
== NULL
)
353 search
.arch
= MACHINE
;
359 /* man(1), whatis(1), apropos(1) */
361 if (search
.argmode
!= ARG_FILE
) {
362 if (search
.argmode
== ARG_NAME
&&
363 outmode
== OUTMODE_ONE
)
364 search
.firstmatch
= 1;
366 /* Access the mandoc database. */
368 manconf_parse(&conf
, conf_file
, defpaths
, auxpaths
);
369 if ( ! mansearch(&search
, &conf
.manpath
,
370 argc
, argv
, &res
, &sz
))
371 usage(search
.argmode
);
374 if (search
.argmode
== ARG_NAME
)
375 fs_search(&search
, &conf
.manpath
,
376 argc
, argv
, &res
, &sz
);
378 warnx("nothing appropriate");
382 rc
= MANDOCLEVEL_BADARG
;
387 * For standard man(1) and -a output mode,
388 * prepare for copying filename pointers
389 * into the program parameter array.
392 if (outmode
== OUTMODE_ONE
) {
395 } else if (outmode
== OUTMODE_ALL
)
398 /* Iterate all matching manuals. */
401 for (i
= 0; i
< sz
; i
++) {
402 if (outmode
== OUTMODE_FLN
)
404 else if (outmode
== OUTMODE_LST
)
405 printf("%s - %s\n", res
[i
].names
,
406 res
[i
].output
== NULL
? "" :
408 else if (outmode
== OUTMODE_ONE
) {
409 /* Search for the best section. */
411 sec
+= strcspn(sec
, "123456789");
414 prio
= sec_prios
[sec
[0] - '1'];
417 if (prio
>= best_prio
)
425 * For man(1), -a and -i output mode, fall through
426 * to the main mandoc(1) code iterating files
427 * and running the parsers on each of them.
430 if (outmode
== OUTMODE_FLN
|| outmode
== OUTMODE_LST
)
438 if (pledge("stdio rpath tmppath tty proc exec", NULL
) == -1)
439 err((int)MANDOCLEVEL_SYSERR
, "pledge");
441 if (pledge("stdio rpath", NULL
) == -1)
442 err((int)MANDOCLEVEL_SYSERR
, "pledge");
446 if (search
.argmode
== ARG_FILE
)
447 moptions(&options
, auxpaths
);
450 curp
.mp
= mparse_alloc(options
, curp
.mmin
, mmsg
,
451 curp
.os_e
, curp
.os_s
);
454 * Conditionally start up the lookaside buffer before parsing.
456 if (OUTT_MAN
== curp
.outtype
)
457 mparse_keep(curp
.mp
);
461 tag_files
= tag_init();
462 parse(&curp
, STDIN_FILENO
, "<stdin>");
466 fd
= mparse_open(curp
.mp
, resp
!= NULL
? resp
->file
: *argv
);
469 tag_files
= tag_init();
474 parse(&curp
, fd
, *argv
);
475 else if (resp
->form
== FORM_SRC
) {
476 /* For .so only; ignore failure. */
477 chdir(conf
.manpath
.paths
[resp
->ipath
]);
478 parse(&curp
, fd
, resp
->file
);
480 passthrough(resp
->file
, fd
,
481 conf
.output
.synopsisonly
);
483 if (argc
> 1 && curp
.outtype
<= OUTT_UTF8
) {
484 if (curp
.outdata
== NULL
)
485 outdata_alloc(&curp
);
486 terminal_sepline(curp
.outdata
);
488 } else if (rc
< MANDOCLEVEL_ERROR
)
489 rc
= MANDOCLEVEL_ERROR
;
491 if (MANDOCLEVEL_OK
!= rc
&& curp
.wstop
)
499 mparse_reset(curp
.mp
);
502 if (curp
.outdata
!= NULL
) {
503 switch (curp
.outtype
) {
505 html_free(curp
.outdata
);
510 ascii_free(curp
.outdata
);
514 pspdf_free(curp
.outdata
);
520 mparse_free(curp
.mp
);
524 if (search
.argmode
!= ARG_FILE
) {
526 mansearch_free(res
, sz
);
532 * When using a pager, finish writing both temporary files,
533 * fork it, wait for the user to close it, and clean up.
536 if (tag_files
!= NULL
) {
539 man_pgid
= getpgid(0);
540 tag_files
->tcpgid
= man_pgid
== getpid() ?
541 getpgid(getppid()) : man_pgid
;
546 /* Stop here until moved to the foreground. */
548 tc_pgid
= tcgetpgrp(tag_files
->ofd
);
549 if (tc_pgid
!= man_pgid
) {
550 if (tc_pgid
== pager_pid
) {
551 (void)tcsetpgrp(tag_files
->ofd
,
553 if (signum
== SIGTTIN
)
556 tag_files
->tcpgid
= tc_pgid
;
561 /* Once in the foreground, activate the pager. */
564 (void)tcsetpgrp(tag_files
->ofd
, pager_pid
);
565 kill(pager_pid
, SIGCONT
);
567 pager_pid
= spawn_pager(tag_files
);
569 /* Wait for the pager to stop or exit. */
571 while ((pid
= waitpid(pager_pid
, &status
,
572 WUNTRACED
)) == -1 && errno
== EINTR
)
577 rc
= MANDOCLEVEL_SYSERR
;
580 if (!WIFSTOPPED(status
))
583 signum
= WSTOPSIG(status
);
592 usage(enum argmode argmode
)
597 fputs("usage: mandoc [-ac] [-I os=name] "
598 "[-K encoding] [-mdoc | -man] [-O options]\n"
599 "\t [-T output] [-W level] [file ...]\n", stderr
);
602 fputs("usage: man [-acfhklw] [-C file] [-M path] "
603 "[-m path] [-S subsection]\n"
604 "\t [[-s] section] name ...\n", stderr
);
607 fputs("usage: whatis [-afk] [-C file] "
608 "[-M path] [-m path] [-O outkey] [-S arch]\n"
609 "\t [-s section] name ...\n", stderr
);
612 fputs("usage: apropos [-afk] [-C file] "
613 "[-M path] [-m path] [-O outkey] [-S arch]\n"
614 "\t [-s section] expression ...\n", stderr
);
617 exit((int)MANDOCLEVEL_BADARG
);
621 fs_lookup(const struct manpaths
*paths
, size_t ipath
,
622 const char *sec
, const char *arch
, const char *name
,
623 struct manpage
**res
, size_t *ressz
)
626 struct manpage
*page
;
632 mandoc_asprintf(&file
, "%s/man%s/%s.%s",
633 paths
->paths
[ipath
], sec
, name
, sec
);
634 if (access(file
, R_OK
) != -1)
638 mandoc_asprintf(&file
, "%s/cat%s/%s.0",
639 paths
->paths
[ipath
], sec
, name
);
640 if (access(file
, R_OK
) != -1) {
647 mandoc_asprintf(&file
, "%s/man%s/%s/%s.%s",
648 paths
->paths
[ipath
], sec
, arch
, name
, sec
);
649 if (access(file
, R_OK
) != -1)
654 mandoc_asprintf(&file
, "%s/man%s/%s.[01-9]*",
655 paths
->paths
[ipath
], sec
, name
);
656 globres
= glob(file
, 0, NULL
, &globinfo
);
657 if (globres
!= 0 && globres
!= GLOB_NOMATCH
)
658 warn("%s: glob", file
);
661 file
= mandoc_strdup(*globinfo
.gl_pathv
);
667 warnx("outdated mandoc.db lacks %s(%s) entry, run %s %s",
668 name
, sec
, BINM_MAKEWHATIS
, paths
->paths
[ipath
]);
669 *res
= mandoc_reallocarray(*res
, ++*ressz
, sizeof(struct manpage
));
670 page
= *res
+ (*ressz
- 1);
675 page
->bits
= NAME_FILE
& NAME_MASK
;
676 page
->sec
= (*sec
>= '1' && *sec
<= '9') ? *sec
- '1' + 1 : 10;
682 fs_search(const struct mansearch
*cfg
, const struct manpaths
*paths
,
683 int argc
, char **argv
, struct manpage
**res
, size_t *ressz
)
685 const char *const sections
[] =
686 {"1", "8", "6", "2", "3", "5", "7", "4", "9", "3p"};
687 const size_t nsec
= sizeof(sections
)/sizeof(sections
[0]);
689 size_t ipath
, isec
, lastsz
;
691 assert(cfg
->argmode
== ARG_NAME
);
696 for (ipath
= 0; ipath
< paths
->sz
; ipath
++) {
697 if (cfg
->sec
!= NULL
) {
698 if (fs_lookup(paths
, ipath
, cfg
->sec
,
699 cfg
->arch
, *argv
, res
, ressz
) &&
702 } else for (isec
= 0; isec
< nsec
; isec
++)
703 if (fs_lookup(paths
, ipath
, sections
[isec
],
704 cfg
->arch
, *argv
, res
, ressz
) &&
708 if (*ressz
== lastsz
)
709 warnx("No entry for %s in the manual.", *argv
);
717 parse(struct curparse
*curp
, int fd
, const char *file
)
719 enum mandoclevel rctmp
;
720 struct roff_man
*man
;
722 /* Begin by parsing the file itself. */
727 rctmp
= mparse_readfd(curp
->mp
, fd
, file
);
728 if (fd
!= STDIN_FILENO
)
734 * With -Wstop and warnings or errors of at least the requested
735 * level, do not produce output.
738 if (rctmp
!= MANDOCLEVEL_OK
&& curp
->wstop
)
741 if (curp
->outdata
== NULL
)
744 mparse_result(curp
->mp
, &man
, NULL
);
746 /* Execute the out device, if it exists. */
750 if (man
->macroset
== MACROSET_MDOC
) {
751 if (curp
->outtype
!= OUTT_TREE
|| !curp
->outopts
->noval
)
753 switch (curp
->outtype
) {
755 html_mdoc(curp
->outdata
, man
);
758 tree_mdoc(curp
->outdata
, man
);
761 man_mdoc(curp
->outdata
, man
);
768 terminal_mdoc(curp
->outdata
, man
);
771 markdown_mdoc(curp
->outdata
, man
);
777 if (man
->macroset
== MACROSET_MAN
) {
778 if (curp
->outtype
!= OUTT_TREE
|| !curp
->outopts
->noval
)
780 switch (curp
->outtype
) {
782 html_man(curp
->outdata
, man
);
785 tree_man(curp
->outdata
, man
);
788 man_man(curp
->outdata
, man
);
795 terminal_man(curp
->outdata
, man
);
801 mparse_updaterc(curp
->mp
, &rc
);
805 outdata_alloc(struct curparse
*curp
)
807 switch (curp
->outtype
) {
809 curp
->outdata
= html_alloc(curp
->outopts
);
812 curp
->outdata
= utf8_alloc(curp
->outopts
);
815 curp
->outdata
= locale_alloc(curp
->outopts
);
818 curp
->outdata
= ascii_alloc(curp
->outopts
);
821 curp
->outdata
= pdf_alloc(curp
->outopts
);
824 curp
->outdata
= ps_alloc(curp
->outopts
);
832 passthrough(const char *file
, int fd
, int synopsis_only
)
834 const char synb
[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
835 const char synr
[] = "SYNOPSIS";
841 ssize_t len
, written
;
847 if (fflush(stdout
) == EOF
) {
852 if ((stream
= fdopen(fd
, "r")) == NULL
) {
859 while ((len
= getline(&line
, &linesz
, stream
)) != -1) {
863 if ( ! isspace((unsigned char)*cp
))
865 while (isspace((unsigned char)*cp
)) {
870 if (strcmp(cp
, synb
) == 0 ||
871 strcmp(cp
, synr
) == 0)
876 for (; len
> 0; len
-= written
) {
877 if ((written
= write(STDOUT_FILENO
, cp
, len
)) != -1)
885 if (ferror(stream
)) {
898 warn("%s: SYSERR: %s", file
, syscall
);
899 if (rc
< MANDOCLEVEL_SYSERR
)
900 rc
= MANDOCLEVEL_SYSERR
;
904 koptions(int *options
, char *arg
)
907 if ( ! strcmp(arg
, "utf-8")) {
908 *options
|= MPARSE_UTF8
;
909 *options
&= ~MPARSE_LATIN1
;
910 } else if ( ! strcmp(arg
, "iso-8859-1")) {
911 *options
|= MPARSE_LATIN1
;
912 *options
&= ~MPARSE_UTF8
;
913 } else if ( ! strcmp(arg
, "us-ascii")) {
914 *options
&= ~(MPARSE_UTF8
| MPARSE_LATIN1
);
916 warnx("-K %s: Bad argument", arg
);
923 moptions(int *options
, char *arg
)
928 if (strcmp(arg
, "doc") == 0)
929 *options
|= MPARSE_MDOC
;
930 else if (strcmp(arg
, "an") == 0)
931 *options
|= MPARSE_MAN
;
935 toptions(struct curparse
*curp
, char *arg
)
938 if (0 == strcmp(arg
, "ascii"))
939 curp
->outtype
= OUTT_ASCII
;
940 else if (0 == strcmp(arg
, "lint")) {
941 curp
->outtype
= OUTT_LINT
;
942 curp
->mmin
= MANDOCERR_BASE
;
943 } else if (0 == strcmp(arg
, "tree"))
944 curp
->outtype
= OUTT_TREE
;
945 else if (0 == strcmp(arg
, "man"))
946 curp
->outtype
= OUTT_MAN
;
947 else if (0 == strcmp(arg
, "html"))
948 curp
->outtype
= OUTT_HTML
;
949 else if (0 == strcmp(arg
, "markdown"))
950 curp
->outtype
= OUTT_MARKDOWN
;
951 else if (0 == strcmp(arg
, "utf8"))
952 curp
->outtype
= OUTT_UTF8
;
953 else if (0 == strcmp(arg
, "locale"))
954 curp
->outtype
= OUTT_LOCALE
;
955 else if (0 == strcmp(arg
, "ps"))
956 curp
->outtype
= OUTT_PS
;
957 else if (0 == strcmp(arg
, "pdf"))
958 curp
->outtype
= OUTT_PDF
;
960 warnx("-T %s: Bad argument", arg
);
968 woptions(struct curparse
*curp
, char *arg
)
971 const char *toks
[11];
987 switch (getsubopt(&arg
, (char * const *)toks
, &v
)) {
993 curp
->mmin
= MANDOCERR_BASE
;
996 curp
->mmin
= MANDOCERR_STYLE
;
999 curp
->mmin
= MANDOCERR_WARNING
;
1002 curp
->mmin
= MANDOCERR_ERROR
;
1005 curp
->mmin
= MANDOCERR_UNSUPP
;
1008 curp
->mmin
= MANDOCERR_MAX
;
1011 curp
->mmin
= MANDOCERR_BASE
;
1012 curp
->os_e
= MANDOC_OS_OPENBSD
;
1015 curp
->mmin
= MANDOCERR_BASE
;
1016 curp
->os_e
= MANDOC_OS_NETBSD
;
1019 warnx("-W %s: Bad argument", o
);
1027 mmsg(enum mandocerr t
, enum mandoclevel lvl
,
1028 const char *file
, int line
, int col
, const char *msg
)
1030 const char *mparse_msg
;
1032 fprintf(stderr
, "%s: %s:", getprogname(),
1033 file
== NULL
? "<stdin>" : file
);
1036 fprintf(stderr
, "%d:%d:", line
, col
+ 1);
1038 fprintf(stderr
, " %s",
1039 t
< MANDOCERR_STYLE
? "BASE" : mparse_strlevel(lvl
));
1041 if ((mparse_msg
= mparse_strerror(t
)) != NULL
)
1042 fprintf(stderr
, ": %s", mparse_msg
);
1045 fprintf(stderr
, ": %s", msg
);
1047 fputc('\n', stderr
);
1051 spawn_pager(struct tag_files
*tag_files
)
1053 const struct timespec timeout
= { 0, 100000000 }; /* 0.1s */
1054 #define MAX_PAGER_ARGS 16
1055 char *argv
[MAX_PAGER_ARGS
];
1062 pager
= getenv("MANPAGER");
1063 if (pager
== NULL
|| *pager
== '\0')
1064 pager
= getenv("PAGER");
1065 if (pager
== NULL
|| *pager
== '\0')
1067 cp
= mandoc_strdup(pager
);
1070 * Parse the pager command into words.
1071 * Intentionally do not do anything fancy here.
1075 while (argc
+ 4 < MAX_PAGER_ARGS
) {
1077 cp
= strchr(cp
, ' ');
1087 /* For less(1), use the tag file. */
1089 if ((cmdlen
= strlen(argv
[0])) >= 4) {
1090 cp
= argv
[0] + cmdlen
- 4;
1091 if (strcmp(cp
, "less") == 0) {
1092 argv
[argc
++] = mandoc_strdup("-T");
1093 argv
[argc
++] = tag_files
->tfn
;
1096 argv
[argc
++] = tag_files
->ofn
;
1099 switch (pager_pid
= fork()) {
1101 err((int)MANDOCLEVEL_SYSERR
, "fork");
1105 (void)setpgid(pager_pid
, 0);
1106 (void)tcsetpgrp(tag_files
->ofd
, pager_pid
);
1108 if (pledge("stdio rpath tmppath tty proc", NULL
) == -1)
1109 err((int)MANDOCLEVEL_SYSERR
, "pledge");
1111 tag_files
->pager_pid
= pager_pid
;
1115 /* The child process becomes the pager. */
1117 if (dup2(tag_files
->ofd
, STDOUT_FILENO
) == -1)
1118 err((int)MANDOCLEVEL_SYSERR
, "pager stdout");
1119 close(tag_files
->ofd
);
1120 close(tag_files
->tfd
);
1122 /* Do not start the pager before controlling the terminal. */
1124 while (tcgetpgrp(STDOUT_FILENO
) != getpid())
1125 nanosleep(&timeout
, NULL
);
1127 execvp(argv
[0], argv
);
1128 err((int)MANDOCLEVEL_SYSERR
, "exec %s", argv
[0]);