version 1.6, 2009/03/20 21:29:29 |
version 1.18, 2009/03/31 13:50:19 |
|
|
#include <unistd.h> |
#include <unistd.h> |
|
|
#include "mdoc.h" |
#include "mdoc.h" |
|
#include "man.h" |
|
|
|
/* Account for FreeBSD and Linux in our declarations. */ |
|
|
#ifdef __linux__ |
#ifdef __linux__ |
extern int getsubopt(char **, char * const *, char **); |
extern int getsubopt(char **, char * const *, char **); |
# ifndef __dead |
# ifndef __dead |
# define __dead __attribute__((__noreturn__)) |
# define __dead __attribute__((__noreturn__)) |
# endif |
# endif |
|
#elif defined(__dead2) |
|
# ifndef __dead |
|
# define __dead __dead2 |
|
# endif |
#endif |
#endif |
|
|
struct buf { |
struct buf { |
Line 49 struct curparse { |
|
Line 56 struct curparse { |
|
#define WARN_WERR (1 << 2) /* Warnings->errors. */ |
#define WARN_WERR (1 << 2) /* Warnings->errors. */ |
}; |
}; |
|
|
enum outt { |
#define IGN_SCOPE (1 << 0) /* Ignore scope errors. */ |
OUTT_ASCII, |
#define IGN_ESCAPE (1 << 1) /* Ignore bad escapes. */ |
OUTT_LATIN1, |
#define IGN_MACRO (1 << 2) /* Ignore unknown macros. */ |
OUTT_UTF8, |
|
|
enum intt { |
|
INTT_MDOC = 0, |
|
INTT_MAN |
|
}; |
|
|
|
enum outt { |
|
OUTT_ASCII = 0, |
OUTT_TREE, |
OUTT_TREE, |
OUTT_LINT |
OUTT_LINT |
}; |
}; |
|
|
typedef int (*out_run)(void *, const struct mdoc *); |
typedef int (*out_run)(void *, const struct man *, |
|
const struct mdoc *); |
typedef void (*out_free)(void *); |
typedef void (*out_free)(void *); |
|
|
extern char *__progname; |
extern char *__progname; |
|
|
extern void *ascii_alloc(void); |
extern void *ascii_alloc(void); |
extern void *latin1_alloc(void); |
extern int terminal_run(void *, const struct man *, |
extern void *utf8_alloc(void); |
const struct mdoc *); |
extern int terminal_run(void *, const struct mdoc *); |
extern int tree_run(void *, const struct man *, |
extern int tree_run(void *, const struct mdoc *); |
const struct mdoc *); |
extern void terminal_free(void *); |
extern void terminal_free(void *); |
|
|
__dead static void version(void); |
|
__dead static void usage(void); |
|
static int foptions(int *, char *); |
static int foptions(int *, char *); |
static int toptions(enum outt *, char *); |
static int toptions(enum outt *, char *); |
|
static int moptions(enum intt *, char *); |
static int woptions(int *, char *); |
static int woptions(int *, char *); |
static int merr(void *, int, int, const char *); |
static int merr(void *, int, int, const char *); |
static int mwarn(void *, int, int, |
static int manwarn(void *, int, int, const char *); |
|
static int mdocwarn(void *, int, int, |
enum mdoc_warn, const char *); |
enum mdoc_warn, const char *); |
static int file(struct buf *, struct buf *, |
static int file(struct buf *, struct buf *, |
const char *, struct mdoc *); |
const char *, |
|
struct man *, struct mdoc *); |
static int fdesc(struct buf *, struct buf *, |
static int fdesc(struct buf *, struct buf *, |
const char *, int, struct mdoc *); |
const char *, int, |
|
struct man *, struct mdoc *); |
|
__dead static void version(void); |
|
__dead static void usage(void); |
|
|
|
|
int |
int |
main(int argc, char *argv[]) |
main(int argc, char *argv[]) |
{ |
{ |
int c, rc, fflags, wflags; |
int c, rc, fflags, pflags; |
struct mdoc_cb cb; |
struct mdoc_cb mdoccb; |
|
struct man_cb mancb; |
|
struct man *man; |
struct mdoc *mdoc; |
struct mdoc *mdoc; |
void *outdata; |
void *outdata; |
enum outt outtype; |
enum outt outtype; |
|
enum intt inttype; |
struct buf ln, blk; |
struct buf ln, blk; |
out_run outrun; |
out_run outrun; |
out_free outfree; |
out_free outfree; |
struct curparse curp; |
struct curparse curp; |
|
|
fflags = wflags = 0; |
fflags = 0; |
outtype = OUTT_ASCII; |
outtype = OUTT_ASCII; |
|
inttype = INTT_MDOC; |
|
|
bzero(&curp, sizeof(struct curparse)); |
bzero(&curp, sizeof(struct curparse)); |
|
|
/* LINTED */ |
/* LINTED */ |
while (-1 != (c = getopt(argc, argv, "f:VW:T:"))) |
while (-1 != (c = getopt(argc, argv, "f:m:VW:T:"))) |
switch (c) { |
switch (c) { |
case ('f'): |
case ('f'): |
if ( ! foptions(&fflags, optarg)) |
if ( ! foptions(&fflags, optarg)) |
return(0); |
return(0); |
break; |
break; |
|
case ('m'): |
|
if ( ! moptions(&inttype, optarg)) |
|
return(0); |
|
break; |
case ('T'): |
case ('T'): |
if ( ! toptions(&outtype, optarg)) |
if ( ! toptions(&outtype, optarg)) |
return(0); |
return(0); |
Line 128 main(int argc, char *argv[]) |
|
Line 155 main(int argc, char *argv[]) |
|
argv += optind; |
argv += optind; |
|
|
/* |
/* |
* Allocate the appropriate front-end. Note that utf8, ascii |
* Allocate the appropriate front-end. Note that utf8, latin1 |
* and latin1 all resolve to the terminal front-end with |
* (both not yet implemented) and ascii all resolve to the |
* different encodings (see terminal.c). Not all frontends have |
* terminal front-end with different encodings (see terminal.c). |
* cleanup or alloc routines. |
* Not all frontends have cleanup or alloc routines. |
*/ |
*/ |
|
|
switch (outtype) { |
switch (outtype) { |
case (OUTT_LATIN1): |
|
outdata = latin1_alloc(); |
|
outrun = terminal_run; |
|
outfree = terminal_free; |
|
break; |
|
case (OUTT_UTF8): |
|
outdata = utf8_alloc(); |
|
outrun = terminal_run; |
|
outfree = terminal_free; |
|
break; |
|
case (OUTT_TREE): |
case (OUTT_TREE): |
outdata = NULL; |
outdata = NULL; |
outrun = tree_run; |
outrun = tree_run; |
Line 167 main(int argc, char *argv[]) |
|
Line 184 main(int argc, char *argv[]) |
|
* screen. XXX - for now, no path for debugging messages. |
* screen. XXX - for now, no path for debugging messages. |
*/ |
*/ |
|
|
cb.mdoc_msg = NULL; |
mdoccb.mdoc_msg = NULL; |
cb.mdoc_err = merr; |
mdoccb.mdoc_err = merr; |
cb.mdoc_warn = mwarn; |
mdoccb.mdoc_warn = mdocwarn; |
|
|
|
mancb.man_err = merr; |
|
mancb.man_warn = manwarn; |
|
|
|
/* Configure buffers. */ |
|
|
bzero(&ln, sizeof(struct buf)); |
bzero(&ln, sizeof(struct buf)); |
bzero(&blk, sizeof(struct buf)); |
bzero(&blk, sizeof(struct buf)); |
|
|
mdoc = mdoc_alloc(&curp, fflags, &cb); |
man = NULL; |
|
mdoc = NULL; |
|
pflags = 0; |
|
|
/* |
/* |
* Loop around available files. |
* Allocate the parser. There are two kinds of parser: libman |
|
* and libmdoc. We must separately copy over the flags that |
|
* we'll use internally. |
*/ |
*/ |
|
|
|
switch (inttype) { |
|
case (INTT_MAN): |
|
if (fflags & IGN_MACRO) |
|
pflags |= MAN_IGN_MACRO; |
|
man = man_alloc(&curp, pflags, &mancb); |
|
break; |
|
default: |
|
if (fflags & IGN_SCOPE) |
|
pflags |= MDOC_IGN_SCOPE; |
|
if (fflags & IGN_ESCAPE) |
|
pflags |= MDOC_IGN_ESCAPE; |
|
if (fflags & IGN_MACRO) |
|
pflags |= MDOC_IGN_MACRO; |
|
mdoc = mdoc_alloc(&curp, pflags, &mdoccb); |
|
if (NULL == mdoc) |
|
errx(1, "memory exhausted"); |
|
break; |
|
} |
|
|
|
/* |
|
* Main loop around available files. |
|
*/ |
|
|
if (NULL == *argv) { |
if (NULL == *argv) { |
curp.file = "<stdin>"; |
curp.file = "<stdin>"; |
c = fdesc(&blk, &ln, "stdin", STDIN_FILENO, mdoc); |
|
rc = 0; |
rc = 0; |
|
c = fdesc(&blk, &ln, "stdin", STDIN_FILENO, man, mdoc); |
|
|
if (c && NULL == outrun) |
if (c && NULL == outrun) |
rc = 1; |
rc = 1; |
else if (c && outrun && (*outrun)(outdata, mdoc)) |
else if (c && outrun && (*outrun)(outdata, man, mdoc)) |
rc = 1; |
rc = 1; |
} else { |
} else { |
while (*argv) { |
while (*argv) { |
curp.file = *argv; |
curp.file = *argv; |
c = file(&blk, &ln, *argv, mdoc); |
c = file(&blk, &ln, *argv, man, mdoc); |
if ( ! c) |
if ( ! c) |
break; |
break; |
if (outrun && ! (*outrun)(outdata, mdoc)) |
if (outrun && ! (*outrun)(outdata, man, mdoc)) |
break; |
break; |
/* Reset the parser for another file. */ |
if (man) |
mdoc_reset(mdoc); |
man_reset(man); |
|
if (mdoc && ! mdoc_reset(mdoc)) { |
|
warnx("memory exhausted"); |
|
break; |
|
} |
argv++; |
argv++; |
} |
} |
rc = NULL == *argv; |
rc = NULL == *argv; |
Line 209 main(int argc, char *argv[]) |
|
Line 263 main(int argc, char *argv[]) |
|
free(ln.buf); |
free(ln.buf); |
if (outfree) |
if (outfree) |
(*outfree)(outdata); |
(*outfree)(outdata); |
|
if (mdoc) |
|
mdoc_free(mdoc); |
|
if (man) |
|
man_free(man); |
|
|
mdoc_free(mdoc); |
|
|
|
return(rc ? EXIT_SUCCESS : EXIT_FAILURE); |
return(rc ? EXIT_SUCCESS : EXIT_FAILURE); |
} |
} |
|
|
|
|
{ |
{ |
|
|
(void)printf("%s %s\n", __progname, VERSION); |
(void)printf("%s %s\n", __progname, VERSION); |
exit(0); |
exit(EXIT_SUCCESS); |
/* NOTREACHED */ |
|
} |
} |
|
|
|
|
Line 230 __dead static void |
|
Line 285 __dead static void |
|
usage(void) |
usage(void) |
{ |
{ |
|
|
(void)fprintf(stderr, "usage: %s\n", __progname); |
(void)fprintf(stderr, "usage: %s [-V] [-foption...] " |
exit(1); |
"[-mformat] [-Toutput] [-Werr...]\n", |
/* NOTREACHED */ |
__progname); |
|
exit(EXIT_FAILURE); |
} |
} |
|
|
|
|
static int |
static int |
file(struct buf *blk, struct buf *ln, |
file(struct buf *blk, struct buf *ln, const char *file, |
const char *file, struct mdoc *mdoc) |
struct man *man, struct mdoc *mdoc) |
{ |
{ |
int fd, c; |
int fd, c; |
|
|
Line 247 file(struct buf *blk, struct buf *ln, |
|
Line 303 file(struct buf *blk, struct buf *ln, |
|
return(0); |
return(0); |
} |
} |
|
|
c = fdesc(blk, ln, file, fd, mdoc); |
c = fdesc(blk, ln, file, fd, man, mdoc); |
|
|
if (-1 == close(fd)) |
if (-1 == close(fd)) |
warn("%s", file); |
warn("%s", file); |
Line 258 file(struct buf *blk, struct buf *ln, |
|
Line 314 file(struct buf *blk, struct buf *ln, |
|
|
|
static int |
static int |
fdesc(struct buf *blk, struct buf *ln, |
fdesc(struct buf *blk, struct buf *ln, |
const char *f, int fd, struct mdoc *mdoc) |
const char *f, int fd, |
|
struct man *man, struct mdoc *mdoc) |
{ |
{ |
size_t sz; |
size_t sz; |
ssize_t ssz; |
ssize_t ssz; |
struct stat st; |
struct stat st; |
int j, i, pos, lnn; |
int j, i, pos, lnn; |
|
|
|
assert( ! (man && mdoc)); |
|
|
/* |
/* |
* Two buffers: ln and buf. buf is the input buffer, optimised |
* Two buffers: ln and buf. buf is the input buffer, optimised |
* for each file's block size. ln is a line buffer. Both |
* for each file's block size. ln is a line buffer. Both |
Line 324 fdesc(struct buf *blk, struct buf *ln, |
|
Line 383 fdesc(struct buf *blk, struct buf *ln, |
|
} |
} |
|
|
ln->buf[pos] = 0; |
ln->buf[pos] = 0; |
if ( ! mdoc_parseln(mdoc, lnn, ln->buf)) |
if (mdoc && ! mdoc_parseln(mdoc, lnn, ln->buf)) |
return(0); |
return(0); |
|
if (man && ! man_parseln(man, lnn, ln->buf)) |
|
return(0); |
lnn++; |
lnn++; |
pos = 0; |
pos = 0; |
} |
} |
} |
} |
|
|
return(mdoc_endparse(mdoc)); |
if (mdoc) |
|
return(mdoc_endparse(mdoc)); |
|
|
|
return(man_endparse(man)); |
} |
} |
|
|
|
|
static int |
static int |
|
moptions(enum intt *tflags, char *arg) |
|
{ |
|
|
|
if (0 == strcmp(arg, "doc")) |
|
*tflags = INTT_MDOC; |
|
else if (0 == strcmp(arg, "an")) |
|
*tflags = INTT_MAN; |
|
else { |
|
warnx("bad argument: -m%s", arg); |
|
return(0); |
|
} |
|
|
|
return(1); |
|
} |
|
|
|
|
|
static int |
toptions(enum outt *tflags, char *arg) |
toptions(enum outt *tflags, char *arg) |
{ |
{ |
|
|
if (0 == strcmp(arg, "ascii")) |
if (0 == strcmp(arg, "ascii")) |
*tflags = OUTT_ASCII; |
*tflags = OUTT_ASCII; |
else if (0 == strcmp(arg, "latin1")) |
|
*tflags = OUTT_LATIN1; |
|
else if (0 == strcmp(arg, "utf8")) |
|
*tflags = OUTT_UTF8; |
|
else if (0 == strcmp(arg, "lint")) |
else if (0 == strcmp(arg, "lint")) |
*tflags = OUTT_LINT; |
*tflags = OUTT_LINT; |
else if (0 == strcmp(arg, "tree")) |
else if (0 == strcmp(arg, "tree")) |
Line 376 foptions(int *fflags, char *arg) |
|
Line 453 foptions(int *fflags, char *arg) |
|
while (*arg) |
while (*arg) |
switch (getsubopt(&arg, toks, &v)) { |
switch (getsubopt(&arg, toks, &v)) { |
case (0): |
case (0): |
*fflags |= MDOC_IGN_SCOPE; |
*fflags |= IGN_SCOPE; |
break; |
break; |
case (1): |
case (1): |
*fflags |= MDOC_IGN_ESCAPE; |
*fflags |= IGN_ESCAPE; |
break; |
break; |
case (2): |
case (2): |
*fflags |= MDOC_IGN_MACRO; |
*fflags |= IGN_MACRO; |
break; |
break; |
default: |
default: |
warnx("bad argument: -f%s", arg); |
warnx("bad argument: -f%s", arg); |
Line 447 merr(void *arg, int line, int col, const char *msg) |
|
Line 524 merr(void *arg, int line, int col, const char *msg) |
|
|
|
|
|
static int |
static int |
mwarn(void *arg, int line, int col, |
mdocwarn(void *arg, int line, int col, |
enum mdoc_warn type, const char *msg) |
enum mdoc_warn type, const char *msg) |
{ |
{ |
struct curparse *curp; |
struct curparse *curp; |
Line 482 mwarn(void *arg, int line, int col, |
|
Line 559 mwarn(void *arg, int line, int col, |
|
} |
} |
|
|
|
|
|
static int |
|
manwarn(void *arg, int line, int col, const char *msg) |
|
{ |
|
struct curparse *curp; |
|
|
|
curp = (struct curparse *)arg; |
|
|
|
if ( ! (curp->wflags & WARN_WSYNTAX)) |
|
return(1); |
|
|
|
warnx("%s:%d: syntax warning: %s (column %d)", |
|
curp->file, line, msg, col); |
|
|
|
if ( ! (curp->wflags & WARN_WERR)) |
|
return(1); |
|
|
|
warnx("%s: considering warnings as errors", |
|
__progname); |
|
return(0); |
|
} |