version 1.14, 2013/06/01 22:57:30 |
version 1.15, 2013/06/02 03:35:21 |
|
|
static void resize_buf(struct buf *, size_t); |
static void resize_buf(struct buf *, size_t); |
static void mparse_buf_r(struct mparse *, struct buf, int); |
static void mparse_buf_r(struct mparse *, struct buf, int); |
static void pset(const char *, int, struct mparse *); |
static void pset(const char *, int, struct mparse *); |
static void pdesc(struct mparse *, const char *, int); |
|
static int read_whole_file(const char *, int, struct buf *, int *); |
static int read_whole_file(const char *, int, struct buf *, int *); |
static void mparse_end(struct mparse *); |
static void mparse_end(struct mparse *); |
|
static void mparse_parse_buffer(struct mparse *, struct buf, |
|
const char *); |
|
|
static const enum mandocerr mandoclimits[MANDOCLEVEL_MAX] = { |
static const enum mandocerr mandoclimits[MANDOCLEVEL_MAX] = { |
MANDOCERR_OK, |
MANDOCERR_OK, |
|
|
free(ln.buf); |
free(ln.buf); |
} |
} |
|
|
static void |
|
pdesc(struct mparse *curp, const char *file, int fd) |
|
{ |
|
struct buf blk; |
|
int with_mmap; |
|
|
|
/* |
|
* Run for each opened file; may be called more than once for |
|
* each full parse sequence if the opened file is nested (i.e., |
|
* from `so'). Simply sucks in the whole file and moves into |
|
* the parse phase for the file. |
|
*/ |
|
|
|
if ( ! read_whole_file(file, fd, &blk, &with_mmap)) { |
|
curp->file_status = MANDOCLEVEL_SYSERR; |
|
return; |
|
} |
|
|
|
/* Line number is per-file. */ |
|
|
|
curp->line = 1; |
|
|
|
mparse_buf_r(curp, blk, 1); |
|
|
|
if (with_mmap) |
|
munmap(blk.buf, blk.sz); |
|
else |
|
free(blk.buf); |
|
} |
|
|
|
static int |
static int |
read_whole_file(const char *file, int fd, struct buf *fb, int *with_mmap) |
read_whole_file(const char *file, int fd, struct buf *fb, int *with_mmap) |
{ |
{ |
|
|
} |
} |
*with_mmap = 1; |
*with_mmap = 1; |
fb->sz = (size_t)st.st_size; |
fb->sz = (size_t)st.st_size; |
fb->buf = mmap(NULL, fb->sz, PROT_READ, |
fb->buf = mmap(NULL, fb->sz, PROT_READ, MAP_SHARED, fd, 0); |
MAP_FILE|MAP_SHARED, fd, 0); |
|
if (fb->buf != MAP_FAILED) |
if (fb->buf != MAP_FAILED) |
return(1); |
return(1); |
} |
} |
|
|
roff_endparse(curp->roff); |
roff_endparse(curp->roff); |
} |
} |
|
|
enum mandoclevel |
static void |
mparse_readfd(struct mparse *curp, int fd, const char *file) |
mparse_parse_buffer(struct mparse *curp, struct buf blk, const char *file) |
{ |
{ |
const char *svfile; |
const char *svfile; |
static int recursion_depth; |
static int recursion_depth; |
|
|
if (64 < recursion_depth) { |
if (64 < recursion_depth) { |
mandoc_msg(MANDOCERR_ROFFLOOP, curp, curp->line, 0, NULL); |
mandoc_msg(MANDOCERR_ROFFLOOP, curp, curp->line, 0, NULL); |
goto out; |
return; |
} |
} |
|
|
|
/* Line number is per-file. */ |
|
svfile = curp->file; |
|
curp->file = file; |
|
curp->line = 1; |
|
recursion_depth++; |
|
|
|
mparse_buf_r(curp, blk, 1); |
|
|
|
if (0 == --recursion_depth && MANDOCLEVEL_FATAL > curp->file_status) |
|
mparse_end(curp); |
|
|
|
curp->file = svfile; |
|
} |
|
|
|
enum mandoclevel |
|
mparse_readfd(struct mparse *curp, int fd, const char *file) |
|
{ |
|
struct buf blk; |
|
int with_mmap; |
|
|
if (-1 == fd) |
if (-1 == fd) |
if (-1 == (fd = open(file, O_RDONLY, 0))) { |
if (-1 == (fd = open(file, O_RDONLY, 0))) { |
perror(file); |
perror(file); |
curp->file_status = MANDOCLEVEL_SYSERR; |
curp->file_status = MANDOCLEVEL_SYSERR; |
goto out; |
goto out; |
} |
} |
|
/* |
|
* Run for each opened file; may be called more than once for |
|
* each full parse sequence if the opened file is nested (i.e., |
|
* from `so'). Simply sucks in the whole file and moves into |
|
* the parse phase for the file. |
|
*/ |
|
|
svfile = curp->file; |
if ( ! read_whole_file(file, fd, &blk, &with_mmap)) { |
curp->file = file; |
curp->file_status = MANDOCLEVEL_SYSERR; |
recursion_depth++; |
goto out; |
|
} |
|
|
pdesc(curp, file, fd); |
mparse_parse_buffer(curp, blk, file); |
|
|
if (0 == --recursion_depth && MANDOCLEVEL_FATAL > curp->file_status) |
if (with_mmap) |
mparse_end(curp); |
munmap(blk.buf, blk.sz); |
|
else |
|
free(blk.buf); |
|
|
if (STDIN_FILENO != fd && -1 == close(fd)) |
if (STDIN_FILENO != fd && -1 == close(fd)) |
perror(file); |
perror(file); |
|
|
curp->file = svfile; |
|
out: |
out: |
return(curp->file_status); |
return(curp->file_status); |
} |
} |