]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
1 /* $Id: main.c,v 1.310 2018/11/22 12:01: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"
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
;
409 res
[sz
].form
= FORM_SRC
;
415 if (search
.argmode
!= ARG_NAME
)
416 warnx("nothing appropriate");
417 rc
= MANDOCLEVEL_BADARG
;
422 * For standard man(1) and -a output mode,
423 * prepare for copying filename pointers
424 * into the program parameter array.
427 if (outmode
== OUTMODE_ONE
) {
430 } else if (outmode
== OUTMODE_ALL
)
433 /* Iterate all matching manuals. */
436 for (i
= 0; i
< sz
; i
++) {
437 if (outmode
== OUTMODE_FLN
)
439 else if (outmode
== OUTMODE_LST
)
440 printf("%s - %s\n", res
[i
].names
,
441 res
[i
].output
== NULL
? "" :
443 else if (outmode
== OUTMODE_ONE
) {
444 /* Search for the best section. */
446 sec
+= strcspn(sec
, "123456789");
449 prio
= sec_prios
[sec
[0] - '1'];
452 if (prio
>= best_prio
)
460 * For man(1), -a and -i output mode, fall through
461 * to the main mandoc(1) code iterating files
462 * and running the parsers on each of them.
465 if (outmode
== OUTMODE_FLN
|| outmode
== OUTMODE_LST
)
473 if (pledge("stdio rpath tmppath tty proc exec", NULL
) == -1)
474 err((int)MANDOCLEVEL_SYSERR
, "pledge");
476 if (pledge("stdio rpath", NULL
) == -1)
477 err((int)MANDOCLEVEL_SYSERR
, "pledge");
481 if (search
.argmode
== ARG_FILE
)
482 moptions(&options
, auxpaths
);
485 curp
.mp
= mparse_alloc(options
, curp
.mmin
, mmsg
,
486 curp
.os_e
, curp
.os_s
);
490 tag_files
= tag_init();
491 parse(&curp
, STDIN_FILENO
, "<stdin>");
495 * Remember the original working directory, if possible.
496 * This will be needed if some names on the command line
497 * are page names and some are relative file names.
498 * Do not error out if the current directory is not
499 * readable: Maybe it won't be needed after all.
501 startdir
= open(".", O_RDONLY
| O_DIRECTORY
);
506 * Changing directories is not needed in ARG_FILE mode.
507 * Do it on a best-effort basis. Even in case of
508 * failure, some functionality may still work.
511 if (resp
->ipath
!= SIZE_MAX
)
512 (void)chdir(conf
.manpath
.paths
[resp
->ipath
]);
513 else if (startdir
!= -1)
514 (void)fchdir(startdir
);
517 fd
= mparse_open(curp
.mp
, resp
!= NULL
? resp
->file
: *argv
);
521 tag_files
= tag_init();
522 if (conf
.output
.tag
!= NULL
&&
523 tag_files
->tagname
== NULL
)
525 *conf
.output
.tag
!= '\0' ?
526 conf
.output
.tag
: *argv
;
530 parse(&curp
, fd
, *argv
);
531 else if (resp
->form
== FORM_SRC
)
532 parse(&curp
, fd
, resp
->file
);
534 passthrough(resp
->file
, fd
,
535 conf
.output
.synopsisonly
);
537 if (ferror(stdout
)) {
538 if (tag_files
!= NULL
) {
539 warn("%s", tag_files
->ofn
);
544 rc
= MANDOCLEVEL_SYSERR
;
548 if (argc
> 1 && curp
.outtype
<= OUTT_UTF8
) {
549 if (curp
.outdata
== NULL
)
550 outdata_alloc(&curp
);
551 terminal_sepline(curp
.outdata
);
553 } else if (rc
< MANDOCLEVEL_ERROR
)
554 rc
= MANDOCLEVEL_ERROR
;
556 if (MANDOCLEVEL_OK
!= rc
&& curp
.wstop
)
564 mparse_reset(curp
.mp
);
566 if (startdir
!= -1) {
567 (void)fchdir(startdir
);
571 if (curp
.outdata
!= NULL
) {
572 switch (curp
.outtype
) {
574 html_free(curp
.outdata
);
579 ascii_free(curp
.outdata
);
583 pspdf_free(curp
.outdata
);
590 mparse_free(curp
.mp
);
594 if (search
.argmode
!= ARG_FILE
) {
596 mansearch_free(res
, sz
);
602 * When using a pager, finish writing both temporary files,
603 * fork it, wait for the user to close it, and clean up.
606 if (tag_files
!= NULL
) {
609 man_pgid
= getpgid(0);
610 tag_files
->tcpgid
= man_pgid
== getpid() ?
611 getpgid(getppid()) : man_pgid
;
616 /* Stop here until moved to the foreground. */
618 tc_pgid
= tcgetpgrp(tag_files
->ofd
);
619 if (tc_pgid
!= man_pgid
) {
620 if (tc_pgid
== pager_pid
) {
621 (void)tcsetpgrp(tag_files
->ofd
,
623 if (signum
== SIGTTIN
)
626 tag_files
->tcpgid
= tc_pgid
;
631 /* Once in the foreground, activate the pager. */
634 (void)tcsetpgrp(tag_files
->ofd
, pager_pid
);
635 kill(pager_pid
, SIGCONT
);
637 pager_pid
= spawn_pager(tag_files
);
639 /* Wait for the pager to stop or exit. */
641 while ((pid
= waitpid(pager_pid
, &status
,
642 WUNTRACED
)) == -1 && errno
== EINTR
)
647 rc
= MANDOCLEVEL_SYSERR
;
650 if (!WIFSTOPPED(status
))
653 signum
= WSTOPSIG(status
);
662 usage(enum argmode argmode
)
667 fputs("usage: mandoc [-ac] [-I os=name] "
668 "[-K encoding] [-mdoc | -man] [-O options]\n"
669 "\t [-T output] [-W level] [file ...]\n", stderr
);
672 fputs("usage: man [-acfhklw] [-C file] [-M path] "
673 "[-m path] [-S subsection]\n"
674 "\t [[-s] section] name ...\n", stderr
);
677 fputs("usage: whatis [-afk] [-C file] "
678 "[-M path] [-m path] [-O outkey] [-S arch]\n"
679 "\t [-s section] name ...\n", stderr
);
682 fputs("usage: apropos [-afk] [-C file] "
683 "[-M path] [-m path] [-O outkey] [-S arch]\n"
684 "\t [-s section] expression ...\n", stderr
);
687 exit((int)MANDOCLEVEL_BADARG
);
691 fs_lookup(const struct manpaths
*paths
, size_t ipath
,
692 const char *sec
, const char *arch
, const char *name
,
693 struct manpage
**res
, size_t *ressz
)
696 struct manpage
*page
;
702 mandoc_asprintf(&file
, "%s/man%s/%s.%s",
703 paths
->paths
[ipath
], sec
, name
, sec
);
704 if (access(file
, R_OK
) != -1)
708 mandoc_asprintf(&file
, "%s/cat%s/%s.0",
709 paths
->paths
[ipath
], sec
, name
);
710 if (access(file
, R_OK
) != -1) {
717 mandoc_asprintf(&file
, "%s/man%s/%s/%s.%s",
718 paths
->paths
[ipath
], sec
, arch
, name
, sec
);
719 if (access(file
, R_OK
) != -1)
724 mandoc_asprintf(&file
, "%s/man%s/%s.[01-9]*",
725 paths
->paths
[ipath
], sec
, name
);
726 globres
= glob(file
, 0, NULL
, &globinfo
);
727 if (globres
!= 0 && globres
!= GLOB_NOMATCH
)
728 warn("%s: glob", file
);
731 file
= mandoc_strdup(*globinfo
.gl_pathv
);
735 if (res
!= NULL
|| ipath
+ 1 != paths
->sz
)
738 mandoc_asprintf(&file
, "%s.%s", name
, sec
);
739 globres
= access(file
, R_OK
);
741 return globres
!= -1;
744 warnx("outdated mandoc.db lacks %s(%s) entry, run %s %s",
745 name
, sec
, BINM_MAKEWHATIS
, paths
->paths
[ipath
]);
750 *res
= mandoc_reallocarray(*res
, ++*ressz
, sizeof(struct manpage
));
751 page
= *res
+ (*ressz
- 1);
756 page
->sec
= (*sec
>= '1' && *sec
<= '9') ? *sec
- '1' + 1 : 10;
762 fs_search(const struct mansearch
*cfg
, const struct manpaths
*paths
,
763 int argc
, char **argv
, struct manpage
**res
, size_t *ressz
)
765 const char *const sections
[] =
766 {"1", "8", "6", "2", "3", "5", "7", "4", "9", "3p"};
767 const size_t nsec
= sizeof(sections
)/sizeof(sections
[0]);
769 size_t ipath
, isec
, lastsz
;
771 assert(cfg
->argmode
== ARG_NAME
);
777 for (ipath
= 0; ipath
< paths
->sz
; ipath
++) {
778 if (cfg
->sec
!= NULL
) {
779 if (fs_lookup(paths
, ipath
, cfg
->sec
,
780 cfg
->arch
, *argv
, res
, ressz
) &&
783 } else for (isec
= 0; isec
< nsec
; isec
++)
784 if (fs_lookup(paths
, ipath
, sections
[isec
],
785 cfg
->arch
, *argv
, res
, ressz
) &&
789 if (res
!= NULL
&& *ressz
== lastsz
&&
790 strchr(*argv
, '/') == NULL
) {
791 if (cfg
->sec
== NULL
)
792 warnx("No entry for %s in the manual.",
795 warnx("No entry for %s in section %s "
796 "of the manual.", *argv
, cfg
->sec
);
806 parse(struct curparse
*curp
, int fd
, const char *file
)
808 enum mandoclevel rctmp
;
809 struct roff_man
*man
;
811 /* Begin by parsing the file itself. */
816 rctmp
= mparse_readfd(curp
->mp
, fd
, file
);
817 if (fd
!= STDIN_FILENO
)
823 * With -Wstop and warnings or errors of at least the requested
824 * level, do not produce output.
827 if (rctmp
!= MANDOCLEVEL_OK
&& curp
->wstop
)
830 if (curp
->outdata
== NULL
)
833 mparse_result(curp
->mp
, &man
, NULL
);
835 /* Execute the out device, if it exists. */
840 if (man
->macroset
== MACROSET_MDOC
) {
841 if (curp
->outtype
!= OUTT_TREE
|| !curp
->outopts
->noval
)
843 switch (curp
->outtype
) {
845 html_mdoc(curp
->outdata
, man
);
848 tree_mdoc(curp
->outdata
, man
);
851 man_mdoc(curp
->outdata
, man
);
858 terminal_mdoc(curp
->outdata
, man
);
861 markdown_mdoc(curp
->outdata
, man
);
867 if (man
->macroset
== MACROSET_MAN
) {
868 if (curp
->outtype
!= OUTT_TREE
|| !curp
->outopts
->noval
)
870 switch (curp
->outtype
) {
872 html_man(curp
->outdata
, man
);
875 tree_man(curp
->outdata
, man
);
878 mparse_copy(curp
->mp
);
885 terminal_man(curp
->outdata
, man
);
891 if (curp
->mmin
< MANDOCERR_STYLE
)
893 mparse_updaterc(curp
->mp
, &rc
);
897 check_xr(const char *file
)
899 static struct manpaths paths
;
900 struct mansearch search
;
901 struct mandoc_xr
*xr
;
906 manpath_base(&paths
);
908 for (xr
= mandoc_xr_get(); xr
!= NULL
; xr
= xr
->next
) {
912 search
.sec
= xr
->sec
;
913 search
.outkey
= NULL
;
914 search
.argmode
= ARG_NAME
;
915 search
.firstmatch
= 1;
916 if (mansearch(&search
, &paths
, 1, &xr
->name
, NULL
, &sz
))
918 if (fs_search(&search
, &paths
, 1, &xr
->name
, NULL
, &sz
))
921 mandoc_asprintf(&cp
, "Xr %s %s", xr
->name
, xr
->sec
);
923 mandoc_asprintf(&cp
, "Xr %s %s (%d times)",
924 xr
->name
, xr
->sec
, xr
->count
);
925 mmsg(MANDOCERR_XR_BAD
, MANDOCLEVEL_STYLE
,
926 file
, xr
->line
, xr
->pos
+ 1, cp
);
932 outdata_alloc(struct curparse
*curp
)
934 switch (curp
->outtype
) {
936 curp
->outdata
= html_alloc(curp
->outopts
);
939 curp
->outdata
= utf8_alloc(curp
->outopts
);
942 curp
->outdata
= locale_alloc(curp
->outopts
);
945 curp
->outdata
= ascii_alloc(curp
->outopts
);
948 curp
->outdata
= pdf_alloc(curp
->outopts
);
951 curp
->outdata
= ps_alloc(curp
->outopts
);
959 passthrough(const char *file
, int fd
, int synopsis_only
)
961 const char synb
[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
962 const char synr
[] = "SYNOPSIS";
968 ssize_t len
, written
;
974 if (fflush(stdout
) == EOF
) {
979 if ((stream
= fdopen(fd
, "r")) == NULL
) {
986 while ((len
= getline(&line
, &linesz
, stream
)) != -1) {
990 if ( ! isspace((unsigned char)*cp
))
992 while (isspace((unsigned char)*cp
)) {
997 if (strcmp(cp
, synb
) == 0 ||
998 strcmp(cp
, synr
) == 0)
1003 for (; len
> 0; len
-= written
) {
1004 if ((written
= write(STDOUT_FILENO
, cp
, len
)) != -1)
1012 if (ferror(stream
)) {
1014 syscall
= "getline";
1025 warn("%s: SYSERR: %s", file
, syscall
);
1026 if (rc
< MANDOCLEVEL_SYSERR
)
1027 rc
= MANDOCLEVEL_SYSERR
;
1031 koptions(int *options
, char *arg
)
1034 if ( ! strcmp(arg
, "utf-8")) {
1035 *options
|= MPARSE_UTF8
;
1036 *options
&= ~MPARSE_LATIN1
;
1037 } else if ( ! strcmp(arg
, "iso-8859-1")) {
1038 *options
|= MPARSE_LATIN1
;
1039 *options
&= ~MPARSE_UTF8
;
1040 } else if ( ! strcmp(arg
, "us-ascii")) {
1041 *options
&= ~(MPARSE_UTF8
| MPARSE_LATIN1
);
1043 warnx("-K %s: Bad argument", arg
);
1050 moptions(int *options
, char *arg
)
1055 if (strcmp(arg
, "doc") == 0)
1056 *options
|= MPARSE_MDOC
;
1057 else if (strcmp(arg
, "an") == 0)
1058 *options
|= MPARSE_MAN
;
1062 toptions(struct curparse
*curp
, char *arg
)
1065 if (0 == strcmp(arg
, "ascii"))
1066 curp
->outtype
= OUTT_ASCII
;
1067 else if (0 == strcmp(arg
, "lint")) {
1068 curp
->outtype
= OUTT_LINT
;
1069 curp
->mmin
= MANDOCERR_BASE
;
1070 mmsg_stream
= stdout
;
1071 } else if (0 == strcmp(arg
, "tree"))
1072 curp
->outtype
= OUTT_TREE
;
1073 else if (0 == strcmp(arg
, "man"))
1074 curp
->outtype
= OUTT_MAN
;
1075 else if (0 == strcmp(arg
, "html"))
1076 curp
->outtype
= OUTT_HTML
;
1077 else if (0 == strcmp(arg
, "markdown"))
1078 curp
->outtype
= OUTT_MARKDOWN
;
1079 else if (0 == strcmp(arg
, "utf8"))
1080 curp
->outtype
= OUTT_UTF8
;
1081 else if (0 == strcmp(arg
, "locale"))
1082 curp
->outtype
= OUTT_LOCALE
;
1083 else if (0 == strcmp(arg
, "ps"))
1084 curp
->outtype
= OUTT_PS
;
1085 else if (0 == strcmp(arg
, "pdf"))
1086 curp
->outtype
= OUTT_PDF
;
1088 warnx("-T %s: Bad argument", arg
);
1096 woptions(struct curparse
*curp
, char *arg
)
1099 const char *toks
[11];
1105 toks
[4] = "warning";
1109 toks
[8] = "openbsd";
1115 switch (getsubopt(&arg
, (char * const *)toks
, &v
)) {
1121 curp
->mmin
= MANDOCERR_BASE
;
1124 curp
->mmin
= MANDOCERR_STYLE
;
1127 curp
->mmin
= MANDOCERR_WARNING
;
1130 curp
->mmin
= MANDOCERR_ERROR
;
1133 curp
->mmin
= MANDOCERR_UNSUPP
;
1136 curp
->mmin
= MANDOCERR_MAX
;
1139 curp
->mmin
= MANDOCERR_BASE
;
1140 curp
->os_e
= MANDOC_OS_OPENBSD
;
1143 curp
->mmin
= MANDOCERR_BASE
;
1144 curp
->os_e
= MANDOC_OS_NETBSD
;
1147 warnx("-W %s: Bad argument", o
);
1155 mmsg(enum mandocerr t
, enum mandoclevel lvl
,
1156 const char *file
, int line
, int col
, const char *msg
)
1158 const char *mparse_msg
;
1160 fprintf(mmsg_stream
, "%s: %s:", getprogname(),
1161 file
== NULL
? "<stdin>" : file
);
1164 fprintf(mmsg_stream
, "%d:%d:", line
, col
+ 1);
1166 fprintf(mmsg_stream
, " %s", mparse_strlevel(lvl
));
1168 if ((mparse_msg
= mparse_strerror(t
)) != NULL
)
1169 fprintf(mmsg_stream
, ": %s", mparse_msg
);
1172 fprintf(mmsg_stream
, ": %s", msg
);
1174 fputc('\n', mmsg_stream
);
1178 spawn_pager(struct tag_files
*tag_files
)
1180 const struct timespec timeout
= { 0, 100000000 }; /* 0.1s */
1181 #define MAX_PAGER_ARGS 16
1182 char *argv
[MAX_PAGER_ARGS
];
1189 pager
= getenv("MANPAGER");
1190 if (pager
== NULL
|| *pager
== '\0')
1191 pager
= getenv("PAGER");
1192 if (pager
== NULL
|| *pager
== '\0')
1194 cp
= mandoc_strdup(pager
);
1197 * Parse the pager command into words.
1198 * Intentionally do not do anything fancy here.
1202 while (argc
+ 5 < MAX_PAGER_ARGS
) {
1204 cp
= strchr(cp
, ' ');
1214 /* For less(1), use the tag file. */
1217 if ((cmdlen
= strlen(argv
[0])) >= 4) {
1218 cp
= argv
[0] + cmdlen
- 4;
1219 if (strcmp(cp
, "less") == 0) {
1220 argv
[argc
++] = mandoc_strdup("-T");
1221 argv
[argc
++] = tag_files
->tfn
;
1222 if (tag_files
->tagname
!= NULL
) {
1223 argv
[argc
++] = mandoc_strdup("-t");
1224 argv
[argc
++] = tag_files
->tagname
;
1230 argv
[argc
++] = tag_files
->ofn
;
1233 switch (pager_pid
= fork()) {
1235 err((int)MANDOCLEVEL_SYSERR
, "fork");
1239 (void)setpgid(pager_pid
, 0);
1240 (void)tcsetpgrp(tag_files
->ofd
, pager_pid
);
1242 if (pledge("stdio rpath tmppath tty proc", NULL
) == -1)
1243 err((int)MANDOCLEVEL_SYSERR
, "pledge");
1245 tag_files
->pager_pid
= pager_pid
;
1249 /* The child process becomes the pager. */
1251 if (dup2(tag_files
->ofd
, STDOUT_FILENO
) == -1)
1252 err((int)MANDOCLEVEL_SYSERR
, "pager stdout");
1253 close(tag_files
->ofd
);
1254 assert(tag_files
->tfd
== -1);
1256 /* Do not start the pager before controlling the terminal. */
1258 while (tcgetpgrp(STDOUT_FILENO
) != getpid())
1259 nanosleep(&timeout
, NULL
);
1261 execvp(argv
[0], argv
);
1262 err((int)MANDOCLEVEL_SYSERR
, "exec %s", argv
[0]);