]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
1 /* $Id: main.c,v 1.285 2017/03/03 14:23:23 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_MARKDOWN
, /* -Tmarkdown */
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 */
86 int mandocdb(int, char *[]);
88 static int fs_lookup(const struct manpaths
*,
89 size_t ipath
, const char *,
90 const char *, const char *,
91 struct manpage
**, size_t *);
92 static void fs_search(const struct mansearch
*,
93 const struct manpaths
*, int, char**,
94 struct manpage
**, size_t *);
95 static int koptions(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 outdata_alloc(struct curparse
*);
100 static void parse(struct curparse
*, int, const char *);
101 static void passthrough(const char *, int, int);
102 static pid_t
spawn_pager(struct tag_files
*);
103 static int toptions(struct curparse
*, char *);
104 static void usage(enum argmode
) __attribute__((__noreturn__
));
105 static int woptions(struct curparse
*, char *);
107 static const int sec_prios
[] = {1, 4, 5, 8, 6, 3, 7, 2, 9};
108 static char help_arg
[] = "help";
109 static char *help_argv
[] = {help_arg
, NULL
};
110 static enum mandoclevel rc
;
114 main(int argc
, char *argv
[])
117 struct mansearch search
;
118 struct curparse curp
;
119 struct tag_files
*tag_files
;
120 struct manpage
*res
, *resp
;
121 const char *progname
, *sec
, *thisarg
;
122 char *conf_file
, *defpaths
, *auxpaths
;
127 enum outmode outmode
;
134 pid_t pager_pid
, tc_pgid
, man_pgid
, pid
;
137 progname
= getprogname();
140 progname
= mandoc_strdup("mandoc");
141 else if ((progname
= strrchr(argv
[0], '/')) == NULL
)
145 setprogname(progname
);
148 if (strncmp(progname
, "mandocdb", 8) == 0 ||
149 strcmp(progname
, BINM_MAKEWHATIS
) == 0)
150 return mandocdb(argc
, argv
);
153 if (pledge("stdio rpath tmppath tty proc exec", NULL
) == -1)
154 err((int)MANDOCLEVEL_SYSERR
, "pledge");
157 #if HAVE_SANDBOX_INIT
158 if (sandbox_init(kSBXProfileNoInternet
, SANDBOX_NAMED
, NULL
) == -1)
159 errx((int)MANDOCLEVEL_SYSERR
, "sandbox_init");
162 /* Search options. */
164 memset(&conf
, 0, sizeof(conf
));
165 conf_file
= defpaths
= NULL
;
168 memset(&search
, 0, sizeof(struct mansearch
));
169 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
;
252 search
.arch
= optarg
;
258 if ( ! toptions(&curp
, optarg
))
259 return (int)MANDOCLEVEL_BADARG
;
262 if ( ! woptions(&curp
, optarg
))
263 return (int)MANDOCLEVEL_BADARG
;
266 outmode
= OUTMODE_FLN
;
275 usage(search
.argmode
);
277 /* Postprocess options. */
279 if (outmode
== OUTMODE_DEF
) {
280 switch (search
.argmode
) {
282 outmode
= OUTMODE_ALL
;
286 outmode
= OUTMODE_ONE
;
289 outmode
= OUTMODE_LST
;
295 if (outmode
== OUTMODE_LST
)
296 search
.outkey
= oarg
;
298 while (oarg
!= NULL
) {
300 if (manconf_output(&conf
.output
,
301 strsep(&oarg
, ","), 0) == 0)
303 warnx("-O %s: Bad argument", thisarg
);
304 return (int)MANDOCLEVEL_BADARG
;
309 if (outmode
== OUTMODE_FLN
||
310 outmode
== OUTMODE_LST
||
311 !isatty(STDOUT_FILENO
))
316 if (pledge("stdio rpath", NULL
) == -1)
317 err((int)MANDOCLEVEL_SYSERR
, "pledge");
320 /* Parse arguments. */
330 * and for a man(1) section argument without -s.
333 if (search
.argmode
== ARG_NAME
) {
334 if (*progname
== 'h') {
339 } else if (argc
> 1 &&
340 ((uc
= (unsigned char *)argv
[0]) != NULL
) &&
341 ((isdigit(uc
[0]) && (uc
[1] == '\0' ||
342 (isalpha(uc
[1]) && uc
[2] == '\0'))) ||
343 (uc
[0] == 'n' && uc
[1] == '\0'))) {
344 search
.sec
= (char *)uc
;
348 if (search
.arch
== NULL
)
349 search
.arch
= getenv("MACHINE");
351 if (search
.arch
== NULL
)
352 search
.arch
= MACHINE
;
358 /* man(1), whatis(1), apropos(1) */
360 if (search
.argmode
!= ARG_FILE
) {
361 if (search
.argmode
== ARG_NAME
&&
362 outmode
== OUTMODE_ONE
)
363 search
.firstmatch
= 1;
365 /* Access the mandoc database. */
367 manconf_parse(&conf
, conf_file
, defpaths
, auxpaths
);
368 if ( ! mansearch(&search
, &conf
.manpath
,
369 argc
, argv
, &res
, &sz
))
370 usage(search
.argmode
);
373 if (search
.argmode
== ARG_NAME
)
374 fs_search(&search
, &conf
.manpath
,
375 argc
, argv
, &res
, &sz
);
377 warnx("nothing appropriate");
381 rc
= MANDOCLEVEL_BADARG
;
386 * For standard man(1) and -a output mode,
387 * prepare for copying filename pointers
388 * into the program parameter array.
391 if (outmode
== OUTMODE_ONE
) {
394 } else if (outmode
== OUTMODE_ALL
)
397 /* Iterate all matching manuals. */
400 for (i
= 0; i
< sz
; i
++) {
401 if (outmode
== OUTMODE_FLN
)
403 else if (outmode
== OUTMODE_LST
)
404 printf("%s - %s\n", res
[i
].names
,
405 res
[i
].output
== NULL
? "" :
407 else if (outmode
== OUTMODE_ONE
) {
408 /* Search for the best section. */
410 sec
+= strcspn(sec
, "123456789");
413 prio
= sec_prios
[sec
[0] - '1'];
416 if (prio
>= best_prio
)
424 * For man(1), -a and -i output mode, fall through
425 * to the main mandoc(1) code iterating files
426 * and running the parsers on each of them.
429 if (outmode
== OUTMODE_FLN
|| outmode
== OUTMODE_LST
)
437 if (pledge("stdio rpath tmppath tty proc exec", NULL
) == -1)
438 err((int)MANDOCLEVEL_SYSERR
, "pledge");
440 if (pledge("stdio rpath", NULL
) == -1)
441 err((int)MANDOCLEVEL_SYSERR
, "pledge");
445 if (search
.argmode
== ARG_FILE
&& ! moptions(&options
, auxpaths
))
446 return (int)MANDOCLEVEL_BADARG
;
449 curp
.mp
= mparse_alloc(options
, curp
.wlevel
, mmsg
, defos
);
452 * Conditionally start up the lookaside buffer before parsing.
454 if (OUTT_MAN
== curp
.outtype
)
455 mparse_keep(curp
.mp
);
459 tag_files
= tag_init();
460 parse(&curp
, STDIN_FILENO
, "<stdin>");
464 fd
= mparse_open(curp
.mp
, resp
!= NULL
? resp
->file
: *argv
);
467 tag_files
= tag_init();
472 parse(&curp
, fd
, *argv
);
473 else if (resp
->form
== FORM_SRC
) {
474 /* For .so only; ignore failure. */
475 chdir(conf
.manpath
.paths
[resp
->ipath
]);
476 parse(&curp
, fd
, resp
->file
);
478 passthrough(resp
->file
, fd
,
479 conf
.output
.synopsisonly
);
481 if (argc
> 1 && curp
.outtype
<= OUTT_UTF8
) {
482 if (curp
.outdata
== NULL
)
483 outdata_alloc(&curp
);
484 terminal_sepline(curp
.outdata
);
486 } else if (rc
< MANDOCLEVEL_ERROR
)
487 rc
= MANDOCLEVEL_ERROR
;
489 if (MANDOCLEVEL_OK
!= rc
&& curp
.wstop
)
497 mparse_reset(curp
.mp
);
500 if (curp
.outdata
!= NULL
) {
501 switch (curp
.outtype
) {
503 html_free(curp
.outdata
);
508 ascii_free(curp
.outdata
);
512 pspdf_free(curp
.outdata
);
518 mparse_free(curp
.mp
);
522 if (search
.argmode
!= ARG_FILE
) {
524 mansearch_free(res
, sz
);
530 * When using a pager, finish writing both temporary files,
531 * fork it, wait for the user to close it, and clean up.
534 if (tag_files
!= NULL
) {
537 man_pgid
= getpgid(0);
538 tag_files
->tcpgid
= man_pgid
== getpid() ?
539 getpgid(getppid()) : man_pgid
;
544 /* Stop here until moved to the foreground. */
546 tc_pgid
= tcgetpgrp(tag_files
->ofd
);
547 if (tc_pgid
!= man_pgid
) {
548 if (tc_pgid
== pager_pid
) {
549 (void)tcsetpgrp(tag_files
->ofd
,
551 if (signum
== SIGTTIN
)
554 tag_files
->tcpgid
= tc_pgid
;
559 /* Once in the foreground, activate the pager. */
562 (void)tcsetpgrp(tag_files
->ofd
, pager_pid
);
563 kill(pager_pid
, SIGCONT
);
565 pager_pid
= spawn_pager(tag_files
);
567 /* Wait for the pager to stop or exit. */
569 while ((pid
= waitpid(pager_pid
, &status
,
570 WUNTRACED
)) == -1 && errno
== EINTR
)
575 rc
= MANDOCLEVEL_SYSERR
;
578 if (!WIFSTOPPED(status
))
581 signum
= WSTOPSIG(status
);
590 usage(enum argmode argmode
)
595 fputs("usage: mandoc [-acfhkl] [-I os=name] "
596 "[-K encoding] [-mformat] [-O option]\n"
597 "\t [-T output] [-W level] [file ...]\n", stderr
);
600 fputs("usage: man [-acfhklw] [-C file] [-I os=name] "
601 "[-K encoding] [-M path] [-m path]\n"
602 "\t [-O option=value] [-S subsection] [-s section] "
603 "[-T output] [-W level]\n"
604 "\t [section] name ...\n", stderr
);
607 fputs("usage: whatis [-acfhklw] [-C file] "
608 "[-M path] [-m path] [-O outkey] [-S arch]\n"
609 "\t [-s section] name ...\n", stderr
);
612 fputs("usage: apropos [-acfhklw] [-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 else if (0 == strcmp(arg
, "doc"))
929 *options
|= MPARSE_MDOC
;
930 else if (0 == strcmp(arg
, "andoc"))
932 else if (0 == strcmp(arg
, "an"))
933 *options
|= MPARSE_MAN
;
935 warnx("-m %s: Bad argument", arg
);
943 toptions(struct curparse
*curp
, char *arg
)
946 if (0 == strcmp(arg
, "ascii"))
947 curp
->outtype
= OUTT_ASCII
;
948 else if (0 == strcmp(arg
, "lint")) {
949 curp
->outtype
= OUTT_LINT
;
950 curp
->wlevel
= MANDOCLEVEL_WARNING
;
951 } else if (0 == strcmp(arg
, "tree"))
952 curp
->outtype
= OUTT_TREE
;
953 else if (0 == strcmp(arg
, "man"))
954 curp
->outtype
= OUTT_MAN
;
955 else if (0 == strcmp(arg
, "html"))
956 curp
->outtype
= OUTT_HTML
;
957 else if (0 == strcmp(arg
, "markdown"))
958 curp
->outtype
= OUTT_MARKDOWN
;
959 else if (0 == strcmp(arg
, "utf8"))
960 curp
->outtype
= OUTT_UTF8
;
961 else if (0 == strcmp(arg
, "locale"))
962 curp
->outtype
= OUTT_LOCALE
;
963 else if (0 == strcmp(arg
, "xhtml"))
964 curp
->outtype
= OUTT_HTML
;
965 else if (0 == strcmp(arg
, "ps"))
966 curp
->outtype
= OUTT_PS
;
967 else if (0 == strcmp(arg
, "pdf"))
968 curp
->outtype
= OUTT_PDF
;
970 warnx("-T %s: Bad argument", arg
);
978 woptions(struct curparse
*curp
, char *arg
)
993 switch (getsubopt(&arg
, (char * const *)toks
, &v
)) {
999 curp
->wlevel
= MANDOCLEVEL_WARNING
;
1002 curp
->wlevel
= MANDOCLEVEL_ERROR
;
1005 curp
->wlevel
= MANDOCLEVEL_UNSUPP
;
1008 curp
->wlevel
= MANDOCLEVEL_BADARG
;
1011 warnx("-W %s: Bad argument", o
);
1020 mmsg(enum mandocerr t
, enum mandoclevel lvl
,
1021 const char *file
, int line
, int col
, const char *msg
)
1023 const char *mparse_msg
;
1025 fprintf(stderr
, "%s: %s:", getprogname(),
1026 file
== NULL
? "<stdin>" : file
);
1029 fprintf(stderr
, "%d:%d:", line
, col
+ 1);
1031 fprintf(stderr
, " %s", mparse_strlevel(lvl
));
1033 if (NULL
!= (mparse_msg
= mparse_strerror(t
)))
1034 fprintf(stderr
, ": %s", mparse_msg
);
1037 fprintf(stderr
, ": %s", msg
);
1039 fputc('\n', stderr
);
1043 spawn_pager(struct tag_files
*tag_files
)
1045 const struct timespec timeout
= { 0, 100000000 }; /* 0.1s */
1046 #define MAX_PAGER_ARGS 16
1047 char *argv
[MAX_PAGER_ARGS
];
1054 pager
= getenv("MANPAGER");
1055 if (pager
== NULL
|| *pager
== '\0')
1056 pager
= getenv("PAGER");
1057 if (pager
== NULL
|| *pager
== '\0')
1059 cp
= mandoc_strdup(pager
);
1062 * Parse the pager command into words.
1063 * Intentionally do not do anything fancy here.
1067 while (argc
+ 4 < MAX_PAGER_ARGS
) {
1069 cp
= strchr(cp
, ' ');
1079 /* For less(1), use the tag file. */
1081 if ((cmdlen
= strlen(argv
[0])) >= 4) {
1082 cp
= argv
[0] + cmdlen
- 4;
1083 if (strcmp(cp
, "less") == 0) {
1084 argv
[argc
++] = mandoc_strdup("-T");
1085 argv
[argc
++] = tag_files
->tfn
;
1088 argv
[argc
++] = tag_files
->ofn
;
1091 switch (pager_pid
= fork()) {
1093 err((int)MANDOCLEVEL_SYSERR
, "fork");
1097 (void)setpgid(pager_pid
, 0);
1098 (void)tcsetpgrp(tag_files
->ofd
, pager_pid
);
1100 if (pledge("stdio rpath tmppath tty proc", NULL
) == -1)
1101 err((int)MANDOCLEVEL_SYSERR
, "pledge");
1103 tag_files
->pager_pid
= pager_pid
;
1107 /* The child process becomes the pager. */
1109 if (dup2(tag_files
->ofd
, STDOUT_FILENO
) == -1)
1110 err((int)MANDOCLEVEL_SYSERR
, "pager stdout");
1111 close(tag_files
->ofd
);
1112 close(tag_files
->tfd
);
1114 /* Do not start the pager before controlling the terminal. */
1116 while (tcgetpgrp(STDOUT_FILENO
) != getpid())
1117 nanosleep(&timeout
, NULL
);
1119 execvp(argv
[0], argv
);
1120 err((int)MANDOCLEVEL_SYSERR
, "exec %s", argv
[0]);