]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
1 /* $Id: main.c,v 1.248 2015/10/12 00:08:15 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 struct mchars
*mchars
; /* character table */
81 enum mandoclevel wlevel
; /* ignore messages below this */
82 int wstop
; /* stop after a file with a warning */
83 enum outt outtype
; /* which output to use */
84 out_mdoc outmdoc
; /* mdoc output ptr */
85 out_man outman
; /* man output ptr */
86 out_free outfree
; /* free output ptr */
87 void *outdata
; /* data for output */
88 struct manoutput
*outopts
; /* output options */
91 static int fs_lookup(const struct manpaths
*,
92 size_t ipath
, const char *,
93 const char *, const char *,
94 struct manpage
**, size_t *);
95 static void fs_search(const struct mansearch
*,
96 const struct manpaths
*, int, char**,
97 struct manpage
**, size_t *);
98 static int koptions(int *, char *);
100 int mandocdb(int, char**);
102 static int moptions(int *, char *);
103 static void mmsg(enum mandocerr
, enum mandoclevel
,
104 const char *, int, int, const char *);
105 static void parse(struct curparse
*, int, const char *);
106 static void passthrough(const char *, int, int);
107 static pid_t
spawn_pager(struct tag_files
*);
108 static int toptions(struct curparse
*, char *);
109 static void usage(enum argmode
) __attribute__((noreturn
));
110 static int woptions(struct curparse
*, char *);
112 extern char *__progname
;
114 static const int sec_prios
[] = {1, 4, 5, 8, 6, 3, 7, 2, 9};
115 static char help_arg
[] = "help";
116 static char *help_argv
[] = {help_arg
, NULL
};
117 static enum mandoclevel rc
;
121 main(int argc
, char *argv
[])
124 struct curparse curp
;
125 struct mansearch search
;
126 struct tag_files
*tag_files
;
130 struct manpage
*res
, *resp
;
131 char *conf_file
, *defpaths
;
135 enum mandoclevel rctmp
;
136 enum outmode outmode
;
145 __progname
= mandoc_strdup("mandoc");
146 else if ((__progname
= strrchr(argv
[0], '/')) == NULL
)
147 __progname
= argv
[0];
153 if (strcmp(__progname
, BINM_MAKEWHATIS
) == 0)
154 return mandocdb(argc
, argv
);
157 /* Search options. */
159 memset(&conf
, 0, sizeof(conf
));
160 conf_file
= defpaths
= NULL
;
163 memset(&search
, 0, sizeof(struct mansearch
));
164 search
.outkey
= "Nd";
166 if (strcmp(__progname
, BINM_MAN
) == 0)
167 search
.argmode
= ARG_NAME
;
168 else if (strcmp(__progname
, BINM_APROPOS
) == 0)
169 search
.argmode
= ARG_EXPR
;
170 else if (strcmp(__progname
, BINM_WHATIS
) == 0)
171 search
.argmode
= ARG_WORD
;
172 else if (strncmp(__progname
, "help", 4) == 0)
173 search
.argmode
= ARG_NAME
;
175 search
.argmode
= ARG_FILE
;
177 /* Parser and formatter options. */
179 memset(&curp
, 0, sizeof(struct curparse
));
180 curp
.outtype
= OUTT_LOCALE
;
181 curp
.wlevel
= MANDOCLEVEL_BADARG
;
182 curp
.outopts
= &conf
.output
;
183 options
= MPARSE_SO
| MPARSE_UTF8
| MPARSE_LATIN1
;
189 outmode
= OUTMODE_DEF
;
191 while (-1 != (c
= getopt(argc
, argv
,
192 "aC:cfhI:iK:klM:m:O:S:s:T:VW:w"))) {
195 outmode
= OUTMODE_ALL
;
204 search
.argmode
= ARG_WORD
;
207 conf
.output
.synopsisonly
= 1;
209 outmode
= OUTMODE_ALL
;
212 if (strncmp(optarg
, "os=", 3)) {
213 warnx("-I %s: Bad argument", optarg
);
214 return (int)MANDOCLEVEL_BADARG
;
217 warnx("-I %s: Duplicate argument", optarg
);
218 return (int)MANDOCLEVEL_BADARG
;
220 defos
= mandoc_strdup(optarg
+ 3);
223 outmode
= OUTMODE_INT
;
226 if ( ! koptions(&options
, optarg
))
227 return (int)MANDOCLEVEL_BADARG
;
230 search
.argmode
= ARG_EXPR
;
233 search
.argmode
= ARG_FILE
;
234 outmode
= OUTMODE_ALL
;
243 search
.outkey
= optarg
;
244 while (optarg
!= NULL
)
245 manconf_output(&conf
.output
,
246 strsep(&optarg
, ","));
249 search
.arch
= optarg
;
255 if ( ! toptions(&curp
, optarg
))
256 return (int)MANDOCLEVEL_BADARG
;
259 if ( ! woptions(&curp
, optarg
))
260 return (int)MANDOCLEVEL_BADARG
;
263 outmode
= OUTMODE_FLN
;
272 usage(search
.argmode
);
274 /* Postprocess options. */
276 if (outmode
== OUTMODE_DEF
) {
277 switch (search
.argmode
) {
279 outmode
= OUTMODE_ALL
;
283 outmode
= OUTMODE_ONE
;
286 outmode
= OUTMODE_LST
;
291 if (outmode
== OUTMODE_FLN
||
292 outmode
== OUTMODE_LST
||
293 !isatty(STDOUT_FILENO
))
296 /* Parse arguments. */
306 * and for a man(1) section argument without -s.
309 if (search
.argmode
== ARG_NAME
) {
310 if (*__progname
== 'h') {
315 } else if (argc
> 1 &&
316 ((uc
= (unsigned char *)argv
[0]) != NULL
) &&
317 ((isdigit(uc
[0]) && (uc
[1] == '\0' ||
318 (isalpha(uc
[1]) && uc
[2] == '\0'))) ||
319 (uc
[0] == 'n' && uc
[1] == '\0'))) {
320 search
.sec
= (char *)uc
;
324 if (search
.arch
== NULL
)
325 search
.arch
= getenv("MACHINE");
327 if (search
.arch
== NULL
)
328 search
.arch
= MACHINE
;
334 /* man(1), whatis(1), apropos(1) */
336 if (search
.argmode
!= ARG_FILE
) {
338 usage(search
.argmode
);
340 if (search
.argmode
== ARG_NAME
&&
341 outmode
== OUTMODE_ONE
)
342 search
.firstmatch
= 1;
344 /* Access the mandoc database. */
346 manconf_parse(&conf
, conf_file
, defpaths
, auxpaths
);
349 if ( ! mansearch(&search
, &conf
.manpath
,
350 argc
, argv
, &res
, &sz
))
351 usage(search
.argmode
);
353 if (search
.argmode
!= ARG_NAME
) {
354 fputs("mandoc: database support not compiled in\n",
356 return (int)MANDOCLEVEL_BADARG
;
362 if (search
.argmode
== ARG_NAME
)
363 fs_search(&search
, &conf
.manpath
,
364 argc
, argv
, &res
, &sz
);
366 warnx("nothing appropriate");
370 rc
= MANDOCLEVEL_BADARG
;
375 * For standard man(1) and -a output mode,
376 * prepare for copying filename pointers
377 * into the program parameter array.
380 if (outmode
== OUTMODE_ONE
) {
383 } else if (outmode
== OUTMODE_ALL
)
386 /* Iterate all matching manuals. */
389 for (i
= 0; i
< sz
; i
++) {
390 if (outmode
== OUTMODE_FLN
)
392 else if (outmode
== OUTMODE_LST
)
393 printf("%s - %s\n", res
[i
].names
,
394 res
[i
].output
== NULL
? "" :
396 else if (outmode
== OUTMODE_ONE
) {
397 /* Search for the best section. */
398 isec
= strcspn(res
[i
].file
, "123456789");
399 sec
= res
[i
].file
[isec
];
402 prio
= sec_prios
[sec
- '1'];
403 if (prio
>= best_prio
)
411 * For man(1), -a and -i output mode, fall through
412 * to the main mandoc(1) code iterating files
413 * and running the parsers on each of them.
416 if (outmode
== OUTMODE_FLN
|| outmode
== OUTMODE_LST
)
422 if (search
.argmode
== ARG_FILE
&& ! moptions(&options
, auxpaths
))
423 return (int)MANDOCLEVEL_BADARG
;
425 curp
.mchars
= mchars_alloc();
426 curp
.mp
= mparse_alloc(options
, curp
.wlevel
, mmsg
,
430 * Conditionally start up the lookaside buffer before parsing.
432 if (OUTT_MAN
== curp
.outtype
)
433 mparse_keep(curp
.mp
);
437 tag_files
= tag_init();
438 parse(&curp
, STDIN_FILENO
, "<stdin>");
442 rctmp
= mparse_open(curp
.mp
, &fd
,
443 resp
!= NULL
? resp
->file
: *argv
);
449 tag_files
= tag_init();
454 parse(&curp
, fd
, *argv
);
455 else if (resp
->form
& FORM_SRC
) {
456 /* For .so only; ignore failure. */
457 chdir(conf
.manpath
.paths
[resp
->ipath
]);
458 parse(&curp
, fd
, resp
->file
);
460 passthrough(resp
->file
, fd
,
461 conf
.output
.synopsisonly
);
463 if (argc
> 1 && curp
.outtype
<= OUTT_UTF8
)
464 ascii_sepline(curp
.outdata
);
467 if (MANDOCLEVEL_OK
!= rc
&& curp
.wstop
)
475 mparse_reset(curp
.mp
);
479 (*curp
.outfree
)(curp
.outdata
);
480 mparse_free(curp
.mp
);
481 mchars_free(curp
.mchars
);
484 if (search
.argmode
!= ARG_FILE
) {
487 mansearch_free(res
, sz
);
495 * When using a pager, finish writing both temporary files,
496 * fork it, wait for the user to close it, and clean up.
499 if (tag_files
!= NULL
) {
502 waitpid(spawn_pager(tag_files
), NULL
, 0);
510 usage(enum argmode argmode
)
515 fputs("usage: mandoc [-acfhkl] [-I os=name] "
516 "[-K encoding] [-mformat] [-O option]\n"
517 "\t [-T output] [-W level] [file ...]\n", stderr
);
520 fputs("usage: man [-acfhklw] [-C file] [-I os=name] "
521 "[-K encoding] [-M path] [-m path]\n"
522 "\t [-O option=value] [-S subsection] [-s section] "
523 "[-T output] [-W level]\n"
524 "\t [section] name ...\n", stderr
);
527 fputs("usage: whatis [-acfhklw] [-C file] "
528 "[-M path] [-m path] [-O outkey] [-S arch]\n"
529 "\t [-s section] name ...\n", stderr
);
532 fputs("usage: apropos [-acfhklw] [-C file] "
533 "[-M path] [-m path] [-O outkey] [-S arch]\n"
534 "\t [-s section] expression ...\n", stderr
);
537 exit((int)MANDOCLEVEL_BADARG
);
541 fs_lookup(const struct manpaths
*paths
, size_t ipath
,
542 const char *sec
, const char *arch
, const char *name
,
543 struct manpage
**res
, size_t *ressz
)
546 struct manpage
*page
;
551 mandoc_asprintf(&file
, "%s/man%s/%s.%s",
552 paths
->paths
[ipath
], sec
, name
, sec
);
553 if (access(file
, R_OK
) != -1)
557 mandoc_asprintf(&file
, "%s/cat%s/%s.0",
558 paths
->paths
[ipath
], sec
, name
);
559 if (access(file
, R_OK
) != -1) {
566 mandoc_asprintf(&file
, "%s/man%s/%s/%s.%s",
567 paths
->paths
[ipath
], sec
, arch
, name
, sec
);
568 if (access(file
, R_OK
) != -1)
573 mandoc_asprintf(&file
, "%s/man%s/%s.[01-9]*",
574 paths
->paths
[ipath
], sec
, name
);
575 globres
= glob(file
, 0, NULL
, &globinfo
);
576 if (globres
!= 0 && globres
!= GLOB_NOMATCH
)
577 warn("%s: glob", file
);
580 file
= mandoc_strdup(*globinfo
.gl_pathv
);
587 warnx("outdated mandoc.db lacks %s(%s) entry, run makewhatis %s\n",
588 name
, sec
, paths
->paths
[ipath
]);
590 *res
= mandoc_reallocarray(*res
, ++*ressz
, sizeof(struct manpage
));
591 page
= *res
+ (*ressz
- 1);
596 page
->bits
= NAME_FILE
& NAME_MASK
;
597 page
->sec
= (*sec
>= '1' && *sec
<= '9') ? *sec
- '1' + 1 : 10;
603 fs_search(const struct mansearch
*cfg
, const struct manpaths
*paths
,
604 int argc
, char **argv
, struct manpage
**res
, size_t *ressz
)
606 const char *const sections
[] =
607 {"1", "8", "6", "2", "3", "3p", "5", "7", "4", "9"};
608 const size_t nsec
= sizeof(sections
)/sizeof(sections
[0]);
610 size_t ipath
, isec
, lastsz
;
612 assert(cfg
->argmode
== ARG_NAME
);
617 for (ipath
= 0; ipath
< paths
->sz
; ipath
++) {
618 if (cfg
->sec
!= NULL
) {
619 if (fs_lookup(paths
, ipath
, cfg
->sec
,
620 cfg
->arch
, *argv
, res
, ressz
) &&
623 } else for (isec
= 0; isec
< nsec
; isec
++)
624 if (fs_lookup(paths
, ipath
, sections
[isec
],
625 cfg
->arch
, *argv
, res
, ressz
) &&
629 if (*ressz
== lastsz
)
630 warnx("No entry for %s in the manual.", *argv
);
638 parse(struct curparse
*curp
, int fd
, const char *file
)
640 enum mandoclevel rctmp
;
641 struct roff_man
*man
;
643 /* Begin by parsing the file itself. */
648 rctmp
= mparse_readfd(curp
->mp
, fd
, file
);
653 * With -Wstop and warnings or errors of at least the requested
654 * level, do not produce output.
657 if (rctmp
!= MANDOCLEVEL_OK
&& curp
->wstop
)
660 /* If unset, allocate output dev now (if applicable). */
662 if ( ! (curp
->outman
&& curp
->outmdoc
)) {
663 switch (curp
->outtype
) {
665 curp
->outdata
= html_alloc(curp
->mchars
,
667 curp
->outfree
= html_free
;
670 curp
->outdata
= utf8_alloc(curp
->mchars
,
672 curp
->outfree
= ascii_free
;
675 curp
->outdata
= locale_alloc(curp
->mchars
,
677 curp
->outfree
= ascii_free
;
680 curp
->outdata
= ascii_alloc(curp
->mchars
,
682 curp
->outfree
= ascii_free
;
685 curp
->outdata
= pdf_alloc(curp
->mchars
,
687 curp
->outfree
= pspdf_free
;
690 curp
->outdata
= ps_alloc(curp
->mchars
,
692 curp
->outfree
= pspdf_free
;
698 switch (curp
->outtype
) {
700 curp
->outman
= html_man
;
701 curp
->outmdoc
= html_mdoc
;
704 curp
->outman
= tree_man
;
705 curp
->outmdoc
= tree_mdoc
;
708 curp
->outmdoc
= man_mdoc
;
709 curp
->outman
= man_man
;
716 curp
->outman
= terminal_man
;
717 curp
->outmdoc
= terminal_mdoc
;
724 mparse_result(curp
->mp
, &man
, NULL
);
726 /* Execute the out device, if it exists. */
730 if (curp
->outmdoc
!= NULL
&& man
->macroset
== MACROSET_MDOC
)
731 (*curp
->outmdoc
)(curp
->outdata
, man
);
732 if (curp
->outman
!= NULL
&& man
->macroset
== MACROSET_MAN
)
733 (*curp
->outman
)(curp
->outdata
, man
);
737 passthrough(const char *file
, int fd
, int synopsis_only
)
739 const char synb
[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
740 const char synr
[] = "SYNOPSIS";
751 if ((stream
= fdopen(fd
, "r")) == NULL
) {
758 while ((line
= fgetln(stream
, &len
)) != NULL
) {
761 if ( ! isspace((unsigned char)*line
))
764 isspace((unsigned char)*line
)) {
769 if ((len
== sizeof(synb
) &&
770 ! strncmp(line
, synb
, len
- 1)) ||
771 (len
== sizeof(synr
) &&
772 ! strncmp(line
, synr
, len
- 1)))
777 for (off
= 0; off
< len
; off
+= nw
)
778 if ((nw
= write(STDOUT_FILENO
, line
+ off
,
779 len
- off
)) == -1 || nw
== 0) {
786 if (ferror(stream
)) {
797 warn("%s: SYSERR: %s", file
, syscall
);
798 if (rc
< MANDOCLEVEL_SYSERR
)
799 rc
= MANDOCLEVEL_SYSERR
;
803 koptions(int *options
, char *arg
)
806 if ( ! strcmp(arg
, "utf-8")) {
807 *options
|= MPARSE_UTF8
;
808 *options
&= ~MPARSE_LATIN1
;
809 } else if ( ! strcmp(arg
, "iso-8859-1")) {
810 *options
|= MPARSE_LATIN1
;
811 *options
&= ~MPARSE_UTF8
;
812 } else if ( ! strcmp(arg
, "us-ascii")) {
813 *options
&= ~(MPARSE_UTF8
| MPARSE_LATIN1
);
815 warnx("-K %s: Bad argument", arg
);
822 moptions(int *options
, char *arg
)
827 else if (0 == strcmp(arg
, "doc"))
828 *options
|= MPARSE_MDOC
;
829 else if (0 == strcmp(arg
, "andoc"))
831 else if (0 == strcmp(arg
, "an"))
832 *options
|= MPARSE_MAN
;
834 warnx("-m %s: Bad argument", arg
);
842 toptions(struct curparse
*curp
, char *arg
)
845 if (0 == strcmp(arg
, "ascii"))
846 curp
->outtype
= OUTT_ASCII
;
847 else if (0 == strcmp(arg
, "lint")) {
848 curp
->outtype
= OUTT_LINT
;
849 curp
->wlevel
= MANDOCLEVEL_WARNING
;
850 } else if (0 == strcmp(arg
, "tree"))
851 curp
->outtype
= OUTT_TREE
;
852 else if (0 == strcmp(arg
, "man"))
853 curp
->outtype
= OUTT_MAN
;
854 else if (0 == strcmp(arg
, "html"))
855 curp
->outtype
= OUTT_HTML
;
856 else if (0 == strcmp(arg
, "utf8"))
857 curp
->outtype
= OUTT_UTF8
;
858 else if (0 == strcmp(arg
, "locale"))
859 curp
->outtype
= OUTT_LOCALE
;
860 else if (0 == strcmp(arg
, "xhtml"))
861 curp
->outtype
= OUTT_HTML
;
862 else if (0 == strcmp(arg
, "ps"))
863 curp
->outtype
= OUTT_PS
;
864 else if (0 == strcmp(arg
, "pdf"))
865 curp
->outtype
= OUTT_PDF
;
867 warnx("-T %s: Bad argument", arg
);
875 woptions(struct curparse
*curp
, char *arg
)
890 switch (getsubopt(&arg
, UNCONST(toks
), &v
)) {
896 curp
->wlevel
= MANDOCLEVEL_WARNING
;
899 curp
->wlevel
= MANDOCLEVEL_ERROR
;
902 curp
->wlevel
= MANDOCLEVEL_UNSUPP
;
905 curp
->wlevel
= MANDOCLEVEL_BADARG
;
908 warnx("-W %s: Bad argument", o
);
917 mmsg(enum mandocerr t
, enum mandoclevel lvl
,
918 const char *file
, int line
, int col
, const char *msg
)
920 const char *mparse_msg
;
922 fprintf(stderr
, "%s: %s:", __progname
, file
);
925 fprintf(stderr
, "%d:%d:", line
, col
+ 1);
927 fprintf(stderr
, " %s", mparse_strlevel(lvl
));
929 if (NULL
!= (mparse_msg
= mparse_strerror(t
)))
930 fprintf(stderr
, ": %s", mparse_msg
);
933 fprintf(stderr
, ": %s", msg
);
939 spawn_pager(struct tag_files
*tag_files
)
941 #define MAX_PAGER_ARGS 16
942 char *argv
[MAX_PAGER_ARGS
];
949 pager
= getenv("MANPAGER");
950 if (pager
== NULL
|| *pager
== '\0')
951 pager
= getenv("PAGER");
952 if (pager
== NULL
|| *pager
== '\0')
954 cp
= mandoc_strdup(pager
);
957 * Parse the pager command into words.
958 * Intentionally do not do anything fancy here.
962 while (argc
+ 4 < MAX_PAGER_ARGS
) {
964 cp
= strchr(cp
, ' ');
974 /* For more(1) and less(1), use the tag file. */
976 if ((cmdlen
= strlen(argv
[0])) >= 4) {
977 cp
= argv
[0] + cmdlen
- 4;
978 if (strcmp(cp
, "less") == 0 || strcmp(cp
, "more") == 0) {
979 argv
[argc
++] = mandoc_strdup("-T");
980 argv
[argc
++] = tag_files
->tfn
;
983 argv
[argc
++] = tag_files
->ofn
;
986 switch (pager_pid
= fork()) {
988 err((int)MANDOCLEVEL_SYSERR
, "fork");
995 /* The child process becomes the pager. */
997 if (dup2(tag_files
->ofd
, STDOUT_FILENO
) == -1)
998 err((int)MANDOCLEVEL_SYSERR
, "pager stdout");
999 close(tag_files
->ofd
);
1000 close(tag_files
->tfd
);
1001 execvp(argv
[0], argv
);
1002 err((int)MANDOCLEVEL_SYSERR
, "exec %s", argv
[0]);