version 1.150.2.7, 2017/02/03 18:22:30 |
version 1.175, 2017/06/11 17:16:51 |
|
|
#include "config.h" |
#include "config.h" |
|
|
#include <sys/types.h> |
#include <sys/types.h> |
#if HAVE_MMAP |
|
#include <sys/mman.h> |
#include <sys/mman.h> |
#include <sys/stat.h> |
#include <sys/stat.h> |
#endif |
|
|
|
#include <assert.h> |
#include <assert.h> |
#include <ctype.h> |
#include <ctype.h> |
|
|
#include <errno.h> |
#include <errno.h> |
#include <fcntl.h> |
#include <fcntl.h> |
#include <stdarg.h> |
#include <stdarg.h> |
#include <stdint.h> |
|
#include <stdio.h> |
#include <stdio.h> |
#include <stdlib.h> |
#include <stdlib.h> |
#include <string.h> |
#include <string.h> |
|
|
|
|
static void choose_parser(struct mparse *); |
static void choose_parser(struct mparse *); |
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, size_t, int); |
static int mparse_buf_r(struct mparse *, struct buf, size_t, int); |
static int read_whole_file(struct mparse *, const char *, int, |
static int read_whole_file(struct mparse *, const char *, int, |
struct buf *, int *); |
struct buf *, int *); |
static void mparse_end(struct mparse *); |
static void mparse_end(struct mparse *); |
Line 78 static void mparse_parse_buffer(struct mparse *, str |
|
Line 75 static void mparse_parse_buffer(struct mparse *, str |
|
|
|
static const enum mandocerr mandoclimits[MANDOCLEVEL_MAX] = { |
static const enum mandocerr mandoclimits[MANDOCLEVEL_MAX] = { |
MANDOCERR_OK, |
MANDOCERR_OK, |
|
MANDOCERR_STYLE, |
MANDOCERR_WARNING, |
MANDOCERR_WARNING, |
MANDOCERR_WARNING, |
|
MANDOCERR_ERROR, |
MANDOCERR_ERROR, |
MANDOCERR_UNSUPP, |
MANDOCERR_UNSUPP, |
MANDOCERR_MAX, |
MANDOCERR_MAX, |
Line 89 static const enum mandocerr mandoclimits[MANDOCLEVEL_M |
|
Line 86 static const enum mandocerr mandoclimits[MANDOCLEVEL_M |
|
static const char * const mandocerrs[MANDOCERR_MAX] = { |
static const char * const mandocerrs[MANDOCERR_MAX] = { |
"ok", |
"ok", |
|
|
|
"generic style suggestion", |
|
|
|
"useless macro", |
|
"consider using OS macro", |
|
"errnos out of order", |
|
"duplicate errno", |
|
"description line ends with a full stop", |
|
"no blank before trailing delimiter", |
|
"function name without markup", |
|
|
"generic warning", |
"generic warning", |
|
|
/* related to the prologue */ |
/* related to the prologue */ |
Line 116 static const char * const mandocerrs[MANDOCERR_MAX] = |
|
Line 123 static const char * const mandocerrs[MANDOCERR_MAX] = |
|
"bad NAME section content", |
"bad NAME section content", |
"missing comma before name", |
"missing comma before name", |
"missing description line, using \"\"", |
"missing description line, using \"\"", |
|
"description line outside NAME section", |
"sections out of conventional order", |
"sections out of conventional order", |
"duplicate section title", |
"duplicate section title", |
"unexpected section", |
"unexpected section", |
Line 135 static const char * const mandocerrs[MANDOCERR_MAX] = |
|
Line 143 static const char * const mandocerrs[MANDOCERR_MAX] = |
|
"fill mode already enabled, skipping", |
"fill mode already enabled, skipping", |
"fill mode already disabled, skipping", |
"fill mode already disabled, skipping", |
"line scope broken", |
"line scope broken", |
|
"skipping blank line in line scope", |
|
|
/* related to missing macro arguments */ |
/* related to missing macro arguments */ |
"skipping empty request", |
"skipping empty request", |
Line 170 static const char * const mandocerrs[MANDOCERR_MAX] = |
|
Line 179 static const char * const mandocerrs[MANDOCERR_MAX] = |
|
"unknown AT&T UNIX version", |
"unknown AT&T UNIX version", |
"comma in function argument", |
"comma in function argument", |
"parenthesis in function name", |
"parenthesis in function name", |
|
"unknown library name", |
"invalid content in Rs block", |
"invalid content in Rs block", |
"invalid Boolean argument", |
"invalid Boolean argument", |
"unknown font, skipping request", |
"unknown font, skipping request", |
Line 222 static const char * const mandocerrs[MANDOCERR_MAX] = |
|
Line 232 static const char * const mandocerrs[MANDOCERR_MAX] = |
|
"NOT IMPLEMENTED: Bd -file", |
"NOT IMPLEMENTED: Bd -file", |
"skipping display without arguments", |
"skipping display without arguments", |
"missing list type, using -item", |
"missing list type, using -item", |
|
"argument is not numeric, using 1", |
"missing manual name, using \"\"", |
"missing manual name, using \"\"", |
"uname(3) system call failed, using UNKNOWN", |
"uname(3) system call failed, using UNKNOWN", |
"unknown standard specifier", |
"unknown standard specifier", |
Line 243 static const char * const mandocerrs[MANDOCERR_MAX] = |
|
Line 254 static const char * const mandocerrs[MANDOCERR_MAX] = |
|
|
|
static const char * const mandoclevels[MANDOCLEVEL_MAX] = { |
static const char * const mandoclevels[MANDOCLEVEL_MAX] = { |
"SUCCESS", |
"SUCCESS", |
"RESERVED", |
"STYLE", |
"WARNING", |
"WARNING", |
"ERROR", |
"ERROR", |
"UNSUPP", |
"UNSUPP", |
Line 295 choose_parser(struct mparse *curp) |
|
Line 306 choose_parser(struct mparse *curp) |
|
} |
} |
|
|
if (format == MPARSE_MDOC) { |
if (format == MPARSE_MDOC) { |
mdoc_hash_init(); |
|
curp->man->macroset = MACROSET_MDOC; |
curp->man->macroset = MACROSET_MDOC; |
curp->man->first->tok = TOKEN_NONE; |
if (curp->man->mdocmac == NULL) |
|
curp->man->mdocmac = roffhash_alloc(MDOC_Dd, MDOC_MAX); |
} else { |
} else { |
man_hash_init(); |
|
curp->man->macroset = MACROSET_MAN; |
curp->man->macroset = MACROSET_MAN; |
curp->man->first->tok = TOKEN_NONE; |
if (curp->man->manmac == NULL) |
|
curp->man->manmac = roffhash_alloc(MAN_TH, MAN_MAX); |
} |
} |
|
curp->man->first->tok = TOKEN_NONE; |
} |
} |
|
|
/* |
/* |
Line 312 choose_parser(struct mparse *curp) |
|
Line 324 choose_parser(struct mparse *curp) |
|
* macros, inline equations, and input line traps) |
* macros, inline equations, and input line traps) |
* and indirectly (for .so file inclusion). |
* and indirectly (for .so file inclusion). |
*/ |
*/ |
static void |
static int |
mparse_buf_r(struct mparse *curp, struct buf blk, size_t i, int start) |
mparse_buf_r(struct mparse *curp, struct buf blk, size_t i, int start) |
{ |
{ |
const struct tbl_span *span; |
const struct tbl_span *span; |
Line 320 mparse_buf_r(struct mparse *curp, struct buf blk, size |
|
Line 332 mparse_buf_r(struct mparse *curp, struct buf blk, size |
|
const char *save_file; |
const char *save_file; |
char *cp; |
char *cp; |
size_t pos; /* byte number in the ln buffer */ |
size_t pos; /* byte number in the ln buffer */ |
size_t j; /* auxiliary byte number in the blk buffer */ |
|
enum rofferr rr; |
enum rofferr rr; |
int of; |
int of; |
int lnn; /* line number in the real file */ |
int lnn; /* line number in the real file */ |
Line 402 mparse_buf_r(struct mparse *curp, struct buf blk, size |
|
Line 413 mparse_buf_r(struct mparse *curp, struct buf blk, size |
|
continue; |
continue; |
} |
} |
|
|
/* Trailing backslash = a plain char. */ |
|
|
|
if (blk.buf[i] != '\\' || i + 1 == blk.sz) { |
|
ln.buf[pos++] = blk.buf[i++]; |
|
continue; |
|
} |
|
|
|
/* |
|
* Found escape and at least one other character. |
|
* When it's a newline character, skip it. |
|
* When there is a carriage return in between, |
|
* skip that one as well. |
|
*/ |
|
|
|
if ('\r' == blk.buf[i + 1] && i + 2 < blk.sz && |
|
'\n' == blk.buf[i + 2]) |
|
++i; |
|
if ('\n' == blk.buf[i + 1]) { |
|
i += 2; |
|
++lnn; |
|
continue; |
|
} |
|
|
|
if ('"' == blk.buf[i + 1] || '#' == blk.buf[i + 1]) { |
|
j = i; |
|
i += 2; |
|
/* Comment, skip to end of line */ |
|
for (; i < blk.sz; ++i) { |
|
if (blk.buf[i] != '\n') |
|
continue; |
|
if (blk.buf[i - 1] == ' ' || |
|
blk.buf[i - 1] == '\t') |
|
mandoc_msg( |
|
MANDOCERR_SPACE_EOL, |
|
curp, curp->line, |
|
pos + i-1 - j, NULL); |
|
++i; |
|
++lnn; |
|
break; |
|
} |
|
|
|
/* Backout trailing whitespaces */ |
|
for (; pos > 0; --pos) { |
|
if (ln.buf[pos - 1] != ' ') |
|
break; |
|
if (pos > 2 && ln.buf[pos - 2] == '\\') |
|
break; |
|
} |
|
break; |
|
} |
|
|
|
/* Catch escaped bogus characters. */ |
|
|
|
c = (unsigned char) blk.buf[i+1]; |
|
|
|
if ( ! (isascii(c) && |
|
(isgraph(c) || isblank(c)))) { |
|
mandoc_vmsg(MANDOCERR_CHAR_BAD, curp, |
|
curp->line, pos, "0x%x", c); |
|
i += 2; |
|
ln.buf[pos++] = '?'; |
|
continue; |
|
} |
|
|
|
/* Some other escape sequence, copy & cont. */ |
|
|
|
ln.buf[pos++] = blk.buf[i++]; |
ln.buf[pos++] = blk.buf[i++]; |
ln.buf[pos++] = blk.buf[i++]; |
|
} |
} |
|
|
if (pos >= ln.sz) |
if (pos + 1 >= ln.sz) |
resize_buf(&ln, 256); |
resize_buf(&ln, 256); |
|
|
|
if (i == blk.sz || blk.buf[i] == '\0') |
|
ln.buf[pos++] = '\n'; |
ln.buf[pos] = '\0'; |
ln.buf[pos] = '\0'; |
|
|
/* |
/* |
|
|
|
|
switch (rr) { |
switch (rr) { |
case ROFF_REPARSE: |
case ROFF_REPARSE: |
if (REPARSE_LIMIT >= ++curp->reparse_count) |
if (++curp->reparse_count > REPARSE_LIMIT) |
mparse_buf_r(curp, ln, of, 0); |
|
else |
|
mandoc_msg(MANDOCERR_ROFFLOOP, curp, |
mandoc_msg(MANDOCERR_ROFFLOOP, curp, |
curp->line, pos, NULL); |
curp->line, pos, NULL); |
pos = 0; |
else if (mparse_buf_r(curp, ln, of, 0) == 1 || |
continue; |
start == 1) { |
|
pos = 0; |
|
continue; |
|
} |
|
free(ln.buf); |
|
return 0; |
case ROFF_APPEND: |
case ROFF_APPEND: |
pos = strlen(ln.buf); |
pos = strlen(ln.buf); |
continue; |
continue; |
|
|
(i >= blk.sz || blk.buf[i] == '\0')) { |
(i >= blk.sz || blk.buf[i] == '\0')) { |
curp->sodest = mandoc_strdup(ln.buf + of); |
curp->sodest = mandoc_strdup(ln.buf + of); |
free(ln.buf); |
free(ln.buf); |
return; |
return 1; |
} |
} |
/* |
/* |
* We remove `so' clauses from our lookaside |
* We remove `so' clauses from our lookaside |
|
|
} |
} |
|
|
free(ln.buf); |
free(ln.buf); |
|
return 1; |
} |
} |
|
|
static int |
static int |
read_whole_file(struct mparse *curp, const char *file, int fd, |
read_whole_file(struct mparse *curp, const char *file, int fd, |
struct buf *fb, int *with_mmap) |
struct buf *fb, int *with_mmap) |
{ |
{ |
|
struct stat st; |
gzFile gz; |
gzFile gz; |
size_t off; |
size_t off; |
ssize_t ssz; |
ssize_t ssz; |
|
|
#if HAVE_MMAP |
|
struct stat st; |
|
|
|
if (fstat(fd, &st) == -1) |
if (fstat(fd, &st) == -1) |
err((int)MANDOCLEVEL_SYSERR, "%s", file); |
err((int)MANDOCLEVEL_SYSERR, "%s", file); |
|
|
Line 633 read_whole_file(struct mparse *curp, const char *file, |
|
Line 581 read_whole_file(struct mparse *curp, const char *file, |
|
if (fb->buf != MAP_FAILED) |
if (fb->buf != MAP_FAILED) |
return 1; |
return 1; |
} |
} |
#endif |
|
|
|
if (curp->gzip) { |
if (curp->gzip) { |
if ((gz = gzdopen(fd, "rb")) == NULL) |
if ((gz = gzdopen(fd, "rb")) == NULL) |
Line 758 mparse_readfd(struct mparse *curp, int fd, const char |
|
Line 705 mparse_readfd(struct mparse *curp, int fd, const char |
|
(MPARSE_UTF8 | MPARSE_LATIN1); |
(MPARSE_UTF8 | MPARSE_LATIN1); |
mparse_parse_buffer(curp, blk, file); |
mparse_parse_buffer(curp, blk, file); |
curp->filenc = save_filenc; |
curp->filenc = save_filenc; |
#if HAVE_MMAP |
|
if (with_mmap) |
if (with_mmap) |
munmap(blk.buf, blk.sz); |
munmap(blk.buf, blk.sz); |
else |
else |
#endif |
|
free(blk.buf); |
free(blk.buf); |
} |
} |
return curp->file_status; |
return curp->file_status; |
Line 821 mparse_alloc(int options, enum mandoclevel wlevel, man |
|
Line 766 mparse_alloc(int options, enum mandoclevel wlevel, man |
|
curp->man = roff_man_alloc( curp->roff, curp, curp->defos, |
curp->man = roff_man_alloc( curp->roff, curp, curp->defos, |
curp->options & MPARSE_QUICK ? 1 : 0); |
curp->options & MPARSE_QUICK ? 1 : 0); |
if (curp->options & MPARSE_MDOC) { |
if (curp->options & MPARSE_MDOC) { |
mdoc_hash_init(); |
|
curp->man->macroset = MACROSET_MDOC; |
curp->man->macroset = MACROSET_MDOC; |
|
if (curp->man->mdocmac == NULL) |
|
curp->man->mdocmac = roffhash_alloc(MDOC_Dd, MDOC_MAX); |
} else if (curp->options & MPARSE_MAN) { |
} else if (curp->options & MPARSE_MAN) { |
man_hash_init(); |
|
curp->man->macroset = MACROSET_MAN; |
curp->man->macroset = MACROSET_MAN; |
|
if (curp->man->manmac == NULL) |
|
curp->man->manmac = roffhash_alloc(MAN_TH, MAN_MAX); |
} |
} |
curp->man->first->tok = TOKEN_NONE; |
curp->man->first->tok = TOKEN_NONE; |
return curp; |
return curp; |
|
|
mparse_free(struct mparse *curp) |
mparse_free(struct mparse *curp) |
{ |
{ |
|
|
|
roffhash_free(curp->man->mdocmac); |
|
roffhash_free(curp->man->manmac); |
roff_man_free(curp->man); |
roff_man_free(curp->man); |
roff_free(curp->roff); |
roff_free(curp->roff); |
if (curp->secondary) |
if (curp->secondary) |