]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
1 /* $Id: main.c,v 1.332 2019/07/19 20:27:25 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 void outdata_alloc(struct curparse
*);
102 static void parse(struct curparse
*, int, const char *);
103 static void passthrough(int, int);
104 static pid_t
spawn_pager(struct tag_files
*);
105 static void usage(enum argmode
) __attribute__((__noreturn__
));
106 static int woptions(struct curparse
*, char *);
108 static const int sec_prios
[] = {1, 4, 5, 8, 6, 3, 7, 2, 9};
109 static char help_arg
[] = "help";
110 static char *help_argv
[] = {help_arg
, NULL
};
114 main(int argc
, char *argv
[])
117 struct mansearch search
;
118 struct curparse curp
;
120 struct tag_files
*tag_files
;
121 struct manpage
*res
, *resp
;
122 const char *progname
, *sec
, *thisarg
;
123 char *conf_file
, *defpaths
, *auxpaths
;
128 enum outmode outmode
;
135 pid_t pager_pid
, tc_pgid
, man_pgid
, pid
;
138 progname
= getprogname();
141 progname
= mandoc_strdup("mandoc");
142 else if ((progname
= strrchr(argv
[0], '/')) == NULL
)
146 setprogname(progname
);
149 mandoc_msg_setoutfile(stderr
);
150 if (strncmp(progname
, "mandocdb", 8) == 0 ||
151 strcmp(progname
, BINM_MAKEWHATIS
) == 0)
152 return mandocdb(argc
, argv
);
155 if (pledge("stdio rpath tmppath tty proc exec", NULL
) == -1) {
156 mandoc_msg(MANDOCERR_PLEDGE
, 0, 0, "%s", strerror(errno
));
157 return mandoc_msg_getrc();
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
.outopts
= &conf
.output
;
191 options
= MPARSE_SO
| MPARSE_UTF8
| MPARSE_LATIN1
;
196 outmode
= OUTMODE_DEF
;
198 while ((c
= getopt(argc
, argv
,
199 "aC:cfhI:iK:klM:m:O:S:s:T:VW:w")) != -1) {
200 if (c
== 'i' && search
.argmode
== ARG_EXPR
) {
206 outmode
= OUTMODE_ALL
;
215 search
.argmode
= ARG_WORD
;
218 conf
.output
.synopsisonly
= 1;
220 outmode
= OUTMODE_ALL
;
223 if (strncmp(optarg
, "os=", 3) != 0) {
224 mandoc_msg(MANDOCERR_BADARG_BAD
, 0, 0,
226 return mandoc_msg_getrc();
228 if (curp
.os_s
!= NULL
) {
229 mandoc_msg(MANDOCERR_BADARG_DUPE
, 0, 0,
231 return mandoc_msg_getrc();
233 curp
.os_s
= mandoc_strdup(optarg
+ 3);
236 options
&= ~(MPARSE_UTF8
| MPARSE_LATIN1
);
237 if (strcmp(optarg
, "utf-8") == 0)
238 options
|= MPARSE_UTF8
;
239 else if (strcmp(optarg
, "iso-8859-1") == 0)
240 options
|= MPARSE_LATIN1
;
241 else if (strcmp(optarg
, "us-ascii") != 0) {
242 mandoc_msg(MANDOCERR_BADARG_BAD
, 0, 0,
244 return mandoc_msg_getrc();
248 search
.argmode
= ARG_EXPR
;
251 search
.argmode
= ARG_FILE
;
252 outmode
= OUTMODE_ALL
;
264 search
.arch
= optarg
;
270 if (strcmp(optarg
, "ascii") == 0)
271 curp
.outtype
= OUTT_ASCII
;
272 else if (strcmp(optarg
, "lint") == 0) {
273 curp
.outtype
= OUTT_LINT
;
274 mandoc_msg_setoutfile(stdout
);
275 mandoc_msg_setmin(MANDOCERR_BASE
);
276 } else if (strcmp(optarg
, "tree") == 0)
277 curp
.outtype
= OUTT_TREE
;
278 else if (strcmp(optarg
, "man") == 0)
279 curp
.outtype
= OUTT_MAN
;
280 else if (strcmp(optarg
, "html") == 0)
281 curp
.outtype
= OUTT_HTML
;
282 else if (strcmp(optarg
, "markdown") == 0)
283 curp
.outtype
= OUTT_MARKDOWN
;
284 else if (strcmp(optarg
, "utf8") == 0)
285 curp
.outtype
= OUTT_UTF8
;
286 else if (strcmp(optarg
, "locale") == 0)
287 curp
.outtype
= OUTT_LOCALE
;
288 else if (strcmp(optarg
, "ps") == 0)
289 curp
.outtype
= OUTT_PS
;
290 else if (strcmp(optarg
, "pdf") == 0)
291 curp
.outtype
= OUTT_PDF
;
293 mandoc_msg(MANDOCERR_BADARG_BAD
, 0, 0,
295 return mandoc_msg_getrc();
299 if (woptions(&curp
, optarg
) == -1)
300 return mandoc_msg_getrc();
303 outmode
= OUTMODE_FLN
;
312 usage(search
.argmode
);
314 /* Postprocess options. */
316 if (outmode
== OUTMODE_DEF
) {
317 switch (search
.argmode
) {
319 outmode
= OUTMODE_ALL
;
323 outmode
= OUTMODE_ONE
;
326 outmode
= OUTMODE_LST
;
332 if (outmode
== OUTMODE_LST
)
333 search
.outkey
= oarg
;
335 while (oarg
!= NULL
) {
336 if (manconf_output(&conf
.output
,
337 strsep(&oarg
, ","), 0) == -1)
338 return mandoc_msg_getrc();
343 if (curp
.outtype
!= OUTT_TREE
|| !curp
.outopts
->noval
)
344 options
|= MPARSE_VALIDATE
;
346 if (outmode
== OUTMODE_FLN
||
347 outmode
== OUTMODE_LST
||
348 !isatty(STDOUT_FILENO
))
352 (conf
.output
.width
== 0 || conf
.output
.indent
== 0) &&
353 ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &ws
) != -1 &&
355 if (conf
.output
.width
== 0 && ws
.ws_col
< 79)
356 conf
.output
.width
= ws
.ws_col
- 1;
357 if (conf
.output
.indent
== 0 && ws
.ws_col
< 66)
358 conf
.output
.indent
= 3;
362 if (use_pager
== 0) {
363 if (pledge("stdio rpath", NULL
) == -1) {
364 mandoc_msg(MANDOCERR_PLEDGE
, 0, 0,
365 "%s", strerror(errno
));
366 return mandoc_msg_getrc();
371 /* Parse arguments. */
381 * and for a man(1) section argument without -s.
384 if (search
.argmode
== ARG_NAME
) {
385 if (*progname
== 'h') {
390 } else if (argc
> 1 &&
391 ((uc
= (unsigned char *)argv
[0]) != NULL
) &&
392 ((isdigit(uc
[0]) && (uc
[1] == '\0' ||
394 (uc
[0] == 'n' && uc
[1] == '\0'))) {
395 search
.sec
= (char *)uc
;
399 if (search
.arch
== NULL
)
400 search
.arch
= getenv("MACHINE");
402 if (search
.arch
== NULL
)
403 search
.arch
= MACHINE
;
408 * Use the first argument for -O tag in addition to
409 * using it as a search term for man(1) or apropos(1).
412 if (conf
.output
.tag
!= NULL
&& *conf
.output
.tag
== '\0') {
413 tagarg
= argc
> 0 && search
.argmode
== ARG_EXPR
?
414 strchr(*argv
, '=') : NULL
;
415 conf
.output
.tag
= tagarg
== NULL
? *argv
: tagarg
+ 1;
418 /* man(1), whatis(1), apropos(1) */
420 if (search
.argmode
!= ARG_FILE
) {
421 if (search
.argmode
== ARG_NAME
&&
422 outmode
== OUTMODE_ONE
)
423 search
.firstmatch
= 1;
425 /* Access the mandoc database. */
427 manconf_parse(&conf
, conf_file
, defpaths
, auxpaths
);
428 if ( ! mansearch(&search
, &conf
.manpath
,
429 argc
, argv
, &res
, &sz
))
430 usage(search
.argmode
);
432 if (sz
== 0 && search
.argmode
== ARG_NAME
)
433 (void)fs_search(&search
, &conf
.manpath
,
434 argc
, argv
, &res
, &sz
);
436 if (search
.argmode
== ARG_NAME
) {
437 for (c
= 0; c
< argc
; c
++) {
438 if (strchr(argv
[c
], '/') == NULL
)
440 if (access(argv
[c
], R_OK
) == -1) {
441 mandoc_msg_setinfilename(argv
[c
]);
442 mandoc_msg(MANDOCERR_BADARG_BAD
,
443 0, 0, "%s", strerror(errno
));
444 mandoc_msg_setinfilename(NULL
);
447 res
= mandoc_reallocarray(res
,
448 sz
+ 1, sizeof(*res
));
449 res
[sz
].file
= mandoc_strdup(argv
[c
]);
450 res
[sz
].names
= NULL
;
451 res
[sz
].output
= NULL
;
453 res
[sz
].ipath
= SIZE_MAX
;
455 res
[sz
].form
= FORM_SRC
;
461 if (search
.argmode
!= ARG_NAME
)
462 warnx("nothing appropriate");
463 mandoc_msg_setrc(MANDOCLEVEL_BADARG
);
468 * For standard man(1) and -a output mode,
469 * prepare for copying filename pointers
470 * into the program parameter array.
473 if (outmode
== OUTMODE_ONE
) {
476 } else if (outmode
== OUTMODE_ALL
)
479 /* Iterate all matching manuals. */
482 for (i
= 0; i
< sz
; i
++) {
483 if (outmode
== OUTMODE_FLN
)
485 else if (outmode
== OUTMODE_LST
)
486 printf("%s - %s\n", res
[i
].names
,
487 res
[i
].output
== NULL
? "" :
489 else if (outmode
== OUTMODE_ONE
) {
490 /* Search for the best section. */
492 sec
+= strcspn(sec
, "123456789");
494 continue; /* No section at all. */
495 prio
= sec_prios
[sec
[0] - '1'];
496 if (search
.sec
!= NULL
) {
497 ssz
= strlen(search
.sec
);
498 if (strncmp(sec
, search
.sec
, ssz
) == 0)
501 sec
++; /* Prefer without suffix. */
503 prio
+= 10; /* Wrong dir name. */
504 if (search
.sec
!= NULL
&&
505 (strlen(sec
) <= ssz
+ 3 ||
506 strcmp(sec
+ strlen(sec
) - ssz
,
508 prio
+= 20; /* Wrong file ext. */
509 if (prio
>= best_prio
)
517 * For man(1), -a and -i output mode, fall through
518 * to the main mandoc(1) code iterating files
519 * and running the parsers on each of them.
522 if (outmode
== OUTMODE_FLN
|| outmode
== OUTMODE_LST
)
530 if (pledge("stdio rpath tmppath tty proc exec", NULL
) == -1) {
531 mandoc_msg(MANDOCERR_PLEDGE
, 0, 0,
532 "%s", strerror(errno
));
533 return mandoc_msg_getrc();
536 if (pledge("stdio rpath", NULL
) == -1) {
537 mandoc_msg(MANDOCERR_PLEDGE
, 0, 0,
538 "%s", strerror(errno
));
539 return mandoc_msg_getrc();
544 if (search
.argmode
== ARG_FILE
&& auxpaths
!= NULL
) {
545 if (strcmp(auxpaths
, "doc") == 0)
546 options
|= MPARSE_MDOC
;
547 else if (strcmp(auxpaths
, "an") == 0)
548 options
|= MPARSE_MAN
;
552 curp
.mp
= mparse_alloc(options
, curp
.os_e
, curp
.os_s
);
556 tag_files
= tag_init();
557 if (tag_files
!= NULL
)
558 tag_files
->tagname
= conf
.output
.tag
;
561 mandoc_msg_setinfilename(thisarg
);
562 parse(&curp
, STDIN_FILENO
, thisarg
);
563 mandoc_msg_setinfilename(NULL
);
567 * Remember the original working directory, if possible.
568 * This will be needed if some names on the command line
569 * are page names and some are relative file names.
570 * Do not error out if the current directory is not
571 * readable: Maybe it won't be needed after all.
573 startdir
= open(".", O_RDONLY
| O_DIRECTORY
);
578 * Changing directories is not needed in ARG_FILE mode.
579 * Do it on a best-effort basis. Even in case of
580 * failure, some functionality may still work.
583 if (resp
->ipath
!= SIZE_MAX
)
584 (void)chdir(conf
.manpath
.paths
[resp
->ipath
]);
585 else if (startdir
!= -1)
586 (void)fchdir(startdir
);
587 thisarg
= resp
->file
;
591 mandoc_msg_setinfilename(thisarg
);
592 fd
= mparse_open(curp
.mp
, thisarg
);
596 tag_files
= tag_init();
597 if (tag_files
!= NULL
)
598 tag_files
->tagname
= conf
.output
.tag
;
601 if (resp
== NULL
|| resp
->form
== FORM_SRC
)
602 parse(&curp
, fd
, thisarg
);
604 passthrough(fd
, conf
.output
.synopsisonly
);
606 if (ferror(stdout
)) {
607 if (tag_files
!= NULL
) {
608 mandoc_msg(MANDOCERR_WRITE
, 0, 0,
609 "%s: %s", tag_files
->ofn
,
614 mandoc_msg(MANDOCERR_WRITE
, 0, 0,
615 "%s", strerror(errno
));
619 if (argc
> 1 && curp
.outtype
<= OUTT_UTF8
) {
620 if (curp
.outdata
== NULL
)
621 outdata_alloc(&curp
);
622 terminal_sepline(curp
.outdata
);
625 mandoc_msg(resp
== NULL
? MANDOCERR_BADARG_BAD
:
626 MANDOCERR_OPEN
, 0, 0, "%s", strerror(errno
));
628 mandoc_msg_setinfilename(NULL
);
630 if (curp
.wstop
&& mandoc_msg_getrc() != MANDOCLEVEL_OK
)
638 mparse_reset(curp
.mp
);
640 if (startdir
!= -1) {
641 (void)fchdir(startdir
);
645 if (curp
.outdata
!= NULL
) {
646 switch (curp
.outtype
) {
648 html_free(curp
.outdata
);
653 ascii_free(curp
.outdata
);
657 pspdf_free(curp
.outdata
);
664 mparse_free(curp
.mp
);
668 if (search
.argmode
!= ARG_FILE
) {
670 mansearch_free(res
, sz
);
676 * When using a pager, finish writing both temporary files,
677 * fork it, wait for the user to close it, and clean up.
680 if (tag_files
!= NULL
) {
683 man_pgid
= getpgid(0);
684 tag_files
->tcpgid
= man_pgid
== getpid() ?
685 getpgid(getppid()) : man_pgid
;
690 /* Stop here until moved to the foreground. */
692 tc_pgid
= tcgetpgrp(tag_files
->ofd
);
693 if (tc_pgid
!= man_pgid
) {
694 if (tc_pgid
== pager_pid
) {
695 (void)tcsetpgrp(tag_files
->ofd
,
697 if (signum
== SIGTTIN
)
700 tag_files
->tcpgid
= tc_pgid
;
705 /* Once in the foreground, activate the pager. */
708 (void)tcsetpgrp(tag_files
->ofd
, pager_pid
);
709 kill(pager_pid
, SIGCONT
);
711 pager_pid
= spawn_pager(tag_files
);
713 /* Wait for the pager to stop or exit. */
715 while ((pid
= waitpid(pager_pid
, &status
,
716 WUNTRACED
)) == -1 && errno
== EINTR
)
720 mandoc_msg(MANDOCERR_WAIT
, 0, 0,
721 "%s", strerror(errno
));
724 if (!WIFSTOPPED(status
))
727 signum
= WSTOPSIG(status
);
730 } else if (curp
.outtype
!= OUTT_LINT
&&
731 (search
.argmode
== ARG_FILE
|| sz
> 0))
732 mandoc_msg_summary();
734 return (int)mandoc_msg_getrc();
738 usage(enum argmode argmode
)
742 fputs("usage: mandoc [-ac] [-I os=name] "
743 "[-K encoding] [-mdoc | -man] [-O options]\n"
744 "\t [-T output] [-W level] [file ...]\n", stderr
);
747 fputs("usage: man [-acfhklw] [-C file] [-M path] "
748 "[-m path] [-S subsection]\n"
749 "\t [[-s] section] name ...\n", stderr
);
752 fputs("usage: whatis [-afk] [-C file] "
753 "[-M path] [-m path] [-O outkey] [-S arch]\n"
754 "\t [-s section] name ...\n", stderr
);
757 fputs("usage: apropos [-afk] [-C file] "
758 "[-M path] [-m path] [-O outkey] [-S arch]\n"
759 "\t [-s section] expression ...\n", stderr
);
762 exit((int)MANDOCLEVEL_BADARG
);
766 fs_lookup(const struct manpaths
*paths
, size_t ipath
,
767 const char *sec
, const char *arch
, const char *name
,
768 struct manpage
**res
, size_t *ressz
)
772 struct manpage
*page
;
778 mandoc_asprintf(&file
, "%s/man%s/%s.%s",
779 paths
->paths
[ipath
], sec
, name
, sec
);
780 if (stat(file
, &sb
) != -1)
784 mandoc_asprintf(&file
, "%s/cat%s/%s.0",
785 paths
->paths
[ipath
], sec
, name
);
786 if (stat(file
, &sb
) != -1) {
793 mandoc_asprintf(&file
, "%s/man%s/%s/%s.%s",
794 paths
->paths
[ipath
], sec
, arch
, name
, sec
);
795 if (stat(file
, &sb
) != -1)
800 mandoc_asprintf(&file
, "%s/man%s/%s.[01-9]*",
801 paths
->paths
[ipath
], sec
, name
);
802 globres
= glob(file
, 0, NULL
, &globinfo
);
803 if (globres
!= 0 && globres
!= GLOB_NOMATCH
)
804 mandoc_msg(MANDOCERR_GLOB
, 0, 0,
805 "%s: %s", file
, strerror(errno
));
808 file
= mandoc_strdup(*globinfo
.gl_pathv
);
811 if (stat(file
, &sb
) != -1)
815 if (res
!= NULL
|| ipath
+ 1 != paths
->sz
)
818 mandoc_asprintf(&file
, "%s.%s", name
, sec
);
819 globres
= stat(file
, &sb
);
824 warnx("outdated mandoc.db lacks %s(%s) entry, run %s %s",
825 name
, sec
, BINM_MAKEWHATIS
, paths
->paths
[ipath
]);
830 *res
= mandoc_reallocarray(*res
, ++*ressz
, sizeof(**res
));
831 page
= *res
+ (*ressz
- 1);
835 page
->bits
= NAME_FILE
& NAME_MASK
;
837 page
->sec
= (*sec
>= '1' && *sec
<= '9') ? *sec
- '1' + 1 : 10;
843 fs_search(const struct mansearch
*cfg
, const struct manpaths
*paths
,
844 int argc
, char **argv
, struct manpage
**res
, size_t *ressz
)
846 const char *const sections
[] =
847 {"1", "8", "6", "2", "3", "5", "7", "4", "9", "3p"};
848 const size_t nsec
= sizeof(sections
)/sizeof(sections
[0]);
850 size_t ipath
, isec
, lastsz
;
852 assert(cfg
->argmode
== ARG_NAME
);
858 for (ipath
= 0; ipath
< paths
->sz
; ipath
++) {
859 if (cfg
->sec
!= NULL
) {
860 if (fs_lookup(paths
, ipath
, cfg
->sec
,
861 cfg
->arch
, *argv
, res
, ressz
) != -1 &&
864 } else for (isec
= 0; isec
< nsec
; isec
++)
865 if (fs_lookup(paths
, ipath
, sections
[isec
],
866 cfg
->arch
, *argv
, res
, ressz
) != -1 &&
870 if (res
!= NULL
&& *ressz
== lastsz
&&
871 strchr(*argv
, '/') == NULL
) {
872 if (cfg
->arch
!= NULL
&&
873 arch_valid(cfg
->arch
, OSENUM
) == 0)
874 warnx("Unknown architecture \"%s\".",
876 else if (cfg
->sec
== NULL
)
877 warnx("No entry for %s in the manual.",
880 warnx("No entry for %s in section %s "
881 "of the manual.", *argv
, cfg
->sec
);
891 parse(struct curparse
*curp
, int fd
, const char *file
)
893 struct roff_meta
*meta
;
895 /* Begin by parsing the file itself. */
900 mparse_readfd(curp
->mp
, fd
, file
);
901 if (fd
!= STDIN_FILENO
)
905 * With -Wstop and warnings or errors of at least the requested
906 * level, do not produce output.
909 if (curp
->wstop
&& mandoc_msg_getrc() != MANDOCLEVEL_OK
)
912 if (curp
->outdata
== NULL
)
914 else if (curp
->outtype
== OUTT_HTML
)
918 meta
= mparse_result(curp
->mp
);
920 /* Execute the out device, if it exists. */
922 if (meta
->macroset
== MACROSET_MDOC
) {
923 switch (curp
->outtype
) {
925 html_mdoc(curp
->outdata
, meta
);
928 tree_mdoc(curp
->outdata
, meta
);
931 man_mdoc(curp
->outdata
, meta
);
938 terminal_mdoc(curp
->outdata
, meta
);
941 markdown_mdoc(curp
->outdata
, meta
);
947 if (meta
->macroset
== MACROSET_MAN
) {
948 switch (curp
->outtype
) {
950 html_man(curp
->outdata
, meta
);
953 tree_man(curp
->outdata
, meta
);
956 mparse_copy(curp
->mp
);
963 terminal_man(curp
->outdata
, meta
);
969 if (mandoc_msg_getmin() < MANDOCERR_STYLE
)
976 static struct manpaths paths
;
977 struct mansearch search
;
978 struct mandoc_xr
*xr
;
982 manpath_base(&paths
);
984 for (xr
= mandoc_xr_get(); xr
!= NULL
; xr
= xr
->next
) {
988 search
.sec
= xr
->sec
;
989 search
.outkey
= NULL
;
990 search
.argmode
= ARG_NAME
;
991 search
.firstmatch
= 1;
992 if (mansearch(&search
, &paths
, 1, &xr
->name
, NULL
, &sz
))
994 if (fs_search(&search
, &paths
, 1, &xr
->name
, NULL
, &sz
) != -1)
997 mandoc_msg(MANDOCERR_XR_BAD
, xr
->line
,
998 xr
->pos
+ 1, "Xr %s %s", xr
->name
, xr
->sec
);
1000 mandoc_msg(MANDOCERR_XR_BAD
, xr
->line
,
1001 xr
->pos
+ 1, "Xr %s %s (%d times)",
1002 xr
->name
, xr
->sec
, xr
->count
);
1007 outdata_alloc(struct curparse
*curp
)
1009 switch (curp
->outtype
) {
1011 curp
->outdata
= html_alloc(curp
->outopts
);
1014 curp
->outdata
= utf8_alloc(curp
->outopts
);
1017 curp
->outdata
= locale_alloc(curp
->outopts
);
1020 curp
->outdata
= ascii_alloc(curp
->outopts
);
1023 curp
->outdata
= pdf_alloc(curp
->outopts
);
1026 curp
->outdata
= ps_alloc(curp
->outopts
);
1034 passthrough(int fd
, int synopsis_only
)
1036 const char synb
[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
1037 const char synr
[] = "SYNOPSIS";
1042 ssize_t len
, written
;
1049 if (fflush(stdout
) == EOF
) {
1050 mandoc_msg(MANDOCERR_FFLUSH
, 0, 0, "%s", strerror(errno
));
1053 if ((stream
= fdopen(fd
, "r")) == NULL
) {
1055 mandoc_msg(MANDOCERR_FDOPEN
, 0, 0, "%s", strerror(errno
));
1060 while ((len
= getline(&line
, &linesz
, stream
)) != -1) {
1063 if (synopsis_only
) {
1065 if ( ! isspace((unsigned char)*cp
))
1067 while (isspace((unsigned char)*cp
)) {
1072 if (strcmp(cp
, synb
) == 0 ||
1073 strcmp(cp
, synr
) == 0)
1078 for (; len
> 0; len
-= written
) {
1079 if ((written
= write(STDOUT_FILENO
, cp
, len
)) == -1) {
1080 mandoc_msg(MANDOCERR_WRITE
, 0, 0,
1081 "%s", strerror(errno
));
1087 mandoc_msg(MANDOCERR_GETLINE
, lno
, 0, "%s", strerror(errno
));
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 mandoc_msg_setmin(MANDOCERR_BASE
);
1124 mandoc_msg_setmin(MANDOCERR_STYLE
);
1127 mandoc_msg_setmin(MANDOCERR_WARNING
);
1130 mandoc_msg_setmin(MANDOCERR_ERROR
);
1133 mandoc_msg_setmin(MANDOCERR_UNSUPP
);
1136 mandoc_msg_setmin(MANDOCERR_BADARG
);
1139 mandoc_msg_setmin(MANDOCERR_BASE
);
1140 curp
->os_e
= MANDOC_OS_OPENBSD
;
1143 mandoc_msg_setmin(MANDOCERR_BASE
);
1144 curp
->os_e
= MANDOC_OS_NETBSD
;
1147 mandoc_msg(MANDOCERR_BADARG_BAD
, 0, 0, "-W %s", o
);
1155 spawn_pager(struct tag_files
*tag_files
)
1157 const struct timespec timeout
= { 0, 100000000 }; /* 0.1s */
1158 #define MAX_PAGER_ARGS 16
1159 char *argv
[MAX_PAGER_ARGS
];
1168 pager
= getenv("MANPAGER");
1169 if (pager
== NULL
|| *pager
== '\0')
1170 pager
= getenv("PAGER");
1171 if (pager
== NULL
|| *pager
== '\0')
1173 cp
= mandoc_strdup(pager
);
1176 * Parse the pager command into words.
1177 * Intentionally do not do anything fancy here.
1181 while (argc
+ 5 < MAX_PAGER_ARGS
) {
1183 cp
= strchr(cp
, ' ');
1193 /* For less(1), use the tag file. */
1197 if (*tag_files
->tfn
!= '\0' && (cmdlen
= strlen(argv
[0])) >= 4) {
1198 cp
= argv
[0] + cmdlen
- 4;
1199 if (strcmp(cp
, "less") == 0) {
1200 argv
[argc
++] = mandoc_strdup("-T");
1201 argv
[argc
++] = tag_files
->tfn
;
1202 if (tag_files
->tagname
!= NULL
) {
1203 argv
[argc
++] = mandoc_strdup("-t");
1204 argv
[argc
++] = tag_files
->tagname
;
1211 argv
[argc
++] = tag_files
->ofn
;
1214 switch (pager_pid
= fork()) {
1216 mandoc_msg(MANDOCERR_FORK
, 0, 0, "%s", strerror(errno
));
1217 exit(mandoc_msg_getrc());
1221 (void)setpgid(pager_pid
, 0);
1222 (void)tcsetpgrp(tag_files
->ofd
, pager_pid
);
1224 if (pledge("stdio rpath tmppath tty proc", NULL
) == -1) {
1225 mandoc_msg(MANDOCERR_PLEDGE
, 0, 0,
1226 "%s", strerror(errno
));
1227 exit(mandoc_msg_getrc());
1230 tag_files
->pager_pid
= pager_pid
;
1234 /* The child process becomes the pager. */
1236 if (dup2(tag_files
->ofd
, STDOUT_FILENO
) == -1) {
1237 mandoc_msg(MANDOCERR_DUP
, 0, 0, "%s", strerror(errno
));
1238 _exit(mandoc_msg_getrc());
1240 close(tag_files
->ofd
);
1241 assert(tag_files
->tfd
== -1);
1243 /* Do not start the pager before controlling the terminal. */
1245 while (tcgetpgrp(STDOUT_FILENO
) != getpid())
1246 nanosleep(&timeout
, NULL
);
1248 execvp(argv
[0], argv
);
1249 mandoc_msg(MANDOCERR_EXEC
, 0, 0, "%s: %s", argv
[0], strerror(errno
));
1250 _exit(mandoc_msg_getrc());