]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
1 /* $Id: main.c,v 1.279 2017/01/09 17:49:57 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"
64 OUTT_ASCII
= 0, /* -Tascii */
65 OUTT_LOCALE
, /* -Tlocale */
66 OUTT_UTF8
, /* -Tutf8 */
67 OUTT_TREE
, /* -Ttree */
69 OUTT_HTML
, /* -Thtml */
70 OUTT_LINT
, /* -Tlint */
77 enum mandoclevel wlevel
; /* ignore messages below this */
78 int wstop
; /* stop after a file with a warning */
79 enum outt outtype
; /* which output to use */
80 void *outdata
; /* data for output */
81 struct manoutput
*outopts
; /* output options */
85 int mandocdb(int, char *[]);
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 *);
94 static int koptions(int *, char *);
95 static int moptions(int *, char *);
96 static void mmsg(enum mandocerr
, enum mandoclevel
,
97 const char *, int, int, const char *);
98 static void outdata_alloc(struct curparse
*);
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 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 flock", 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";
172 if (strcmp(progname
, BINM_MAN
) == 0)
173 search
.argmode
= ARG_NAME
;
174 else if (strcmp(progname
, BINM_APROPOS
) == 0)
175 search
.argmode
= ARG_EXPR
;
176 else if (strcmp(progname
, BINM_WHATIS
) == 0)
177 search
.argmode
= ARG_WORD
;
178 else if (strncmp(progname
, "help", 4) == 0)
179 search
.argmode
= ARG_NAME
;
181 search
.argmode
= ARG_FILE
;
183 /* Parser and formatter options. */
185 memset(&curp
, 0, sizeof(struct curparse
));
186 curp
.outtype
= OUTT_LOCALE
;
187 curp
.wlevel
= MANDOCLEVEL_BADARG
;
188 curp
.outopts
= &conf
.output
;
189 options
= MPARSE_SO
| MPARSE_UTF8
| MPARSE_LATIN1
;
195 outmode
= OUTMODE_DEF
;
197 while (-1 != (c
= getopt(argc
, argv
,
198 "aC:cfhI:iK:klM:m:O:S:s:T:VW:w"))) {
201 outmode
= OUTMODE_ALL
;
210 search
.argmode
= ARG_WORD
;
213 conf
.output
.synopsisonly
= 1;
215 outmode
= OUTMODE_ALL
;
218 if (strncmp(optarg
, "os=", 3)) {
219 warnx("-I %s: Bad argument", optarg
);
220 return (int)MANDOCLEVEL_BADARG
;
223 warnx("-I %s: Duplicate argument", optarg
);
224 return (int)MANDOCLEVEL_BADARG
;
226 defos
= mandoc_strdup(optarg
+ 3);
229 outmode
= OUTMODE_INT
;
232 if ( ! koptions(&options
, optarg
))
233 return (int)MANDOCLEVEL_BADARG
;
236 search
.argmode
= ARG_EXPR
;
239 search
.argmode
= ARG_FILE
;
240 outmode
= OUTMODE_ALL
;
249 search
.outkey
= optarg
;
250 while (optarg
!= NULL
)
251 manconf_output(&conf
.output
,
252 strsep(&optarg
, ","));
255 search
.arch
= optarg
;
261 if ( ! toptions(&curp
, optarg
))
262 return (int)MANDOCLEVEL_BADARG
;
265 if ( ! woptions(&curp
, optarg
))
266 return (int)MANDOCLEVEL_BADARG
;
269 outmode
= OUTMODE_FLN
;
278 usage(search
.argmode
);
280 /* Postprocess options. */
282 if (outmode
== OUTMODE_DEF
) {
283 switch (search
.argmode
) {
285 outmode
= OUTMODE_ALL
;
289 outmode
= OUTMODE_ONE
;
292 outmode
= OUTMODE_LST
;
297 if (outmode
== OUTMODE_FLN
||
298 outmode
== OUTMODE_LST
||
299 !isatty(STDOUT_FILENO
))
304 if (pledge("stdio rpath flock", NULL
) == -1)
305 err((int)MANDOCLEVEL_SYSERR
, "pledge");
308 /* Parse arguments. */
318 * and for a man(1) section argument without -s.
321 if (search
.argmode
== ARG_NAME
) {
322 if (*progname
== 'h') {
327 } else if (argc
> 1 &&
328 ((uc
= (unsigned char *)argv
[0]) != NULL
) &&
329 ((isdigit(uc
[0]) && (uc
[1] == '\0' ||
330 (isalpha(uc
[1]) && uc
[2] == '\0'))) ||
331 (uc
[0] == 'n' && uc
[1] == '\0'))) {
332 search
.sec
= (char *)uc
;
336 if (search
.arch
== NULL
)
337 search
.arch
= getenv("MACHINE");
339 if (search
.arch
== NULL
)
340 search
.arch
= MACHINE
;
346 /* man(1), whatis(1), apropos(1) */
348 if (search
.argmode
!= ARG_FILE
) {
349 if (search
.argmode
== ARG_NAME
&&
350 outmode
== OUTMODE_ONE
)
351 search
.firstmatch
= 1;
353 /* Access the mandoc database. */
355 manconf_parse(&conf
, conf_file
, defpaths
, auxpaths
);
356 if ( ! mansearch(&search
, &conf
.manpath
,
357 argc
, argv
, &res
, &sz
))
358 usage(search
.argmode
);
361 if (search
.argmode
== ARG_NAME
)
362 fs_search(&search
, &conf
.manpath
,
363 argc
, argv
, &res
, &sz
);
365 warnx("nothing appropriate");
369 rc
= MANDOCLEVEL_BADARG
;
374 * For standard man(1) and -a output mode,
375 * prepare for copying filename pointers
376 * into the program parameter array.
379 if (outmode
== OUTMODE_ONE
) {
382 } else if (outmode
== OUTMODE_ALL
)
385 /* Iterate all matching manuals. */
388 for (i
= 0; i
< sz
; i
++) {
389 if (outmode
== OUTMODE_FLN
)
391 else if (outmode
== OUTMODE_LST
)
392 printf("%s - %s\n", res
[i
].names
,
393 res
[i
].output
== NULL
? "" :
395 else if (outmode
== OUTMODE_ONE
) {
396 /* Search for the best section. */
398 sec
+= strcspn(sec
, "123456789");
401 prio
= sec_prios
[sec
[0] - '1'];
404 if (prio
>= best_prio
)
412 * For man(1), -a and -i output mode, fall through
413 * to the main mandoc(1) code iterating files
414 * and running the parsers on each of them.
417 if (outmode
== OUTMODE_FLN
|| outmode
== OUTMODE_LST
)
425 if (pledge("stdio rpath tmppath tty proc exec", NULL
) == -1)
426 err((int)MANDOCLEVEL_SYSERR
, "pledge");
428 if (pledge("stdio rpath", NULL
) == -1)
429 err((int)MANDOCLEVEL_SYSERR
, "pledge");
433 if (search
.argmode
== ARG_FILE
&& ! moptions(&options
, auxpaths
))
434 return (int)MANDOCLEVEL_BADARG
;
437 curp
.mp
= mparse_alloc(options
, curp
.wlevel
, mmsg
, defos
);
440 * Conditionally start up the lookaside buffer before parsing.
442 if (OUTT_MAN
== curp
.outtype
)
443 mparse_keep(curp
.mp
);
447 tag_files
= tag_init();
448 parse(&curp
, STDIN_FILENO
, "<stdin>");
452 fd
= mparse_open(curp
.mp
, resp
!= NULL
? resp
->file
: *argv
);
455 tag_files
= tag_init();
460 parse(&curp
, fd
, *argv
);
461 else if (resp
->form
== FORM_SRC
) {
462 /* For .so only; ignore failure. */
463 chdir(conf
.manpath
.paths
[resp
->ipath
]);
464 parse(&curp
, fd
, resp
->file
);
466 passthrough(resp
->file
, fd
,
467 conf
.output
.synopsisonly
);
469 if (argc
> 1 && curp
.outtype
<= OUTT_UTF8
) {
470 if (curp
.outdata
== NULL
)
471 outdata_alloc(&curp
);
472 terminal_sepline(curp
.outdata
);
474 } else if (rc
< MANDOCLEVEL_ERROR
)
475 rc
= MANDOCLEVEL_ERROR
;
477 if (MANDOCLEVEL_OK
!= rc
&& curp
.wstop
)
485 mparse_reset(curp
.mp
);
488 if (curp
.outdata
!= NULL
) {
489 switch (curp
.outtype
) {
491 html_free(curp
.outdata
);
496 ascii_free(curp
.outdata
);
500 pspdf_free(curp
.outdata
);
506 mparse_free(curp
.mp
);
510 if (search
.argmode
!= ARG_FILE
) {
512 mansearch_free(res
, sz
);
518 * When using a pager, finish writing both temporary files,
519 * fork it, wait for the user to close it, and clean up.
522 if (tag_files
!= NULL
) {
525 man_pgid
= getpgid(0);
526 tag_files
->tcpgid
= man_pgid
== getpid() ?
527 getpgid(getppid()) : man_pgid
;
532 /* Stop here until moved to the foreground. */
534 tc_pgid
= tcgetpgrp(tag_files
->ofd
);
535 if (tc_pgid
!= man_pgid
) {
536 if (tc_pgid
== pager_pid
) {
537 (void)tcsetpgrp(tag_files
->ofd
,
539 if (signum
== SIGTTIN
)
542 tag_files
->tcpgid
= tc_pgid
;
547 /* Once in the foreground, activate the pager. */
550 (void)tcsetpgrp(tag_files
->ofd
, pager_pid
);
551 kill(pager_pid
, SIGCONT
);
553 pager_pid
= spawn_pager(tag_files
);
555 /* Wait for the pager to stop or exit. */
557 while ((pid
= waitpid(pager_pid
, &status
,
558 WUNTRACED
)) == -1 && errno
== EINTR
)
563 rc
= MANDOCLEVEL_SYSERR
;
566 if (!WIFSTOPPED(status
))
569 signum
= WSTOPSIG(status
);
578 usage(enum argmode argmode
)
583 fputs("usage: mandoc [-acfhkl] [-I os=name] "
584 "[-K encoding] [-mformat] [-O option]\n"
585 "\t [-T output] [-W level] [file ...]\n", stderr
);
588 fputs("usage: man [-acfhklw] [-C file] [-I os=name] "
589 "[-K encoding] [-M path] [-m path]\n"
590 "\t [-O option=value] [-S subsection] [-s section] "
591 "[-T output] [-W level]\n"
592 "\t [section] name ...\n", stderr
);
595 fputs("usage: whatis [-acfhklw] [-C file] "
596 "[-M path] [-m path] [-O outkey] [-S arch]\n"
597 "\t [-s section] name ...\n", stderr
);
600 fputs("usage: apropos [-acfhklw] [-C file] "
601 "[-M path] [-m path] [-O outkey] [-S arch]\n"
602 "\t [-s section] expression ...\n", stderr
);
605 exit((int)MANDOCLEVEL_BADARG
);
609 fs_lookup(const struct manpaths
*paths
, size_t ipath
,
610 const char *sec
, const char *arch
, const char *name
,
611 struct manpage
**res
, size_t *ressz
)
614 struct manpage
*page
;
620 mandoc_asprintf(&file
, "%s/man%s/%s.%s",
621 paths
->paths
[ipath
], sec
, name
, sec
);
622 if (access(file
, R_OK
) != -1)
626 mandoc_asprintf(&file
, "%s/cat%s/%s.0",
627 paths
->paths
[ipath
], sec
, name
);
628 if (access(file
, R_OK
) != -1) {
635 mandoc_asprintf(&file
, "%s/man%s/%s/%s.%s",
636 paths
->paths
[ipath
], sec
, arch
, name
, sec
);
637 if (access(file
, R_OK
) != -1)
642 mandoc_asprintf(&file
, "%s/man%s/%s.[01-9]*",
643 paths
->paths
[ipath
], sec
, name
);
644 globres
= glob(file
, 0, NULL
, &globinfo
);
645 if (globres
!= 0 && globres
!= GLOB_NOMATCH
)
646 warn("%s: glob", file
);
649 file
= mandoc_strdup(*globinfo
.gl_pathv
);
655 warnx("outdated mandoc.db lacks %s(%s) entry, run %s %s",
656 name
, sec
, BINM_MAKEWHATIS
, paths
->paths
[ipath
]);
657 *res
= mandoc_reallocarray(*res
, ++*ressz
, sizeof(struct manpage
));
658 page
= *res
+ (*ressz
- 1);
663 page
->bits
= NAME_FILE
& NAME_MASK
;
664 page
->sec
= (*sec
>= '1' && *sec
<= '9') ? *sec
- '1' + 1 : 10;
670 fs_search(const struct mansearch
*cfg
, const struct manpaths
*paths
,
671 int argc
, char **argv
, struct manpage
**res
, size_t *ressz
)
673 const char *const sections
[] =
674 {"1", "8", "6", "2", "3", "5", "7", "4", "9", "3p"};
675 const size_t nsec
= sizeof(sections
)/sizeof(sections
[0]);
677 size_t ipath
, isec
, lastsz
;
679 assert(cfg
->argmode
== ARG_NAME
);
684 for (ipath
= 0; ipath
< paths
->sz
; ipath
++) {
685 if (cfg
->sec
!= NULL
) {
686 if (fs_lookup(paths
, ipath
, cfg
->sec
,
687 cfg
->arch
, *argv
, res
, ressz
) &&
690 } else for (isec
= 0; isec
< nsec
; isec
++)
691 if (fs_lookup(paths
, ipath
, sections
[isec
],
692 cfg
->arch
, *argv
, res
, ressz
) &&
696 if (*ressz
== lastsz
)
697 warnx("No entry for %s in the manual.", *argv
);
705 parse(struct curparse
*curp
, int fd
, const char *file
)
707 enum mandoclevel rctmp
;
708 struct roff_man
*man
;
710 /* Begin by parsing the file itself. */
715 rctmp
= mparse_readfd(curp
->mp
, fd
, file
);
716 if (fd
!= STDIN_FILENO
)
722 * With -Wstop and warnings or errors of at least the requested
723 * level, do not produce output.
726 if (rctmp
!= MANDOCLEVEL_OK
&& curp
->wstop
)
729 if (curp
->outdata
== NULL
)
732 mparse_result(curp
->mp
, &man
, NULL
);
734 /* Execute the out device, if it exists. */
738 if (man
->macroset
== MACROSET_MDOC
) {
740 switch (curp
->outtype
) {
742 html_mdoc(curp
->outdata
, man
);
745 tree_mdoc(curp
->outdata
, man
);
748 man_mdoc(curp
->outdata
, man
);
755 terminal_mdoc(curp
->outdata
, man
);
761 if (man
->macroset
== MACROSET_MAN
) {
763 switch (curp
->outtype
) {
765 html_man(curp
->outdata
, man
);
768 tree_man(curp
->outdata
, man
);
771 man_man(curp
->outdata
, man
);
778 terminal_man(curp
->outdata
, man
);
784 mparse_updaterc(curp
->mp
, &rc
);
788 outdata_alloc(struct curparse
*curp
)
790 switch (curp
->outtype
) {
792 curp
->outdata
= html_alloc(curp
->outopts
);
795 curp
->outdata
= utf8_alloc(curp
->outopts
);
798 curp
->outdata
= locale_alloc(curp
->outopts
);
801 curp
->outdata
= ascii_alloc(curp
->outopts
);
804 curp
->outdata
= pdf_alloc(curp
->outopts
);
807 curp
->outdata
= ps_alloc(curp
->outopts
);
815 passthrough(const char *file
, int fd
, int synopsis_only
)
817 const char synb
[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
818 const char synr
[] = "SYNOPSIS";
824 ssize_t len
, written
;
830 if (fflush(stdout
) == EOF
) {
835 if ((stream
= fdopen(fd
, "r")) == NULL
) {
842 while ((len
= getline(&line
, &linesz
, stream
)) != -1) {
846 if ( ! isspace((unsigned char)*cp
))
848 while (isspace((unsigned char)*cp
)) {
853 if (strcmp(cp
, synb
) == 0 ||
854 strcmp(cp
, synr
) == 0)
859 for (; len
> 0; len
-= written
) {
860 if ((written
= write(STDOUT_FILENO
, cp
, len
)) != -1)
868 if (ferror(stream
)) {
881 warn("%s: SYSERR: %s", file
, syscall
);
882 if (rc
< MANDOCLEVEL_SYSERR
)
883 rc
= MANDOCLEVEL_SYSERR
;
887 koptions(int *options
, char *arg
)
890 if ( ! strcmp(arg
, "utf-8")) {
891 *options
|= MPARSE_UTF8
;
892 *options
&= ~MPARSE_LATIN1
;
893 } else if ( ! strcmp(arg
, "iso-8859-1")) {
894 *options
|= MPARSE_LATIN1
;
895 *options
&= ~MPARSE_UTF8
;
896 } else if ( ! strcmp(arg
, "us-ascii")) {
897 *options
&= ~(MPARSE_UTF8
| MPARSE_LATIN1
);
899 warnx("-K %s: Bad argument", arg
);
906 moptions(int *options
, char *arg
)
911 else if (0 == strcmp(arg
, "doc"))
912 *options
|= MPARSE_MDOC
;
913 else if (0 == strcmp(arg
, "andoc"))
915 else if (0 == strcmp(arg
, "an"))
916 *options
|= MPARSE_MAN
;
918 warnx("-m %s: Bad argument", arg
);
926 toptions(struct curparse
*curp
, char *arg
)
929 if (0 == strcmp(arg
, "ascii"))
930 curp
->outtype
= OUTT_ASCII
;
931 else if (0 == strcmp(arg
, "lint")) {
932 curp
->outtype
= OUTT_LINT
;
933 curp
->wlevel
= MANDOCLEVEL_WARNING
;
934 } else if (0 == strcmp(arg
, "tree"))
935 curp
->outtype
= OUTT_TREE
;
936 else if (0 == strcmp(arg
, "man"))
937 curp
->outtype
= OUTT_MAN
;
938 else if (0 == strcmp(arg
, "html"))
939 curp
->outtype
= OUTT_HTML
;
940 else if (0 == strcmp(arg
, "utf8"))
941 curp
->outtype
= OUTT_UTF8
;
942 else if (0 == strcmp(arg
, "locale"))
943 curp
->outtype
= OUTT_LOCALE
;
944 else if (0 == strcmp(arg
, "xhtml"))
945 curp
->outtype
= OUTT_HTML
;
946 else if (0 == strcmp(arg
, "ps"))
947 curp
->outtype
= OUTT_PS
;
948 else if (0 == strcmp(arg
, "pdf"))
949 curp
->outtype
= OUTT_PDF
;
951 warnx("-T %s: Bad argument", arg
);
959 woptions(struct curparse
*curp
, char *arg
)
974 switch (getsubopt(&arg
, (char * const *)toks
, &v
)) {
980 curp
->wlevel
= MANDOCLEVEL_WARNING
;
983 curp
->wlevel
= MANDOCLEVEL_ERROR
;
986 curp
->wlevel
= MANDOCLEVEL_UNSUPP
;
989 curp
->wlevel
= MANDOCLEVEL_BADARG
;
992 warnx("-W %s: Bad argument", o
);
1001 mmsg(enum mandocerr t
, enum mandoclevel lvl
,
1002 const char *file
, int line
, int col
, const char *msg
)
1004 const char *mparse_msg
;
1006 fprintf(stderr
, "%s: %s:", getprogname(),
1007 file
== NULL
? "<stdin>" : file
);
1010 fprintf(stderr
, "%d:%d:", line
, col
+ 1);
1012 fprintf(stderr
, " %s", mparse_strlevel(lvl
));
1014 if (NULL
!= (mparse_msg
= mparse_strerror(t
)))
1015 fprintf(stderr
, ": %s", mparse_msg
);
1018 fprintf(stderr
, ": %s", msg
);
1020 fputc('\n', stderr
);
1024 spawn_pager(struct tag_files
*tag_files
)
1026 const struct timespec timeout
= { 0, 100000000 }; /* 0.1s */
1027 #define MAX_PAGER_ARGS 16
1028 char *argv
[MAX_PAGER_ARGS
];
1035 pager
= getenv("MANPAGER");
1036 if (pager
== NULL
|| *pager
== '\0')
1037 pager
= getenv("PAGER");
1038 if (pager
== NULL
|| *pager
== '\0')
1040 cp
= mandoc_strdup(pager
);
1043 * Parse the pager command into words.
1044 * Intentionally do not do anything fancy here.
1048 while (argc
+ 4 < MAX_PAGER_ARGS
) {
1050 cp
= strchr(cp
, ' ');
1060 /* For less(1), use the tag file. */
1062 if ((cmdlen
= strlen(argv
[0])) >= 4) {
1063 cp
= argv
[0] + cmdlen
- 4;
1064 if (strcmp(cp
, "less") == 0) {
1065 argv
[argc
++] = mandoc_strdup("-T");
1066 argv
[argc
++] = tag_files
->tfn
;
1069 argv
[argc
++] = tag_files
->ofn
;
1072 switch (pager_pid
= fork()) {
1074 err((int)MANDOCLEVEL_SYSERR
, "fork");
1078 (void)setpgid(pager_pid
, 0);
1079 (void)tcsetpgrp(tag_files
->ofd
, pager_pid
);
1081 if (pledge("stdio rpath tmppath tty proc", NULL
) == -1)
1082 err((int)MANDOCLEVEL_SYSERR
, "pledge");
1084 tag_files
->pager_pid
= pager_pid
;
1088 /* The child process becomes the pager. */
1090 if (dup2(tag_files
->ofd
, STDOUT_FILENO
) == -1)
1091 err((int)MANDOCLEVEL_SYSERR
, "pager stdout");
1092 close(tag_files
->ofd
);
1093 close(tag_files
->tfd
);
1095 /* Do not start the pager before controlling the terminal. */
1097 while (tcgetpgrp(STDOUT_FILENO
) != getpid())
1098 nanosleep(&timeout
, NULL
);
1100 execvp(argv
[0], argv
);
1101 err((int)MANDOCLEVEL_SYSERR
, "exec %s", argv
[0]);