]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
1 /* $Id: main.c,v 1.316 2019/01/01 08:18:11 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 (curp
.outtype
!= OUTT_TREE
|| !curp
.outopts
->noval
)
312 options
|= MPARSE_VALIDATE
;
314 if (outmode
== OUTMODE_FLN
||
315 outmode
== OUTMODE_LST
||
316 !isatty(STDOUT_FILENO
))
320 (conf
.output
.width
== 0 || conf
.output
.indent
== 0) &&
321 ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &ws
) != -1 &&
323 if (conf
.output
.width
== 0 && ws
.ws_col
< 79)
324 conf
.output
.width
= ws
.ws_col
- 1;
325 if (conf
.output
.indent
== 0 && ws
.ws_col
< 66)
326 conf
.output
.indent
= 3;
331 if (pledge("stdio rpath", NULL
) == -1)
332 err((int)MANDOCLEVEL_SYSERR
, "pledge");
335 /* Parse arguments. */
345 * and for a man(1) section argument without -s.
348 if (search
.argmode
== ARG_NAME
) {
349 if (*progname
== 'h') {
354 } else if (argc
> 1 &&
355 ((uc
= (unsigned char *)argv
[0]) != NULL
) &&
356 ((isdigit(uc
[0]) && (uc
[1] == '\0' ||
357 (isalpha(uc
[1]) && uc
[2] == '\0'))) ||
358 (uc
[0] == 'n' && uc
[1] == '\0'))) {
359 search
.sec
= (char *)uc
;
363 if (search
.arch
== NULL
)
364 search
.arch
= getenv("MACHINE");
366 if (search
.arch
== NULL
)
367 search
.arch
= MACHINE
;
372 * Use the first argument for -O tag in addition to
373 * using it as a search term for man(1) or apropos(1).
376 if (conf
.output
.tag
!= NULL
&& *conf
.output
.tag
== '\0') {
377 tagarg
= argc
> 0 && search
.argmode
== ARG_EXPR
?
378 strchr(*argv
, '=') : NULL
;
379 conf
.output
.tag
= tagarg
== NULL
? *argv
: tagarg
+ 1;
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;
389 /* Access the mandoc database. */
391 manconf_parse(&conf
, conf_file
, defpaths
, auxpaths
);
392 if ( ! mansearch(&search
, &conf
.manpath
,
393 argc
, argv
, &res
, &sz
))
394 usage(search
.argmode
);
396 if (sz
== 0 && search
.argmode
== ARG_NAME
)
397 fs_search(&search
, &conf
.manpath
,
398 argc
, argv
, &res
, &sz
);
400 if (search
.argmode
== ARG_NAME
) {
401 for (c
= 0; c
< argc
; c
++) {
402 if (strchr(argv
[c
], '/') == NULL
)
404 if (access(argv
[c
], R_OK
) == -1) {
408 res
= mandoc_reallocarray(res
,
409 sz
+ 1, sizeof(*res
));
410 res
[sz
].file
= mandoc_strdup(argv
[c
]);
411 res
[sz
].names
= NULL
;
412 res
[sz
].output
= NULL
;
413 res
[sz
].ipath
= SIZE_MAX
;
415 res
[sz
].form
= FORM_SRC
;
421 if (search
.argmode
!= ARG_NAME
)
422 warnx("nothing appropriate");
423 mandoc_msg_setrc(MANDOCLEVEL_BADARG
);
428 * For standard man(1) and -a output mode,
429 * prepare for copying filename pointers
430 * into the program parameter array.
433 if (outmode
== OUTMODE_ONE
) {
436 } else if (outmode
== OUTMODE_ALL
)
439 /* Iterate all matching manuals. */
442 for (i
= 0; i
< sz
; i
++) {
443 if (outmode
== OUTMODE_FLN
)
445 else if (outmode
== OUTMODE_LST
)
446 printf("%s - %s\n", res
[i
].names
,
447 res
[i
].output
== NULL
? "" :
449 else if (outmode
== OUTMODE_ONE
) {
450 /* Search for the best section. */
452 sec
+= strcspn(sec
, "123456789");
455 prio
= sec_prios
[sec
[0] - '1'];
458 if (prio
>= best_prio
)
466 * For man(1), -a and -i output mode, fall through
467 * to the main mandoc(1) code iterating files
468 * and running the parsers on each of them.
471 if (outmode
== OUTMODE_FLN
|| outmode
== OUTMODE_LST
)
479 if (pledge("stdio rpath tmppath tty proc exec", NULL
) == -1)
480 err((int)MANDOCLEVEL_SYSERR
, "pledge");
482 if (pledge("stdio rpath", NULL
) == -1)
483 err((int)MANDOCLEVEL_SYSERR
, "pledge");
487 if (search
.argmode
== ARG_FILE
)
488 moptions(&options
, auxpaths
);
491 curp
.mp
= mparse_alloc(options
, curp
.os_e
, curp
.os_s
);
495 tag_files
= tag_init();
496 tag_files
->tagname
= conf
.output
.tag
;
499 mandoc_msg_setinfilename(thisarg
);
500 parse(&curp
, STDIN_FILENO
, thisarg
);
501 mandoc_msg_setinfilename(NULL
);
505 * Remember the original working directory, if possible.
506 * This will be needed if some names on the command line
507 * are page names and some are relative file names.
508 * Do not error out if the current directory is not
509 * readable: Maybe it won't be needed after all.
511 startdir
= open(".", O_RDONLY
| O_DIRECTORY
);
516 * Changing directories is not needed in ARG_FILE mode.
517 * Do it on a best-effort basis. Even in case of
518 * failure, some functionality may still work.
521 if (resp
->ipath
!= SIZE_MAX
)
522 (void)chdir(conf
.manpath
.paths
[resp
->ipath
]);
523 else if (startdir
!= -1)
524 (void)fchdir(startdir
);
525 thisarg
= resp
->file
;
529 fd
= mparse_open(curp
.mp
, thisarg
);
533 tag_files
= tag_init();
534 tag_files
->tagname
= conf
.output
.tag
;
537 mandoc_msg_setinfilename(thisarg
);
538 if (resp
== NULL
|| resp
->form
== FORM_SRC
)
539 parse(&curp
, fd
, thisarg
);
541 passthrough(resp
->file
, fd
,
542 conf
.output
.synopsisonly
);
543 mandoc_msg_setinfilename(NULL
);
545 if (ferror(stdout
)) {
546 if (tag_files
!= NULL
) {
547 warn("%s", tag_files
->ofn
);
552 mandoc_msg_setrc(MANDOCLEVEL_SYSERR
);
556 if (argc
> 1 && curp
.outtype
<= OUTT_UTF8
) {
557 if (curp
.outdata
== NULL
)
558 outdata_alloc(&curp
);
559 terminal_sepline(curp
.outdata
);
562 mandoc_msg(MANDOCERR_FILE
, 0, 0,
563 "%s", strerror(errno
));
565 if (curp
.wstop
&& mandoc_msg_getrc() != MANDOCLEVEL_OK
)
573 mparse_reset(curp
.mp
);
575 if (startdir
!= -1) {
576 (void)fchdir(startdir
);
580 if (curp
.outdata
!= NULL
) {
581 switch (curp
.outtype
) {
583 html_free(curp
.outdata
);
588 ascii_free(curp
.outdata
);
592 pspdf_free(curp
.outdata
);
599 mparse_free(curp
.mp
);
603 if (search
.argmode
!= ARG_FILE
) {
605 mansearch_free(res
, sz
);
611 * When using a pager, finish writing both temporary files,
612 * fork it, wait for the user to close it, and clean up.
615 if (tag_files
!= NULL
) {
618 man_pgid
= getpgid(0);
619 tag_files
->tcpgid
= man_pgid
== getpid() ?
620 getpgid(getppid()) : man_pgid
;
625 /* Stop here until moved to the foreground. */
627 tc_pgid
= tcgetpgrp(tag_files
->ofd
);
628 if (tc_pgid
!= man_pgid
) {
629 if (tc_pgid
== pager_pid
) {
630 (void)tcsetpgrp(tag_files
->ofd
,
632 if (signum
== SIGTTIN
)
635 tag_files
->tcpgid
= tc_pgid
;
640 /* Once in the foreground, activate the pager. */
643 (void)tcsetpgrp(tag_files
->ofd
, pager_pid
);
644 kill(pager_pid
, SIGCONT
);
646 pager_pid
= spawn_pager(tag_files
);
648 /* Wait for the pager to stop or exit. */
650 while ((pid
= waitpid(pager_pid
, &status
,
651 WUNTRACED
)) == -1 && errno
== EINTR
)
656 mandoc_msg_setrc(MANDOCLEVEL_SYSERR
);
659 if (!WIFSTOPPED(status
))
662 signum
= WSTOPSIG(status
);
666 return (int)mandoc_msg_getrc();
670 usage(enum argmode argmode
)
675 fputs("usage: mandoc [-ac] [-I os=name] "
676 "[-K encoding] [-mdoc | -man] [-O options]\n"
677 "\t [-T output] [-W level] [file ...]\n", stderr
);
680 fputs("usage: man [-acfhklw] [-C file] [-M path] "
681 "[-m path] [-S subsection]\n"
682 "\t [[-s] section] name ...\n", stderr
);
685 fputs("usage: whatis [-afk] [-C file] "
686 "[-M path] [-m path] [-O outkey] [-S arch]\n"
687 "\t [-s section] name ...\n", stderr
);
690 fputs("usage: apropos [-afk] [-C file] "
691 "[-M path] [-m path] [-O outkey] [-S arch]\n"
692 "\t [-s section] expression ...\n", stderr
);
695 exit((int)MANDOCLEVEL_BADARG
);
699 fs_lookup(const struct manpaths
*paths
, size_t ipath
,
700 const char *sec
, const char *arch
, const char *name
,
701 struct manpage
**res
, size_t *ressz
)
704 struct manpage
*page
;
710 mandoc_asprintf(&file
, "%s/man%s/%s.%s",
711 paths
->paths
[ipath
], sec
, name
, sec
);
712 if (access(file
, R_OK
) != -1)
716 mandoc_asprintf(&file
, "%s/cat%s/%s.0",
717 paths
->paths
[ipath
], sec
, name
);
718 if (access(file
, R_OK
) != -1) {
725 mandoc_asprintf(&file
, "%s/man%s/%s/%s.%s",
726 paths
->paths
[ipath
], sec
, arch
, name
, sec
);
727 if (access(file
, R_OK
) != -1)
732 mandoc_asprintf(&file
, "%s/man%s/%s.[01-9]*",
733 paths
->paths
[ipath
], sec
, name
);
734 globres
= glob(file
, 0, NULL
, &globinfo
);
735 if (globres
!= 0 && globres
!= GLOB_NOMATCH
)
736 warn("%s: glob", file
);
739 file
= mandoc_strdup(*globinfo
.gl_pathv
);
743 if (res
!= NULL
|| ipath
+ 1 != paths
->sz
)
746 mandoc_asprintf(&file
, "%s.%s", name
, sec
);
747 globres
= access(file
, R_OK
);
749 return globres
!= -1;
752 warnx("outdated mandoc.db lacks %s(%s) entry, run %s %s",
753 name
, sec
, BINM_MAKEWHATIS
, paths
->paths
[ipath
]);
758 *res
= mandoc_reallocarray(*res
, ++*ressz
, sizeof(struct manpage
));
759 page
= *res
+ (*ressz
- 1);
764 page
->sec
= (*sec
>= '1' && *sec
<= '9') ? *sec
- '1' + 1 : 10;
770 fs_search(const struct mansearch
*cfg
, const struct manpaths
*paths
,
771 int argc
, char **argv
, struct manpage
**res
, size_t *ressz
)
773 const char *const sections
[] =
774 {"1", "8", "6", "2", "3", "5", "7", "4", "9", "3p"};
775 const size_t nsec
= sizeof(sections
)/sizeof(sections
[0]);
777 size_t ipath
, isec
, lastsz
;
779 assert(cfg
->argmode
== ARG_NAME
);
785 for (ipath
= 0; ipath
< paths
->sz
; ipath
++) {
786 if (cfg
->sec
!= NULL
) {
787 if (fs_lookup(paths
, ipath
, cfg
->sec
,
788 cfg
->arch
, *argv
, res
, ressz
) &&
791 } else for (isec
= 0; isec
< nsec
; isec
++)
792 if (fs_lookup(paths
, ipath
, sections
[isec
],
793 cfg
->arch
, *argv
, res
, ressz
) &&
797 if (res
!= NULL
&& *ressz
== lastsz
&&
798 strchr(*argv
, '/') == NULL
) {
799 if (cfg
->sec
== NULL
)
800 warnx("No entry for %s in the manual.",
803 warnx("No entry for %s in section %s "
804 "of the manual.", *argv
, cfg
->sec
);
814 parse(struct curparse
*curp
, int fd
, const char *file
)
816 struct roff_meta
*meta
;
818 /* Begin by parsing the file itself. */
823 mparse_readfd(curp
->mp
, fd
, file
);
824 if (fd
!= STDIN_FILENO
)
828 * With -Wstop and warnings or errors of at least the requested
829 * level, do not produce output.
832 if (curp
->wstop
&& mandoc_msg_getrc() != MANDOCLEVEL_OK
)
835 if (curp
->outdata
== NULL
)
839 meta
= mparse_result(curp
->mp
);
841 /* Execute the out device, if it exists. */
843 if (meta
->macroset
== MACROSET_MDOC
) {
844 switch (curp
->outtype
) {
846 html_mdoc(curp
->outdata
, meta
);
849 tree_mdoc(curp
->outdata
, meta
);
852 man_mdoc(curp
->outdata
, meta
);
859 terminal_mdoc(curp
->outdata
, meta
);
862 markdown_mdoc(curp
->outdata
, meta
);
868 if (meta
->macroset
== MACROSET_MAN
) {
869 switch (curp
->outtype
) {
871 html_man(curp
->outdata
, meta
);
874 tree_man(curp
->outdata
, meta
);
877 mparse_copy(curp
->mp
);
884 terminal_man(curp
->outdata
, meta
);
890 if (mandoc_msg_getmin() < MANDOCERR_STYLE
)
897 static struct manpaths paths
;
898 struct mansearch search
;
899 struct mandoc_xr
*xr
;
903 manpath_base(&paths
);
905 for (xr
= mandoc_xr_get(); xr
!= NULL
; xr
= xr
->next
) {
909 search
.sec
= xr
->sec
;
910 search
.outkey
= NULL
;
911 search
.argmode
= ARG_NAME
;
912 search
.firstmatch
= 1;
913 if (mansearch(&search
, &paths
, 1, &xr
->name
, NULL
, &sz
))
915 if (fs_search(&search
, &paths
, 1, &xr
->name
, NULL
, &sz
))
918 mandoc_msg(MANDOCERR_XR_BAD
, xr
->line
,
919 xr
->pos
+ 1, "Xr %s %s", xr
->name
, xr
->sec
);
921 mandoc_msg(MANDOCERR_XR_BAD
, xr
->line
,
922 xr
->pos
+ 1, "Xr %s %s (%d times)",
923 xr
->name
, xr
->sec
, xr
->count
);
928 outdata_alloc(struct curparse
*curp
)
930 switch (curp
->outtype
) {
932 curp
->outdata
= html_alloc(curp
->outopts
);
935 curp
->outdata
= utf8_alloc(curp
->outopts
);
938 curp
->outdata
= locale_alloc(curp
->outopts
);
941 curp
->outdata
= ascii_alloc(curp
->outopts
);
944 curp
->outdata
= pdf_alloc(curp
->outopts
);
947 curp
->outdata
= ps_alloc(curp
->outopts
);
955 passthrough(const char *file
, int fd
, int synopsis_only
)
957 const char synb
[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
958 const char synr
[] = "SYNOPSIS";
964 ssize_t len
, written
;
970 if (fflush(stdout
) == EOF
) {
975 if ((stream
= fdopen(fd
, "r")) == NULL
) {
982 while ((len
= getline(&line
, &linesz
, stream
)) != -1) {
986 if ( ! isspace((unsigned char)*cp
))
988 while (isspace((unsigned char)*cp
)) {
993 if (strcmp(cp
, synb
) == 0 ||
994 strcmp(cp
, synr
) == 0)
999 for (; len
> 0; len
-= written
) {
1000 if ((written
= write(STDOUT_FILENO
, cp
, len
)) != -1)
1008 if (ferror(stream
)) {
1010 syscall
= "getline";
1021 warn("%s: SYSERR: %s", file
, syscall
);
1022 mandoc_msg_setrc(MANDOCLEVEL_SYSERR
);
1026 koptions(int *options
, char *arg
)
1029 if ( ! strcmp(arg
, "utf-8")) {
1030 *options
|= MPARSE_UTF8
;
1031 *options
&= ~MPARSE_LATIN1
;
1032 } else if ( ! strcmp(arg
, "iso-8859-1")) {
1033 *options
|= MPARSE_LATIN1
;
1034 *options
&= ~MPARSE_UTF8
;
1035 } else if ( ! strcmp(arg
, "us-ascii")) {
1036 *options
&= ~(MPARSE_UTF8
| MPARSE_LATIN1
);
1038 warnx("-K %s: Bad argument", arg
);
1045 moptions(int *options
, char *arg
)
1050 if (strcmp(arg
, "doc") == 0)
1051 *options
|= MPARSE_MDOC
;
1052 else if (strcmp(arg
, "an") == 0)
1053 *options
|= MPARSE_MAN
;
1057 toptions(struct curparse
*curp
, char *arg
)
1060 if (0 == strcmp(arg
, "ascii"))
1061 curp
->outtype
= OUTT_ASCII
;
1062 else if (0 == strcmp(arg
, "lint")) {
1063 curp
->outtype
= OUTT_LINT
;
1064 mandoc_msg_setoutfile(stdout
);
1065 mandoc_msg_setmin(MANDOCERR_BASE
);
1066 } else if (0 == strcmp(arg
, "tree"))
1067 curp
->outtype
= OUTT_TREE
;
1068 else if (0 == strcmp(arg
, "man"))
1069 curp
->outtype
= OUTT_MAN
;
1070 else if (0 == strcmp(arg
, "html"))
1071 curp
->outtype
= OUTT_HTML
;
1072 else if (0 == strcmp(arg
, "markdown"))
1073 curp
->outtype
= OUTT_MARKDOWN
;
1074 else if (0 == strcmp(arg
, "utf8"))
1075 curp
->outtype
= OUTT_UTF8
;
1076 else if (0 == strcmp(arg
, "locale"))
1077 curp
->outtype
= OUTT_LOCALE
;
1078 else if (0 == strcmp(arg
, "ps"))
1079 curp
->outtype
= OUTT_PS
;
1080 else if (0 == strcmp(arg
, "pdf"))
1081 curp
->outtype
= OUTT_PDF
;
1083 warnx("-T %s: Bad argument", arg
);
1091 woptions(struct curparse
*curp
, char *arg
)
1094 const char *toks
[11];
1100 toks
[4] = "warning";
1104 toks
[8] = "openbsd";
1110 switch (getsubopt(&arg
, (char * const *)toks
, &v
)) {
1116 mandoc_msg_setmin(MANDOCERR_BASE
);
1119 mandoc_msg_setmin(MANDOCERR_STYLE
);
1122 mandoc_msg_setmin(MANDOCERR_WARNING
);
1125 mandoc_msg_setmin(MANDOCERR_ERROR
);
1128 mandoc_msg_setmin(MANDOCERR_UNSUPP
);
1131 mandoc_msg_setmin(MANDOCERR_MAX
);
1134 mandoc_msg_setmin(MANDOCERR_BASE
);
1135 curp
->os_e
= MANDOC_OS_OPENBSD
;
1138 mandoc_msg_setmin(MANDOCERR_BASE
);
1139 curp
->os_e
= MANDOC_OS_NETBSD
;
1142 warnx("-W %s: Bad argument", o
);
1150 spawn_pager(struct tag_files
*tag_files
)
1152 const struct timespec timeout
= { 0, 100000000 }; /* 0.1s */
1153 #define MAX_PAGER_ARGS 16
1154 char *argv
[MAX_PAGER_ARGS
];
1161 pager
= getenv("MANPAGER");
1162 if (pager
== NULL
|| *pager
== '\0')
1163 pager
= getenv("PAGER");
1164 if (pager
== NULL
|| *pager
== '\0')
1166 cp
= mandoc_strdup(pager
);
1169 * Parse the pager command into words.
1170 * Intentionally do not do anything fancy here.
1174 while (argc
+ 5 < MAX_PAGER_ARGS
) {
1176 cp
= strchr(cp
, ' ');
1186 /* For less(1), use the tag file. */
1189 if ((cmdlen
= strlen(argv
[0])) >= 4) {
1190 cp
= argv
[0] + cmdlen
- 4;
1191 if (strcmp(cp
, "less") == 0) {
1192 argv
[argc
++] = mandoc_strdup("-T");
1193 argv
[argc
++] = tag_files
->tfn
;
1194 if (tag_files
->tagname
!= NULL
) {
1195 argv
[argc
++] = mandoc_strdup("-t");
1196 argv
[argc
++] = tag_files
->tagname
;
1202 argv
[argc
++] = tag_files
->ofn
;
1205 switch (pager_pid
= fork()) {
1207 err((int)MANDOCLEVEL_SYSERR
, "fork");
1211 (void)setpgid(pager_pid
, 0);
1212 (void)tcsetpgrp(tag_files
->ofd
, pager_pid
);
1214 if (pledge("stdio rpath tmppath tty proc", NULL
) == -1)
1215 err((int)MANDOCLEVEL_SYSERR
, "pledge");
1217 tag_files
->pager_pid
= pager_pid
;
1221 /* The child process becomes the pager. */
1223 if (dup2(tag_files
->ofd
, STDOUT_FILENO
) == -1)
1224 err((int)MANDOCLEVEL_SYSERR
, "pager stdout");
1225 close(tag_files
->ofd
);
1226 assert(tag_files
->tfd
== -1);
1228 /* Do not start the pager before controlling the terminal. */
1230 while (tcgetpgrp(STDOUT_FILENO
) != getpid())
1231 nanosleep(&timeout
, NULL
);
1233 execvp(argv
[0], argv
);
1234 err((int)MANDOCLEVEL_SYSERR
, "exec %s", argv
[0]);