]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
1 /* $Id: main.c,v 1.254 2015/11/06 16:30:33 schwarze Exp $ */
3 * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010-2012, 2014, 2015 Ingo Schwarze <schwarze@openbsd.org>
5 * Copyright (c) 2010 Joerg Sonnenberger <joerg@netbsd.org>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 #include <sys/types.h>
22 #include <sys/param.h> /* MACHINE */
37 #include "mandoc_aux.h"
45 #include "mansearch.h"
47 #if !defined(__GNUC__) || (__GNUC__ < 2)
49 # define __attribute__(x)
51 #endif /* !defined(__GNUC__) || (__GNUC__ < 2) */
63 OUTT_ASCII
= 0, /* -Tascii */
64 OUTT_LOCALE
, /* -Tlocale */
65 OUTT_UTF8
, /* -Tutf8 */
66 OUTT_TREE
, /* -Ttree */
68 OUTT_HTML
, /* -Thtml */
69 OUTT_LINT
, /* -Tlint */
76 enum mandoclevel wlevel
; /* ignore messages below this */
77 int wstop
; /* stop after a file with a warning */
78 enum outt outtype
; /* which output to use */
79 void *outdata
; /* data for output */
80 struct manoutput
*outopts
; /* output options */
83 static int fs_lookup(const struct manpaths
*,
84 size_t ipath
, const char *,
85 const char *, const char *,
86 struct manpage
**, size_t *);
87 static void fs_search(const struct mansearch
*,
88 const struct manpaths
*, int, char**,
89 struct manpage
**, size_t *);
90 static int koptions(int *, char *);
92 int mandocdb(int, char**);
94 static int moptions(int *, char *);
95 static void mmsg(enum mandocerr
, enum mandoclevel
,
96 const char *, int, int, const char *);
97 static void parse(struct curparse
*, int, const char *);
98 static void passthrough(const char *, int, int);
99 static pid_t
spawn_pager(struct tag_files
*);
100 static int toptions(struct curparse
*, char *);
101 static void usage(enum argmode
) __attribute__((noreturn
));
102 static int woptions(struct curparse
*, char *);
104 static const int sec_prios
[] = {1, 4, 5, 8, 6, 3, 7, 2, 9};
105 static char help_arg
[] = "help";
106 static char *help_argv
[] = {help_arg
, NULL
};
107 static enum mandoclevel rc
;
111 main(int argc
, char *argv
[])
114 struct curparse curp
;
115 struct mansearch search
;
116 struct tag_files
*tag_files
;
117 const char *progname
;
121 struct manpage
*res
, *resp
;
122 char *conf_file
, *defpaths
;
126 enum mandoclevel rctmp
;
127 enum outmode outmode
;
135 progname
= getprogname();
138 progname
= mandoc_strdup("mandoc");
139 else if ((progname
= strrchr(argv
[0], '/')) == NULL
)
143 setprogname(progname
);
147 if (strncmp(progname
, "mandocdb", 8) == 0 ||
148 strcmp(progname
, BINM_MAKEWHATIS
) == 0)
149 return mandocdb(argc
, argv
);
152 /* Search options. */
154 memset(&conf
, 0, sizeof(conf
));
155 conf_file
= defpaths
= NULL
;
158 memset(&search
, 0, sizeof(struct mansearch
));
159 search
.outkey
= "Nd";
161 if (strcmp(progname
, BINM_MAN
) == 0)
162 search
.argmode
= ARG_NAME
;
163 else if (strcmp(progname
, BINM_APROPOS
) == 0)
164 search
.argmode
= ARG_EXPR
;
165 else if (strcmp(progname
, BINM_WHATIS
) == 0)
166 search
.argmode
= ARG_WORD
;
167 else if (strncmp(progname
, "help", 4) == 0)
168 search
.argmode
= ARG_NAME
;
170 search
.argmode
= ARG_FILE
;
172 /* Parser and formatter options. */
174 memset(&curp
, 0, sizeof(struct curparse
));
175 curp
.outtype
= OUTT_LOCALE
;
176 curp
.wlevel
= MANDOCLEVEL_BADARG
;
177 curp
.outopts
= &conf
.output
;
178 options
= MPARSE_SO
| MPARSE_UTF8
| MPARSE_LATIN1
;
184 outmode
= OUTMODE_DEF
;
186 while (-1 != (c
= getopt(argc
, argv
,
187 "aC:cfhI:iK:klM:m:O:S:s:T:VW:w"))) {
190 outmode
= OUTMODE_ALL
;
199 search
.argmode
= ARG_WORD
;
202 conf
.output
.synopsisonly
= 1;
204 outmode
= OUTMODE_ALL
;
207 if (strncmp(optarg
, "os=", 3)) {
208 warnx("-I %s: Bad argument", optarg
);
209 return (int)MANDOCLEVEL_BADARG
;
212 warnx("-I %s: Duplicate argument", optarg
);
213 return (int)MANDOCLEVEL_BADARG
;
215 defos
= mandoc_strdup(optarg
+ 3);
218 outmode
= OUTMODE_INT
;
221 if ( ! koptions(&options
, optarg
))
222 return (int)MANDOCLEVEL_BADARG
;
225 search
.argmode
= ARG_EXPR
;
228 search
.argmode
= ARG_FILE
;
229 outmode
= OUTMODE_ALL
;
238 search
.outkey
= optarg
;
239 while (optarg
!= NULL
)
240 manconf_output(&conf
.output
,
241 strsep(&optarg
, ","));
244 search
.arch
= optarg
;
250 if ( ! toptions(&curp
, optarg
))
251 return (int)MANDOCLEVEL_BADARG
;
254 if ( ! woptions(&curp
, optarg
))
255 return (int)MANDOCLEVEL_BADARG
;
258 outmode
= OUTMODE_FLN
;
267 usage(search
.argmode
);
269 /* Postprocess options. */
271 if (outmode
== OUTMODE_DEF
) {
272 switch (search
.argmode
) {
274 outmode
= OUTMODE_ALL
;
278 outmode
= OUTMODE_ONE
;
281 outmode
= OUTMODE_LST
;
286 if (outmode
== OUTMODE_FLN
||
287 outmode
== OUTMODE_LST
||
288 !isatty(STDOUT_FILENO
))
291 /* Parse arguments. */
301 * and for a man(1) section argument without -s.
304 if (search
.argmode
== ARG_NAME
) {
305 if (*progname
== 'h') {
310 } else if (argc
> 1 &&
311 ((uc
= (unsigned char *)argv
[0]) != NULL
) &&
312 ((isdigit(uc
[0]) && (uc
[1] == '\0' ||
313 (isalpha(uc
[1]) && uc
[2] == '\0'))) ||
314 (uc
[0] == 'n' && uc
[1] == '\0'))) {
315 search
.sec
= (char *)uc
;
319 if (search
.arch
== NULL
)
320 search
.arch
= getenv("MACHINE");
322 if (search
.arch
== NULL
)
323 search
.arch
= MACHINE
;
329 /* man(1), whatis(1), apropos(1) */
331 if (search
.argmode
!= ARG_FILE
) {
333 usage(search
.argmode
);
335 if (search
.argmode
== ARG_NAME
&&
336 outmode
== OUTMODE_ONE
)
337 search
.firstmatch
= 1;
339 /* Access the mandoc database. */
341 manconf_parse(&conf
, conf_file
, defpaths
, auxpaths
);
344 if ( ! mansearch(&search
, &conf
.manpath
,
345 argc
, argv
, &res
, &sz
))
346 usage(search
.argmode
);
348 if (search
.argmode
!= ARG_NAME
) {
349 fputs("mandoc: database support not compiled in\n",
351 return (int)MANDOCLEVEL_BADARG
;
357 if (search
.argmode
== ARG_NAME
)
358 fs_search(&search
, &conf
.manpath
,
359 argc
, argv
, &res
, &sz
);
361 warnx("nothing appropriate");
365 rc
= MANDOCLEVEL_BADARG
;
370 * For standard man(1) and -a output mode,
371 * prepare for copying filename pointers
372 * into the program parameter array.
375 if (outmode
== OUTMODE_ONE
) {
378 } else if (outmode
== OUTMODE_ALL
)
381 /* Iterate all matching manuals. */
384 for (i
= 0; i
< sz
; i
++) {
385 if (outmode
== OUTMODE_FLN
)
387 else if (outmode
== OUTMODE_LST
)
388 printf("%s - %s\n", res
[i
].names
,
389 res
[i
].output
== NULL
? "" :
391 else if (outmode
== OUTMODE_ONE
) {
392 /* Search for the best section. */
393 isec
= strcspn(res
[i
].file
, "123456789");
394 sec
= res
[i
].file
[isec
];
397 prio
= sec_prios
[sec
- '1'];
398 if (prio
>= best_prio
)
406 * For man(1), -a and -i output mode, fall through
407 * to the main mandoc(1) code iterating files
408 * and running the parsers on each of them.
411 if (outmode
== OUTMODE_FLN
|| outmode
== OUTMODE_LST
)
417 if (search
.argmode
== ARG_FILE
&& ! moptions(&options
, auxpaths
))
418 return (int)MANDOCLEVEL_BADARG
;
421 curp
.mp
= mparse_alloc(options
, curp
.wlevel
, mmsg
, defos
);
424 * Conditionally start up the lookaside buffer before parsing.
426 if (OUTT_MAN
== curp
.outtype
)
427 mparse_keep(curp
.mp
);
431 tag_files
= tag_init();
432 parse(&curp
, STDIN_FILENO
, "<stdin>");
436 rctmp
= mparse_open(curp
.mp
, &fd
,
437 resp
!= NULL
? resp
->file
: *argv
);
443 tag_files
= tag_init();
448 parse(&curp
, fd
, *argv
);
449 else if (resp
->form
& FORM_SRC
) {
450 /* For .so only; ignore failure. */
451 chdir(conf
.manpath
.paths
[resp
->ipath
]);
452 parse(&curp
, fd
, resp
->file
);
454 passthrough(resp
->file
, fd
,
455 conf
.output
.synopsisonly
);
457 if (argc
> 1 && curp
.outtype
<= OUTT_UTF8
)
458 ascii_sepline(curp
.outdata
);
461 if (MANDOCLEVEL_OK
!= rc
&& curp
.wstop
)
469 mparse_reset(curp
.mp
);
472 if (curp
.outdata
!= NULL
) {
473 switch (curp
.outtype
) {
475 html_free(curp
.outdata
);
480 ascii_free(curp
.outdata
);
484 pspdf_free(curp
.outdata
);
490 mparse_free(curp
.mp
);
494 if (search
.argmode
!= ARG_FILE
) {
497 mansearch_free(res
, sz
);
505 * When using a pager, finish writing both temporary files,
506 * fork it, wait for the user to close it, and clean up.
509 if (tag_files
!= NULL
) {
512 waitpid(spawn_pager(tag_files
), NULL
, 0);
520 usage(enum argmode argmode
)
525 fputs("usage: mandoc [-acfhkl] [-I os=name] "
526 "[-K encoding] [-mformat] [-O option]\n"
527 "\t [-T output] [-W level] [file ...]\n", stderr
);
530 fputs("usage: man [-acfhklw] [-C file] [-I os=name] "
531 "[-K encoding] [-M path] [-m path]\n"
532 "\t [-O option=value] [-S subsection] [-s section] "
533 "[-T output] [-W level]\n"
534 "\t [section] name ...\n", stderr
);
537 fputs("usage: whatis [-acfhklw] [-C file] "
538 "[-M path] [-m path] [-O outkey] [-S arch]\n"
539 "\t [-s section] name ...\n", stderr
);
542 fputs("usage: apropos [-acfhklw] [-C file] "
543 "[-M path] [-m path] [-O outkey] [-S arch]\n"
544 "\t [-s section] expression ...\n", stderr
);
547 exit((int)MANDOCLEVEL_BADARG
);
551 fs_lookup(const struct manpaths
*paths
, size_t ipath
,
552 const char *sec
, const char *arch
, const char *name
,
553 struct manpage
**res
, size_t *ressz
)
556 struct manpage
*page
;
561 mandoc_asprintf(&file
, "%s/man%s/%s.%s",
562 paths
->paths
[ipath
], sec
, name
, sec
);
563 if (access(file
, R_OK
) != -1)
567 mandoc_asprintf(&file
, "%s/cat%s/%s.0",
568 paths
->paths
[ipath
], sec
, name
);
569 if (access(file
, R_OK
) != -1) {
576 mandoc_asprintf(&file
, "%s/man%s/%s/%s.%s",
577 paths
->paths
[ipath
], sec
, arch
, name
, sec
);
578 if (access(file
, R_OK
) != -1)
583 mandoc_asprintf(&file
, "%s/man%s/%s.[01-9]*",
584 paths
->paths
[ipath
], sec
, name
);
585 globres
= glob(file
, 0, NULL
, &globinfo
);
586 if (globres
!= 0 && globres
!= GLOB_NOMATCH
)
587 warn("%s: glob", file
);
590 file
= mandoc_strdup(*globinfo
.gl_pathv
);
597 warnx("outdated mandoc.db lacks %s(%s) entry, run makewhatis %s",
598 name
, sec
, paths
->paths
[ipath
]);
600 *res
= mandoc_reallocarray(*res
, ++*ressz
, sizeof(struct manpage
));
601 page
= *res
+ (*ressz
- 1);
606 page
->bits
= NAME_FILE
& NAME_MASK
;
607 page
->sec
= (*sec
>= '1' && *sec
<= '9') ? *sec
- '1' + 1 : 10;
613 fs_search(const struct mansearch
*cfg
, const struct manpaths
*paths
,
614 int argc
, char **argv
, struct manpage
**res
, size_t *ressz
)
616 const char *const sections
[] =
617 {"1", "8", "6", "2", "3", "3p", "5", "7", "4", "9"};
618 const size_t nsec
= sizeof(sections
)/sizeof(sections
[0]);
620 size_t ipath
, isec
, lastsz
;
622 assert(cfg
->argmode
== ARG_NAME
);
627 for (ipath
= 0; ipath
< paths
->sz
; ipath
++) {
628 if (cfg
->sec
!= NULL
) {
629 if (fs_lookup(paths
, ipath
, cfg
->sec
,
630 cfg
->arch
, *argv
, res
, ressz
) &&
633 } else for (isec
= 0; isec
< nsec
; isec
++)
634 if (fs_lookup(paths
, ipath
, sections
[isec
],
635 cfg
->arch
, *argv
, res
, ressz
) &&
639 if (*ressz
== lastsz
)
640 warnx("No entry for %s in the manual.", *argv
);
648 parse(struct curparse
*curp
, int fd
, const char *file
)
650 enum mandoclevel rctmp
;
651 struct roff_man
*man
;
653 /* Begin by parsing the file itself. */
658 rctmp
= mparse_readfd(curp
->mp
, fd
, file
);
663 * With -Wstop and warnings or errors of at least the requested
664 * level, do not produce output.
667 if (rctmp
!= MANDOCLEVEL_OK
&& curp
->wstop
)
670 /* If unset, allocate output dev now (if applicable). */
672 if (curp
->outdata
== NULL
) {
673 switch (curp
->outtype
) {
675 curp
->outdata
= html_alloc(curp
->outopts
);
678 curp
->outdata
= utf8_alloc(curp
->outopts
);
681 curp
->outdata
= locale_alloc(curp
->outopts
);
684 curp
->outdata
= ascii_alloc(curp
->outopts
);
687 curp
->outdata
= pdf_alloc(curp
->outopts
);
690 curp
->outdata
= ps_alloc(curp
->outopts
);
697 mparse_result(curp
->mp
, &man
, NULL
);
699 /* Execute the out device, if it exists. */
703 if (man
->macroset
== MACROSET_MDOC
) {
705 switch (curp
->outtype
) {
707 html_mdoc(curp
->outdata
, man
);
710 tree_mdoc(curp
->outdata
, man
);
713 man_mdoc(curp
->outdata
, man
);
720 terminal_mdoc(curp
->outdata
, man
);
726 if (man
->macroset
== MACROSET_MAN
) {
728 switch (curp
->outtype
) {
730 html_man(curp
->outdata
, man
);
733 tree_man(curp
->outdata
, man
);
736 man_man(curp
->outdata
, man
);
743 terminal_man(curp
->outdata
, man
);
752 passthrough(const char *file
, int fd
, int synopsis_only
)
754 const char synb
[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
755 const char synr
[] = "SYNOPSIS";
766 if ((stream
= fdopen(fd
, "r")) == NULL
) {
773 while ((line
= fgetln(stream
, &len
)) != NULL
) {
776 if ( ! isspace((unsigned char)*line
))
779 isspace((unsigned char)*line
)) {
784 if ((len
== sizeof(synb
) &&
785 ! strncmp(line
, synb
, len
- 1)) ||
786 (len
== sizeof(synr
) &&
787 ! strncmp(line
, synr
, len
- 1)))
792 for (off
= 0; off
< len
; off
+= nw
)
793 if ((nw
= write(STDOUT_FILENO
, line
+ off
,
794 len
- off
)) == -1 || nw
== 0) {
801 if (ferror(stream
)) {
812 warn("%s: SYSERR: %s", file
, syscall
);
813 if (rc
< MANDOCLEVEL_SYSERR
)
814 rc
= MANDOCLEVEL_SYSERR
;
818 koptions(int *options
, char *arg
)
821 if ( ! strcmp(arg
, "utf-8")) {
822 *options
|= MPARSE_UTF8
;
823 *options
&= ~MPARSE_LATIN1
;
824 } else if ( ! strcmp(arg
, "iso-8859-1")) {
825 *options
|= MPARSE_LATIN1
;
826 *options
&= ~MPARSE_UTF8
;
827 } else if ( ! strcmp(arg
, "us-ascii")) {
828 *options
&= ~(MPARSE_UTF8
| MPARSE_LATIN1
);
830 warnx("-K %s: Bad argument", arg
);
837 moptions(int *options
, char *arg
)
842 else if (0 == strcmp(arg
, "doc"))
843 *options
|= MPARSE_MDOC
;
844 else if (0 == strcmp(arg
, "andoc"))
846 else if (0 == strcmp(arg
, "an"))
847 *options
|= MPARSE_MAN
;
849 warnx("-m %s: Bad argument", arg
);
857 toptions(struct curparse
*curp
, char *arg
)
860 if (0 == strcmp(arg
, "ascii"))
861 curp
->outtype
= OUTT_ASCII
;
862 else if (0 == strcmp(arg
, "lint")) {
863 curp
->outtype
= OUTT_LINT
;
864 curp
->wlevel
= MANDOCLEVEL_WARNING
;
865 } else if (0 == strcmp(arg
, "tree"))
866 curp
->outtype
= OUTT_TREE
;
867 else if (0 == strcmp(arg
, "man"))
868 curp
->outtype
= OUTT_MAN
;
869 else if (0 == strcmp(arg
, "html"))
870 curp
->outtype
= OUTT_HTML
;
871 else if (0 == strcmp(arg
, "utf8"))
872 curp
->outtype
= OUTT_UTF8
;
873 else if (0 == strcmp(arg
, "locale"))
874 curp
->outtype
= OUTT_LOCALE
;
875 else if (0 == strcmp(arg
, "xhtml"))
876 curp
->outtype
= OUTT_HTML
;
877 else if (0 == strcmp(arg
, "ps"))
878 curp
->outtype
= OUTT_PS
;
879 else if (0 == strcmp(arg
, "pdf"))
880 curp
->outtype
= OUTT_PDF
;
882 warnx("-T %s: Bad argument", arg
);
890 woptions(struct curparse
*curp
, char *arg
)
905 switch (getsubopt(&arg
, UNCONST(toks
), &v
)) {
911 curp
->wlevel
= MANDOCLEVEL_WARNING
;
914 curp
->wlevel
= MANDOCLEVEL_ERROR
;
917 curp
->wlevel
= MANDOCLEVEL_UNSUPP
;
920 curp
->wlevel
= MANDOCLEVEL_BADARG
;
923 warnx("-W %s: Bad argument", o
);
932 mmsg(enum mandocerr t
, enum mandoclevel lvl
,
933 const char *file
, int line
, int col
, const char *msg
)
935 const char *mparse_msg
;
937 fprintf(stderr
, "%s: %s:", getprogname(), file
);
940 fprintf(stderr
, "%d:%d:", line
, col
+ 1);
942 fprintf(stderr
, " %s", mparse_strlevel(lvl
));
944 if (NULL
!= (mparse_msg
= mparse_strerror(t
)))
945 fprintf(stderr
, ": %s", mparse_msg
);
948 fprintf(stderr
, ": %s", msg
);
954 spawn_pager(struct tag_files
*tag_files
)
956 #define MAX_PAGER_ARGS 16
957 char *argv
[MAX_PAGER_ARGS
];
964 pager
= getenv("MANPAGER");
965 if (pager
== NULL
|| *pager
== '\0')
966 pager
= getenv("PAGER");
967 if (pager
== NULL
|| *pager
== '\0')
969 cp
= mandoc_strdup(pager
);
972 * Parse the pager command into words.
973 * Intentionally do not do anything fancy here.
977 while (argc
+ 4 < MAX_PAGER_ARGS
) {
979 cp
= strchr(cp
, ' ');
989 /* For more(1) and less(1), use the tag file. */
991 if ((cmdlen
= strlen(argv
[0])) >= 4) {
992 cp
= argv
[0] + cmdlen
- 4;
993 if (strcmp(cp
, "less") == 0 || strcmp(cp
, "more") == 0) {
994 argv
[argc
++] = mandoc_strdup("-T");
995 argv
[argc
++] = tag_files
->tfn
;
998 argv
[argc
++] = tag_files
->ofn
;
1001 switch (pager_pid
= fork()) {
1003 err((int)MANDOCLEVEL_SYSERR
, "fork");
1010 /* The child process becomes the pager. */
1012 if (dup2(tag_files
->ofd
, STDOUT_FILENO
) == -1)
1013 err((int)MANDOCLEVEL_SYSERR
, "pager stdout");
1014 close(tag_files
->ofd
);
1015 close(tag_files
->tfd
);
1016 execvp(argv
[0], argv
);
1017 err((int)MANDOCLEVEL_SYSERR
, "exec %s", argv
[0]);