]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
a863915c6540b77c473441a09f8fb1fbcf34d5c3
1 /* $Id: main.c,v 1.311 2018/12/13 11:55:46 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 mandocerr mmin
; /* ignore messages below this */
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(const char *);
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 int koptions(int *, char *);
102 static void moptions(int *, char *);
103 static void mmsg(enum mandocerr
, enum mandoclevel
,
104 const char *, int, int, const char *);
105 static void outdata_alloc(struct curparse
*);
106 static void parse(struct curparse
*, int, const char *);
107 static void passthrough(const char *, int, int);
108 static pid_t
spawn_pager(struct tag_files
*);
109 static int toptions(struct curparse
*, char *);
110 static void usage(enum argmode
) __attribute__((__noreturn__
));
111 static int woptions(struct curparse
*, char *);
113 static const int sec_prios
[] = {1, 4, 5, 8, 6, 3, 7, 2, 9};
114 static char help_arg
[] = "help";
115 static char *help_argv
[] = {help_arg
, NULL
};
116 static enum mandoclevel rc
;
117 static FILE *mmsg_stream
;
121 main(int argc
, char *argv
[])
124 struct mansearch search
;
125 struct curparse curp
;
127 struct tag_files
*tag_files
;
128 struct manpage
*res
, *resp
;
129 const char *progname
, *sec
, *thisarg
;
130 char *conf_file
, *defpaths
, *auxpaths
;
135 enum outmode outmode
;
142 pid_t pager_pid
, tc_pgid
, man_pgid
, pid
;
145 progname
= getprogname();
148 progname
= mandoc_strdup("mandoc");
149 else if ((progname
= strrchr(argv
[0], '/')) == NULL
)
153 setprogname(progname
);
156 if (strncmp(progname
, "mandocdb", 8) == 0 ||
157 strcmp(progname
, BINM_MAKEWHATIS
) == 0)
158 return mandocdb(argc
, argv
);
161 if (pledge("stdio rpath tmppath tty proc exec", NULL
) == -1)
162 err((int)MANDOCLEVEL_SYSERR
, "pledge");
165 #if HAVE_SANDBOX_INIT
166 if (sandbox_init(kSBXProfileNoInternet
, SANDBOX_NAMED
, NULL
) == -1)
167 errx((int)MANDOCLEVEL_SYSERR
, "sandbox_init");
170 /* Search options. */
172 memset(&conf
, 0, sizeof(conf
));
173 conf_file
= defpaths
= NULL
;
176 memset(&search
, 0, sizeof(struct mansearch
));
177 search
.outkey
= "Nd";
180 if (strcmp(progname
, BINM_MAN
) == 0)
181 search
.argmode
= ARG_NAME
;
182 else if (strcmp(progname
, BINM_APROPOS
) == 0)
183 search
.argmode
= ARG_EXPR
;
184 else if (strcmp(progname
, BINM_WHATIS
) == 0)
185 search
.argmode
= ARG_WORD
;
186 else if (strncmp(progname
, "help", 4) == 0)
187 search
.argmode
= ARG_NAME
;
189 search
.argmode
= ARG_FILE
;
191 /* Parser and formatter options. */
193 memset(&curp
, 0, sizeof(struct curparse
));
194 curp
.outtype
= OUTT_LOCALE
;
195 curp
.mmin
= MANDOCERR_MAX
;
196 curp
.outopts
= &conf
.output
;
197 options
= MPARSE_SO
| MPARSE_UTF8
| MPARSE_LATIN1
;
198 mmsg_stream
= stderr
;
203 outmode
= OUTMODE_DEF
;
205 while ((c
= getopt(argc
, argv
,
206 "aC:cfhI:iK:klM:m:O:S:s:T:VW:w")) != -1) {
207 if (c
== 'i' && search
.argmode
== ARG_EXPR
) {
213 outmode
= OUTMODE_ALL
;
222 search
.argmode
= ARG_WORD
;
225 conf
.output
.synopsisonly
= 1;
227 outmode
= OUTMODE_ALL
;
230 if (strncmp(optarg
, "os=", 3)) {
231 warnx("-I %s: Bad argument", optarg
);
232 return (int)MANDOCLEVEL_BADARG
;
234 if (curp
.os_s
!= NULL
) {
235 warnx("-I %s: Duplicate argument", optarg
);
236 return (int)MANDOCLEVEL_BADARG
;
238 curp
.os_s
= mandoc_strdup(optarg
+ 3);
241 if ( ! koptions(&options
, optarg
))
242 return (int)MANDOCLEVEL_BADARG
;
245 search
.argmode
= ARG_EXPR
;
248 search
.argmode
= ARG_FILE
;
249 outmode
= OUTMODE_ALL
;
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 (outmode
== OUTMODE_FLN
||
319 outmode
== OUTMODE_LST
||
320 !isatty(STDOUT_FILENO
))
324 (conf
.output
.width
== 0 || conf
.output
.indent
== 0) &&
325 ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &ws
) != -1 &&
327 if (conf
.output
.width
== 0 && ws
.ws_col
< 79)
328 conf
.output
.width
= ws
.ws_col
- 1;
329 if (conf
.output
.indent
== 0 && ws
.ws_col
< 66)
330 conf
.output
.indent
= 3;
335 if (pledge("stdio rpath", NULL
) == -1)
336 err((int)MANDOCLEVEL_SYSERR
, "pledge");
339 /* Parse arguments. */
349 * and for a man(1) section argument without -s.
352 if (search
.argmode
== ARG_NAME
) {
353 if (*progname
== 'h') {
358 } else if (argc
> 1 &&
359 ((uc
= (unsigned char *)argv
[0]) != NULL
) &&
360 ((isdigit(uc
[0]) && (uc
[1] == '\0' ||
361 (isalpha(uc
[1]) && uc
[2] == '\0'))) ||
362 (uc
[0] == 'n' && uc
[1] == '\0'))) {
363 search
.sec
= (char *)uc
;
367 if (search
.arch
== NULL
)
368 search
.arch
= getenv("MACHINE");
370 if (search
.arch
== NULL
)
371 search
.arch
= MACHINE
;
377 /* man(1), whatis(1), apropos(1) */
379 if (search
.argmode
!= ARG_FILE
) {
380 if (search
.argmode
== ARG_NAME
&&
381 outmode
== OUTMODE_ONE
)
382 search
.firstmatch
= 1;
384 /* Access the mandoc database. */
386 manconf_parse(&conf
, conf_file
, defpaths
, auxpaths
);
387 if ( ! mansearch(&search
, &conf
.manpath
,
388 argc
, argv
, &res
, &sz
))
389 usage(search
.argmode
);
391 if (sz
== 0 && search
.argmode
== ARG_NAME
)
392 fs_search(&search
, &conf
.manpath
,
393 argc
, argv
, &res
, &sz
);
395 if (search
.argmode
== ARG_NAME
) {
396 for (c
= 0; c
< argc
; c
++) {
397 if (strchr(argv
[c
], '/') == NULL
)
399 if (access(argv
[c
], R_OK
) == -1) {
403 res
= mandoc_reallocarray(res
,
404 sz
+ 1, sizeof(*res
));
405 res
[sz
].file
= mandoc_strdup(argv
[c
]);
406 res
[sz
].names
= NULL
;
407 res
[sz
].output
= NULL
;
408 res
[sz
].ipath
= SIZE_MAX
;
410 res
[sz
].form
= FORM_SRC
;
416 if (search
.argmode
!= ARG_NAME
)
417 warnx("nothing appropriate");
418 rc
= MANDOCLEVEL_BADARG
;
423 * For standard man(1) and -a output mode,
424 * prepare for copying filename pointers
425 * into the program parameter array.
428 if (outmode
== OUTMODE_ONE
) {
431 } else if (outmode
== OUTMODE_ALL
)
434 /* Iterate all matching manuals. */
437 for (i
= 0; i
< sz
; i
++) {
438 if (outmode
== OUTMODE_FLN
)
440 else if (outmode
== OUTMODE_LST
)
441 printf("%s - %s\n", res
[i
].names
,
442 res
[i
].output
== NULL
? "" :
444 else if (outmode
== OUTMODE_ONE
) {
445 /* Search for the best section. */
447 sec
+= strcspn(sec
, "123456789");
450 prio
= sec_prios
[sec
[0] - '1'];
453 if (prio
>= best_prio
)
461 * For man(1), -a and -i output mode, fall through
462 * to the main mandoc(1) code iterating files
463 * and running the parsers on each of them.
466 if (outmode
== OUTMODE_FLN
|| outmode
== OUTMODE_LST
)
474 if (pledge("stdio rpath tmppath tty proc exec", NULL
) == -1)
475 err((int)MANDOCLEVEL_SYSERR
, "pledge");
477 if (pledge("stdio rpath", NULL
) == -1)
478 err((int)MANDOCLEVEL_SYSERR
, "pledge");
482 if (search
.argmode
== ARG_FILE
)
483 moptions(&options
, auxpaths
);
486 curp
.mp
= mparse_alloc(options
, curp
.mmin
, mmsg
,
487 curp
.os_e
, curp
.os_s
);
491 tag_files
= tag_init();
492 parse(&curp
, STDIN_FILENO
, "<stdin>");
496 * Remember the original working directory, if possible.
497 * This will be needed if some names on the command line
498 * are page names and some are relative file names.
499 * Do not error out if the current directory is not
500 * readable: Maybe it won't be needed after all.
502 startdir
= open(".", O_RDONLY
| O_DIRECTORY
);
507 * Changing directories is not needed in ARG_FILE mode.
508 * Do it on a best-effort basis. Even in case of
509 * failure, some functionality may still work.
512 if (resp
->ipath
!= SIZE_MAX
)
513 (void)chdir(conf
.manpath
.paths
[resp
->ipath
]);
514 else if (startdir
!= -1)
515 (void)fchdir(startdir
);
518 fd
= mparse_open(curp
.mp
, resp
!= NULL
? resp
->file
: *argv
);
522 tag_files
= tag_init();
523 if (conf
.output
.tag
!= NULL
&&
524 tag_files
->tagname
== NULL
)
526 *conf
.output
.tag
!= '\0' ?
527 conf
.output
.tag
: *argv
;
531 parse(&curp
, fd
, *argv
);
532 else if (resp
->form
== FORM_SRC
)
533 parse(&curp
, fd
, resp
->file
);
535 passthrough(resp
->file
, fd
,
536 conf
.output
.synopsisonly
);
538 if (ferror(stdout
)) {
539 if (tag_files
!= NULL
) {
540 warn("%s", tag_files
->ofn
);
545 rc
= MANDOCLEVEL_SYSERR
;
549 if (argc
> 1 && curp
.outtype
<= OUTT_UTF8
) {
550 if (curp
.outdata
== NULL
)
551 outdata_alloc(&curp
);
552 terminal_sepline(curp
.outdata
);
554 } else if (rc
< MANDOCLEVEL_ERROR
)
555 rc
= MANDOCLEVEL_ERROR
;
557 if (MANDOCLEVEL_OK
!= rc
&& curp
.wstop
)
565 mparse_reset(curp
.mp
);
567 if (startdir
!= -1) {
568 (void)fchdir(startdir
);
572 if (curp
.outdata
!= NULL
) {
573 switch (curp
.outtype
) {
575 html_free(curp
.outdata
);
580 ascii_free(curp
.outdata
);
584 pspdf_free(curp
.outdata
);
591 mparse_free(curp
.mp
);
595 if (search
.argmode
!= ARG_FILE
) {
597 mansearch_free(res
, sz
);
603 * When using a pager, finish writing both temporary files,
604 * fork it, wait for the user to close it, and clean up.
607 if (tag_files
!= NULL
) {
610 man_pgid
= getpgid(0);
611 tag_files
->tcpgid
= man_pgid
== getpid() ?
612 getpgid(getppid()) : man_pgid
;
617 /* Stop here until moved to the foreground. */
619 tc_pgid
= tcgetpgrp(tag_files
->ofd
);
620 if (tc_pgid
!= man_pgid
) {
621 if (tc_pgid
== pager_pid
) {
622 (void)tcsetpgrp(tag_files
->ofd
,
624 if (signum
== SIGTTIN
)
627 tag_files
->tcpgid
= tc_pgid
;
632 /* Once in the foreground, activate the pager. */
635 (void)tcsetpgrp(tag_files
->ofd
, pager_pid
);
636 kill(pager_pid
, SIGCONT
);
638 pager_pid
= spawn_pager(tag_files
);
640 /* Wait for the pager to stop or exit. */
642 while ((pid
= waitpid(pager_pid
, &status
,
643 WUNTRACED
)) == -1 && errno
== EINTR
)
648 rc
= MANDOCLEVEL_SYSERR
;
651 if (!WIFSTOPPED(status
))
654 signum
= WSTOPSIG(status
);
663 usage(enum argmode argmode
)
668 fputs("usage: mandoc [-ac] [-I os=name] "
669 "[-K encoding] [-mdoc | -man] [-O options]\n"
670 "\t [-T output] [-W level] [file ...]\n", stderr
);
673 fputs("usage: man [-acfhklw] [-C file] [-M path] "
674 "[-m path] [-S subsection]\n"
675 "\t [[-s] section] name ...\n", stderr
);
678 fputs("usage: whatis [-afk] [-C file] "
679 "[-M path] [-m path] [-O outkey] [-S arch]\n"
680 "\t [-s section] name ...\n", stderr
);
683 fputs("usage: apropos [-afk] [-C file] "
684 "[-M path] [-m path] [-O outkey] [-S arch]\n"
685 "\t [-s section] expression ...\n", stderr
);
688 exit((int)MANDOCLEVEL_BADARG
);
692 fs_lookup(const struct manpaths
*paths
, size_t ipath
,
693 const char *sec
, const char *arch
, const char *name
,
694 struct manpage
**res
, size_t *ressz
)
697 struct manpage
*page
;
703 mandoc_asprintf(&file
, "%s/man%s/%s.%s",
704 paths
->paths
[ipath
], sec
, name
, sec
);
705 if (access(file
, R_OK
) != -1)
709 mandoc_asprintf(&file
, "%s/cat%s/%s.0",
710 paths
->paths
[ipath
], sec
, name
);
711 if (access(file
, R_OK
) != -1) {
718 mandoc_asprintf(&file
, "%s/man%s/%s/%s.%s",
719 paths
->paths
[ipath
], sec
, arch
, name
, sec
);
720 if (access(file
, R_OK
) != -1)
725 mandoc_asprintf(&file
, "%s/man%s/%s.[01-9]*",
726 paths
->paths
[ipath
], sec
, name
);
727 globres
= glob(file
, 0, NULL
, &globinfo
);
728 if (globres
!= 0 && globres
!= GLOB_NOMATCH
)
729 warn("%s: glob", file
);
732 file
= mandoc_strdup(*globinfo
.gl_pathv
);
736 if (res
!= NULL
|| ipath
+ 1 != paths
->sz
)
739 mandoc_asprintf(&file
, "%s.%s", name
, sec
);
740 globres
= access(file
, R_OK
);
742 return globres
!= -1;
745 warnx("outdated mandoc.db lacks %s(%s) entry, run %s %s",
746 name
, sec
, BINM_MAKEWHATIS
, paths
->paths
[ipath
]);
751 *res
= mandoc_reallocarray(*res
, ++*ressz
, sizeof(struct manpage
));
752 page
= *res
+ (*ressz
- 1);
757 page
->sec
= (*sec
>= '1' && *sec
<= '9') ? *sec
- '1' + 1 : 10;
763 fs_search(const struct mansearch
*cfg
, const struct manpaths
*paths
,
764 int argc
, char **argv
, struct manpage
**res
, size_t *ressz
)
766 const char *const sections
[] =
767 {"1", "8", "6", "2", "3", "5", "7", "4", "9", "3p"};
768 const size_t nsec
= sizeof(sections
)/sizeof(sections
[0]);
770 size_t ipath
, isec
, lastsz
;
772 assert(cfg
->argmode
== ARG_NAME
);
778 for (ipath
= 0; ipath
< paths
->sz
; ipath
++) {
779 if (cfg
->sec
!= NULL
) {
780 if (fs_lookup(paths
, ipath
, cfg
->sec
,
781 cfg
->arch
, *argv
, res
, ressz
) &&
784 } else for (isec
= 0; isec
< nsec
; isec
++)
785 if (fs_lookup(paths
, ipath
, sections
[isec
],
786 cfg
->arch
, *argv
, res
, ressz
) &&
790 if (res
!= NULL
&& *ressz
== lastsz
&&
791 strchr(*argv
, '/') == NULL
) {
792 if (cfg
->sec
== NULL
)
793 warnx("No entry for %s in the manual.",
796 warnx("No entry for %s in section %s "
797 "of the manual.", *argv
, cfg
->sec
);
807 parse(struct curparse
*curp
, int fd
, const char *file
)
809 enum mandoclevel rctmp
;
810 struct roff_man
*man
;
812 /* Begin by parsing the file itself. */
817 rctmp
= mparse_readfd(curp
->mp
, fd
, file
);
818 if (fd
!= STDIN_FILENO
)
824 * With -Wstop and warnings or errors of at least the requested
825 * level, do not produce output.
828 if (rctmp
!= MANDOCLEVEL_OK
&& curp
->wstop
)
831 if (curp
->outdata
== NULL
)
834 mparse_result(curp
->mp
, &man
, NULL
);
836 /* Execute the out device, if it exists. */
841 if (man
->macroset
== MACROSET_MDOC
) {
842 if (curp
->outtype
!= OUTT_TREE
|| !curp
->outopts
->noval
)
844 switch (curp
->outtype
) {
846 html_mdoc(curp
->outdata
, man
);
849 tree_mdoc(curp
->outdata
, man
);
852 man_mdoc(curp
->outdata
, man
);
859 terminal_mdoc(curp
->outdata
, man
);
862 markdown_mdoc(curp
->outdata
, man
);
868 if (man
->macroset
== MACROSET_MAN
) {
869 if (curp
->outtype
!= OUTT_TREE
|| !curp
->outopts
->noval
)
871 switch (curp
->outtype
) {
873 html_man(curp
->outdata
, man
);
876 tree_man(curp
->outdata
, man
);
879 mparse_copy(curp
->mp
);
886 terminal_man(curp
->outdata
, man
);
892 if (curp
->mmin
< MANDOCERR_STYLE
)
894 mparse_updaterc(curp
->mp
, &rc
);
898 check_xr(const char *file
)
900 static struct manpaths paths
;
901 struct mansearch search
;
902 struct mandoc_xr
*xr
;
907 manpath_base(&paths
);
909 for (xr
= mandoc_xr_get(); xr
!= NULL
; xr
= xr
->next
) {
913 search
.sec
= xr
->sec
;
914 search
.outkey
= NULL
;
915 search
.argmode
= ARG_NAME
;
916 search
.firstmatch
= 1;
917 if (mansearch(&search
, &paths
, 1, &xr
->name
, NULL
, &sz
))
919 if (fs_search(&search
, &paths
, 1, &xr
->name
, NULL
, &sz
))
922 mandoc_asprintf(&cp
, "Xr %s %s", xr
->name
, xr
->sec
);
924 mandoc_asprintf(&cp
, "Xr %s %s (%d times)",
925 xr
->name
, xr
->sec
, xr
->count
);
926 mmsg(MANDOCERR_XR_BAD
, MANDOCLEVEL_STYLE
,
927 file
, xr
->line
, xr
->pos
+ 1, cp
);
933 outdata_alloc(struct curparse
*curp
)
935 switch (curp
->outtype
) {
937 curp
->outdata
= html_alloc(curp
->outopts
);
940 curp
->outdata
= utf8_alloc(curp
->outopts
);
943 curp
->outdata
= locale_alloc(curp
->outopts
);
946 curp
->outdata
= ascii_alloc(curp
->outopts
);
949 curp
->outdata
= pdf_alloc(curp
->outopts
);
952 curp
->outdata
= ps_alloc(curp
->outopts
);
960 passthrough(const char *file
, int fd
, int synopsis_only
)
962 const char synb
[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
963 const char synr
[] = "SYNOPSIS";
969 ssize_t len
, written
;
975 if (fflush(stdout
) == EOF
) {
980 if ((stream
= fdopen(fd
, "r")) == NULL
) {
987 while ((len
= getline(&line
, &linesz
, stream
)) != -1) {
991 if ( ! isspace((unsigned char)*cp
))
993 while (isspace((unsigned char)*cp
)) {
998 if (strcmp(cp
, synb
) == 0 ||
999 strcmp(cp
, synr
) == 0)
1004 for (; len
> 0; len
-= written
) {
1005 if ((written
= write(STDOUT_FILENO
, cp
, len
)) != -1)
1013 if (ferror(stream
)) {
1015 syscall
= "getline";
1026 warn("%s: SYSERR: %s", file
, syscall
);
1027 if (rc
< MANDOCLEVEL_SYSERR
)
1028 rc
= MANDOCLEVEL_SYSERR
;
1032 koptions(int *options
, char *arg
)
1035 if ( ! strcmp(arg
, "utf-8")) {
1036 *options
|= MPARSE_UTF8
;
1037 *options
&= ~MPARSE_LATIN1
;
1038 } else if ( ! strcmp(arg
, "iso-8859-1")) {
1039 *options
|= MPARSE_LATIN1
;
1040 *options
&= ~MPARSE_UTF8
;
1041 } else if ( ! strcmp(arg
, "us-ascii")) {
1042 *options
&= ~(MPARSE_UTF8
| MPARSE_LATIN1
);
1044 warnx("-K %s: Bad argument", arg
);
1051 moptions(int *options
, char *arg
)
1056 if (strcmp(arg
, "doc") == 0)
1057 *options
|= MPARSE_MDOC
;
1058 else if (strcmp(arg
, "an") == 0)
1059 *options
|= MPARSE_MAN
;
1063 toptions(struct curparse
*curp
, char *arg
)
1066 if (0 == strcmp(arg
, "ascii"))
1067 curp
->outtype
= OUTT_ASCII
;
1068 else if (0 == strcmp(arg
, "lint")) {
1069 curp
->outtype
= OUTT_LINT
;
1070 curp
->mmin
= MANDOCERR_BASE
;
1071 mmsg_stream
= stdout
;
1072 } else if (0 == strcmp(arg
, "tree"))
1073 curp
->outtype
= OUTT_TREE
;
1074 else if (0 == strcmp(arg
, "man"))
1075 curp
->outtype
= OUTT_MAN
;
1076 else if (0 == strcmp(arg
, "html"))
1077 curp
->outtype
= OUTT_HTML
;
1078 else if (0 == strcmp(arg
, "markdown"))
1079 curp
->outtype
= OUTT_MARKDOWN
;
1080 else if (0 == strcmp(arg
, "utf8"))
1081 curp
->outtype
= OUTT_UTF8
;
1082 else if (0 == strcmp(arg
, "locale"))
1083 curp
->outtype
= OUTT_LOCALE
;
1084 else if (0 == strcmp(arg
, "ps"))
1085 curp
->outtype
= OUTT_PS
;
1086 else if (0 == strcmp(arg
, "pdf"))
1087 curp
->outtype
= OUTT_PDF
;
1089 warnx("-T %s: Bad argument", arg
);
1097 woptions(struct curparse
*curp
, char *arg
)
1100 const char *toks
[11];
1106 toks
[4] = "warning";
1110 toks
[8] = "openbsd";
1116 switch (getsubopt(&arg
, (char * const *)toks
, &v
)) {
1122 curp
->mmin
= MANDOCERR_BASE
;
1125 curp
->mmin
= MANDOCERR_STYLE
;
1128 curp
->mmin
= MANDOCERR_WARNING
;
1131 curp
->mmin
= MANDOCERR_ERROR
;
1134 curp
->mmin
= MANDOCERR_UNSUPP
;
1137 curp
->mmin
= MANDOCERR_MAX
;
1140 curp
->mmin
= MANDOCERR_BASE
;
1141 curp
->os_e
= MANDOC_OS_OPENBSD
;
1144 curp
->mmin
= MANDOCERR_BASE
;
1145 curp
->os_e
= MANDOC_OS_NETBSD
;
1148 warnx("-W %s: Bad argument", o
);
1156 mmsg(enum mandocerr t
, enum mandoclevel lvl
,
1157 const char *file
, int line
, int col
, const char *msg
)
1159 const char *mparse_msg
;
1161 fprintf(mmsg_stream
, "%s: %s:", getprogname(),
1162 file
== NULL
? "<stdin>" : file
);
1165 fprintf(mmsg_stream
, "%d:%d:", line
, col
+ 1);
1167 fprintf(mmsg_stream
, " %s", mparse_strlevel(lvl
));
1169 if ((mparse_msg
= mparse_strerror(t
)) != NULL
)
1170 fprintf(mmsg_stream
, ": %s", mparse_msg
);
1173 fprintf(mmsg_stream
, ": %s", msg
);
1175 fputc('\n', mmsg_stream
);
1179 spawn_pager(struct tag_files
*tag_files
)
1181 const struct timespec timeout
= { 0, 100000000 }; /* 0.1s */
1182 #define MAX_PAGER_ARGS 16
1183 char *argv
[MAX_PAGER_ARGS
];
1190 pager
= getenv("MANPAGER");
1191 if (pager
== NULL
|| *pager
== '\0')
1192 pager
= getenv("PAGER");
1193 if (pager
== NULL
|| *pager
== '\0')
1195 cp
= mandoc_strdup(pager
);
1198 * Parse the pager command into words.
1199 * Intentionally do not do anything fancy here.
1203 while (argc
+ 5 < MAX_PAGER_ARGS
) {
1205 cp
= strchr(cp
, ' ');
1215 /* For less(1), use the tag file. */
1218 if ((cmdlen
= strlen(argv
[0])) >= 4) {
1219 cp
= argv
[0] + cmdlen
- 4;
1220 if (strcmp(cp
, "less") == 0) {
1221 argv
[argc
++] = mandoc_strdup("-T");
1222 argv
[argc
++] = tag_files
->tfn
;
1223 if (tag_files
->tagname
!= NULL
) {
1224 argv
[argc
++] = mandoc_strdup("-t");
1225 argv
[argc
++] = tag_files
->tagname
;
1231 argv
[argc
++] = tag_files
->ofn
;
1234 switch (pager_pid
= fork()) {
1236 err((int)MANDOCLEVEL_SYSERR
, "fork");
1240 (void)setpgid(pager_pid
, 0);
1241 (void)tcsetpgrp(tag_files
->ofd
, pager_pid
);
1243 if (pledge("stdio rpath tmppath tty proc", NULL
) == -1)
1244 err((int)MANDOCLEVEL_SYSERR
, "pledge");
1246 tag_files
->pager_pid
= pager_pid
;
1250 /* The child process becomes the pager. */
1252 if (dup2(tag_files
->ofd
, STDOUT_FILENO
) == -1)
1253 err((int)MANDOCLEVEL_SYSERR
, "pager stdout");
1254 close(tag_files
->ofd
);
1255 assert(tag_files
->tfd
== -1);
1257 /* Do not start the pager before controlling the terminal. */
1259 while (tcgetpgrp(STDOUT_FILENO
) != getpid())
1260 nanosleep(&timeout
, NULL
);
1262 execvp(argv
[0], argv
);
1263 err((int)MANDOCLEVEL_SYSERR
, "exec %s", argv
[0]);