]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
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 */
24 #include <sys/termios.h>
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
;
260 search
.arch
= optarg
;
266 if ( ! toptions(&curp
, optarg
))
267 return (int)MANDOCLEVEL_BADARG
;
270 if ( ! woptions(&curp
, optarg
))
271 return (int)MANDOCLEVEL_BADARG
;
274 outmode
= OUTMODE_FLN
;
283 usage(search
.argmode
);
285 /* Postprocess options. */
287 if (outmode
== OUTMODE_DEF
) {
288 switch (search
.argmode
) {
290 outmode
= OUTMODE_ALL
;
294 outmode
= OUTMODE_ONE
;
297 outmode
= OUTMODE_LST
;
303 if (outmode
== OUTMODE_LST
)
304 search
.outkey
= oarg
;
306 while (oarg
!= NULL
) {
308 if (manconf_output(&conf
.output
,
309 strsep(&oarg
, ","), 0) == 0)
311 warnx("-O %s: Bad argument", thisarg
);
312 return (int)MANDOCLEVEL_BADARG
;
317 if (outmode
== OUTMODE_FLN
||
318 outmode
== OUTMODE_LST
||
319 !isatty(STDOUT_FILENO
))
323 (conf
.output
.width
== 0 || conf
.output
.indent
== 0) &&
324 ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &ws
) != -1 &&
326 if (conf
.output
.width
== 0 && ws
.ws_col
< 79)
327 conf
.output
.width
= ws
.ws_col
- 1;
328 if (conf
.output
.indent
== 0 && ws
.ws_col
< 66)
329 conf
.output
.indent
= 3;
334 if (pledge("stdio rpath", NULL
) == -1)
335 err((int)MANDOCLEVEL_SYSERR
, "pledge");
338 /* Parse arguments. */
348 * and for a man(1) section argument without -s.
351 if (search
.argmode
== ARG_NAME
) {
352 if (*progname
== 'h') {
357 } else if (argc
> 1 &&
358 ((uc
= (unsigned char *)argv
[0]) != NULL
) &&
359 ((isdigit(uc
[0]) && (uc
[1] == '\0' ||
360 (isalpha(uc
[1]) && uc
[2] == '\0'))) ||
361 (uc
[0] == 'n' && uc
[1] == '\0'))) {
362 search
.sec
= (char *)uc
;
366 if (search
.arch
== NULL
)
367 search
.arch
= getenv("MACHINE");
369 if (search
.arch
== NULL
)
370 search
.arch
= MACHINE
;
376 /* man(1), whatis(1), apropos(1) */
378 if (search
.argmode
!= ARG_FILE
) {
379 if (search
.argmode
== ARG_NAME
&&
380 outmode
== OUTMODE_ONE
)
381 search
.firstmatch
= 1;
383 /* Access the mandoc database. */
385 manconf_parse(&conf
, conf_file
, defpaths
, auxpaths
);
386 if ( ! mansearch(&search
, &conf
.manpath
,
387 argc
, argv
, &res
, &sz
))
388 usage(search
.argmode
);
390 if (sz
== 0 && search
.argmode
== ARG_NAME
)
391 fs_search(&search
, &conf
.manpath
,
392 argc
, argv
, &res
, &sz
);
394 if (search
.argmode
== ARG_NAME
) {
395 for (c
= 0; c
< argc
; c
++) {
396 if (strchr(argv
[c
], '/') == NULL
)
398 if (access(argv
[c
], R_OK
) == -1) {
402 res
= mandoc_reallocarray(res
,
403 sz
+ 1, sizeof(*res
));
404 res
[sz
].file
= mandoc_strdup(argv
[c
]);
405 res
[sz
].names
= NULL
;
406 res
[sz
].output
= NULL
;
407 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
);
490 * Conditionally start up the lookaside buffer before parsing.
492 if (OUTT_MAN
== curp
.outtype
)
493 mparse_keep(curp
.mp
);
497 tag_files
= tag_init();
498 parse(&curp
, STDIN_FILENO
, "<stdin>");
502 * Remember the original working directory, if possible.
503 * This will be needed if some names on the command line
504 * are page names and some are relative file names.
505 * Do not error out if the current directory is not
506 * readable: Maybe it won't be needed after all.
508 startdir
= open(".", O_RDONLY
| O_DIRECTORY
);
513 * Changing directories is not needed in ARG_FILE mode.
514 * Do it on a best-effort basis. Even in case of
515 * failure, some functionality may still work.
518 if (resp
->ipath
!= SIZE_MAX
)
519 (void)chdir(conf
.manpath
.paths
[resp
->ipath
]);
520 else if (startdir
!= -1)
521 (void)fchdir(startdir
);
524 fd
= mparse_open(curp
.mp
, resp
!= NULL
? resp
->file
: *argv
);
527 tag_files
= tag_init();
532 parse(&curp
, fd
, *argv
);
533 else if (resp
->form
== FORM_SRC
)
534 parse(&curp
, fd
, resp
->file
);
536 passthrough(resp
->file
, fd
,
537 conf
.output
.synopsisonly
);
539 if (ferror(stdout
)) {
540 if (tag_files
!= NULL
) {
541 warn("%s", tag_files
->ofn
);
546 rc
= MANDOCLEVEL_SYSERR
;
550 if (argc
> 1 && curp
.outtype
<= OUTT_UTF8
) {
551 if (curp
.outdata
== NULL
)
552 outdata_alloc(&curp
);
553 terminal_sepline(curp
.outdata
);
555 } else if (rc
< MANDOCLEVEL_ERROR
)
556 rc
= MANDOCLEVEL_ERROR
;
558 if (MANDOCLEVEL_OK
!= rc
&& curp
.wstop
)
566 mparse_reset(curp
.mp
);
568 if (startdir
!= -1) {
569 (void)fchdir(startdir
);
573 if (curp
.outdata
!= NULL
) {
574 switch (curp
.outtype
) {
576 html_free(curp
.outdata
);
581 ascii_free(curp
.outdata
);
585 pspdf_free(curp
.outdata
);
592 mparse_free(curp
.mp
);
596 if (search
.argmode
!= ARG_FILE
) {
598 mansearch_free(res
, sz
);
604 * When using a pager, finish writing both temporary files,
605 * fork it, wait for the user to close it, and clean up.
608 if (tag_files
!= NULL
) {
611 man_pgid
= getpgid(0);
612 tag_files
->tcpgid
= man_pgid
== getpid() ?
613 getpgid(getppid()) : man_pgid
;
618 /* Stop here until moved to the foreground. */
620 tc_pgid
= tcgetpgrp(tag_files
->ofd
);
621 if (tc_pgid
!= man_pgid
) {
622 if (tc_pgid
== pager_pid
) {
623 (void)tcsetpgrp(tag_files
->ofd
,
625 if (signum
== SIGTTIN
)
628 tag_files
->tcpgid
= tc_pgid
;
633 /* Once in the foreground, activate the pager. */
636 (void)tcsetpgrp(tag_files
->ofd
, pager_pid
);
637 kill(pager_pid
, SIGCONT
);
639 pager_pid
= spawn_pager(tag_files
);
641 /* Wait for the pager to stop or exit. */
643 while ((pid
= waitpid(pager_pid
, &status
,
644 WUNTRACED
)) == -1 && errno
== EINTR
)
649 rc
= MANDOCLEVEL_SYSERR
;
652 if (!WIFSTOPPED(status
))
655 signum
= WSTOPSIG(status
);
664 usage(enum argmode argmode
)
669 fputs("usage: mandoc [-ac] [-I os=name] "
670 "[-K encoding] [-mdoc | -man] [-O options]\n"
671 "\t [-T output] [-W level] [file ...]\n", stderr
);
674 fputs("usage: man [-acfhklw] [-C file] [-M path] "
675 "[-m path] [-S subsection]\n"
676 "\t [[-s] section] name ...\n", stderr
);
679 fputs("usage: whatis [-afk] [-C file] "
680 "[-M path] [-m path] [-O outkey] [-S arch]\n"
681 "\t [-s section] name ...\n", stderr
);
684 fputs("usage: apropos [-afk] [-C file] "
685 "[-M path] [-m path] [-O outkey] [-S arch]\n"
686 "\t [-s section] expression ...\n", stderr
);
689 exit((int)MANDOCLEVEL_BADARG
);
693 fs_lookup(const struct manpaths
*paths
, size_t ipath
,
694 const char *sec
, const char *arch
, const char *name
,
695 struct manpage
**res
, size_t *ressz
)
698 struct manpage
*page
;
704 mandoc_asprintf(&file
, "%s/man%s/%s.%s",
705 paths
->paths
[ipath
], sec
, name
, sec
);
706 if (access(file
, R_OK
) != -1)
710 mandoc_asprintf(&file
, "%s/cat%s/%s.0",
711 paths
->paths
[ipath
], sec
, name
);
712 if (access(file
, R_OK
) != -1) {
719 mandoc_asprintf(&file
, "%s/man%s/%s/%s.%s",
720 paths
->paths
[ipath
], sec
, arch
, name
, sec
);
721 if (access(file
, R_OK
) != -1)
726 mandoc_asprintf(&file
, "%s/man%s/%s.[01-9]*",
727 paths
->paths
[ipath
], sec
, name
);
728 globres
= glob(file
, 0, NULL
, &globinfo
);
729 if (globres
!= 0 && globres
!= GLOB_NOMATCH
)
730 warn("%s: glob", file
);
733 file
= mandoc_strdup(*globinfo
.gl_pathv
);
737 if (res
!= NULL
|| ipath
+ 1 != paths
->sz
)
740 mandoc_asprintf(&file
, "%s.%s", name
, sec
);
741 globres
= access(file
, R_OK
);
743 return globres
!= -1;
746 warnx("outdated mandoc.db lacks %s(%s) entry, run %s %s",
747 name
, sec
, BINM_MAKEWHATIS
, paths
->paths
[ipath
]);
752 *res
= mandoc_reallocarray(*res
, ++*ressz
, sizeof(struct manpage
));
753 page
= *res
+ (*ressz
- 1);
758 page
->bits
= NAME_FILE
& NAME_MASK
;
759 page
->sec
= (*sec
>= '1' && *sec
<= '9') ? *sec
- '1' + 1 : 10;
765 fs_search(const struct mansearch
*cfg
, const struct manpaths
*paths
,
766 int argc
, char **argv
, struct manpage
**res
, size_t *ressz
)
768 const char *const sections
[] =
769 {"1", "8", "6", "2", "3", "5", "7", "4", "9", "3p"};
770 const size_t nsec
= sizeof(sections
)/sizeof(sections
[0]);
772 size_t ipath
, isec
, lastsz
;
774 assert(cfg
->argmode
== ARG_NAME
);
780 for (ipath
= 0; ipath
< paths
->sz
; ipath
++) {
781 if (cfg
->sec
!= NULL
) {
782 if (fs_lookup(paths
, ipath
, cfg
->sec
,
783 cfg
->arch
, *argv
, res
, ressz
) &&
786 } else for (isec
= 0; isec
< nsec
; isec
++)
787 if (fs_lookup(paths
, ipath
, sections
[isec
],
788 cfg
->arch
, *argv
, res
, ressz
) &&
792 if (res
!= NULL
&& *ressz
== lastsz
&&
793 strchr(*argv
, '/') == NULL
)
794 warnx("No entry for %s in the manual.", *argv
);
803 parse(struct curparse
*curp
, int fd
, const char *file
)
805 enum mandoclevel rctmp
;
806 struct roff_man
*man
;
808 /* Begin by parsing the file itself. */
813 rctmp
= mparse_readfd(curp
->mp
, fd
, file
);
814 if (fd
!= STDIN_FILENO
)
820 * With -Wstop and warnings or errors of at least the requested
821 * level, do not produce output.
824 if (rctmp
!= MANDOCLEVEL_OK
&& curp
->wstop
)
827 if (curp
->outdata
== NULL
)
830 mparse_result(curp
->mp
, &man
, NULL
);
832 /* Execute the out device, if it exists. */
837 if (man
->macroset
== MACROSET_MDOC
) {
838 if (curp
->outtype
!= OUTT_TREE
|| !curp
->outopts
->noval
)
840 switch (curp
->outtype
) {
842 html_mdoc(curp
->outdata
, man
);
845 tree_mdoc(curp
->outdata
, man
);
848 man_mdoc(curp
->outdata
, man
);
855 terminal_mdoc(curp
->outdata
, man
);
858 markdown_mdoc(curp
->outdata
, man
);
864 if (man
->macroset
== MACROSET_MAN
) {
865 if (curp
->outtype
!= OUTT_TREE
|| !curp
->outopts
->noval
)
867 switch (curp
->outtype
) {
869 html_man(curp
->outdata
, man
);
872 tree_man(curp
->outdata
, man
);
875 man_man(curp
->outdata
, man
);
882 terminal_man(curp
->outdata
, man
);
888 if (curp
->mmin
< MANDOCERR_STYLE
)
890 mparse_updaterc(curp
->mp
, &rc
);
894 check_xr(const char *file
)
896 static struct manpaths paths
;
897 struct mansearch search
;
898 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_asprintf(&cp
, "Xr %s %s", xr
->name
, xr
->sec
);
920 mandoc_asprintf(&cp
, "Xr %s %s (%d times)",
921 xr
->name
, xr
->sec
, xr
->count
);
922 mmsg(MANDOCERR_XR_BAD
, MANDOCLEVEL_STYLE
,
923 file
, xr
->line
, xr
->pos
+ 1, cp
);
929 outdata_alloc(struct curparse
*curp
)
931 switch (curp
->outtype
) {
933 curp
->outdata
= html_alloc(curp
->outopts
);
936 curp
->outdata
= utf8_alloc(curp
->outopts
);
939 curp
->outdata
= locale_alloc(curp
->outopts
);
942 curp
->outdata
= ascii_alloc(curp
->outopts
);
945 curp
->outdata
= pdf_alloc(curp
->outopts
);
948 curp
->outdata
= ps_alloc(curp
->outopts
);
956 passthrough(const char *file
, int fd
, int synopsis_only
)
958 const char synb
[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
959 const char synr
[] = "SYNOPSIS";
965 ssize_t len
, written
;
971 if (fflush(stdout
) == EOF
) {
976 if ((stream
= fdopen(fd
, "r")) == NULL
) {
983 while ((len
= getline(&line
, &linesz
, stream
)) != -1) {
987 if ( ! isspace((unsigned char)*cp
))
989 while (isspace((unsigned char)*cp
)) {
994 if (strcmp(cp
, synb
) == 0 ||
995 strcmp(cp
, synr
) == 0)
1000 for (; len
> 0; len
-= written
) {
1001 if ((written
= write(STDOUT_FILENO
, cp
, len
)) != -1)
1009 if (ferror(stream
)) {
1011 syscall
= "getline";
1022 warn("%s: SYSERR: %s", file
, syscall
);
1023 if (rc
< MANDOCLEVEL_SYSERR
)
1024 rc
= MANDOCLEVEL_SYSERR
;
1028 koptions(int *options
, char *arg
)
1031 if ( ! strcmp(arg
, "utf-8")) {
1032 *options
|= MPARSE_UTF8
;
1033 *options
&= ~MPARSE_LATIN1
;
1034 } else if ( ! strcmp(arg
, "iso-8859-1")) {
1035 *options
|= MPARSE_LATIN1
;
1036 *options
&= ~MPARSE_UTF8
;
1037 } else if ( ! strcmp(arg
, "us-ascii")) {
1038 *options
&= ~(MPARSE_UTF8
| MPARSE_LATIN1
);
1040 warnx("-K %s: Bad argument", arg
);
1047 moptions(int *options
, char *arg
)
1052 if (strcmp(arg
, "doc") == 0)
1053 *options
|= MPARSE_MDOC
;
1054 else if (strcmp(arg
, "an") == 0)
1055 *options
|= MPARSE_MAN
;
1059 toptions(struct curparse
*curp
, char *arg
)
1062 if (0 == strcmp(arg
, "ascii"))
1063 curp
->outtype
= OUTT_ASCII
;
1064 else if (0 == strcmp(arg
, "lint")) {
1065 curp
->outtype
= OUTT_LINT
;
1066 curp
->mmin
= MANDOCERR_BASE
;
1067 mmsg_stream
= stdout
;
1068 } else if (0 == strcmp(arg
, "tree"))
1069 curp
->outtype
= OUTT_TREE
;
1070 else if (0 == strcmp(arg
, "man"))
1071 curp
->outtype
= OUTT_MAN
;
1072 else if (0 == strcmp(arg
, "html"))
1073 curp
->outtype
= OUTT_HTML
;
1074 else if (0 == strcmp(arg
, "markdown"))
1075 curp
->outtype
= OUTT_MARKDOWN
;
1076 else if (0 == strcmp(arg
, "utf8"))
1077 curp
->outtype
= OUTT_UTF8
;
1078 else if (0 == strcmp(arg
, "locale"))
1079 curp
->outtype
= OUTT_LOCALE
;
1080 else if (0 == strcmp(arg
, "ps"))
1081 curp
->outtype
= OUTT_PS
;
1082 else if (0 == strcmp(arg
, "pdf"))
1083 curp
->outtype
= OUTT_PDF
;
1085 warnx("-T %s: Bad argument", arg
);
1093 woptions(struct curparse
*curp
, char *arg
)
1096 const char *toks
[11];
1102 toks
[4] = "warning";
1106 toks
[8] = "openbsd";
1112 switch (getsubopt(&arg
, (char * const *)toks
, &v
)) {
1118 curp
->mmin
= MANDOCERR_BASE
;
1121 curp
->mmin
= MANDOCERR_STYLE
;
1124 curp
->mmin
= MANDOCERR_WARNING
;
1127 curp
->mmin
= MANDOCERR_ERROR
;
1130 curp
->mmin
= MANDOCERR_UNSUPP
;
1133 curp
->mmin
= MANDOCERR_MAX
;
1136 curp
->mmin
= MANDOCERR_BASE
;
1137 curp
->os_e
= MANDOC_OS_OPENBSD
;
1140 curp
->mmin
= MANDOCERR_BASE
;
1141 curp
->os_e
= MANDOC_OS_NETBSD
;
1144 warnx("-W %s: Bad argument", o
);
1152 mmsg(enum mandocerr t
, enum mandoclevel lvl
,
1153 const char *file
, int line
, int col
, const char *msg
)
1155 const char *mparse_msg
;
1157 fprintf(mmsg_stream
, "%s: %s:", getprogname(),
1158 file
== NULL
? "<stdin>" : file
);
1161 fprintf(mmsg_stream
, "%d:%d:", line
, col
+ 1);
1163 fprintf(mmsg_stream
, " %s", mparse_strlevel(lvl
));
1165 if ((mparse_msg
= mparse_strerror(t
)) != NULL
)
1166 fprintf(mmsg_stream
, ": %s", mparse_msg
);
1169 fprintf(mmsg_stream
, ": %s", msg
);
1171 fputc('\n', mmsg_stream
);
1175 spawn_pager(struct tag_files
*tag_files
)
1177 const struct timespec timeout
= { 0, 100000000 }; /* 0.1s */
1178 #define MAX_PAGER_ARGS 16
1179 char *argv
[MAX_PAGER_ARGS
];
1186 pager
= getenv("MANPAGER");
1187 if (pager
== NULL
|| *pager
== '\0')
1188 pager
= getenv("PAGER");
1189 if (pager
== NULL
|| *pager
== '\0')
1191 cp
= mandoc_strdup(pager
);
1194 * Parse the pager command into words.
1195 * Intentionally do not do anything fancy here.
1199 while (argc
+ 4 < MAX_PAGER_ARGS
) {
1201 cp
= strchr(cp
, ' ');
1211 /* For less(1), use the tag file. */
1213 if ((cmdlen
= strlen(argv
[0])) >= 4) {
1214 cp
= argv
[0] + cmdlen
- 4;
1215 if (strcmp(cp
, "less") == 0) {
1216 argv
[argc
++] = mandoc_strdup("-T");
1217 argv
[argc
++] = tag_files
->tfn
;
1220 argv
[argc
++] = tag_files
->ofn
;
1223 switch (pager_pid
= fork()) {
1225 err((int)MANDOCLEVEL_SYSERR
, "fork");
1229 (void)setpgid(pager_pid
, 0);
1230 (void)tcsetpgrp(tag_files
->ofd
, pager_pid
);
1232 if (pledge("stdio rpath tmppath tty proc", NULL
) == -1)
1233 err((int)MANDOCLEVEL_SYSERR
, "pledge");
1235 tag_files
->pager_pid
= pager_pid
;
1239 /* The child process becomes the pager. */
1241 if (dup2(tag_files
->ofd
, STDOUT_FILENO
) == -1)
1242 err((int)MANDOCLEVEL_SYSERR
, "pager stdout");
1243 close(tag_files
->ofd
);
1244 assert(tag_files
->tfd
== -1);
1246 /* Do not start the pager before controlling the terminal. */
1248 while (tcgetpgrp(STDOUT_FILENO
) != getpid())
1249 nanosleep(&timeout
, NULL
);
1251 execvp(argv
[0], argv
);
1252 err((int)MANDOCLEVEL_SYSERR
, "exec %s", argv
[0]);