]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
1 /* $Id: main.c,v 1.313 2018/12/14 05:18:02 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 */
24 #include <sys/termios.h>
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 if (strncmp(progname
, "mandocdb", 8) == 0 ||
152 strcmp(progname
, BINM_MAKEWHATIS
) == 0)
153 return mandocdb(argc
, argv
);
156 if (pledge("stdio rpath tmppath tty proc exec", NULL
) == -1)
157 err((int)MANDOCLEVEL_SYSERR
, "pledge");
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)) {
224 warnx("-I %s: Bad argument", optarg
);
225 return (int)MANDOCLEVEL_BADARG
;
227 if (curp
.os_s
!= NULL
) {
228 warnx("-I %s: Duplicate argument", optarg
);
229 return (int)MANDOCLEVEL_BADARG
;
231 curp
.os_s
= mandoc_strdup(optarg
+ 3);
234 if ( ! koptions(&options
, optarg
))
235 return (int)MANDOCLEVEL_BADARG
;
238 search
.argmode
= ARG_EXPR
;
241 search
.argmode
= ARG_FILE
;
242 outmode
= OUTMODE_ALL
;
254 search
.arch
= optarg
;
260 if ( ! toptions(&curp
, optarg
))
261 return (int)MANDOCLEVEL_BADARG
;
264 if ( ! woptions(&curp
, optarg
))
265 return (int)MANDOCLEVEL_BADARG
;
268 outmode
= OUTMODE_FLN
;
277 usage(search
.argmode
);
279 /* Postprocess options. */
281 if (outmode
== OUTMODE_DEF
) {
282 switch (search
.argmode
) {
284 outmode
= OUTMODE_ALL
;
288 outmode
= OUTMODE_ONE
;
291 outmode
= OUTMODE_LST
;
297 if (outmode
== OUTMODE_LST
)
298 search
.outkey
= oarg
;
300 while (oarg
!= NULL
) {
302 if (manconf_output(&conf
.output
,
303 strsep(&oarg
, ","), 0) == 0)
305 warnx("-O %s: Bad argument", thisarg
);
306 return (int)MANDOCLEVEL_BADARG
;
311 if (outmode
== OUTMODE_FLN
||
312 outmode
== OUTMODE_LST
||
313 !isatty(STDOUT_FILENO
))
317 (conf
.output
.width
== 0 || conf
.output
.indent
== 0) &&
318 ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &ws
) != -1 &&
320 if (conf
.output
.width
== 0 && ws
.ws_col
< 79)
321 conf
.output
.width
= ws
.ws_col
- 1;
322 if (conf
.output
.indent
== 0 && ws
.ws_col
< 66)
323 conf
.output
.indent
= 3;
328 if (pledge("stdio rpath", NULL
) == -1)
329 err((int)MANDOCLEVEL_SYSERR
, "pledge");
332 /* Parse arguments. */
342 * and for a man(1) section argument without -s.
345 if (search
.argmode
== ARG_NAME
) {
346 if (*progname
== 'h') {
351 } else if (argc
> 1 &&
352 ((uc
= (unsigned char *)argv
[0]) != NULL
) &&
353 ((isdigit(uc
[0]) && (uc
[1] == '\0' ||
354 (isalpha(uc
[1]) && uc
[2] == '\0'))) ||
355 (uc
[0] == 'n' && uc
[1] == '\0'))) {
356 search
.sec
= (char *)uc
;
360 if (search
.arch
== NULL
)
361 search
.arch
= getenv("MACHINE");
363 if (search
.arch
== NULL
)
364 search
.arch
= MACHINE
;
368 /* man(1), whatis(1), apropos(1) */
370 if (search
.argmode
!= ARG_FILE
) {
371 if (search
.argmode
== ARG_NAME
&&
372 outmode
== OUTMODE_ONE
)
373 search
.firstmatch
= 1;
375 /* Access the mandoc database. */
377 manconf_parse(&conf
, conf_file
, defpaths
, auxpaths
);
378 if ( ! mansearch(&search
, &conf
.manpath
,
379 argc
, argv
, &res
, &sz
))
380 usage(search
.argmode
);
382 if (sz
== 0 && search
.argmode
== ARG_NAME
)
383 fs_search(&search
, &conf
.manpath
,
384 argc
, argv
, &res
, &sz
);
386 if (search
.argmode
== ARG_NAME
) {
387 for (c
= 0; c
< argc
; c
++) {
388 if (strchr(argv
[c
], '/') == NULL
)
390 if (access(argv
[c
], R_OK
) == -1) {
394 res
= mandoc_reallocarray(res
,
395 sz
+ 1, sizeof(*res
));
396 res
[sz
].file
= mandoc_strdup(argv
[c
]);
397 res
[sz
].names
= NULL
;
398 res
[sz
].output
= NULL
;
399 res
[sz
].ipath
= SIZE_MAX
;
401 res
[sz
].form
= FORM_SRC
;
407 if (search
.argmode
!= ARG_NAME
)
408 warnx("nothing appropriate");
409 mandoc_msg_setrc(MANDOCLEVEL_BADARG
);
414 * For standard man(1) and -a output mode,
415 * prepare for copying filename pointers
416 * into the program parameter array.
419 if (outmode
== OUTMODE_ONE
) {
422 } else if (outmode
== OUTMODE_ALL
)
425 /* Iterate all matching manuals. */
428 for (i
= 0; i
< sz
; i
++) {
429 if (outmode
== OUTMODE_FLN
)
431 else if (outmode
== OUTMODE_LST
)
432 printf("%s - %s\n", res
[i
].names
,
433 res
[i
].output
== NULL
? "" :
435 else if (outmode
== OUTMODE_ONE
) {
436 /* Search for the best section. */
438 sec
+= strcspn(sec
, "123456789");
441 prio
= sec_prios
[sec
[0] - '1'];
444 if (prio
>= best_prio
)
452 * For man(1), -a and -i output mode, fall through
453 * to the main mandoc(1) code iterating files
454 * and running the parsers on each of them.
457 if (outmode
== OUTMODE_FLN
|| outmode
== OUTMODE_LST
)
465 if (pledge("stdio rpath tmppath tty proc exec", NULL
) == -1)
466 err((int)MANDOCLEVEL_SYSERR
, "pledge");
468 if (pledge("stdio rpath", NULL
) == -1)
469 err((int)MANDOCLEVEL_SYSERR
, "pledge");
473 if (search
.argmode
== ARG_FILE
)
474 moptions(&options
, auxpaths
);
477 curp
.mp
= mparse_alloc(options
, curp
.os_e
, curp
.os_s
);
481 tag_files
= tag_init();
483 mandoc_msg_setinfilename(thisarg
);
484 parse(&curp
, STDIN_FILENO
, thisarg
);
485 mandoc_msg_setinfilename(NULL
);
489 * Remember the original working directory, if possible.
490 * This will be needed if some names on the command line
491 * are page names and some are relative file names.
492 * Do not error out if the current directory is not
493 * readable: Maybe it won't be needed after all.
495 startdir
= open(".", O_RDONLY
| O_DIRECTORY
);
500 * Changing directories is not needed in ARG_FILE mode.
501 * Do it on a best-effort basis. Even in case of
502 * failure, some functionality may still work.
505 if (resp
->ipath
!= SIZE_MAX
)
506 (void)chdir(conf
.manpath
.paths
[resp
->ipath
]);
507 else if (startdir
!= -1)
508 (void)fchdir(startdir
);
509 thisarg
= resp
->file
;
513 mandoc_msg_setinfilename(thisarg
);
514 fd
= mparse_open(curp
.mp
, thisarg
);
518 tag_files
= tag_init();
519 if (conf
.output
.tag
!= NULL
&&
520 tag_files
->tagname
== NULL
)
522 *conf
.output
.tag
!= '\0' ?
523 conf
.output
.tag
: *argv
;
526 if (resp
== NULL
|| resp
->form
== FORM_SRC
)
527 parse(&curp
, fd
, thisarg
);
529 passthrough(resp
->file
, fd
,
530 conf
.output
.synopsisonly
);
532 if (ferror(stdout
)) {
533 if (tag_files
!= NULL
) {
534 warn("%s", tag_files
->ofn
);
539 mandoc_msg_setrc(MANDOCLEVEL_SYSERR
);
543 if (argc
> 1 && curp
.outtype
<= OUTT_UTF8
) {
544 if (curp
.outdata
== NULL
)
545 outdata_alloc(&curp
);
546 terminal_sepline(curp
.outdata
);
549 mandoc_msg_setinfilename(NULL
);
551 if (curp
.wstop
&& mandoc_msg_getrc() != MANDOCLEVEL_OK
)
559 mparse_reset(curp
.mp
);
561 if (startdir
!= -1) {
562 (void)fchdir(startdir
);
566 if (curp
.outdata
!= NULL
) {
567 switch (curp
.outtype
) {
569 html_free(curp
.outdata
);
574 ascii_free(curp
.outdata
);
578 pspdf_free(curp
.outdata
);
585 mparse_free(curp
.mp
);
589 if (search
.argmode
!= ARG_FILE
) {
591 mansearch_free(res
, sz
);
597 * When using a pager, finish writing both temporary files,
598 * fork it, wait for the user to close it, and clean up.
601 if (tag_files
!= NULL
) {
604 man_pgid
= getpgid(0);
605 tag_files
->tcpgid
= man_pgid
== getpid() ?
606 getpgid(getppid()) : man_pgid
;
611 /* Stop here until moved to the foreground. */
613 tc_pgid
= tcgetpgrp(tag_files
->ofd
);
614 if (tc_pgid
!= man_pgid
) {
615 if (tc_pgid
== pager_pid
) {
616 (void)tcsetpgrp(tag_files
->ofd
,
618 if (signum
== SIGTTIN
)
621 tag_files
->tcpgid
= tc_pgid
;
626 /* Once in the foreground, activate the pager. */
629 (void)tcsetpgrp(tag_files
->ofd
, pager_pid
);
630 kill(pager_pid
, SIGCONT
);
632 pager_pid
= spawn_pager(tag_files
);
634 /* Wait for the pager to stop or exit. */
636 while ((pid
= waitpid(pager_pid
, &status
,
637 WUNTRACED
)) == -1 && errno
== EINTR
)
642 mandoc_msg_setrc(MANDOCLEVEL_SYSERR
);
645 if (!WIFSTOPPED(status
))
648 signum
= WSTOPSIG(status
);
652 return (int)mandoc_msg_getrc();
656 usage(enum argmode argmode
)
661 fputs("usage: mandoc [-ac] [-I os=name] "
662 "[-K encoding] [-mdoc | -man] [-O options]\n"
663 "\t [-T output] [-W level] [file ...]\n", stderr
);
666 fputs("usage: man [-acfhklw] [-C file] [-M path] "
667 "[-m path] [-S subsection]\n"
668 "\t [[-s] section] name ...\n", stderr
);
671 fputs("usage: whatis [-afk] [-C file] "
672 "[-M path] [-m path] [-O outkey] [-S arch]\n"
673 "\t [-s section] name ...\n", stderr
);
676 fputs("usage: apropos [-afk] [-C file] "
677 "[-M path] [-m path] [-O outkey] [-S arch]\n"
678 "\t [-s section] expression ...\n", stderr
);
681 exit((int)MANDOCLEVEL_BADARG
);
685 fs_lookup(const struct manpaths
*paths
, size_t ipath
,
686 const char *sec
, const char *arch
, const char *name
,
687 struct manpage
**res
, size_t *ressz
)
690 struct manpage
*page
;
696 mandoc_asprintf(&file
, "%s/man%s/%s.%s",
697 paths
->paths
[ipath
], sec
, name
, sec
);
698 if (access(file
, R_OK
) != -1)
702 mandoc_asprintf(&file
, "%s/cat%s/%s.0",
703 paths
->paths
[ipath
], sec
, name
);
704 if (access(file
, R_OK
) != -1) {
711 mandoc_asprintf(&file
, "%s/man%s/%s/%s.%s",
712 paths
->paths
[ipath
], sec
, arch
, name
, sec
);
713 if (access(file
, R_OK
) != -1)
718 mandoc_asprintf(&file
, "%s/man%s/%s.[01-9]*",
719 paths
->paths
[ipath
], sec
, name
);
720 globres
= glob(file
, 0, NULL
, &globinfo
);
721 if (globres
!= 0 && globres
!= GLOB_NOMATCH
)
722 warn("%s: glob", file
);
725 file
= mandoc_strdup(*globinfo
.gl_pathv
);
729 if (res
!= NULL
|| ipath
+ 1 != paths
->sz
)
732 mandoc_asprintf(&file
, "%s.%s", name
, sec
);
733 globres
= access(file
, R_OK
);
735 return globres
!= -1;
738 warnx("outdated mandoc.db lacks %s(%s) entry, run %s %s",
739 name
, sec
, BINM_MAKEWHATIS
, paths
->paths
[ipath
]);
744 *res
= mandoc_reallocarray(*res
, ++*ressz
, sizeof(struct manpage
));
745 page
= *res
+ (*ressz
- 1);
750 page
->sec
= (*sec
>= '1' && *sec
<= '9') ? *sec
- '1' + 1 : 10;
756 fs_search(const struct mansearch
*cfg
, const struct manpaths
*paths
,
757 int argc
, char **argv
, struct manpage
**res
, size_t *ressz
)
759 const char *const sections
[] =
760 {"1", "8", "6", "2", "3", "5", "7", "4", "9", "3p"};
761 const size_t nsec
= sizeof(sections
)/sizeof(sections
[0]);
763 size_t ipath
, isec
, lastsz
;
765 assert(cfg
->argmode
== ARG_NAME
);
771 for (ipath
= 0; ipath
< paths
->sz
; ipath
++) {
772 if (cfg
->sec
!= NULL
) {
773 if (fs_lookup(paths
, ipath
, cfg
->sec
,
774 cfg
->arch
, *argv
, res
, ressz
) &&
777 } else for (isec
= 0; isec
< nsec
; isec
++)
778 if (fs_lookup(paths
, ipath
, sections
[isec
],
779 cfg
->arch
, *argv
, res
, ressz
) &&
783 if (res
!= NULL
&& *ressz
== lastsz
&&
784 strchr(*argv
, '/') == NULL
) {
785 if (cfg
->sec
== NULL
)
786 warnx("No entry for %s in the manual.",
789 warnx("No entry for %s in section %s "
790 "of the manual.", *argv
, cfg
->sec
);
800 parse(struct curparse
*curp
, int fd
, const char *file
)
802 struct roff_man
*man
;
804 /* Begin by parsing the file itself. */
809 mparse_readfd(curp
->mp
, fd
, file
);
810 if (fd
!= STDIN_FILENO
)
814 * With -Wstop and warnings or errors of at least the requested
815 * level, do not produce output.
818 if (curp
->wstop
&& mandoc_msg_getrc() != MANDOCLEVEL_OK
)
821 if (curp
->outdata
== NULL
)
824 mparse_result(curp
->mp
, &man
, NULL
);
826 /* Execute the out device, if it exists. */
831 if (man
->macroset
== MACROSET_MDOC
) {
832 if (curp
->outtype
!= OUTT_TREE
|| !curp
->outopts
->noval
)
834 switch (curp
->outtype
) {
836 html_mdoc(curp
->outdata
, man
);
839 tree_mdoc(curp
->outdata
, man
);
842 man_mdoc(curp
->outdata
, man
);
849 terminal_mdoc(curp
->outdata
, man
);
852 markdown_mdoc(curp
->outdata
, man
);
858 if (man
->macroset
== MACROSET_MAN
) {
859 if (curp
->outtype
!= OUTT_TREE
|| !curp
->outopts
->noval
)
861 switch (curp
->outtype
) {
863 html_man(curp
->outdata
, man
);
866 tree_man(curp
->outdata
, man
);
869 mparse_copy(curp
->mp
);
876 terminal_man(curp
->outdata
, man
);
882 if (mandoc_msg_getmin() < MANDOCERR_STYLE
)
889 static struct manpaths paths
;
890 struct mansearch search
;
891 struct mandoc_xr
*xr
;
895 manpath_base(&paths
);
897 for (xr
= mandoc_xr_get(); xr
!= NULL
; xr
= xr
->next
) {
901 search
.sec
= xr
->sec
;
902 search
.outkey
= NULL
;
903 search
.argmode
= ARG_NAME
;
904 search
.firstmatch
= 1;
905 if (mansearch(&search
, &paths
, 1, &xr
->name
, NULL
, &sz
))
907 if (fs_search(&search
, &paths
, 1, &xr
->name
, NULL
, &sz
))
910 mandoc_msg(MANDOCERR_XR_BAD
, xr
->line
,
911 xr
->pos
+ 1, "Xr %s %s", xr
->name
, xr
->sec
);
913 mandoc_msg(MANDOCERR_XR_BAD
, xr
->line
,
914 xr
->pos
+ 1, "Xr %s %s (%d times)",
915 xr
->name
, xr
->sec
, xr
->count
);
920 outdata_alloc(struct curparse
*curp
)
922 switch (curp
->outtype
) {
924 curp
->outdata
= html_alloc(curp
->outopts
);
927 curp
->outdata
= utf8_alloc(curp
->outopts
);
930 curp
->outdata
= locale_alloc(curp
->outopts
);
933 curp
->outdata
= ascii_alloc(curp
->outopts
);
936 curp
->outdata
= pdf_alloc(curp
->outopts
);
939 curp
->outdata
= ps_alloc(curp
->outopts
);
947 passthrough(const char *file
, int fd
, int synopsis_only
)
949 const char synb
[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
950 const char synr
[] = "SYNOPSIS";
956 ssize_t len
, written
;
962 if (fflush(stdout
) == EOF
) {
967 if ((stream
= fdopen(fd
, "r")) == NULL
) {
974 while ((len
= getline(&line
, &linesz
, stream
)) != -1) {
978 if ( ! isspace((unsigned char)*cp
))
980 while (isspace((unsigned char)*cp
)) {
985 if (strcmp(cp
, synb
) == 0 ||
986 strcmp(cp
, synr
) == 0)
991 for (; len
> 0; len
-= written
) {
992 if ((written
= write(STDOUT_FILENO
, cp
, len
)) != -1)
1000 if (ferror(stream
)) {
1002 syscall
= "getline";
1013 warn("%s: SYSERR: %s", file
, syscall
);
1014 mandoc_msg_setrc(MANDOCLEVEL_SYSERR
);
1018 koptions(int *options
, char *arg
)
1021 if ( ! strcmp(arg
, "utf-8")) {
1022 *options
|= MPARSE_UTF8
;
1023 *options
&= ~MPARSE_LATIN1
;
1024 } else if ( ! strcmp(arg
, "iso-8859-1")) {
1025 *options
|= MPARSE_LATIN1
;
1026 *options
&= ~MPARSE_UTF8
;
1027 } else if ( ! strcmp(arg
, "us-ascii")) {
1028 *options
&= ~(MPARSE_UTF8
| MPARSE_LATIN1
);
1030 warnx("-K %s: Bad argument", arg
);
1037 moptions(int *options
, char *arg
)
1042 if (strcmp(arg
, "doc") == 0)
1043 *options
|= MPARSE_MDOC
;
1044 else if (strcmp(arg
, "an") == 0)
1045 *options
|= MPARSE_MAN
;
1049 toptions(struct curparse
*curp
, char *arg
)
1052 if (0 == strcmp(arg
, "ascii"))
1053 curp
->outtype
= OUTT_ASCII
;
1054 else if (0 == strcmp(arg
, "lint")) {
1055 curp
->outtype
= OUTT_LINT
;
1056 mandoc_msg_setoutfile(stdout
);
1057 mandoc_msg_setmin(MANDOCERR_BASE
);
1058 } else if (0 == strcmp(arg
, "tree"))
1059 curp
->outtype
= OUTT_TREE
;
1060 else if (0 == strcmp(arg
, "man"))
1061 curp
->outtype
= OUTT_MAN
;
1062 else if (0 == strcmp(arg
, "html"))
1063 curp
->outtype
= OUTT_HTML
;
1064 else if (0 == strcmp(arg
, "markdown"))
1065 curp
->outtype
= OUTT_MARKDOWN
;
1066 else if (0 == strcmp(arg
, "utf8"))
1067 curp
->outtype
= OUTT_UTF8
;
1068 else if (0 == strcmp(arg
, "locale"))
1069 curp
->outtype
= OUTT_LOCALE
;
1070 else if (0 == strcmp(arg
, "ps"))
1071 curp
->outtype
= OUTT_PS
;
1072 else if (0 == strcmp(arg
, "pdf"))
1073 curp
->outtype
= OUTT_PDF
;
1075 warnx("-T %s: Bad argument", arg
);
1083 woptions(struct curparse
*curp
, char *arg
)
1086 const char *toks
[11];
1092 toks
[4] = "warning";
1096 toks
[8] = "openbsd";
1102 switch (getsubopt(&arg
, (char * const *)toks
, &v
)) {
1108 mandoc_msg_setmin(MANDOCERR_BASE
);
1111 mandoc_msg_setmin(MANDOCERR_STYLE
);
1114 mandoc_msg_setmin(MANDOCERR_WARNING
);
1117 mandoc_msg_setmin(MANDOCERR_ERROR
);
1120 mandoc_msg_setmin(MANDOCERR_UNSUPP
);
1123 mandoc_msg_setmin(MANDOCERR_MAX
);
1126 mandoc_msg_setmin(MANDOCERR_BASE
);
1127 curp
->os_e
= MANDOC_OS_OPENBSD
;
1130 mandoc_msg_setmin(MANDOCERR_BASE
);
1131 curp
->os_e
= MANDOC_OS_NETBSD
;
1134 warnx("-W %s: Bad argument", o
);
1142 spawn_pager(struct tag_files
*tag_files
)
1144 const struct timespec timeout
= { 0, 100000000 }; /* 0.1s */
1145 #define MAX_PAGER_ARGS 16
1146 char *argv
[MAX_PAGER_ARGS
];
1153 pager
= getenv("MANPAGER");
1154 if (pager
== NULL
|| *pager
== '\0')
1155 pager
= getenv("PAGER");
1156 if (pager
== NULL
|| *pager
== '\0')
1158 cp
= mandoc_strdup(pager
);
1161 * Parse the pager command into words.
1162 * Intentionally do not do anything fancy here.
1166 while (argc
+ 5 < MAX_PAGER_ARGS
) {
1168 cp
= strchr(cp
, ' ');
1178 /* For less(1), use the tag file. */
1181 if ((cmdlen
= strlen(argv
[0])) >= 4) {
1182 cp
= argv
[0] + cmdlen
- 4;
1183 if (strcmp(cp
, "less") == 0) {
1184 argv
[argc
++] = mandoc_strdup("-T");
1185 argv
[argc
++] = tag_files
->tfn
;
1186 if (tag_files
->tagname
!= NULL
) {
1187 argv
[argc
++] = mandoc_strdup("-t");
1188 argv
[argc
++] = tag_files
->tagname
;
1194 argv
[argc
++] = tag_files
->ofn
;
1197 switch (pager_pid
= fork()) {
1199 err((int)MANDOCLEVEL_SYSERR
, "fork");
1203 (void)setpgid(pager_pid
, 0);
1204 (void)tcsetpgrp(tag_files
->ofd
, pager_pid
);
1206 if (pledge("stdio rpath tmppath tty proc", NULL
) == -1)
1207 err((int)MANDOCLEVEL_SYSERR
, "pledge");
1209 tag_files
->pager_pid
= pager_pid
;
1213 /* The child process becomes the pager. */
1215 if (dup2(tag_files
->ofd
, STDOUT_FILENO
) == -1)
1216 err((int)MANDOCLEVEL_SYSERR
, "pager stdout");
1217 close(tag_files
->ofd
);
1218 assert(tag_files
->tfd
== -1);
1220 /* Do not start the pager before controlling the terminal. */
1222 while (tcgetpgrp(STDOUT_FILENO
) != getpid())
1223 nanosleep(&timeout
, NULL
);
1225 execvp(argv
[0], argv
);
1226 err((int)MANDOCLEVEL_SYSERR
, "exec %s", argv
[0]);