1 /* $Id: main.c,v 1.332 2019/07/19 20:27:25 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 */
47 #include "mandoc_aux.h"
49 #include "mandoc_xr.h"
53 #include "mandoc_parse.h"
57 #include "mansearch.h"
68 OUTT_ASCII = 0, /* -Tascii */
69 OUTT_LOCALE, /* -Tlocale */
70 OUTT_UTF8, /* -Tutf8 */
71 OUTT_TREE, /* -Ttree */
73 OUTT_HTML, /* -Thtml */
74 OUTT_MARKDOWN, /* -Tmarkdown */
75 OUTT_LINT, /* -Tlint */
82 struct manoutput *outopts; /* output options */
83 void *outdata; /* data for output */
84 char *os_s; /* operating system for display */
85 int wstop; /* stop after a file with a warning */
86 enum mandoc_os os_e; /* check base system conventions */
87 enum outt outtype; /* which output to use */
91 int mandocdb(int, char *[]);
93 static void check_xr(void);
94 static int fs_lookup(const struct manpaths *,
95 size_t ipath, const char *,
96 const char *, const char *,
97 struct manpage **, size_t *);
98 static int fs_search(const struct mansearch *,
99 const struct manpaths *, int, char**,
100 struct manpage **, size_t *);
101 static void outdata_alloc(struct curparse *);
102 static void parse(struct curparse *, int, const char *);
103 static void passthrough(int, int);
104 static pid_t spawn_pager(struct tag_files *);
105 static void usage(enum argmode) __attribute__((__noreturn__));
106 static int woptions(struct curparse *, char *);
108 static const int sec_prios[] = {1, 4, 5, 8, 6, 3, 7, 2, 9};
109 static char help_arg[] = "help";
110 static char *help_argv[] = {help_arg, NULL};
114 main(int argc, char *argv[])
117 struct mansearch search;
118 struct curparse curp;
120 struct tag_files *tag_files;
121 struct manpage *res, *resp;
122 const char *progname, *sec, *thisarg;
123 char *conf_file, *defpaths, *auxpaths;
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 mandoc_msg_setoutfile(stderr);
150 if (strncmp(progname, "mandocdb", 8) == 0 ||
151 strcmp(progname, BINM_MAKEWHATIS) == 0)
152 return mandocdb(argc, argv);
155 if (pledge("stdio rpath tmppath tty proc exec", NULL) == -1) {
156 mandoc_msg(MANDOCERR_PLEDGE, 0, 0, "%s", strerror(errno));
157 return mandoc_msg_getrc();
160 #if HAVE_SANDBOX_INIT
161 if (sandbox_init(kSBXProfileNoInternet, SANDBOX_NAMED, NULL) == -1)
162 errx((int)MANDOCLEVEL_SYSERR, "sandbox_init");
165 /* Search options. */
167 memset(&conf, 0, sizeof(conf));
168 conf_file = defpaths = NULL;
171 memset(&search, 0, sizeof(struct mansearch));
172 search.outkey = "Nd";
175 if (strcmp(progname, BINM_MAN) == 0)
176 search.argmode = ARG_NAME;
177 else if (strcmp(progname, BINM_APROPOS) == 0)
178 search.argmode = ARG_EXPR;
179 else if (strcmp(progname, BINM_WHATIS) == 0)
180 search.argmode = ARG_WORD;
181 else if (strncmp(progname, "help", 4) == 0)
182 search.argmode = ARG_NAME;
184 search.argmode = ARG_FILE;
186 /* Parser and formatter options. */
188 memset(&curp, 0, sizeof(struct curparse));
189 curp.outtype = OUTT_LOCALE;
190 curp.outopts = &conf.output;
191 options = MPARSE_SO | MPARSE_UTF8 | MPARSE_LATIN1;
196 outmode = OUTMODE_DEF;
198 while ((c = getopt(argc, argv,
199 "aC:cfhI:iK:klM:m:O:S:s:T:VW:w")) != -1) {
200 if (c == 'i' && search.argmode == ARG_EXPR) {
206 outmode = OUTMODE_ALL;
215 search.argmode = ARG_WORD;
218 conf.output.synopsisonly = 1;
220 outmode = OUTMODE_ALL;
223 if (strncmp(optarg, "os=", 3) != 0) {
224 mandoc_msg(MANDOCERR_BADARG_BAD, 0, 0,
226 return mandoc_msg_getrc();
228 if (curp.os_s != NULL) {
229 mandoc_msg(MANDOCERR_BADARG_DUPE, 0, 0,
231 return mandoc_msg_getrc();
233 curp.os_s = mandoc_strdup(optarg + 3);
236 options &= ~(MPARSE_UTF8 | MPARSE_LATIN1);
237 if (strcmp(optarg, "utf-8") == 0)
238 options |= MPARSE_UTF8;
239 else if (strcmp(optarg, "iso-8859-1") == 0)
240 options |= MPARSE_LATIN1;
241 else if (strcmp(optarg, "us-ascii") != 0) {
242 mandoc_msg(MANDOCERR_BADARG_BAD, 0, 0,
244 return mandoc_msg_getrc();
248 search.argmode = ARG_EXPR;
251 search.argmode = ARG_FILE;
252 outmode = OUTMODE_ALL;
256 defpaths = strdup(optarg);
257 if (defpaths == NULL)
270 search.arch = optarg;
276 if (strcmp(optarg, "ascii") == 0)
277 curp.outtype = OUTT_ASCII;
278 else if (strcmp(optarg, "lint") == 0) {
279 curp.outtype = OUTT_LINT;
280 mandoc_msg_setoutfile(stdout);
281 mandoc_msg_setmin(MANDOCERR_BASE);
282 } else if (strcmp(optarg, "tree") == 0)
283 curp.outtype = OUTT_TREE;
284 else if (strcmp(optarg, "man") == 0)
285 curp.outtype = OUTT_MAN;
286 else if (strcmp(optarg, "html") == 0)
287 curp.outtype = OUTT_HTML;
288 else if (strcmp(optarg, "markdown") == 0)
289 curp.outtype = OUTT_MARKDOWN;
290 else if (strcmp(optarg, "utf8") == 0)
291 curp.outtype = OUTT_UTF8;
292 else if (strcmp(optarg, "locale") == 0)
293 curp.outtype = OUTT_LOCALE;
294 else if (strcmp(optarg, "ps") == 0)
295 curp.outtype = OUTT_PS;
296 else if (strcmp(optarg, "pdf") == 0)
297 curp.outtype = OUTT_PDF;
299 mandoc_msg(MANDOCERR_BADARG_BAD, 0, 0,
301 return mandoc_msg_getrc();
305 if (woptions(&curp, optarg) == -1)
306 return mandoc_msg_getrc();
309 outmode = OUTMODE_FLN;
318 usage(search.argmode);
320 /* Postprocess options. */
322 if (outmode == OUTMODE_DEF) {
323 switch (search.argmode) {
325 outmode = OUTMODE_ALL;
329 outmode = OUTMODE_ONE;
332 outmode = OUTMODE_LST;
338 if (outmode == OUTMODE_LST)
339 search.outkey = oarg;
341 while (oarg != NULL) {
342 if (manconf_output(&conf.output,
343 strsep(&oarg, ","), 0) == -1)
344 return mandoc_msg_getrc();
349 if (curp.outtype != OUTT_TREE || !curp.outopts->noval)
350 options |= MPARSE_VALIDATE;
352 if (outmode == OUTMODE_FLN ||
353 outmode == OUTMODE_LST ||
354 !isatty(STDOUT_FILENO))
358 (conf.output.width == 0 || conf.output.indent == 0) &&
359 ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) != -1 &&
361 if (conf.output.width == 0 && ws.ws_col < 79)
362 conf.output.width = ws.ws_col - 1;
363 if (conf.output.indent == 0 && ws.ws_col < 66)
364 conf.output.indent = 3;
368 if (use_pager == 0) {
369 if (pledge("stdio rpath", NULL) == -1) {
370 mandoc_msg(MANDOCERR_PLEDGE, 0, 0,
371 "%s", strerror(errno));
372 return mandoc_msg_getrc();
377 /* Parse arguments. */
387 * and for a man(1) section argument without -s.
390 if (search.argmode == ARG_NAME) {
391 if (*progname == 'h') {
396 } else if (argc > 1 &&
397 ((uc = (unsigned char *)argv[0]) != NULL) &&
398 ((isdigit(uc[0]) && (uc[1] == '\0' ||
400 (uc[0] == 'n' && uc[1] == '\0'))) {
401 search.sec = (char *)uc;
405 if (search.arch == NULL)
406 search.arch = getenv("MACHINE");
408 if (search.arch == NULL)
409 search.arch = MACHINE;
414 * Use the first argument for -O tag in addition to
415 * using it as a search term for man(1) or apropos(1).
418 if (conf.output.tag != NULL && *conf.output.tag == '\0') {
419 tagarg = argc > 0 && search.argmode == ARG_EXPR ?
420 strchr(*argv, '=') : NULL;
421 conf.output.tag = tagarg == NULL ? *argv : tagarg + 1;
424 /* man(1), whatis(1), apropos(1) */
426 if (search.argmode != ARG_FILE) {
427 if (search.argmode == ARG_NAME &&
428 outmode == OUTMODE_ONE)
429 search.firstmatch = 1;
433 * Use manpath(1) to populate defpaths if -M is not specified.
434 * Don't treat any failures as fatal.
436 if (defpaths == NULL) {
441 if ((fp = popen("/usr/bin/manpath -q", "r")) != NULL) {
442 if ((linelen = getline(&defpaths,
443 &linecap, fp)) > 0) {
444 /* Strip trailing newline */
445 defpaths[linelen - 1] = '\0';
452 /* Access the mandoc database. */
454 manconf_parse(&conf, conf_file, defpaths, auxpaths);
459 if ( ! mansearch(&search, &conf.manpath,
460 argc, argv, &res, &sz))
461 usage(search.argmode);
463 if (sz == 0 && search.argmode == ARG_NAME)
464 (void)fs_search(&search, &conf.manpath,
465 argc, argv, &res, &sz);
467 if (search.argmode == ARG_NAME) {
468 for (c = 0; c < argc; c++) {
469 if (strchr(argv[c], '/') == NULL)
471 if (access(argv[c], R_OK) == -1) {
472 mandoc_msg_setinfilename(argv[c]);
473 mandoc_msg(MANDOCERR_BADARG_BAD,
474 0, 0, "%s", strerror(errno));
475 mandoc_msg_setinfilename(NULL);
478 res = mandoc_reallocarray(res,
479 sz + 1, sizeof(*res));
480 res[sz].file = mandoc_strdup(argv[c]);
481 res[sz].names = NULL;
482 res[sz].output = NULL;
484 res[sz].ipath = SIZE_MAX;
486 res[sz].form = FORM_SRC;
492 if (search.argmode != ARG_NAME)
493 warnx("nothing appropriate");
494 mandoc_msg_setrc(MANDOCLEVEL_BADARG);
499 * For standard man(1) and -a output mode,
500 * prepare for copying filename pointers
501 * into the program parameter array.
504 if (outmode == OUTMODE_ONE) {
507 } else if (outmode == OUTMODE_ALL)
510 /* Iterate all matching manuals. */
513 for (i = 0; i < sz; i++) {
514 if (outmode == OUTMODE_FLN)
516 else if (outmode == OUTMODE_LST)
517 printf("%s - %s\n", res[i].names,
518 res[i].output == NULL ? "" :
520 else if (outmode == OUTMODE_ONE) {
521 /* Search for the best section. */
523 sec += strcspn(sec, "123456789");
525 continue; /* No section at all. */
526 prio = sec_prios[sec[0] - '1'];
527 if (search.sec != NULL) {
528 ssz = strlen(search.sec);
529 if (strncmp(sec, search.sec, ssz) == 0)
532 sec++; /* Prefer without suffix. */
534 prio += 10; /* Wrong dir name. */
535 if (search.sec != NULL &&
536 (strlen(sec) <= ssz + 3 ||
537 strcmp(sec + strlen(sec) - ssz,
539 prio += 20; /* Wrong file ext. */
540 if (prio >= best_prio)
548 * For man(1), -a and -i output mode, fall through
549 * to the main mandoc(1) code iterating files
550 * and running the parsers on each of them.
553 if (outmode == OUTMODE_FLN || outmode == OUTMODE_LST)
561 if (pledge("stdio rpath tmppath tty proc exec", NULL) == -1) {
562 mandoc_msg(MANDOCERR_PLEDGE, 0, 0,
563 "%s", strerror(errno));
564 return mandoc_msg_getrc();
567 if (pledge("stdio rpath", NULL) == -1) {
568 mandoc_msg(MANDOCERR_PLEDGE, 0, 0,
569 "%s", strerror(errno));
570 return mandoc_msg_getrc();
575 if (search.argmode == ARG_FILE && auxpaths != NULL) {
576 if (strcmp(auxpaths, "doc") == 0)
577 options |= MPARSE_MDOC;
578 else if (strcmp(auxpaths, "an") == 0)
579 options |= MPARSE_MAN;
583 curp.mp = mparse_alloc(options, curp.os_e, curp.os_s);
587 tag_files = tag_init();
588 if (tag_files != NULL)
589 tag_files->tagname = conf.output.tag;
592 mandoc_msg_setinfilename(thisarg);
593 parse(&curp, STDIN_FILENO, thisarg);
594 mandoc_msg_setinfilename(NULL);
598 * Remember the original working directory, if possible.
599 * This will be needed if some names on the command line
600 * are page names and some are relative file names.
601 * Do not error out if the current directory is not
602 * readable: Maybe it won't be needed after all.
604 startdir = open(".", O_RDONLY | O_DIRECTORY);
609 * Changing directories is not needed in ARG_FILE mode.
610 * Do it on a best-effort basis. Even in case of
611 * failure, some functionality may still work.
614 if (resp->ipath != SIZE_MAX)
615 (void)chdir(conf.manpath.paths[resp->ipath]);
616 else if (startdir != -1)
617 (void)fchdir(startdir);
618 thisarg = resp->file;
622 mandoc_msg_setinfilename(thisarg);
623 fd = mparse_open(curp.mp, thisarg);
627 tag_files = tag_init();
628 if (tag_files != NULL)
629 tag_files->tagname = conf.output.tag;
632 if (resp == NULL || resp->form == FORM_SRC)
633 parse(&curp, fd, thisarg);
635 passthrough(fd, conf.output.synopsisonly);
637 if (ferror(stdout)) {
638 if (tag_files != NULL) {
639 mandoc_msg(MANDOCERR_WRITE, 0, 0,
640 "%s: %s", tag_files->ofn,
645 mandoc_msg(MANDOCERR_WRITE, 0, 0,
646 "%s", strerror(errno));
650 if (argc > 1 && curp.outtype <= OUTT_UTF8) {
651 if (curp.outdata == NULL)
652 outdata_alloc(&curp);
653 terminal_sepline(curp.outdata);
656 mandoc_msg(resp == NULL ? MANDOCERR_BADARG_BAD :
657 MANDOCERR_OPEN, 0, 0, "%s", strerror(errno));
659 mandoc_msg_setinfilename(NULL);
661 if (curp.wstop && mandoc_msg_getrc() != MANDOCLEVEL_OK)
669 mparse_reset(curp.mp);
671 if (startdir != -1) {
672 (void)fchdir(startdir);
676 if (curp.outdata != NULL) {
677 switch (curp.outtype) {
679 html_free(curp.outdata);
684 ascii_free(curp.outdata);
688 pspdf_free(curp.outdata);
695 mparse_free(curp.mp);
699 if (search.argmode != ARG_FILE) {
701 mansearch_free(res, sz);
707 * When using a pager, finish writing both temporary files,
708 * fork it, wait for the user to close it, and clean up.
711 if (tag_files != NULL) {
714 man_pgid = getpgid(0);
715 tag_files->tcpgid = man_pgid == getpid() ?
716 getpgid(getppid()) : man_pgid;
721 /* Stop here until moved to the foreground. */
723 tc_pgid = tcgetpgrp(tag_files->ofd);
724 if (tc_pgid != man_pgid) {
725 if (tc_pgid == pager_pid) {
726 (void)tcsetpgrp(tag_files->ofd,
728 if (signum == SIGTTIN)
731 tag_files->tcpgid = tc_pgid;
736 /* Once in the foreground, activate the pager. */
739 (void)tcsetpgrp(tag_files->ofd, pager_pid);
740 kill(pager_pid, SIGCONT);
742 pager_pid = spawn_pager(tag_files);
744 /* Wait for the pager to stop or exit. */
746 while ((pid = waitpid(pager_pid, &status,
747 WUNTRACED)) == -1 && errno == EINTR)
751 mandoc_msg(MANDOCERR_WAIT, 0, 0,
752 "%s", strerror(errno));
755 if (!WIFSTOPPED(status))
758 signum = WSTOPSIG(status);
761 } else if (curp.outtype != OUTT_LINT &&
762 (search.argmode == ARG_FILE || sz > 0))
763 mandoc_msg_summary();
765 return (int)mandoc_msg_getrc();
769 usage(enum argmode argmode)
773 fputs("usage: mandoc [-ac] [-I os=name] "
774 "[-K encoding] [-mdoc | -man] [-O options]\n"
775 "\t [-T output] [-W level] [file ...]\n", stderr);
778 fputs("usage: man [-acfhklw] [-C file] [-M path] "
779 "[-m path] [-S subsection]\n"
780 "\t [[-s] section] name ...\n", stderr);
783 fputs("usage: whatis [-afk] [-C file] "
784 "[-M path] [-m path] [-O outkey] [-S arch]\n"
785 "\t [-s section] name ...\n", stderr);
788 fputs("usage: apropos [-afk] [-C file] "
789 "[-M path] [-m path] [-O outkey] [-S arch]\n"
790 "\t [-s section] expression ...\n", stderr);
793 exit((int)MANDOCLEVEL_BADARG);
797 fs_lookup(const struct manpaths *paths, size_t ipath,
798 const char *sec, const char *arch, const char *name,
799 struct manpage **res, size_t *ressz)
803 struct manpage *page;
809 mandoc_asprintf(&file, "%s/man%s/%s.%s",
810 paths->paths[ipath], sec, name, sec);
811 if (stat(file, &sb) != -1)
815 mandoc_asprintf(&file, "%s/cat%s/%s.0",
816 paths->paths[ipath], sec, name);
817 if (stat(file, &sb) != -1) {
824 mandoc_asprintf(&file, "%s/man%s/%s/%s.%s",
825 paths->paths[ipath], sec, arch, name, sec);
826 if (stat(file, &sb) != -1)
831 mandoc_asprintf(&file, "%s/man%s/%s.[01-9]*",
832 paths->paths[ipath], sec, name);
833 globres = glob(file, 0, NULL, &globinfo);
834 if (globres != 0 && globres != GLOB_NOMATCH)
835 mandoc_msg(MANDOCERR_GLOB, 0, 0,
836 "%s: %s", file, strerror(errno));
839 file = mandoc_strdup(*globinfo.gl_pathv);
842 if (stat(file, &sb) != -1)
846 if (res != NULL || ipath + 1 != paths->sz)
849 mandoc_asprintf(&file, "%s.%s", name, sec);
850 globres = stat(file, &sb);
855 warnx("outdated mandoc.db lacks %s(%s) entry, run %s %s",
856 name, sec, BINM_MAKEWHATIS, paths->paths[ipath]);
861 *res = mandoc_reallocarray(*res, ++*ressz, sizeof(**res));
862 page = *res + (*ressz - 1);
866 page->bits = NAME_FILE & NAME_MASK;
868 page->sec = (*sec >= '1' && *sec <= '9') ? *sec - '1' + 1 : 10;
874 fs_search(const struct mansearch *cfg, const struct manpaths *paths,
875 int argc, char **argv, struct manpage **res, size_t *ressz)
877 const char *const sections[] =
878 {"1", "8", "6", "2", "3", "5", "7", "4", "9", "3p"};
879 const size_t nsec = sizeof(sections)/sizeof(sections[0]);
881 size_t ipath, isec, lastsz;
883 assert(cfg->argmode == ARG_NAME);
889 for (ipath = 0; ipath < paths->sz; ipath++) {
890 if (cfg->sec != NULL) {
891 if (fs_lookup(paths, ipath, cfg->sec,
892 cfg->arch, *argv, res, ressz) != -1 &&
895 } else for (isec = 0; isec < nsec; isec++)
896 if (fs_lookup(paths, ipath, sections[isec],
897 cfg->arch, *argv, res, ressz) != -1 &&
901 if (res != NULL && *ressz == lastsz &&
902 strchr(*argv, '/') == NULL) {
903 if (cfg->arch != NULL &&
904 arch_valid(cfg->arch, OSENUM) == 0)
905 warnx("Unknown architecture \"%s\".",
907 else if (cfg->sec == NULL)
908 warnx("No entry for %s in the manual.",
911 warnx("No entry for %s in section %s "
912 "of the manual.", *argv, cfg->sec);
922 parse(struct curparse *curp, int fd, const char *file)
924 struct roff_meta *meta;
926 /* Begin by parsing the file itself. */
931 mparse_readfd(curp->mp, fd, file);
932 if (fd != STDIN_FILENO)
936 * With -Wstop and warnings or errors of at least the requested
937 * level, do not produce output.
940 if (curp->wstop && mandoc_msg_getrc() != MANDOCLEVEL_OK)
943 if (curp->outdata == NULL)
945 else if (curp->outtype == OUTT_HTML)
949 meta = mparse_result(curp->mp);
951 /* Execute the out device, if it exists. */
953 if (meta->macroset == MACROSET_MDOC) {
954 switch (curp->outtype) {
956 html_mdoc(curp->outdata, meta);
959 tree_mdoc(curp->outdata, meta);
962 man_mdoc(curp->outdata, meta);
969 terminal_mdoc(curp->outdata, meta);
972 markdown_mdoc(curp->outdata, meta);
978 if (meta->macroset == MACROSET_MAN) {
979 switch (curp->outtype) {
981 html_man(curp->outdata, meta);
984 tree_man(curp->outdata, meta);
987 mparse_copy(curp->mp);
994 terminal_man(curp->outdata, meta);
1000 if (mandoc_msg_getmin() < MANDOCERR_STYLE)
1007 static struct manpaths paths;
1008 struct mansearch search;
1009 struct mandoc_xr *xr;
1013 manpath_base(&paths);
1015 for (xr = mandoc_xr_get(); xr != NULL; xr = xr->next) {
1019 search.sec = xr->sec;
1020 search.outkey = NULL;
1021 search.argmode = ARG_NAME;
1022 search.firstmatch = 1;
1023 if (mansearch(&search, &paths, 1, &xr->name, NULL, &sz))
1025 if (fs_search(&search, &paths, 1, &xr->name, NULL, &sz) != -1)
1028 mandoc_msg(MANDOCERR_XR_BAD, xr->line,
1029 xr->pos + 1, "Xr %s %s", xr->name, xr->sec);
1031 mandoc_msg(MANDOCERR_XR_BAD, xr->line,
1032 xr->pos + 1, "Xr %s %s (%d times)",
1033 xr->name, xr->sec, xr->count);
1038 outdata_alloc(struct curparse *curp)
1040 switch (curp->outtype) {
1042 curp->outdata = html_alloc(curp->outopts);
1045 curp->outdata = utf8_alloc(curp->outopts);
1048 curp->outdata = locale_alloc(curp->outopts);
1051 curp->outdata = ascii_alloc(curp->outopts);
1054 curp->outdata = pdf_alloc(curp->outopts);
1057 curp->outdata = ps_alloc(curp->outopts);
1065 passthrough(int fd, int synopsis_only)
1067 const char synb[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
1068 const char synr[] = "SYNOPSIS";
1073 ssize_t len, written;
1080 if (fflush(stdout) == EOF) {
1081 mandoc_msg(MANDOCERR_FFLUSH, 0, 0, "%s", strerror(errno));
1084 if ((stream = fdopen(fd, "r")) == NULL) {
1086 mandoc_msg(MANDOCERR_FDOPEN, 0, 0, "%s", strerror(errno));
1091 while ((len = getline(&line, &linesz, stream)) != -1) {
1094 if (synopsis_only) {
1096 if ( ! isspace((unsigned char)*cp))
1098 while (isspace((unsigned char)*cp)) {
1103 if (strcmp(cp, synb) == 0 ||
1104 strcmp(cp, synr) == 0)
1109 for (; len > 0; len -= written) {
1110 if ((written = write(STDOUT_FILENO, cp, len)) == -1) {
1111 mandoc_msg(MANDOCERR_WRITE, 0, 0,
1112 "%s", strerror(errno));
1118 mandoc_msg(MANDOCERR_GETLINE, lno, 0, "%s", strerror(errno));
1127 woptions(struct curparse *curp, char *arg)
1130 const char *toks[11];
1136 toks[4] = "warning";
1140 toks[8] = "openbsd";
1146 switch (getsubopt(&arg, (char * const *)toks, &v)) {
1152 mandoc_msg_setmin(MANDOCERR_BASE);
1155 mandoc_msg_setmin(MANDOCERR_STYLE);
1158 mandoc_msg_setmin(MANDOCERR_WARNING);
1161 mandoc_msg_setmin(MANDOCERR_ERROR);
1164 mandoc_msg_setmin(MANDOCERR_UNSUPP);
1167 mandoc_msg_setmin(MANDOCERR_BADARG);
1170 mandoc_msg_setmin(MANDOCERR_BASE);
1171 curp->os_e = MANDOC_OS_OPENBSD;
1174 mandoc_msg_setmin(MANDOCERR_BASE);
1175 curp->os_e = MANDOC_OS_NETBSD;
1178 mandoc_msg(MANDOCERR_BADARG_BAD, 0, 0, "-W %s", o);
1186 spawn_pager(struct tag_files *tag_files)
1188 const struct timespec timeout = { 0, 100000000 }; /* 0.1s */
1189 #define MAX_PAGER_ARGS 16
1190 char *argv[MAX_PAGER_ARGS];
1199 pager = getenv("MANPAGER");
1200 if (pager == NULL || *pager == '\0')
1201 pager = getenv("PAGER");
1202 if (pager == NULL || *pager == '\0')
1204 cp = mandoc_strdup(pager);
1207 * Parse the pager command into words.
1208 * Intentionally do not do anything fancy here.
1212 while (argc + 5 < MAX_PAGER_ARGS) {
1214 cp = strchr(cp, ' ');
1224 /* For less(1), use the tag file. */
1228 if (*tag_files->tfn != '\0' && (cmdlen = strlen(argv[0])) >= 4) {
1229 cp = argv[0] + cmdlen - 4;
1230 if (strcmp(cp, "less") == 0) {
1231 argv[argc++] = mandoc_strdup("-T");
1232 argv[argc++] = tag_files->tfn;
1233 if (tag_files->tagname != NULL) {
1234 argv[argc++] = mandoc_strdup("-t");
1235 argv[argc++] = tag_files->tagname;
1242 argv[argc++] = tag_files->ofn;
1245 switch (pager_pid = fork()) {
1247 mandoc_msg(MANDOCERR_FORK, 0, 0, "%s", strerror(errno));
1248 exit(mandoc_msg_getrc());
1252 (void)setpgid(pager_pid, 0);
1253 (void)tcsetpgrp(tag_files->ofd, pager_pid);
1255 if (pledge("stdio rpath tmppath tty proc", NULL) == -1) {
1256 mandoc_msg(MANDOCERR_PLEDGE, 0, 0,
1257 "%s", strerror(errno));
1258 exit(mandoc_msg_getrc());
1261 tag_files->pager_pid = pager_pid;
1265 /* The child process becomes the pager. */
1267 if (dup2(tag_files->ofd, STDOUT_FILENO) == -1) {
1268 mandoc_msg(MANDOCERR_DUP, 0, 0, "%s", strerror(errno));
1269 _exit(mandoc_msg_getrc());
1271 close(tag_files->ofd);
1272 assert(tag_files->tfd == -1);
1274 /* Do not start the pager before controlling the terminal. */
1276 while (tcgetpgrp(STDOUT_FILENO) != getpid())
1277 nanosleep(&timeout, NULL);
1279 execvp(argv[0], argv);
1280 mandoc_msg(MANDOCERR_EXEC, 0, 0, "%s: %s", argv[0], strerror(errno));
1281 _exit(mandoc_msg_getrc());