]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
ef051e539a4dad8569f095fdec3653e50656282d
1 /* $Id: main.c,v 1.299 2017/07/04 14:40:38 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
;
117 main(int argc
, char *argv
[])
120 struct mansearch search
;
121 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
.mmin
= MANDOCERR_MAX
;
191 curp
.outopts
= &conf
.output
;
192 options
= MPARSE_SO
| MPARSE_UTF8
| MPARSE_LATIN1
;
197 outmode
= OUTMODE_DEF
;
199 while ((c
= getopt(argc
, argv
,
200 "aC:cfhI:iK:klM:m:O:S:s:T:VW:w")) != -1) {
201 if (c
== 'i' && search
.argmode
== ARG_EXPR
) {
207 outmode
= OUTMODE_ALL
;
216 search
.argmode
= ARG_WORD
;
219 conf
.output
.synopsisonly
= 1;
221 outmode
= OUTMODE_ALL
;
224 if (strncmp(optarg
, "os=", 3)) {
225 warnx("-I %s: Bad argument", optarg
);
226 return (int)MANDOCLEVEL_BADARG
;
228 if (curp
.os_s
!= NULL
) {
229 warnx("-I %s: Duplicate argument", optarg
);
230 return (int)MANDOCLEVEL_BADARG
;
232 curp
.os_s
= mandoc_strdup(optarg
+ 3);
235 if ( ! koptions(&options
, optarg
))
236 return (int)MANDOCLEVEL_BADARG
;
239 search
.argmode
= ARG_EXPR
;
242 search
.argmode
= ARG_FILE
;
243 outmode
= OUTMODE_ALL
;
255 search
.arch
= optarg
;
261 if ( ! toptions(&curp
, optarg
))
262 return (int)MANDOCLEVEL_BADARG
;
265 if ( ! woptions(&curp
, optarg
))
266 return (int)MANDOCLEVEL_BADARG
;
269 outmode
= OUTMODE_FLN
;
278 usage(search
.argmode
);
280 /* Postprocess options. */
282 if (outmode
== OUTMODE_DEF
) {
283 switch (search
.argmode
) {
285 outmode
= OUTMODE_ALL
;
289 outmode
= OUTMODE_ONE
;
292 outmode
= OUTMODE_LST
;
298 if (outmode
== OUTMODE_LST
)
299 search
.outkey
= oarg
;
301 while (oarg
!= NULL
) {
303 if (manconf_output(&conf
.output
,
304 strsep(&oarg
, ","), 0) == 0)
306 warnx("-O %s: Bad argument", thisarg
);
307 return (int)MANDOCLEVEL_BADARG
;
312 if (outmode
== OUTMODE_FLN
||
313 outmode
== OUTMODE_LST
||
314 !isatty(STDOUT_FILENO
))
319 if (pledge("stdio rpath", NULL
) == -1)
320 err((int)MANDOCLEVEL_SYSERR
, "pledge");
323 /* Parse arguments. */
333 * and for a man(1) section argument without -s.
336 if (search
.argmode
== ARG_NAME
) {
337 if (*progname
== 'h') {
342 } else if (argc
> 1 &&
343 ((uc
= (unsigned char *)argv
[0]) != NULL
) &&
344 ((isdigit(uc
[0]) && (uc
[1] == '\0' ||
345 (isalpha(uc
[1]) && uc
[2] == '\0'))) ||
346 (uc
[0] == 'n' && uc
[1] == '\0'))) {
347 search
.sec
= (char *)uc
;
351 if (search
.arch
== NULL
)
352 search
.arch
= getenv("MACHINE");
354 if (search
.arch
== NULL
)
355 search
.arch
= MACHINE
;
361 /* man(1), whatis(1), apropos(1) */
363 if (search
.argmode
!= ARG_FILE
) {
364 if (search
.argmode
== ARG_NAME
&&
365 outmode
== OUTMODE_ONE
)
366 search
.firstmatch
= 1;
368 /* Access the mandoc database. */
370 manconf_parse(&conf
, conf_file
, defpaths
, auxpaths
);
371 if ( ! mansearch(&search
, &conf
.manpath
,
372 argc
, argv
, &res
, &sz
))
373 usage(search
.argmode
);
376 if (search
.argmode
== ARG_NAME
)
377 fs_search(&search
, &conf
.manpath
,
378 argc
, argv
, &res
, &sz
);
380 warnx("nothing appropriate");
384 rc
= MANDOCLEVEL_BADARG
;
389 * For standard man(1) and -a output mode,
390 * prepare for copying filename pointers
391 * into the program parameter array.
394 if (outmode
== OUTMODE_ONE
) {
397 } else if (outmode
== OUTMODE_ALL
)
400 /* Iterate all matching manuals. */
403 for (i
= 0; i
< sz
; i
++) {
404 if (outmode
== OUTMODE_FLN
)
406 else if (outmode
== OUTMODE_LST
)
407 printf("%s - %s\n", res
[i
].names
,
408 res
[i
].output
== NULL
? "" :
410 else if (outmode
== OUTMODE_ONE
) {
411 /* Search for the best section. */
413 sec
+= strcspn(sec
, "123456789");
416 prio
= sec_prios
[sec
[0] - '1'];
419 if (prio
>= best_prio
)
427 * For man(1), -a and -i output mode, fall through
428 * to the main mandoc(1) code iterating files
429 * and running the parsers on each of them.
432 if (outmode
== OUTMODE_FLN
|| outmode
== OUTMODE_LST
)
440 if (pledge("stdio rpath tmppath tty proc exec", NULL
) == -1)
441 err((int)MANDOCLEVEL_SYSERR
, "pledge");
443 if (pledge("stdio rpath", NULL
) == -1)
444 err((int)MANDOCLEVEL_SYSERR
, "pledge");
448 if (search
.argmode
== ARG_FILE
)
449 moptions(&options
, auxpaths
);
452 curp
.mp
= mparse_alloc(options
, curp
.mmin
, mmsg
,
453 curp
.os_e
, curp
.os_s
);
456 * Conditionally start up the lookaside buffer before parsing.
458 if (OUTT_MAN
== curp
.outtype
)
459 mparse_keep(curp
.mp
);
463 tag_files
= tag_init();
464 parse(&curp
, STDIN_FILENO
, "<stdin>");
468 fd
= mparse_open(curp
.mp
, resp
!= NULL
? resp
->file
: *argv
);
471 tag_files
= tag_init();
476 parse(&curp
, fd
, *argv
);
477 else if (resp
->form
== FORM_SRC
) {
478 /* For .so only; ignore failure. */
479 chdir(conf
.manpath
.paths
[resp
->ipath
]);
480 parse(&curp
, fd
, resp
->file
);
482 passthrough(resp
->file
, fd
,
483 conf
.output
.synopsisonly
);
485 if (argc
> 1 && curp
.outtype
<= OUTT_UTF8
) {
486 if (curp
.outdata
== NULL
)
487 outdata_alloc(&curp
);
488 terminal_sepline(curp
.outdata
);
490 } else if (rc
< MANDOCLEVEL_ERROR
)
491 rc
= MANDOCLEVEL_ERROR
;
493 if (MANDOCLEVEL_OK
!= rc
&& curp
.wstop
)
501 mparse_reset(curp
.mp
);
504 if (curp
.outdata
!= NULL
) {
505 switch (curp
.outtype
) {
507 html_free(curp
.outdata
);
512 ascii_free(curp
.outdata
);
516 pspdf_free(curp
.outdata
);
523 mparse_free(curp
.mp
);
527 if (search
.argmode
!= ARG_FILE
) {
529 mansearch_free(res
, sz
);
535 * When using a pager, finish writing both temporary files,
536 * fork it, wait for the user to close it, and clean up.
539 if (tag_files
!= NULL
) {
542 man_pgid
= getpgid(0);
543 tag_files
->tcpgid
= man_pgid
== getpid() ?
544 getpgid(getppid()) : man_pgid
;
549 /* Stop here until moved to the foreground. */
551 tc_pgid
= tcgetpgrp(tag_files
->ofd
);
552 if (tc_pgid
!= man_pgid
) {
553 if (tc_pgid
== pager_pid
) {
554 (void)tcsetpgrp(tag_files
->ofd
,
556 if (signum
== SIGTTIN
)
559 tag_files
->tcpgid
= tc_pgid
;
564 /* Once in the foreground, activate the pager. */
567 (void)tcsetpgrp(tag_files
->ofd
, pager_pid
);
568 kill(pager_pid
, SIGCONT
);
570 pager_pid
= spawn_pager(tag_files
);
572 /* Wait for the pager to stop or exit. */
574 while ((pid
= waitpid(pager_pid
, &status
,
575 WUNTRACED
)) == -1 && errno
== EINTR
)
580 rc
= MANDOCLEVEL_SYSERR
;
583 if (!WIFSTOPPED(status
))
586 signum
= WSTOPSIG(status
);
595 usage(enum argmode argmode
)
600 fputs("usage: mandoc [-ac] [-I os=name] "
601 "[-K encoding] [-mdoc | -man] [-O options]\n"
602 "\t [-T output] [-W level] [file ...]\n", stderr
);
605 fputs("usage: man [-acfhklw] [-C file] [-M path] "
606 "[-m path] [-S subsection]\n"
607 "\t [[-s] section] name ...\n", stderr
);
610 fputs("usage: whatis [-afk] [-C file] "
611 "[-M path] [-m path] [-O outkey] [-S arch]\n"
612 "\t [-s section] name ...\n", stderr
);
615 fputs("usage: apropos [-afk] [-C file] "
616 "[-M path] [-m path] [-O outkey] [-S arch]\n"
617 "\t [-s section] expression ...\n", stderr
);
620 exit((int)MANDOCLEVEL_BADARG
);
624 fs_lookup(const struct manpaths
*paths
, size_t ipath
,
625 const char *sec
, const char *arch
, const char *name
,
626 struct manpage
**res
, size_t *ressz
)
629 struct manpage
*page
;
635 mandoc_asprintf(&file
, "%s/man%s/%s.%s",
636 paths
->paths
[ipath
], sec
, name
, sec
);
637 if (access(file
, R_OK
) != -1)
641 mandoc_asprintf(&file
, "%s/cat%s/%s.0",
642 paths
->paths
[ipath
], sec
, name
);
643 if (access(file
, R_OK
) != -1) {
650 mandoc_asprintf(&file
, "%s/man%s/%s/%s.%s",
651 paths
->paths
[ipath
], sec
, arch
, name
, sec
);
652 if (access(file
, R_OK
) != -1)
657 mandoc_asprintf(&file
, "%s/man%s/%s.[01-9]*",
658 paths
->paths
[ipath
], sec
, name
);
659 globres
= glob(file
, 0, NULL
, &globinfo
);
660 if (globres
!= 0 && globres
!= GLOB_NOMATCH
)
661 warn("%s: glob", file
);
664 file
= mandoc_strdup(*globinfo
.gl_pathv
);
668 if (res
!= NULL
|| ipath
+ 1 != paths
->sz
)
671 mandoc_asprintf(&file
, "%s.%s", name
, sec
);
672 globres
= access(file
, R_OK
);
674 return globres
!= -1;
677 warnx("outdated mandoc.db lacks %s(%s) entry, run %s %s",
678 name
, sec
, BINM_MAKEWHATIS
, paths
->paths
[ipath
]);
683 *res
= mandoc_reallocarray(*res
, ++*ressz
, sizeof(struct manpage
));
684 page
= *res
+ (*ressz
- 1);
689 page
->bits
= NAME_FILE
& NAME_MASK
;
690 page
->sec
= (*sec
>= '1' && *sec
<= '9') ? *sec
- '1' + 1 : 10;
696 fs_search(const struct mansearch
*cfg
, const struct manpaths
*paths
,
697 int argc
, char **argv
, struct manpage
**res
, size_t *ressz
)
699 const char *const sections
[] =
700 {"1", "8", "6", "2", "3", "5", "7", "4", "9", "3p"};
701 const size_t nsec
= sizeof(sections
)/sizeof(sections
[0]);
703 size_t ipath
, isec
, lastsz
;
705 assert(cfg
->argmode
== ARG_NAME
);
711 for (ipath
= 0; ipath
< paths
->sz
; ipath
++) {
712 if (cfg
->sec
!= NULL
) {
713 if (fs_lookup(paths
, ipath
, cfg
->sec
,
714 cfg
->arch
, *argv
, res
, ressz
) &&
717 } else for (isec
= 0; isec
< nsec
; isec
++)
718 if (fs_lookup(paths
, ipath
, sections
[isec
],
719 cfg
->arch
, *argv
, res
, ressz
) &&
723 if (res
!= NULL
&& *ressz
== lastsz
)
724 warnx("No entry for %s in the manual.", *argv
);
733 parse(struct curparse
*curp
, int fd
, const char *file
)
735 enum mandoclevel rctmp
;
736 struct roff_man
*man
;
738 /* Begin by parsing the file itself. */
743 rctmp
= mparse_readfd(curp
->mp
, fd
, file
);
744 if (fd
!= STDIN_FILENO
)
750 * With -Wstop and warnings or errors of at least the requested
751 * level, do not produce output.
754 if (rctmp
!= MANDOCLEVEL_OK
&& curp
->wstop
)
757 if (curp
->outdata
== NULL
)
760 mparse_result(curp
->mp
, &man
, NULL
);
762 /* Execute the out device, if it exists. */
767 if (man
->macroset
== MACROSET_MDOC
) {
768 if (curp
->outtype
!= OUTT_TREE
|| !curp
->outopts
->noval
)
770 switch (curp
->outtype
) {
772 html_mdoc(curp
->outdata
, man
);
775 tree_mdoc(curp
->outdata
, man
);
778 man_mdoc(curp
->outdata
, man
);
785 terminal_mdoc(curp
->outdata
, man
);
788 markdown_mdoc(curp
->outdata
, man
);
794 if (man
->macroset
== MACROSET_MAN
) {
795 if (curp
->outtype
!= OUTT_TREE
|| !curp
->outopts
->noval
)
797 switch (curp
->outtype
) {
799 html_man(curp
->outdata
, man
);
802 tree_man(curp
->outdata
, man
);
805 man_man(curp
->outdata
, man
);
812 terminal_man(curp
->outdata
, man
);
818 if (curp
->mmin
< MANDOCERR_STYLE
)
820 mparse_updaterc(curp
->mp
, &rc
);
824 check_xr(const char *file
)
826 static struct manpaths paths
;
827 struct mansearch search
;
828 struct mandoc_xr
*xr
;
833 manpath_base(&paths
);
835 for (xr
= mandoc_xr_get(); xr
!= NULL
; xr
= xr
->next
) {
839 search
.sec
= xr
->sec
;
840 search
.outkey
= NULL
;
841 search
.argmode
= ARG_NAME
;
842 search
.firstmatch
= 1;
843 if (mansearch(&search
, &paths
, 1, &xr
->name
, NULL
, &sz
))
845 if (fs_search(&search
, &paths
, 1, &xr
->name
, NULL
, &sz
))
848 mandoc_asprintf(&cp
, "Xr %s %s", xr
->name
, xr
->sec
);
850 mandoc_asprintf(&cp
, "Xr %s %s (%d times)",
851 xr
->name
, xr
->sec
, xr
->count
);
852 mmsg(MANDOCERR_XR_BAD
, MANDOCLEVEL_STYLE
,
853 file
, xr
->line
, xr
->pos
+ 1, cp
);
859 outdata_alloc(struct curparse
*curp
)
861 switch (curp
->outtype
) {
863 curp
->outdata
= html_alloc(curp
->outopts
);
866 curp
->outdata
= utf8_alloc(curp
->outopts
);
869 curp
->outdata
= locale_alloc(curp
->outopts
);
872 curp
->outdata
= ascii_alloc(curp
->outopts
);
875 curp
->outdata
= pdf_alloc(curp
->outopts
);
878 curp
->outdata
= ps_alloc(curp
->outopts
);
886 passthrough(const char *file
, int fd
, int synopsis_only
)
888 const char synb
[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
889 const char synr
[] = "SYNOPSIS";
895 ssize_t len
, written
;
901 if (fflush(stdout
) == EOF
) {
906 if ((stream
= fdopen(fd
, "r")) == NULL
) {
913 while ((len
= getline(&line
, &linesz
, stream
)) != -1) {
917 if ( ! isspace((unsigned char)*cp
))
919 while (isspace((unsigned char)*cp
)) {
924 if (strcmp(cp
, synb
) == 0 ||
925 strcmp(cp
, synr
) == 0)
930 for (; len
> 0; len
-= written
) {
931 if ((written
= write(STDOUT_FILENO
, cp
, len
)) != -1)
939 if (ferror(stream
)) {
952 warn("%s: SYSERR: %s", file
, syscall
);
953 if (rc
< MANDOCLEVEL_SYSERR
)
954 rc
= MANDOCLEVEL_SYSERR
;
958 koptions(int *options
, char *arg
)
961 if ( ! strcmp(arg
, "utf-8")) {
962 *options
|= MPARSE_UTF8
;
963 *options
&= ~MPARSE_LATIN1
;
964 } else if ( ! strcmp(arg
, "iso-8859-1")) {
965 *options
|= MPARSE_LATIN1
;
966 *options
&= ~MPARSE_UTF8
;
967 } else if ( ! strcmp(arg
, "us-ascii")) {
968 *options
&= ~(MPARSE_UTF8
| MPARSE_LATIN1
);
970 warnx("-K %s: Bad argument", arg
);
977 moptions(int *options
, char *arg
)
982 if (strcmp(arg
, "doc") == 0)
983 *options
|= MPARSE_MDOC
;
984 else if (strcmp(arg
, "an") == 0)
985 *options
|= MPARSE_MAN
;
989 toptions(struct curparse
*curp
, char *arg
)
992 if (0 == strcmp(arg
, "ascii"))
993 curp
->outtype
= OUTT_ASCII
;
994 else if (0 == strcmp(arg
, "lint")) {
995 curp
->outtype
= OUTT_LINT
;
996 curp
->mmin
= MANDOCERR_BASE
;
997 } else if (0 == strcmp(arg
, "tree"))
998 curp
->outtype
= OUTT_TREE
;
999 else if (0 == strcmp(arg
, "man"))
1000 curp
->outtype
= OUTT_MAN
;
1001 else if (0 == strcmp(arg
, "html"))
1002 curp
->outtype
= OUTT_HTML
;
1003 else if (0 == strcmp(arg
, "markdown"))
1004 curp
->outtype
= OUTT_MARKDOWN
;
1005 else if (0 == strcmp(arg
, "utf8"))
1006 curp
->outtype
= OUTT_UTF8
;
1007 else if (0 == strcmp(arg
, "locale"))
1008 curp
->outtype
= OUTT_LOCALE
;
1009 else if (0 == strcmp(arg
, "ps"))
1010 curp
->outtype
= OUTT_PS
;
1011 else if (0 == strcmp(arg
, "pdf"))
1012 curp
->outtype
= OUTT_PDF
;
1014 warnx("-T %s: Bad argument", arg
);
1022 woptions(struct curparse
*curp
, char *arg
)
1025 const char *toks
[11];
1031 toks
[4] = "warning";
1035 toks
[8] = "openbsd";
1041 switch (getsubopt(&arg
, (char * const *)toks
, &v
)) {
1047 curp
->mmin
= MANDOCERR_BASE
;
1050 curp
->mmin
= MANDOCERR_STYLE
;
1053 curp
->mmin
= MANDOCERR_WARNING
;
1056 curp
->mmin
= MANDOCERR_ERROR
;
1059 curp
->mmin
= MANDOCERR_UNSUPP
;
1062 curp
->mmin
= MANDOCERR_MAX
;
1065 curp
->mmin
= MANDOCERR_BASE
;
1066 curp
->os_e
= MANDOC_OS_OPENBSD
;
1069 curp
->mmin
= MANDOCERR_BASE
;
1070 curp
->os_e
= MANDOC_OS_NETBSD
;
1073 warnx("-W %s: Bad argument", o
);
1081 mmsg(enum mandocerr t
, enum mandoclevel lvl
,
1082 const char *file
, int line
, int col
, const char *msg
)
1084 const char *mparse_msg
;
1086 fprintf(stderr
, "%s: %s:", getprogname(),
1087 file
== NULL
? "<stdin>" : file
);
1090 fprintf(stderr
, "%d:%d:", line
, col
+ 1);
1092 fprintf(stderr
, " %s", mparse_strlevel(lvl
));
1094 if ((mparse_msg
= mparse_strerror(t
)) != NULL
)
1095 fprintf(stderr
, ": %s", mparse_msg
);
1098 fprintf(stderr
, ": %s", msg
);
1100 fputc('\n', stderr
);
1104 spawn_pager(struct tag_files
*tag_files
)
1106 const struct timespec timeout
= { 0, 100000000 }; /* 0.1s */
1107 #define MAX_PAGER_ARGS 16
1108 char *argv
[MAX_PAGER_ARGS
];
1115 pager
= getenv("MANPAGER");
1116 if (pager
== NULL
|| *pager
== '\0')
1117 pager
= getenv("PAGER");
1118 if (pager
== NULL
|| *pager
== '\0')
1120 cp
= mandoc_strdup(pager
);
1123 * Parse the pager command into words.
1124 * Intentionally do not do anything fancy here.
1128 while (argc
+ 4 < MAX_PAGER_ARGS
) {
1130 cp
= strchr(cp
, ' ');
1140 /* For less(1), use the tag file. */
1142 if ((cmdlen
= strlen(argv
[0])) >= 4) {
1143 cp
= argv
[0] + cmdlen
- 4;
1144 if (strcmp(cp
, "less") == 0) {
1145 argv
[argc
++] = mandoc_strdup("-T");
1146 argv
[argc
++] = tag_files
->tfn
;
1149 argv
[argc
++] = tag_files
->ofn
;
1152 switch (pager_pid
= fork()) {
1154 err((int)MANDOCLEVEL_SYSERR
, "fork");
1158 (void)setpgid(pager_pid
, 0);
1159 (void)tcsetpgrp(tag_files
->ofd
, pager_pid
);
1161 if (pledge("stdio rpath tmppath tty proc", NULL
) == -1)
1162 err((int)MANDOCLEVEL_SYSERR
, "pledge");
1164 tag_files
->pager_pid
= pager_pid
;
1168 /* The child process becomes the pager. */
1170 if (dup2(tag_files
->ofd
, STDOUT_FILENO
) == -1)
1171 err((int)MANDOCLEVEL_SYSERR
, "pager stdout");
1172 close(tag_files
->ofd
);
1173 close(tag_files
->tfd
);
1175 /* Do not start the pager before controlling the terminal. */
1177 while (tcgetpgrp(STDOUT_FILENO
) != getpid())
1178 nanosleep(&timeout
, NULL
);
1180 execvp(argv
[0], argv
);
1181 err((int)MANDOCLEVEL_SYSERR
, "exec %s", argv
[0]);