]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
1 /* $Id: main.c,v 1.180 2014/08/17 03:24:47 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) */
44 typedef void (*out_mdoc
)(void *, const struct mdoc
*);
45 typedef void (*out_man
)(void *, const struct man
*);
46 typedef void (*out_free
)(void *);
49 OUTT_ASCII
= 0, /* -Tascii */
50 OUTT_LOCALE
, /* -Tlocale */
51 OUTT_UTF8
, /* -Tutf8 */
52 OUTT_TREE
, /* -Ttree */
54 OUTT_HTML
, /* -Thtml */
55 OUTT_XHTML
, /* -Txhtml */
56 OUTT_LINT
, /* -Tlint */
63 enum mandoclevel wlevel
; /* ignore messages below this */
64 int wstop
; /* stop after a file with a warning */
65 enum outt outtype
; /* which output to use */
66 out_mdoc outmdoc
; /* mdoc output ptr */
67 out_man outman
; /* man output ptr */
68 out_free outfree
; /* free output ptr */
69 void *outdata
; /* data for output */
70 char outopts
[BUFSIZ
]; /* buf of output opts */
73 static int moptions(int *, char *);
74 static void mmsg(enum mandocerr
, enum mandoclevel
,
75 const char *, int, int, const char *);
76 static void parse(struct curparse
*, int,
77 const char *, enum mandoclevel
*);
78 static int toptions(struct curparse
*, char *);
79 static void usage(enum argmode
) __attribute__((noreturn
));
80 static void version(void) __attribute__((noreturn
));
81 static int woptions(struct curparse
*, char *);
83 static const char *progname
;
87 main(int argc
, char *argv
[])
90 struct mansearch search
;
91 struct manpaths paths
;
92 char *conf_file
, *defpaths
, *auxpaths
;
103 progname
= strrchr(argv
[0], '/');
104 if (progname
== NULL
)
109 /* Search options. */
111 memset(&paths
, 0, sizeof(struct manpaths
));
112 conf_file
= defpaths
= auxpaths
= NULL
;
114 memset(&search
, 0, sizeof(struct mansearch
));
115 search
.outkey
= "Nd";
117 if (strcmp(progname
, "man") == 0)
118 search
.argmode
= ARG_NAME
;
119 else if (strncmp(progname
, "apropos", 7) == 0)
120 search
.argmode
= ARG_EXPR
;
121 else if (strncmp(progname
, "whatis", 6) == 0)
122 search
.argmode
= ARG_WORD
;
124 search
.argmode
= ARG_FILE
;
126 /* Parser and formatter options. */
128 memset(&curp
, 0, sizeof(struct curparse
));
129 curp
.outtype
= OUTT_ASCII
;
130 curp
.wlevel
= MANDOCLEVEL_FATAL
;
135 while (-1 != (c
= getopt(argc
, argv
, "C:fI:kM:m:O:S:s:T:VW:"))) {
141 search
.argmode
= ARG_WORD
;
144 if (strncmp(optarg
, "os=", 3)) {
146 "%s: -I%s: Bad argument\n",
148 return((int)MANDOCLEVEL_BADARG
);
152 "%s: -I%s: Duplicate argument\n",
154 return((int)MANDOCLEVEL_BADARG
);
156 defos
= mandoc_strdup(optarg
+ 3);
159 search
.argmode
= ARG_EXPR
;
168 search
.outkey
= optarg
;
169 (void)strlcat(curp
.outopts
, optarg
, BUFSIZ
);
170 (void)strlcat(curp
.outopts
, ",", BUFSIZ
);
173 search
.arch
= optarg
;
179 if ( ! toptions(&curp
, optarg
))
180 return((int)MANDOCLEVEL_BADARG
);
183 if ( ! woptions(&curp
, optarg
))
184 return((int)MANDOCLEVEL_BADARG
);
196 usage(search
.argmode
);
201 /* man(1), whatis(1), apropos(1) */
203 if (search
.argmode
!= ARG_FILE
) {
206 usage(search
.argmode
);
207 manpath_parse(&paths
, conf_file
, defpaths
, auxpaths
);
209 if( ! mansearch(&search
, &paths
, argc
, argv
, &res
, &sz
))
210 usage(search
.argmode
);
211 manpath_free(&paths
);
212 for (i
= 0; i
< sz
; i
++)
213 printf("%s - %s\n", res
[i
].names
,
214 res
[i
].output
== NULL
? "" : res
[i
].output
);
215 mansearch_free(res
, sz
);
217 return((int)MANDOCLEVEL_OK
);
219 fputs("mandoc: database support not compiled in\n",
221 return((int)MANDOCLEVEL_BADARG
);
227 if ( ! moptions(&options
, auxpaths
))
228 return((int)MANDOCLEVEL_BADARG
);
230 curp
.mp
= mparse_alloc(options
, curp
.wlevel
, mmsg
, defos
);
233 * Conditionally start up the lookaside buffer before parsing.
235 if (OUTT_MAN
== curp
.outtype
)
236 mparse_keep(curp
.mp
);
241 parse(&curp
, STDIN_FILENO
, "<stdin>", &rc
);
244 parse(&curp
, -1, *argv
, &rc
);
245 if (MANDOCLEVEL_OK
!= rc
&& curp
.wstop
)
251 (*curp
.outfree
)(curp
.outdata
);
253 mparse_free(curp
.mp
);
263 printf("mandoc %s\n", VERSION
);
264 exit((int)MANDOCLEVEL_OK
);
268 usage(enum argmode argmode
)
273 fputs("usage: mandoc [-V] [-Ios=name] [-mformat]"
274 " [-Ooption] [-Toutput] [-Wlevel]\n"
275 "\t [file ...]\n", stderr
);
278 fputs("usage: man [-acfhkVw] [-C file] "
279 "[-M path] [-m path] [-S arch] [-s section]\n"
280 "\t [section] name ...\n", stderr
);
283 fputs("usage: whatis [-V] [-C file] [-M path] [-m path] "
284 "[-S arch] [-s section] name ...\n", stderr
);
287 fputs("usage: apropos [-V] [-C file] [-M path] [-m path] "
288 "[-O outkey] [-S arch]\n"
289 "\t [-s section] expression ...\n", stderr
);
292 exit((int)MANDOCLEVEL_BADARG
);
296 parse(struct curparse
*curp
, int fd
, const char *file
,
297 enum mandoclevel
*level
)
303 /* Begin by parsing the file itself. */
308 rc
= mparse_readfd(curp
->mp
, fd
, file
);
310 /* Stop immediately if the parse has failed. */
312 if (MANDOCLEVEL_FATAL
<= rc
)
316 * With -Wstop and warnings or errors of at least the requested
317 * level, do not produce output.
320 if (MANDOCLEVEL_OK
!= rc
&& curp
->wstop
)
323 /* If unset, allocate output dev now (if applicable). */
325 if ( ! (curp
->outman
&& curp
->outmdoc
)) {
326 switch (curp
->outtype
) {
328 curp
->outdata
= xhtml_alloc(curp
->outopts
);
329 curp
->outfree
= html_free
;
332 curp
->outdata
= html_alloc(curp
->outopts
);
333 curp
->outfree
= html_free
;
336 curp
->outdata
= utf8_alloc(curp
->outopts
);
337 curp
->outfree
= ascii_free
;
340 curp
->outdata
= locale_alloc(curp
->outopts
);
341 curp
->outfree
= ascii_free
;
344 curp
->outdata
= ascii_alloc(curp
->outopts
);
345 curp
->outfree
= ascii_free
;
348 curp
->outdata
= pdf_alloc(curp
->outopts
);
349 curp
->outfree
= pspdf_free
;
352 curp
->outdata
= ps_alloc(curp
->outopts
);
353 curp
->outfree
= pspdf_free
;
359 switch (curp
->outtype
) {
363 curp
->outman
= html_man
;
364 curp
->outmdoc
= html_mdoc
;
367 curp
->outman
= tree_man
;
368 curp
->outmdoc
= tree_mdoc
;
371 curp
->outmdoc
= man_mdoc
;
372 curp
->outman
= man_man
;
383 curp
->outman
= terminal_man
;
384 curp
->outmdoc
= terminal_mdoc
;
391 mparse_result(curp
->mp
, &mdoc
, &man
, NULL
);
393 /* Execute the out device, if it exists. */
395 if (man
&& curp
->outman
)
396 (*curp
->outman
)(curp
->outdata
, man
);
397 if (mdoc
&& curp
->outmdoc
)
398 (*curp
->outmdoc
)(curp
->outdata
, mdoc
);
402 mparse_reset(curp
->mp
);
409 moptions(int *options
, char *arg
)
414 else if (0 == strcmp(arg
, "doc"))
415 *options
|= MPARSE_MDOC
;
416 else if (0 == strcmp(arg
, "andoc"))
418 else if (0 == strcmp(arg
, "an"))
419 *options
|= MPARSE_MAN
;
421 fprintf(stderr
, "%s: -m%s: Bad argument\n",
430 toptions(struct curparse
*curp
, char *arg
)
433 if (0 == strcmp(arg
, "ascii"))
434 curp
->outtype
= OUTT_ASCII
;
435 else if (0 == strcmp(arg
, "lint")) {
436 curp
->outtype
= OUTT_LINT
;
437 curp
->wlevel
= MANDOCLEVEL_WARNING
;
438 } else if (0 == strcmp(arg
, "tree"))
439 curp
->outtype
= OUTT_TREE
;
440 else if (0 == strcmp(arg
, "man"))
441 curp
->outtype
= OUTT_MAN
;
442 else if (0 == strcmp(arg
, "html"))
443 curp
->outtype
= OUTT_HTML
;
444 else if (0 == strcmp(arg
, "utf8"))
445 curp
->outtype
= OUTT_UTF8
;
446 else if (0 == strcmp(arg
, "locale"))
447 curp
->outtype
= OUTT_LOCALE
;
448 else if (0 == strcmp(arg
, "xhtml"))
449 curp
->outtype
= OUTT_XHTML
;
450 else if (0 == strcmp(arg
, "ps"))
451 curp
->outtype
= OUTT_PS
;
452 else if (0 == strcmp(arg
, "pdf"))
453 curp
->outtype
= OUTT_PDF
;
455 fprintf(stderr
, "%s: -T%s: Bad argument\n",
464 woptions(struct curparse
*curp
, char *arg
)
478 switch (getsubopt(&arg
, UNCONST(toks
), &v
)) {
485 curp
->wlevel
= MANDOCLEVEL_WARNING
;
488 curp
->wlevel
= MANDOCLEVEL_ERROR
;
491 curp
->wlevel
= MANDOCLEVEL_FATAL
;
494 fprintf(stderr
, "%s: -W%s: Bad argument\n",
504 mmsg(enum mandocerr t
, enum mandoclevel lvl
,
505 const char *file
, int line
, int col
, const char *msg
)
507 const char *mparse_msg
;
509 fprintf(stderr
, "%s: %s:", progname
, file
);
512 fprintf(stderr
, "%d:%d:", line
, col
+ 1);
514 fprintf(stderr
, " %s", mparse_strlevel(lvl
));
516 if (NULL
!= (mparse_msg
= mparse_strerror(t
)))
517 fprintf(stderr
, ": %s", mparse_msg
);
520 fprintf(stderr
, ": %s", msg
);