]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
1 /* $Id: main.c,v 1.360 2021/10/04 21:29:17 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 progname
= getprogname();
156 progname
= mandoc_strdup("mandoc");
157 else if ((progname
= strrchr(argv
[0], '/')) == NULL
)
161 setprogname(progname
);
164 mandoc_msg_setoutfile(stderr
);
165 if (strncmp(progname
, "mandocdb", 8) == 0 ||
166 strcmp(progname
, BINM_MAKEWHATIS
) == 0)
167 return mandocdb(argc
, argv
);
170 if (pledge("stdio rpath wpath cpath tmppath tty proc exec", NULL
) == -1) {
171 mandoc_msg(MANDOCERR_PLEDGE
, 0, 0, "%s", strerror(errno
));
172 return mandoc_msg_getrc();
175 #if HAVE_SANDBOX_INIT
176 if (sandbox_init(kSBXProfileNoInternet
, SANDBOX_NAMED
, NULL
) == -1)
177 errx((int)MANDOCLEVEL_SYSERR
, "sandbox_init");
180 /* Search options. */
182 memset(&conf
, 0, sizeof(conf
));
184 defpaths
= auxpaths
= NULL
;
186 memset(&search
, 0, sizeof(struct mansearch
));
187 search
.outkey
= "Nd";
190 if (strcmp(progname
, BINM_MAN
) == 0)
191 search
.argmode
= ARG_NAME
;
192 else if (strcmp(progname
, BINM_APROPOS
) == 0)
193 search
.argmode
= ARG_EXPR
;
194 else if (strcmp(progname
, BINM_WHATIS
) == 0)
195 search
.argmode
= ARG_WORD
;
196 else if (strncmp(progname
, "help", 4) == 0)
197 search
.argmode
= ARG_NAME
;
199 search
.argmode
= ARG_FILE
;
201 /* Parser options. */
203 options
= MPARSE_SO
| MPARSE_UTF8
| MPARSE_LATIN1
;
204 os_e
= MANDOC_OS_OTHER
;
207 /* Formatter options. */
209 memset(&outst
, 0, sizeof(outst
));
210 outst
.tag_files
= NULL
;
211 outst
.outtype
= OUTT_LOCALE
;
215 outmode
= OUTMODE_DEF
;
217 while ((c
= getopt(argc
, argv
,
218 "aC:cfhI:iK:klM:m:O:S:s:T:VW:w")) != -1) {
219 if (c
== 'i' && search
.argmode
== ARG_EXPR
) {
225 outmode
= OUTMODE_ALL
;
234 search
.argmode
= ARG_WORD
;
237 conf
.output
.synopsisonly
= 1;
239 outmode
= OUTMODE_ALL
;
242 if (strncmp(optarg
, "os=", 3) != 0) {
243 mandoc_msg(MANDOCERR_BADARG_BAD
, 0, 0,
245 return mandoc_msg_getrc();
248 mandoc_msg(MANDOCERR_BADARG_DUPE
, 0, 0,
250 return mandoc_msg_getrc();
255 options
&= ~(MPARSE_UTF8
| MPARSE_LATIN1
);
256 if (strcmp(optarg
, "utf-8") == 0)
257 options
|= MPARSE_UTF8
;
258 else if (strcmp(optarg
, "iso-8859-1") == 0)
259 options
|= MPARSE_LATIN1
;
260 else if (strcmp(optarg
, "us-ascii") != 0) {
261 mandoc_msg(MANDOCERR_BADARG_BAD
, 0, 0,
263 return mandoc_msg_getrc();
267 search
.argmode
= ARG_EXPR
;
270 search
.argmode
= ARG_FILE
;
271 outmode
= OUTMODE_ALL
;
283 search
.arch
= optarg
;
289 if (strcmp(optarg
, "ascii") == 0)
290 outst
.outtype
= OUTT_ASCII
;
291 else if (strcmp(optarg
, "lint") == 0) {
292 outst
.outtype
= OUTT_LINT
;
293 mandoc_msg_setoutfile(stdout
);
294 mandoc_msg_setmin(MANDOCERR_BASE
);
295 } else if (strcmp(optarg
, "tree") == 0)
296 outst
.outtype
= OUTT_TREE
;
297 else if (strcmp(optarg
, "man") == 0)
298 outst
.outtype
= OUTT_MAN
;
299 else if (strcmp(optarg
, "html") == 0)
300 outst
.outtype
= OUTT_HTML
;
301 else if (strcmp(optarg
, "markdown") == 0)
302 outst
.outtype
= OUTT_MARKDOWN
;
303 else if (strcmp(optarg
, "utf8") == 0)
304 outst
.outtype
= OUTT_UTF8
;
305 else if (strcmp(optarg
, "locale") == 0)
306 outst
.outtype
= OUTT_LOCALE
;
307 else if (strcmp(optarg
, "ps") == 0)
308 outst
.outtype
= OUTT_PS
;
309 else if (strcmp(optarg
, "pdf") == 0)
310 outst
.outtype
= OUTT_PDF
;
312 mandoc_msg(MANDOCERR_BADARG_BAD
, 0, 0,
314 return mandoc_msg_getrc();
318 if (woptions(optarg
, &os_e
, &outst
.wstop
) == -1)
319 return mandoc_msg_getrc();
322 outmode
= OUTMODE_FLN
;
331 usage(search
.argmode
);
333 /* Postprocess options. */
337 switch (search
.argmode
) {
339 outmode
= OUTMODE_ALL
;
343 outmode
= OUTMODE_ONE
;
346 outmode
= OUTMODE_LST
;
351 if (search
.argmode
== ARG_FILE
)
352 outmode
= OUTMODE_ALL
;
362 if (outmode
== OUTMODE_LST
)
363 search
.outkey
= oarg
;
365 while (oarg
!= NULL
) {
366 if (manconf_output(&conf
.output
,
367 strsep(&oarg
, ","), 0) == -1)
368 return mandoc_msg_getrc();
373 if (outst
.outtype
!= OUTT_TREE
|| conf
.output
.noval
== 0)
374 options
|= MPARSE_VALIDATE
;
376 if (outmode
== OUTMODE_FLN
||
377 outmode
== OUTMODE_LST
||
378 (conf
.output
.outfilename
== NULL
&&
379 conf
.output
.tagfilename
== NULL
&&
380 isatty(STDOUT_FILENO
) == 0))
383 if (outst
.use_pager
&&
384 (conf
.output
.width
== 0 || conf
.output
.indent
== 0) &&
385 ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &ws
) != -1 &&
387 if (conf
.output
.width
== 0 && ws
.ws_col
< 79)
388 conf
.output
.width
= ws
.ws_col
- 1;
389 if (conf
.output
.indent
== 0 && ws
.ws_col
< 66)
390 conf
.output
.indent
= 3;
394 if (outst
.use_pager
== 0)
395 c
= pledge("stdio rpath", NULL
);
396 else if (conf
.output
.outfilename
!= NULL
||
397 conf
.output
.tagfilename
!= NULL
)
398 c
= pledge("stdio rpath wpath cpath", NULL
);
400 c
= pledge("stdio rpath tmppath tty proc exec", NULL
);
402 mandoc_msg(MANDOCERR_PLEDGE
, 0, 0, "%s", strerror(errno
));
403 return mandoc_msg_getrc();
407 /* Parse arguments. */
415 * Quirks for help(1) and man(1),
416 * in particular for a section argument without -s.
419 if (search
.argmode
== ARG_NAME
) {
420 if (*progname
== 'h') {
425 } else if (argc
> 1 &&
426 ((uc
= (unsigned char *)argv
[0]) != NULL
) &&
427 ((isdigit(uc
[0]) && (uc
[1] == '\0' ||
429 (uc
[0] == 'n' && uc
[1] == '\0'))) {
430 search
.sec
= (char *)uc
;
434 if (search
.arch
== NULL
)
435 search
.arch
= getenv("MACHINE");
437 if (search
.arch
== NULL
)
438 search
.arch
= MACHINE
;
440 if (outmode
== OUTMODE_ONE
)
441 search
.firstmatch
= 1;
445 * Use the first argument for -O tag in addition to
446 * using it as a search term for man(1) or apropos(1).
449 if (conf
.output
.tag
!= NULL
&& *conf
.output
.tag
== '\0') {
450 tagarg
= argc
> 0 && search
.argmode
== ARG_EXPR
?
451 strchr(*argv
, '=') : NULL
;
452 conf
.output
.tag
= tagarg
== NULL
? *argv
: tagarg
+ 1;
455 /* Read the configuration file. */
457 if (search
.argmode
!= ARG_FILE
||
458 mandoc_msg_getmin() == MANDOCERR_STYLE
)
459 manconf_parse(&conf
, conf_file
, defpaths
, auxpaths
);
461 /* man(1): Resolve each name individually. */
463 if (search
.argmode
== ARG_NAME
) {
465 if (outmode
!= OUTMODE_FLN
)
467 if (conf
.manpath
.sz
== 0) {
468 warnx("The manpath is empty.");
469 mandoc_msg_setrc(MANDOCLEVEL_BADARG
);
471 for (i
= 0; i
+ 1 < conf
.manpath
.sz
; i
++)
472 printf("%s:", conf
.manpath
.paths
[i
]);
473 printf("%s\n", conf
.manpath
.paths
[i
]);
476 return (int)mandoc_msg_getrc();
478 for (res
= NULL
, ressz
= 0; argc
> 0; argc
--, argv
++) {
479 (void)mansearch(&search
, &conf
.manpath
,
480 1, argv
, &resn
, &resnsz
);
482 (void)fs_search(&search
, &conf
.manpath
,
483 *argv
, &resn
, &resnsz
);
484 if (resnsz
== 0 && strchr(*argv
, '/') == NULL
) {
485 if (search
.arch
!= NULL
&&
486 arch_valid(search
.arch
, OSENUM
) == 0)
487 warnx("Unknown architecture \"%s\".",
489 else if (search
.sec
!= NULL
)
490 warnx("No entry for %s in "
491 "section %s of the manual.",
494 warnx("No entry for %s in "
495 "the manual.", *argv
);
496 mandoc_msg_setrc(MANDOCLEVEL_BADARG
);
500 if (access(*argv
, R_OK
) == -1) {
501 mandoc_msg_setinfilename(*argv
);
502 mandoc_msg(MANDOCERR_BADARG_BAD
,
503 0, 0, "%s", strerror(errno
));
504 mandoc_msg_setinfilename(NULL
);
508 resn
= mandoc_calloc(resnsz
, sizeof(*res
));
509 resn
->file
= mandoc_strdup(*argv
);
510 resn
->ipath
= SIZE_MAX
;
511 resn
->form
= FORM_SRC
;
513 if (outmode
!= OUTMODE_ONE
|| resnsz
== 1) {
514 res
= mandoc_reallocarray(res
,
515 ressz
+ resnsz
, sizeof(*res
));
516 memcpy(res
+ ressz
, resn
,
517 sizeof(*resn
) * resnsz
);
525 /* Search for the best section. */
528 for (ib
= i
= 0; i
< resnsz
; i
++) {
530 sec
+= strcspn(sec
, "123456789");
532 continue; /* No section at all. */
533 prio
= sec_prios
[sec
[0] - '1'];
534 if (search
.sec
!= NULL
) {
535 ssz
= strlen(search
.sec
);
536 if (strncmp(sec
, search
.sec
, ssz
) == 0)
539 sec
++; /* Prefer without suffix. */
541 prio
+= 10; /* Wrong dir name. */
542 if (search
.sec
!= NULL
) {
543 ep
= strchr(sec
, '\0');
545 strncmp(ep
- 3, ".gz", 3) == 0)
547 if ((size_t)(ep
- sec
) < ssz
+ 3 ||
548 strncmp(ep
- ssz
, search
.sec
,
549 ssz
) != 0) /* Wrong file */
550 prio
+= 20; /* extension. */
552 if (prio
>= best_prio
)
557 res
= mandoc_reallocarray(res
, ressz
+ 1,
559 memcpy(res
+ ressz
++, resn
+ ib
, sizeof(*resn
));
560 memset(resn
+ ib
, 0, sizeof(*resn
));
561 mansearch_free(resn
, resnsz
);
566 /* apropos(1), whatis(1): Process the full search expression. */
568 } else if (search
.argmode
!= ARG_FILE
) {
569 if (mansearch(&search
, &conf
.manpath
,
570 argc
, argv
, &res
, &ressz
) == 0)
571 usage(search
.argmode
);
574 warnx("nothing appropriate");
575 mandoc_msg_setrc(MANDOCLEVEL_BADARG
);
579 /* mandoc(1): Take command line arguments as file names. */
582 ressz
= argc
> 0 ? argc
: 1;
583 res
= mandoc_calloc(ressz
, sizeof(*res
));
584 for (i
= 0; i
< ressz
; i
++) {
586 res
[i
].file
= mandoc_strdup(argv
[i
]);
587 res
[i
].ipath
= SIZE_MAX
;
588 res
[i
].form
= FORM_SRC
;
594 for (i
= 0; i
< ressz
; i
++)
598 for (i
= 0; i
< ressz
; i
++)
599 printf("%s - %s\n", res
[i
].names
,
600 res
[i
].output
== NULL
? "" :
607 if (search
.argmode
== ARG_FILE
&& auxpaths
!= NULL
) {
608 if (strcmp(auxpaths
, "doc") == 0)
609 options
|= MPARSE_MDOC
;
610 else if (strcmp(auxpaths
, "an") == 0)
611 options
|= MPARSE_MAN
;
615 mp
= mparse_alloc(options
, os_e
, os_s
);
618 * Remember the original working directory, if possible.
619 * This will be needed if some names on the command line
620 * are page names and some are relative file names.
621 * Do not error out if the current directory is not
622 * readable: Maybe it won't be needed after all.
624 startdir
= open(".", O_RDONLY
| O_DIRECTORY
);
625 for (i
= 0; i
< ressz
; i
++) {
626 process_onefile(mp
, res
+ i
, startdir
, &outst
, &conf
);
627 if (outst
.wstop
&& mandoc_msg_getrc() != MANDOCLEVEL_OK
)
630 if (startdir
!= -1) {
631 (void)fchdir(startdir
);
634 if (conf
.output
.tag
!= NULL
&& conf
.output
.tag_found
== 0) {
635 mandoc_msg(MANDOCERR_TAG
, 0, 0, "%s", conf
.output
.tag
);
636 conf
.output
.tag
= NULL
;
638 if (outst
.outdata
!= NULL
) {
639 switch (outst
.outtype
) {
641 html_free(outst
.outdata
);
646 ascii_free(outst
.outdata
);
650 pspdf_free(outst
.outdata
);
661 mansearch_free(res
, ressz
);
662 if (search
.argmode
!= ARG_FILE
)
665 if (outst
.tag_files
!= NULL
) {
666 if (term_tag_close() != -1 &&
667 conf
.output
.outfilename
== NULL
&&
668 conf
.output
.tagfilename
== NULL
)
669 run_pager(&outst
, conf
.output
.tag
);
671 } else if (outst
.had_output
&& outst
.outtype
!= OUTT_LINT
)
672 mandoc_msg_summary();
674 return (int)mandoc_msg_getrc();
678 usage(enum argmode argmode
)
682 fputs("usage: mandoc [-ac] [-I os=name] "
683 "[-K encoding] [-mdoc | -man] [-O options]\n"
684 "\t [-T output] [-W level] [file ...]\n", stderr
);
687 fputs("usage: man [-acfhklw] [-C file] [-M path] "
688 "[-m path] [-S subsection]\n"
689 "\t [[-s] section] name ...\n", stderr
);
692 fputs("usage: whatis [-afk] [-C file] "
693 "[-M path] [-m path] [-O outkey] [-S arch]\n"
694 "\t [-s section] name ...\n", stderr
);
697 fputs("usage: apropos [-afk] [-C file] "
698 "[-M path] [-m path] [-O outkey] [-S arch]\n"
699 "\t [-s section] expression ...\n", stderr
);
702 exit((int)MANDOCLEVEL_BADARG
);
706 glob_esc(char **dst
, const char *src
, const char *suffix
)
708 while (*src
!= '\0') {
709 if (strchr("*?[", *src
) != NULL
)
713 while (*suffix
!= '\0')
714 *(*dst
)++ = *suffix
++;
718 fs_append(char **file
, size_t filesz
, int copy
, size_t ipath
,
719 const char *sec
, enum form form
, struct manpage
**res
, size_t *ressz
)
721 struct manpage
*page
;
723 *res
= mandoc_reallocarray(*res
, *ressz
+ filesz
, sizeof(**res
));
724 page
= *res
+ *ressz
;
727 page
->file
= copy
? mandoc_strdup(*file
) : *file
;
730 page
->bits
= NAME_FILE
& NAME_MASK
;
732 page
->sec
= (*sec
>= '1' && *sec
<= '9') ? *sec
- '1' + 1 : 10;
742 fs_lookup(const struct manpaths
*paths
, size_t ipath
,
743 const char *sec
, const char *arch
, const char *name
,
744 struct manpage
**res
, size_t *ressz
)
748 char *file
, *cp
, secnum
[2];
752 const char *const slman
= "/man";
753 const char *const slash
= "/";
754 const char *const sglob
= ".[01-9]*";
755 const char *const dot
= ".";
756 const char *const aster
= "*";
758 memset(&globinfo
, 0, sizeof(globinfo
));
761 mandoc_asprintf(&file
, "%s/man%s/%s.%s",
762 paths
->paths
[ipath
], sec
, name
, sec
);
763 if (stat(file
, &sb
) != -1)
767 mandoc_asprintf(&file
, "%s/cat%s/%s.0",
768 paths
->paths
[ipath
], sec
, name
);
769 if (stat(file
, &sb
) != -1) {
776 mandoc_asprintf(&file
, "%s/man%s/%s/%s.%s",
777 paths
->paths
[ipath
], sec
, arch
, name
, sec
);
778 if (stat(file
, &sb
) != -1)
783 cp
= file
= mandoc_malloc(strlen(paths
->paths
[ipath
]) * 2 +
784 strlen(slman
) + strlen(sec
) * 2 + strlen(slash
) +
785 strlen(name
) * 2 + strlen(sglob
) + 1);
786 glob_esc(&cp
, paths
->paths
[ipath
], slman
);
787 glob_esc(&cp
, sec
, slash
);
788 glob_esc(&cp
, name
, sglob
);
790 globres
= glob(file
, 0, NULL
, &globinfo
);
791 if (globres
!= 0 && globres
!= GLOB_NOMATCH
)
792 mandoc_msg(MANDOCERR_GLOB
, 0, 0,
793 "%s: %s", file
, strerror(errno
));
800 if (sec
[1] != '\0' && *ressz
== 0) {
803 cp
= file
= mandoc_malloc(strlen(paths
->paths
[ipath
]) * 2 +
804 strlen(slman
) + strlen(secnum
) * 2 + strlen(slash
) +
805 strlen(name
) * 2 + strlen(dot
) +
806 strlen(sec
) * 2 + strlen(aster
) + 1);
807 glob_esc(&cp
, paths
->paths
[ipath
], slman
);
808 glob_esc(&cp
, secnum
, slash
);
809 glob_esc(&cp
, name
, dot
);
810 glob_esc(&cp
, sec
, aster
);
812 globres
= glob(file
, 0, NULL
, &globinfo
);
813 if (globres
!= 0 && globres
!= GLOB_NOMATCH
)
814 mandoc_msg(MANDOCERR_GLOB
, 0, 0,
815 "%s: %s", file
, strerror(errno
));
823 if (res
!= NULL
|| ipath
+ 1 != paths
->sz
)
826 mandoc_asprintf(&file
, "%s.%s", name
, sec
);
827 globres
= stat(file
, &sb
);
832 warnx("outdated mandoc.db lacks %s(%s) entry, run %s %s",
833 name
, sec
, BINM_MAKEWHATIS
, paths
->paths
[ipath
]);
836 else if (file
== NULL
)
837 fs_append(globinfo
.gl_pathv
, globinfo
.gl_pathc
, 1,
838 ipath
, sec
, form
, res
, ressz
);
840 fs_append(&file
, 1, 0, ipath
, sec
, form
, res
, ressz
);
846 fs_search(const struct mansearch
*cfg
, const struct manpaths
*paths
,
847 const char *name
, struct manpage
**res
, size_t *ressz
)
849 const char *const sections
[] =
850 {"1", "8", "6", "2", "3", "5", "7", "4", "9", "3p"};
851 const size_t nsec
= sizeof(sections
)/sizeof(sections
[0]);
855 assert(cfg
->argmode
== ARG_NAME
);
859 for (ipath
= 0; ipath
< paths
->sz
; ipath
++) {
860 if (cfg
->sec
!= NULL
) {
861 if (fs_lookup(paths
, ipath
, cfg
->sec
, cfg
->arch
,
862 name
, res
, ressz
) != -1 && cfg
->firstmatch
)
865 for (isec
= 0; isec
< nsec
; isec
++)
866 if (fs_lookup(paths
, ipath
, sections
[isec
],
867 cfg
->arch
, name
, res
, ressz
) != -1 &&
876 process_onefile(struct mparse
*mp
, struct manpage
*resp
, int startdir
,
877 struct outstate
*outst
, struct manconf
*conf
)
882 * Changing directories is not needed in ARG_FILE mode.
883 * Do it on a best-effort basis. Even in case of
884 * failure, some functionality may still work.
886 if (resp
->ipath
!= SIZE_MAX
)
887 (void)chdir(conf
->manpath
.paths
[resp
->ipath
]);
888 else if (startdir
!= -1)
889 (void)fchdir(startdir
);
891 mandoc_msg_setinfilename(resp
->file
);
892 if (resp
->file
!= NULL
) {
893 if ((fd
= mparse_open(mp
, resp
->file
)) == -1) {
894 mandoc_msg(resp
->ipath
== SIZE_MAX
?
895 MANDOCERR_BADARG_BAD
: MANDOCERR_OPEN
,
896 0, 0, "%s", strerror(errno
));
897 mandoc_msg_setinfilename(NULL
);
903 if (outst
->use_pager
) {
904 outst
->use_pager
= 0;
905 outst
->tag_files
= term_tag_init(conf
->output
.outfilename
,
906 outst
->outtype
== OUTT_HTML
? ".html" : "",
907 conf
->output
.tagfilename
);
909 if ((conf
->output
.outfilename
!= NULL
||
910 conf
->output
.tagfilename
!= NULL
) &&
911 pledge("stdio rpath cpath", NULL
) == -1) {
912 mandoc_msg(MANDOCERR_PLEDGE
, 0, 0,
913 "%s", strerror(errno
));
914 exit(mandoc_msg_getrc());
918 if (outst
->had_output
&& outst
->outtype
<= OUTT_UTF8
) {
919 if (outst
->outdata
== NULL
)
920 outdata_alloc(outst
, &conf
->output
);
921 terminal_sepline(outst
->outdata
);
924 if (resp
->form
== FORM_SRC
)
925 parse(mp
, fd
, resp
->file
, outst
, conf
);
927 passthrough(fd
, conf
->output
.synopsisonly
);
928 outst
->had_output
= 1;
931 if (ferror(stdout
)) {
932 if (outst
->tag_files
!= NULL
) {
933 mandoc_msg(MANDOCERR_WRITE
, 0, 0, "%s: %s",
934 outst
->tag_files
->ofn
, strerror(errno
));
936 outst
->tag_files
= NULL
;
938 mandoc_msg(MANDOCERR_WRITE
, 0, 0, "%s",
941 mandoc_msg_setinfilename(NULL
);
945 parse(struct mparse
*mp
, int fd
, const char *file
,
946 struct outstate
*outst
, struct manconf
*conf
)
948 static struct manpaths basepaths
;
950 struct roff_meta
*meta
;
961 mparse_readfd(mp
, fd
, file
);
962 if (fd
!= STDIN_FILENO
)
966 * With -Wstop and warnings or errors of at least the requested
967 * level, do not produce output.
970 if (outst
->wstop
&& mandoc_msg_getrc() != MANDOCLEVEL_OK
)
973 if (outst
->outdata
== NULL
)
974 outdata_alloc(outst
, &conf
->output
);
975 else if (outst
->outtype
== OUTT_HTML
)
976 html_reset(outst
->outdata
);
979 meta
= mparse_result(mp
);
981 /* Execute the out device, if it exists. */
983 outst
->had_output
= 1;
984 if (meta
->macroset
== MACROSET_MDOC
) {
985 switch (outst
->outtype
) {
987 html_mdoc(outst
->outdata
, meta
);
990 tree_mdoc(outst
->outdata
, meta
);
993 man_mdoc(outst
->outdata
, meta
);
1000 terminal_mdoc(outst
->outdata
, meta
);
1003 markdown_mdoc(outst
->outdata
, meta
);
1009 if (meta
->macroset
== MACROSET_MAN
) {
1010 switch (outst
->outtype
) {
1012 html_man(outst
->outdata
, meta
);
1015 tree_man(outst
->outdata
, meta
);
1025 terminal_man(outst
->outdata
, meta
);
1028 mandoc_msg(MANDOCERR_MAN_TMARKDOWN
, 0, 0, NULL
);
1034 if (conf
->output
.tag
!= NULL
&& conf
->output
.tag_found
== 0 &&
1035 tag_exists(conf
->output
.tag
))
1036 conf
->output
.tag_found
= 1;
1038 if (mandoc_msg_getmin() < MANDOCERR_STYLE
) {
1039 if (basepaths
.sz
== 0)
1040 manpath_base(&basepaths
);
1041 check_xr(&basepaths
);
1042 } else if (mandoc_msg_getmin() < MANDOCERR_WARNING
)
1043 check_xr(&conf
->manpath
);
1047 check_xr(struct manpaths
*paths
)
1049 struct mansearch search
;
1050 struct mandoc_xr
*xr
;
1053 for (xr
= mandoc_xr_get(); xr
!= NULL
; xr
= xr
->next
) {
1057 search
.sec
= xr
->sec
;
1058 search
.outkey
= NULL
;
1059 search
.argmode
= ARG_NAME
;
1060 search
.firstmatch
= 1;
1061 if (mansearch(&search
, paths
, 1, &xr
->name
, NULL
, &sz
))
1063 if (fs_search(&search
, paths
, xr
->name
, NULL
, &sz
) != -1)
1066 mandoc_msg(MANDOCERR_XR_BAD
, xr
->line
,
1067 xr
->pos
+ 1, "Xr %s %s", xr
->name
, xr
->sec
);
1069 mandoc_msg(MANDOCERR_XR_BAD
, xr
->line
,
1070 xr
->pos
+ 1, "Xr %s %s (%d times)",
1071 xr
->name
, xr
->sec
, xr
->count
);
1076 outdata_alloc(struct outstate
*outst
, struct manoutput
*outconf
)
1078 switch (outst
->outtype
) {
1080 outst
->outdata
= html_alloc(outconf
);
1083 outst
->outdata
= utf8_alloc(outconf
);
1086 outst
->outdata
= locale_alloc(outconf
);
1089 outst
->outdata
= ascii_alloc(outconf
);
1092 outst
->outdata
= pdf_alloc(outconf
);
1095 outst
->outdata
= ps_alloc(outconf
);
1103 passthrough(int fd
, int synopsis_only
)
1105 const char synb
[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
1106 const char synr
[] = "SYNOPSIS";
1111 ssize_t len
, written
;
1118 if (fflush(stdout
) == EOF
) {
1119 mandoc_msg(MANDOCERR_FFLUSH
, 0, 0, "%s", strerror(errno
));
1122 if ((stream
= fdopen(fd
, "r")) == NULL
) {
1124 mandoc_msg(MANDOCERR_FDOPEN
, 0, 0, "%s", strerror(errno
));
1129 while ((len
= getline(&line
, &linesz
, stream
)) != -1) {
1132 if (synopsis_only
) {
1134 if ( ! isspace((unsigned char)*cp
))
1136 while (isspace((unsigned char)*cp
)) {
1141 if (strcmp(cp
, synb
) == 0 ||
1142 strcmp(cp
, synr
) == 0)
1147 for (; len
> 0; len
-= written
) {
1148 if ((written
= write(STDOUT_FILENO
, cp
, len
)) == -1) {
1149 mandoc_msg(MANDOCERR_WRITE
, 0, 0,
1150 "%s", strerror(errno
));
1156 mandoc_msg(MANDOCERR_GETLINE
, lno
, 0, "%s", strerror(errno
));
1165 woptions(char *arg
, enum mandoc_os
*os_e
, int *wstop
)
1168 const char *toks
[11];
1174 toks
[4] = "warning";
1178 toks
[8] = "openbsd";
1184 switch (getsubopt(&arg
, (char * const *)toks
, &v
)) {
1190 mandoc_msg_setmin(MANDOCERR_BASE
);
1193 mandoc_msg_setmin(MANDOCERR_STYLE
);
1196 mandoc_msg_setmin(MANDOCERR_WARNING
);
1199 mandoc_msg_setmin(MANDOCERR_ERROR
);
1202 mandoc_msg_setmin(MANDOCERR_UNSUPP
);
1205 mandoc_msg_setmin(MANDOCERR_BADARG
);
1208 mandoc_msg_setmin(MANDOCERR_BASE
);
1209 *os_e
= MANDOC_OS_OPENBSD
;
1212 mandoc_msg_setmin(MANDOCERR_BASE
);
1213 *os_e
= MANDOC_OS_NETBSD
;
1216 mandoc_msg(MANDOCERR_BADARG_BAD
, 0, 0, "-W %s", o
);
1224 * Wait until moved to the foreground,
1225 * then fork the pager and wait for the user to close it.
1228 run_pager(struct outstate
*outst
, char *tag_target
)
1231 pid_t man_pgid
, tc_pgid
;
1232 pid_t pager_pid
, wait_pid
;
1234 man_pgid
= getpgid(0);
1235 outst
->tag_files
->tcpgid
=
1236 man_pgid
== getpid() ? getpgid(getppid()) : man_pgid
;
1241 /* Stop here until moved to the foreground. */
1243 tc_pgid
= tcgetpgrp(STDOUT_FILENO
);
1244 if (tc_pgid
!= man_pgid
) {
1245 if (tc_pgid
== pager_pid
) {
1246 (void)tcsetpgrp(STDOUT_FILENO
, man_pgid
);
1247 if (signum
== SIGTTIN
)
1250 outst
->tag_files
->tcpgid
= tc_pgid
;
1255 /* Once in the foreground, activate the pager. */
1258 (void)tcsetpgrp(STDOUT_FILENO
, pager_pid
);
1259 kill(pager_pid
, SIGCONT
);
1261 pager_pid
= spawn_pager(outst
, tag_target
);
1263 /* Wait for the pager to stop or exit. */
1265 while ((wait_pid
= waitpid(pager_pid
, &status
,
1266 WUNTRACED
)) == -1 && errno
== EINTR
)
1269 if (wait_pid
== -1) {
1270 mandoc_msg(MANDOCERR_WAIT
, 0, 0,
1271 "%s", strerror(errno
));
1274 if (!WIFSTOPPED(status
))
1277 signum
= WSTOPSIG(status
);
1282 spawn_pager(struct outstate
*outst
, char *tag_target
)
1284 const struct timespec timeout
= { 0, 100000000 }; /* 0.1s */
1285 #define MAX_PAGER_ARGS 16
1286 char *argv
[MAX_PAGER_ARGS
];
1296 assert(outst
->tag_files
->ofd
== -1);
1297 assert(outst
->tag_files
->tfs
== NULL
);
1299 pager
= getenv("MANPAGER");
1300 if (pager
== NULL
|| *pager
== '\0')
1301 pager
= getenv("PAGER");
1302 if (pager
== NULL
|| *pager
== '\0')
1306 * Parse the pager command into words.
1307 * Intentionally do not do anything fancy here.
1311 while (*pager
!= '\0' && argc
+ 5 < MAX_PAGER_ARGS
) {
1312 wordlen
= strcspn(pager
, " ");
1313 argv
[argc
++] = mandoc_strndup(pager
, wordlen
);
1315 while (*pager
== ' ')
1319 /* For less(1), use the tag file. */
1323 if (*outst
->tag_files
->tfn
!= '\0' &&
1324 (cmdlen
= strlen(argv
[0])) >= 4) {
1325 cp
= argv
[0] + cmdlen
- 4;
1326 if (strcmp(cp
, "less") == 0) {
1327 argv
[argc
++] = mandoc_strdup("-T");
1328 argv
[argc
++] = mandoc_strdup(outst
->tag_files
->tfn
);
1329 if (tag_target
!= NULL
) {
1330 argv
[argc
++] = mandoc_strdup("-t");
1331 argv
[argc
++] = mandoc_strdup(tag_target
);
1338 if (outst
->outtype
== OUTT_HTML
&& tag_target
!= NULL
)
1339 mandoc_asprintf(&argv
[argc
], "file://%s#%s",
1340 outst
->tag_files
->ofn
, tag_target
);
1342 argv
[argc
] = mandoc_strdup(outst
->tag_files
->ofn
);
1347 switch (pager_pid
= fork()) {
1349 mandoc_msg(MANDOCERR_FORK
, 0, 0, "%s", strerror(errno
));
1350 exit(mandoc_msg_getrc());
1356 (void)setpgid(pager_pid
, 0);
1357 (void)tcsetpgrp(STDOUT_FILENO
, pager_pid
);
1359 if (pledge("stdio rpath tmppath tty proc", NULL
) == -1) {
1360 mandoc_msg(MANDOCERR_PLEDGE
, 0, 0,
1361 "%s", strerror(errno
));
1362 exit(mandoc_msg_getrc());
1365 outst
->tag_files
->pager_pid
= pager_pid
;
1370 * The child process becomes the pager.
1371 * Do not start it before controlling the terminal.
1374 while (tcgetpgrp(STDOUT_FILENO
) != getpid())
1375 nanosleep(&timeout
, NULL
);
1377 execvp(argv
[0], argv
);
1378 mandoc_msg(MANDOCERR_EXEC
, 0, 0, "%s: %s", argv
[0], strerror(errno
));
1379 _exit(mandoc_msg_getrc());