]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
1 /* $Id: main.c,v 1.354 2021/03/30 17:16:55 schwarze Exp $ */
3 * Copyright (c) 2010-2012, 2014-2020 Ingo Schwarze <schwarze@openbsd.org>
4 * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
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.
19 * Main program for mandoc(1), man(1), apropos(1), whatis(1), and help(1).
23 #include <sys/types.h>
24 #include <sys/ioctl.h>
25 #include <sys/param.h> /* MACHINE */
50 #include "mandoc_aux.h"
52 #include "mandoc_xr.h"
56 #include "mandoc_parse.h"
61 #include "mansearch.h"
72 OUTT_ASCII
= 0, /* -Tascii */
73 OUTT_LOCALE
, /* -Tlocale */
74 OUTT_UTF8
, /* -Tutf8 */
75 OUTT_TREE
, /* -Ttree */
77 OUTT_HTML
, /* -Thtml */
78 OUTT_MARKDOWN
, /* -Tmarkdown */
79 OUTT_LINT
, /* -Tlint */
85 struct tag_files
*tag_files
; /* Tagging state variables. */
86 void *outdata
; /* data for output */
88 int wstop
; /* stop after a file with a warning */
89 int had_output
; /* Some output was generated. */
90 enum outt outtype
; /* which output to use */
94 int mandocdb(int, char *[]);
96 static void check_xr(void);
97 static int fs_lookup(const struct manpaths
*,
98 size_t ipath
, const char *,
99 const char *, const char *,
100 struct manpage
**, size_t *);
101 static int fs_search(const struct mansearch
*,
102 const struct manpaths
*, const char *,
103 struct manpage
**, size_t *);
104 static void glob_esc(char **, const char *, const char *);
105 static void outdata_alloc(struct outstate
*, struct manoutput
*);
106 static void parse(struct mparse
*, int, const char *,
107 struct outstate
*, struct manoutput
*);
108 static void passthrough(int, int);
109 static void process_onefile(struct mparse
*, struct manpage
*,
110 int, struct outstate
*, struct manconf
*);
111 static void run_pager(struct outstate
*, char *);
112 static pid_t
spawn_pager(struct outstate
*, char *);
113 static void usage(enum argmode
) __attribute__((__noreturn__
));
114 static int woptions(char *, enum mandoc_os
*, int *);
116 static const int sec_prios
[] = {1, 4, 5, 8, 6, 3, 7, 2, 9};
117 static char help_arg
[] = "help";
118 static char *help_argv
[] = {help_arg
, NULL
};
122 main(int argc
, char *argv
[])
124 struct manconf conf
; /* Manpaths and output options. */
125 struct outstate outst
; /* Output state. */
126 struct winsize ws
; /* Result of ioctl(TIOCGWINSZ). */
127 struct mansearch search
; /* Search options. */
128 struct manpage
*res
; /* Complete list of search results. */
129 struct manpage
*resn
; /* Search results for one name. */
130 struct mparse
*mp
; /* Opaque parser object. */
131 const char *conf_file
; /* -C: alternate config file. */
132 const char *os_s
; /* -I: Operating system for display. */
133 const char *progname
, *sec
;
134 char *defpaths
; /* -M: override manpaths. */
135 char *auxpaths
; /* -m: additional manpaths. */
136 char *oarg
; /* -O: output option string. */
137 char *tagarg
; /* -O tag: default value. */
139 size_t ressz
; /* Number of elements in res[]. */
140 size_t resnsz
; /* Number of elements in resn[]. */
142 int options
; /* Parser options. */
143 int show_usage
; /* Invalid argument: give up. */
147 enum mandoc_os os_e
; /* Check base system conventions. */
148 enum outmode outmode
; /* According to command line. */
151 progname
= getprogname();
154 progname
= mandoc_strdup("mandoc");
155 else if ((progname
= strrchr(argv
[0], '/')) == NULL
)
159 setprogname(progname
);
162 mandoc_msg_setoutfile(stderr
);
163 if (strncmp(progname
, "mandocdb", 8) == 0 ||
164 strcmp(progname
, BINM_MAKEWHATIS
) == 0)
165 return mandocdb(argc
, argv
);
168 if (pledge("stdio rpath wpath cpath tmppath tty proc exec", NULL
) == -1) {
169 mandoc_msg(MANDOCERR_PLEDGE
, 0, 0, "%s", strerror(errno
));
170 return mandoc_msg_getrc();
173 #if HAVE_SANDBOX_INIT
174 if (sandbox_init(kSBXProfileNoInternet
, SANDBOX_NAMED
, NULL
) == -1)
175 errx((int)MANDOCLEVEL_SYSERR
, "sandbox_init");
178 /* Search options. */
180 memset(&conf
, 0, sizeof(conf
));
182 defpaths
= auxpaths
= NULL
;
184 memset(&search
, 0, sizeof(struct mansearch
));
185 search
.outkey
= "Nd";
188 if (strcmp(progname
, BINM_MAN
) == 0)
189 search
.argmode
= ARG_NAME
;
190 else if (strcmp(progname
, BINM_APROPOS
) == 0)
191 search
.argmode
= ARG_EXPR
;
192 else if (strcmp(progname
, BINM_WHATIS
) == 0)
193 search
.argmode
= ARG_WORD
;
194 else if (strncmp(progname
, "help", 4) == 0)
195 search
.argmode
= ARG_NAME
;
197 search
.argmode
= ARG_FILE
;
199 /* Parser options. */
201 options
= MPARSE_SO
| MPARSE_UTF8
| MPARSE_LATIN1
;
202 os_e
= MANDOC_OS_OTHER
;
205 /* Formatter options. */
207 memset(&outst
, 0, sizeof(outst
));
208 outst
.tag_files
= NULL
;
209 outst
.outtype
= OUTT_LOCALE
;
213 outmode
= OUTMODE_DEF
;
215 while ((c
= getopt(argc
, argv
,
216 "aC:cfhI:iK:klM:m:O:S:s:T:VW:w")) != -1) {
217 if (c
== 'i' && search
.argmode
== ARG_EXPR
) {
223 outmode
= OUTMODE_ALL
;
232 search
.argmode
= ARG_WORD
;
235 conf
.output
.synopsisonly
= 1;
237 outmode
= OUTMODE_ALL
;
240 if (strncmp(optarg
, "os=", 3) != 0) {
241 mandoc_msg(MANDOCERR_BADARG_BAD
, 0, 0,
243 return mandoc_msg_getrc();
246 mandoc_msg(MANDOCERR_BADARG_DUPE
, 0, 0,
248 return mandoc_msg_getrc();
253 options
&= ~(MPARSE_UTF8
| MPARSE_LATIN1
);
254 if (strcmp(optarg
, "utf-8") == 0)
255 options
|= MPARSE_UTF8
;
256 else if (strcmp(optarg
, "iso-8859-1") == 0)
257 options
|= MPARSE_LATIN1
;
258 else if (strcmp(optarg
, "us-ascii") != 0) {
259 mandoc_msg(MANDOCERR_BADARG_BAD
, 0, 0,
261 return mandoc_msg_getrc();
265 search
.argmode
= ARG_EXPR
;
268 search
.argmode
= ARG_FILE
;
269 outmode
= OUTMODE_ALL
;
281 search
.arch
= optarg
;
287 if (strcmp(optarg
, "ascii") == 0)
288 outst
.outtype
= OUTT_ASCII
;
289 else if (strcmp(optarg
, "lint") == 0) {
290 outst
.outtype
= OUTT_LINT
;
291 mandoc_msg_setoutfile(stdout
);
292 mandoc_msg_setmin(MANDOCERR_BASE
);
293 } else if (strcmp(optarg
, "tree") == 0)
294 outst
.outtype
= OUTT_TREE
;
295 else if (strcmp(optarg
, "man") == 0)
296 outst
.outtype
= OUTT_MAN
;
297 else if (strcmp(optarg
, "html") == 0)
298 outst
.outtype
= OUTT_HTML
;
299 else if (strcmp(optarg
, "markdown") == 0)
300 outst
.outtype
= OUTT_MARKDOWN
;
301 else if (strcmp(optarg
, "utf8") == 0)
302 outst
.outtype
= OUTT_UTF8
;
303 else if (strcmp(optarg
, "locale") == 0)
304 outst
.outtype
= OUTT_LOCALE
;
305 else if (strcmp(optarg
, "ps") == 0)
306 outst
.outtype
= OUTT_PS
;
307 else if (strcmp(optarg
, "pdf") == 0)
308 outst
.outtype
= OUTT_PDF
;
310 mandoc_msg(MANDOCERR_BADARG_BAD
, 0, 0,
312 return mandoc_msg_getrc();
316 if (woptions(optarg
, &os_e
, &outst
.wstop
) == -1)
317 return mandoc_msg_getrc();
320 outmode
= OUTMODE_FLN
;
329 usage(search
.argmode
);
331 /* Postprocess options. */
335 switch (search
.argmode
) {
337 outmode
= OUTMODE_ALL
;
341 outmode
= OUTMODE_ONE
;
344 outmode
= OUTMODE_LST
;
349 if (search
.argmode
== ARG_FILE
)
350 outmode
= OUTMODE_ALL
;
360 if (outmode
== OUTMODE_LST
)
361 search
.outkey
= oarg
;
363 while (oarg
!= NULL
) {
364 if (manconf_output(&conf
.output
,
365 strsep(&oarg
, ","), 0) == -1)
366 return mandoc_msg_getrc();
371 if (outst
.outtype
!= OUTT_TREE
|| conf
.output
.noval
== 0)
372 options
|= MPARSE_VALIDATE
;
374 if (outmode
== OUTMODE_FLN
||
375 outmode
== OUTMODE_LST
||
376 (conf
.output
.outfilename
== NULL
&&
377 conf
.output
.tagfilename
== NULL
&&
378 isatty(STDOUT_FILENO
) == 0))
381 if (outst
.use_pager
&&
382 (conf
.output
.width
== 0 || conf
.output
.indent
== 0) &&
383 ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &ws
) != -1 &&
385 if (conf
.output
.width
== 0 && ws
.ws_col
< 79)
386 conf
.output
.width
= ws
.ws_col
- 1;
387 if (conf
.output
.indent
== 0 && ws
.ws_col
< 66)
388 conf
.output
.indent
= 3;
392 if (outst
.use_pager
== 0)
393 c
= pledge("stdio rpath", NULL
);
394 else if (conf
.output
.outfilename
!= NULL
||
395 conf
.output
.tagfilename
!= NULL
)
396 c
= pledge("stdio rpath wpath cpath", NULL
);
398 c
= pledge("stdio rpath tmppath tty proc exec", NULL
);
400 mandoc_msg(MANDOCERR_PLEDGE
, 0, 0, "%s", strerror(errno
));
401 return mandoc_msg_getrc();
405 /* Parse arguments. */
413 * Quirks for help(1) and man(1),
414 * in particular for a section argument without -s.
417 if (search
.argmode
== ARG_NAME
) {
418 if (*progname
== 'h') {
423 } else if (argc
> 1 &&
424 ((uc
= (unsigned char *)argv
[0]) != NULL
) &&
425 ((isdigit(uc
[0]) && (uc
[1] == '\0' ||
427 (uc
[0] == 'n' && uc
[1] == '\0'))) {
428 search
.sec
= (char *)uc
;
432 if (search
.arch
== NULL
)
433 search
.arch
= getenv("MACHINE");
435 if (search
.arch
== NULL
)
436 search
.arch
= MACHINE
;
438 if (outmode
== OUTMODE_ONE
)
439 search
.firstmatch
= 1;
443 * Use the first argument for -O tag in addition to
444 * using it as a search term for man(1) or apropos(1).
447 if (conf
.output
.tag
!= NULL
&& *conf
.output
.tag
== '\0') {
448 tagarg
= argc
> 0 && search
.argmode
== ARG_EXPR
?
449 strchr(*argv
, '=') : NULL
;
450 conf
.output
.tag
= tagarg
== NULL
? *argv
: tagarg
+ 1;
453 /* Read the configuration file. */
455 if (search
.argmode
!= ARG_FILE
)
456 manconf_parse(&conf
, conf_file
, defpaths
, auxpaths
);
458 /* man(1): Resolve each name individually. */
460 if (search
.argmode
== ARG_NAME
) {
462 if (outmode
!= OUTMODE_FLN
)
464 if (conf
.manpath
.sz
== 0) {
465 warnx("The manpath is empty.");
466 mandoc_msg_setrc(MANDOCLEVEL_BADARG
);
468 for (i
= 0; i
+ 1 < conf
.manpath
.sz
; i
++)
469 printf("%s:", conf
.manpath
.paths
[i
]);
470 printf("%s\n", conf
.manpath
.paths
[i
]);
473 return (int)mandoc_msg_getrc();
475 for (res
= NULL
, ressz
= 0; argc
> 0; argc
--, argv
++) {
476 (void)mansearch(&search
, &conf
.manpath
,
477 1, argv
, &resn
, &resnsz
);
479 (void)fs_search(&search
, &conf
.manpath
,
480 *argv
, &resn
, &resnsz
);
481 if (resnsz
== 0 && strchr(*argv
, '/') == NULL
) {
482 if (search
.arch
!= NULL
&&
483 arch_valid(search
.arch
, OSENUM
) == 0)
484 warnx("Unknown architecture \"%s\".",
486 else if (search
.sec
!= NULL
)
487 warnx("No entry for %s in "
488 "section %s of the manual.",
491 warnx("No entry for %s in "
492 "the manual.", *argv
);
493 mandoc_msg_setrc(MANDOCLEVEL_BADARG
);
497 if (access(*argv
, R_OK
) == -1) {
498 mandoc_msg_setinfilename(*argv
);
499 mandoc_msg(MANDOCERR_BADARG_BAD
,
500 0, 0, "%s", strerror(errno
));
501 mandoc_msg_setinfilename(NULL
);
505 resn
= mandoc_calloc(resnsz
, sizeof(*res
));
506 resn
->file
= mandoc_strdup(*argv
);
507 resn
->ipath
= SIZE_MAX
;
508 resn
->form
= FORM_SRC
;
510 if (outmode
!= OUTMODE_ONE
|| resnsz
== 1) {
511 res
= mandoc_reallocarray(res
,
512 ressz
+ resnsz
, sizeof(*res
));
513 memcpy(res
+ ressz
, resn
,
514 sizeof(*resn
) * resnsz
);
519 /* Search for the best section. */
522 for (ib
= i
= 0; i
< resnsz
; i
++) {
524 sec
+= strcspn(sec
, "123456789");
526 continue; /* No section at all. */
527 prio
= sec_prios
[sec
[0] - '1'];
528 if (search
.sec
!= NULL
) {
529 ssz
= strlen(search
.sec
);
530 if (strncmp(sec
, search
.sec
, ssz
) == 0)
533 sec
++; /* Prefer without suffix. */
535 prio
+= 10; /* Wrong dir name. */
536 if (search
.sec
!= NULL
&&
537 (strlen(sec
) <= ssz
+ 3 ||
538 strcmp(sec
+ strlen(sec
) - ssz
,
540 prio
+= 20; /* Wrong file ext. */
541 if (prio
>= best_prio
)
546 res
= mandoc_reallocarray(res
, ressz
+ 1,
548 memcpy(res
+ ressz
++, resn
+ ib
, sizeof(*resn
));
551 /* apropos(1), whatis(1): Process the full search expression. */
553 } else if (search
.argmode
!= ARG_FILE
) {
554 if (mansearch(&search
, &conf
.manpath
,
555 argc
, argv
, &res
, &ressz
) == 0)
556 usage(search
.argmode
);
559 warnx("nothing appropriate");
560 mandoc_msg_setrc(MANDOCLEVEL_BADARG
);
564 /* mandoc(1): Take command line arguments as file names. */
567 ressz
= argc
> 0 ? argc
: 1;
568 res
= mandoc_calloc(ressz
, sizeof(*res
));
569 for (i
= 0; i
< ressz
; i
++) {
571 res
[i
].file
= mandoc_strdup(argv
[i
]);
572 res
[i
].ipath
= SIZE_MAX
;
573 res
[i
].form
= FORM_SRC
;
579 for (i
= 0; i
< ressz
; i
++)
583 for (i
= 0; i
< ressz
; i
++)
584 printf("%s - %s\n", res
[i
].names
,
585 res
[i
].output
== NULL
? "" :
592 if (search
.argmode
== ARG_FILE
&& auxpaths
!= NULL
) {
593 if (strcmp(auxpaths
, "doc") == 0)
594 options
|= MPARSE_MDOC
;
595 else if (strcmp(auxpaths
, "an") == 0)
596 options
|= MPARSE_MAN
;
600 mp
= mparse_alloc(options
, os_e
, os_s
);
603 * Remember the original working directory, if possible.
604 * This will be needed if some names on the command line
605 * are page names and some are relative file names.
606 * Do not error out if the current directory is not
607 * readable: Maybe it won't be needed after all.
609 startdir
= open(".", O_RDONLY
| O_DIRECTORY
);
610 for (i
= 0; i
< ressz
; i
++) {
611 process_onefile(mp
, res
+ i
, startdir
, &outst
, &conf
);
612 if (outst
.wstop
&& mandoc_msg_getrc() != MANDOCLEVEL_OK
)
615 if (startdir
!= -1) {
616 (void)fchdir(startdir
);
619 if (conf
.output
.tag
!= NULL
&& conf
.output
.tag_found
== 0) {
620 mandoc_msg(MANDOCERR_TAG
, 0, 0, "%s", conf
.output
.tag
);
621 conf
.output
.tag
= NULL
;
623 if (outst
.outdata
!= NULL
) {
624 switch (outst
.outtype
) {
626 html_free(outst
.outdata
);
631 ascii_free(outst
.outdata
);
635 pspdf_free(outst
.outdata
);
646 mansearch_free(res
, ressz
);
647 if (search
.argmode
!= ARG_FILE
)
650 if (outst
.tag_files
!= NULL
) {
651 if (term_tag_close() != -1 &&
652 conf
.output
.outfilename
== NULL
&&
653 conf
.output
.tagfilename
== NULL
)
654 run_pager(&outst
, conf
.output
.tag
);
656 } else if (outst
.had_output
&& outst
.outtype
!= OUTT_LINT
)
657 mandoc_msg_summary();
659 return (int)mandoc_msg_getrc();
663 usage(enum argmode argmode
)
667 fputs("usage: mandoc [-ac] [-I os=name] "
668 "[-K encoding] [-mdoc | -man] [-O options]\n"
669 "\t [-T output] [-W level] [file ...]\n", stderr
);
672 fputs("usage: man [-acfhklw] [-C file] [-M path] "
673 "[-m path] [-S subsection]\n"
674 "\t [[-s] section] name ...\n", stderr
);
677 fputs("usage: whatis [-afk] [-C file] "
678 "[-M path] [-m path] [-O outkey] [-S arch]\n"
679 "\t [-s section] name ...\n", stderr
);
682 fputs("usage: apropos [-afk] [-C file] "
683 "[-M path] [-m path] [-O outkey] [-S arch]\n"
684 "\t [-s section] expression ...\n", stderr
);
687 exit((int)MANDOCLEVEL_BADARG
);
691 glob_esc(char **dst
, const char *src
, const char *suffix
)
693 while (*src
!= '\0') {
694 if (strchr("*?[", *src
) != NULL
)
698 while (*suffix
!= '\0')
699 *(*dst
)++ = *suffix
++;
703 fs_lookup(const struct manpaths
*paths
, size_t ipath
,
704 const char *sec
, const char *arch
, const char *name
,
705 struct manpage
**res
, size_t *ressz
)
709 struct manpage
*page
;
714 const char *const slman
= "/man";
715 const char *const slash
= "/";
716 const char *const sglob
= ".[01-9]*";
719 mandoc_asprintf(&file
, "%s/man%s/%s.%s",
720 paths
->paths
[ipath
], sec
, name
, sec
);
721 if (stat(file
, &sb
) != -1)
725 mandoc_asprintf(&file
, "%s/cat%s/%s.0",
726 paths
->paths
[ipath
], sec
, name
);
727 if (stat(file
, &sb
) != -1) {
734 mandoc_asprintf(&file
, "%s/man%s/%s/%s.%s",
735 paths
->paths
[ipath
], sec
, arch
, name
, sec
);
736 if (stat(file
, &sb
) != -1)
741 cp
= file
= mandoc_malloc(strlen(paths
->paths
[ipath
]) * 2 +
742 strlen(slman
) + strlen(sec
) * 2 + strlen(slash
) +
743 strlen(name
) * 2 + strlen(sglob
) + 1);
744 glob_esc(&cp
, paths
->paths
[ipath
], slman
);
745 glob_esc(&cp
, sec
, slash
);
746 glob_esc(&cp
, name
, sglob
);
748 globres
= glob(file
, 0, NULL
, &globinfo
);
749 if (globres
!= 0 && globres
!= GLOB_NOMATCH
)
750 mandoc_msg(MANDOCERR_GLOB
, 0, 0,
751 "%s: %s", file
, strerror(errno
));
754 file
= mandoc_strdup(*globinfo
.gl_pathv
);
757 if (stat(file
, &sb
) != -1)
761 if (res
!= NULL
|| ipath
+ 1 != paths
->sz
)
764 mandoc_asprintf(&file
, "%s.%s", name
, sec
);
765 globres
= stat(file
, &sb
);
770 warnx("outdated mandoc.db lacks %s(%s) entry, run %s %s",
771 name
, sec
, BINM_MAKEWHATIS
, paths
->paths
[ipath
]);
776 *res
= mandoc_reallocarray(*res
, ++*ressz
, sizeof(**res
));
777 page
= *res
+ (*ressz
- 1);
781 page
->bits
= NAME_FILE
& NAME_MASK
;
783 page
->sec
= (*sec
>= '1' && *sec
<= '9') ? *sec
- '1' + 1 : 10;
789 fs_search(const struct mansearch
*cfg
, const struct manpaths
*paths
,
790 const char *name
, struct manpage
**res
, size_t *ressz
)
792 const char *const sections
[] =
793 {"1", "8", "6", "2", "3", "5", "7", "4", "9", "3p"};
794 const size_t nsec
= sizeof(sections
)/sizeof(sections
[0]);
798 assert(cfg
->argmode
== ARG_NAME
);
802 for (ipath
= 0; ipath
< paths
->sz
; ipath
++) {
803 if (cfg
->sec
!= NULL
) {
804 if (fs_lookup(paths
, ipath
, cfg
->sec
, cfg
->arch
,
805 name
, res
, ressz
) != -1 && cfg
->firstmatch
)
808 for (isec
= 0; isec
< nsec
; isec
++)
809 if (fs_lookup(paths
, ipath
, sections
[isec
],
810 cfg
->arch
, name
, res
, ressz
) != -1 &&
819 process_onefile(struct mparse
*mp
, struct manpage
*resp
, int startdir
,
820 struct outstate
*outst
, struct manconf
*conf
)
825 * Changing directories is not needed in ARG_FILE mode.
826 * Do it on a best-effort basis. Even in case of
827 * failure, some functionality may still work.
829 if (resp
->ipath
!= SIZE_MAX
)
830 (void)chdir(conf
->manpath
.paths
[resp
->ipath
]);
831 else if (startdir
!= -1)
832 (void)fchdir(startdir
);
834 mandoc_msg_setinfilename(resp
->file
);
835 if (resp
->file
!= NULL
) {
836 if ((fd
= mparse_open(mp
, resp
->file
)) == -1) {
837 mandoc_msg(resp
->ipath
== SIZE_MAX
?
838 MANDOCERR_BADARG_BAD
: MANDOCERR_OPEN
,
839 0, 0, "%s", strerror(errno
));
840 mandoc_msg_setinfilename(NULL
);
846 if (outst
->use_pager
) {
847 outst
->use_pager
= 0;
848 outst
->tag_files
= term_tag_init(conf
->output
.outfilename
,
849 outst
->outtype
== OUTT_HTML
? ".html" : "",
850 conf
->output
.tagfilename
);
852 if ((conf
->output
.outfilename
!= NULL
||
853 conf
->output
.tagfilename
!= NULL
) &&
854 pledge("stdio rpath cpath", NULL
) == -1) {
855 mandoc_msg(MANDOCERR_PLEDGE
, 0, 0,
856 "%s", strerror(errno
));
857 exit(mandoc_msg_getrc());
861 if (outst
->had_output
&& outst
->outtype
<= OUTT_UTF8
) {
862 if (outst
->outdata
== NULL
)
863 outdata_alloc(outst
, &conf
->output
);
864 terminal_sepline(outst
->outdata
);
867 if (resp
->form
== FORM_SRC
)
868 parse(mp
, fd
, resp
->file
, outst
, &conf
->output
);
870 passthrough(fd
, conf
->output
.synopsisonly
);
871 outst
->had_output
= 1;
874 if (ferror(stdout
)) {
875 if (outst
->tag_files
!= NULL
) {
876 mandoc_msg(MANDOCERR_WRITE
, 0, 0, "%s: %s",
877 outst
->tag_files
->ofn
, strerror(errno
));
879 outst
->tag_files
= NULL
;
881 mandoc_msg(MANDOCERR_WRITE
, 0, 0, "%s",
884 mandoc_msg_setinfilename(NULL
);
888 parse(struct mparse
*mp
, int fd
, const char *file
,
889 struct outstate
*outst
, struct manoutput
*outconf
)
892 struct roff_meta
*meta
;
903 mparse_readfd(mp
, fd
, file
);
904 if (fd
!= STDIN_FILENO
)
908 * With -Wstop and warnings or errors of at least the requested
909 * level, do not produce output.
912 if (outst
->wstop
&& mandoc_msg_getrc() != MANDOCLEVEL_OK
)
915 if (outst
->outdata
== NULL
)
916 outdata_alloc(outst
, outconf
);
917 else if (outst
->outtype
== OUTT_HTML
)
918 html_reset(outst
->outdata
);
921 meta
= mparse_result(mp
);
923 /* Execute the out device, if it exists. */
925 outst
->had_output
= 1;
926 if (meta
->macroset
== MACROSET_MDOC
) {
927 switch (outst
->outtype
) {
929 html_mdoc(outst
->outdata
, meta
);
932 tree_mdoc(outst
->outdata
, meta
);
935 man_mdoc(outst
->outdata
, meta
);
942 terminal_mdoc(outst
->outdata
, meta
);
945 markdown_mdoc(outst
->outdata
, meta
);
951 if (meta
->macroset
== MACROSET_MAN
) {
952 switch (outst
->outtype
) {
954 html_man(outst
->outdata
, meta
);
957 tree_man(outst
->outdata
, meta
);
967 terminal_man(outst
->outdata
, meta
);
973 if (outconf
->tag
!= NULL
&& outconf
->tag_found
== 0 &&
974 tag_exists(outconf
->tag
))
975 outconf
->tag_found
= 1;
976 if (mandoc_msg_getmin() < MANDOCERR_STYLE
)
983 static struct manpaths paths
;
984 struct mansearch search
;
985 struct mandoc_xr
*xr
;
989 manpath_base(&paths
);
991 for (xr
= mandoc_xr_get(); xr
!= NULL
; xr
= xr
->next
) {
995 search
.sec
= xr
->sec
;
996 search
.outkey
= NULL
;
997 search
.argmode
= ARG_NAME
;
998 search
.firstmatch
= 1;
999 if (mansearch(&search
, &paths
, 1, &xr
->name
, NULL
, &sz
))
1001 if (fs_search(&search
, &paths
, xr
->name
, NULL
, &sz
) != -1)
1004 mandoc_msg(MANDOCERR_XR_BAD
, xr
->line
,
1005 xr
->pos
+ 1, "Xr %s %s", xr
->name
, xr
->sec
);
1007 mandoc_msg(MANDOCERR_XR_BAD
, xr
->line
,
1008 xr
->pos
+ 1, "Xr %s %s (%d times)",
1009 xr
->name
, xr
->sec
, xr
->count
);
1014 outdata_alloc(struct outstate
*outst
, struct manoutput
*outconf
)
1016 switch (outst
->outtype
) {
1018 outst
->outdata
= html_alloc(outconf
);
1021 outst
->outdata
= utf8_alloc(outconf
);
1024 outst
->outdata
= locale_alloc(outconf
);
1027 outst
->outdata
= ascii_alloc(outconf
);
1030 outst
->outdata
= pdf_alloc(outconf
);
1033 outst
->outdata
= ps_alloc(outconf
);
1041 passthrough(int fd
, int synopsis_only
)
1043 const char synb
[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
1044 const char synr
[] = "SYNOPSIS";
1049 ssize_t len
, written
;
1056 if (fflush(stdout
) == EOF
) {
1057 mandoc_msg(MANDOCERR_FFLUSH
, 0, 0, "%s", strerror(errno
));
1060 if ((stream
= fdopen(fd
, "r")) == NULL
) {
1062 mandoc_msg(MANDOCERR_FDOPEN
, 0, 0, "%s", strerror(errno
));
1067 while ((len
= getline(&line
, &linesz
, stream
)) != -1) {
1070 if (synopsis_only
) {
1072 if ( ! isspace((unsigned char)*cp
))
1074 while (isspace((unsigned char)*cp
)) {
1079 if (strcmp(cp
, synb
) == 0 ||
1080 strcmp(cp
, synr
) == 0)
1085 for (; len
> 0; len
-= written
) {
1086 if ((written
= write(STDOUT_FILENO
, cp
, len
)) == -1) {
1087 mandoc_msg(MANDOCERR_WRITE
, 0, 0,
1088 "%s", strerror(errno
));
1094 mandoc_msg(MANDOCERR_GETLINE
, lno
, 0, "%s", strerror(errno
));
1103 woptions(char *arg
, enum mandoc_os
*os_e
, int *wstop
)
1106 const char *toks
[11];
1112 toks
[4] = "warning";
1116 toks
[8] = "openbsd";
1122 switch (getsubopt(&arg
, (char * const *)toks
, &v
)) {
1128 mandoc_msg_setmin(MANDOCERR_BASE
);
1131 mandoc_msg_setmin(MANDOCERR_STYLE
);
1134 mandoc_msg_setmin(MANDOCERR_WARNING
);
1137 mandoc_msg_setmin(MANDOCERR_ERROR
);
1140 mandoc_msg_setmin(MANDOCERR_UNSUPP
);
1143 mandoc_msg_setmin(MANDOCERR_BADARG
);
1146 mandoc_msg_setmin(MANDOCERR_BASE
);
1147 *os_e
= MANDOC_OS_OPENBSD
;
1150 mandoc_msg_setmin(MANDOCERR_BASE
);
1151 *os_e
= MANDOC_OS_NETBSD
;
1154 mandoc_msg(MANDOCERR_BADARG_BAD
, 0, 0, "-W %s", o
);
1162 * Wait until moved to the foreground,
1163 * then fork the pager and wait for the user to close it.
1166 run_pager(struct outstate
*outst
, char *tag_target
)
1169 pid_t man_pgid
, tc_pgid
;
1170 pid_t pager_pid
, wait_pid
;
1172 man_pgid
= getpgid(0);
1173 outst
->tag_files
->tcpgid
=
1174 man_pgid
== getpid() ? getpgid(getppid()) : man_pgid
;
1179 /* Stop here until moved to the foreground. */
1181 tc_pgid
= tcgetpgrp(STDOUT_FILENO
);
1182 if (tc_pgid
!= man_pgid
) {
1183 if (tc_pgid
== pager_pid
) {
1184 (void)tcsetpgrp(STDOUT_FILENO
, man_pgid
);
1185 if (signum
== SIGTTIN
)
1188 outst
->tag_files
->tcpgid
= tc_pgid
;
1193 /* Once in the foreground, activate the pager. */
1196 (void)tcsetpgrp(STDOUT_FILENO
, pager_pid
);
1197 kill(pager_pid
, SIGCONT
);
1199 pager_pid
= spawn_pager(outst
, tag_target
);
1201 /* Wait for the pager to stop or exit. */
1203 while ((wait_pid
= waitpid(pager_pid
, &status
,
1204 WUNTRACED
)) == -1 && errno
== EINTR
)
1207 if (wait_pid
== -1) {
1208 mandoc_msg(MANDOCERR_WAIT
, 0, 0,
1209 "%s", strerror(errno
));
1212 if (!WIFSTOPPED(status
))
1215 signum
= WSTOPSIG(status
);
1220 spawn_pager(struct outstate
*outst
, char *tag_target
)
1222 const struct timespec timeout
= { 0, 100000000 }; /* 0.1s */
1223 #define MAX_PAGER_ARGS 16
1224 char *argv
[MAX_PAGER_ARGS
];
1233 assert(outst
->tag_files
->ofd
== -1);
1234 assert(outst
->tag_files
->tfs
== NULL
);
1236 pager
= getenv("MANPAGER");
1237 if (pager
== NULL
|| *pager
== '\0')
1238 pager
= getenv("PAGER");
1239 if (pager
== NULL
|| *pager
== '\0')
1241 cp
= mandoc_strdup(pager
);
1244 * Parse the pager command into words.
1245 * Intentionally do not do anything fancy here.
1249 while (argc
+ 5 < MAX_PAGER_ARGS
) {
1251 cp
= strchr(cp
, ' ');
1261 /* For less(1), use the tag file. */
1265 if (*outst
->tag_files
->tfn
!= '\0' &&
1266 (cmdlen
= strlen(argv
[0])) >= 4) {
1267 cp
= argv
[0] + cmdlen
- 4;
1268 if (strcmp(cp
, "less") == 0) {
1269 argv
[argc
++] = mandoc_strdup("-T");
1270 argv
[argc
++] = outst
->tag_files
->tfn
;
1271 if (tag_target
!= NULL
) {
1272 argv
[argc
++] = mandoc_strdup("-t");
1273 argv
[argc
++] = tag_target
;
1280 if (outst
->outtype
== OUTT_HTML
&& tag_target
!= NULL
)
1281 mandoc_asprintf(&argv
[argc
], "file://%s#%s",
1282 outst
->tag_files
->ofn
, tag_target
);
1284 argv
[argc
] = outst
->tag_files
->ofn
;
1289 switch (pager_pid
= fork()) {
1291 mandoc_msg(MANDOCERR_FORK
, 0, 0, "%s", strerror(errno
));
1292 exit(mandoc_msg_getrc());
1296 (void)setpgid(pager_pid
, 0);
1297 (void)tcsetpgrp(STDOUT_FILENO
, pager_pid
);
1299 if (pledge("stdio rpath tmppath tty proc", NULL
) == -1) {
1300 mandoc_msg(MANDOCERR_PLEDGE
, 0, 0,
1301 "%s", strerror(errno
));
1302 exit(mandoc_msg_getrc());
1305 outst
->tag_files
->pager_pid
= pager_pid
;
1310 * The child process becomes the pager.
1311 * Do not start it before controlling the terminal.
1314 while (tcgetpgrp(STDOUT_FILENO
) != getpid())
1315 nanosleep(&timeout
, NULL
);
1317 execvp(argv
[0], argv
);
1318 mandoc_msg(MANDOCERR_EXEC
, 0, 0, "%s: %s", argv
[0], strerror(errno
));
1319 _exit(mandoc_msg_getrc());