]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
1 /* $Id: main.c,v 1.343 2020/02/10 13:49:15 schwarze Exp $ */
3 * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010-2012, 2014-2020 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 */
48 #include "mandoc_aux.h"
50 #include "mandoc_xr.h"
54 #include "mandoc_parse.h"
58 #include "mansearch.h"
69 OUTT_ASCII
= 0, /* -Tascii */
70 OUTT_LOCALE
, /* -Tlocale */
71 OUTT_UTF8
, /* -Tutf8 */
72 OUTT_TREE
, /* -Ttree */
74 OUTT_HTML
, /* -Thtml */
75 OUTT_MARKDOWN
, /* -Tmarkdown */
76 OUTT_LINT
, /* -Tlint */
82 struct tag_files
*tag_files
; /* Tagging state variables. */
83 void *outdata
; /* data for output */
85 int wstop
; /* stop after a file with a warning */
86 int had_output
; /* Some output was generated. */
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
*, const char *,
100 struct manpage
**, size_t *);
101 static void outdata_alloc(struct outstate
*, struct manoutput
*);
102 static void parse(struct mparse
*, int, const char *,
103 struct outstate
*, struct manoutput
*);
104 static void passthrough(int, int);
105 static void process_onefile(struct mparse
*, struct manpage
*,
106 int, struct outstate
*, struct manconf
*);
107 static void run_pager(struct tag_files
*);
108 static pid_t
spawn_pager(struct tag_files
*);
109 static void usage(enum argmode
) __attribute__((__noreturn__
));
110 static int woptions(char *, enum mandoc_os
*, int *);
112 static const int sec_prios
[] = {1, 4, 5, 8, 6, 3, 7, 2, 9};
113 static char help_arg
[] = "help";
114 static char *help_argv
[] = {help_arg
, NULL
};
118 main(int argc
, char *argv
[])
120 struct manconf conf
; /* Manpaths and output options. */
121 struct outstate outst
; /* Output state. */
122 struct winsize ws
; /* Result of ioctl(TIOCGWINSZ). */
123 struct mansearch search
; /* Search options. */
124 struct manpage
*res
; /* Complete list of search results. */
125 struct manpage
*resn
; /* Search results for one name. */
126 struct mparse
*mp
; /* Opaque parser object. */
127 const char *conf_file
; /* -C: alternate config file. */
128 const char *os_s
; /* -I: Operating system for display. */
129 const char *progname
, *sec
;
130 char *defpaths
; /* -M: override manpaths. */
131 char *auxpaths
; /* -m: additional manpaths. */
132 char *oarg
; /* -O: output option string. */
133 char *tagarg
; /* -O tag: default value. */
135 size_t ressz
; /* Number of elements in res[]. */
136 size_t resnsz
; /* Number of elements in resn[]. */
138 int options
; /* Parser options. */
139 int show_usage
; /* Invalid argument: give up. */
143 enum mandoc_os os_e
; /* Check base system conventions. */
144 enum outmode outmode
; /* According to command line. */
147 progname
= getprogname();
150 progname
= mandoc_strdup("mandoc");
151 else if ((progname
= strrchr(argv
[0], '/')) == NULL
)
155 setprogname(progname
);
158 mandoc_msg_setoutfile(stderr
);
159 if (strncmp(progname
, "mandocdb", 8) == 0 ||
160 strcmp(progname
, BINM_MAKEWHATIS
) == 0)
161 return mandocdb(argc
, argv
);
164 if (pledge("stdio rpath tmppath tty proc exec", NULL
) == -1) {
165 mandoc_msg(MANDOCERR_PLEDGE
, 0, 0, "%s", strerror(errno
));
166 return mandoc_msg_getrc();
169 #if HAVE_SANDBOX_INIT
170 if (sandbox_init(kSBXProfileNoInternet
, SANDBOX_NAMED
, NULL
) == -1)
171 errx((int)MANDOCLEVEL_SYSERR
, "sandbox_init");
174 /* Search options. */
176 memset(&conf
, 0, sizeof(conf
));
178 defpaths
= auxpaths
= NULL
;
180 memset(&search
, 0, sizeof(struct mansearch
));
181 search
.outkey
= "Nd";
184 if (strcmp(progname
, BINM_MAN
) == 0)
185 search
.argmode
= ARG_NAME
;
186 else if (strcmp(progname
, BINM_APROPOS
) == 0)
187 search
.argmode
= ARG_EXPR
;
188 else if (strcmp(progname
, BINM_WHATIS
) == 0)
189 search
.argmode
= ARG_WORD
;
190 else if (strncmp(progname
, "help", 4) == 0)
191 search
.argmode
= ARG_NAME
;
193 search
.argmode
= ARG_FILE
;
195 /* Parser options. */
197 options
= MPARSE_SO
| MPARSE_UTF8
| MPARSE_LATIN1
;
198 os_e
= MANDOC_OS_OTHER
;
201 /* Formatter options. */
203 memset(&outst
, 0, sizeof(outst
));
204 outst
.tag_files
= NULL
;
205 outst
.outtype
= OUTT_LOCALE
;
209 outmode
= OUTMODE_DEF
;
211 while ((c
= getopt(argc
, argv
,
212 "aC:cfhI:iK:klM:m:O:S:s:T:VW:w")) != -1) {
213 if (c
== 'i' && search
.argmode
== ARG_EXPR
) {
219 outmode
= OUTMODE_ALL
;
228 search
.argmode
= ARG_WORD
;
231 conf
.output
.synopsisonly
= 1;
233 outmode
= OUTMODE_ALL
;
236 if (strncmp(optarg
, "os=", 3) != 0) {
237 mandoc_msg(MANDOCERR_BADARG_BAD
, 0, 0,
239 return mandoc_msg_getrc();
242 mandoc_msg(MANDOCERR_BADARG_DUPE
, 0, 0,
244 return mandoc_msg_getrc();
249 options
&= ~(MPARSE_UTF8
| MPARSE_LATIN1
);
250 if (strcmp(optarg
, "utf-8") == 0)
251 options
|= MPARSE_UTF8
;
252 else if (strcmp(optarg
, "iso-8859-1") == 0)
253 options
|= MPARSE_LATIN1
;
254 else if (strcmp(optarg
, "us-ascii") != 0) {
255 mandoc_msg(MANDOCERR_BADARG_BAD
, 0, 0,
257 return mandoc_msg_getrc();
261 search
.argmode
= ARG_EXPR
;
264 search
.argmode
= ARG_FILE
;
265 outmode
= OUTMODE_ALL
;
277 search
.arch
= optarg
;
283 if (strcmp(optarg
, "ascii") == 0)
284 outst
.outtype
= OUTT_ASCII
;
285 else if (strcmp(optarg
, "lint") == 0) {
286 outst
.outtype
= OUTT_LINT
;
287 mandoc_msg_setoutfile(stdout
);
288 mandoc_msg_setmin(MANDOCERR_BASE
);
289 } else if (strcmp(optarg
, "tree") == 0)
290 outst
.outtype
= OUTT_TREE
;
291 else if (strcmp(optarg
, "man") == 0)
292 outst
.outtype
= OUTT_MAN
;
293 else if (strcmp(optarg
, "html") == 0)
294 outst
.outtype
= OUTT_HTML
;
295 else if (strcmp(optarg
, "markdown") == 0)
296 outst
.outtype
= OUTT_MARKDOWN
;
297 else if (strcmp(optarg
, "utf8") == 0)
298 outst
.outtype
= OUTT_UTF8
;
299 else if (strcmp(optarg
, "locale") == 0)
300 outst
.outtype
= OUTT_LOCALE
;
301 else if (strcmp(optarg
, "ps") == 0)
302 outst
.outtype
= OUTT_PS
;
303 else if (strcmp(optarg
, "pdf") == 0)
304 outst
.outtype
= OUTT_PDF
;
306 mandoc_msg(MANDOCERR_BADARG_BAD
, 0, 0,
308 return mandoc_msg_getrc();
312 if (woptions(optarg
, &os_e
, &outst
.wstop
) == -1)
313 return mandoc_msg_getrc();
316 outmode
= OUTMODE_FLN
;
325 usage(search
.argmode
);
327 /* Postprocess options. */
331 switch (search
.argmode
) {
333 outmode
= OUTMODE_ALL
;
337 outmode
= OUTMODE_ONE
;
340 outmode
= OUTMODE_LST
;
345 if (search
.argmode
== ARG_FILE
)
346 outmode
= OUTMODE_ALL
;
356 if (outmode
== OUTMODE_LST
)
357 search
.outkey
= oarg
;
359 while (oarg
!= NULL
) {
360 if (manconf_output(&conf
.output
,
361 strsep(&oarg
, ","), 0) == -1)
362 return mandoc_msg_getrc();
367 if (outst
.outtype
!= OUTT_TREE
|| conf
.output
.noval
== 0)
368 options
|= MPARSE_VALIDATE
;
370 if (outmode
== OUTMODE_FLN
||
371 outmode
== OUTMODE_LST
||
372 !isatty(STDOUT_FILENO
))
375 if (outst
.use_pager
&&
376 (conf
.output
.width
== 0 || conf
.output
.indent
== 0) &&
377 ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &ws
) != -1 &&
379 if (conf
.output
.width
== 0 && ws
.ws_col
< 79)
380 conf
.output
.width
= ws
.ws_col
- 1;
381 if (conf
.output
.indent
== 0 && ws
.ws_col
< 66)
382 conf
.output
.indent
= 3;
386 if (outst
.use_pager
== 0) {
387 if (pledge("stdio rpath", NULL
) == -1) {
388 mandoc_msg(MANDOCERR_PLEDGE
, 0, 0,
389 "%s", strerror(errno
));
390 return mandoc_msg_getrc();
395 /* Parse arguments. */
403 * Quirks for help(1) and man(1),
404 * in particular for a section argument without -s.
407 if (search
.argmode
== ARG_NAME
) {
408 if (*progname
== 'h') {
413 } else if (argc
> 1 &&
414 ((uc
= (unsigned char *)argv
[0]) != NULL
) &&
415 ((isdigit(uc
[0]) && (uc
[1] == '\0' ||
417 (uc
[0] == 'n' && uc
[1] == '\0'))) {
418 search
.sec
= (char *)uc
;
422 if (search
.arch
== NULL
)
423 search
.arch
= getenv("MACHINE");
425 if (search
.arch
== NULL
)
426 search
.arch
= MACHINE
;
428 if (outmode
== OUTMODE_ONE
)
429 search
.firstmatch
= 1;
433 * Use the first argument for -O tag in addition to
434 * using it as a search term for man(1) or apropos(1).
437 if (conf
.output
.tag
!= NULL
&& *conf
.output
.tag
== '\0') {
438 tagarg
= argc
> 0 && search
.argmode
== ARG_EXPR
?
439 strchr(*argv
, '=') : NULL
;
440 conf
.output
.tag
= tagarg
== NULL
? *argv
: tagarg
+ 1;
443 /* Read the configuration file. */
445 if (search
.argmode
!= ARG_FILE
)
446 manconf_parse(&conf
, conf_file
, defpaths
, auxpaths
);
448 /* man(1): Resolve each name individually. */
450 if (search
.argmode
== ARG_NAME
) {
452 if (outmode
!= OUTMODE_FLN
)
454 if (conf
.manpath
.sz
== 0) {
455 warnx("The manpath is empty.");
456 mandoc_msg_setrc(MANDOCLEVEL_BADARG
);
458 for (i
= 0; i
+ 1 < conf
.manpath
.sz
; i
++)
459 printf("%s:", conf
.manpath
.paths
[i
]);
460 printf("%s\n", conf
.manpath
.paths
[i
]);
463 return (int)mandoc_msg_getrc();
465 for (res
= NULL
, ressz
= 0; argc
> 0; argc
--, argv
++) {
466 (void)mansearch(&search
, &conf
.manpath
,
467 1, argv
, &resn
, &resnsz
);
469 (void)fs_search(&search
, &conf
.manpath
,
470 *argv
, &resn
, &resnsz
);
471 if (resnsz
== 0 && strchr(*argv
, '/') == NULL
) {
472 if (search
.arch
!= NULL
&&
473 arch_valid(search
.arch
, OSENUM
) == 0)
474 warnx("Unknown architecture \"%s\".",
476 else if (search
.sec
!= NULL
)
477 warnx("No entry for %s in "
478 "section %s of the manual.",
481 warnx("No entry for %s in "
482 "the manual.", *argv
);
483 mandoc_msg_setrc(MANDOCLEVEL_BADARG
);
487 if (access(*argv
, R_OK
) == -1) {
488 mandoc_msg_setinfilename(*argv
);
489 mandoc_msg(MANDOCERR_BADARG_BAD
,
490 0, 0, "%s", strerror(errno
));
491 mandoc_msg_setinfilename(NULL
);
495 resn
= mandoc_calloc(resnsz
, sizeof(*res
));
496 resn
->file
= mandoc_strdup(*argv
);
497 resn
->ipath
= SIZE_MAX
;
498 resn
->form
= FORM_SRC
;
500 if (outmode
!= OUTMODE_ONE
|| resnsz
== 1) {
501 res
= mandoc_reallocarray(res
,
502 ressz
+ resnsz
, sizeof(*res
));
503 memcpy(res
+ ressz
, resn
,
504 sizeof(*resn
) * resnsz
);
509 /* Search for the best section. */
512 for (ib
= i
= 0; i
< resnsz
; i
++) {
514 sec
+= strcspn(sec
, "123456789");
516 continue; /* No section at all. */
517 prio
= sec_prios
[sec
[0] - '1'];
518 if (search
.sec
!= NULL
) {
519 ssz
= strlen(search
.sec
);
520 if (strncmp(sec
, search
.sec
, ssz
) == 0)
523 sec
++; /* Prefer without suffix. */
525 prio
+= 10; /* Wrong dir name. */
526 if (search
.sec
!= NULL
&&
527 (strlen(sec
) <= ssz
+ 3 ||
528 strcmp(sec
+ strlen(sec
) - ssz
,
530 prio
+= 20; /* Wrong file ext. */
531 if (prio
>= best_prio
)
536 res
= mandoc_reallocarray(res
, ressz
+ 1,
538 memcpy(res
+ ressz
++, resn
+ ib
, sizeof(*resn
));
541 /* apropos(1), whatis(1): Process the full search expression. */
543 } else if (search
.argmode
!= ARG_FILE
) {
544 if (mansearch(&search
, &conf
.manpath
,
545 argc
, argv
, &res
, &ressz
) == 0)
546 usage(search
.argmode
);
549 warnx("nothing appropriate");
550 mandoc_msg_setrc(MANDOCLEVEL_BADARG
);
554 /* mandoc(1): Take command line arguments as file names. */
557 ressz
= argc
> 0 ? argc
: 1;
558 res
= mandoc_calloc(ressz
, sizeof(*res
));
559 for (i
= 0; i
< ressz
; i
++) {
561 res
[i
].file
= mandoc_strdup(argv
[i
]);
562 res
[i
].ipath
= SIZE_MAX
;
563 res
[i
].form
= FORM_SRC
;
569 for (i
= 0; i
< ressz
; i
++)
573 for (i
= 0; i
< ressz
; i
++)
574 printf("%s - %s\n", res
[i
].names
,
575 res
[i
].output
== NULL
? "" :
582 if (search
.argmode
== ARG_FILE
&& auxpaths
!= NULL
) {
583 if (strcmp(auxpaths
, "doc") == 0)
584 options
|= MPARSE_MDOC
;
585 else if (strcmp(auxpaths
, "an") == 0)
586 options
|= MPARSE_MAN
;
590 mp
= mparse_alloc(options
, os_e
, os_s
);
593 * Remember the original working directory, if possible.
594 * This will be needed if some names on the command line
595 * are page names and some are relative file names.
596 * Do not error out if the current directory is not
597 * readable: Maybe it won't be needed after all.
599 startdir
= open(".", O_RDONLY
| O_DIRECTORY
);
601 for (i
= 0; i
< ressz
; i
++) {
602 process_onefile(mp
, res
+ i
, startdir
, &outst
, &conf
);
603 if (outst
.wstop
&& mandoc_msg_getrc() != MANDOCLEVEL_OK
)
606 if (startdir
!= -1) {
607 (void)fchdir(startdir
);
611 if (outst
.outdata
!= NULL
) {
612 switch (outst
.outtype
) {
614 html_free(outst
.outdata
);
619 ascii_free(outst
.outdata
);
623 pspdf_free(outst
.outdata
);
634 mansearch_free(res
, ressz
);
635 if (search
.argmode
!= ARG_FILE
)
638 if (outst
.tag_files
!= NULL
) {
641 run_pager(outst
.tag_files
);
643 } else if (outst
.had_output
&& outst
.outtype
!= OUTT_LINT
)
644 mandoc_msg_summary();
646 return (int)mandoc_msg_getrc();
650 usage(enum argmode argmode
)
654 fputs("usage: mandoc [-ac] [-I os=name] "
655 "[-K encoding] [-mdoc | -man] [-O options]\n"
656 "\t [-T output] [-W level] [file ...]\n", stderr
);
659 fputs("usage: man [-acfhklw] [-C file] [-M path] "
660 "[-m path] [-S subsection]\n"
661 "\t [[-s] section] name ...\n", stderr
);
664 fputs("usage: whatis [-afk] [-C file] "
665 "[-M path] [-m path] [-O outkey] [-S arch]\n"
666 "\t [-s section] name ...\n", stderr
);
669 fputs("usage: apropos [-afk] [-C file] "
670 "[-M path] [-m path] [-O outkey] [-S arch]\n"
671 "\t [-s section] expression ...\n", stderr
);
674 exit((int)MANDOCLEVEL_BADARG
);
678 fs_lookup(const struct manpaths
*paths
, size_t ipath
,
679 const char *sec
, const char *arch
, const char *name
,
680 struct manpage
**res
, size_t *ressz
)
684 struct manpage
*page
;
690 mandoc_asprintf(&file
, "%s/man%s/%s.%s",
691 paths
->paths
[ipath
], sec
, name
, sec
);
692 if (stat(file
, &sb
) != -1)
696 mandoc_asprintf(&file
, "%s/cat%s/%s.0",
697 paths
->paths
[ipath
], sec
, name
);
698 if (stat(file
, &sb
) != -1) {
705 mandoc_asprintf(&file
, "%s/man%s/%s/%s.%s",
706 paths
->paths
[ipath
], sec
, arch
, name
, sec
);
707 if (stat(file
, &sb
) != -1)
712 mandoc_asprintf(&file
, "%s/man%s/%s.[01-9]*",
713 paths
->paths
[ipath
], sec
, name
);
714 globres
= glob(file
, 0, NULL
, &globinfo
);
715 if (globres
!= 0 && globres
!= GLOB_NOMATCH
)
716 mandoc_msg(MANDOCERR_GLOB
, 0, 0,
717 "%s: %s", file
, strerror(errno
));
720 file
= mandoc_strdup(*globinfo
.gl_pathv
);
723 if (stat(file
, &sb
) != -1)
727 if (res
!= NULL
|| ipath
+ 1 != paths
->sz
)
730 mandoc_asprintf(&file
, "%s.%s", name
, sec
);
731 globres
= stat(file
, &sb
);
736 warnx("outdated mandoc.db lacks %s(%s) entry, run %s %s",
737 name
, sec
, BINM_MAKEWHATIS
, paths
->paths
[ipath
]);
742 *res
= mandoc_reallocarray(*res
, ++*ressz
, sizeof(**res
));
743 page
= *res
+ (*ressz
- 1);
747 page
->bits
= NAME_FILE
& NAME_MASK
;
749 page
->sec
= (*sec
>= '1' && *sec
<= '9') ? *sec
- '1' + 1 : 10;
755 fs_search(const struct mansearch
*cfg
, const struct manpaths
*paths
,
756 const char *name
, struct manpage
**res
, size_t *ressz
)
758 const char *const sections
[] =
759 {"1", "8", "6", "2", "3", "5", "7", "4", "9", "3p"};
760 const size_t nsec
= sizeof(sections
)/sizeof(sections
[0]);
764 assert(cfg
->argmode
== ARG_NAME
);
768 for (ipath
= 0; ipath
< paths
->sz
; ipath
++) {
769 if (cfg
->sec
!= NULL
) {
770 if (fs_lookup(paths
, ipath
, cfg
->sec
, cfg
->arch
,
771 name
, res
, ressz
) != -1 && cfg
->firstmatch
)
774 for (isec
= 0; isec
< nsec
; isec
++)
775 if (fs_lookup(paths
, ipath
, sections
[isec
],
776 cfg
->arch
, name
, res
, ressz
) != -1 &&
785 process_onefile(struct mparse
*mp
, struct manpage
*resp
, int startdir
,
786 struct outstate
*outst
, struct manconf
*conf
)
791 * Changing directories is not needed in ARG_FILE mode.
792 * Do it on a best-effort basis. Even in case of
793 * failure, some functionality may still work.
795 if (resp
->ipath
!= SIZE_MAX
)
796 (void)chdir(conf
->manpath
.paths
[resp
->ipath
]);
797 else if (startdir
!= -1)
798 (void)fchdir(startdir
);
800 mandoc_msg_setinfilename(resp
->file
);
801 if (resp
->file
!= NULL
) {
802 if ((fd
= mparse_open(mp
, resp
->file
)) == -1) {
803 mandoc_msg(resp
->ipath
== SIZE_MAX
?
804 MANDOCERR_BADARG_BAD
: MANDOCERR_OPEN
,
805 0, 0, "%s", strerror(errno
));
806 mandoc_msg_setinfilename(NULL
);
812 if (outst
->use_pager
) {
813 outst
->use_pager
= 0;
814 outst
->tag_files
= tag_init(conf
->output
.tag
);
817 if (outst
->had_output
&& outst
->outtype
<= OUTT_UTF8
) {
818 if (outst
->outdata
== NULL
)
819 outdata_alloc(outst
, &conf
->output
);
820 terminal_sepline(outst
->outdata
);
823 if (resp
->form
== FORM_SRC
)
824 parse(mp
, fd
, resp
->file
, outst
, &conf
->output
);
826 passthrough(fd
, conf
->output
.synopsisonly
);
827 outst
->had_output
= 1;
830 if (ferror(stdout
)) {
831 if (outst
->tag_files
!= NULL
) {
832 mandoc_msg(MANDOCERR_WRITE
, 0, 0, "%s: %s",
833 outst
->tag_files
->ofn
, strerror(errno
));
835 outst
->tag_files
= NULL
;
837 mandoc_msg(MANDOCERR_WRITE
, 0, 0, "%s",
840 mandoc_msg_setinfilename(NULL
);
844 parse(struct mparse
*mp
, int fd
, const char *file
,
845 struct outstate
*outst
, struct manoutput
*outconf
)
848 struct roff_meta
*meta
;
859 mparse_readfd(mp
, fd
, file
);
860 if (fd
!= STDIN_FILENO
)
864 * With -Wstop and warnings or errors of at least the requested
865 * level, do not produce output.
868 if (outst
->wstop
&& mandoc_msg_getrc() != MANDOCLEVEL_OK
)
871 if (outst
->outdata
== NULL
)
872 outdata_alloc(outst
, outconf
);
873 else if (outst
->outtype
== OUTT_HTML
)
877 meta
= mparse_result(mp
);
879 /* Execute the out device, if it exists. */
881 outst
->had_output
= 1;
882 if (meta
->macroset
== MACROSET_MDOC
) {
883 switch (outst
->outtype
) {
885 html_mdoc(outst
->outdata
, meta
);
888 tree_mdoc(outst
->outdata
, meta
);
891 man_mdoc(outst
->outdata
, meta
);
898 terminal_mdoc(outst
->outdata
, meta
);
901 markdown_mdoc(outst
->outdata
, meta
);
907 if (meta
->macroset
== MACROSET_MAN
) {
908 switch (outst
->outtype
) {
910 html_man(outst
->outdata
, meta
);
913 tree_man(outst
->outdata
, meta
);
923 terminal_man(outst
->outdata
, meta
);
929 if (mandoc_msg_getmin() < MANDOCERR_STYLE
)
936 static struct manpaths paths
;
937 struct mansearch search
;
938 struct mandoc_xr
*xr
;
942 manpath_base(&paths
);
944 for (xr
= mandoc_xr_get(); xr
!= NULL
; xr
= xr
->next
) {
948 search
.sec
= xr
->sec
;
949 search
.outkey
= NULL
;
950 search
.argmode
= ARG_NAME
;
951 search
.firstmatch
= 1;
952 if (mansearch(&search
, &paths
, 1, &xr
->name
, NULL
, &sz
))
954 if (fs_search(&search
, &paths
, xr
->name
, NULL
, &sz
) != -1)
957 mandoc_msg(MANDOCERR_XR_BAD
, xr
->line
,
958 xr
->pos
+ 1, "Xr %s %s", xr
->name
, xr
->sec
);
960 mandoc_msg(MANDOCERR_XR_BAD
, xr
->line
,
961 xr
->pos
+ 1, "Xr %s %s (%d times)",
962 xr
->name
, xr
->sec
, xr
->count
);
967 outdata_alloc(struct outstate
*outst
, struct manoutput
*outconf
)
969 switch (outst
->outtype
) {
971 outst
->outdata
= html_alloc(outconf
);
974 outst
->outdata
= utf8_alloc(outconf
);
977 outst
->outdata
= locale_alloc(outconf
);
980 outst
->outdata
= ascii_alloc(outconf
);
983 outst
->outdata
= pdf_alloc(outconf
);
986 outst
->outdata
= ps_alloc(outconf
);
994 passthrough(int fd
, int synopsis_only
)
996 const char synb
[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
997 const char synr
[] = "SYNOPSIS";
1002 ssize_t len
, written
;
1009 if (fflush(stdout
) == EOF
) {
1010 mandoc_msg(MANDOCERR_FFLUSH
, 0, 0, "%s", strerror(errno
));
1013 if ((stream
= fdopen(fd
, "r")) == NULL
) {
1015 mandoc_msg(MANDOCERR_FDOPEN
, 0, 0, "%s", strerror(errno
));
1020 while ((len
= getline(&line
, &linesz
, stream
)) != -1) {
1023 if (synopsis_only
) {
1025 if ( ! isspace((unsigned char)*cp
))
1027 while (isspace((unsigned char)*cp
)) {
1032 if (strcmp(cp
, synb
) == 0 ||
1033 strcmp(cp
, synr
) == 0)
1038 for (; len
> 0; len
-= written
) {
1039 if ((written
= write(STDOUT_FILENO
, cp
, len
)) == -1) {
1040 mandoc_msg(MANDOCERR_WRITE
, 0, 0,
1041 "%s", strerror(errno
));
1047 mandoc_msg(MANDOCERR_GETLINE
, lno
, 0, "%s", strerror(errno
));
1056 woptions(char *arg
, enum mandoc_os
*os_e
, int *wstop
)
1059 const char *toks
[11];
1065 toks
[4] = "warning";
1069 toks
[8] = "openbsd";
1075 switch (getsubopt(&arg
, (char * const *)toks
, &v
)) {
1081 mandoc_msg_setmin(MANDOCERR_BASE
);
1084 mandoc_msg_setmin(MANDOCERR_STYLE
);
1087 mandoc_msg_setmin(MANDOCERR_WARNING
);
1090 mandoc_msg_setmin(MANDOCERR_ERROR
);
1093 mandoc_msg_setmin(MANDOCERR_UNSUPP
);
1096 mandoc_msg_setmin(MANDOCERR_BADARG
);
1099 mandoc_msg_setmin(MANDOCERR_BASE
);
1100 *os_e
= MANDOC_OS_OPENBSD
;
1103 mandoc_msg_setmin(MANDOCERR_BASE
);
1104 *os_e
= MANDOC_OS_NETBSD
;
1107 mandoc_msg(MANDOCERR_BADARG_BAD
, 0, 0, "-W %s", o
);
1115 * Wait until moved to the foreground,
1116 * then fork the pager and wait for the user to close it.
1119 run_pager(struct tag_files
*tag_files
)
1122 pid_t man_pgid
, tc_pgid
;
1123 pid_t pager_pid
, wait_pid
;
1125 man_pgid
= getpgid(0);
1126 tag_files
->tcpgid
= man_pgid
== getpid() ? getpgid(getppid()) :
1132 /* Stop here until moved to the foreground. */
1134 tc_pgid
= tcgetpgrp(tag_files
->ofd
);
1135 if (tc_pgid
!= man_pgid
) {
1136 if (tc_pgid
== pager_pid
) {
1137 (void)tcsetpgrp(tag_files
->ofd
, man_pgid
);
1138 if (signum
== SIGTTIN
)
1141 tag_files
->tcpgid
= tc_pgid
;
1146 /* Once in the foreground, activate the pager. */
1149 (void)tcsetpgrp(tag_files
->ofd
, pager_pid
);
1150 kill(pager_pid
, SIGCONT
);
1152 pager_pid
= spawn_pager(tag_files
);
1154 /* Wait for the pager to stop or exit. */
1156 while ((wait_pid
= waitpid(pager_pid
, &status
,
1157 WUNTRACED
)) == -1 && errno
== EINTR
)
1160 if (wait_pid
== -1) {
1161 mandoc_msg(MANDOCERR_WAIT
, 0, 0,
1162 "%s", strerror(errno
));
1165 if (!WIFSTOPPED(status
))
1168 signum
= WSTOPSIG(status
);
1173 spawn_pager(struct tag_files
*tag_files
)
1175 const struct timespec timeout
= { 0, 100000000 }; /* 0.1s */
1176 #define MAX_PAGER_ARGS 16
1177 char *argv
[MAX_PAGER_ARGS
];
1186 pager
= getenv("MANPAGER");
1187 if (pager
== NULL
|| *pager
== '\0')
1188 pager
= getenv("PAGER");
1189 if (pager
== NULL
|| *pager
== '\0')
1191 cp
= mandoc_strdup(pager
);
1194 * Parse the pager command into words.
1195 * Intentionally do not do anything fancy here.
1199 while (argc
+ 5 < MAX_PAGER_ARGS
) {
1201 cp
= strchr(cp
, ' ');
1211 /* For less(1), use the tag file. */
1215 if (*tag_files
->tfn
!= '\0' && (cmdlen
= strlen(argv
[0])) >= 4) {
1216 cp
= argv
[0] + cmdlen
- 4;
1217 if (strcmp(cp
, "less") == 0) {
1218 argv
[argc
++] = mandoc_strdup("-T");
1219 argv
[argc
++] = tag_files
->tfn
;
1220 if (tag_files
->tagname
!= NULL
) {
1221 argv
[argc
++] = mandoc_strdup("-t");
1222 argv
[argc
++] = tag_files
->tagname
;
1229 argv
[argc
++] = tag_files
->ofn
;
1232 switch (pager_pid
= fork()) {
1234 mandoc_msg(MANDOCERR_FORK
, 0, 0, "%s", strerror(errno
));
1235 exit(mandoc_msg_getrc());
1239 (void)setpgid(pager_pid
, 0);
1240 (void)tcsetpgrp(tag_files
->ofd
, pager_pid
);
1242 if (pledge("stdio rpath tmppath tty proc", NULL
) == -1) {
1243 mandoc_msg(MANDOCERR_PLEDGE
, 0, 0,
1244 "%s", strerror(errno
));
1245 exit(mandoc_msg_getrc());
1248 tag_files
->pager_pid
= pager_pid
;
1252 /* The child process becomes the pager. */
1254 if (dup2(tag_files
->ofd
, STDOUT_FILENO
) == -1) {
1255 mandoc_msg(MANDOCERR_DUP
, 0, 0, "%s", strerror(errno
));
1256 _exit(mandoc_msg_getrc());
1258 close(tag_files
->ofd
);
1259 assert(tag_files
->tfd
== -1);
1261 /* Do not start the pager before controlling the terminal. */
1263 while (tcgetpgrp(STDOUT_FILENO
) != getpid())
1264 nanosleep(&timeout
, NULL
);
1266 execvp(argv
[0], argv
);
1267 mandoc_msg(MANDOCERR_EXEC
, 0, 0, "%s: %s", argv
[0], strerror(errno
));
1268 _exit(mandoc_msg_getrc());