]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
1 /* $Id: main.c,v 1.249 2015/10/13 22:59:54 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) */
62 typedef void (*out_mdoc
)(void *, const struct roff_man
*);
63 typedef void (*out_man
)(void *, const struct roff_man
*);
64 typedef void (*out_free
)(void *);
67 OUTT_ASCII
= 0, /* -Tascii */
68 OUTT_LOCALE
, /* -Tlocale */
69 OUTT_UTF8
, /* -Tutf8 */
70 OUTT_TREE
, /* -Ttree */
72 OUTT_HTML
, /* -Thtml */
73 OUTT_LINT
, /* -Tlint */
80 enum mandoclevel wlevel
; /* ignore messages below this */
81 int wstop
; /* stop after a file with a warning */
82 enum outt outtype
; /* which output to use */
83 out_mdoc outmdoc
; /* mdoc output ptr */
84 out_man outman
; /* man output ptr */
85 out_free outfree
; /* free output ptr */
86 void *outdata
; /* data for output */
87 struct manoutput
*outopts
; /* output options */
90 static int fs_lookup(const struct manpaths
*,
91 size_t ipath
, const char *,
92 const char *, const char *,
93 struct manpage
**, size_t *);
94 static void fs_search(const struct mansearch
*,
95 const struct manpaths
*, int, char**,
96 struct manpage
**, size_t *);
97 static int koptions(int *, char *);
99 int mandocdb(int, char**);
101 static int moptions(int *, char *);
102 static void mmsg(enum mandocerr
, enum mandoclevel
,
103 const char *, int, int, const char *);
104 static void parse(struct curparse
*, int, const char *);
105 static void passthrough(const char *, int, int);
106 static pid_t
spawn_pager(struct tag_files
*);
107 static int toptions(struct curparse
*, char *);
108 static void usage(enum argmode
) __attribute__((noreturn
));
109 static int woptions(struct curparse
*, char *);
111 extern char *__progname
;
113 static const int sec_prios
[] = {1, 4, 5, 8, 6, 3, 7, 2, 9};
114 static char help_arg
[] = "help";
115 static char *help_argv
[] = {help_arg
, NULL
};
116 static enum mandoclevel rc
;
120 main(int argc
, char *argv
[])
123 struct curparse curp
;
124 struct mansearch search
;
125 struct tag_files
*tag_files
;
129 struct manpage
*res
, *resp
;
130 char *conf_file
, *defpaths
;
134 enum mandoclevel rctmp
;
135 enum outmode outmode
;
144 __progname
= mandoc_strdup("mandoc");
145 else if ((__progname
= strrchr(argv
[0], '/')) == NULL
)
146 __progname
= argv
[0];
152 if (strcmp(__progname
, BINM_MAKEWHATIS
) == 0)
153 return mandocdb(argc
, argv
);
156 /* Search options. */
158 memset(&conf
, 0, sizeof(conf
));
159 conf_file
= defpaths
= NULL
;
162 memset(&search
, 0, sizeof(struct mansearch
));
163 search
.outkey
= "Nd";
165 if (strcmp(__progname
, BINM_MAN
) == 0)
166 search
.argmode
= ARG_NAME
;
167 else if (strcmp(__progname
, BINM_APROPOS
) == 0)
168 search
.argmode
= ARG_EXPR
;
169 else if (strcmp(__progname
, BINM_WHATIS
) == 0)
170 search
.argmode
= ARG_WORD
;
171 else if (strncmp(__progname
, "help", 4) == 0)
172 search
.argmode
= ARG_NAME
;
174 search
.argmode
= ARG_FILE
;
176 /* Parser and formatter options. */
178 memset(&curp
, 0, sizeof(struct curparse
));
179 curp
.outtype
= OUTT_LOCALE
;
180 curp
.wlevel
= MANDOCLEVEL_BADARG
;
181 curp
.outopts
= &conf
.output
;
182 options
= MPARSE_SO
| MPARSE_UTF8
| MPARSE_LATIN1
;
188 outmode
= OUTMODE_DEF
;
190 while (-1 != (c
= getopt(argc
, argv
,
191 "aC:cfhI:iK:klM:m:O:S:s:T:VW:w"))) {
194 outmode
= OUTMODE_ALL
;
203 search
.argmode
= ARG_WORD
;
206 conf
.output
.synopsisonly
= 1;
208 outmode
= OUTMODE_ALL
;
211 if (strncmp(optarg
, "os=", 3)) {
212 warnx("-I %s: Bad argument", optarg
);
213 return (int)MANDOCLEVEL_BADARG
;
216 warnx("-I %s: Duplicate argument", optarg
);
217 return (int)MANDOCLEVEL_BADARG
;
219 defos
= mandoc_strdup(optarg
+ 3);
222 outmode
= OUTMODE_INT
;
225 if ( ! koptions(&options
, optarg
))
226 return (int)MANDOCLEVEL_BADARG
;
229 search
.argmode
= ARG_EXPR
;
232 search
.argmode
= ARG_FILE
;
233 outmode
= OUTMODE_ALL
;
242 search
.outkey
= optarg
;
243 while (optarg
!= NULL
)
244 manconf_output(&conf
.output
,
245 strsep(&optarg
, ","));
248 search
.arch
= optarg
;
254 if ( ! toptions(&curp
, optarg
))
255 return (int)MANDOCLEVEL_BADARG
;
258 if ( ! woptions(&curp
, optarg
))
259 return (int)MANDOCLEVEL_BADARG
;
262 outmode
= OUTMODE_FLN
;
271 usage(search
.argmode
);
273 /* Postprocess options. */
275 if (outmode
== OUTMODE_DEF
) {
276 switch (search
.argmode
) {
278 outmode
= OUTMODE_ALL
;
282 outmode
= OUTMODE_ONE
;
285 outmode
= OUTMODE_LST
;
290 if (outmode
== OUTMODE_FLN
||
291 outmode
== OUTMODE_LST
||
292 !isatty(STDOUT_FILENO
))
295 /* Parse arguments. */
305 * and for a man(1) section argument without -s.
308 if (search
.argmode
== ARG_NAME
) {
309 if (*__progname
== 'h') {
314 } else if (argc
> 1 &&
315 ((uc
= (unsigned char *)argv
[0]) != NULL
) &&
316 ((isdigit(uc
[0]) && (uc
[1] == '\0' ||
317 (isalpha(uc
[1]) && uc
[2] == '\0'))) ||
318 (uc
[0] == 'n' && uc
[1] == '\0'))) {
319 search
.sec
= (char *)uc
;
323 if (search
.arch
== NULL
)
324 search
.arch
= getenv("MACHINE");
326 if (search
.arch
== NULL
)
327 search
.arch
= MACHINE
;
333 /* man(1), whatis(1), apropos(1) */
335 if (search
.argmode
!= ARG_FILE
) {
337 usage(search
.argmode
);
339 if (search
.argmode
== ARG_NAME
&&
340 outmode
== OUTMODE_ONE
)
341 search
.firstmatch
= 1;
343 /* Access the mandoc database. */
345 manconf_parse(&conf
, conf_file
, defpaths
, auxpaths
);
348 if ( ! mansearch(&search
, &conf
.manpath
,
349 argc
, argv
, &res
, &sz
))
350 usage(search
.argmode
);
352 if (search
.argmode
!= ARG_NAME
) {
353 fputs("mandoc: database support not compiled in\n",
355 return (int)MANDOCLEVEL_BADARG
;
361 if (search
.argmode
== ARG_NAME
)
362 fs_search(&search
, &conf
.manpath
,
363 argc
, argv
, &res
, &sz
);
365 warnx("nothing appropriate");
369 rc
= MANDOCLEVEL_BADARG
;
374 * For standard man(1) and -a output mode,
375 * prepare for copying filename pointers
376 * into the program parameter array.
379 if (outmode
== OUTMODE_ONE
) {
382 } else if (outmode
== OUTMODE_ALL
)
385 /* Iterate all matching manuals. */
388 for (i
= 0; i
< sz
; i
++) {
389 if (outmode
== OUTMODE_FLN
)
391 else if (outmode
== OUTMODE_LST
)
392 printf("%s - %s\n", res
[i
].names
,
393 res
[i
].output
== NULL
? "" :
395 else if (outmode
== OUTMODE_ONE
) {
396 /* Search for the best section. */
397 isec
= strcspn(res
[i
].file
, "123456789");
398 sec
= res
[i
].file
[isec
];
401 prio
= sec_prios
[sec
- '1'];
402 if (prio
>= best_prio
)
410 * For man(1), -a and -i output mode, fall through
411 * to the main mandoc(1) code iterating files
412 * and running the parsers on each of them.
415 if (outmode
== OUTMODE_FLN
|| outmode
== OUTMODE_LST
)
421 if (search
.argmode
== ARG_FILE
&& ! moptions(&options
, auxpaths
))
422 return (int)MANDOCLEVEL_BADARG
;
425 curp
.mp
= mparse_alloc(options
, curp
.wlevel
, mmsg
, defos
);
428 * Conditionally start up the lookaside buffer before parsing.
430 if (OUTT_MAN
== curp
.outtype
)
431 mparse_keep(curp
.mp
);
435 tag_files
= tag_init();
436 parse(&curp
, STDIN_FILENO
, "<stdin>");
440 rctmp
= mparse_open(curp
.mp
, &fd
,
441 resp
!= NULL
? resp
->file
: *argv
);
447 tag_files
= tag_init();
452 parse(&curp
, fd
, *argv
);
453 else if (resp
->form
& FORM_SRC
) {
454 /* For .so only; ignore failure. */
455 chdir(conf
.manpath
.paths
[resp
->ipath
]);
456 parse(&curp
, fd
, resp
->file
);
458 passthrough(resp
->file
, fd
,
459 conf
.output
.synopsisonly
);
461 if (argc
> 1 && curp
.outtype
<= OUTT_UTF8
)
462 ascii_sepline(curp
.outdata
);
465 if (MANDOCLEVEL_OK
!= rc
&& curp
.wstop
)
473 mparse_reset(curp
.mp
);
477 (*curp
.outfree
)(curp
.outdata
);
478 mparse_free(curp
.mp
);
482 if (search
.argmode
!= ARG_FILE
) {
485 mansearch_free(res
, sz
);
493 * When using a pager, finish writing both temporary files,
494 * fork it, wait for the user to close it, and clean up.
497 if (tag_files
!= NULL
) {
500 waitpid(spawn_pager(tag_files
), NULL
, 0);
508 usage(enum argmode argmode
)
513 fputs("usage: mandoc [-acfhkl] [-I os=name] "
514 "[-K encoding] [-mformat] [-O option]\n"
515 "\t [-T output] [-W level] [file ...]\n", stderr
);
518 fputs("usage: man [-acfhklw] [-C file] [-I os=name] "
519 "[-K encoding] [-M path] [-m path]\n"
520 "\t [-O option=value] [-S subsection] [-s section] "
521 "[-T output] [-W level]\n"
522 "\t [section] name ...\n", stderr
);
525 fputs("usage: whatis [-acfhklw] [-C file] "
526 "[-M path] [-m path] [-O outkey] [-S arch]\n"
527 "\t [-s section] name ...\n", stderr
);
530 fputs("usage: apropos [-acfhklw] [-C file] "
531 "[-M path] [-m path] [-O outkey] [-S arch]\n"
532 "\t [-s section] expression ...\n", stderr
);
535 exit((int)MANDOCLEVEL_BADARG
);
539 fs_lookup(const struct manpaths
*paths
, size_t ipath
,
540 const char *sec
, const char *arch
, const char *name
,
541 struct manpage
**res
, size_t *ressz
)
544 struct manpage
*page
;
549 mandoc_asprintf(&file
, "%s/man%s/%s.%s",
550 paths
->paths
[ipath
], sec
, name
, sec
);
551 if (access(file
, R_OK
) != -1)
555 mandoc_asprintf(&file
, "%s/cat%s/%s.0",
556 paths
->paths
[ipath
], sec
, name
);
557 if (access(file
, R_OK
) != -1) {
564 mandoc_asprintf(&file
, "%s/man%s/%s/%s.%s",
565 paths
->paths
[ipath
], sec
, arch
, name
, sec
);
566 if (access(file
, R_OK
) != -1)
571 mandoc_asprintf(&file
, "%s/man%s/%s.[01-9]*",
572 paths
->paths
[ipath
], sec
, name
);
573 globres
= glob(file
, 0, NULL
, &globinfo
);
574 if (globres
!= 0 && globres
!= GLOB_NOMATCH
)
575 warn("%s: glob", file
);
578 file
= mandoc_strdup(*globinfo
.gl_pathv
);
585 warnx("outdated mandoc.db lacks %s(%s) entry, run makewhatis %s\n",
586 name
, sec
, paths
->paths
[ipath
]);
588 *res
= mandoc_reallocarray(*res
, ++*ressz
, sizeof(struct manpage
));
589 page
= *res
+ (*ressz
- 1);
594 page
->bits
= NAME_FILE
& NAME_MASK
;
595 page
->sec
= (*sec
>= '1' && *sec
<= '9') ? *sec
- '1' + 1 : 10;
601 fs_search(const struct mansearch
*cfg
, const struct manpaths
*paths
,
602 int argc
, char **argv
, struct manpage
**res
, size_t *ressz
)
604 const char *const sections
[] =
605 {"1", "8", "6", "2", "3", "3p", "5", "7", "4", "9"};
606 const size_t nsec
= sizeof(sections
)/sizeof(sections
[0]);
608 size_t ipath
, isec
, lastsz
;
610 assert(cfg
->argmode
== ARG_NAME
);
615 for (ipath
= 0; ipath
< paths
->sz
; ipath
++) {
616 if (cfg
->sec
!= NULL
) {
617 if (fs_lookup(paths
, ipath
, cfg
->sec
,
618 cfg
->arch
, *argv
, res
, ressz
) &&
621 } else for (isec
= 0; isec
< nsec
; isec
++)
622 if (fs_lookup(paths
, ipath
, sections
[isec
],
623 cfg
->arch
, *argv
, res
, ressz
) &&
627 if (*ressz
== lastsz
)
628 warnx("No entry for %s in the manual.", *argv
);
636 parse(struct curparse
*curp
, int fd
, const char *file
)
638 enum mandoclevel rctmp
;
639 struct roff_man
*man
;
641 /* Begin by parsing the file itself. */
646 rctmp
= mparse_readfd(curp
->mp
, fd
, file
);
651 * With -Wstop and warnings or errors of at least the requested
652 * level, do not produce output.
655 if (rctmp
!= MANDOCLEVEL_OK
&& curp
->wstop
)
658 /* If unset, allocate output dev now (if applicable). */
660 if ( ! (curp
->outman
&& curp
->outmdoc
)) {
661 switch (curp
->outtype
) {
663 curp
->outdata
= html_alloc(curp
->outopts
);
664 curp
->outfree
= html_free
;
667 curp
->outdata
= utf8_alloc(curp
->outopts
);
668 curp
->outfree
= ascii_free
;
671 curp
->outdata
= locale_alloc(curp
->outopts
);
672 curp
->outfree
= ascii_free
;
675 curp
->outdata
= ascii_alloc(curp
->outopts
);
676 curp
->outfree
= ascii_free
;
679 curp
->outdata
= pdf_alloc(curp
->outopts
);
680 curp
->outfree
= pspdf_free
;
683 curp
->outdata
= ps_alloc(curp
->outopts
);
684 curp
->outfree
= pspdf_free
;
690 switch (curp
->outtype
) {
692 curp
->outman
= html_man
;
693 curp
->outmdoc
= html_mdoc
;
696 curp
->outman
= tree_man
;
697 curp
->outmdoc
= tree_mdoc
;
700 curp
->outmdoc
= man_mdoc
;
701 curp
->outman
= man_man
;
708 curp
->outman
= terminal_man
;
709 curp
->outmdoc
= terminal_mdoc
;
716 mparse_result(curp
->mp
, &man
, NULL
);
718 /* Execute the out device, if it exists. */
722 if (curp
->outmdoc
!= NULL
&& man
->macroset
== MACROSET_MDOC
)
723 (*curp
->outmdoc
)(curp
->outdata
, man
);
724 if (curp
->outman
!= NULL
&& man
->macroset
== MACROSET_MAN
)
725 (*curp
->outman
)(curp
->outdata
, man
);
729 passthrough(const char *file
, int fd
, int synopsis_only
)
731 const char synb
[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
732 const char synr
[] = "SYNOPSIS";
743 if ((stream
= fdopen(fd
, "r")) == NULL
) {
750 while ((line
= fgetln(stream
, &len
)) != NULL
) {
753 if ( ! isspace((unsigned char)*line
))
756 isspace((unsigned char)*line
)) {
761 if ((len
== sizeof(synb
) &&
762 ! strncmp(line
, synb
, len
- 1)) ||
763 (len
== sizeof(synr
) &&
764 ! strncmp(line
, synr
, len
- 1)))
769 for (off
= 0; off
< len
; off
+= nw
)
770 if ((nw
= write(STDOUT_FILENO
, line
+ off
,
771 len
- off
)) == -1 || nw
== 0) {
778 if (ferror(stream
)) {
789 warn("%s: SYSERR: %s", file
, syscall
);
790 if (rc
< MANDOCLEVEL_SYSERR
)
791 rc
= MANDOCLEVEL_SYSERR
;
795 koptions(int *options
, char *arg
)
798 if ( ! strcmp(arg
, "utf-8")) {
799 *options
|= MPARSE_UTF8
;
800 *options
&= ~MPARSE_LATIN1
;
801 } else if ( ! strcmp(arg
, "iso-8859-1")) {
802 *options
|= MPARSE_LATIN1
;
803 *options
&= ~MPARSE_UTF8
;
804 } else if ( ! strcmp(arg
, "us-ascii")) {
805 *options
&= ~(MPARSE_UTF8
| MPARSE_LATIN1
);
807 warnx("-K %s: Bad argument", arg
);
814 moptions(int *options
, char *arg
)
819 else if (0 == strcmp(arg
, "doc"))
820 *options
|= MPARSE_MDOC
;
821 else if (0 == strcmp(arg
, "andoc"))
823 else if (0 == strcmp(arg
, "an"))
824 *options
|= MPARSE_MAN
;
826 warnx("-m %s: Bad argument", arg
);
834 toptions(struct curparse
*curp
, char *arg
)
837 if (0 == strcmp(arg
, "ascii"))
838 curp
->outtype
= OUTT_ASCII
;
839 else if (0 == strcmp(arg
, "lint")) {
840 curp
->outtype
= OUTT_LINT
;
841 curp
->wlevel
= MANDOCLEVEL_WARNING
;
842 } else if (0 == strcmp(arg
, "tree"))
843 curp
->outtype
= OUTT_TREE
;
844 else if (0 == strcmp(arg
, "man"))
845 curp
->outtype
= OUTT_MAN
;
846 else if (0 == strcmp(arg
, "html"))
847 curp
->outtype
= OUTT_HTML
;
848 else if (0 == strcmp(arg
, "utf8"))
849 curp
->outtype
= OUTT_UTF8
;
850 else if (0 == strcmp(arg
, "locale"))
851 curp
->outtype
= OUTT_LOCALE
;
852 else if (0 == strcmp(arg
, "xhtml"))
853 curp
->outtype
= OUTT_HTML
;
854 else if (0 == strcmp(arg
, "ps"))
855 curp
->outtype
= OUTT_PS
;
856 else if (0 == strcmp(arg
, "pdf"))
857 curp
->outtype
= OUTT_PDF
;
859 warnx("-T %s: Bad argument", arg
);
867 woptions(struct curparse
*curp
, char *arg
)
882 switch (getsubopt(&arg
, UNCONST(toks
), &v
)) {
888 curp
->wlevel
= MANDOCLEVEL_WARNING
;
891 curp
->wlevel
= MANDOCLEVEL_ERROR
;
894 curp
->wlevel
= MANDOCLEVEL_UNSUPP
;
897 curp
->wlevel
= MANDOCLEVEL_BADARG
;
900 warnx("-W %s: Bad argument", o
);
909 mmsg(enum mandocerr t
, enum mandoclevel lvl
,
910 const char *file
, int line
, int col
, const char *msg
)
912 const char *mparse_msg
;
914 fprintf(stderr
, "%s: %s:", __progname
, file
);
917 fprintf(stderr
, "%d:%d:", line
, col
+ 1);
919 fprintf(stderr
, " %s", mparse_strlevel(lvl
));
921 if (NULL
!= (mparse_msg
= mparse_strerror(t
)))
922 fprintf(stderr
, ": %s", mparse_msg
);
925 fprintf(stderr
, ": %s", msg
);
931 spawn_pager(struct tag_files
*tag_files
)
933 #define MAX_PAGER_ARGS 16
934 char *argv
[MAX_PAGER_ARGS
];
941 pager
= getenv("MANPAGER");
942 if (pager
== NULL
|| *pager
== '\0')
943 pager
= getenv("PAGER");
944 if (pager
== NULL
|| *pager
== '\0')
946 cp
= mandoc_strdup(pager
);
949 * Parse the pager command into words.
950 * Intentionally do not do anything fancy here.
954 while (argc
+ 4 < MAX_PAGER_ARGS
) {
956 cp
= strchr(cp
, ' ');
966 /* For more(1) and less(1), use the tag file. */
968 if ((cmdlen
= strlen(argv
[0])) >= 4) {
969 cp
= argv
[0] + cmdlen
- 4;
970 if (strcmp(cp
, "less") == 0 || strcmp(cp
, "more") == 0) {
971 argv
[argc
++] = mandoc_strdup("-T");
972 argv
[argc
++] = tag_files
->tfn
;
975 argv
[argc
++] = tag_files
->ofn
;
978 switch (pager_pid
= fork()) {
980 err((int)MANDOCLEVEL_SYSERR
, "fork");
987 /* The child process becomes the pager. */
989 if (dup2(tag_files
->ofd
, STDOUT_FILENO
) == -1)
990 err((int)MANDOCLEVEL_SYSERR
, "pager stdout");
991 close(tag_files
->ofd
);
992 close(tag_files
->tfd
);
993 execvp(argv
[0], argv
);
994 err((int)MANDOCLEVEL_SYSERR
, "exec %s", argv
[0]);