]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
1 /* $Id: main.c,v 1.300 2017/07/20 15:26:41 schwarze Exp $ */
3 * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010-2012, 2014-2017 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/param.h> /* MACHINE */
44 #include "mandoc_aux.h"
46 #include "mandoc_xr.h"
53 #include "mansearch.h"
64 OUTT_ASCII
= 0, /* -Tascii */
65 OUTT_LOCALE
, /* -Tlocale */
66 OUTT_UTF8
, /* -Tutf8 */
67 OUTT_TREE
, /* -Ttree */
69 OUTT_HTML
, /* -Thtml */
70 OUTT_MARKDOWN
, /* -Tmarkdown */
71 OUTT_LINT
, /* -Tlint */
78 struct manoutput
*outopts
; /* output options */
79 void *outdata
; /* data for output */
80 char *os_s
; /* operating system for display */
81 int wstop
; /* stop after a file with a warning */
82 enum mandocerr mmin
; /* ignore messages below this */
83 enum mandoc_os os_e
; /* check base system conventions */
84 enum outt outtype
; /* which output to use */
88 int mandocdb(int, char *[]);
90 static void check_xr(const char *);
91 static int fs_lookup(const struct manpaths
*,
92 size_t ipath
, const char *,
93 const char *, const char *,
94 struct manpage
**, size_t *);
95 static int fs_search(const struct mansearch
*,
96 const struct manpaths
*, int, char**,
97 struct manpage
**, size_t *);
98 static int koptions(int *, char *);
99 static void moptions(int *, char *);
100 static void mmsg(enum mandocerr
, enum mandoclevel
,
101 const char *, int, int, const 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
};
113 static enum mandoclevel rc
;
114 static FILE *mmsg_stream
;
118 main(int argc
, char *argv
[])
121 struct mansearch search
;
122 struct curparse curp
;
123 struct tag_files
*tag_files
;
124 struct manpage
*res
, *resp
;
125 const char *progname
, *sec
, *thisarg
;
126 char *conf_file
, *defpaths
, *auxpaths
;
131 enum outmode outmode
;
138 pid_t pager_pid
, tc_pgid
, man_pgid
, pid
;
141 progname
= getprogname();
144 progname
= mandoc_strdup("mandoc");
145 else if ((progname
= strrchr(argv
[0], '/')) == NULL
)
149 setprogname(progname
);
152 if (strncmp(progname
, "mandocdb", 8) == 0 ||
153 strcmp(progname
, BINM_MAKEWHATIS
) == 0)
154 return mandocdb(argc
, argv
);
157 if (pledge("stdio rpath tmppath tty proc exec", NULL
) == -1)
158 err((int)MANDOCLEVEL_SYSERR
, "pledge");
161 #if HAVE_SANDBOX_INIT
162 if (sandbox_init(kSBXProfileNoInternet
, SANDBOX_NAMED
, NULL
) == -1)
163 errx((int)MANDOCLEVEL_SYSERR
, "sandbox_init");
166 /* Search options. */
168 memset(&conf
, 0, sizeof(conf
));
169 conf_file
= defpaths
= NULL
;
172 memset(&search
, 0, sizeof(struct mansearch
));
173 search
.outkey
= "Nd";
176 if (strcmp(progname
, BINM_MAN
) == 0)
177 search
.argmode
= ARG_NAME
;
178 else if (strcmp(progname
, BINM_APROPOS
) == 0)
179 search
.argmode
= ARG_EXPR
;
180 else if (strcmp(progname
, BINM_WHATIS
) == 0)
181 search
.argmode
= ARG_WORD
;
182 else if (strncmp(progname
, "help", 4) == 0)
183 search
.argmode
= ARG_NAME
;
185 search
.argmode
= ARG_FILE
;
187 /* Parser and formatter options. */
189 memset(&curp
, 0, sizeof(struct curparse
));
190 curp
.outtype
= OUTT_LOCALE
;
191 curp
.mmin
= MANDOCERR_MAX
;
192 curp
.outopts
= &conf
.output
;
193 options
= MPARSE_SO
| MPARSE_UTF8
| MPARSE_LATIN1
;
194 mmsg_stream
= stderr
;
199 outmode
= OUTMODE_DEF
;
201 while ((c
= getopt(argc
, argv
,
202 "aC:cfhI:iK:klM:m:O:S:s:T:VW:w")) != -1) {
203 if (c
== 'i' && search
.argmode
== ARG_EXPR
) {
209 outmode
= OUTMODE_ALL
;
218 search
.argmode
= ARG_WORD
;
221 conf
.output
.synopsisonly
= 1;
223 outmode
= OUTMODE_ALL
;
226 if (strncmp(optarg
, "os=", 3)) {
227 warnx("-I %s: Bad argument", optarg
);
228 return (int)MANDOCLEVEL_BADARG
;
230 if (curp
.os_s
!= NULL
) {
231 warnx("-I %s: Duplicate argument", optarg
);
232 return (int)MANDOCLEVEL_BADARG
;
234 curp
.os_s
= mandoc_strdup(optarg
+ 3);
237 if ( ! koptions(&options
, optarg
))
238 return (int)MANDOCLEVEL_BADARG
;
241 search
.argmode
= ARG_EXPR
;
244 search
.argmode
= ARG_FILE
;
245 outmode
= OUTMODE_ALL
;
257 search
.arch
= optarg
;
263 if ( ! toptions(&curp
, optarg
))
264 return (int)MANDOCLEVEL_BADARG
;
267 if ( ! woptions(&curp
, optarg
))
268 return (int)MANDOCLEVEL_BADARG
;
271 outmode
= OUTMODE_FLN
;
280 usage(search
.argmode
);
282 /* Postprocess options. */
284 if (outmode
== OUTMODE_DEF
) {
285 switch (search
.argmode
) {
287 outmode
= OUTMODE_ALL
;
291 outmode
= OUTMODE_ONE
;
294 outmode
= OUTMODE_LST
;
300 if (outmode
== OUTMODE_LST
)
301 search
.outkey
= oarg
;
303 while (oarg
!= NULL
) {
305 if (manconf_output(&conf
.output
,
306 strsep(&oarg
, ","), 0) == 0)
308 warnx("-O %s: Bad argument", thisarg
);
309 return (int)MANDOCLEVEL_BADARG
;
314 if (outmode
== OUTMODE_FLN
||
315 outmode
== OUTMODE_LST
||
316 !isatty(STDOUT_FILENO
))
321 if (pledge("stdio rpath", NULL
) == -1)
322 err((int)MANDOCLEVEL_SYSERR
, "pledge");
325 /* Parse arguments. */
335 * and for a man(1) section argument without -s.
338 if (search
.argmode
== ARG_NAME
) {
339 if (*progname
== 'h') {
344 } else if (argc
> 1 &&
345 ((uc
= (unsigned char *)argv
[0]) != NULL
) &&
346 ((isdigit(uc
[0]) && (uc
[1] == '\0' ||
347 (isalpha(uc
[1]) && uc
[2] == '\0'))) ||
348 (uc
[0] == 'n' && uc
[1] == '\0'))) {
349 search
.sec
= (char *)uc
;
353 if (search
.arch
== NULL
)
354 search
.arch
= getenv("MACHINE");
356 if (search
.arch
== NULL
)
357 search
.arch
= MACHINE
;
363 /* man(1), whatis(1), apropos(1) */
365 if (search
.argmode
!= ARG_FILE
) {
366 if (search
.argmode
== ARG_NAME
&&
367 outmode
== OUTMODE_ONE
)
368 search
.firstmatch
= 1;
370 /* Access the mandoc database. */
372 manconf_parse(&conf
, conf_file
, defpaths
, auxpaths
);
373 if ( ! mansearch(&search
, &conf
.manpath
,
374 argc
, argv
, &res
, &sz
))
375 usage(search
.argmode
);
378 if (search
.argmode
== ARG_NAME
)
379 fs_search(&search
, &conf
.manpath
,
380 argc
, argv
, &res
, &sz
);
382 warnx("nothing appropriate");
386 rc
= MANDOCLEVEL_BADARG
;
391 * For standard man(1) and -a output mode,
392 * prepare for copying filename pointers
393 * into the program parameter array.
396 if (outmode
== OUTMODE_ONE
) {
399 } else if (outmode
== OUTMODE_ALL
)
402 /* Iterate all matching manuals. */
405 for (i
= 0; i
< sz
; i
++) {
406 if (outmode
== OUTMODE_FLN
)
408 else if (outmode
== OUTMODE_LST
)
409 printf("%s - %s\n", res
[i
].names
,
410 res
[i
].output
== NULL
? "" :
412 else if (outmode
== OUTMODE_ONE
) {
413 /* Search for the best section. */
415 sec
+= strcspn(sec
, "123456789");
418 prio
= sec_prios
[sec
[0] - '1'];
421 if (prio
>= best_prio
)
429 * For man(1), -a and -i output mode, fall through
430 * to the main mandoc(1) code iterating files
431 * and running the parsers on each of them.
434 if (outmode
== OUTMODE_FLN
|| outmode
== OUTMODE_LST
)
442 if (pledge("stdio rpath tmppath tty proc exec", NULL
) == -1)
443 err((int)MANDOCLEVEL_SYSERR
, "pledge");
445 if (pledge("stdio rpath", NULL
) == -1)
446 err((int)MANDOCLEVEL_SYSERR
, "pledge");
450 if (search
.argmode
== ARG_FILE
)
451 moptions(&options
, auxpaths
);
454 curp
.mp
= mparse_alloc(options
, curp
.mmin
, mmsg
,
455 curp
.os_e
, curp
.os_s
);
458 * Conditionally start up the lookaside buffer before parsing.
460 if (OUTT_MAN
== curp
.outtype
)
461 mparse_keep(curp
.mp
);
465 tag_files
= tag_init();
466 parse(&curp
, STDIN_FILENO
, "<stdin>");
470 fd
= mparse_open(curp
.mp
, resp
!= NULL
? resp
->file
: *argv
);
473 tag_files
= tag_init();
478 parse(&curp
, fd
, *argv
);
479 else if (resp
->form
== FORM_SRC
) {
480 /* For .so only; ignore failure. */
481 chdir(conf
.manpath
.paths
[resp
->ipath
]);
482 parse(&curp
, fd
, resp
->file
);
484 passthrough(resp
->file
, fd
,
485 conf
.output
.synopsisonly
);
487 if (argc
> 1 && curp
.outtype
<= OUTT_UTF8
) {
488 if (curp
.outdata
== NULL
)
489 outdata_alloc(&curp
);
490 terminal_sepline(curp
.outdata
);
492 } else if (rc
< MANDOCLEVEL_ERROR
)
493 rc
= MANDOCLEVEL_ERROR
;
495 if (MANDOCLEVEL_OK
!= rc
&& curp
.wstop
)
503 mparse_reset(curp
.mp
);
506 if (curp
.outdata
!= NULL
) {
507 switch (curp
.outtype
) {
509 html_free(curp
.outdata
);
514 ascii_free(curp
.outdata
);
518 pspdf_free(curp
.outdata
);
525 mparse_free(curp
.mp
);
529 if (search
.argmode
!= ARG_FILE
) {
531 mansearch_free(res
, sz
);
537 * When using a pager, finish writing both temporary files,
538 * fork it, wait for the user to close it, and clean up.
541 if (tag_files
!= NULL
) {
544 man_pgid
= getpgid(0);
545 tag_files
->tcpgid
= man_pgid
== getpid() ?
546 getpgid(getppid()) : man_pgid
;
551 /* Stop here until moved to the foreground. */
553 tc_pgid
= tcgetpgrp(tag_files
->ofd
);
554 if (tc_pgid
!= man_pgid
) {
555 if (tc_pgid
== pager_pid
) {
556 (void)tcsetpgrp(tag_files
->ofd
,
558 if (signum
== SIGTTIN
)
561 tag_files
->tcpgid
= tc_pgid
;
566 /* Once in the foreground, activate the pager. */
569 (void)tcsetpgrp(tag_files
->ofd
, pager_pid
);
570 kill(pager_pid
, SIGCONT
);
572 pager_pid
= spawn_pager(tag_files
);
574 /* Wait for the pager to stop or exit. */
576 while ((pid
= waitpid(pager_pid
, &status
,
577 WUNTRACED
)) == -1 && errno
== EINTR
)
582 rc
= MANDOCLEVEL_SYSERR
;
585 if (!WIFSTOPPED(status
))
588 signum
= WSTOPSIG(status
);
597 usage(enum argmode argmode
)
602 fputs("usage: mandoc [-ac] [-I os=name] "
603 "[-K encoding] [-mdoc | -man] [-O options]\n"
604 "\t [-T output] [-W level] [file ...]\n", stderr
);
607 fputs("usage: man [-acfhklw] [-C file] [-M path] "
608 "[-m path] [-S subsection]\n"
609 "\t [[-s] section] name ...\n", stderr
);
612 fputs("usage: whatis [-afk] [-C file] "
613 "[-M path] [-m path] [-O outkey] [-S arch]\n"
614 "\t [-s section] name ...\n", stderr
);
617 fputs("usage: apropos [-afk] [-C file] "
618 "[-M path] [-m path] [-O outkey] [-S arch]\n"
619 "\t [-s section] expression ...\n", stderr
);
622 exit((int)MANDOCLEVEL_BADARG
);
626 fs_lookup(const struct manpaths
*paths
, size_t ipath
,
627 const char *sec
, const char *arch
, const char *name
,
628 struct manpage
**res
, size_t *ressz
)
631 struct manpage
*page
;
637 mandoc_asprintf(&file
, "%s/man%s/%s.%s",
638 paths
->paths
[ipath
], sec
, name
, sec
);
639 if (access(file
, R_OK
) != -1)
643 mandoc_asprintf(&file
, "%s/cat%s/%s.0",
644 paths
->paths
[ipath
], sec
, name
);
645 if (access(file
, R_OK
) != -1) {
652 mandoc_asprintf(&file
, "%s/man%s/%s/%s.%s",
653 paths
->paths
[ipath
], sec
, arch
, name
, sec
);
654 if (access(file
, R_OK
) != -1)
659 mandoc_asprintf(&file
, "%s/man%s/%s.[01-9]*",
660 paths
->paths
[ipath
], sec
, name
);
661 globres
= glob(file
, 0, NULL
, &globinfo
);
662 if (globres
!= 0 && globres
!= GLOB_NOMATCH
)
663 warn("%s: glob", file
);
666 file
= mandoc_strdup(*globinfo
.gl_pathv
);
670 if (res
!= NULL
|| ipath
+ 1 != paths
->sz
)
673 mandoc_asprintf(&file
, "%s.%s", name
, sec
);
674 globres
= access(file
, R_OK
);
676 return globres
!= -1;
679 warnx("outdated mandoc.db lacks %s(%s) entry, run %s %s",
680 name
, sec
, BINM_MAKEWHATIS
, paths
->paths
[ipath
]);
685 *res
= mandoc_reallocarray(*res
, ++*ressz
, sizeof(struct manpage
));
686 page
= *res
+ (*ressz
- 1);
691 page
->bits
= NAME_FILE
& NAME_MASK
;
692 page
->sec
= (*sec
>= '1' && *sec
<= '9') ? *sec
- '1' + 1 : 10;
698 fs_search(const struct mansearch
*cfg
, const struct manpaths
*paths
,
699 int argc
, char **argv
, struct manpage
**res
, size_t *ressz
)
701 const char *const sections
[] =
702 {"1", "8", "6", "2", "3", "5", "7", "4", "9", "3p"};
703 const size_t nsec
= sizeof(sections
)/sizeof(sections
[0]);
705 size_t ipath
, isec
, lastsz
;
707 assert(cfg
->argmode
== ARG_NAME
);
713 for (ipath
= 0; ipath
< paths
->sz
; ipath
++) {
714 if (cfg
->sec
!= NULL
) {
715 if (fs_lookup(paths
, ipath
, cfg
->sec
,
716 cfg
->arch
, *argv
, res
, ressz
) &&
719 } else for (isec
= 0; isec
< nsec
; isec
++)
720 if (fs_lookup(paths
, ipath
, sections
[isec
],
721 cfg
->arch
, *argv
, res
, ressz
) &&
725 if (res
!= NULL
&& *ressz
== lastsz
)
726 warnx("No entry for %s in the manual.", *argv
);
735 parse(struct curparse
*curp
, int fd
, const char *file
)
737 enum mandoclevel rctmp
;
738 struct roff_man
*man
;
740 /* Begin by parsing the file itself. */
745 rctmp
= mparse_readfd(curp
->mp
, fd
, file
);
746 if (fd
!= STDIN_FILENO
)
752 * With -Wstop and warnings or errors of at least the requested
753 * level, do not produce output.
756 if (rctmp
!= MANDOCLEVEL_OK
&& curp
->wstop
)
759 if (curp
->outdata
== NULL
)
762 mparse_result(curp
->mp
, &man
, NULL
);
764 /* Execute the out device, if it exists. */
769 if (man
->macroset
== MACROSET_MDOC
) {
770 if (curp
->outtype
!= OUTT_TREE
|| !curp
->outopts
->noval
)
772 switch (curp
->outtype
) {
774 html_mdoc(curp
->outdata
, man
);
777 tree_mdoc(curp
->outdata
, man
);
780 man_mdoc(curp
->outdata
, man
);
787 terminal_mdoc(curp
->outdata
, man
);
790 markdown_mdoc(curp
->outdata
, man
);
796 if (man
->macroset
== MACROSET_MAN
) {
797 if (curp
->outtype
!= OUTT_TREE
|| !curp
->outopts
->noval
)
799 switch (curp
->outtype
) {
801 html_man(curp
->outdata
, man
);
804 tree_man(curp
->outdata
, man
);
807 man_man(curp
->outdata
, man
);
814 terminal_man(curp
->outdata
, man
);
820 if (curp
->mmin
< MANDOCERR_STYLE
)
822 mparse_updaterc(curp
->mp
, &rc
);
826 check_xr(const char *file
)
828 static struct manpaths paths
;
829 struct mansearch search
;
830 struct mandoc_xr
*xr
;
835 manpath_base(&paths
);
837 for (xr
= mandoc_xr_get(); xr
!= NULL
; xr
= xr
->next
) {
841 search
.sec
= xr
->sec
;
842 search
.outkey
= NULL
;
843 search
.argmode
= ARG_NAME
;
844 search
.firstmatch
= 1;
845 if (mansearch(&search
, &paths
, 1, &xr
->name
, NULL
, &sz
))
847 if (fs_search(&search
, &paths
, 1, &xr
->name
, NULL
, &sz
))
850 mandoc_asprintf(&cp
, "Xr %s %s", xr
->name
, xr
->sec
);
852 mandoc_asprintf(&cp
, "Xr %s %s (%d times)",
853 xr
->name
, xr
->sec
, xr
->count
);
854 mmsg(MANDOCERR_XR_BAD
, MANDOCLEVEL_STYLE
,
855 file
, xr
->line
, xr
->pos
+ 1, cp
);
861 outdata_alloc(struct curparse
*curp
)
863 switch (curp
->outtype
) {
865 curp
->outdata
= html_alloc(curp
->outopts
);
868 curp
->outdata
= utf8_alloc(curp
->outopts
);
871 curp
->outdata
= locale_alloc(curp
->outopts
);
874 curp
->outdata
= ascii_alloc(curp
->outopts
);
877 curp
->outdata
= pdf_alloc(curp
->outopts
);
880 curp
->outdata
= ps_alloc(curp
->outopts
);
888 passthrough(const char *file
, int fd
, int synopsis_only
)
890 const char synb
[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
891 const char synr
[] = "SYNOPSIS";
897 ssize_t len
, written
;
903 if (fflush(stdout
) == EOF
) {
908 if ((stream
= fdopen(fd
, "r")) == NULL
) {
915 while ((len
= getline(&line
, &linesz
, stream
)) != -1) {
919 if ( ! isspace((unsigned char)*cp
))
921 while (isspace((unsigned char)*cp
)) {
926 if (strcmp(cp
, synb
) == 0 ||
927 strcmp(cp
, synr
) == 0)
932 for (; len
> 0; len
-= written
) {
933 if ((written
= write(STDOUT_FILENO
, cp
, len
)) != -1)
941 if (ferror(stream
)) {
954 warn("%s: SYSERR: %s", file
, syscall
);
955 if (rc
< MANDOCLEVEL_SYSERR
)
956 rc
= MANDOCLEVEL_SYSERR
;
960 koptions(int *options
, char *arg
)
963 if ( ! strcmp(arg
, "utf-8")) {
964 *options
|= MPARSE_UTF8
;
965 *options
&= ~MPARSE_LATIN1
;
966 } else if ( ! strcmp(arg
, "iso-8859-1")) {
967 *options
|= MPARSE_LATIN1
;
968 *options
&= ~MPARSE_UTF8
;
969 } else if ( ! strcmp(arg
, "us-ascii")) {
970 *options
&= ~(MPARSE_UTF8
| MPARSE_LATIN1
);
972 warnx("-K %s: Bad argument", arg
);
979 moptions(int *options
, char *arg
)
984 if (strcmp(arg
, "doc") == 0)
985 *options
|= MPARSE_MDOC
;
986 else if (strcmp(arg
, "an") == 0)
987 *options
|= MPARSE_MAN
;
991 toptions(struct curparse
*curp
, char *arg
)
994 if (0 == strcmp(arg
, "ascii"))
995 curp
->outtype
= OUTT_ASCII
;
996 else if (0 == strcmp(arg
, "lint")) {
997 curp
->outtype
= OUTT_LINT
;
998 curp
->mmin
= MANDOCERR_BASE
;
999 mmsg_stream
= stdout
;
1000 } else if (0 == strcmp(arg
, "tree"))
1001 curp
->outtype
= OUTT_TREE
;
1002 else if (0 == strcmp(arg
, "man"))
1003 curp
->outtype
= OUTT_MAN
;
1004 else if (0 == strcmp(arg
, "html"))
1005 curp
->outtype
= OUTT_HTML
;
1006 else if (0 == strcmp(arg
, "markdown"))
1007 curp
->outtype
= OUTT_MARKDOWN
;
1008 else if (0 == strcmp(arg
, "utf8"))
1009 curp
->outtype
= OUTT_UTF8
;
1010 else if (0 == strcmp(arg
, "locale"))
1011 curp
->outtype
= OUTT_LOCALE
;
1012 else if (0 == strcmp(arg
, "ps"))
1013 curp
->outtype
= OUTT_PS
;
1014 else if (0 == strcmp(arg
, "pdf"))
1015 curp
->outtype
= OUTT_PDF
;
1017 warnx("-T %s: Bad argument", arg
);
1025 woptions(struct curparse
*curp
, char *arg
)
1028 const char *toks
[11];
1034 toks
[4] = "warning";
1038 toks
[8] = "openbsd";
1044 switch (getsubopt(&arg
, (char * const *)toks
, &v
)) {
1050 curp
->mmin
= MANDOCERR_BASE
;
1053 curp
->mmin
= MANDOCERR_STYLE
;
1056 curp
->mmin
= MANDOCERR_WARNING
;
1059 curp
->mmin
= MANDOCERR_ERROR
;
1062 curp
->mmin
= MANDOCERR_UNSUPP
;
1065 curp
->mmin
= MANDOCERR_MAX
;
1068 curp
->mmin
= MANDOCERR_BASE
;
1069 curp
->os_e
= MANDOC_OS_OPENBSD
;
1072 curp
->mmin
= MANDOCERR_BASE
;
1073 curp
->os_e
= MANDOC_OS_NETBSD
;
1076 warnx("-W %s: Bad argument", o
);
1084 mmsg(enum mandocerr t
, enum mandoclevel lvl
,
1085 const char *file
, int line
, int col
, const char *msg
)
1087 const char *mparse_msg
;
1089 fprintf(mmsg_stream
, "%s: %s:", getprogname(),
1090 file
== NULL
? "<stdin>" : file
);
1093 fprintf(mmsg_stream
, "%d:%d:", line
, col
+ 1);
1095 fprintf(mmsg_stream
, " %s", mparse_strlevel(lvl
));
1097 if ((mparse_msg
= mparse_strerror(t
)) != NULL
)
1098 fprintf(mmsg_stream
, ": %s", mparse_msg
);
1101 fprintf(mmsg_stream
, ": %s", msg
);
1103 fputc('\n', mmsg_stream
);
1107 spawn_pager(struct tag_files
*tag_files
)
1109 const struct timespec timeout
= { 0, 100000000 }; /* 0.1s */
1110 #define MAX_PAGER_ARGS 16
1111 char *argv
[MAX_PAGER_ARGS
];
1118 pager
= getenv("MANPAGER");
1119 if (pager
== NULL
|| *pager
== '\0')
1120 pager
= getenv("PAGER");
1121 if (pager
== NULL
|| *pager
== '\0')
1123 cp
= mandoc_strdup(pager
);
1126 * Parse the pager command into words.
1127 * Intentionally do not do anything fancy here.
1131 while (argc
+ 4 < MAX_PAGER_ARGS
) {
1133 cp
= strchr(cp
, ' ');
1143 /* For less(1), use the tag file. */
1145 if ((cmdlen
= strlen(argv
[0])) >= 4) {
1146 cp
= argv
[0] + cmdlen
- 4;
1147 if (strcmp(cp
, "less") == 0) {
1148 argv
[argc
++] = mandoc_strdup("-T");
1149 argv
[argc
++] = tag_files
->tfn
;
1152 argv
[argc
++] = tag_files
->ofn
;
1155 switch (pager_pid
= fork()) {
1157 err((int)MANDOCLEVEL_SYSERR
, "fork");
1161 (void)setpgid(pager_pid
, 0);
1162 (void)tcsetpgrp(tag_files
->ofd
, pager_pid
);
1164 if (pledge("stdio rpath tmppath tty proc", NULL
) == -1)
1165 err((int)MANDOCLEVEL_SYSERR
, "pledge");
1167 tag_files
->pager_pid
= pager_pid
;
1171 /* The child process becomes the pager. */
1173 if (dup2(tag_files
->ofd
, STDOUT_FILENO
) == -1)
1174 err((int)MANDOCLEVEL_SYSERR
, "pager stdout");
1175 close(tag_files
->ofd
);
1176 close(tag_files
->tfd
);
1178 /* Do not start the pager before controlling the terminal. */
1180 while (tcgetpgrp(STDOUT_FILENO
) != getpid())
1181 nanosleep(&timeout
, NULL
);
1183 execvp(argv
[0], argv
);
1184 err((int)MANDOCLEVEL_SYSERR
, "exec %s", argv
[0]);