]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
1 /* $Id: main.c,v 1.339 2019/07/28 18:36:06 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
*, int, 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 1, argv
, &resn
, &resnsz
);
448 if (strchr(*argv
, '/') == NULL
) {
449 mandoc_msg_setrc(MANDOCLEVEL_BADARG
);
452 if (access(*argv
, R_OK
) == -1) {
453 mandoc_msg_setinfilename(*argv
);
454 mandoc_msg(MANDOCERR_BADARG_BAD
,
455 0, 0, "%s", strerror(errno
));
456 mandoc_msg_setinfilename(NULL
);
460 resn
= mandoc_calloc(resnsz
, sizeof(*res
));
461 resn
->file
= mandoc_strdup(*argv
);
462 resn
->ipath
= SIZE_MAX
;
463 resn
->form
= FORM_SRC
;
465 if (outmode
!= OUTMODE_ONE
|| resnsz
== 1) {
466 res
= mandoc_reallocarray(res
,
467 ressz
+ resnsz
, sizeof(*res
));
468 memcpy(res
+ ressz
, resn
,
469 sizeof(*resn
) * resnsz
);
474 /* Search for the best section. */
477 for (ib
= i
= 0; i
< resnsz
; i
++) {
479 sec
+= strcspn(sec
, "123456789");
481 continue; /* No section at all. */
482 prio
= sec_prios
[sec
[0] - '1'];
483 if (search
.sec
!= NULL
) {
484 ssz
= strlen(search
.sec
);
485 if (strncmp(sec
, search
.sec
, ssz
) == 0)
488 sec
++; /* Prefer without suffix. */
490 prio
+= 10; /* Wrong dir name. */
491 if (search
.sec
!= NULL
&&
492 (strlen(sec
) <= ssz
+ 3 ||
493 strcmp(sec
+ strlen(sec
) - ssz
,
495 prio
+= 20; /* Wrong file ext. */
496 if (prio
>= best_prio
)
501 res
= mandoc_reallocarray(res
, ressz
+ 1,
503 memcpy(res
+ ressz
++, resn
+ ib
, sizeof(*resn
));
506 /* apropos(1), whatis(1): Process the full search expression. */
508 } else if (search
.argmode
!= ARG_FILE
) {
509 if (mansearch(&search
, &conf
.manpath
,
510 argc
, argv
, &res
, &ressz
) == 0)
511 usage(search
.argmode
);
514 warnx("nothing appropriate");
515 mandoc_msg_setrc(MANDOCLEVEL_BADARG
);
519 /* mandoc(1): Take command line arguments as file names. */
522 ressz
= argc
> 0 ? argc
: 1;
523 res
= mandoc_calloc(ressz
, sizeof(*res
));
524 for (i
= 0; i
< ressz
; i
++) {
526 res
[i
].file
= mandoc_strdup(argv
[i
]);
527 res
[i
].ipath
= SIZE_MAX
;
528 res
[i
].form
= FORM_SRC
;
534 for (i
= 0; i
< ressz
; i
++)
538 for (i
= 0; i
< ressz
; i
++)
539 printf("%s - %s\n", res
[i
].names
,
540 res
[i
].output
== NULL
? "" :
547 if (search
.argmode
== ARG_FILE
&& auxpaths
!= NULL
) {
548 if (strcmp(auxpaths
, "doc") == 0)
549 options
|= MPARSE_MDOC
;
550 else if (strcmp(auxpaths
, "an") == 0)
551 options
|= MPARSE_MAN
;
555 mp
= mparse_alloc(options
, os_e
, os_s
);
558 * Remember the original working directory, if possible.
559 * This will be needed if some names on the command line
560 * are page names and some are relative file names.
561 * Do not error out if the current directory is not
562 * readable: Maybe it won't be needed after all.
564 startdir
= open(".", O_RDONLY
| O_DIRECTORY
);
566 for (i
= 0; i
< ressz
; i
++) {
567 process_onefile(mp
, res
+ i
, startdir
, &outst
, &conf
);
568 if (outst
.wstop
&& mandoc_msg_getrc() != MANDOCLEVEL_OK
)
571 if (startdir
!= -1) {
572 (void)fchdir(startdir
);
576 if (outst
.outdata
!= NULL
) {
577 switch (outst
.outtype
) {
579 html_free(outst
.outdata
);
584 ascii_free(outst
.outdata
);
588 pspdf_free(outst
.outdata
);
599 mansearch_free(res
, ressz
);
600 if (search
.argmode
!= ARG_FILE
)
603 if (outst
.tag_files
!= NULL
) {
606 run_pager(outst
.tag_files
);
608 } else if (outst
.had_output
&& outst
.outtype
!= OUTT_LINT
)
609 mandoc_msg_summary();
611 return (int)mandoc_msg_getrc();
615 usage(enum argmode argmode
)
619 fputs("usage: mandoc [-ac] [-I os=name] "
620 "[-K encoding] [-mdoc | -man] [-O options]\n"
621 "\t [-T output] [-W level] [file ...]\n", stderr
);
624 fputs("usage: man [-acfhklw] [-C file] [-M path] "
625 "[-m path] [-S subsection]\n"
626 "\t [[-s] section] name ...\n", stderr
);
629 fputs("usage: whatis [-afk] [-C file] "
630 "[-M path] [-m path] [-O outkey] [-S arch]\n"
631 "\t [-s section] name ...\n", stderr
);
634 fputs("usage: apropos [-afk] [-C file] "
635 "[-M path] [-m path] [-O outkey] [-S arch]\n"
636 "\t [-s section] expression ...\n", stderr
);
639 exit((int)MANDOCLEVEL_BADARG
);
643 fs_lookup(const struct manpaths
*paths
, size_t ipath
,
644 const char *sec
, const char *arch
, const char *name
,
645 struct manpage
**res
, size_t *ressz
)
649 struct manpage
*page
;
655 mandoc_asprintf(&file
, "%s/man%s/%s.%s",
656 paths
->paths
[ipath
], sec
, name
, sec
);
657 if (stat(file
, &sb
) != -1)
661 mandoc_asprintf(&file
, "%s/cat%s/%s.0",
662 paths
->paths
[ipath
], sec
, name
);
663 if (stat(file
, &sb
) != -1) {
670 mandoc_asprintf(&file
, "%s/man%s/%s/%s.%s",
671 paths
->paths
[ipath
], sec
, arch
, name
, sec
);
672 if (stat(file
, &sb
) != -1)
677 mandoc_asprintf(&file
, "%s/man%s/%s.[01-9]*",
678 paths
->paths
[ipath
], sec
, name
);
679 globres
= glob(file
, 0, NULL
, &globinfo
);
680 if (globres
!= 0 && globres
!= GLOB_NOMATCH
)
681 mandoc_msg(MANDOCERR_GLOB
, 0, 0,
682 "%s: %s", file
, strerror(errno
));
685 file
= mandoc_strdup(*globinfo
.gl_pathv
);
688 if (stat(file
, &sb
) != -1)
692 if (res
!= NULL
|| ipath
+ 1 != paths
->sz
)
695 mandoc_asprintf(&file
, "%s.%s", name
, sec
);
696 globres
= stat(file
, &sb
);
701 warnx("outdated mandoc.db lacks %s(%s) entry, run %s %s",
702 name
, sec
, BINM_MAKEWHATIS
, paths
->paths
[ipath
]);
707 *res
= mandoc_reallocarray(*res
, ++*ressz
, sizeof(**res
));
708 page
= *res
+ (*ressz
- 1);
712 page
->bits
= NAME_FILE
& NAME_MASK
;
714 page
->sec
= (*sec
>= '1' && *sec
<= '9') ? *sec
- '1' + 1 : 10;
720 fs_search(const struct mansearch
*cfg
, const struct manpaths
*paths
,
721 int argc
, char **argv
, struct manpage
**res
, size_t *ressz
)
723 const char *const sections
[] =
724 {"1", "8", "6", "2", "3", "5", "7", "4", "9", "3p"};
725 const size_t nsec
= sizeof(sections
)/sizeof(sections
[0]);
727 size_t ipath
, isec
, lastsz
;
729 assert(cfg
->argmode
== ARG_NAME
);
735 for (ipath
= 0; ipath
< paths
->sz
; ipath
++) {
736 if (cfg
->sec
!= NULL
) {
737 if (fs_lookup(paths
, ipath
, cfg
->sec
,
738 cfg
->arch
, *argv
, res
, ressz
) != -1 &&
741 } else for (isec
= 0; isec
< nsec
; isec
++)
742 if (fs_lookup(paths
, ipath
, sections
[isec
],
743 cfg
->arch
, *argv
, res
, ressz
) != -1 &&
747 if (res
!= NULL
&& *ressz
== lastsz
&&
748 strchr(*argv
, '/') == NULL
) {
749 if (cfg
->arch
!= NULL
&&
750 arch_valid(cfg
->arch
, OSENUM
) == 0)
751 warnx("Unknown architecture \"%s\".",
753 else if (cfg
->sec
== NULL
)
754 warnx("No entry for %s in the manual.",
757 warnx("No entry for %s in section %s "
758 "of the manual.", *argv
, cfg
->sec
);
768 process_onefile(struct mparse
*mp
, struct manpage
*resp
, int startdir
,
769 struct outstate
*outst
, struct manconf
*conf
)
774 * Changing directories is not needed in ARG_FILE mode.
775 * Do it on a best-effort basis. Even in case of
776 * failure, some functionality may still work.
778 if (resp
->ipath
!= SIZE_MAX
)
779 (void)chdir(conf
->manpath
.paths
[resp
->ipath
]);
780 else if (startdir
!= -1)
781 (void)fchdir(startdir
);
783 mandoc_msg_setinfilename(resp
->file
);
784 if (resp
->file
!= NULL
) {
785 if ((fd
= mparse_open(mp
, resp
->file
)) == -1) {
786 mandoc_msg(resp
->ipath
== SIZE_MAX
?
787 MANDOCERR_BADARG_BAD
: MANDOCERR_OPEN
,
788 0, 0, "%s", strerror(errno
));
789 mandoc_msg_setinfilename(NULL
);
795 if (outst
->use_pager
) {
796 outst
->use_pager
= 0;
797 outst
->tag_files
= tag_init(conf
->output
.tag
);
800 if (outst
->had_output
&& outst
->outtype
<= OUTT_UTF8
) {
801 if (outst
->outdata
== NULL
)
802 outdata_alloc(outst
, &conf
->output
);
803 terminal_sepline(outst
->outdata
);
806 if (resp
->form
== FORM_SRC
)
807 parse(mp
, fd
, resp
->file
, outst
, &conf
->output
);
809 passthrough(fd
, conf
->output
.synopsisonly
);
810 outst
->had_output
= 1;
813 if (ferror(stdout
)) {
814 if (outst
->tag_files
!= NULL
) {
815 mandoc_msg(MANDOCERR_WRITE
, 0, 0, "%s: %s",
816 outst
->tag_files
->ofn
, strerror(errno
));
818 outst
->tag_files
= NULL
;
820 mandoc_msg(MANDOCERR_WRITE
, 0, 0, "%s",
823 mandoc_msg_setinfilename(NULL
);
827 parse(struct mparse
*mp
, int fd
, const char *file
,
828 struct outstate
*outst
, struct manoutput
*outconf
)
831 struct roff_meta
*meta
;
842 mparse_readfd(mp
, fd
, file
);
843 if (fd
!= STDIN_FILENO
)
847 * With -Wstop and warnings or errors of at least the requested
848 * level, do not produce output.
851 if (outst
->wstop
&& mandoc_msg_getrc() != MANDOCLEVEL_OK
)
854 if (outst
->outdata
== NULL
)
855 outdata_alloc(outst
, outconf
);
856 else if (outst
->outtype
== OUTT_HTML
)
860 meta
= mparse_result(mp
);
862 /* Execute the out device, if it exists. */
864 outst
->had_output
= 1;
865 if (meta
->macroset
== MACROSET_MDOC
) {
866 switch (outst
->outtype
) {
868 html_mdoc(outst
->outdata
, meta
);
871 tree_mdoc(outst
->outdata
, meta
);
874 man_mdoc(outst
->outdata
, meta
);
881 terminal_mdoc(outst
->outdata
, meta
);
884 markdown_mdoc(outst
->outdata
, meta
);
890 if (meta
->macroset
== MACROSET_MAN
) {
891 switch (outst
->outtype
) {
893 html_man(outst
->outdata
, meta
);
896 tree_man(outst
->outdata
, meta
);
906 terminal_man(outst
->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
) != -1)
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 outstate
*outst
, struct manoutput
*outconf
)
952 switch (outst
->outtype
) {
954 outst
->outdata
= html_alloc(outconf
);
957 outst
->outdata
= utf8_alloc(outconf
);
960 outst
->outdata
= locale_alloc(outconf
);
963 outst
->outdata
= ascii_alloc(outconf
);
966 outst
->outdata
= pdf_alloc(outconf
);
969 outst
->outdata
= ps_alloc(outconf
);
977 passthrough(int fd
, int synopsis_only
)
979 const char synb
[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
980 const char synr
[] = "SYNOPSIS";
985 ssize_t len
, written
;
992 if (fflush(stdout
) == EOF
) {
993 mandoc_msg(MANDOCERR_FFLUSH
, 0, 0, "%s", strerror(errno
));
996 if ((stream
= fdopen(fd
, "r")) == NULL
) {
998 mandoc_msg(MANDOCERR_FDOPEN
, 0, 0, "%s", strerror(errno
));
1003 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) {
1023 mandoc_msg(MANDOCERR_WRITE
, 0, 0,
1024 "%s", strerror(errno
));
1030 mandoc_msg(MANDOCERR_GETLINE
, lno
, 0, "%s", strerror(errno
));
1039 woptions(char *arg
, enum mandoc_os
*os_e
, int *wstop
)
1042 const char *toks
[11];
1048 toks
[4] = "warning";
1052 toks
[8] = "openbsd";
1058 switch (getsubopt(&arg
, (char * const *)toks
, &v
)) {
1064 mandoc_msg_setmin(MANDOCERR_BASE
);
1067 mandoc_msg_setmin(MANDOCERR_STYLE
);
1070 mandoc_msg_setmin(MANDOCERR_WARNING
);
1073 mandoc_msg_setmin(MANDOCERR_ERROR
);
1076 mandoc_msg_setmin(MANDOCERR_UNSUPP
);
1079 mandoc_msg_setmin(MANDOCERR_BADARG
);
1082 mandoc_msg_setmin(MANDOCERR_BASE
);
1083 *os_e
= MANDOC_OS_OPENBSD
;
1086 mandoc_msg_setmin(MANDOCERR_BASE
);
1087 *os_e
= MANDOC_OS_NETBSD
;
1090 mandoc_msg(MANDOCERR_BADARG_BAD
, 0, 0, "-W %s", o
);
1098 * Wait until moved to the foreground,
1099 * then fork the pager and wait for the user to close it.
1102 run_pager(struct tag_files
*tag_files
)
1105 pid_t man_pgid
, tc_pgid
;
1106 pid_t pager_pid
, wait_pid
;
1108 man_pgid
= getpgid(0);
1109 tag_files
->tcpgid
= man_pgid
== getpid() ? getpgid(getppid()) :
1115 /* Stop here until moved to the foreground. */
1117 tc_pgid
= tcgetpgrp(tag_files
->ofd
);
1118 if (tc_pgid
!= man_pgid
) {
1119 if (tc_pgid
== pager_pid
) {
1120 (void)tcsetpgrp(tag_files
->ofd
, man_pgid
);
1121 if (signum
== SIGTTIN
)
1124 tag_files
->tcpgid
= tc_pgid
;
1129 /* Once in the foreground, activate the pager. */
1132 (void)tcsetpgrp(tag_files
->ofd
, pager_pid
);
1133 kill(pager_pid
, SIGCONT
);
1135 pager_pid
= spawn_pager(tag_files
);
1137 /* Wait for the pager to stop or exit. */
1139 while ((wait_pid
= waitpid(pager_pid
, &status
,
1140 WUNTRACED
)) == -1 && errno
== EINTR
)
1143 if (wait_pid
== -1) {
1144 mandoc_msg(MANDOCERR_WAIT
, 0, 0,
1145 "%s", strerror(errno
));
1148 if (!WIFSTOPPED(status
))
1151 signum
= WSTOPSIG(status
);
1156 spawn_pager(struct tag_files
*tag_files
)
1158 const struct timespec timeout
= { 0, 100000000 }; /* 0.1s */
1159 #define MAX_PAGER_ARGS 16
1160 char *argv
[MAX_PAGER_ARGS
];
1169 pager
= getenv("MANPAGER");
1170 if (pager
== NULL
|| *pager
== '\0')
1171 pager
= getenv("PAGER");
1172 if (pager
== NULL
|| *pager
== '\0')
1174 cp
= mandoc_strdup(pager
);
1177 * Parse the pager command into words.
1178 * Intentionally do not do anything fancy here.
1182 while (argc
+ 5 < MAX_PAGER_ARGS
) {
1184 cp
= strchr(cp
, ' ');
1194 /* For less(1), use the tag file. */
1198 if (*tag_files
->tfn
!= '\0' && (cmdlen
= strlen(argv
[0])) >= 4) {
1199 cp
= argv
[0] + cmdlen
- 4;
1200 if (strcmp(cp
, "less") == 0) {
1201 argv
[argc
++] = mandoc_strdup("-T");
1202 argv
[argc
++] = tag_files
->tfn
;
1203 if (tag_files
->tagname
!= NULL
) {
1204 argv
[argc
++] = mandoc_strdup("-t");
1205 argv
[argc
++] = tag_files
->tagname
;
1212 argv
[argc
++] = tag_files
->ofn
;
1215 switch (pager_pid
= fork()) {
1217 mandoc_msg(MANDOCERR_FORK
, 0, 0, "%s", strerror(errno
));
1218 exit(mandoc_msg_getrc());
1222 (void)setpgid(pager_pid
, 0);
1223 (void)tcsetpgrp(tag_files
->ofd
, pager_pid
);
1225 if (pledge("stdio rpath tmppath tty proc", NULL
) == -1) {
1226 mandoc_msg(MANDOCERR_PLEDGE
, 0, 0,
1227 "%s", strerror(errno
));
1228 exit(mandoc_msg_getrc());
1231 tag_files
->pager_pid
= pager_pid
;
1235 /* The child process becomes the pager. */
1237 if (dup2(tag_files
->ofd
, STDOUT_FILENO
) == -1) {
1238 mandoc_msg(MANDOCERR_DUP
, 0, 0, "%s", strerror(errno
));
1239 _exit(mandoc_msg_getrc());
1241 close(tag_files
->ofd
);
1242 assert(tag_files
->tfd
== -1);
1244 /* Do not start the pager before controlling the terminal. */
1246 while (tcgetpgrp(STDOUT_FILENO
) != getpid())
1247 nanosleep(&timeout
, NULL
);
1249 execvp(argv
[0], argv
);
1250 mandoc_msg(MANDOCERR_EXEC
, 0, 0, "%s: %s", argv
[0], strerror(errno
));
1251 _exit(mandoc_msg_getrc());