1 /* $Id: main.c,v 1.306 2018/05/14 14:10:23 schwarze Exp $ */
3 * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010-2012, 2014-2018 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"
55 #include "mansearch.h"
66 OUTT_ASCII = 0, /* -Tascii */
67 OUTT_LOCALE, /* -Tlocale */
68 OUTT_UTF8, /* -Tutf8 */
69 OUTT_TREE, /* -Ttree */
71 OUTT_HTML, /* -Thtml */
72 OUTT_MARKDOWN, /* -Tmarkdown */
73 OUTT_LINT, /* -Tlint */
80 struct manoutput *outopts; /* output options */
81 void *outdata; /* data for output */
82 char *os_s; /* operating system for display */
83 int wstop; /* stop after a file with a warning */
84 enum mandocerr mmin; /* ignore messages below this */
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(const char *);
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 mmsg(enum mandocerr, enum mandoclevel,
103 const char *, int, int, const char *);
104 static void outdata_alloc(struct curparse *);
105 static void parse(struct curparse *, int, const char *);
106 static void passthrough(const char *, int, int);
107 static pid_t spawn_pager(struct tag_files *);
108 static int toptions(struct curparse *, char *);
109 static void usage(enum argmode) __attribute__((__noreturn__));
110 static int woptions(struct curparse *, char *);
112 static const int sec_prios[] = {1, 4, 5, 8, 6, 3, 7, 2, 9};
113 static char help_arg[] = "help";
114 static char *help_argv[] = {help_arg, NULL};
115 static enum mandoclevel rc;
116 static FILE *mmsg_stream;
120 main(int argc, char *argv[])
123 struct mansearch search;
124 struct curparse curp;
126 struct tag_files *tag_files;
127 struct manpage *res, *resp;
128 const char *progname, *sec, *thisarg;
129 char *conf_file, *defpaths, *auxpaths;
134 enum outmode outmode;
141 pid_t pager_pid, tc_pgid, man_pgid, pid;
144 progname = getprogname();
147 progname = mandoc_strdup("mandoc");
148 else if ((progname = strrchr(argv[0], '/')) == NULL)
152 setprogname(progname);
155 if (strncmp(progname, "mandocdb", 8) == 0 ||
156 strcmp(progname, BINM_MAKEWHATIS) == 0)
157 return mandocdb(argc, argv);
160 if (pledge("stdio rpath tmppath tty proc exec", NULL) == -1)
161 err((int)MANDOCLEVEL_SYSERR, "pledge");
164 #if HAVE_SANDBOX_INIT
165 if (sandbox_init(kSBXProfileNoInternet, SANDBOX_NAMED, NULL) == -1)
166 errx((int)MANDOCLEVEL_SYSERR, "sandbox_init");
169 /* Search options. */
171 memset(&conf, 0, sizeof(conf));
172 conf_file = defpaths = NULL;
175 memset(&search, 0, sizeof(struct mansearch));
176 search.outkey = "Nd";
179 if (strcmp(progname, BINM_MAN) == 0)
180 search.argmode = ARG_NAME;
181 else if (strcmp(progname, BINM_APROPOS) == 0)
182 search.argmode = ARG_EXPR;
183 else if (strcmp(progname, BINM_WHATIS) == 0)
184 search.argmode = ARG_WORD;
185 else if (strncmp(progname, "help", 4) == 0)
186 search.argmode = ARG_NAME;
188 search.argmode = ARG_FILE;
190 /* Parser and formatter options. */
192 memset(&curp, 0, sizeof(struct curparse));
193 curp.outtype = OUTT_LOCALE;
194 curp.mmin = MANDOCERR_MAX;
195 curp.outopts = &conf.output;
196 options = MPARSE_SO | MPARSE_UTF8 | MPARSE_LATIN1;
197 mmsg_stream = stderr;
202 outmode = OUTMODE_DEF;
204 while ((c = getopt(argc, argv,
205 "aC:cfhI:iK:klM:m:O:S:s:T:VW:w")) != -1) {
206 if (c == 'i' && search.argmode == ARG_EXPR) {
212 outmode = OUTMODE_ALL;
221 search.argmode = ARG_WORD;
224 conf.output.synopsisonly = 1;
226 outmode = OUTMODE_ALL;
229 if (strncmp(optarg, "os=", 3)) {
230 warnx("-I %s: Bad argument", optarg);
231 return (int)MANDOCLEVEL_BADARG;
233 if (curp.os_s != NULL) {
234 warnx("-I %s: Duplicate argument", optarg);
235 return (int)MANDOCLEVEL_BADARG;
237 curp.os_s = mandoc_strdup(optarg + 3);
240 if ( ! koptions(&options, optarg))
241 return (int)MANDOCLEVEL_BADARG;
244 search.argmode = ARG_EXPR;
247 search.argmode = ARG_FILE;
248 outmode = OUTMODE_ALL;
252 defpaths = strdup(optarg);
253 if (defpaths == NULL)
266 search.arch = optarg;
272 if ( ! toptions(&curp, optarg))
273 return (int)MANDOCLEVEL_BADARG;
276 if ( ! woptions(&curp, optarg))
277 return (int)MANDOCLEVEL_BADARG;
280 outmode = OUTMODE_FLN;
289 usage(search.argmode);
291 /* Postprocess options. */
293 if (outmode == OUTMODE_DEF) {
294 switch (search.argmode) {
296 outmode = OUTMODE_ALL;
300 outmode = OUTMODE_ONE;
303 outmode = OUTMODE_LST;
309 if (outmode == OUTMODE_LST)
310 search.outkey = oarg;
312 while (oarg != NULL) {
314 if (manconf_output(&conf.output,
315 strsep(&oarg, ","), 0) == 0)
317 warnx("-O %s: Bad argument", thisarg);
318 return (int)MANDOCLEVEL_BADARG;
323 if (outmode == OUTMODE_FLN ||
324 outmode == OUTMODE_LST ||
325 !isatty(STDOUT_FILENO))
329 (conf.output.width == 0 || conf.output.indent == 0) &&
330 ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) != -1 &&
332 if (conf.output.width == 0 && ws.ws_col < 79)
333 conf.output.width = ws.ws_col - 1;
334 if (conf.output.indent == 0 && ws.ws_col < 66)
335 conf.output.indent = 3;
340 if (pledge("stdio rpath", NULL) == -1)
341 err((int)MANDOCLEVEL_SYSERR, "pledge");
344 /* Parse arguments. */
354 * and for a man(1) section argument without -s.
357 if (search.argmode == ARG_NAME) {
358 if (*progname == 'h') {
363 } else if (argc > 1 &&
364 ((uc = (unsigned char *)argv[0]) != NULL) &&
365 ((isdigit(uc[0]) && (uc[1] == '\0' ||
366 (isalpha(uc[1]) && uc[2] == '\0'))) ||
367 (uc[0] == 'n' && uc[1] == '\0'))) {
368 search.sec = (char *)uc;
372 if (search.arch == NULL)
373 search.arch = getenv("MACHINE");
375 if (search.arch == NULL)
376 search.arch = MACHINE;
382 /* man(1), whatis(1), apropos(1) */
384 if (search.argmode != ARG_FILE) {
385 if (search.argmode == ARG_NAME &&
386 outmode == OUTMODE_ONE)
387 search.firstmatch = 1;
391 * Use manpath(1) to populate defpaths if -M is not specified.
392 * Don't treat any failures as fatal.
394 if (defpaths == NULL) {
399 if ((fp = popen("/usr/bin/manpath -q", "r")) != NULL) {
400 if ((linelen = getline(&defpaths,
401 &linecap, fp)) > 0) {
402 /* Strip trailing newline */
403 defpaths[linelen - 1] = '\0';
410 /* Access the mandoc database. */
412 manconf_parse(&conf, conf_file, defpaths, auxpaths);
417 if ( ! mansearch(&search, &conf.manpath,
418 argc, argv, &res, &sz))
419 usage(search.argmode);
421 if (sz == 0 && search.argmode == ARG_NAME)
422 fs_search(&search, &conf.manpath,
423 argc, argv, &res, &sz);
425 if (search.argmode == ARG_NAME) {
426 for (c = 0; c < argc; c++) {
427 if (strchr(argv[c], '/') == NULL)
429 if (access(argv[c], R_OK) == -1) {
433 res = mandoc_reallocarray(res,
434 sz + 1, sizeof(*res));
435 res[sz].file = mandoc_strdup(argv[c]);
436 res[sz].names = NULL;
437 res[sz].output = NULL;
438 res[sz].ipath = SIZE_MAX;
441 res[sz].form = FORM_SRC;
447 if (search.argmode != ARG_NAME)
448 warnx("nothing appropriate");
449 rc = MANDOCLEVEL_BADARG;
454 * For standard man(1) and -a output mode,
455 * prepare for copying filename pointers
456 * into the program parameter array.
459 if (outmode == OUTMODE_ONE) {
462 } else if (outmode == OUTMODE_ALL)
465 /* Iterate all matching manuals. */
468 for (i = 0; i < sz; i++) {
469 if (outmode == OUTMODE_FLN)
471 else if (outmode == OUTMODE_LST)
472 printf("%s - %s\n", res[i].names,
473 res[i].output == NULL ? "" :
475 else if (outmode == OUTMODE_ONE) {
476 /* Search for the best section. */
478 sec += strcspn(sec, "123456789");
481 prio = sec_prios[sec[0] - '1'];
484 if (prio >= best_prio)
492 * For man(1), -a and -i output mode, fall through
493 * to the main mandoc(1) code iterating files
494 * and running the parsers on each of them.
497 if (outmode == OUTMODE_FLN || outmode == OUTMODE_LST)
505 if (pledge("stdio rpath tmppath tty proc exec", NULL) == -1)
506 err((int)MANDOCLEVEL_SYSERR, "pledge");
508 if (pledge("stdio rpath", NULL) == -1)
509 err((int)MANDOCLEVEL_SYSERR, "pledge");
513 if (search.argmode == ARG_FILE)
514 moptions(&options, auxpaths);
517 curp.mp = mparse_alloc(options, curp.mmin, mmsg,
518 curp.os_e, curp.os_s);
521 * Conditionally start up the lookaside buffer before parsing.
523 if (OUTT_MAN == curp.outtype)
524 mparse_keep(curp.mp);
528 tag_files = tag_init();
529 parse(&curp, STDIN_FILENO, "<stdin>");
533 * Remember the original working directory, if possible.
534 * This will be needed if some names on the command line
535 * are page names and some are relative file names.
536 * Do not error out if the current directory is not
537 * readable: Maybe it won't be needed after all.
539 startdir = open(".", O_RDONLY | O_DIRECTORY);
544 * Changing directories is not needed in ARG_FILE mode.
545 * Do it on a best-effort basis. Even in case of
546 * failure, some functionality may still work.
549 if (resp->ipath != SIZE_MAX)
550 (void)chdir(conf.manpath.paths[resp->ipath]);
551 else if (startdir != -1)
552 (void)fchdir(startdir);
555 fd = mparse_open(curp.mp, resp != NULL ? resp->file : *argv);
558 tag_files = tag_init();
563 parse(&curp, fd, *argv);
564 else if (resp->form == FORM_SRC)
565 parse(&curp, fd, resp->file);
567 passthrough(resp->file, fd,
568 conf.output.synopsisonly);
570 if (ferror(stdout)) {
571 if (tag_files != NULL) {
572 warn("%s", tag_files->ofn);
577 rc = MANDOCLEVEL_SYSERR;
581 if (argc > 1 && curp.outtype <= OUTT_UTF8) {
582 if (curp.outdata == NULL)
583 outdata_alloc(&curp);
584 terminal_sepline(curp.outdata);
586 } else if (rc < MANDOCLEVEL_ERROR)
587 rc = MANDOCLEVEL_ERROR;
589 if (MANDOCLEVEL_OK != rc && curp.wstop)
597 mparse_reset(curp.mp);
599 if (startdir != -1) {
600 (void)fchdir(startdir);
604 if (curp.outdata != NULL) {
605 switch (curp.outtype) {
607 html_free(curp.outdata);
612 ascii_free(curp.outdata);
616 pspdf_free(curp.outdata);
623 mparse_free(curp.mp);
627 if (search.argmode != ARG_FILE) {
629 mansearch_free(res, sz);
635 * When using a pager, finish writing both temporary files,
636 * fork it, wait for the user to close it, and clean up.
639 if (tag_files != NULL) {
642 man_pgid = getpgid(0);
643 tag_files->tcpgid = man_pgid == getpid() ?
644 getpgid(getppid()) : man_pgid;
649 /* Stop here until moved to the foreground. */
651 tc_pgid = tcgetpgrp(tag_files->ofd);
652 if (tc_pgid != man_pgid) {
653 if (tc_pgid == pager_pid) {
654 (void)tcsetpgrp(tag_files->ofd,
656 if (signum == SIGTTIN)
659 tag_files->tcpgid = tc_pgid;
664 /* Once in the foreground, activate the pager. */
667 (void)tcsetpgrp(tag_files->ofd, pager_pid);
668 kill(pager_pid, SIGCONT);
670 pager_pid = spawn_pager(tag_files);
672 /* Wait for the pager to stop or exit. */
674 while ((pid = waitpid(pager_pid, &status,
675 WUNTRACED)) == -1 && errno == EINTR)
680 rc = MANDOCLEVEL_SYSERR;
683 if (!WIFSTOPPED(status))
686 signum = WSTOPSIG(status);
695 usage(enum argmode argmode)
700 fputs("usage: mandoc [-ac] [-I os=name] "
701 "[-K encoding] [-mdoc | -man] [-O options]\n"
702 "\t [-T output] [-W level] [file ...]\n", stderr);
705 fputs("usage: man [-acfhklw] [-C file] [-M path] "
706 "[-m path] [-S subsection]\n"
707 "\t [[-s] section] name ...\n", stderr);
710 fputs("usage: whatis [-afk] [-C file] "
711 "[-M path] [-m path] [-O outkey] [-S arch]\n"
712 "\t [-s section] name ...\n", stderr);
715 fputs("usage: apropos [-afk] [-C file] "
716 "[-M path] [-m path] [-O outkey] [-S arch]\n"
717 "\t [-s section] expression ...\n", stderr);
720 exit((int)MANDOCLEVEL_BADARG);
724 fs_lookup(const struct manpaths *paths, size_t ipath,
725 const char *sec, const char *arch, const char *name,
726 struct manpage **res, size_t *ressz)
729 struct manpage *page;
735 mandoc_asprintf(&file, "%s/man%s/%s.%s",
736 paths->paths[ipath], sec, name, sec);
737 if (access(file, R_OK) != -1)
741 mandoc_asprintf(&file, "%s/cat%s/%s.0",
742 paths->paths[ipath], sec, name);
743 if (access(file, R_OK) != -1) {
750 mandoc_asprintf(&file, "%s/man%s/%s/%s.%s",
751 paths->paths[ipath], sec, arch, name, sec);
752 if (access(file, R_OK) != -1)
757 mandoc_asprintf(&file, "%s/man%s/%s.[01-9]*",
758 paths->paths[ipath], sec, name);
759 globres = glob(file, 0, NULL, &globinfo);
760 if (globres != 0 && globres != GLOB_NOMATCH)
761 warn("%s: glob", file);
764 file = mandoc_strdup(*globinfo.gl_pathv);
768 if (res != NULL || ipath + 1 != paths->sz)
771 mandoc_asprintf(&file, "%s.%s", name, sec);
772 globres = access(file, R_OK);
774 return globres != -1;
777 warnx("outdated mandoc.db lacks %s(%s) entry, run %s %s",
778 name, sec, BINM_MAKEWHATIS, paths->paths[ipath]);
783 *res = mandoc_reallocarray(*res, ++*ressz, sizeof(struct manpage));
784 page = *res + (*ressz - 1);
789 page->bits = NAME_FILE & NAME_MASK;
790 page->sec = (*sec >= '1' && *sec <= '9') ? *sec - '1' + 1 : 10;
796 fs_search(const struct mansearch *cfg, const struct manpaths *paths,
797 int argc, char **argv, struct manpage **res, size_t *ressz)
799 const char *const sections[] =
800 {"1", "8", "6", "2", "3", "5", "7", "4", "9", "3p"};
801 const size_t nsec = sizeof(sections)/sizeof(sections[0]);
803 size_t ipath, isec, lastsz;
805 assert(cfg->argmode == ARG_NAME);
811 for (ipath = 0; ipath < paths->sz; ipath++) {
812 if (cfg->sec != NULL) {
813 if (fs_lookup(paths, ipath, cfg->sec,
814 cfg->arch, *argv, res, ressz) &&
817 } else for (isec = 0; isec < nsec; isec++)
818 if (fs_lookup(paths, ipath, sections[isec],
819 cfg->arch, *argv, res, ressz) &&
823 if (res != NULL && *ressz == lastsz &&
824 strchr(*argv, '/') == NULL)
825 warnx("No entry for %s in the manual.", *argv);
834 parse(struct curparse *curp, int fd, const char *file)
836 enum mandoclevel rctmp;
837 struct roff_man *man;
839 /* Begin by parsing the file itself. */
844 rctmp = mparse_readfd(curp->mp, fd, file);
845 if (fd != STDIN_FILENO)
851 * With -Wstop and warnings or errors of at least the requested
852 * level, do not produce output.
855 if (rctmp != MANDOCLEVEL_OK && curp->wstop)
858 if (curp->outdata == NULL)
861 mparse_result(curp->mp, &man, NULL);
863 /* Execute the out device, if it exists. */
868 if (man->macroset == MACROSET_MDOC) {
869 if (curp->outtype != OUTT_TREE || !curp->outopts->noval)
871 switch (curp->outtype) {
873 html_mdoc(curp->outdata, man);
876 tree_mdoc(curp->outdata, man);
879 man_mdoc(curp->outdata, man);
886 terminal_mdoc(curp->outdata, man);
889 markdown_mdoc(curp->outdata, man);
895 if (man->macroset == MACROSET_MAN) {
896 if (curp->outtype != OUTT_TREE || !curp->outopts->noval)
898 switch (curp->outtype) {
900 html_man(curp->outdata, man);
903 tree_man(curp->outdata, man);
906 man_man(curp->outdata, man);
913 terminal_man(curp->outdata, man);
919 if (curp->mmin < MANDOCERR_STYLE)
921 mparse_updaterc(curp->mp, &rc);
925 check_xr(const char *file)
927 static struct manpaths paths;
928 struct mansearch search;
929 struct mandoc_xr *xr;
934 manpath_base(&paths);
936 for (xr = mandoc_xr_get(); xr != NULL; xr = xr->next) {
940 search.sec = xr->sec;
941 search.outkey = NULL;
942 search.argmode = ARG_NAME;
943 search.firstmatch = 1;
944 if (mansearch(&search, &paths, 1, &xr->name, NULL, &sz))
946 if (fs_search(&search, &paths, 1, &xr->name, NULL, &sz))
949 mandoc_asprintf(&cp, "Xr %s %s", xr->name, xr->sec);
951 mandoc_asprintf(&cp, "Xr %s %s (%d times)",
952 xr->name, xr->sec, xr->count);
953 mmsg(MANDOCERR_XR_BAD, MANDOCLEVEL_STYLE,
954 file, xr->line, xr->pos + 1, cp);
960 outdata_alloc(struct curparse *curp)
962 switch (curp->outtype) {
964 curp->outdata = html_alloc(curp->outopts);
967 curp->outdata = utf8_alloc(curp->outopts);
970 curp->outdata = locale_alloc(curp->outopts);
973 curp->outdata = ascii_alloc(curp->outopts);
976 curp->outdata = pdf_alloc(curp->outopts);
979 curp->outdata = ps_alloc(curp->outopts);
987 passthrough(const char *file, int fd, int synopsis_only)
989 const char synb[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
990 const char synr[] = "SYNOPSIS";
996 ssize_t len, written;
1002 if (fflush(stdout) == EOF) {
1007 if ((stream = fdopen(fd, "r")) == NULL) {
1014 while ((len = getline(&line, &linesz, stream)) != -1) {
1016 if (synopsis_only) {
1018 if ( ! isspace((unsigned char)*cp))
1020 while (isspace((unsigned char)*cp)) {
1025 if (strcmp(cp, synb) == 0 ||
1026 strcmp(cp, synr) == 0)
1031 for (; len > 0; len -= written) {
1032 if ((written = write(STDOUT_FILENO, cp, len)) != -1)
1040 if (ferror(stream)) {
1042 syscall = "getline";
1053 warn("%s: SYSERR: %s", file, syscall);
1054 if (rc < MANDOCLEVEL_SYSERR)
1055 rc = MANDOCLEVEL_SYSERR;
1059 koptions(int *options, char *arg)
1062 if ( ! strcmp(arg, "utf-8")) {
1063 *options |= MPARSE_UTF8;
1064 *options &= ~MPARSE_LATIN1;
1065 } else if ( ! strcmp(arg, "iso-8859-1")) {
1066 *options |= MPARSE_LATIN1;
1067 *options &= ~MPARSE_UTF8;
1068 } else if ( ! strcmp(arg, "us-ascii")) {
1069 *options &= ~(MPARSE_UTF8 | MPARSE_LATIN1);
1071 warnx("-K %s: Bad argument", arg);
1078 moptions(int *options, char *arg)
1083 if (strcmp(arg, "doc") == 0)
1084 *options |= MPARSE_MDOC;
1085 else if (strcmp(arg, "an") == 0)
1086 *options |= MPARSE_MAN;
1090 toptions(struct curparse *curp, char *arg)
1093 if (0 == strcmp(arg, "ascii"))
1094 curp->outtype = OUTT_ASCII;
1095 else if (0 == strcmp(arg, "lint")) {
1096 curp->outtype = OUTT_LINT;
1097 curp->mmin = MANDOCERR_BASE;
1098 mmsg_stream = stdout;
1099 } else if (0 == strcmp(arg, "tree"))
1100 curp->outtype = OUTT_TREE;
1101 else if (0 == strcmp(arg, "man"))
1102 curp->outtype = OUTT_MAN;
1103 else if (0 == strcmp(arg, "html"))
1104 curp->outtype = OUTT_HTML;
1105 else if (0 == strcmp(arg, "markdown"))
1106 curp->outtype = OUTT_MARKDOWN;
1107 else if (0 == strcmp(arg, "utf8"))
1108 curp->outtype = OUTT_UTF8;
1109 else if (0 == strcmp(arg, "locale"))
1110 curp->outtype = OUTT_LOCALE;
1111 else if (0 == strcmp(arg, "ps"))
1112 curp->outtype = OUTT_PS;
1113 else if (0 == strcmp(arg, "pdf"))
1114 curp->outtype = OUTT_PDF;
1116 warnx("-T %s: Bad argument", arg);
1124 woptions(struct curparse *curp, char *arg)
1127 const char *toks[11];
1133 toks[4] = "warning";
1137 toks[8] = "openbsd";
1143 switch (getsubopt(&arg, (char * const *)toks, &v)) {
1149 curp->mmin = MANDOCERR_BASE;
1152 curp->mmin = MANDOCERR_STYLE;
1155 curp->mmin = MANDOCERR_WARNING;
1158 curp->mmin = MANDOCERR_ERROR;
1161 curp->mmin = MANDOCERR_UNSUPP;
1164 curp->mmin = MANDOCERR_MAX;
1167 curp->mmin = MANDOCERR_BASE;
1168 curp->os_e = MANDOC_OS_OPENBSD;
1171 curp->mmin = MANDOCERR_BASE;
1172 curp->os_e = MANDOC_OS_NETBSD;
1175 warnx("-W %s: Bad argument", o);
1183 mmsg(enum mandocerr t, enum mandoclevel lvl,
1184 const char *file, int line, int col, const char *msg)
1186 const char *mparse_msg;
1188 fprintf(mmsg_stream, "%s: %s:", getprogname(),
1189 file == NULL ? "<stdin>" : file);
1192 fprintf(mmsg_stream, "%d:%d:", line, col + 1);
1194 fprintf(mmsg_stream, " %s", mparse_strlevel(lvl));
1196 if ((mparse_msg = mparse_strerror(t)) != NULL)
1197 fprintf(mmsg_stream, ": %s", mparse_msg);
1200 fprintf(mmsg_stream, ": %s", msg);
1202 fputc('\n', mmsg_stream);
1206 spawn_pager(struct tag_files *tag_files)
1208 const struct timespec timeout = { 0, 100000000 }; /* 0.1s */
1209 #define MAX_PAGER_ARGS 16
1210 char *argv[MAX_PAGER_ARGS];
1217 pager = getenv("MANPAGER");
1218 if (pager == NULL || *pager == '\0')
1219 pager = getenv("PAGER");
1220 if (pager == NULL || *pager == '\0')
1222 cp = mandoc_strdup(pager);
1225 * Parse the pager command into words.
1226 * Intentionally do not do anything fancy here.
1230 while (argc + 4 < MAX_PAGER_ARGS) {
1232 cp = strchr(cp, ' ');
1242 /* For less(1), use the tag file. */
1244 if ((cmdlen = strlen(argv[0])) >= 4) {
1245 cp = argv[0] + cmdlen - 4;
1246 if (strcmp(cp, "less") == 0) {
1247 argv[argc++] = mandoc_strdup("-T");
1248 argv[argc++] = tag_files->tfn;
1251 argv[argc++] = tag_files->ofn;
1254 switch (pager_pid = fork()) {
1256 err((int)MANDOCLEVEL_SYSERR, "fork");
1260 (void)setpgid(pager_pid, 0);
1261 (void)tcsetpgrp(tag_files->ofd, pager_pid);
1263 if (pledge("stdio rpath tmppath tty proc", NULL) == -1)
1264 err((int)MANDOCLEVEL_SYSERR, "pledge");
1266 tag_files->pager_pid = pager_pid;
1270 /* The child process becomes the pager. */
1272 if (dup2(tag_files->ofd, STDOUT_FILENO) == -1)
1273 err((int)MANDOCLEVEL_SYSERR, "pager stdout");
1274 close(tag_files->ofd);
1275 assert(tag_files->tfd == -1);
1277 /* Do not start the pager before controlling the terminal. */
1279 while (tcgetpgrp(STDOUT_FILENO) != getpid())
1280 nanosleep(&timeout, NULL);
1282 execvp(argv[0], argv);
1283 err((int)MANDOCLEVEL_SYSERR, "exec %s", argv[0]);