]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
1 /* $Id: main.c,v 1.345 2020/03/13 15:32:28 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"
60 #include "mansearch.h"
71 OUTT_ASCII
= 0, /* -Tascii */
72 OUTT_LOCALE
, /* -Tlocale */
73 OUTT_UTF8
, /* -Tutf8 */
74 OUTT_TREE
, /* -Ttree */
76 OUTT_HTML
, /* -Thtml */
77 OUTT_MARKDOWN
, /* -Tmarkdown */
78 OUTT_LINT
, /* -Tlint */
84 struct tag_files
*tag_files
; /* Tagging state variables. */
85 void *outdata
; /* data for output */
87 int wstop
; /* stop after a file with a warning */
88 int had_output
; /* Some output was generated. */
89 enum outt outtype
; /* which output to use */
93 int mandocdb(int, char *[]);
95 static void check_xr(void);
96 static int fs_lookup(const struct manpaths
*,
97 size_t ipath
, const char *,
98 const char *, const char *,
99 struct manpage
**, size_t *);
100 static int fs_search(const struct mansearch
*,
101 const struct manpaths
*, const char *,
102 struct manpage
**, size_t *);
103 static void glob_esc(char **, const char *, const char *);
104 static void outdata_alloc(struct outstate
*, struct manoutput
*);
105 static void parse(struct mparse
*, int, const char *,
106 struct outstate
*, struct manoutput
*);
107 static void passthrough(int, int);
108 static void process_onefile(struct mparse
*, struct manpage
*,
109 int, struct outstate
*, struct manconf
*);
110 static void run_pager(struct tag_files
*);
111 static pid_t
spawn_pager(struct tag_files
*);
112 static void usage(enum argmode
) __attribute__((__noreturn__
));
113 static int woptions(char *, enum mandoc_os
*, int *);
115 static const int sec_prios
[] = {1, 4, 5, 8, 6, 3, 7, 2, 9};
116 static char help_arg
[] = "help";
117 static char *help_argv
[] = {help_arg
, NULL
};
121 main(int argc
, char *argv
[])
123 struct manconf conf
; /* Manpaths and output options. */
124 struct outstate outst
; /* Output state. */
125 struct winsize ws
; /* Result of ioctl(TIOCGWINSZ). */
126 struct mansearch search
; /* Search options. */
127 struct manpage
*res
; /* Complete list of search results. */
128 struct manpage
*resn
; /* Search results for one name. */
129 struct mparse
*mp
; /* Opaque parser object. */
130 const char *conf_file
; /* -C: alternate config file. */
131 const char *os_s
; /* -I: Operating system for display. */
132 const char *progname
, *sec
;
133 char *defpaths
; /* -M: override manpaths. */
134 char *auxpaths
; /* -m: additional manpaths. */
135 char *oarg
; /* -O: output option string. */
136 char *tagarg
; /* -O tag: default value. */
138 size_t ressz
; /* Number of elements in res[]. */
139 size_t resnsz
; /* Number of elements in resn[]. */
141 int options
; /* Parser options. */
142 int show_usage
; /* Invalid argument: give up. */
146 enum mandoc_os os_e
; /* Check base system conventions. */
147 enum outmode outmode
; /* According to command line. */
150 progname
= getprogname();
153 progname
= mandoc_strdup("mandoc");
154 else if ((progname
= strrchr(argv
[0], '/')) == NULL
)
158 setprogname(progname
);
161 mandoc_msg_setoutfile(stderr
);
162 if (strncmp(progname
, "mandocdb", 8) == 0 ||
163 strcmp(progname
, BINM_MAKEWHATIS
) == 0)
164 return mandocdb(argc
, argv
);
167 if (pledge("stdio rpath tmppath tty proc exec", NULL
) == -1) {
168 mandoc_msg(MANDOCERR_PLEDGE
, 0, 0, "%s", strerror(errno
));
169 return mandoc_msg_getrc();
172 #if HAVE_SANDBOX_INIT
173 if (sandbox_init(kSBXProfileNoInternet
, SANDBOX_NAMED
, NULL
) == -1)
174 errx((int)MANDOCLEVEL_SYSERR
, "sandbox_init");
177 /* Search options. */
179 memset(&conf
, 0, sizeof(conf
));
181 defpaths
= auxpaths
= NULL
;
183 memset(&search
, 0, sizeof(struct mansearch
));
184 search
.outkey
= "Nd";
187 if (strcmp(progname
, BINM_MAN
) == 0)
188 search
.argmode
= ARG_NAME
;
189 else if (strcmp(progname
, BINM_APROPOS
) == 0)
190 search
.argmode
= ARG_EXPR
;
191 else if (strcmp(progname
, BINM_WHATIS
) == 0)
192 search
.argmode
= ARG_WORD
;
193 else if (strncmp(progname
, "help", 4) == 0)
194 search
.argmode
= ARG_NAME
;
196 search
.argmode
= ARG_FILE
;
198 /* Parser options. */
200 options
= MPARSE_SO
| MPARSE_UTF8
| MPARSE_LATIN1
;
201 os_e
= MANDOC_OS_OTHER
;
204 /* Formatter options. */
206 memset(&outst
, 0, sizeof(outst
));
207 outst
.tag_files
= NULL
;
208 outst
.outtype
= OUTT_LOCALE
;
212 outmode
= OUTMODE_DEF
;
214 while ((c
= getopt(argc
, argv
,
215 "aC:cfhI:iK:klM:m:O:S:s:T:VW:w")) != -1) {
216 if (c
== 'i' && search
.argmode
== ARG_EXPR
) {
222 outmode
= OUTMODE_ALL
;
231 search
.argmode
= ARG_WORD
;
234 conf
.output
.synopsisonly
= 1;
236 outmode
= OUTMODE_ALL
;
239 if (strncmp(optarg
, "os=", 3) != 0) {
240 mandoc_msg(MANDOCERR_BADARG_BAD
, 0, 0,
242 return mandoc_msg_getrc();
245 mandoc_msg(MANDOCERR_BADARG_DUPE
, 0, 0,
247 return mandoc_msg_getrc();
252 options
&= ~(MPARSE_UTF8
| MPARSE_LATIN1
);
253 if (strcmp(optarg
, "utf-8") == 0)
254 options
|= MPARSE_UTF8
;
255 else if (strcmp(optarg
, "iso-8859-1") == 0)
256 options
|= MPARSE_LATIN1
;
257 else if (strcmp(optarg
, "us-ascii") != 0) {
258 mandoc_msg(MANDOCERR_BADARG_BAD
, 0, 0,
260 return mandoc_msg_getrc();
264 search
.argmode
= ARG_EXPR
;
267 search
.argmode
= ARG_FILE
;
268 outmode
= OUTMODE_ALL
;
280 search
.arch
= optarg
;
286 if (strcmp(optarg
, "ascii") == 0)
287 outst
.outtype
= OUTT_ASCII
;
288 else if (strcmp(optarg
, "lint") == 0) {
289 outst
.outtype
= OUTT_LINT
;
290 mandoc_msg_setoutfile(stdout
);
291 mandoc_msg_setmin(MANDOCERR_BASE
);
292 } else if (strcmp(optarg
, "tree") == 0)
293 outst
.outtype
= OUTT_TREE
;
294 else if (strcmp(optarg
, "man") == 0)
295 outst
.outtype
= OUTT_MAN
;
296 else if (strcmp(optarg
, "html") == 0)
297 outst
.outtype
= OUTT_HTML
;
298 else if (strcmp(optarg
, "markdown") == 0)
299 outst
.outtype
= OUTT_MARKDOWN
;
300 else if (strcmp(optarg
, "utf8") == 0)
301 outst
.outtype
= OUTT_UTF8
;
302 else if (strcmp(optarg
, "locale") == 0)
303 outst
.outtype
= OUTT_LOCALE
;
304 else if (strcmp(optarg
, "ps") == 0)
305 outst
.outtype
= OUTT_PS
;
306 else if (strcmp(optarg
, "pdf") == 0)
307 outst
.outtype
= OUTT_PDF
;
309 mandoc_msg(MANDOCERR_BADARG_BAD
, 0, 0,
311 return mandoc_msg_getrc();
315 if (woptions(optarg
, &os_e
, &outst
.wstop
) == -1)
316 return mandoc_msg_getrc();
319 outmode
= OUTMODE_FLN
;
328 usage(search
.argmode
);
330 /* Postprocess options. */
334 switch (search
.argmode
) {
336 outmode
= OUTMODE_ALL
;
340 outmode
= OUTMODE_ONE
;
343 outmode
= OUTMODE_LST
;
348 if (search
.argmode
== ARG_FILE
)
349 outmode
= OUTMODE_ALL
;
359 if (outmode
== OUTMODE_LST
)
360 search
.outkey
= oarg
;
362 while (oarg
!= NULL
) {
363 if (manconf_output(&conf
.output
,
364 strsep(&oarg
, ","), 0) == -1)
365 return mandoc_msg_getrc();
370 if (outst
.outtype
!= OUTT_TREE
|| conf
.output
.noval
== 0)
371 options
|= MPARSE_VALIDATE
;
373 if (outmode
== OUTMODE_FLN
||
374 outmode
== OUTMODE_LST
||
375 !isatty(STDOUT_FILENO
))
378 if (outst
.use_pager
&&
379 (conf
.output
.width
== 0 || conf
.output
.indent
== 0) &&
380 ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &ws
) != -1 &&
382 if (conf
.output
.width
== 0 && ws
.ws_col
< 79)
383 conf
.output
.width
= ws
.ws_col
- 1;
384 if (conf
.output
.indent
== 0 && ws
.ws_col
< 66)
385 conf
.output
.indent
= 3;
389 if (outst
.use_pager
== 0) {
390 if (pledge("stdio rpath", NULL
) == -1) {
391 mandoc_msg(MANDOCERR_PLEDGE
, 0, 0,
392 "%s", strerror(errno
));
393 return mandoc_msg_getrc();
398 /* Parse arguments. */
406 * Quirks for help(1) and man(1),
407 * in particular for a section argument without -s.
410 if (search
.argmode
== ARG_NAME
) {
411 if (*progname
== 'h') {
416 } else if (argc
> 1 &&
417 ((uc
= (unsigned char *)argv
[0]) != NULL
) &&
418 ((isdigit(uc
[0]) && (uc
[1] == '\0' ||
420 (uc
[0] == 'n' && uc
[1] == '\0'))) {
421 search
.sec
= (char *)uc
;
425 if (search
.arch
== NULL
)
426 search
.arch
= getenv("MACHINE");
428 if (search
.arch
== NULL
)
429 search
.arch
= MACHINE
;
431 if (outmode
== OUTMODE_ONE
)
432 search
.firstmatch
= 1;
436 * Use the first argument for -O tag in addition to
437 * using it as a search term for man(1) or apropos(1).
440 if (conf
.output
.tag
!= NULL
&& *conf
.output
.tag
== '\0') {
441 tagarg
= argc
> 0 && search
.argmode
== ARG_EXPR
?
442 strchr(*argv
, '=') : NULL
;
443 conf
.output
.tag
= tagarg
== NULL
? *argv
: tagarg
+ 1;
446 /* Read the configuration file. */
448 if (search
.argmode
!= ARG_FILE
)
449 manconf_parse(&conf
, conf_file
, defpaths
, auxpaths
);
451 /* man(1): Resolve each name individually. */
453 if (search
.argmode
== ARG_NAME
) {
455 if (outmode
!= OUTMODE_FLN
)
457 if (conf
.manpath
.sz
== 0) {
458 warnx("The manpath is empty.");
459 mandoc_msg_setrc(MANDOCLEVEL_BADARG
);
461 for (i
= 0; i
+ 1 < conf
.manpath
.sz
; i
++)
462 printf("%s:", conf
.manpath
.paths
[i
]);
463 printf("%s\n", conf
.manpath
.paths
[i
]);
466 return (int)mandoc_msg_getrc();
468 for (res
= NULL
, ressz
= 0; argc
> 0; argc
--, argv
++) {
469 (void)mansearch(&search
, &conf
.manpath
,
470 1, argv
, &resn
, &resnsz
);
472 (void)fs_search(&search
, &conf
.manpath
,
473 *argv
, &resn
, &resnsz
);
474 if (resnsz
== 0 && strchr(*argv
, '/') == NULL
) {
475 if (search
.arch
!= NULL
&&
476 arch_valid(search
.arch
, OSENUM
) == 0)
477 warnx("Unknown architecture \"%s\".",
479 else if (search
.sec
!= NULL
)
480 warnx("No entry for %s in "
481 "section %s of the manual.",
484 warnx("No entry for %s in "
485 "the manual.", *argv
);
486 mandoc_msg_setrc(MANDOCLEVEL_BADARG
);
490 if (access(*argv
, R_OK
) == -1) {
491 mandoc_msg_setinfilename(*argv
);
492 mandoc_msg(MANDOCERR_BADARG_BAD
,
493 0, 0, "%s", strerror(errno
));
494 mandoc_msg_setinfilename(NULL
);
498 resn
= mandoc_calloc(resnsz
, sizeof(*res
));
499 resn
->file
= mandoc_strdup(*argv
);
500 resn
->ipath
= SIZE_MAX
;
501 resn
->form
= FORM_SRC
;
503 if (outmode
!= OUTMODE_ONE
|| resnsz
== 1) {
504 res
= mandoc_reallocarray(res
,
505 ressz
+ resnsz
, sizeof(*res
));
506 memcpy(res
+ ressz
, resn
,
507 sizeof(*resn
) * resnsz
);
512 /* Search for the best section. */
515 for (ib
= i
= 0; i
< resnsz
; i
++) {
517 sec
+= strcspn(sec
, "123456789");
519 continue; /* No section at all. */
520 prio
= sec_prios
[sec
[0] - '1'];
521 if (search
.sec
!= NULL
) {
522 ssz
= strlen(search
.sec
);
523 if (strncmp(sec
, search
.sec
, ssz
) == 0)
526 sec
++; /* Prefer without suffix. */
528 prio
+= 10; /* Wrong dir name. */
529 if (search
.sec
!= NULL
&&
530 (strlen(sec
) <= ssz
+ 3 ||
531 strcmp(sec
+ strlen(sec
) - ssz
,
533 prio
+= 20; /* Wrong file ext. */
534 if (prio
>= best_prio
)
539 res
= mandoc_reallocarray(res
, ressz
+ 1,
541 memcpy(res
+ ressz
++, resn
+ ib
, sizeof(*resn
));
544 /* apropos(1), whatis(1): Process the full search expression. */
546 } else if (search
.argmode
!= ARG_FILE
) {
547 if (mansearch(&search
, &conf
.manpath
,
548 argc
, argv
, &res
, &ressz
) == 0)
549 usage(search
.argmode
);
552 warnx("nothing appropriate");
553 mandoc_msg_setrc(MANDOCLEVEL_BADARG
);
557 /* mandoc(1): Take command line arguments as file names. */
560 ressz
= argc
> 0 ? argc
: 1;
561 res
= mandoc_calloc(ressz
, sizeof(*res
));
562 for (i
= 0; i
< ressz
; i
++) {
564 res
[i
].file
= mandoc_strdup(argv
[i
]);
565 res
[i
].ipath
= SIZE_MAX
;
566 res
[i
].form
= FORM_SRC
;
572 for (i
= 0; i
< ressz
; i
++)
576 for (i
= 0; i
< ressz
; i
++)
577 printf("%s - %s\n", res
[i
].names
,
578 res
[i
].output
== NULL
? "" :
585 if (search
.argmode
== ARG_FILE
&& auxpaths
!= NULL
) {
586 if (strcmp(auxpaths
, "doc") == 0)
587 options
|= MPARSE_MDOC
;
588 else if (strcmp(auxpaths
, "an") == 0)
589 options
|= MPARSE_MAN
;
593 mp
= mparse_alloc(options
, os_e
, os_s
);
596 * Remember the original working directory, if possible.
597 * This will be needed if some names on the command line
598 * are page names and some are relative file names.
599 * Do not error out if the current directory is not
600 * readable: Maybe it won't be needed after all.
602 startdir
= open(".", O_RDONLY
| O_DIRECTORY
);
603 for (i
= 0; i
< ressz
; i
++) {
604 process_onefile(mp
, res
+ i
, startdir
, &outst
, &conf
);
605 if (outst
.wstop
&& mandoc_msg_getrc() != MANDOCLEVEL_OK
)
608 if (startdir
!= -1) {
609 (void)fchdir(startdir
);
612 if (outst
.outdata
!= NULL
) {
613 switch (outst
.outtype
) {
615 html_free(outst
.outdata
);
621 ascii_free(outst
.outdata
);
625 pspdf_free(outst
.outdata
);
636 mansearch_free(res
, ressz
);
637 if (search
.argmode
!= ARG_FILE
)
640 if (outst
.tag_files
!= NULL
) {
642 run_pager(outst
.tag_files
);
644 } else if (outst
.had_output
&& outst
.outtype
!= OUTT_LINT
)
645 mandoc_msg_summary();
647 return (int)mandoc_msg_getrc();
651 usage(enum argmode argmode
)
655 fputs("usage: mandoc [-ac] [-I os=name] "
656 "[-K encoding] [-mdoc | -man] [-O options]\n"
657 "\t [-T output] [-W level] [file ...]\n", stderr
);
660 fputs("usage: man [-acfhklw] [-C file] [-M path] "
661 "[-m path] [-S subsection]\n"
662 "\t [[-s] section] name ...\n", stderr
);
665 fputs("usage: whatis [-afk] [-C file] "
666 "[-M path] [-m path] [-O outkey] [-S arch]\n"
667 "\t [-s section] name ...\n", stderr
);
670 fputs("usage: apropos [-afk] [-C file] "
671 "[-M path] [-m path] [-O outkey] [-S arch]\n"
672 "\t [-s section] expression ...\n", stderr
);
675 exit((int)MANDOCLEVEL_BADARG
);
679 glob_esc(char **dst
, const char *src
, const char *suffix
)
681 while (*src
!= '\0') {
682 if (strchr("*?[", *src
) != NULL
)
686 while (*suffix
!= '\0')
687 *(*dst
)++ = *suffix
++;
691 fs_lookup(const struct manpaths
*paths
, size_t ipath
,
692 const char *sec
, const char *arch
, const char *name
,
693 struct manpage
**res
, size_t *ressz
)
697 struct manpage
*page
;
702 const char *const slman
= "/man";
703 const char *const slash
= "/";
704 const char *const sglob
= ".[01-9]*";
707 mandoc_asprintf(&file
, "%s/man%s/%s.%s",
708 paths
->paths
[ipath
], sec
, name
, sec
);
709 if (stat(file
, &sb
) != -1)
713 mandoc_asprintf(&file
, "%s/cat%s/%s.0",
714 paths
->paths
[ipath
], sec
, name
);
715 if (stat(file
, &sb
) != -1) {
722 mandoc_asprintf(&file
, "%s/man%s/%s/%s.%s",
723 paths
->paths
[ipath
], sec
, arch
, name
, sec
);
724 if (stat(file
, &sb
) != -1)
729 cp
= file
= mandoc_malloc(strlen(paths
->paths
[ipath
]) * 2 +
730 strlen(slman
) + strlen(sec
) * 2 + strlen(slash
) +
731 strlen(name
) * 2 + strlen(sglob
) + 1);
732 glob_esc(&cp
, paths
->paths
[ipath
], slman
);
733 glob_esc(&cp
, sec
, slash
);
734 glob_esc(&cp
, name
, sglob
);
736 globres
= glob(file
, 0, NULL
, &globinfo
);
737 if (globres
!= 0 && globres
!= GLOB_NOMATCH
)
738 mandoc_msg(MANDOCERR_GLOB
, 0, 0,
739 "%s: %s", file
, strerror(errno
));
742 file
= mandoc_strdup(*globinfo
.gl_pathv
);
745 if (stat(file
, &sb
) != -1)
749 if (res
!= NULL
|| ipath
+ 1 != paths
->sz
)
752 mandoc_asprintf(&file
, "%s.%s", name
, sec
);
753 globres
= stat(file
, &sb
);
758 warnx("outdated mandoc.db lacks %s(%s) entry, run %s %s",
759 name
, sec
, BINM_MAKEWHATIS
, paths
->paths
[ipath
]);
764 *res
= mandoc_reallocarray(*res
, ++*ressz
, sizeof(**res
));
765 page
= *res
+ (*ressz
- 1);
769 page
->bits
= NAME_FILE
& NAME_MASK
;
771 page
->sec
= (*sec
>= '1' && *sec
<= '9') ? *sec
- '1' + 1 : 10;
777 fs_search(const struct mansearch
*cfg
, const struct manpaths
*paths
,
778 const char *name
, struct manpage
**res
, size_t *ressz
)
780 const char *const sections
[] =
781 {"1", "8", "6", "2", "3", "5", "7", "4", "9", "3p"};
782 const size_t nsec
= sizeof(sections
)/sizeof(sections
[0]);
786 assert(cfg
->argmode
== ARG_NAME
);
790 for (ipath
= 0; ipath
< paths
->sz
; ipath
++) {
791 if (cfg
->sec
!= NULL
) {
792 if (fs_lookup(paths
, ipath
, cfg
->sec
, cfg
->arch
,
793 name
, res
, ressz
) != -1 && cfg
->firstmatch
)
796 for (isec
= 0; isec
< nsec
; isec
++)
797 if (fs_lookup(paths
, ipath
, sections
[isec
],
798 cfg
->arch
, name
, res
, ressz
) != -1 &&
807 process_onefile(struct mparse
*mp
, struct manpage
*resp
, int startdir
,
808 struct outstate
*outst
, struct manconf
*conf
)
813 * Changing directories is not needed in ARG_FILE mode.
814 * Do it on a best-effort basis. Even in case of
815 * failure, some functionality may still work.
817 if (resp
->ipath
!= SIZE_MAX
)
818 (void)chdir(conf
->manpath
.paths
[resp
->ipath
]);
819 else if (startdir
!= -1)
820 (void)fchdir(startdir
);
822 mandoc_msg_setinfilename(resp
->file
);
823 if (resp
->file
!= NULL
) {
824 if ((fd
= mparse_open(mp
, resp
->file
)) == -1) {
825 mandoc_msg(resp
->ipath
== SIZE_MAX
?
826 MANDOCERR_BADARG_BAD
: MANDOCERR_OPEN
,
827 0, 0, "%s", strerror(errno
));
828 mandoc_msg_setinfilename(NULL
);
834 if (outst
->outtype
<= OUTT_UTF8
) {
835 if (outst
->use_pager
) {
836 outst
->use_pager
= 0;
837 outst
->tag_files
= term_tag_init(conf
->output
.tag
);
839 if (outst
->had_output
) {
840 if (outst
->outdata
== NULL
)
841 outdata_alloc(outst
, &conf
->output
);
842 terminal_sepline(outst
->outdata
);
846 if (resp
->form
== FORM_SRC
)
847 parse(mp
, fd
, resp
->file
, outst
, &conf
->output
);
849 passthrough(fd
, conf
->output
.synopsisonly
);
850 outst
->had_output
= 1;
853 if (ferror(stdout
)) {
854 if (outst
->tag_files
!= NULL
) {
855 mandoc_msg(MANDOCERR_WRITE
, 0, 0, "%s: %s",
856 outst
->tag_files
->ofn
, strerror(errno
));
858 outst
->tag_files
= NULL
;
860 mandoc_msg(MANDOCERR_WRITE
, 0, 0, "%s",
863 mandoc_msg_setinfilename(NULL
);
867 parse(struct mparse
*mp
, int fd
, const char *file
,
868 struct outstate
*outst
, struct manoutput
*outconf
)
871 struct roff_meta
*meta
;
882 mparse_readfd(mp
, fd
, file
);
883 if (fd
!= STDIN_FILENO
)
887 * With -Wstop and warnings or errors of at least the requested
888 * level, do not produce output.
891 if (outst
->wstop
&& mandoc_msg_getrc() != MANDOCLEVEL_OK
)
894 if (outst
->outdata
== NULL
)
895 outdata_alloc(outst
, outconf
);
896 else if (outst
->outtype
== OUTT_HTML
)
900 meta
= mparse_result(mp
);
902 /* Execute the out device, if it exists. */
904 outst
->had_output
= 1;
905 if (meta
->macroset
== MACROSET_MDOC
) {
906 switch (outst
->outtype
) {
908 html_mdoc(outst
->outdata
, meta
);
911 tree_mdoc(outst
->outdata
, meta
);
914 man_mdoc(outst
->outdata
, meta
);
921 terminal_mdoc(outst
->outdata
, meta
);
924 markdown_mdoc(outst
->outdata
, meta
);
930 if (meta
->macroset
== MACROSET_MAN
) {
931 switch (outst
->outtype
) {
933 html_man(outst
->outdata
, meta
);
936 tree_man(outst
->outdata
, meta
);
946 terminal_man(outst
->outdata
, meta
);
952 if (mandoc_msg_getmin() < MANDOCERR_STYLE
)
959 static struct manpaths paths
;
960 struct mansearch search
;
961 struct mandoc_xr
*xr
;
965 manpath_base(&paths
);
967 for (xr
= mandoc_xr_get(); xr
!= NULL
; xr
= xr
->next
) {
971 search
.sec
= xr
->sec
;
972 search
.outkey
= NULL
;
973 search
.argmode
= ARG_NAME
;
974 search
.firstmatch
= 1;
975 if (mansearch(&search
, &paths
, 1, &xr
->name
, NULL
, &sz
))
977 if (fs_search(&search
, &paths
, xr
->name
, NULL
, &sz
) != -1)
980 mandoc_msg(MANDOCERR_XR_BAD
, xr
->line
,
981 xr
->pos
+ 1, "Xr %s %s", xr
->name
, xr
->sec
);
983 mandoc_msg(MANDOCERR_XR_BAD
, xr
->line
,
984 xr
->pos
+ 1, "Xr %s %s (%d times)",
985 xr
->name
, xr
->sec
, xr
->count
);
990 outdata_alloc(struct outstate
*outst
, struct manoutput
*outconf
)
992 switch (outst
->outtype
) {
994 outst
->outdata
= html_alloc(outconf
);
997 outst
->outdata
= utf8_alloc(outconf
);
1000 outst
->outdata
= locale_alloc(outconf
);
1003 outst
->outdata
= ascii_alloc(outconf
);
1006 outst
->outdata
= pdf_alloc(outconf
);
1009 outst
->outdata
= ps_alloc(outconf
);
1017 passthrough(int fd
, int synopsis_only
)
1019 const char synb
[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
1020 const char synr
[] = "SYNOPSIS";
1025 ssize_t len
, written
;
1032 if (fflush(stdout
) == EOF
) {
1033 mandoc_msg(MANDOCERR_FFLUSH
, 0, 0, "%s", strerror(errno
));
1036 if ((stream
= fdopen(fd
, "r")) == NULL
) {
1038 mandoc_msg(MANDOCERR_FDOPEN
, 0, 0, "%s", strerror(errno
));
1043 while ((len
= getline(&line
, &linesz
, stream
)) != -1) {
1046 if (synopsis_only
) {
1048 if ( ! isspace((unsigned char)*cp
))
1050 while (isspace((unsigned char)*cp
)) {
1055 if (strcmp(cp
, synb
) == 0 ||
1056 strcmp(cp
, synr
) == 0)
1061 for (; len
> 0; len
-= written
) {
1062 if ((written
= write(STDOUT_FILENO
, cp
, len
)) == -1) {
1063 mandoc_msg(MANDOCERR_WRITE
, 0, 0,
1064 "%s", strerror(errno
));
1070 mandoc_msg(MANDOCERR_GETLINE
, lno
, 0, "%s", strerror(errno
));
1079 woptions(char *arg
, enum mandoc_os
*os_e
, int *wstop
)
1082 const char *toks
[11];
1088 toks
[4] = "warning";
1092 toks
[8] = "openbsd";
1098 switch (getsubopt(&arg
, (char * const *)toks
, &v
)) {
1104 mandoc_msg_setmin(MANDOCERR_BASE
);
1107 mandoc_msg_setmin(MANDOCERR_STYLE
);
1110 mandoc_msg_setmin(MANDOCERR_WARNING
);
1113 mandoc_msg_setmin(MANDOCERR_ERROR
);
1116 mandoc_msg_setmin(MANDOCERR_UNSUPP
);
1119 mandoc_msg_setmin(MANDOCERR_BADARG
);
1122 mandoc_msg_setmin(MANDOCERR_BASE
);
1123 *os_e
= MANDOC_OS_OPENBSD
;
1126 mandoc_msg_setmin(MANDOCERR_BASE
);
1127 *os_e
= MANDOC_OS_NETBSD
;
1130 mandoc_msg(MANDOCERR_BADARG_BAD
, 0, 0, "-W %s", o
);
1138 * Wait until moved to the foreground,
1139 * then fork the pager and wait for the user to close it.
1142 run_pager(struct tag_files
*tag_files
)
1145 pid_t man_pgid
, tc_pgid
;
1146 pid_t pager_pid
, wait_pid
;
1148 man_pgid
= getpgid(0);
1149 tag_files
->tcpgid
= man_pgid
== getpid() ? getpgid(getppid()) :
1155 /* Stop here until moved to the foreground. */
1157 tc_pgid
= tcgetpgrp(tag_files
->ofd
);
1158 if (tc_pgid
!= man_pgid
) {
1159 if (tc_pgid
== pager_pid
) {
1160 (void)tcsetpgrp(tag_files
->ofd
, man_pgid
);
1161 if (signum
== SIGTTIN
)
1164 tag_files
->tcpgid
= tc_pgid
;
1169 /* Once in the foreground, activate the pager. */
1172 (void)tcsetpgrp(tag_files
->ofd
, pager_pid
);
1173 kill(pager_pid
, SIGCONT
);
1175 pager_pid
= spawn_pager(tag_files
);
1177 /* Wait for the pager to stop or exit. */
1179 while ((wait_pid
= waitpid(pager_pid
, &status
,
1180 WUNTRACED
)) == -1 && errno
== EINTR
)
1183 if (wait_pid
== -1) {
1184 mandoc_msg(MANDOCERR_WAIT
, 0, 0,
1185 "%s", strerror(errno
));
1188 if (!WIFSTOPPED(status
))
1191 signum
= WSTOPSIG(status
);
1196 spawn_pager(struct tag_files
*tag_files
)
1198 const struct timespec timeout
= { 0, 100000000 }; /* 0.1s */
1199 #define MAX_PAGER_ARGS 16
1200 char *argv
[MAX_PAGER_ARGS
];
1209 pager
= getenv("MANPAGER");
1210 if (pager
== NULL
|| *pager
== '\0')
1211 pager
= getenv("PAGER");
1212 if (pager
== NULL
|| *pager
== '\0')
1214 cp
= mandoc_strdup(pager
);
1217 * Parse the pager command into words.
1218 * Intentionally do not do anything fancy here.
1222 while (argc
+ 5 < MAX_PAGER_ARGS
) {
1224 cp
= strchr(cp
, ' ');
1234 /* For less(1), use the tag file. */
1238 if (*tag_files
->tfn
!= '\0' && (cmdlen
= strlen(argv
[0])) >= 4) {
1239 cp
= argv
[0] + cmdlen
- 4;
1240 if (strcmp(cp
, "less") == 0) {
1241 argv
[argc
++] = mandoc_strdup("-T");
1242 argv
[argc
++] = tag_files
->tfn
;
1243 if (tag_files
->tagname
!= NULL
) {
1244 argv
[argc
++] = mandoc_strdup("-t");
1245 argv
[argc
++] = tag_files
->tagname
;
1252 argv
[argc
++] = tag_files
->ofn
;
1255 switch (pager_pid
= fork()) {
1257 mandoc_msg(MANDOCERR_FORK
, 0, 0, "%s", strerror(errno
));
1258 exit(mandoc_msg_getrc());
1262 (void)setpgid(pager_pid
, 0);
1263 (void)tcsetpgrp(tag_files
->ofd
, pager_pid
);
1265 if (pledge("stdio rpath tmppath tty proc", NULL
) == -1) {
1266 mandoc_msg(MANDOCERR_PLEDGE
, 0, 0,
1267 "%s", strerror(errno
));
1268 exit(mandoc_msg_getrc());
1271 tag_files
->pager_pid
= pager_pid
;
1275 /* The child process becomes the pager. */
1277 if (dup2(tag_files
->ofd
, STDOUT_FILENO
) == -1) {
1278 mandoc_msg(MANDOCERR_DUP
, 0, 0, "%s", strerror(errno
));
1279 _exit(mandoc_msg_getrc());
1281 close(tag_files
->ofd
);
1282 assert(tag_files
->tfs
== NULL
);
1284 /* Do not start the pager before controlling the terminal. */
1286 while (tcgetpgrp(STDOUT_FILENO
) != getpid())
1287 nanosleep(&timeout
, NULL
);
1289 execvp(argv
[0], argv
);
1290 mandoc_msg(MANDOCERR_EXEC
, 0, 0, "%s: %s", argv
[0], strerror(errno
));
1291 _exit(mandoc_msg_getrc());