]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
1 /* $Id: main.c,v 1.340 2019/07/28 19:41:21 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 */
81 struct tag_files
*tag_files
; /* Tagging state variables. */
82 void *outdata
; /* data for output */
84 int wstop
; /* stop after a file with a warning */
85 int had_output
; /* Some output was generated. */
86 enum outt outtype
; /* which output to use */
90 int mandocdb(int, char *[]);
92 static void check_xr(void);
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
*, const char *,
99 struct manpage
**, size_t *);
100 static void outdata_alloc(struct outstate
*, struct manoutput
*);
101 static void parse(struct mparse
*, int, const char *,
102 struct outstate
*, struct manoutput
*);
103 static void passthrough(int, int);
104 static void process_onefile(struct mparse
*, struct manpage
*,
105 int, struct outstate
*, struct manconf
*);
106 static void run_pager(struct tag_files
*);
107 static pid_t
spawn_pager(struct tag_files
*);
108 static void usage(enum argmode
) __attribute__((__noreturn__
));
109 static int woptions(char *, enum mandoc_os
*, int *);
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
[])
119 struct manconf conf
; /* Manpaths and output options. */
120 struct outstate outst
; /* Output state. */
121 struct winsize ws
; /* Result of ioctl(TIOCGWINSZ). */
122 struct mansearch search
; /* Search options. */
123 struct manpage
*res
; /* Complete list of search results. */
124 struct manpage
*resn
; /* Search results for one name. */
125 struct mparse
*mp
; /* Opaque parser object. */
126 const char *conf_file
; /* -C: alternate config file. */
127 const char *os_s
; /* -I: Operating system for display. */
128 const char *progname
, *sec
;
129 char *defpaths
; /* -M: override manpaths. */
130 char *auxpaths
; /* -m: additional manpaths. */
131 char *oarg
; /* -O: output option string. */
132 char *tagarg
; /* -O tag: default value. */
134 size_t ressz
; /* Number of elements in res[]. */
135 size_t resnsz
; /* Number of elements in resn[]. */
137 int options
; /* Parser options. */
138 int show_usage
; /* Invalid argument: give up. */
142 enum mandoc_os os_e
; /* Check base system conventions. */
143 enum outmode outmode
; /* According to command line. */
146 progname
= getprogname();
149 progname
= mandoc_strdup("mandoc");
150 else if ((progname
= strrchr(argv
[0], '/')) == NULL
)
154 setprogname(progname
);
157 mandoc_msg_setoutfile(stderr
);
158 if (strncmp(progname
, "mandocdb", 8) == 0 ||
159 strcmp(progname
, BINM_MAKEWHATIS
) == 0)
160 return mandocdb(argc
, argv
);
163 if (pledge("stdio rpath tmppath tty proc exec", NULL
) == -1) {
164 mandoc_msg(MANDOCERR_PLEDGE
, 0, 0, "%s", strerror(errno
));
165 return mandoc_msg_getrc();
168 #if HAVE_SANDBOX_INIT
169 if (sandbox_init(kSBXProfileNoInternet
, SANDBOX_NAMED
, NULL
) == -1)
170 errx((int)MANDOCLEVEL_SYSERR
, "sandbox_init");
173 /* Search options. */
175 memset(&conf
, 0, sizeof(conf
));
177 defpaths
= auxpaths
= NULL
;
179 memset(&search
, 0, sizeof(struct mansearch
));
180 search
.outkey
= "Nd";
183 if (strcmp(progname
, BINM_MAN
) == 0)
184 search
.argmode
= ARG_NAME
;
185 else if (strcmp(progname
, BINM_APROPOS
) == 0)
186 search
.argmode
= ARG_EXPR
;
187 else if (strcmp(progname
, BINM_WHATIS
) == 0)
188 search
.argmode
= ARG_WORD
;
189 else if (strncmp(progname
, "help", 4) == 0)
190 search
.argmode
= ARG_NAME
;
192 search
.argmode
= ARG_FILE
;
194 /* Parser options. */
196 options
= MPARSE_SO
| MPARSE_UTF8
| MPARSE_LATIN1
;
197 os_e
= MANDOC_OS_OTHER
;
200 /* Formatter options. */
202 memset(&outst
, 0, sizeof(outst
));
203 outst
.tag_files
= NULL
;
204 outst
.outtype
= OUTT_LOCALE
;
208 outmode
= OUTMODE_DEF
;
210 while ((c
= getopt(argc
, argv
,
211 "aC:cfhI:iK:klM:m:O:S:s:T:VW:w")) != -1) {
212 if (c
== 'i' && search
.argmode
== ARG_EXPR
) {
218 outmode
= OUTMODE_ALL
;
227 search
.argmode
= ARG_WORD
;
230 conf
.output
.synopsisonly
= 1;
232 outmode
= OUTMODE_ALL
;
235 if (strncmp(optarg
, "os=", 3) != 0) {
236 mandoc_msg(MANDOCERR_BADARG_BAD
, 0, 0,
238 return mandoc_msg_getrc();
241 mandoc_msg(MANDOCERR_BADARG_DUPE
, 0, 0,
243 return mandoc_msg_getrc();
248 options
&= ~(MPARSE_UTF8
| MPARSE_LATIN1
);
249 if (strcmp(optarg
, "utf-8") == 0)
250 options
|= MPARSE_UTF8
;
251 else if (strcmp(optarg
, "iso-8859-1") == 0)
252 options
|= MPARSE_LATIN1
;
253 else if (strcmp(optarg
, "us-ascii") != 0) {
254 mandoc_msg(MANDOCERR_BADARG_BAD
, 0, 0,
256 return mandoc_msg_getrc();
260 search
.argmode
= ARG_EXPR
;
263 search
.argmode
= ARG_FILE
;
264 outmode
= OUTMODE_ALL
;
276 search
.arch
= optarg
;
282 if (strcmp(optarg
, "ascii") == 0)
283 outst
.outtype
= OUTT_ASCII
;
284 else if (strcmp(optarg
, "lint") == 0) {
285 outst
.outtype
= OUTT_LINT
;
286 mandoc_msg_setoutfile(stdout
);
287 mandoc_msg_setmin(MANDOCERR_BASE
);
288 } else if (strcmp(optarg
, "tree") == 0)
289 outst
.outtype
= OUTT_TREE
;
290 else if (strcmp(optarg
, "man") == 0)
291 outst
.outtype
= OUTT_MAN
;
292 else if (strcmp(optarg
, "html") == 0)
293 outst
.outtype
= OUTT_HTML
;
294 else if (strcmp(optarg
, "markdown") == 0)
295 outst
.outtype
= OUTT_MARKDOWN
;
296 else if (strcmp(optarg
, "utf8") == 0)
297 outst
.outtype
= OUTT_UTF8
;
298 else if (strcmp(optarg
, "locale") == 0)
299 outst
.outtype
= OUTT_LOCALE
;
300 else if (strcmp(optarg
, "ps") == 0)
301 outst
.outtype
= OUTT_PS
;
302 else if (strcmp(optarg
, "pdf") == 0)
303 outst
.outtype
= OUTT_PDF
;
305 mandoc_msg(MANDOCERR_BADARG_BAD
, 0, 0,
307 return mandoc_msg_getrc();
311 if (woptions(optarg
, &os_e
, &outst
.wstop
) == -1)
312 return mandoc_msg_getrc();
315 outmode
= OUTMODE_FLN
;
324 usage(search
.argmode
);
326 /* Postprocess options. */
328 if (outmode
== OUTMODE_DEF
) {
329 switch (search
.argmode
) {
331 outmode
= OUTMODE_ALL
;
335 outmode
= OUTMODE_ONE
;
338 outmode
= OUTMODE_LST
;
344 if (outmode
== OUTMODE_LST
)
345 search
.outkey
= oarg
;
347 while (oarg
!= NULL
) {
348 if (manconf_output(&conf
.output
,
349 strsep(&oarg
, ","), 0) == -1)
350 return mandoc_msg_getrc();
355 if (outst
.outtype
!= OUTT_TREE
|| conf
.output
.noval
== 0)
356 options
|= MPARSE_VALIDATE
;
358 if (outmode
== OUTMODE_FLN
||
359 outmode
== OUTMODE_LST
||
360 !isatty(STDOUT_FILENO
))
363 if (outst
.use_pager
&&
364 (conf
.output
.width
== 0 || conf
.output
.indent
== 0) &&
365 ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &ws
) != -1 &&
367 if (conf
.output
.width
== 0 && ws
.ws_col
< 79)
368 conf
.output
.width
= ws
.ws_col
- 1;
369 if (conf
.output
.indent
== 0 && ws
.ws_col
< 66)
370 conf
.output
.indent
= 3;
374 if (outst
.use_pager
== 0) {
375 if (pledge("stdio rpath", NULL
) == -1) {
376 mandoc_msg(MANDOCERR_PLEDGE
, 0, 0,
377 "%s", strerror(errno
));
378 return mandoc_msg_getrc();
383 /* Parse arguments. */
391 * Quirks for help(1) and man(1),
392 * in particular for a section argument without -s.
395 if (search
.argmode
== ARG_NAME
) {
396 if (*progname
== 'h') {
401 } else if (argc
> 1 &&
402 ((uc
= (unsigned char *)argv
[0]) != NULL
) &&
403 ((isdigit(uc
[0]) && (uc
[1] == '\0' ||
405 (uc
[0] == 'n' && uc
[1] == '\0'))) {
406 search
.sec
= (char *)uc
;
410 if (search
.arch
== NULL
)
411 search
.arch
= getenv("MACHINE");
413 if (search
.arch
== NULL
)
414 search
.arch
= MACHINE
;
416 if (outmode
== OUTMODE_ONE
)
417 search
.firstmatch
= 1;
421 * Use the first argument for -O tag in addition to
422 * using it as a search term for man(1) or apropos(1).
425 if (conf
.output
.tag
!= NULL
&& *conf
.output
.tag
== '\0') {
426 tagarg
= argc
> 0 && search
.argmode
== ARG_EXPR
?
427 strchr(*argv
, '=') : NULL
;
428 conf
.output
.tag
= tagarg
== NULL
? *argv
: tagarg
+ 1;
431 /* Read the configuration file. */
433 if (search
.argmode
!= ARG_FILE
)
434 manconf_parse(&conf
, conf_file
, defpaths
, auxpaths
);
436 /* man(1): Resolve each name individually. */
438 if (search
.argmode
== ARG_NAME
) {
441 for (res
= NULL
, ressz
= 0; argc
> 0; argc
--, argv
++) {
442 (void)mansearch(&search
, &conf
.manpath
,
443 1, argv
, &resn
, &resnsz
);
445 (void)fs_search(&search
, &conf
.manpath
,
446 *argv
, &resn
, &resnsz
);
447 if (resnsz
== 0 && strchr(*argv
, '/') == NULL
) {
448 if (search
.arch
!= NULL
&&
449 arch_valid(search
.arch
, OSENUM
) == 0)
450 warnx("Unknown architecture \"%s\".",
452 else if (search
.sec
!= NULL
)
453 warnx("No entry for %s in "
454 "section %s of the manual.",
457 warnx("No entry for %s in "
458 "the manual.", *argv
);
459 mandoc_msg_setrc(MANDOCLEVEL_BADARG
);
463 if (access(*argv
, R_OK
) == -1) {
464 mandoc_msg_setinfilename(*argv
);
465 mandoc_msg(MANDOCERR_BADARG_BAD
,
466 0, 0, "%s", strerror(errno
));
467 mandoc_msg_setinfilename(NULL
);
471 resn
= mandoc_calloc(resnsz
, sizeof(*res
));
472 resn
->file
= mandoc_strdup(*argv
);
473 resn
->ipath
= SIZE_MAX
;
474 resn
->form
= FORM_SRC
;
476 if (outmode
!= OUTMODE_ONE
|| resnsz
== 1) {
477 res
= mandoc_reallocarray(res
,
478 ressz
+ resnsz
, sizeof(*res
));
479 memcpy(res
+ ressz
, resn
,
480 sizeof(*resn
) * resnsz
);
485 /* Search for the best section. */
488 for (ib
= i
= 0; i
< resnsz
; i
++) {
490 sec
+= strcspn(sec
, "123456789");
492 continue; /* No section at all. */
493 prio
= sec_prios
[sec
[0] - '1'];
494 if (search
.sec
!= NULL
) {
495 ssz
= strlen(search
.sec
);
496 if (strncmp(sec
, search
.sec
, ssz
) == 0)
499 sec
++; /* Prefer without suffix. */
501 prio
+= 10; /* Wrong dir name. */
502 if (search
.sec
!= NULL
&&
503 (strlen(sec
) <= ssz
+ 3 ||
504 strcmp(sec
+ strlen(sec
) - ssz
,
506 prio
+= 20; /* Wrong file ext. */
507 if (prio
>= best_prio
)
512 res
= mandoc_reallocarray(res
, ressz
+ 1,
514 memcpy(res
+ ressz
++, resn
+ ib
, sizeof(*resn
));
517 /* apropos(1), whatis(1): Process the full search expression. */
519 } else if (search
.argmode
!= ARG_FILE
) {
520 if (mansearch(&search
, &conf
.manpath
,
521 argc
, argv
, &res
, &ressz
) == 0)
522 usage(search
.argmode
);
525 warnx("nothing appropriate");
526 mandoc_msg_setrc(MANDOCLEVEL_BADARG
);
530 /* mandoc(1): Take command line arguments as file names. */
533 ressz
= argc
> 0 ? argc
: 1;
534 res
= mandoc_calloc(ressz
, sizeof(*res
));
535 for (i
= 0; i
< ressz
; i
++) {
537 res
[i
].file
= mandoc_strdup(argv
[i
]);
538 res
[i
].ipath
= SIZE_MAX
;
539 res
[i
].form
= FORM_SRC
;
545 for (i
= 0; i
< ressz
; i
++)
549 for (i
= 0; i
< ressz
; i
++)
550 printf("%s - %s\n", res
[i
].names
,
551 res
[i
].output
== NULL
? "" :
558 if (search
.argmode
== ARG_FILE
&& auxpaths
!= NULL
) {
559 if (strcmp(auxpaths
, "doc") == 0)
560 options
|= MPARSE_MDOC
;
561 else if (strcmp(auxpaths
, "an") == 0)
562 options
|= MPARSE_MAN
;
566 mp
= mparse_alloc(options
, os_e
, os_s
);
569 * Remember the original working directory, if possible.
570 * This will be needed if some names on the command line
571 * are page names and some are relative file names.
572 * Do not error out if the current directory is not
573 * readable: Maybe it won't be needed after all.
575 startdir
= open(".", O_RDONLY
| O_DIRECTORY
);
577 for (i
= 0; i
< ressz
; i
++) {
578 process_onefile(mp
, res
+ i
, startdir
, &outst
, &conf
);
579 if (outst
.wstop
&& mandoc_msg_getrc() != MANDOCLEVEL_OK
)
582 if (startdir
!= -1) {
583 (void)fchdir(startdir
);
587 if (outst
.outdata
!= NULL
) {
588 switch (outst
.outtype
) {
590 html_free(outst
.outdata
);
595 ascii_free(outst
.outdata
);
599 pspdf_free(outst
.outdata
);
610 mansearch_free(res
, ressz
);
611 if (search
.argmode
!= ARG_FILE
)
614 if (outst
.tag_files
!= NULL
) {
617 run_pager(outst
.tag_files
);
619 } else if (outst
.had_output
&& outst
.outtype
!= OUTT_LINT
)
620 mandoc_msg_summary();
622 return (int)mandoc_msg_getrc();
626 usage(enum argmode argmode
)
630 fputs("usage: mandoc [-ac] [-I os=name] "
631 "[-K encoding] [-mdoc | -man] [-O options]\n"
632 "\t [-T output] [-W level] [file ...]\n", stderr
);
635 fputs("usage: man [-acfhklw] [-C file] [-M path] "
636 "[-m path] [-S subsection]\n"
637 "\t [[-s] section] name ...\n", stderr
);
640 fputs("usage: whatis [-afk] [-C file] "
641 "[-M path] [-m path] [-O outkey] [-S arch]\n"
642 "\t [-s section] name ...\n", stderr
);
645 fputs("usage: apropos [-afk] [-C file] "
646 "[-M path] [-m path] [-O outkey] [-S arch]\n"
647 "\t [-s section] expression ...\n", stderr
);
650 exit((int)MANDOCLEVEL_BADARG
);
654 fs_lookup(const struct manpaths
*paths
, size_t ipath
,
655 const char *sec
, const char *arch
, const char *name
,
656 struct manpage
**res
, size_t *ressz
)
660 struct manpage
*page
;
666 mandoc_asprintf(&file
, "%s/man%s/%s.%s",
667 paths
->paths
[ipath
], sec
, name
, sec
);
668 if (stat(file
, &sb
) != -1)
672 mandoc_asprintf(&file
, "%s/cat%s/%s.0",
673 paths
->paths
[ipath
], sec
, name
);
674 if (stat(file
, &sb
) != -1) {
681 mandoc_asprintf(&file
, "%s/man%s/%s/%s.%s",
682 paths
->paths
[ipath
], sec
, arch
, name
, sec
);
683 if (stat(file
, &sb
) != -1)
688 mandoc_asprintf(&file
, "%s/man%s/%s.[01-9]*",
689 paths
->paths
[ipath
], sec
, name
);
690 globres
= glob(file
, 0, NULL
, &globinfo
);
691 if (globres
!= 0 && globres
!= GLOB_NOMATCH
)
692 mandoc_msg(MANDOCERR_GLOB
, 0, 0,
693 "%s: %s", file
, strerror(errno
));
696 file
= mandoc_strdup(*globinfo
.gl_pathv
);
699 if (stat(file
, &sb
) != -1)
703 if (res
!= NULL
|| ipath
+ 1 != paths
->sz
)
706 mandoc_asprintf(&file
, "%s.%s", name
, sec
);
707 globres
= stat(file
, &sb
);
712 warnx("outdated mandoc.db lacks %s(%s) entry, run %s %s",
713 name
, sec
, BINM_MAKEWHATIS
, paths
->paths
[ipath
]);
718 *res
= mandoc_reallocarray(*res
, ++*ressz
, sizeof(**res
));
719 page
= *res
+ (*ressz
- 1);
723 page
->bits
= NAME_FILE
& NAME_MASK
;
725 page
->sec
= (*sec
>= '1' && *sec
<= '9') ? *sec
- '1' + 1 : 10;
731 fs_search(const struct mansearch
*cfg
, const struct manpaths
*paths
,
732 const char *name
, struct manpage
**res
, size_t *ressz
)
734 const char *const sections
[] =
735 {"1", "8", "6", "2", "3", "5", "7", "4", "9", "3p"};
736 const size_t nsec
= sizeof(sections
)/sizeof(sections
[0]);
740 assert(cfg
->argmode
== ARG_NAME
);
744 for (ipath
= 0; ipath
< paths
->sz
; ipath
++) {
745 if (cfg
->sec
!= NULL
) {
746 if (fs_lookup(paths
, ipath
, cfg
->sec
, cfg
->arch
,
747 name
, res
, ressz
) != -1 && cfg
->firstmatch
)
750 for (isec
= 0; isec
< nsec
; isec
++)
751 if (fs_lookup(paths
, ipath
, sections
[isec
],
752 cfg
->arch
, name
, res
, ressz
) != -1 &&
761 process_onefile(struct mparse
*mp
, struct manpage
*resp
, int startdir
,
762 struct outstate
*outst
, struct manconf
*conf
)
767 * Changing directories is not needed in ARG_FILE mode.
768 * Do it on a best-effort basis. Even in case of
769 * failure, some functionality may still work.
771 if (resp
->ipath
!= SIZE_MAX
)
772 (void)chdir(conf
->manpath
.paths
[resp
->ipath
]);
773 else if (startdir
!= -1)
774 (void)fchdir(startdir
);
776 mandoc_msg_setinfilename(resp
->file
);
777 if (resp
->file
!= NULL
) {
778 if ((fd
= mparse_open(mp
, resp
->file
)) == -1) {
779 mandoc_msg(resp
->ipath
== SIZE_MAX
?
780 MANDOCERR_BADARG_BAD
: MANDOCERR_OPEN
,
781 0, 0, "%s", strerror(errno
));
782 mandoc_msg_setinfilename(NULL
);
788 if (outst
->use_pager
) {
789 outst
->use_pager
= 0;
790 outst
->tag_files
= tag_init(conf
->output
.tag
);
793 if (outst
->had_output
&& outst
->outtype
<= OUTT_UTF8
) {
794 if (outst
->outdata
== NULL
)
795 outdata_alloc(outst
, &conf
->output
);
796 terminal_sepline(outst
->outdata
);
799 if (resp
->form
== FORM_SRC
)
800 parse(mp
, fd
, resp
->file
, outst
, &conf
->output
);
802 passthrough(fd
, conf
->output
.synopsisonly
);
803 outst
->had_output
= 1;
806 if (ferror(stdout
)) {
807 if (outst
->tag_files
!= NULL
) {
808 mandoc_msg(MANDOCERR_WRITE
, 0, 0, "%s: %s",
809 outst
->tag_files
->ofn
, strerror(errno
));
811 outst
->tag_files
= NULL
;
813 mandoc_msg(MANDOCERR_WRITE
, 0, 0, "%s",
816 mandoc_msg_setinfilename(NULL
);
820 parse(struct mparse
*mp
, int fd
, const char *file
,
821 struct outstate
*outst
, struct manoutput
*outconf
)
824 struct roff_meta
*meta
;
835 mparse_readfd(mp
, fd
, file
);
836 if (fd
!= STDIN_FILENO
)
840 * With -Wstop and warnings or errors of at least the requested
841 * level, do not produce output.
844 if (outst
->wstop
&& mandoc_msg_getrc() != MANDOCLEVEL_OK
)
847 if (outst
->outdata
== NULL
)
848 outdata_alloc(outst
, outconf
);
849 else if (outst
->outtype
== OUTT_HTML
)
853 meta
= mparse_result(mp
);
855 /* Execute the out device, if it exists. */
857 outst
->had_output
= 1;
858 if (meta
->macroset
== MACROSET_MDOC
) {
859 switch (outst
->outtype
) {
861 html_mdoc(outst
->outdata
, meta
);
864 tree_mdoc(outst
->outdata
, meta
);
867 man_mdoc(outst
->outdata
, meta
);
874 terminal_mdoc(outst
->outdata
, meta
);
877 markdown_mdoc(outst
->outdata
, meta
);
883 if (meta
->macroset
== MACROSET_MAN
) {
884 switch (outst
->outtype
) {
886 html_man(outst
->outdata
, meta
);
889 tree_man(outst
->outdata
, meta
);
899 terminal_man(outst
->outdata
, meta
);
905 if (mandoc_msg_getmin() < MANDOCERR_STYLE
)
912 static struct manpaths paths
;
913 struct mansearch search
;
914 struct mandoc_xr
*xr
;
918 manpath_base(&paths
);
920 for (xr
= mandoc_xr_get(); xr
!= NULL
; xr
= xr
->next
) {
924 search
.sec
= xr
->sec
;
925 search
.outkey
= NULL
;
926 search
.argmode
= ARG_NAME
;
927 search
.firstmatch
= 1;
928 if (mansearch(&search
, &paths
, 1, &xr
->name
, NULL
, &sz
))
930 if (fs_search(&search
, &paths
, xr
->name
, NULL
, &sz
) != -1)
933 mandoc_msg(MANDOCERR_XR_BAD
, xr
->line
,
934 xr
->pos
+ 1, "Xr %s %s", xr
->name
, xr
->sec
);
936 mandoc_msg(MANDOCERR_XR_BAD
, xr
->line
,
937 xr
->pos
+ 1, "Xr %s %s (%d times)",
938 xr
->name
, xr
->sec
, xr
->count
);
943 outdata_alloc(struct outstate
*outst
, struct manoutput
*outconf
)
945 switch (outst
->outtype
) {
947 outst
->outdata
= html_alloc(outconf
);
950 outst
->outdata
= utf8_alloc(outconf
);
953 outst
->outdata
= locale_alloc(outconf
);
956 outst
->outdata
= ascii_alloc(outconf
);
959 outst
->outdata
= pdf_alloc(outconf
);
962 outst
->outdata
= ps_alloc(outconf
);
970 passthrough(int fd
, int synopsis_only
)
972 const char synb
[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
973 const char synr
[] = "SYNOPSIS";
978 ssize_t len
, written
;
985 if (fflush(stdout
) == EOF
) {
986 mandoc_msg(MANDOCERR_FFLUSH
, 0, 0, "%s", strerror(errno
));
989 if ((stream
= fdopen(fd
, "r")) == NULL
) {
991 mandoc_msg(MANDOCERR_FDOPEN
, 0, 0, "%s", strerror(errno
));
996 while ((len
= getline(&line
, &linesz
, stream
)) != -1) {
1001 if ( ! isspace((unsigned char)*cp
))
1003 while (isspace((unsigned char)*cp
)) {
1008 if (strcmp(cp
, synb
) == 0 ||
1009 strcmp(cp
, synr
) == 0)
1014 for (; len
> 0; len
-= written
) {
1015 if ((written
= write(STDOUT_FILENO
, cp
, len
)) == -1) {
1016 mandoc_msg(MANDOCERR_WRITE
, 0, 0,
1017 "%s", strerror(errno
));
1023 mandoc_msg(MANDOCERR_GETLINE
, lno
, 0, "%s", strerror(errno
));
1032 woptions(char *arg
, enum mandoc_os
*os_e
, int *wstop
)
1035 const char *toks
[11];
1041 toks
[4] = "warning";
1045 toks
[8] = "openbsd";
1051 switch (getsubopt(&arg
, (char * const *)toks
, &v
)) {
1057 mandoc_msg_setmin(MANDOCERR_BASE
);
1060 mandoc_msg_setmin(MANDOCERR_STYLE
);
1063 mandoc_msg_setmin(MANDOCERR_WARNING
);
1066 mandoc_msg_setmin(MANDOCERR_ERROR
);
1069 mandoc_msg_setmin(MANDOCERR_UNSUPP
);
1072 mandoc_msg_setmin(MANDOCERR_BADARG
);
1075 mandoc_msg_setmin(MANDOCERR_BASE
);
1076 *os_e
= MANDOC_OS_OPENBSD
;
1079 mandoc_msg_setmin(MANDOCERR_BASE
);
1080 *os_e
= MANDOC_OS_NETBSD
;
1083 mandoc_msg(MANDOCERR_BADARG_BAD
, 0, 0, "-W %s", o
);
1091 * Wait until moved to the foreground,
1092 * then fork the pager and wait for the user to close it.
1095 run_pager(struct tag_files
*tag_files
)
1098 pid_t man_pgid
, tc_pgid
;
1099 pid_t pager_pid
, wait_pid
;
1101 man_pgid
= getpgid(0);
1102 tag_files
->tcpgid
= man_pgid
== getpid() ? getpgid(getppid()) :
1108 /* Stop here until moved to the foreground. */
1110 tc_pgid
= tcgetpgrp(tag_files
->ofd
);
1111 if (tc_pgid
!= man_pgid
) {
1112 if (tc_pgid
== pager_pid
) {
1113 (void)tcsetpgrp(tag_files
->ofd
, man_pgid
);
1114 if (signum
== SIGTTIN
)
1117 tag_files
->tcpgid
= tc_pgid
;
1122 /* Once in the foreground, activate the pager. */
1125 (void)tcsetpgrp(tag_files
->ofd
, pager_pid
);
1126 kill(pager_pid
, SIGCONT
);
1128 pager_pid
= spawn_pager(tag_files
);
1130 /* Wait for the pager to stop or exit. */
1132 while ((wait_pid
= waitpid(pager_pid
, &status
,
1133 WUNTRACED
)) == -1 && errno
== EINTR
)
1136 if (wait_pid
== -1) {
1137 mandoc_msg(MANDOCERR_WAIT
, 0, 0,
1138 "%s", strerror(errno
));
1141 if (!WIFSTOPPED(status
))
1144 signum
= WSTOPSIG(status
);
1149 spawn_pager(struct tag_files
*tag_files
)
1151 const struct timespec timeout
= { 0, 100000000 }; /* 0.1s */
1152 #define MAX_PAGER_ARGS 16
1153 char *argv
[MAX_PAGER_ARGS
];
1162 pager
= getenv("MANPAGER");
1163 if (pager
== NULL
|| *pager
== '\0')
1164 pager
= getenv("PAGER");
1165 if (pager
== NULL
|| *pager
== '\0')
1167 cp
= mandoc_strdup(pager
);
1170 * Parse the pager command into words.
1171 * Intentionally do not do anything fancy here.
1175 while (argc
+ 5 < MAX_PAGER_ARGS
) {
1177 cp
= strchr(cp
, ' ');
1187 /* For less(1), use the tag file. */
1191 if (*tag_files
->tfn
!= '\0' && (cmdlen
= strlen(argv
[0])) >= 4) {
1192 cp
= argv
[0] + cmdlen
- 4;
1193 if (strcmp(cp
, "less") == 0) {
1194 argv
[argc
++] = mandoc_strdup("-T");
1195 argv
[argc
++] = tag_files
->tfn
;
1196 if (tag_files
->tagname
!= NULL
) {
1197 argv
[argc
++] = mandoc_strdup("-t");
1198 argv
[argc
++] = tag_files
->tagname
;
1205 argv
[argc
++] = tag_files
->ofn
;
1208 switch (pager_pid
= fork()) {
1210 mandoc_msg(MANDOCERR_FORK
, 0, 0, "%s", strerror(errno
));
1211 exit(mandoc_msg_getrc());
1215 (void)setpgid(pager_pid
, 0);
1216 (void)tcsetpgrp(tag_files
->ofd
, pager_pid
);
1218 if (pledge("stdio rpath tmppath tty proc", NULL
) == -1) {
1219 mandoc_msg(MANDOCERR_PLEDGE
, 0, 0,
1220 "%s", strerror(errno
));
1221 exit(mandoc_msg_getrc());
1224 tag_files
->pager_pid
= pager_pid
;
1228 /* The child process becomes the pager. */
1230 if (dup2(tag_files
->ofd
, STDOUT_FILENO
) == -1) {
1231 mandoc_msg(MANDOCERR_DUP
, 0, 0, "%s", strerror(errno
));
1232 _exit(mandoc_msg_getrc());
1234 close(tag_files
->ofd
);
1235 assert(tag_files
->tfd
== -1);
1237 /* Do not start the pager before controlling the terminal. */
1239 while (tcgetpgrp(STDOUT_FILENO
) != getpid())
1240 nanosleep(&timeout
, NULL
);
1242 execvp(argv
[0], argv
);
1243 mandoc_msg(MANDOCERR_EXEC
, 0, 0, "%s: %s", argv
[0], strerror(errno
));
1244 _exit(mandoc_msg_getrc());