]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
1 /* $Id: main.c,v 1.182 2014/08/21 00:32:15 schwarze Exp $ */
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010, 2011, 2012, 2014 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 AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR 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>
31 #include "mandoc_aux.h"
36 #include "mansearch.h"
38 #if !defined(__GNUC__) || (__GNUC__ < 2)
40 # define __attribute__(x)
42 #endif /* !defined(__GNUC__) || (__GNUC__ < 2) */
53 typedef void (*out_mdoc
)(void *, const struct mdoc
*);
54 typedef void (*out_man
)(void *, const struct man
*);
55 typedef void (*out_free
)(void *);
58 OUTT_ASCII
= 0, /* -Tascii */
59 OUTT_LOCALE
, /* -Tlocale */
60 OUTT_UTF8
, /* -Tutf8 */
61 OUTT_TREE
, /* -Ttree */
63 OUTT_HTML
, /* -Thtml */
64 OUTT_XHTML
, /* -Txhtml */
65 OUTT_LINT
, /* -Tlint */
72 enum mandoclevel wlevel
; /* ignore messages below this */
73 int wstop
; /* stop after a file with a warning */
74 enum outt outtype
; /* which output to use */
75 out_mdoc outmdoc
; /* mdoc output ptr */
76 out_man outman
; /* man output ptr */
77 out_free outfree
; /* free output ptr */
78 void *outdata
; /* data for output */
79 char outopts
[BUFSIZ
]; /* buf of output opts */
82 static int moptions(int *, char *);
83 static void mmsg(enum mandocerr
, enum mandoclevel
,
84 const char *, int, int, const char *);
85 static void parse(struct curparse
*, int,
86 const char *, enum mandoclevel
*);
87 static int toptions(struct curparse
*, char *);
88 static void usage(enum argmode
) __attribute__((noreturn
));
89 static void version(void) __attribute__((noreturn
));
90 static int woptions(struct curparse
*, char *);
92 static const int sec_prios
[] = {1, 4, 5, 8, 6, 3, 7, 2, 9};
93 static const char *progname
;
97 main(int argc
, char *argv
[])
100 struct mansearch search
;
101 struct manpaths paths
;
102 char *conf_file
, *defpaths
, *auxpaths
;
112 enum outmode outmode
;
117 progname
= strrchr(argv
[0], '/');
118 if (progname
== NULL
)
123 /* Search options. */
125 memset(&paths
, 0, sizeof(struct manpaths
));
126 conf_file
= defpaths
= auxpaths
= NULL
;
128 memset(&search
, 0, sizeof(struct mansearch
));
129 search
.outkey
= "Nd";
131 if (strcmp(progname
, "man") == 0)
132 search
.argmode
= ARG_NAME
;
133 else if (strncmp(progname
, "apropos", 7) == 0)
134 search
.argmode
= ARG_EXPR
;
135 else if (strncmp(progname
, "whatis", 6) == 0)
136 search
.argmode
= ARG_WORD
;
138 search
.argmode
= ARG_FILE
;
140 /* Parser and formatter options. */
142 memset(&curp
, 0, sizeof(struct curparse
));
143 curp
.outtype
= OUTT_ASCII
;
144 curp
.wlevel
= MANDOCLEVEL_FATAL
;
149 outmode
= OUTMODE_DEF
;
150 while (-1 != (c
= getopt(argc
, argv
, "aC:fI:ikM:m:O:S:s:T:VW:w"))) {
153 outmode
= OUTMODE_ALL
;
159 search
.argmode
= ARG_WORD
;
162 if (strncmp(optarg
, "os=", 3)) {
164 "%s: -I%s: Bad argument\n",
166 return((int)MANDOCLEVEL_BADARG
);
170 "%s: -I%s: Duplicate argument\n",
172 return((int)MANDOCLEVEL_BADARG
);
174 defos
= mandoc_strdup(optarg
+ 3);
177 outmode
= OUTMODE_INT
;
180 search
.argmode
= ARG_EXPR
;
189 search
.outkey
= optarg
;
190 (void)strlcat(curp
.outopts
, optarg
, BUFSIZ
);
191 (void)strlcat(curp
.outopts
, ",", BUFSIZ
);
194 search
.arch
= optarg
;
200 if ( ! toptions(&curp
, optarg
))
201 return((int)MANDOCLEVEL_BADARG
);
204 if ( ! woptions(&curp
, optarg
))
205 return((int)MANDOCLEVEL_BADARG
);
208 outmode
= OUTMODE_FLN
;
220 usage(search
.argmode
);
222 if (outmode
== OUTMODE_DEF
) {
223 switch (search
.argmode
) {
225 outmode
= OUTMODE_ALL
;
228 outmode
= OUTMODE_ONE
;
231 outmode
= OUTMODE_LST
;
244 /* man(1), whatis(1), apropos(1) */
246 if (search
.argmode
!= ARG_FILE
) {
249 usage(search
.argmode
);
251 /* Access the mandoc database. */
253 manpath_parse(&paths
, conf_file
, defpaths
, auxpaths
);
255 if( ! mansearch(&search
, &paths
, argc
, argv
, &res
, &sz
))
256 usage(search
.argmode
);
257 manpath_free(&paths
);
260 * For standard man(1) and -a output mode,
261 * prepare for copying filename pointers
262 * into the program parameter array.
265 if (outmode
== OUTMODE_ONE
) {
267 argv
[0] = res
[0].file
;
270 } else if (outmode
== OUTMODE_ALL
) {
272 argv
= auxargv
= mandoc_reallocarray(
273 NULL
, sz
+ 1, sizeof(char *));
277 /* Iterate all matching manuals. */
279 for (i
= 0; i
< sz
; i
++) {
280 if (outmode
== OUTMODE_FLN
)
282 else if (outmode
== OUTMODE_LST
)
283 printf("%s - %s\n", res
[i
].names
,
284 res
[i
].output
== NULL
? "" :
286 else if (outmode
== OUTMODE_ALL
)
287 argv
[i
] = res
[i
].file
;
289 /* Search for the best section. */
290 isec
= strcspn(res
[i
].file
, "123456789");
291 sec
= res
[i
].file
[isec
];
294 prio
= sec_prios
[sec
- '1'];
295 if (prio
>= best_prio
)
298 argv
[0] = res
[i
].file
;
303 * For man(1), -a and -i output mode, fall through
304 * to the main mandoc(1) code iterating files
305 * and running the parsers on each of them.
308 if (outmode
== OUTMODE_FLN
|| outmode
== OUTMODE_LST
)
311 fputs("mandoc: database support not compiled in\n",
313 return((int)MANDOCLEVEL_BADARG
);
319 if ( ! moptions(&options
, auxpaths
))
320 return((int)MANDOCLEVEL_BADARG
);
322 curp
.mp
= mparse_alloc(options
, curp
.wlevel
, mmsg
, defos
);
325 * Conditionally start up the lookaside buffer before parsing.
327 if (OUTT_MAN
== curp
.outtype
)
328 mparse_keep(curp
.mp
);
331 parse(&curp
, STDIN_FILENO
, "<stdin>", &rc
);
334 parse(&curp
, -1, *argv
, &rc
);
335 if (MANDOCLEVEL_OK
!= rc
&& curp
.wstop
)
341 (*curp
.outfree
)(curp
.outdata
);
343 mparse_free(curp
.mp
);
347 if (search
.argmode
!= ARG_FILE
) {
348 mansearch_free(res
, sz
);
363 printf("mandoc %s\n", VERSION
);
364 exit((int)MANDOCLEVEL_OK
);
368 usage(enum argmode argmode
)
373 fputs("usage: mandoc [-V] [-Ios=name] [-mformat]"
374 " [-Ooption] [-Toutput] [-Wlevel]\n"
375 "\t [file ...]\n", stderr
);
378 fputs("usage: man [-acfhkVw] [-C file] "
379 "[-M path] [-m path] [-S arch] [-s section]\n"
380 "\t [section] name ...\n", stderr
);
383 fputs("usage: whatis [-V] [-C file] [-M path] [-m path] "
384 "[-S arch] [-s section] name ...\n", stderr
);
387 fputs("usage: apropos [-V] [-C file] [-M path] [-m path] "
388 "[-O outkey] [-S arch]\n"
389 "\t [-s section] expression ...\n", stderr
);
392 exit((int)MANDOCLEVEL_BADARG
);
396 parse(struct curparse
*curp
, int fd
, const char *file
,
397 enum mandoclevel
*level
)
403 /* Begin by parsing the file itself. */
408 rc
= mparse_readfd(curp
->mp
, fd
, file
);
410 /* Stop immediately if the parse has failed. */
412 if (MANDOCLEVEL_FATAL
<= rc
)
416 * With -Wstop and warnings or errors of at least the requested
417 * level, do not produce output.
420 if (MANDOCLEVEL_OK
!= rc
&& curp
->wstop
)
423 /* If unset, allocate output dev now (if applicable). */
425 if ( ! (curp
->outman
&& curp
->outmdoc
)) {
426 switch (curp
->outtype
) {
428 curp
->outdata
= xhtml_alloc(curp
->outopts
);
429 curp
->outfree
= html_free
;
432 curp
->outdata
= html_alloc(curp
->outopts
);
433 curp
->outfree
= html_free
;
436 curp
->outdata
= utf8_alloc(curp
->outopts
);
437 curp
->outfree
= ascii_free
;
440 curp
->outdata
= locale_alloc(curp
->outopts
);
441 curp
->outfree
= ascii_free
;
444 curp
->outdata
= ascii_alloc(curp
->outopts
);
445 curp
->outfree
= ascii_free
;
448 curp
->outdata
= pdf_alloc(curp
->outopts
);
449 curp
->outfree
= pspdf_free
;
452 curp
->outdata
= ps_alloc(curp
->outopts
);
453 curp
->outfree
= pspdf_free
;
459 switch (curp
->outtype
) {
463 curp
->outman
= html_man
;
464 curp
->outmdoc
= html_mdoc
;
467 curp
->outman
= tree_man
;
468 curp
->outmdoc
= tree_mdoc
;
471 curp
->outmdoc
= man_mdoc
;
472 curp
->outman
= man_man
;
483 curp
->outman
= terminal_man
;
484 curp
->outmdoc
= terminal_mdoc
;
491 mparse_result(curp
->mp
, &mdoc
, &man
, NULL
);
493 /* Execute the out device, if it exists. */
495 if (man
&& curp
->outman
)
496 (*curp
->outman
)(curp
->outdata
, man
);
497 if (mdoc
&& curp
->outmdoc
)
498 (*curp
->outmdoc
)(curp
->outdata
, mdoc
);
502 mparse_reset(curp
->mp
);
509 moptions(int *options
, char *arg
)
514 else if (0 == strcmp(arg
, "doc"))
515 *options
|= MPARSE_MDOC
;
516 else if (0 == strcmp(arg
, "andoc"))
518 else if (0 == strcmp(arg
, "an"))
519 *options
|= MPARSE_MAN
;
521 fprintf(stderr
, "%s: -m%s: Bad argument\n",
530 toptions(struct curparse
*curp
, char *arg
)
533 if (0 == strcmp(arg
, "ascii"))
534 curp
->outtype
= OUTT_ASCII
;
535 else if (0 == strcmp(arg
, "lint")) {
536 curp
->outtype
= OUTT_LINT
;
537 curp
->wlevel
= MANDOCLEVEL_WARNING
;
538 } else if (0 == strcmp(arg
, "tree"))
539 curp
->outtype
= OUTT_TREE
;
540 else if (0 == strcmp(arg
, "man"))
541 curp
->outtype
= OUTT_MAN
;
542 else if (0 == strcmp(arg
, "html"))
543 curp
->outtype
= OUTT_HTML
;
544 else if (0 == strcmp(arg
, "utf8"))
545 curp
->outtype
= OUTT_UTF8
;
546 else if (0 == strcmp(arg
, "locale"))
547 curp
->outtype
= OUTT_LOCALE
;
548 else if (0 == strcmp(arg
, "xhtml"))
549 curp
->outtype
= OUTT_XHTML
;
550 else if (0 == strcmp(arg
, "ps"))
551 curp
->outtype
= OUTT_PS
;
552 else if (0 == strcmp(arg
, "pdf"))
553 curp
->outtype
= OUTT_PDF
;
555 fprintf(stderr
, "%s: -T%s: Bad argument\n",
564 woptions(struct curparse
*curp
, char *arg
)
578 switch (getsubopt(&arg
, UNCONST(toks
), &v
)) {
585 curp
->wlevel
= MANDOCLEVEL_WARNING
;
588 curp
->wlevel
= MANDOCLEVEL_ERROR
;
591 curp
->wlevel
= MANDOCLEVEL_FATAL
;
594 fprintf(stderr
, "%s: -W%s: Bad argument\n",
604 mmsg(enum mandocerr t
, enum mandoclevel lvl
,
605 const char *file
, int line
, int col
, const char *msg
)
607 const char *mparse_msg
;
609 fprintf(stderr
, "%s: %s:", progname
, file
);
612 fprintf(stderr
, "%d:%d:", line
, col
+ 1);
614 fprintf(stderr
, " %s", mparse_strlevel(lvl
));
616 if (NULL
!= (mparse_msg
= mparse_strerror(t
)))
617 fprintf(stderr
, ": %s", mparse_msg
);
620 fprintf(stderr
, ": %s", msg
);