]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
1 /* $Id: main.c,v 1.327 2019/05/03 18:39:34 schwarze Exp $ */
3 * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010-2012, 2014-2019 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 */
47 #include "mandoc_aux.h"
49 #include "mandoc_xr.h"
53 #include "mandoc_parse.h"
57 #include "mansearch.h"
68 OUTT_ASCII
= 0, /* -Tascii */
69 OUTT_LOCALE
, /* -Tlocale */
70 OUTT_UTF8
, /* -Tutf8 */
71 OUTT_TREE
, /* -Ttree */
73 OUTT_HTML
, /* -Thtml */
74 OUTT_MARKDOWN
, /* -Tmarkdown */
75 OUTT_LINT
, /* -Tlint */
82 struct manoutput
*outopts
; /* output options */
83 void *outdata
; /* data for output */
84 char *os_s
; /* operating system for display */
85 int wstop
; /* stop after a file with a warning */
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(void);
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 outdata_alloc(struct curparse
*);
104 static void parse(struct curparse
*, int, const char *);
105 static void passthrough(const char *, int, int);
106 static pid_t
spawn_pager(struct tag_files
*);
107 static int toptions(struct curparse
*, char *);
108 static void usage(enum argmode
) __attribute__((__noreturn__
));
109 static int woptions(struct curparse
*, char *);
111 static const int sec_prios
[] = {1, 4, 5, 8, 6, 3, 7, 2, 9};
112 static char help_arg
[] = "help";
113 static char *help_argv
[] = {help_arg
, NULL
};
117 main(int argc
, char *argv
[])
120 struct mansearch search
;
121 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 mandoc_msg_setoutfile(stderr
);
153 if (strncmp(progname
, "mandocdb", 8) == 0 ||
154 strcmp(progname
, BINM_MAKEWHATIS
) == 0)
155 return mandocdb(argc
, argv
);
158 if (pledge("stdio rpath tmppath tty proc exec", NULL
) == -1)
159 err((int)MANDOCLEVEL_SYSERR
, "pledge");
162 #if HAVE_SANDBOX_INIT
163 if (sandbox_init(kSBXProfileNoInternet
, SANDBOX_NAMED
, NULL
) == -1)
164 errx((int)MANDOCLEVEL_SYSERR
, "sandbox_init");
167 /* Search options. */
169 memset(&conf
, 0, sizeof(conf
));
170 conf_file
= defpaths
= NULL
;
173 memset(&search
, 0, sizeof(struct mansearch
));
174 search
.outkey
= "Nd";
177 if (strcmp(progname
, BINM_MAN
) == 0)
178 search
.argmode
= ARG_NAME
;
179 else if (strcmp(progname
, BINM_APROPOS
) == 0)
180 search
.argmode
= ARG_EXPR
;
181 else if (strcmp(progname
, BINM_WHATIS
) == 0)
182 search
.argmode
= ARG_WORD
;
183 else if (strncmp(progname
, "help", 4) == 0)
184 search
.argmode
= ARG_NAME
;
186 search
.argmode
= ARG_FILE
;
188 /* Parser and formatter options. */
190 memset(&curp
, 0, sizeof(struct curparse
));
191 curp
.outtype
= OUTT_LOCALE
;
192 curp
.outopts
= &conf
.output
;
193 options
= MPARSE_SO
| MPARSE_UTF8
| MPARSE_LATIN1
;
198 outmode
= OUTMODE_DEF
;
200 while ((c
= getopt(argc
, argv
,
201 "aC:cfhI:iK:klM:m:O:S:s:T:VW:w")) != -1) {
202 if (c
== 'i' && search
.argmode
== ARG_EXPR
) {
208 outmode
= OUTMODE_ALL
;
217 search
.argmode
= ARG_WORD
;
220 conf
.output
.synopsisonly
= 1;
222 outmode
= OUTMODE_ALL
;
225 if (strncmp(optarg
, "os=", 3)) {
226 warnx("-I %s: Bad argument", optarg
);
227 return (int)MANDOCLEVEL_BADARG
;
229 if (curp
.os_s
!= NULL
) {
230 warnx("-I %s: Duplicate argument", optarg
);
231 return (int)MANDOCLEVEL_BADARG
;
233 curp
.os_s
= mandoc_strdup(optarg
+ 3);
236 if ( ! koptions(&options
, optarg
))
237 return (int)MANDOCLEVEL_BADARG
;
240 search
.argmode
= ARG_EXPR
;
243 search
.argmode
= ARG_FILE
;
244 outmode
= OUTMODE_ALL
;
256 search
.arch
= optarg
;
262 if ( ! toptions(&curp
, optarg
))
263 return (int)MANDOCLEVEL_BADARG
;
266 if ( ! woptions(&curp
, optarg
))
267 return (int)MANDOCLEVEL_BADARG
;
270 outmode
= OUTMODE_FLN
;
279 usage(search
.argmode
);
281 /* Postprocess options. */
283 if (outmode
== OUTMODE_DEF
) {
284 switch (search
.argmode
) {
286 outmode
= OUTMODE_ALL
;
290 outmode
= OUTMODE_ONE
;
293 outmode
= OUTMODE_LST
;
299 if (outmode
== OUTMODE_LST
)
300 search
.outkey
= oarg
;
302 while (oarg
!= NULL
) {
303 if (manconf_output(&conf
.output
,
304 strsep(&oarg
, ","), 0) == -1)
305 return (int)MANDOCLEVEL_BADARG
;
310 if (curp
.outtype
!= OUTT_TREE
|| !curp
.outopts
->noval
)
311 options
|= MPARSE_VALIDATE
;
313 if (outmode
== OUTMODE_FLN
||
314 outmode
== OUTMODE_LST
||
315 !isatty(STDOUT_FILENO
))
319 (conf
.output
.width
== 0 || conf
.output
.indent
== 0) &&
320 ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &ws
) != -1 &&
322 if (conf
.output
.width
== 0 && ws
.ws_col
< 79)
323 conf
.output
.width
= ws
.ws_col
- 1;
324 if (conf
.output
.indent
== 0 && ws
.ws_col
< 66)
325 conf
.output
.indent
= 3;
330 if (pledge("stdio rpath", NULL
) == -1)
331 err((int)MANDOCLEVEL_SYSERR
, "pledge");
334 /* Parse arguments. */
344 * and for a man(1) section argument without -s.
347 if (search
.argmode
== ARG_NAME
) {
348 if (*progname
== 'h') {
353 } else if (argc
> 1 &&
354 ((uc
= (unsigned char *)argv
[0]) != NULL
) &&
355 ((isdigit(uc
[0]) && (uc
[1] == '\0' ||
357 (uc
[0] == 'n' && uc
[1] == '\0'))) {
358 search
.sec
= (char *)uc
;
362 if (search
.arch
== NULL
)
363 search
.arch
= getenv("MACHINE");
365 if (search
.arch
== NULL
)
366 search
.arch
= MACHINE
;
371 * Use the first argument for -O tag in addition to
372 * using it as a search term for man(1) or apropos(1).
375 if (conf
.output
.tag
!= NULL
&& *conf
.output
.tag
== '\0') {
376 tagarg
= argc
> 0 && search
.argmode
== ARG_EXPR
?
377 strchr(*argv
, '=') : NULL
;
378 conf
.output
.tag
= tagarg
== NULL
? *argv
: tagarg
+ 1;
381 /* man(1), whatis(1), apropos(1) */
383 if (search
.argmode
!= ARG_FILE
) {
384 if (search
.argmode
== ARG_NAME
&&
385 outmode
== OUTMODE_ONE
)
386 search
.firstmatch
= 1;
388 /* Access the mandoc database. */
390 manconf_parse(&conf
, conf_file
, defpaths
, auxpaths
);
391 if ( ! mansearch(&search
, &conf
.manpath
,
392 argc
, argv
, &res
, &sz
))
393 usage(search
.argmode
);
395 if (sz
== 0 && search
.argmode
== ARG_NAME
)
396 fs_search(&search
, &conf
.manpath
,
397 argc
, argv
, &res
, &sz
);
399 if (search
.argmode
== ARG_NAME
) {
400 for (c
= 0; c
< argc
; c
++) {
401 if (strchr(argv
[c
], '/') == NULL
)
403 if (access(argv
[c
], R_OK
) == -1) {
407 res
= mandoc_reallocarray(res
,
408 sz
+ 1, sizeof(*res
));
409 res
[sz
].file
= mandoc_strdup(argv
[c
]);
410 res
[sz
].names
= NULL
;
411 res
[sz
].output
= NULL
;
413 res
[sz
].ipath
= SIZE_MAX
;
415 res
[sz
].form
= FORM_SRC
;
421 if (search
.argmode
!= ARG_NAME
)
422 warnx("nothing appropriate");
423 mandoc_msg_setrc(MANDOCLEVEL_BADARG
);
428 * For standard man(1) and -a output mode,
429 * prepare for copying filename pointers
430 * into the program parameter array.
433 if (outmode
== OUTMODE_ONE
) {
436 } else if (outmode
== OUTMODE_ALL
)
439 /* Iterate all matching manuals. */
442 for (i
= 0; i
< sz
; i
++) {
443 if (outmode
== OUTMODE_FLN
)
445 else if (outmode
== OUTMODE_LST
)
446 printf("%s - %s\n", res
[i
].names
,
447 res
[i
].output
== NULL
? "" :
449 else if (outmode
== OUTMODE_ONE
) {
450 /* Search for the best section. */
452 sec
+= strcspn(sec
, "123456789");
454 continue; /* No section at all. */
455 prio
= sec_prios
[sec
[0] - '1'];
456 if (search
.sec
!= NULL
) {
457 ssz
= strlen(search
.sec
);
458 if (strncmp(sec
, search
.sec
, ssz
) == 0)
461 sec
++; /* Prefer without suffix. */
463 prio
+= 10; /* Wrong dir name. */
464 if (search
.sec
!= NULL
&&
465 (strlen(sec
) <= ssz
+ 3 ||
466 strcmp(sec
+ strlen(sec
) - ssz
,
468 prio
+= 20; /* Wrong file ext. */
469 if (prio
>= best_prio
)
477 * For man(1), -a and -i output mode, fall through
478 * to the main mandoc(1) code iterating files
479 * and running the parsers on each of them.
482 if (outmode
== OUTMODE_FLN
|| outmode
== OUTMODE_LST
)
490 if (pledge("stdio rpath tmppath tty proc exec", NULL
) == -1)
491 err((int)MANDOCLEVEL_SYSERR
, "pledge");
493 if (pledge("stdio rpath", NULL
) == -1)
494 err((int)MANDOCLEVEL_SYSERR
, "pledge");
498 if (search
.argmode
== ARG_FILE
)
499 moptions(&options
, auxpaths
);
502 curp
.mp
= mparse_alloc(options
, curp
.os_e
, curp
.os_s
);
506 tag_files
= tag_init();
507 tag_files
->tagname
= conf
.output
.tag
;
510 mandoc_msg_setinfilename(thisarg
);
511 parse(&curp
, STDIN_FILENO
, thisarg
);
512 mandoc_msg_setinfilename(NULL
);
516 * Remember the original working directory, if possible.
517 * This will be needed if some names on the command line
518 * are page names and some are relative file names.
519 * Do not error out if the current directory is not
520 * readable: Maybe it won't be needed after all.
522 startdir
= open(".", O_RDONLY
| O_DIRECTORY
);
527 * Changing directories is not needed in ARG_FILE mode.
528 * Do it on a best-effort basis. Even in case of
529 * failure, some functionality may still work.
532 if (resp
->ipath
!= SIZE_MAX
)
533 (void)chdir(conf
.manpath
.paths
[resp
->ipath
]);
534 else if (startdir
!= -1)
535 (void)fchdir(startdir
);
536 thisarg
= resp
->file
;
540 fd
= mparse_open(curp
.mp
, thisarg
);
544 tag_files
= tag_init();
545 tag_files
->tagname
= conf
.output
.tag
;
548 mandoc_msg_setinfilename(thisarg
);
549 if (resp
== NULL
|| resp
->form
== FORM_SRC
)
550 parse(&curp
, fd
, thisarg
);
552 passthrough(resp
->file
, fd
,
553 conf
.output
.synopsisonly
);
554 mandoc_msg_setinfilename(NULL
);
556 if (ferror(stdout
)) {
557 if (tag_files
!= NULL
) {
558 warn("%s", tag_files
->ofn
);
563 mandoc_msg_setrc(MANDOCLEVEL_SYSERR
);
567 if (argc
> 1 && curp
.outtype
<= OUTT_UTF8
) {
568 if (curp
.outdata
== NULL
)
569 outdata_alloc(&curp
);
570 terminal_sepline(curp
.outdata
);
573 mandoc_msg(MANDOCERR_FILE
, 0, 0,
574 "%s: %s", thisarg
, strerror(errno
));
576 if (curp
.wstop
&& mandoc_msg_getrc() != MANDOCLEVEL_OK
)
584 mparse_reset(curp
.mp
);
586 if (startdir
!= -1) {
587 (void)fchdir(startdir
);
591 if (curp
.outdata
!= NULL
) {
592 switch (curp
.outtype
) {
594 html_free(curp
.outdata
);
599 ascii_free(curp
.outdata
);
603 pspdf_free(curp
.outdata
);
610 mparse_free(curp
.mp
);
614 if (search
.argmode
!= ARG_FILE
) {
616 mansearch_free(res
, sz
);
622 * When using a pager, finish writing both temporary files,
623 * fork it, wait for the user to close it, and clean up.
626 if (tag_files
!= NULL
) {
629 man_pgid
= getpgid(0);
630 tag_files
->tcpgid
= man_pgid
== getpid() ?
631 getpgid(getppid()) : man_pgid
;
636 /* Stop here until moved to the foreground. */
638 tc_pgid
= tcgetpgrp(tag_files
->ofd
);
639 if (tc_pgid
!= man_pgid
) {
640 if (tc_pgid
== pager_pid
) {
641 (void)tcsetpgrp(tag_files
->ofd
,
643 if (signum
== SIGTTIN
)
646 tag_files
->tcpgid
= tc_pgid
;
651 /* Once in the foreground, activate the pager. */
654 (void)tcsetpgrp(tag_files
->ofd
, pager_pid
);
655 kill(pager_pid
, SIGCONT
);
657 pager_pid
= spawn_pager(tag_files
);
659 /* Wait for the pager to stop or exit. */
661 while ((pid
= waitpid(pager_pid
, &status
,
662 WUNTRACED
)) == -1 && errno
== EINTR
)
667 mandoc_msg_setrc(MANDOCLEVEL_SYSERR
);
670 if (!WIFSTOPPED(status
))
673 signum
= WSTOPSIG(status
);
677 return (int)mandoc_msg_getrc();
681 usage(enum argmode argmode
)
686 fputs("usage: mandoc [-ac] [-I os=name] "
687 "[-K encoding] [-mdoc | -man] [-O options]\n"
688 "\t [-T output] [-W level] [file ...]\n", stderr
);
691 fputs("usage: man [-acfhklw] [-C file] [-M path] "
692 "[-m path] [-S subsection]\n"
693 "\t [[-s] section] name ...\n", stderr
);
696 fputs("usage: whatis [-afk] [-C file] "
697 "[-M path] [-m path] [-O outkey] [-S arch]\n"
698 "\t [-s section] name ...\n", stderr
);
701 fputs("usage: apropos [-afk] [-C file] "
702 "[-M path] [-m path] [-O outkey] [-S arch]\n"
703 "\t [-s section] expression ...\n", stderr
);
706 exit((int)MANDOCLEVEL_BADARG
);
710 fs_lookup(const struct manpaths
*paths
, size_t ipath
,
711 const char *sec
, const char *arch
, const char *name
,
712 struct manpage
**res
, size_t *ressz
)
716 struct manpage
*page
;
722 mandoc_asprintf(&file
, "%s/man%s/%s.%s",
723 paths
->paths
[ipath
], sec
, name
, sec
);
724 if (stat(file
, &sb
) != -1)
728 mandoc_asprintf(&file
, "%s/cat%s/%s.0",
729 paths
->paths
[ipath
], sec
, name
);
730 if (stat(file
, &sb
) != -1) {
737 mandoc_asprintf(&file
, "%s/man%s/%s/%s.%s",
738 paths
->paths
[ipath
], sec
, arch
, name
, sec
);
739 if (stat(file
, &sb
) != -1)
744 mandoc_asprintf(&file
, "%s/man%s/%s.[01-9]*",
745 paths
->paths
[ipath
], sec
, name
);
746 globres
= glob(file
, 0, NULL
, &globinfo
);
747 if (globres
!= 0 && globres
!= GLOB_NOMATCH
)
748 warn("%s: glob", file
);
751 file
= mandoc_strdup(*globinfo
.gl_pathv
);
754 if (stat(file
, &sb
) != -1)
758 if (res
!= NULL
|| ipath
+ 1 != paths
->sz
)
761 mandoc_asprintf(&file
, "%s.%s", name
, sec
);
762 globres
= stat(file
, &sb
);
764 return globres
!= -1;
767 warnx("outdated mandoc.db lacks %s(%s) entry, run %s %s",
768 name
, sec
, BINM_MAKEWHATIS
, paths
->paths
[ipath
]);
773 *res
= mandoc_reallocarray(*res
, ++*ressz
, sizeof(struct manpage
));
774 page
= *res
+ (*ressz
- 1);
778 page
->bits
= NAME_FILE
& NAME_MASK
;
780 page
->sec
= (*sec
>= '1' && *sec
<= '9') ? *sec
- '1' + 1 : 10;
786 fs_search(const struct mansearch
*cfg
, const struct manpaths
*paths
,
787 int argc
, char **argv
, struct manpage
**res
, size_t *ressz
)
789 const char *const sections
[] =
790 {"1", "8", "6", "2", "3", "5", "7", "4", "9", "3p"};
791 const size_t nsec
= sizeof(sections
)/sizeof(sections
[0]);
793 size_t ipath
, isec
, lastsz
;
795 assert(cfg
->argmode
== ARG_NAME
);
801 for (ipath
= 0; ipath
< paths
->sz
; ipath
++) {
802 if (cfg
->sec
!= NULL
) {
803 if (fs_lookup(paths
, ipath
, cfg
->sec
,
804 cfg
->arch
, *argv
, res
, ressz
) &&
807 } else for (isec
= 0; isec
< nsec
; isec
++)
808 if (fs_lookup(paths
, ipath
, sections
[isec
],
809 cfg
->arch
, *argv
, res
, ressz
) &&
813 if (res
!= NULL
&& *ressz
== lastsz
&&
814 strchr(*argv
, '/') == NULL
) {
815 if (cfg
->arch
!= NULL
&&
816 arch_valid(cfg
->arch
, OSENUM
) == 0)
817 warnx("Unknown architecture \"%s\".",
819 else if (cfg
->sec
== NULL
)
820 warnx("No entry for %s in the manual.",
823 warnx("No entry for %s in section %s "
824 "of the manual.", *argv
, cfg
->sec
);
834 parse(struct curparse
*curp
, int fd
, const char *file
)
836 struct roff_meta
*meta
;
838 /* Begin by parsing the file itself. */
843 mparse_readfd(curp
->mp
, fd
, file
);
844 if (fd
!= STDIN_FILENO
)
848 * With -Wstop and warnings or errors of at least the requested
849 * level, do not produce output.
852 if (curp
->wstop
&& mandoc_msg_getrc() != MANDOCLEVEL_OK
)
855 if (curp
->outdata
== NULL
)
857 else if (curp
->outtype
== OUTT_HTML
)
861 meta
= mparse_result(curp
->mp
);
863 /* Execute the out device, if it exists. */
865 if (meta
->macroset
== MACROSET_MDOC
) {
866 switch (curp
->outtype
) {
868 html_mdoc(curp
->outdata
, meta
);
871 tree_mdoc(curp
->outdata
, meta
);
874 man_mdoc(curp
->outdata
, meta
);
881 terminal_mdoc(curp
->outdata
, meta
);
884 markdown_mdoc(curp
->outdata
, meta
);
890 if (meta
->macroset
== MACROSET_MAN
) {
891 switch (curp
->outtype
) {
893 html_man(curp
->outdata
, meta
);
896 tree_man(curp
->outdata
, meta
);
899 mparse_copy(curp
->mp
);
906 terminal_man(curp
->outdata
, meta
);
912 if (mandoc_msg_getmin() < MANDOCERR_STYLE
)
919 static struct manpaths paths
;
920 struct mansearch search
;
921 struct mandoc_xr
*xr
;
925 manpath_base(&paths
);
927 for (xr
= mandoc_xr_get(); xr
!= NULL
; xr
= xr
->next
) {
931 search
.sec
= xr
->sec
;
932 search
.outkey
= NULL
;
933 search
.argmode
= ARG_NAME
;
934 search
.firstmatch
= 1;
935 if (mansearch(&search
, &paths
, 1, &xr
->name
, NULL
, &sz
))
937 if (fs_search(&search
, &paths
, 1, &xr
->name
, NULL
, &sz
))
940 mandoc_msg(MANDOCERR_XR_BAD
, xr
->line
,
941 xr
->pos
+ 1, "Xr %s %s", xr
->name
, xr
->sec
);
943 mandoc_msg(MANDOCERR_XR_BAD
, xr
->line
,
944 xr
->pos
+ 1, "Xr %s %s (%d times)",
945 xr
->name
, xr
->sec
, xr
->count
);
950 outdata_alloc(struct curparse
*curp
)
952 switch (curp
->outtype
) {
954 curp
->outdata
= html_alloc(curp
->outopts
);
957 curp
->outdata
= utf8_alloc(curp
->outopts
);
960 curp
->outdata
= locale_alloc(curp
->outopts
);
963 curp
->outdata
= ascii_alloc(curp
->outopts
);
966 curp
->outdata
= pdf_alloc(curp
->outopts
);
969 curp
->outdata
= ps_alloc(curp
->outopts
);
977 passthrough(const char *file
, int fd
, int synopsis_only
)
979 const char synb
[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
980 const char synr
[] = "SYNOPSIS";
986 ssize_t len
, written
;
992 if (fflush(stdout
) == EOF
) {
997 if ((stream
= fdopen(fd
, "r")) == NULL
) {
1004 while ((len
= getline(&line
, &linesz
, stream
)) != -1) {
1006 if (synopsis_only
) {
1008 if ( ! isspace((unsigned char)*cp
))
1010 while (isspace((unsigned char)*cp
)) {
1015 if (strcmp(cp
, synb
) == 0 ||
1016 strcmp(cp
, synr
) == 0)
1021 for (; len
> 0; len
-= written
) {
1022 if ((written
= write(STDOUT_FILENO
, cp
, len
)) != -1)
1030 if (ferror(stream
)) {
1032 syscall
= "getline";
1043 warn("%s: SYSERR: %s", file
, syscall
);
1044 mandoc_msg_setrc(MANDOCLEVEL_SYSERR
);
1048 koptions(int *options
, char *arg
)
1051 if ( ! strcmp(arg
, "utf-8")) {
1052 *options
|= MPARSE_UTF8
;
1053 *options
&= ~MPARSE_LATIN1
;
1054 } else if ( ! strcmp(arg
, "iso-8859-1")) {
1055 *options
|= MPARSE_LATIN1
;
1056 *options
&= ~MPARSE_UTF8
;
1057 } else if ( ! strcmp(arg
, "us-ascii")) {
1058 *options
&= ~(MPARSE_UTF8
| MPARSE_LATIN1
);
1060 warnx("-K %s: Bad argument", arg
);
1067 moptions(int *options
, char *arg
)
1072 if (strcmp(arg
, "doc") == 0)
1073 *options
|= MPARSE_MDOC
;
1074 else if (strcmp(arg
, "an") == 0)
1075 *options
|= MPARSE_MAN
;
1079 toptions(struct curparse
*curp
, char *arg
)
1082 if (0 == strcmp(arg
, "ascii"))
1083 curp
->outtype
= OUTT_ASCII
;
1084 else if (0 == strcmp(arg
, "lint")) {
1085 curp
->outtype
= OUTT_LINT
;
1086 mandoc_msg_setoutfile(stdout
);
1087 mandoc_msg_setmin(MANDOCERR_BASE
);
1088 } else if (0 == strcmp(arg
, "tree"))
1089 curp
->outtype
= OUTT_TREE
;
1090 else if (0 == strcmp(arg
, "man"))
1091 curp
->outtype
= OUTT_MAN
;
1092 else if (0 == strcmp(arg
, "html"))
1093 curp
->outtype
= OUTT_HTML
;
1094 else if (0 == strcmp(arg
, "markdown"))
1095 curp
->outtype
= OUTT_MARKDOWN
;
1096 else if (0 == strcmp(arg
, "utf8"))
1097 curp
->outtype
= OUTT_UTF8
;
1098 else if (0 == strcmp(arg
, "locale"))
1099 curp
->outtype
= OUTT_LOCALE
;
1100 else if (0 == strcmp(arg
, "ps"))
1101 curp
->outtype
= OUTT_PS
;
1102 else if (0 == strcmp(arg
, "pdf"))
1103 curp
->outtype
= OUTT_PDF
;
1105 warnx("-T %s: Bad argument", arg
);
1113 woptions(struct curparse
*curp
, char *arg
)
1116 const char *toks
[11];
1122 toks
[4] = "warning";
1126 toks
[8] = "openbsd";
1132 switch (getsubopt(&arg
, (char * const *)toks
, &v
)) {
1138 mandoc_msg_setmin(MANDOCERR_BASE
);
1141 mandoc_msg_setmin(MANDOCERR_STYLE
);
1144 mandoc_msg_setmin(MANDOCERR_WARNING
);
1147 mandoc_msg_setmin(MANDOCERR_ERROR
);
1150 mandoc_msg_setmin(MANDOCERR_UNSUPP
);
1153 mandoc_msg_setmin(MANDOCERR_MAX
);
1156 mandoc_msg_setmin(MANDOCERR_BASE
);
1157 curp
->os_e
= MANDOC_OS_OPENBSD
;
1160 mandoc_msg_setmin(MANDOCERR_BASE
);
1161 curp
->os_e
= MANDOC_OS_NETBSD
;
1164 warnx("-W %s: Bad argument", o
);
1172 spawn_pager(struct tag_files
*tag_files
)
1174 const struct timespec timeout
= { 0, 100000000 }; /* 0.1s */
1175 #define MAX_PAGER_ARGS 16
1176 char *argv
[MAX_PAGER_ARGS
];
1185 pager
= getenv("MANPAGER");
1186 if (pager
== NULL
|| *pager
== '\0')
1187 pager
= getenv("PAGER");
1188 if (pager
== NULL
|| *pager
== '\0')
1190 cp
= mandoc_strdup(pager
);
1193 * Parse the pager command into words.
1194 * Intentionally do not do anything fancy here.
1198 while (argc
+ 5 < MAX_PAGER_ARGS
) {
1200 cp
= strchr(cp
, ' ');
1210 /* For less(1), use the tag file. */
1214 if ((cmdlen
= strlen(argv
[0])) >= 4) {
1215 cp
= argv
[0] + cmdlen
- 4;
1216 if (strcmp(cp
, "less") == 0) {
1217 argv
[argc
++] = mandoc_strdup("-T");
1218 argv
[argc
++] = tag_files
->tfn
;
1219 if (tag_files
->tagname
!= NULL
) {
1220 argv
[argc
++] = mandoc_strdup("-t");
1221 argv
[argc
++] = tag_files
->tagname
;
1228 argv
[argc
++] = tag_files
->ofn
;
1231 switch (pager_pid
= fork()) {
1233 err((int)MANDOCLEVEL_SYSERR
, "fork");
1237 (void)setpgid(pager_pid
, 0);
1238 (void)tcsetpgrp(tag_files
->ofd
, pager_pid
);
1240 if (pledge("stdio rpath tmppath tty proc", NULL
) == -1)
1241 err((int)MANDOCLEVEL_SYSERR
, "pledge");
1243 tag_files
->pager_pid
= pager_pid
;
1247 /* The child process becomes the pager. */
1249 if (dup2(tag_files
->ofd
, STDOUT_FILENO
) == -1)
1250 err((int)MANDOCLEVEL_SYSERR
, "pager stdout");
1251 close(tag_files
->ofd
);
1252 assert(tag_files
->tfd
== -1);
1254 /* Do not start the pager before controlling the terminal. */
1256 while (tcgetpgrp(STDOUT_FILENO
) != getpid())
1257 nanosleep(&timeout
, NULL
);
1259 execvp(argv
[0], argv
);
1260 err((int)MANDOCLEVEL_SYSERR
, "exec %s", argv
[0]);