1 /* $Id: main.c,v 1.322 2019/03/06 10:18:58 schwarze Exp $ */
3 * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010-2012, 2014-2019 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 */
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 mandoc_msg_setoutfile(stderr);
152 if (strncmp(progname, "mandocdb", 8) == 0 ||
153 strcmp(progname, BINM_MAKEWHATIS) == 0)
154 return mandocdb(argc, argv);
157 if (pledge("stdio rpath tmppath tty proc exec", NULL) == -1)
158 err((int)MANDOCLEVEL_SYSERR, "pledge");
161 #if HAVE_SANDBOX_INIT
162 if (sandbox_init(kSBXProfileNoInternet, SANDBOX_NAMED, NULL) == -1)
163 errx((int)MANDOCLEVEL_SYSERR, "sandbox_init");
166 /* Search options. */
168 memset(&conf, 0, sizeof(conf));
169 conf_file = defpaths = NULL;
172 memset(&search, 0, sizeof(struct mansearch));
173 search.outkey = "Nd";
176 if (strcmp(progname, BINM_MAN) == 0)
177 search.argmode = ARG_NAME;
178 else if (strcmp(progname, BINM_APROPOS) == 0)
179 search.argmode = ARG_EXPR;
180 else if (strcmp(progname, BINM_WHATIS) == 0)
181 search.argmode = ARG_WORD;
182 else if (strncmp(progname, "help", 4) == 0)
183 search.argmode = ARG_NAME;
185 search.argmode = ARG_FILE;
187 /* Parser and formatter options. */
189 memset(&curp, 0, sizeof(struct curparse));
190 curp.outtype = OUTT_LOCALE;
191 curp.outopts = &conf.output;
192 options = MPARSE_SO | MPARSE_UTF8 | MPARSE_LATIN1;
197 outmode = OUTMODE_DEF;
199 while ((c = getopt(argc, argv,
200 "aC:cfhI:iK:klM:m:O:S:s:T:VW:w")) != -1) {
201 if (c == 'i' && search.argmode == ARG_EXPR) {
207 outmode = OUTMODE_ALL;
216 search.argmode = ARG_WORD;
219 conf.output.synopsisonly = 1;
221 outmode = OUTMODE_ALL;
224 if (strncmp(optarg, "os=", 3)) {
225 warnx("-I %s: Bad argument", optarg);
226 return (int)MANDOCLEVEL_BADARG;
228 if (curp.os_s != NULL) {
229 warnx("-I %s: Duplicate argument", optarg);
230 return (int)MANDOCLEVEL_BADARG;
232 curp.os_s = mandoc_strdup(optarg + 3);
235 if ( ! koptions(&options, optarg))
236 return (int)MANDOCLEVEL_BADARG;
239 search.argmode = ARG_EXPR;
242 search.argmode = ARG_FILE;
243 outmode = OUTMODE_ALL;
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;
298 if (outmode == OUTMODE_LST)
299 search.outkey = oarg;
301 while (oarg != NULL) {
303 if (manconf_output(&conf.output,
304 strsep(&oarg, ","), 0) == 0)
306 warnx("-O %s: Bad argument", thisarg);
307 return (int)MANDOCLEVEL_BADARG;
312 if (curp.outtype != OUTT_TREE || !curp.outopts->noval)
313 options |= MPARSE_VALIDATE;
315 if (outmode == OUTMODE_FLN ||
316 outmode == OUTMODE_LST ||
317 !isatty(STDOUT_FILENO))
321 (conf.output.width == 0 || conf.output.indent == 0) &&
322 ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) != -1 &&
324 if (conf.output.width == 0 && ws.ws_col < 79)
325 conf.output.width = ws.ws_col - 1;
326 if (conf.output.indent == 0 && ws.ws_col < 66)
327 conf.output.indent = 3;
332 if (pledge("stdio rpath", NULL) == -1)
333 err((int)MANDOCLEVEL_SYSERR, "pledge");
336 /* Parse arguments. */
346 * and for a man(1) section argument without -s.
349 if (search.argmode == ARG_NAME) {
350 if (*progname == 'h') {
355 } else if (argc > 1 &&
356 ((uc = (unsigned char *)argv[0]) != NULL) &&
357 ((isdigit(uc[0]) && (uc[1] == '\0' ||
358 (isalpha(uc[1]) && uc[2] == '\0'))) ||
359 (uc[0] == 'n' && uc[1] == '\0'))) {
360 search.sec = (char *)uc;
364 if (search.arch == NULL)
365 search.arch = getenv("MACHINE");
367 if (search.arch == NULL)
368 search.arch = MACHINE;
373 * Use the first argument for -O tag in addition to
374 * using it as a search term for man(1) or apropos(1).
377 if (conf.output.tag != NULL && *conf.output.tag == '\0') {
378 tagarg = argc > 0 && search.argmode == ARG_EXPR ?
379 strchr(*argv, '=') : NULL;
380 conf.output.tag = tagarg == NULL ? *argv : tagarg + 1;
383 /* man(1), whatis(1), apropos(1) */
385 if (search.argmode != ARG_FILE) {
386 if (search.argmode == ARG_NAME &&
387 outmode == OUTMODE_ONE)
388 search.firstmatch = 1;
390 /* Access the mandoc database. */
392 manconf_parse(&conf, conf_file, defpaths, auxpaths);
393 if ( ! mansearch(&search, &conf.manpath,
394 argc, argv, &res, &sz))
395 usage(search.argmode);
397 if (sz == 0 && search.argmode == ARG_NAME)
398 fs_search(&search, &conf.manpath,
399 argc, argv, &res, &sz);
401 if (search.argmode == ARG_NAME) {
402 for (c = 0; c < argc; c++) {
403 if (strchr(argv[c], '/') == NULL)
405 if (access(argv[c], R_OK) == -1) {
409 res = mandoc_reallocarray(res,
410 sz + 1, sizeof(*res));
411 res[sz].file = mandoc_strdup(argv[c]);
412 res[sz].names = NULL;
413 res[sz].output = NULL;
414 res[sz].ipath = SIZE_MAX;
416 res[sz].form = FORM_SRC;
422 if (search.argmode != ARG_NAME)
423 warnx("nothing appropriate");
424 mandoc_msg_setrc(MANDOCLEVEL_BADARG);
429 * For standard man(1) and -a output mode,
430 * prepare for copying filename pointers
431 * into the program parameter array.
434 if (outmode == OUTMODE_ONE) {
437 } else if (outmode == OUTMODE_ALL)
440 /* Iterate all matching manuals. */
443 for (i = 0; i < sz; i++) {
444 if (outmode == OUTMODE_FLN)
446 else if (outmode == OUTMODE_LST)
447 printf("%s - %s\n", res[i].names,
448 res[i].output == NULL ? "" :
450 else if (outmode == OUTMODE_ONE) {
451 /* Search for the best section. */
453 sec += strcspn(sec, "123456789");
456 prio = sec_prios[sec[0] - '1'];
459 if (prio >= best_prio)
467 * For man(1), -a and -i output mode, fall through
468 * to the main mandoc(1) code iterating files
469 * and running the parsers on each of them.
472 if (outmode == OUTMODE_FLN || outmode == OUTMODE_LST)
480 if (pledge("stdio rpath tmppath tty proc exec", NULL) == -1)
481 err((int)MANDOCLEVEL_SYSERR, "pledge");
483 if (pledge("stdio rpath", NULL) == -1)
484 err((int)MANDOCLEVEL_SYSERR, "pledge");
488 if (search.argmode == ARG_FILE)
489 moptions(&options, auxpaths);
492 curp.mp = mparse_alloc(options, curp.os_e, curp.os_s);
496 tag_files = tag_init();
497 tag_files->tagname = conf.output.tag;
500 mandoc_msg_setinfilename(thisarg);
501 parse(&curp, STDIN_FILENO, thisarg);
502 mandoc_msg_setinfilename(NULL);
506 * Remember the original working directory, if possible.
507 * This will be needed if some names on the command line
508 * are page names and some are relative file names.
509 * Do not error out if the current directory is not
510 * readable: Maybe it won't be needed after all.
512 startdir = open(".", O_RDONLY | O_DIRECTORY);
517 * Changing directories is not needed in ARG_FILE mode.
518 * Do it on a best-effort basis. Even in case of
519 * failure, some functionality may still work.
522 if (resp->ipath != SIZE_MAX)
523 (void)chdir(conf.manpath.paths[resp->ipath]);
524 else if (startdir != -1)
525 (void)fchdir(startdir);
526 thisarg = resp->file;
530 fd = mparse_open(curp.mp, thisarg);
534 tag_files = tag_init();
535 tag_files->tagname = conf.output.tag;
538 mandoc_msg_setinfilename(thisarg);
539 if (resp == NULL || resp->form == FORM_SRC)
540 parse(&curp, fd, thisarg);
542 passthrough(resp->file, fd,
543 conf.output.synopsisonly);
544 mandoc_msg_setinfilename(NULL);
546 if (ferror(stdout)) {
547 if (tag_files != NULL) {
548 warn("%s", tag_files->ofn);
553 mandoc_msg_setrc(MANDOCLEVEL_SYSERR);
557 if (argc > 1 && curp.outtype <= OUTT_UTF8) {
558 if (curp.outdata == NULL)
559 outdata_alloc(&curp);
560 terminal_sepline(curp.outdata);
563 mandoc_msg(MANDOCERR_FILE, 0, 0,
564 "%s: %s", thisarg, strerror(errno));
566 if (curp.wstop && mandoc_msg_getrc() != MANDOCLEVEL_OK)
574 mparse_reset(curp.mp);
576 if (startdir != -1) {
577 (void)fchdir(startdir);
581 if (curp.outdata != NULL) {
582 switch (curp.outtype) {
584 html_free(curp.outdata);
589 ascii_free(curp.outdata);
593 pspdf_free(curp.outdata);
600 mparse_free(curp.mp);
604 if (search.argmode != ARG_FILE) {
606 mansearch_free(res, sz);
612 * When using a pager, finish writing both temporary files,
613 * fork it, wait for the user to close it, and clean up.
616 if (tag_files != NULL) {
619 man_pgid = getpgid(0);
620 tag_files->tcpgid = man_pgid == getpid() ?
621 getpgid(getppid()) : man_pgid;
626 /* Stop here until moved to the foreground. */
628 tc_pgid = tcgetpgrp(tag_files->ofd);
629 if (tc_pgid != man_pgid) {
630 if (tc_pgid == pager_pid) {
631 (void)tcsetpgrp(tag_files->ofd,
633 if (signum == SIGTTIN)
636 tag_files->tcpgid = tc_pgid;
641 /* Once in the foreground, activate the pager. */
644 (void)tcsetpgrp(tag_files->ofd, pager_pid);
645 kill(pager_pid, SIGCONT);
647 pager_pid = spawn_pager(tag_files);
649 /* Wait for the pager to stop or exit. */
651 while ((pid = waitpid(pager_pid, &status,
652 WUNTRACED)) == -1 && errno == EINTR)
657 mandoc_msg_setrc(MANDOCLEVEL_SYSERR);
660 if (!WIFSTOPPED(status))
663 signum = WSTOPSIG(status);
667 return (int)mandoc_msg_getrc();
671 usage(enum argmode argmode)
676 fputs("usage: mandoc [-ac] [-I os=name] "
677 "[-K encoding] [-mdoc | -man] [-O options]\n"
678 "\t [-T output] [-W level] [file ...]\n", stderr);
681 fputs("usage: man [-acfhklw] [-C file] [-M path] "
682 "[-m path] [-S subsection]\n"
683 "\t [[-s] section] name ...\n", stderr);
686 fputs("usage: whatis [-afk] [-C file] "
687 "[-M path] [-m path] [-O outkey] [-S arch]\n"
688 "\t [-s section] name ...\n", stderr);
691 fputs("usage: apropos [-afk] [-C file] "
692 "[-M path] [-m path] [-O outkey] [-S arch]\n"
693 "\t [-s section] expression ...\n", stderr);
696 exit((int)MANDOCLEVEL_BADARG);
700 fs_lookup(const struct manpaths *paths, size_t ipath,
701 const char *sec, const char *arch, const char *name,
702 struct manpage **res, size_t *ressz)
705 struct manpage *page;
711 mandoc_asprintf(&file, "%s/man%s/%s.%s",
712 paths->paths[ipath], sec, name, sec);
713 if (access(file, R_OK) != -1)
717 mandoc_asprintf(&file, "%s/cat%s/%s.0",
718 paths->paths[ipath], sec, name);
719 if (access(file, R_OK) != -1) {
726 mandoc_asprintf(&file, "%s/man%s/%s/%s.%s",
727 paths->paths[ipath], sec, arch, name, sec);
728 if (access(file, R_OK) != -1)
733 mandoc_asprintf(&file, "%s/man%s/%s.[01-9]*",
734 paths->paths[ipath], sec, name);
735 globres = glob(file, 0, NULL, &globinfo);
736 if (globres != 0 && globres != GLOB_NOMATCH)
737 warn("%s: glob", file);
740 file = mandoc_strdup(*globinfo.gl_pathv);
744 if (res != NULL || ipath + 1 != paths->sz)
747 mandoc_asprintf(&file, "%s.%s", name, sec);
748 globres = access(file, R_OK);
750 return globres != -1;
753 warnx("outdated mandoc.db lacks %s(%s) entry, run %s %s",
754 name, sec, BINM_MAKEWHATIS, paths->paths[ipath]);
759 *res = mandoc_reallocarray(*res, ++*ressz, sizeof(struct manpage));
760 page = *res + (*ressz - 1);
765 page->sec = (*sec >= '1' && *sec <= '9') ? *sec - '1' + 1 : 10;
771 fs_search(const struct mansearch *cfg, const struct manpaths *paths,
772 int argc, char **argv, struct manpage **res, size_t *ressz)
774 const char *const sections[] =
775 {"1", "8", "6", "2", "3", "5", "7", "4", "9", "3p"};
776 const size_t nsec = sizeof(sections)/sizeof(sections[0]);
778 size_t ipath, isec, lastsz;
780 assert(cfg->argmode == ARG_NAME);
786 for (ipath = 0; ipath < paths->sz; ipath++) {
787 if (cfg->sec != NULL) {
788 if (fs_lookup(paths, ipath, cfg->sec,
789 cfg->arch, *argv, res, ressz) &&
792 } else for (isec = 0; isec < nsec; isec++)
793 if (fs_lookup(paths, ipath, sections[isec],
794 cfg->arch, *argv, res, ressz) &&
798 if (res != NULL && *ressz == lastsz &&
799 strchr(*argv, '/') == NULL) {
800 if (cfg->arch != NULL &&
801 arch_valid(cfg->arch, OSENUM) == 0)
802 warnx("Unknown architecture \"%s\".",
804 else if (cfg->sec == NULL)
805 warnx("No entry for %s in the manual.",
808 warnx("No entry for %s in section %s "
809 "of the manual.", *argv, cfg->sec);
819 parse(struct curparse *curp, int fd, const char *file)
821 struct roff_meta *meta;
823 /* Begin by parsing the file itself. */
828 mparse_readfd(curp->mp, fd, file);
829 if (fd != STDIN_FILENO)
833 * With -Wstop and warnings or errors of at least the requested
834 * level, do not produce output.
837 if (curp->wstop && mandoc_msg_getrc() != MANDOCLEVEL_OK)
840 if (curp->outdata == NULL)
842 else if (curp->outtype == OUTT_HTML)
846 meta = mparse_result(curp->mp);
848 /* Execute the out device, if it exists. */
850 if (meta->macroset == MACROSET_MDOC) {
851 switch (curp->outtype) {
853 html_mdoc(curp->outdata, meta);
856 tree_mdoc(curp->outdata, meta);
859 man_mdoc(curp->outdata, meta);
866 terminal_mdoc(curp->outdata, meta);
869 markdown_mdoc(curp->outdata, meta);
875 if (meta->macroset == MACROSET_MAN) {
876 switch (curp->outtype) {
878 html_man(curp->outdata, meta);
881 tree_man(curp->outdata, meta);
884 mparse_copy(curp->mp);
891 terminal_man(curp->outdata, meta);
897 if (mandoc_msg_getmin() < MANDOCERR_STYLE)
904 static struct manpaths paths;
905 struct mansearch search;
906 struct mandoc_xr *xr;
910 manpath_base(&paths);
912 for (xr = mandoc_xr_get(); xr != NULL; xr = xr->next) {
916 search.sec = xr->sec;
917 search.outkey = NULL;
918 search.argmode = ARG_NAME;
919 search.firstmatch = 1;
920 if (mansearch(&search, &paths, 1, &xr->name, NULL, &sz))
922 if (fs_search(&search, &paths, 1, &xr->name, NULL, &sz))
925 mandoc_msg(MANDOCERR_XR_BAD, xr->line,
926 xr->pos + 1, "Xr %s %s", xr->name, xr->sec);
928 mandoc_msg(MANDOCERR_XR_BAD, xr->line,
929 xr->pos + 1, "Xr %s %s (%d times)",
930 xr->name, xr->sec, xr->count);
935 outdata_alloc(struct curparse *curp)
937 switch (curp->outtype) {
939 curp->outdata = html_alloc(curp->outopts);
942 curp->outdata = utf8_alloc(curp->outopts);
945 curp->outdata = locale_alloc(curp->outopts);
948 curp->outdata = ascii_alloc(curp->outopts);
951 curp->outdata = pdf_alloc(curp->outopts);
954 curp->outdata = ps_alloc(curp->outopts);
962 passthrough(const char *file, int fd, int synopsis_only)
964 const char synb[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
965 const char synr[] = "SYNOPSIS";
971 ssize_t len, written;
977 if (fflush(stdout) == EOF) {
982 if ((stream = fdopen(fd, "r")) == NULL) {
989 while ((len = getline(&line, &linesz, stream)) != -1) {
993 if ( ! isspace((unsigned char)*cp))
995 while (isspace((unsigned char)*cp)) {
1000 if (strcmp(cp, synb) == 0 ||
1001 strcmp(cp, synr) == 0)
1006 for (; len > 0; len -= written) {
1007 if ((written = write(STDOUT_FILENO, cp, len)) != -1)
1015 if (ferror(stream)) {
1017 syscall = "getline";
1028 warn("%s: SYSERR: %s", file, syscall);
1029 mandoc_msg_setrc(MANDOCLEVEL_SYSERR);
1033 koptions(int *options, char *arg)
1036 if ( ! strcmp(arg, "utf-8")) {
1037 *options |= MPARSE_UTF8;
1038 *options &= ~MPARSE_LATIN1;
1039 } else if ( ! strcmp(arg, "iso-8859-1")) {
1040 *options |= MPARSE_LATIN1;
1041 *options &= ~MPARSE_UTF8;
1042 } else if ( ! strcmp(arg, "us-ascii")) {
1043 *options &= ~(MPARSE_UTF8 | MPARSE_LATIN1);
1045 warnx("-K %s: Bad argument", arg);
1052 moptions(int *options, char *arg)
1057 if (strcmp(arg, "doc") == 0)
1058 *options |= MPARSE_MDOC;
1059 else if (strcmp(arg, "an") == 0)
1060 *options |= MPARSE_MAN;
1064 toptions(struct curparse *curp, char *arg)
1067 if (0 == strcmp(arg, "ascii"))
1068 curp->outtype = OUTT_ASCII;
1069 else if (0 == strcmp(arg, "lint")) {
1070 curp->outtype = OUTT_LINT;
1071 mandoc_msg_setoutfile(stdout);
1072 mandoc_msg_setmin(MANDOCERR_BASE);
1073 } else if (0 == strcmp(arg, "tree"))
1074 curp->outtype = OUTT_TREE;
1075 else if (0 == strcmp(arg, "man"))
1076 curp->outtype = OUTT_MAN;
1077 else if (0 == strcmp(arg, "html"))
1078 curp->outtype = OUTT_HTML;
1079 else if (0 == strcmp(arg, "markdown"))
1080 curp->outtype = OUTT_MARKDOWN;
1081 else if (0 == strcmp(arg, "utf8"))
1082 curp->outtype = OUTT_UTF8;
1083 else if (0 == strcmp(arg, "locale"))
1084 curp->outtype = OUTT_LOCALE;
1085 else if (0 == strcmp(arg, "ps"))
1086 curp->outtype = OUTT_PS;
1087 else if (0 == strcmp(arg, "pdf"))
1088 curp->outtype = OUTT_PDF;
1090 warnx("-T %s: Bad argument", arg);
1098 woptions(struct curparse *curp, char *arg)
1101 const char *toks[11];
1107 toks[4] = "warning";
1111 toks[8] = "openbsd";
1117 switch (getsubopt(&arg, (char * const *)toks, &v)) {
1123 mandoc_msg_setmin(MANDOCERR_BASE);
1126 mandoc_msg_setmin(MANDOCERR_STYLE);
1129 mandoc_msg_setmin(MANDOCERR_WARNING);
1132 mandoc_msg_setmin(MANDOCERR_ERROR);
1135 mandoc_msg_setmin(MANDOCERR_UNSUPP);
1138 mandoc_msg_setmin(MANDOCERR_MAX);
1141 mandoc_msg_setmin(MANDOCERR_BASE);
1142 curp->os_e = MANDOC_OS_OPENBSD;
1145 mandoc_msg_setmin(MANDOCERR_BASE);
1146 curp->os_e = MANDOC_OS_NETBSD;
1149 warnx("-W %s: Bad argument", o);
1157 spawn_pager(struct tag_files *tag_files)
1159 const struct timespec timeout = { 0, 100000000 }; /* 0.1s */
1160 #define MAX_PAGER_ARGS 16
1161 char *argv[MAX_PAGER_ARGS];
1170 pager = getenv("MANPAGER");
1171 if (pager == NULL || *pager == '\0')
1172 pager = getenv("PAGER");
1173 if (pager == NULL || *pager == '\0')
1175 cp = mandoc_strdup(pager);
1178 * Parse the pager command into words.
1179 * Intentionally do not do anything fancy here.
1183 while (argc + 5 < MAX_PAGER_ARGS) {
1185 cp = strchr(cp, ' ');
1195 /* For less(1), use the tag file. */
1199 if ((cmdlen = strlen(argv[0])) >= 4) {
1200 cp = argv[0] + cmdlen - 4;
1201 if (strcmp(cp, "less") == 0) {
1202 argv[argc++] = mandoc_strdup("-T");
1203 argv[argc++] = tag_files->tfn;
1204 if (tag_files->tagname != NULL) {
1205 argv[argc++] = mandoc_strdup("-t");
1206 argv[argc++] = tag_files->tagname;
1213 argv[argc++] = tag_files->ofn;
1216 switch (pager_pid = fork()) {
1218 err((int)MANDOCLEVEL_SYSERR, "fork");
1222 (void)setpgid(pager_pid, 0);
1223 (void)tcsetpgrp(tag_files->ofd, pager_pid);
1225 if (pledge("stdio rpath tmppath tty proc", NULL) == -1)
1226 err((int)MANDOCLEVEL_SYSERR, "pledge");
1228 tag_files->pager_pid = pager_pid;
1232 /* The child process becomes the pager. */
1234 if (dup2(tag_files->ofd, STDOUT_FILENO) == -1)
1235 err((int)MANDOCLEVEL_SYSERR, "pager stdout");
1236 close(tag_files->ofd);
1237 assert(tag_files->tfd == -1);
1239 /* Do not start the pager before controlling the terminal. */
1241 while (tcgetpgrp(STDOUT_FILENO) != getpid())
1242 nanosleep(&timeout, NULL);
1244 execvp(argv[0], argv);
1245 err((int)MANDOCLEVEL_SYSERR, "exec %s", argv[0]);