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;
247 defpaths = strdup(optarg);
248 if (defpaths == NULL)
261 search.arch = optarg;
267 if ( ! toptions(&curp, optarg))
268 return (int)MANDOCLEVEL_BADARG;
271 if ( ! woptions(&curp, optarg))
272 return (int)MANDOCLEVEL_BADARG;
275 outmode = OUTMODE_FLN;
284 usage(search.argmode);
286 /* Postprocess options. */
288 if (outmode == OUTMODE_DEF) {
289 switch (search.argmode) {
291 outmode = OUTMODE_ALL;
295 outmode = OUTMODE_ONE;
298 outmode = OUTMODE_LST;
304 if (outmode == OUTMODE_LST)
305 search.outkey = oarg;
307 while (oarg != NULL) {
309 if (manconf_output(&conf.output,
310 strsep(&oarg, ","), 0) == 0)
312 warnx("-O %s: Bad argument", thisarg);
313 return (int)MANDOCLEVEL_BADARG;
318 if (curp.outtype != OUTT_TREE || !curp.outopts->noval)
319 options |= MPARSE_VALIDATE;
321 if (outmode == OUTMODE_FLN ||
322 outmode == OUTMODE_LST ||
323 !isatty(STDOUT_FILENO))
327 (conf.output.width == 0 || conf.output.indent == 0) &&
328 ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) != -1 &&
330 if (conf.output.width == 0 && ws.ws_col < 79)
331 conf.output.width = ws.ws_col - 1;
332 if (conf.output.indent == 0 && ws.ws_col < 66)
333 conf.output.indent = 3;
338 if (pledge("stdio rpath", NULL) == -1)
339 err((int)MANDOCLEVEL_SYSERR, "pledge");
342 /* Parse arguments. */
352 * and for a man(1) section argument without -s.
355 if (search.argmode == ARG_NAME) {
356 if (*progname == 'h') {
361 } else if (argc > 1 &&
362 ((uc = (unsigned char *)argv[0]) != NULL) &&
363 ((isdigit(uc[0]) && (uc[1] == '\0' ||
364 (isalpha(uc[1]) && uc[2] == '\0'))) ||
365 (uc[0] == 'n' && uc[1] == '\0'))) {
366 search.sec = (char *)uc;
370 if (search.arch == NULL)
371 search.arch = getenv("MACHINE");
373 if (search.arch == NULL)
374 search.arch = MACHINE;
379 * Use the first argument for -O tag in addition to
380 * using it as a search term for man(1) or apropos(1).
383 if (conf.output.tag != NULL && *conf.output.tag == '\0') {
384 tagarg = argc > 0 && search.argmode == ARG_EXPR ?
385 strchr(*argv, '=') : NULL;
386 conf.output.tag = tagarg == NULL ? *argv : tagarg + 1;
389 /* man(1), whatis(1), apropos(1) */
391 if (search.argmode != ARG_FILE) {
392 if (search.argmode == ARG_NAME &&
393 outmode == OUTMODE_ONE)
394 search.firstmatch = 1;
398 * Use manpath(1) to populate defpaths if -M is not specified.
399 * Don't treat any failures as fatal.
401 if (defpaths == NULL) {
406 if ((fp = popen("/usr/bin/manpath -q", "r")) != NULL) {
407 if ((linelen = getline(&defpaths,
408 &linecap, fp)) > 0) {
409 /* Strip trailing newline */
410 defpaths[linelen - 1] = '\0';
417 /* Access the mandoc database. */
419 manconf_parse(&conf, conf_file, defpaths, auxpaths);
424 if ( ! mansearch(&search, &conf.manpath,
425 argc, argv, &res, &sz))
426 usage(search.argmode);
428 if (sz == 0 && search.argmode == ARG_NAME)
429 fs_search(&search, &conf.manpath,
430 argc, argv, &res, &sz);
432 if (search.argmode == ARG_NAME) {
433 for (c = 0; c < argc; c++) {
434 if (strchr(argv[c], '/') == NULL)
436 if (access(argv[c], R_OK) == -1) {
440 res = mandoc_reallocarray(res,
441 sz + 1, sizeof(*res));
442 res[sz].file = mandoc_strdup(argv[c]);
443 res[sz].names = NULL;
444 res[sz].output = NULL;
445 res[sz].ipath = SIZE_MAX;
447 res[sz].form = FORM_SRC;
453 if (search.argmode != ARG_NAME)
454 warnx("nothing appropriate");
455 mandoc_msg_setrc(MANDOCLEVEL_BADARG);
460 * For standard man(1) and -a output mode,
461 * prepare for copying filename pointers
462 * into the program parameter array.
465 if (outmode == OUTMODE_ONE) {
468 } else if (outmode == OUTMODE_ALL)
471 /* Iterate all matching manuals. */
474 for (i = 0; i < sz; i++) {
475 if (outmode == OUTMODE_FLN)
477 else if (outmode == OUTMODE_LST)
478 printf("%s - %s\n", res[i].names,
479 res[i].output == NULL ? "" :
481 else if (outmode == OUTMODE_ONE) {
482 /* Search for the best section. */
484 sec += strcspn(sec, "123456789");
487 prio = sec_prios[sec[0] - '1'];
490 if (prio >= best_prio)
498 * For man(1), -a and -i output mode, fall through
499 * to the main mandoc(1) code iterating files
500 * and running the parsers on each of them.
503 if (outmode == OUTMODE_FLN || outmode == OUTMODE_LST)
511 if (pledge("stdio rpath tmppath tty proc exec", NULL) == -1)
512 err((int)MANDOCLEVEL_SYSERR, "pledge");
514 if (pledge("stdio rpath", NULL) == -1)
515 err((int)MANDOCLEVEL_SYSERR, "pledge");
519 if (search.argmode == ARG_FILE)
520 moptions(&options, auxpaths);
523 curp.mp = mparse_alloc(options, curp.os_e, curp.os_s);
527 tag_files = tag_init();
528 tag_files->tagname = conf.output.tag;
531 mandoc_msg_setinfilename(thisarg);
532 parse(&curp, STDIN_FILENO, thisarg);
533 mandoc_msg_setinfilename(NULL);
537 * Remember the original working directory, if possible.
538 * This will be needed if some names on the command line
539 * are page names and some are relative file names.
540 * Do not error out if the current directory is not
541 * readable: Maybe it won't be needed after all.
543 startdir = open(".", O_RDONLY | O_DIRECTORY);
548 * Changing directories is not needed in ARG_FILE mode.
549 * Do it on a best-effort basis. Even in case of
550 * failure, some functionality may still work.
553 if (resp->ipath != SIZE_MAX)
554 (void)chdir(conf.manpath.paths[resp->ipath]);
555 else if (startdir != -1)
556 (void)fchdir(startdir);
557 thisarg = resp->file;
561 fd = mparse_open(curp.mp, thisarg);
565 tag_files = tag_init();
566 tag_files->tagname = conf.output.tag;
569 mandoc_msg_setinfilename(thisarg);
570 if (resp == NULL || resp->form == FORM_SRC)
571 parse(&curp, fd, thisarg);
573 passthrough(resp->file, fd,
574 conf.output.synopsisonly);
575 mandoc_msg_setinfilename(NULL);
577 if (ferror(stdout)) {
578 if (tag_files != NULL) {
579 warn("%s", tag_files->ofn);
584 mandoc_msg_setrc(MANDOCLEVEL_SYSERR);
588 if (argc > 1 && curp.outtype <= OUTT_UTF8) {
589 if (curp.outdata == NULL)
590 outdata_alloc(&curp);
591 terminal_sepline(curp.outdata);
594 mandoc_msg(MANDOCERR_FILE, 0, 0,
595 "%s: %s", thisarg, strerror(errno));
597 if (curp.wstop && mandoc_msg_getrc() != MANDOCLEVEL_OK)
605 mparse_reset(curp.mp);
607 if (startdir != -1) {
608 (void)fchdir(startdir);
612 if (curp.outdata != NULL) {
613 switch (curp.outtype) {
615 html_free(curp.outdata);
620 ascii_free(curp.outdata);
624 pspdf_free(curp.outdata);
631 mparse_free(curp.mp);
635 if (search.argmode != ARG_FILE) {
637 mansearch_free(res, sz);
643 * When using a pager, finish writing both temporary files,
644 * fork it, wait for the user to close it, and clean up.
647 if (tag_files != NULL) {
650 man_pgid = getpgid(0);
651 tag_files->tcpgid = man_pgid == getpid() ?
652 getpgid(getppid()) : man_pgid;
657 /* Stop here until moved to the foreground. */
659 tc_pgid = tcgetpgrp(tag_files->ofd);
660 if (tc_pgid != man_pgid) {
661 if (tc_pgid == pager_pid) {
662 (void)tcsetpgrp(tag_files->ofd,
664 if (signum == SIGTTIN)
667 tag_files->tcpgid = tc_pgid;
672 /* Once in the foreground, activate the pager. */
675 (void)tcsetpgrp(tag_files->ofd, pager_pid);
676 kill(pager_pid, SIGCONT);
678 pager_pid = spawn_pager(tag_files);
680 /* Wait for the pager to stop or exit. */
682 while ((pid = waitpid(pager_pid, &status,
683 WUNTRACED)) == -1 && errno == EINTR)
688 mandoc_msg_setrc(MANDOCLEVEL_SYSERR);
691 if (!WIFSTOPPED(status))
694 signum = WSTOPSIG(status);
698 return (int)mandoc_msg_getrc();
702 usage(enum argmode argmode)
707 fputs("usage: mandoc [-ac] [-I os=name] "
708 "[-K encoding] [-mdoc | -man] [-O options]\n"
709 "\t [-T output] [-W level] [file ...]\n", stderr);
712 fputs("usage: man [-acfhklw] [-C file] [-M path] "
713 "[-m path] [-S subsection]\n"
714 "\t [[-s] section] name ...\n", stderr);
717 fputs("usage: whatis [-afk] [-C file] "
718 "[-M path] [-m path] [-O outkey] [-S arch]\n"
719 "\t [-s section] name ...\n", stderr);
722 fputs("usage: apropos [-afk] [-C file] "
723 "[-M path] [-m path] [-O outkey] [-S arch]\n"
724 "\t [-s section] expression ...\n", stderr);
727 exit((int)MANDOCLEVEL_BADARG);
731 fs_lookup(const struct manpaths *paths, size_t ipath,
732 const char *sec, const char *arch, const char *name,
733 struct manpage **res, size_t *ressz)
736 struct manpage *page;
742 mandoc_asprintf(&file, "%s/man%s/%s.%s",
743 paths->paths[ipath], sec, name, sec);
744 if (access(file, R_OK) != -1)
748 mandoc_asprintf(&file, "%s/cat%s/%s.0",
749 paths->paths[ipath], sec, name);
750 if (access(file, R_OK) != -1) {
757 mandoc_asprintf(&file, "%s/man%s/%s/%s.%s",
758 paths->paths[ipath], sec, arch, name, sec);
759 if (access(file, R_OK) != -1)
764 mandoc_asprintf(&file, "%s/man%s/%s.[01-9]*",
765 paths->paths[ipath], sec, name);
766 globres = glob(file, 0, NULL, &globinfo);
767 if (globres != 0 && globres != GLOB_NOMATCH)
768 warn("%s: glob", file);
771 file = mandoc_strdup(*globinfo.gl_pathv);
775 if (res != NULL || ipath + 1 != paths->sz)
778 mandoc_asprintf(&file, "%s.%s", name, sec);
779 globres = access(file, R_OK);
781 return globres != -1;
784 warnx("outdated mandoc.db lacks %s(%s) entry, run %s %s",
785 name, sec, BINM_MAKEWHATIS, paths->paths[ipath]);
790 *res = mandoc_reallocarray(*res, ++*ressz, sizeof(struct manpage));
791 page = *res + (*ressz - 1);
796 page->sec = (*sec >= '1' && *sec <= '9') ? *sec - '1' + 1 : 10;
802 fs_search(const struct mansearch *cfg, const struct manpaths *paths,
803 int argc, char **argv, struct manpage **res, size_t *ressz)
805 const char *const sections[] =
806 {"1", "8", "6", "2", "3", "5", "7", "4", "9", "3p"};
807 const size_t nsec = sizeof(sections)/sizeof(sections[0]);
809 size_t ipath, isec, lastsz;
811 assert(cfg->argmode == ARG_NAME);
817 for (ipath = 0; ipath < paths->sz; ipath++) {
818 if (cfg->sec != NULL) {
819 if (fs_lookup(paths, ipath, cfg->sec,
820 cfg->arch, *argv, res, ressz) &&
823 } else for (isec = 0; isec < nsec; isec++)
824 if (fs_lookup(paths, ipath, sections[isec],
825 cfg->arch, *argv, res, ressz) &&
829 if (res != NULL && *ressz == lastsz &&
830 strchr(*argv, '/') == NULL) {
831 if (cfg->arch != NULL &&
832 arch_valid(cfg->arch, OSENUM) == 0)
833 warnx("Unknown architecture \"%s\".",
835 else if (cfg->sec == NULL)
836 warnx("No entry for %s in the manual.",
839 warnx("No entry for %s in section %s "
840 "of the manual.", *argv, cfg->sec);
850 parse(struct curparse *curp, int fd, const char *file)
852 struct roff_meta *meta;
854 /* Begin by parsing the file itself. */
859 mparse_readfd(curp->mp, fd, file);
860 if (fd != STDIN_FILENO)
864 * With -Wstop and warnings or errors of at least the requested
865 * level, do not produce output.
868 if (curp->wstop && mandoc_msg_getrc() != MANDOCLEVEL_OK)
871 if (curp->outdata == NULL)
873 else if (curp->outtype == OUTT_HTML)
877 meta = mparse_result(curp->mp);
879 /* Execute the out device, if it exists. */
881 if (meta->macroset == MACROSET_MDOC) {
882 switch (curp->outtype) {
884 html_mdoc(curp->outdata, meta);
887 tree_mdoc(curp->outdata, meta);
890 man_mdoc(curp->outdata, meta);
897 terminal_mdoc(curp->outdata, meta);
900 markdown_mdoc(curp->outdata, meta);
906 if (meta->macroset == MACROSET_MAN) {
907 switch (curp->outtype) {
909 html_man(curp->outdata, meta);
912 tree_man(curp->outdata, meta);
915 mparse_copy(curp->mp);
922 terminal_man(curp->outdata, meta);
928 if (mandoc_msg_getmin() < MANDOCERR_STYLE)
935 static struct manpaths paths;
936 struct mansearch search;
937 struct mandoc_xr *xr;
941 manpath_base(&paths);
943 for (xr = mandoc_xr_get(); xr != NULL; xr = xr->next) {
947 search.sec = xr->sec;
948 search.outkey = NULL;
949 search.argmode = ARG_NAME;
950 search.firstmatch = 1;
951 if (mansearch(&search, &paths, 1, &xr->name, NULL, &sz))
953 if (fs_search(&search, &paths, 1, &xr->name, NULL, &sz))
956 mandoc_msg(MANDOCERR_XR_BAD, xr->line,
957 xr->pos + 1, "Xr %s %s", xr->name, xr->sec);
959 mandoc_msg(MANDOCERR_XR_BAD, xr->line,
960 xr->pos + 1, "Xr %s %s (%d times)",
961 xr->name, xr->sec, xr->count);
966 outdata_alloc(struct curparse *curp)
968 switch (curp->outtype) {
970 curp->outdata = html_alloc(curp->outopts);
973 curp->outdata = utf8_alloc(curp->outopts);
976 curp->outdata = locale_alloc(curp->outopts);
979 curp->outdata = ascii_alloc(curp->outopts);
982 curp->outdata = pdf_alloc(curp->outopts);
985 curp->outdata = ps_alloc(curp->outopts);
993 passthrough(const char *file, int fd, int synopsis_only)
995 const char synb[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
996 const char synr[] = "SYNOPSIS";
1002 ssize_t len, written;
1008 if (fflush(stdout) == EOF) {
1013 if ((stream = fdopen(fd, "r")) == NULL) {
1020 while ((len = getline(&line, &linesz, stream)) != -1) {
1022 if (synopsis_only) {
1024 if ( ! isspace((unsigned char)*cp))
1026 while (isspace((unsigned char)*cp)) {
1031 if (strcmp(cp, synb) == 0 ||
1032 strcmp(cp, synr) == 0)
1037 for (; len > 0; len -= written) {
1038 if ((written = write(STDOUT_FILENO, cp, len)) != -1)
1046 if (ferror(stream)) {
1048 syscall = "getline";
1059 warn("%s: SYSERR: %s", file, syscall);
1060 mandoc_msg_setrc(MANDOCLEVEL_SYSERR);
1064 koptions(int *options, char *arg)
1067 if ( ! strcmp(arg, "utf-8")) {
1068 *options |= MPARSE_UTF8;
1069 *options &= ~MPARSE_LATIN1;
1070 } else if ( ! strcmp(arg, "iso-8859-1")) {
1071 *options |= MPARSE_LATIN1;
1072 *options &= ~MPARSE_UTF8;
1073 } else if ( ! strcmp(arg, "us-ascii")) {
1074 *options &= ~(MPARSE_UTF8 | MPARSE_LATIN1);
1076 warnx("-K %s: Bad argument", arg);
1083 moptions(int *options, char *arg)
1088 if (strcmp(arg, "doc") == 0)
1089 *options |= MPARSE_MDOC;
1090 else if (strcmp(arg, "an") == 0)
1091 *options |= MPARSE_MAN;
1095 toptions(struct curparse *curp, char *arg)
1098 if (0 == strcmp(arg, "ascii"))
1099 curp->outtype = OUTT_ASCII;
1100 else if (0 == strcmp(arg, "lint")) {
1101 curp->outtype = OUTT_LINT;
1102 mandoc_msg_setoutfile(stdout);
1103 mandoc_msg_setmin(MANDOCERR_BASE);
1104 } else if (0 == strcmp(arg, "tree"))
1105 curp->outtype = OUTT_TREE;
1106 else if (0 == strcmp(arg, "man"))
1107 curp->outtype = OUTT_MAN;
1108 else if (0 == strcmp(arg, "html"))
1109 curp->outtype = OUTT_HTML;
1110 else if (0 == strcmp(arg, "markdown"))
1111 curp->outtype = OUTT_MARKDOWN;
1112 else if (0 == strcmp(arg, "utf8"))
1113 curp->outtype = OUTT_UTF8;
1114 else if (0 == strcmp(arg, "locale"))
1115 curp->outtype = OUTT_LOCALE;
1116 else if (0 == strcmp(arg, "ps"))
1117 curp->outtype = OUTT_PS;
1118 else if (0 == strcmp(arg, "pdf"))
1119 curp->outtype = OUTT_PDF;
1121 warnx("-T %s: Bad argument", arg);
1129 woptions(struct curparse *curp, char *arg)
1132 const char *toks[11];
1138 toks[4] = "warning";
1142 toks[8] = "openbsd";
1148 switch (getsubopt(&arg, (char * const *)toks, &v)) {
1154 mandoc_msg_setmin(MANDOCERR_BASE);
1157 mandoc_msg_setmin(MANDOCERR_STYLE);
1160 mandoc_msg_setmin(MANDOCERR_WARNING);
1163 mandoc_msg_setmin(MANDOCERR_ERROR);
1166 mandoc_msg_setmin(MANDOCERR_UNSUPP);
1169 mandoc_msg_setmin(MANDOCERR_MAX);
1172 mandoc_msg_setmin(MANDOCERR_BASE);
1173 curp->os_e = MANDOC_OS_OPENBSD;
1176 mandoc_msg_setmin(MANDOCERR_BASE);
1177 curp->os_e = MANDOC_OS_NETBSD;
1180 warnx("-W %s: Bad argument", o);
1188 spawn_pager(struct tag_files *tag_files)
1190 const struct timespec timeout = { 0, 100000000 }; /* 0.1s */
1191 #define MAX_PAGER_ARGS 16
1192 char *argv[MAX_PAGER_ARGS];
1201 pager = getenv("MANPAGER");
1202 if (pager == NULL || *pager == '\0')
1203 pager = getenv("PAGER");
1204 if (pager == NULL || *pager == '\0')
1206 cp = mandoc_strdup(pager);
1209 * Parse the pager command into words.
1210 * Intentionally do not do anything fancy here.
1214 while (argc + 5 < MAX_PAGER_ARGS) {
1216 cp = strchr(cp, ' ');
1226 /* For less(1), use the tag file. */
1230 if ((cmdlen = strlen(argv[0])) >= 4) {
1231 cp = argv[0] + cmdlen - 4;
1232 if (strcmp(cp, "less") == 0) {
1233 argv[argc++] = mandoc_strdup("-T");
1234 argv[argc++] = tag_files->tfn;
1235 if (tag_files->tagname != NULL) {
1236 argv[argc++] = mandoc_strdup("-t");
1237 argv[argc++] = tag_files->tagname;
1244 argv[argc++] = tag_files->ofn;
1247 switch (pager_pid = fork()) {
1249 err((int)MANDOCLEVEL_SYSERR, "fork");
1253 (void)setpgid(pager_pid, 0);
1254 (void)tcsetpgrp(tag_files->ofd, pager_pid);
1256 if (pledge("stdio rpath tmppath tty proc", NULL) == -1)
1257 err((int)MANDOCLEVEL_SYSERR, "pledge");
1259 tag_files->pager_pid = pager_pid;
1263 /* The child process becomes the pager. */
1265 if (dup2(tag_files->ofd, STDOUT_FILENO) == -1)
1266 err((int)MANDOCLEVEL_SYSERR, "pager stdout");
1267 close(tag_files->ofd);
1268 assert(tag_files->tfd == -1);
1270 /* Do not start the pager before controlling the terminal. */
1272 while (tcgetpgrp(STDOUT_FILENO) != getpid())
1273 nanosleep(&timeout, NULL);
1275 execvp(argv[0], argv);
1276 err((int)MANDOCLEVEL_SYSERR, "exec %s", argv[0]);