]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
1 /* $Id: main.c,v 1.361 2022/04/14 16:43:43 schwarze Exp $ */
3 * Copyright (c) 2010-2012, 2014-2021 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(struct manpaths
*);
97 static void fs_append(char **, size_t, int,
98 size_t, const char *, enum form
,
99 struct manpage
**, size_t *);
100 static int fs_lookup(const struct manpaths
*, size_t,
101 const char *, const char *, const char *,
102 struct manpage
**, size_t *);
103 static int fs_search(const struct mansearch
*,
104 const struct manpaths
*, const char *,
105 struct manpage
**, size_t *);
106 static void glob_esc(char **, const char *, const char *);
107 static void outdata_alloc(struct outstate
*, struct manoutput
*);
108 static void parse(struct mparse
*, int, const char *,
109 struct outstate
*, struct manconf
*);
110 static void passthrough(int, int);
111 static void process_onefile(struct mparse
*, struct manpage
*,
112 int, struct outstate
*, struct manconf
*);
113 static void run_pager(struct outstate
*, char *);
114 static pid_t
spawn_pager(struct outstate
*, char *);
115 static void usage(enum argmode
) __attribute__((__noreturn__
));
116 static int woptions(char *, enum mandoc_os
*, int *);
118 static const int sec_prios
[] = {1, 4, 5, 8, 6, 3, 7, 2, 9};
119 static char help_arg
[] = "help";
120 static char *help_argv
[] = {help_arg
, NULL
};
124 main(int argc
, char *argv
[])
126 struct manconf conf
; /* Manpaths and output options. */
127 struct outstate outst
; /* Output state. */
128 struct winsize ws
; /* Result of ioctl(TIOCGWINSZ). */
129 struct mansearch search
; /* Search options. */
130 struct manpage
*res
; /* Complete list of search results. */
131 struct manpage
*resn
; /* Search results for one name. */
132 struct mparse
*mp
; /* Opaque parser object. */
133 const char *conf_file
; /* -C: alternate config file. */
134 const char *os_s
; /* -I: Operating system for display. */
135 const char *progname
, *sec
, *ep
;
136 char *defpaths
; /* -M: override manpaths. */
137 char *auxpaths
; /* -m: additional manpaths. */
138 char *oarg
; /* -O: output option string. */
139 char *tagarg
; /* -O tag: default value. */
141 size_t ressz
; /* Number of elements in res[]. */
142 size_t resnsz
; /* Number of elements in resn[]. */
144 int options
; /* Parser options. */
145 int show_usage
; /* Invalid argument: give up. */
149 enum mandoc_os os_e
; /* Check base system conventions. */
150 enum outmode outmode
; /* According to command line. */
153 mandoc_dbg_init(argc
, argv
);
156 progname
= getprogname();
160 else if ((progname
= strrchr(argv
[0], '/')) == NULL
)
164 setprogname(progname
);
167 mandoc_msg_setoutfile(stderr
);
168 if (strncmp(progname
, "mandocdb", 8) == 0 ||
169 strcmp(progname
, BINM_MAKEWHATIS
) == 0)
170 return mandocdb(argc
, argv
);
173 if (pledge("stdio rpath wpath cpath tmppath tty proc exec", NULL
) == -1) {
174 mandoc_msg(MANDOCERR_PLEDGE
, 0, 0, "%s", strerror(errno
));
175 return mandoc_msg_getrc();
178 #if HAVE_SANDBOX_INIT
179 if (sandbox_init(kSBXProfileNoInternet
, SANDBOX_NAMED
, NULL
) == -1)
180 errx((int)MANDOCLEVEL_SYSERR
, "sandbox_init");
183 /* Search options. */
185 memset(&conf
, 0, sizeof(conf
));
187 defpaths
= auxpaths
= NULL
;
189 memset(&search
, 0, sizeof(struct mansearch
));
190 search
.outkey
= "Nd";
193 if (strcmp(progname
, BINM_MAN
) == 0)
194 search
.argmode
= ARG_NAME
;
195 else if (strcmp(progname
, BINM_APROPOS
) == 0)
196 search
.argmode
= ARG_EXPR
;
197 else if (strcmp(progname
, BINM_WHATIS
) == 0)
198 search
.argmode
= ARG_WORD
;
199 else if (strncmp(progname
, "help", 4) == 0)
200 search
.argmode
= ARG_NAME
;
202 search
.argmode
= ARG_FILE
;
204 /* Parser options. */
206 options
= MPARSE_SO
| MPARSE_UTF8
| MPARSE_LATIN1
;
207 os_e
= MANDOC_OS_OTHER
;
210 /* Formatter options. */
212 memset(&outst
, 0, sizeof(outst
));
213 outst
.tag_files
= NULL
;
214 outst
.outtype
= OUTT_LOCALE
;
218 outmode
= OUTMODE_DEF
;
220 while ((c
= getopt(argc
, argv
,
221 "aC:cfhI:iK:klM:m:O:S:s:T:VW:w")) != -1) {
222 if (c
== 'i' && search
.argmode
== ARG_EXPR
) {
228 outmode
= OUTMODE_ALL
;
237 search
.argmode
= ARG_WORD
;
240 conf
.output
.synopsisonly
= 1;
242 outmode
= OUTMODE_ALL
;
245 if (strncmp(optarg
, "os=", 3) != 0) {
246 mandoc_msg(MANDOCERR_BADARG_BAD
, 0, 0,
248 return mandoc_msg_getrc();
251 mandoc_msg(MANDOCERR_BADARG_DUPE
, 0, 0,
253 return mandoc_msg_getrc();
258 options
&= ~(MPARSE_UTF8
| MPARSE_LATIN1
);
259 if (strcmp(optarg
, "utf-8") == 0)
260 options
|= MPARSE_UTF8
;
261 else if (strcmp(optarg
, "iso-8859-1") == 0)
262 options
|= MPARSE_LATIN1
;
263 else if (strcmp(optarg
, "us-ascii") != 0) {
264 mandoc_msg(MANDOCERR_BADARG_BAD
, 0, 0,
266 return mandoc_msg_getrc();
270 search
.argmode
= ARG_EXPR
;
273 search
.argmode
= ARG_FILE
;
274 outmode
= OUTMODE_ALL
;
286 search
.arch
= optarg
;
292 if (strcmp(optarg
, "ascii") == 0)
293 outst
.outtype
= OUTT_ASCII
;
294 else if (strcmp(optarg
, "lint") == 0) {
295 outst
.outtype
= OUTT_LINT
;
296 mandoc_msg_setoutfile(stdout
);
297 mandoc_msg_setmin(MANDOCERR_BASE
);
298 } else if (strcmp(optarg
, "tree") == 0)
299 outst
.outtype
= OUTT_TREE
;
300 else if (strcmp(optarg
, "man") == 0)
301 outst
.outtype
= OUTT_MAN
;
302 else if (strcmp(optarg
, "html") == 0)
303 outst
.outtype
= OUTT_HTML
;
304 else if (strcmp(optarg
, "markdown") == 0)
305 outst
.outtype
= OUTT_MARKDOWN
;
306 else if (strcmp(optarg
, "utf8") == 0)
307 outst
.outtype
= OUTT_UTF8
;
308 else if (strcmp(optarg
, "locale") == 0)
309 outst
.outtype
= OUTT_LOCALE
;
310 else if (strcmp(optarg
, "ps") == 0)
311 outst
.outtype
= OUTT_PS
;
312 else if (strcmp(optarg
, "pdf") == 0)
313 outst
.outtype
= OUTT_PDF
;
315 mandoc_msg(MANDOCERR_BADARG_BAD
, 0, 0,
317 return mandoc_msg_getrc();
321 if (woptions(optarg
, &os_e
, &outst
.wstop
) == -1)
322 return mandoc_msg_getrc();
325 outmode
= OUTMODE_FLN
;
334 usage(search
.argmode
);
336 /* Postprocess options. */
340 switch (search
.argmode
) {
342 outmode
= OUTMODE_ALL
;
346 outmode
= OUTMODE_ONE
;
349 outmode
= OUTMODE_LST
;
354 if (search
.argmode
== ARG_FILE
)
355 outmode
= OUTMODE_ALL
;
365 if (outmode
== OUTMODE_LST
)
366 search
.outkey
= oarg
;
368 while (oarg
!= NULL
) {
369 if (manconf_output(&conf
.output
,
370 strsep(&oarg
, ","), 0) == -1)
371 return mandoc_msg_getrc();
376 if (outst
.outtype
!= OUTT_TREE
|| conf
.output
.noval
== 0)
377 options
|= MPARSE_VALIDATE
;
379 if (outmode
== OUTMODE_FLN
||
380 outmode
== OUTMODE_LST
||
381 (conf
.output
.outfilename
== NULL
&&
382 conf
.output
.tagfilename
== NULL
&&
383 isatty(STDOUT_FILENO
) == 0))
386 if (outst
.use_pager
&&
387 (conf
.output
.width
== 0 || conf
.output
.indent
== 0) &&
388 ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &ws
) != -1 &&
390 if (conf
.output
.width
== 0 && ws
.ws_col
< 79)
391 conf
.output
.width
= ws
.ws_col
- 1;
392 if (conf
.output
.indent
== 0 && ws
.ws_col
< 66)
393 conf
.output
.indent
= 3;
397 if (outst
.use_pager
== 0)
398 c
= pledge("stdio rpath", NULL
);
399 else if (conf
.output
.outfilename
!= NULL
||
400 conf
.output
.tagfilename
!= NULL
)
401 c
= pledge("stdio rpath wpath cpath", NULL
);
403 c
= pledge("stdio rpath tmppath tty proc exec", NULL
);
405 mandoc_msg(MANDOCERR_PLEDGE
, 0, 0, "%s", strerror(errno
));
406 return mandoc_msg_getrc();
410 /* Parse arguments. */
418 * Quirks for help(1) and man(1),
419 * in particular for a section argument without -s.
422 if (search
.argmode
== ARG_NAME
) {
423 if (*progname
== 'h') {
428 } else if (argc
> 1 &&
429 ((uc
= (unsigned char *)argv
[0]) != NULL
) &&
430 ((isdigit(uc
[0]) && (uc
[1] == '\0' ||
432 (uc
[0] == 'n' && uc
[1] == '\0'))) {
433 search
.sec
= (char *)uc
;
437 if (search
.arch
== NULL
)
438 search
.arch
= getenv("MACHINE");
440 if (search
.arch
== NULL
)
441 search
.arch
= MACHINE
;
443 if (outmode
== OUTMODE_ONE
)
444 search
.firstmatch
= 1;
448 * Use the first argument for -O tag in addition to
449 * using it as a search term for man(1) or apropos(1).
452 if (conf
.output
.tag
!= NULL
&& *conf
.output
.tag
== '\0') {
453 tagarg
= argc
> 0 && search
.argmode
== ARG_EXPR
?
454 strchr(*argv
, '=') : NULL
;
455 conf
.output
.tag
= tagarg
== NULL
? *argv
: tagarg
+ 1;
458 /* Read the configuration file. */
460 if (search
.argmode
!= ARG_FILE
||
461 mandoc_msg_getmin() == MANDOCERR_STYLE
)
462 manconf_parse(&conf
, conf_file
, defpaths
, auxpaths
);
464 /* man(1): Resolve each name individually. */
466 if (search
.argmode
== ARG_NAME
) {
468 if (outmode
!= OUTMODE_FLN
)
470 if (conf
.manpath
.sz
== 0) {
471 warnx("The manpath is empty.");
472 mandoc_msg_setrc(MANDOCLEVEL_BADARG
);
474 for (i
= 0; i
+ 1 < conf
.manpath
.sz
; i
++)
475 printf("%s:", conf
.manpath
.paths
[i
]);
476 printf("%s\n", conf
.manpath
.paths
[i
]);
479 return (int)mandoc_msg_getrc();
481 for (res
= NULL
, ressz
= 0; argc
> 0; argc
--, argv
++) {
482 (void)mansearch(&search
, &conf
.manpath
,
483 1, argv
, &resn
, &resnsz
);
485 (void)fs_search(&search
, &conf
.manpath
,
486 *argv
, &resn
, &resnsz
);
487 if (resnsz
== 0 && strchr(*argv
, '/') == NULL
) {
488 if (search
.arch
!= NULL
&&
489 arch_valid(search
.arch
, OSENUM
) == 0)
490 warnx("Unknown architecture \"%s\".",
492 else if (search
.sec
!= NULL
)
493 warnx("No entry for %s in "
494 "section %s of the manual.",
497 warnx("No entry for %s in "
498 "the manual.", *argv
);
499 mandoc_msg_setrc(MANDOCLEVEL_BADARG
);
503 if (access(*argv
, R_OK
) == -1) {
504 mandoc_msg_setinfilename(*argv
);
505 mandoc_msg(MANDOCERR_BADARG_BAD
,
506 0, 0, "%s", strerror(errno
));
507 mandoc_msg_setinfilename(NULL
);
511 resn
= mandoc_calloc(resnsz
, sizeof(*res
));
512 resn
->file
= mandoc_strdup(*argv
);
513 resn
->ipath
= SIZE_MAX
;
514 resn
->form
= FORM_SRC
;
516 if (outmode
!= OUTMODE_ONE
|| resnsz
== 1) {
517 res
= mandoc_reallocarray(res
,
518 ressz
+ resnsz
, sizeof(*res
));
519 memcpy(res
+ ressz
, resn
,
520 sizeof(*resn
) * resnsz
);
528 /* Search for the best section. */
531 for (ib
= i
= 0; i
< resnsz
; i
++) {
533 sec
+= strcspn(sec
, "123456789");
535 continue; /* No section at all. */
536 prio
= sec_prios
[sec
[0] - '1'];
537 if (search
.sec
!= NULL
) {
538 ssz
= strlen(search
.sec
);
539 if (strncmp(sec
, search
.sec
, ssz
) == 0)
542 sec
++; /* Prefer without suffix. */
544 prio
+= 10; /* Wrong dir name. */
545 if (search
.sec
!= NULL
) {
546 ep
= strchr(sec
, '\0');
548 strncmp(ep
- 3, ".gz", 3) == 0)
550 if ((size_t)(ep
- sec
) < ssz
+ 3 ||
551 strncmp(ep
- ssz
, search
.sec
,
552 ssz
) != 0) /* Wrong file */
553 prio
+= 20; /* extension. */
555 if (prio
>= best_prio
)
560 res
= mandoc_reallocarray(res
, ressz
+ 1,
562 memcpy(res
+ ressz
++, resn
+ ib
, sizeof(*resn
));
563 memset(resn
+ ib
, 0, sizeof(*resn
));
564 mansearch_free(resn
, resnsz
);
569 /* apropos(1), whatis(1): Process the full search expression. */
571 } else if (search
.argmode
!= ARG_FILE
) {
572 if (mansearch(&search
, &conf
.manpath
,
573 argc
, argv
, &res
, &ressz
) == 0)
574 usage(search
.argmode
);
577 warnx("nothing appropriate");
578 mandoc_msg_setrc(MANDOCLEVEL_BADARG
);
582 /* mandoc(1): Take command line arguments as file names. */
585 ressz
= argc
> 0 ? argc
: 1;
586 res
= mandoc_calloc(ressz
, sizeof(*res
));
587 for (i
= 0; i
< ressz
; i
++) {
589 res
[i
].file
= mandoc_strdup(argv
[i
]);
590 res
[i
].ipath
= SIZE_MAX
;
591 res
[i
].form
= FORM_SRC
;
597 for (i
= 0; i
< ressz
; i
++)
601 for (i
= 0; i
< ressz
; i
++)
602 printf("%s - %s\n", res
[i
].names
,
603 res
[i
].output
== NULL
? "" :
610 if (search
.argmode
== ARG_FILE
&& auxpaths
!= NULL
) {
611 if (strcmp(auxpaths
, "doc") == 0)
612 options
|= MPARSE_MDOC
;
613 else if (strcmp(auxpaths
, "an") == 0)
614 options
|= MPARSE_MAN
;
618 mp
= mparse_alloc(options
, os_e
, os_s
);
621 * Remember the original working directory, if possible.
622 * This will be needed if some names on the command line
623 * are page names and some are relative file names.
624 * Do not error out if the current directory is not
625 * readable: Maybe it won't be needed after all.
627 startdir
= open(".", O_RDONLY
| O_DIRECTORY
);
628 for (i
= 0; i
< ressz
; i
++) {
629 process_onefile(mp
, res
+ i
, startdir
, &outst
, &conf
);
630 if (outst
.wstop
&& mandoc_msg_getrc() != MANDOCLEVEL_OK
)
633 if (startdir
!= -1) {
634 (void)fchdir(startdir
);
637 if (conf
.output
.tag
!= NULL
&& conf
.output
.tag_found
== 0) {
638 mandoc_msg(MANDOCERR_TAG
, 0, 0, "%s", conf
.output
.tag
);
639 conf
.output
.tag
= NULL
;
641 if (outst
.outdata
!= NULL
) {
642 switch (outst
.outtype
) {
644 html_free(outst
.outdata
);
649 ascii_free(outst
.outdata
);
653 pspdf_free(outst
.outdata
);
664 mansearch_free(res
, ressz
);
665 if (search
.argmode
!= ARG_FILE
)
668 if (outst
.tag_files
!= NULL
) {
669 if (term_tag_close() != -1 &&
670 conf
.output
.outfilename
== NULL
&&
671 conf
.output
.tagfilename
== NULL
)
672 run_pager(&outst
, conf
.output
.tag
);
674 } else if (outst
.had_output
&& outst
.outtype
!= OUTT_LINT
)
675 mandoc_msg_summary();
680 return (int)mandoc_msg_getrc();
684 usage(enum argmode argmode
)
688 fputs("usage: mandoc [-ac] [-I os=name] "
689 "[-K encoding] [-mdoc | -man] [-O options]\n"
690 "\t [-T output] [-W level] [file ...]\n", stderr
);
693 fputs("usage: man [-acfhklw] [-C file] [-M path] "
694 "[-m path] [-S subsection]\n"
695 "\t [[-s] section] name ...\n", stderr
);
698 fputs("usage: whatis [-afk] [-C file] "
699 "[-M path] [-m path] [-O outkey] [-S arch]\n"
700 "\t [-s section] name ...\n", stderr
);
703 fputs("usage: apropos [-afk] [-C file] "
704 "[-M path] [-m path] [-O outkey] [-S arch]\n"
705 "\t [-s section] expression ...\n", stderr
);
708 exit((int)MANDOCLEVEL_BADARG
);
712 glob_esc(char **dst
, const char *src
, const char *suffix
)
714 while (*src
!= '\0') {
715 if (strchr("*?[", *src
) != NULL
)
719 while (*suffix
!= '\0')
720 *(*dst
)++ = *suffix
++;
724 fs_append(char **file
, size_t filesz
, int copy
, size_t ipath
,
725 const char *sec
, enum form form
, struct manpage
**res
, size_t *ressz
)
727 struct manpage
*page
;
729 *res
= mandoc_reallocarray(*res
, *ressz
+ filesz
, sizeof(**res
));
730 page
= *res
+ *ressz
;
733 page
->file
= copy
? mandoc_strdup(*file
) : *file
;
736 page
->bits
= NAME_FILE
& NAME_MASK
;
738 page
->sec
= (*sec
>= '1' && *sec
<= '9') ? *sec
- '1' + 1 : 10;
748 fs_lookup(const struct manpaths
*paths
, size_t ipath
,
749 const char *sec
, const char *arch
, const char *name
,
750 struct manpage
**res
, size_t *ressz
)
754 char *file
, *cp
, secnum
[2];
758 const char *const slman
= "/man";
759 const char *const slash
= "/";
760 const char *const sglob
= ".[01-9]*";
761 const char *const dot
= ".";
762 const char *const aster
= "*";
764 memset(&globinfo
, 0, sizeof(globinfo
));
767 mandoc_asprintf(&file
, "%s/man%s/%s.%s",
768 paths
->paths
[ipath
], sec
, name
, sec
);
769 if (stat(file
, &sb
) != -1)
773 mandoc_asprintf(&file
, "%s/cat%s/%s.0",
774 paths
->paths
[ipath
], sec
, name
);
775 if (stat(file
, &sb
) != -1) {
782 mandoc_asprintf(&file
, "%s/man%s/%s/%s.%s",
783 paths
->paths
[ipath
], sec
, arch
, name
, sec
);
784 if (stat(file
, &sb
) != -1)
789 cp
= file
= mandoc_malloc(strlen(paths
->paths
[ipath
]) * 2 +
790 strlen(slman
) + strlen(sec
) * 2 + strlen(slash
) +
791 strlen(name
) * 2 + strlen(sglob
) + 1);
792 glob_esc(&cp
, paths
->paths
[ipath
], slman
);
793 glob_esc(&cp
, sec
, slash
);
794 glob_esc(&cp
, name
, sglob
);
796 globres
= glob(file
, 0, NULL
, &globinfo
);
797 if (globres
!= 0 && globres
!= GLOB_NOMATCH
)
798 mandoc_msg(MANDOCERR_GLOB
, 0, 0,
799 "%s: %s", file
, strerror(errno
));
806 if (sec
[1] != '\0' && *ressz
== 0) {
809 cp
= file
= mandoc_malloc(strlen(paths
->paths
[ipath
]) * 2 +
810 strlen(slman
) + strlen(secnum
) * 2 + strlen(slash
) +
811 strlen(name
) * 2 + strlen(dot
) +
812 strlen(sec
) * 2 + strlen(aster
) + 1);
813 glob_esc(&cp
, paths
->paths
[ipath
], slman
);
814 glob_esc(&cp
, secnum
, slash
);
815 glob_esc(&cp
, name
, dot
);
816 glob_esc(&cp
, sec
, aster
);
818 globres
= glob(file
, 0, NULL
, &globinfo
);
819 if (globres
!= 0 && globres
!= GLOB_NOMATCH
)
820 mandoc_msg(MANDOCERR_GLOB
, 0, 0,
821 "%s: %s", file
, strerror(errno
));
829 if (res
!= NULL
|| ipath
+ 1 != paths
->sz
)
832 mandoc_asprintf(&file
, "%s.%s", name
, sec
);
833 globres
= stat(file
, &sb
);
838 warnx("outdated mandoc.db lacks %s(%s) entry, run %s %s",
839 name
, sec
, BINM_MAKEWHATIS
, paths
->paths
[ipath
]);
842 else if (file
== NULL
)
843 fs_append(globinfo
.gl_pathv
, globinfo
.gl_pathc
, 1,
844 ipath
, sec
, form
, res
, ressz
);
846 fs_append(&file
, 1, 0, ipath
, sec
, form
, res
, ressz
);
852 fs_search(const struct mansearch
*cfg
, const struct manpaths
*paths
,
853 const char *name
, struct manpage
**res
, size_t *ressz
)
855 const char *const sections
[] =
856 {"1", "8", "6", "2", "3", "5", "7", "4", "9", "3p"};
857 const size_t nsec
= sizeof(sections
)/sizeof(sections
[0]);
861 assert(cfg
->argmode
== ARG_NAME
);
865 for (ipath
= 0; ipath
< paths
->sz
; ipath
++) {
866 if (cfg
->sec
!= NULL
) {
867 if (fs_lookup(paths
, ipath
, cfg
->sec
, cfg
->arch
,
868 name
, res
, ressz
) != -1 && cfg
->firstmatch
)
871 for (isec
= 0; isec
< nsec
; isec
++)
872 if (fs_lookup(paths
, ipath
, sections
[isec
],
873 cfg
->arch
, name
, res
, ressz
) != -1 &&
882 process_onefile(struct mparse
*mp
, struct manpage
*resp
, int startdir
,
883 struct outstate
*outst
, struct manconf
*conf
)
888 * Changing directories is not needed in ARG_FILE mode.
889 * Do it on a best-effort basis. Even in case of
890 * failure, some functionality may still work.
892 if (resp
->ipath
!= SIZE_MAX
)
893 (void)chdir(conf
->manpath
.paths
[resp
->ipath
]);
894 else if (startdir
!= -1)
895 (void)fchdir(startdir
);
897 mandoc_msg_setinfilename(resp
->file
);
898 if (resp
->file
!= NULL
) {
899 if ((fd
= mparse_open(mp
, resp
->file
)) == -1) {
900 mandoc_msg(resp
->ipath
== SIZE_MAX
?
901 MANDOCERR_BADARG_BAD
: MANDOCERR_OPEN
,
902 0, 0, "%s", strerror(errno
));
903 mandoc_msg_setinfilename(NULL
);
909 if (outst
->use_pager
) {
910 outst
->use_pager
= 0;
911 outst
->tag_files
= term_tag_init(conf
->output
.outfilename
,
912 outst
->outtype
== OUTT_HTML
? ".html" : "",
913 conf
->output
.tagfilename
);
915 if ((conf
->output
.outfilename
!= NULL
||
916 conf
->output
.tagfilename
!= NULL
) &&
917 pledge("stdio rpath cpath", NULL
) == -1) {
918 mandoc_msg(MANDOCERR_PLEDGE
, 0, 0,
919 "%s", strerror(errno
));
920 exit(mandoc_msg_getrc());
924 if (outst
->had_output
&& outst
->outtype
<= OUTT_UTF8
) {
925 if (outst
->outdata
== NULL
)
926 outdata_alloc(outst
, &conf
->output
);
927 terminal_sepline(outst
->outdata
);
930 if (resp
->form
== FORM_SRC
)
931 parse(mp
, fd
, resp
->file
, outst
, conf
);
933 passthrough(fd
, conf
->output
.synopsisonly
);
934 outst
->had_output
= 1;
937 if (ferror(stdout
)) {
938 if (outst
->tag_files
!= NULL
) {
939 mandoc_msg(MANDOCERR_WRITE
, 0, 0, "%s: %s",
940 outst
->tag_files
->ofn
, strerror(errno
));
942 outst
->tag_files
= NULL
;
944 mandoc_msg(MANDOCERR_WRITE
, 0, 0, "%s",
947 mandoc_msg_setinfilename(NULL
);
951 parse(struct mparse
*mp
, int fd
, const char *file
,
952 struct outstate
*outst
, struct manconf
*conf
)
954 static struct manpaths basepaths
;
956 struct roff_meta
*meta
;
967 mparse_readfd(mp
, fd
, file
);
968 if (fd
!= STDIN_FILENO
)
972 * With -Wstop and warnings or errors of at least the requested
973 * level, do not produce output.
976 if (outst
->wstop
&& mandoc_msg_getrc() != MANDOCLEVEL_OK
)
979 if (outst
->outdata
== NULL
)
980 outdata_alloc(outst
, &conf
->output
);
981 else if (outst
->outtype
== OUTT_HTML
)
982 html_reset(outst
->outdata
);
985 meta
= mparse_result(mp
);
987 /* Execute the out device, if it exists. */
989 outst
->had_output
= 1;
990 if (meta
->macroset
== MACROSET_MDOC
) {
991 switch (outst
->outtype
) {
993 html_mdoc(outst
->outdata
, meta
);
996 tree_mdoc(outst
->outdata
, meta
);
999 man_mdoc(outst
->outdata
, meta
);
1006 terminal_mdoc(outst
->outdata
, meta
);
1009 markdown_mdoc(outst
->outdata
, meta
);
1015 if (meta
->macroset
== MACROSET_MAN
) {
1016 switch (outst
->outtype
) {
1018 html_man(outst
->outdata
, meta
);
1021 tree_man(outst
->outdata
, meta
);
1031 terminal_man(outst
->outdata
, meta
);
1034 mandoc_msg(MANDOCERR_MAN_TMARKDOWN
, 0, 0, NULL
);
1040 if (conf
->output
.tag
!= NULL
&& conf
->output
.tag_found
== 0 &&
1041 tag_exists(conf
->output
.tag
))
1042 conf
->output
.tag_found
= 1;
1044 if (mandoc_msg_getmin() < MANDOCERR_STYLE
) {
1045 if (basepaths
.sz
== 0)
1046 manpath_base(&basepaths
);
1047 check_xr(&basepaths
);
1048 } else if (mandoc_msg_getmin() < MANDOCERR_WARNING
)
1049 check_xr(&conf
->manpath
);
1053 check_xr(struct manpaths
*paths
)
1055 struct mansearch search
;
1056 struct mandoc_xr
*xr
;
1059 for (xr
= mandoc_xr_get(); xr
!= NULL
; xr
= xr
->next
) {
1063 search
.sec
= xr
->sec
;
1064 search
.outkey
= NULL
;
1065 search
.argmode
= ARG_NAME
;
1066 search
.firstmatch
= 1;
1067 if (mansearch(&search
, paths
, 1, &xr
->name
, NULL
, &sz
))
1069 if (fs_search(&search
, paths
, xr
->name
, NULL
, &sz
) != -1)
1072 mandoc_msg(MANDOCERR_XR_BAD
, xr
->line
,
1073 xr
->pos
+ 1, "Xr %s %s", xr
->name
, xr
->sec
);
1075 mandoc_msg(MANDOCERR_XR_BAD
, xr
->line
,
1076 xr
->pos
+ 1, "Xr %s %s (%d times)",
1077 xr
->name
, xr
->sec
, xr
->count
);
1082 outdata_alloc(struct outstate
*outst
, struct manoutput
*outconf
)
1084 switch (outst
->outtype
) {
1086 outst
->outdata
= html_alloc(outconf
);
1089 outst
->outdata
= utf8_alloc(outconf
);
1092 outst
->outdata
= locale_alloc(outconf
);
1095 outst
->outdata
= ascii_alloc(outconf
);
1098 outst
->outdata
= pdf_alloc(outconf
);
1101 outst
->outdata
= ps_alloc(outconf
);
1109 passthrough(int fd
, int synopsis_only
)
1111 const char synb
[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
1112 const char synr
[] = "SYNOPSIS";
1117 ssize_t len
, written
;
1124 if (fflush(stdout
) == EOF
) {
1125 mandoc_msg(MANDOCERR_FFLUSH
, 0, 0, "%s", strerror(errno
));
1128 if ((stream
= fdopen(fd
, "r")) == NULL
) {
1130 mandoc_msg(MANDOCERR_FDOPEN
, 0, 0, "%s", strerror(errno
));
1135 while ((len
= getline(&line
, &linesz
, stream
)) != -1) {
1138 if (synopsis_only
) {
1140 if ( ! isspace((unsigned char)*cp
))
1142 while (isspace((unsigned char)*cp
)) {
1147 if (strcmp(cp
, synb
) == 0 ||
1148 strcmp(cp
, synr
) == 0)
1153 for (; len
> 0; len
-= written
) {
1154 if ((written
= write(STDOUT_FILENO
, cp
, len
)) == -1) {
1155 mandoc_msg(MANDOCERR_WRITE
, 0, 0,
1156 "%s", strerror(errno
));
1162 mandoc_msg(MANDOCERR_GETLINE
, lno
, 0, "%s", strerror(errno
));
1171 woptions(char *arg
, enum mandoc_os
*os_e
, int *wstop
)
1174 const char *toks
[11];
1180 toks
[4] = "warning";
1184 toks
[8] = "openbsd";
1190 switch (getsubopt(&arg
, (char * const *)toks
, &v
)) {
1196 mandoc_msg_setmin(MANDOCERR_BASE
);
1199 mandoc_msg_setmin(MANDOCERR_STYLE
);
1202 mandoc_msg_setmin(MANDOCERR_WARNING
);
1205 mandoc_msg_setmin(MANDOCERR_ERROR
);
1208 mandoc_msg_setmin(MANDOCERR_UNSUPP
);
1211 mandoc_msg_setmin(MANDOCERR_BADARG
);
1214 mandoc_msg_setmin(MANDOCERR_BASE
);
1215 *os_e
= MANDOC_OS_OPENBSD
;
1218 mandoc_msg_setmin(MANDOCERR_BASE
);
1219 *os_e
= MANDOC_OS_NETBSD
;
1222 mandoc_msg(MANDOCERR_BADARG_BAD
, 0, 0, "-W %s", o
);
1230 * Wait until moved to the foreground,
1231 * then fork the pager and wait for the user to close it.
1234 run_pager(struct outstate
*outst
, char *tag_target
)
1237 pid_t man_pgid
, tc_pgid
;
1238 pid_t pager_pid
, wait_pid
;
1240 man_pgid
= getpgid(0);
1241 outst
->tag_files
->tcpgid
=
1242 man_pgid
== getpid() ? getpgid(getppid()) : man_pgid
;
1247 /* Stop here until moved to the foreground. */
1249 tc_pgid
= tcgetpgrp(STDOUT_FILENO
);
1250 if (tc_pgid
!= man_pgid
) {
1251 if (tc_pgid
== pager_pid
) {
1252 (void)tcsetpgrp(STDOUT_FILENO
, man_pgid
);
1253 if (signum
== SIGTTIN
)
1256 outst
->tag_files
->tcpgid
= tc_pgid
;
1261 /* Once in the foreground, activate the pager. */
1264 (void)tcsetpgrp(STDOUT_FILENO
, pager_pid
);
1265 kill(pager_pid
, SIGCONT
);
1267 pager_pid
= spawn_pager(outst
, tag_target
);
1269 /* Wait for the pager to stop or exit. */
1271 while ((wait_pid
= waitpid(pager_pid
, &status
,
1272 WUNTRACED
)) == -1 && errno
== EINTR
)
1275 if (wait_pid
== -1) {
1276 mandoc_msg(MANDOCERR_WAIT
, 0, 0,
1277 "%s", strerror(errno
));
1280 if (!WIFSTOPPED(status
))
1283 signum
= WSTOPSIG(status
);
1288 spawn_pager(struct outstate
*outst
, char *tag_target
)
1290 const struct timespec timeout
= { 0, 100000000 }; /* 0.1s */
1291 #define MAX_PAGER_ARGS 16
1292 char *argv
[MAX_PAGER_ARGS
];
1302 assert(outst
->tag_files
->ofd
== -1);
1303 assert(outst
->tag_files
->tfs
== NULL
);
1305 pager
= getenv("MANPAGER");
1306 if (pager
== NULL
|| *pager
== '\0')
1307 pager
= getenv("PAGER");
1308 if (pager
== NULL
|| *pager
== '\0')
1312 * Parse the pager command into words.
1313 * Intentionally do not do anything fancy here.
1317 while (*pager
!= '\0' && argc
+ 5 < MAX_PAGER_ARGS
) {
1318 wordlen
= strcspn(pager
, " ");
1319 argv
[argc
++] = mandoc_strndup(pager
, wordlen
);
1321 while (*pager
== ' ')
1325 /* For less(1), use the tag file. */
1329 if (*outst
->tag_files
->tfn
!= '\0' &&
1330 (cmdlen
= strlen(argv
[0])) >= 4) {
1331 cp
= argv
[0] + cmdlen
- 4;
1332 if (strcmp(cp
, "less") == 0) {
1333 argv
[argc
++] = mandoc_strdup("-T");
1334 argv
[argc
++] = mandoc_strdup(outst
->tag_files
->tfn
);
1335 if (tag_target
!= NULL
) {
1336 argv
[argc
++] = mandoc_strdup("-t");
1337 argv
[argc
++] = mandoc_strdup(tag_target
);
1344 if (outst
->outtype
== OUTT_HTML
&& tag_target
!= NULL
)
1345 mandoc_asprintf(&argv
[argc
], "file://%s#%s",
1346 outst
->tag_files
->ofn
, tag_target
);
1348 argv
[argc
] = mandoc_strdup(outst
->tag_files
->ofn
);
1353 switch (pager_pid
= fork()) {
1355 mandoc_msg(MANDOCERR_FORK
, 0, 0, "%s", strerror(errno
));
1356 exit(mandoc_msg_getrc());
1362 (void)setpgid(pager_pid
, 0);
1363 (void)tcsetpgrp(STDOUT_FILENO
, pager_pid
);
1365 if (pledge("stdio rpath tmppath tty proc", NULL
) == -1) {
1366 mandoc_msg(MANDOCERR_PLEDGE
, 0, 0,
1367 "%s", strerror(errno
));
1368 exit(mandoc_msg_getrc());
1371 outst
->tag_files
->pager_pid
= pager_pid
;
1376 * The child process becomes the pager.
1377 * Do not start it before controlling the terminal.
1380 while (tcgetpgrp(STDOUT_FILENO
) != getpid())
1381 nanosleep(&timeout
, NULL
);
1383 execvp(argv
[0], argv
);
1384 mandoc_msg(MANDOCERR_EXEC
, 0, 0, "%s: %s", argv
[0], strerror(errno
));
1385 _exit(mandoc_msg_getrc());