version 1.46, 2014/03/23 11:25:26 |
version 1.51, 2014/06/21 22:24:01 |
|
|
#define REPARSE_LIMIT 1000 |
#define REPARSE_LIMIT 1000 |
|
|
struct buf { |
struct buf { |
char *buf; /* binary input buffer */ |
char *buf; /* binary input buffer */ |
size_t sz; /* size of binary buffer */ |
size_t sz; /* size of binary buffer */ |
}; |
}; |
|
|
|
|
char *sodest; /* filename pointed to by .so */ |
char *sodest; /* filename pointed to by .so */ |
int reparse_count; /* finite interp. stack */ |
int reparse_count; /* finite interp. stack */ |
mandocmsg mmsg; /* warning/error message handler */ |
mandocmsg mmsg; /* warning/error message handler */ |
const char *file; |
const char *file; |
struct buf *secondary; |
struct buf *secondary; |
char *defos; /* default operating system */ |
char *defos; /* default operating system */ |
}; |
}; |
Line 93 static const char * const mandocerrs[MANDOCERR_MAX] = |
|
Line 93 static const char * const mandocerrs[MANDOCERR_MAX] = |
|
"generic warning", |
"generic warning", |
|
|
/* related to the prologue */ |
/* related to the prologue */ |
"no title in document", |
"no TH macro in document", |
"document title should be all caps", |
"document title should be all caps", |
"unknown manual section", |
"unknown manual section", |
"unknown manual volume or arch", |
"unknown manual volume or arch", |
Line 106 static const char * const mandocerrs[MANDOCERR_MAX] = |
|
Line 106 static const char * const mandocerrs[MANDOCERR_MAX] = |
|
|
|
/* related to document structure */ |
/* related to document structure */ |
".so is fragile, better use ln(1)", |
".so is fragile, better use ln(1)", |
|
"no document body", |
|
"content before the first section header", |
"NAME section must come first", |
"NAME section must come first", |
"bad NAME section contents", |
"bad NAME section contents", |
"sections out of conventional order", |
"sections out of conventional order", |
Line 153 static const char * const mandocerrs[MANDOCERR_MAX] = |
|
Line 155 static const char * const mandocerrs[MANDOCERR_MAX] = |
|
|
|
/* related to equations */ |
/* related to equations */ |
"unexpected literal in equation", |
"unexpected literal in equation", |
|
|
"generic error", |
"generic error", |
|
|
/* related to equations */ |
/* related to equations */ |
Line 204 static const char * const mandocerrs[MANDOCERR_MAX] = |
|
Line 206 static const char * const mandocerrs[MANDOCERR_MAX] = |
|
"child violates parent syntax", |
"child violates parent syntax", |
"argument count wrong, violates syntax", |
"argument count wrong, violates syntax", |
"NOT IMPLEMENTED: .so with absolute path or \"..\"", |
"NOT IMPLEMENTED: .so with absolute path or \"..\"", |
"no document body", |
|
"no document prologue", |
"no document prologue", |
"static buffer exhausted", |
"static buffer exhausted", |
|
|
/* system errors */ |
/* system errors */ |
"cannot open file", |
NULL, |
"cannot stat file", |
"cannot stat file", |
"cannot read file", |
"cannot read file", |
}; |
}; |
Line 224 static const char * const mandoclevels[MANDOCLEVEL_MAX |
|
Line 225 static const char * const mandoclevels[MANDOCLEVEL_MAX |
|
"SYSERR" |
"SYSERR" |
}; |
}; |
|
|
|
|
static void |
static void |
resize_buf(struct buf *buf, size_t initial) |
resize_buf(struct buf *buf, size_t initial) |
{ |
{ |
Line 257 pset(const char *buf, int pos, struct mparse *curp) |
|
Line 259 pset(const char *buf, int pos, struct mparse *curp) |
|
} |
} |
|
|
if (MPARSE_MDOC & curp->options) { |
if (MPARSE_MDOC & curp->options) { |
if (NULL == curp->pmdoc) |
if (NULL == curp->pmdoc) |
curp->pmdoc = mdoc_alloc( |
curp->pmdoc = mdoc_alloc( |
curp->roff, curp, curp->defos, |
curp->roff, curp, curp->defos, |
MPARSE_QUICK & curp->options ? 1 : 0); |
MPARSE_QUICK & curp->options ? 1 : 0); |
Line 265 pset(const char *buf, int pos, struct mparse *curp) |
|
Line 267 pset(const char *buf, int pos, struct mparse *curp) |
|
curp->mdoc = curp->pmdoc; |
curp->mdoc = curp->pmdoc; |
return; |
return; |
} else if (MPARSE_MAN & curp->options) { |
} else if (MPARSE_MAN & curp->options) { |
if (NULL == curp->pman) |
if (NULL == curp->pman) |
curp->pman = man_alloc(curp->roff, curp, |
curp->pman = man_alloc(curp->roff, curp, |
MPARSE_QUICK & curp->options ? 1 : 0); |
MPARSE_QUICK & curp->options ? 1 : 0); |
assert(curp->pman); |
assert(curp->pman); |
Line 274 pset(const char *buf, int pos, struct mparse *curp) |
|
Line 276 pset(const char *buf, int pos, struct mparse *curp) |
|
} |
} |
|
|
if (pos >= 3 && 0 == memcmp(buf, ".Dd", 3)) { |
if (pos >= 3 && 0 == memcmp(buf, ".Dd", 3)) { |
if (NULL == curp->pmdoc) |
if (NULL == curp->pmdoc) |
curp->pmdoc = mdoc_alloc( |
curp->pmdoc = mdoc_alloc( |
curp->roff, curp, curp->defos, |
curp->roff, curp, curp->defos, |
MPARSE_QUICK & curp->options ? 1 : 0); |
MPARSE_QUICK & curp->options ? 1 : 0); |
assert(curp->pmdoc); |
assert(curp->pmdoc); |
curp->mdoc = curp->pmdoc; |
curp->mdoc = curp->pmdoc; |
return; |
return; |
} |
} |
|
|
if (NULL == curp->pman) |
if (NULL == curp->pman) |
curp->pman = man_alloc(curp->roff, curp, |
curp->pman = man_alloc(curp->roff, curp, |
MPARSE_QUICK & curp->options ? 1 : 0); |
MPARSE_QUICK & curp->options ? 1 : 0); |
assert(curp->pman); |
assert(curp->pman); |
Line 308 mparse_buf_r(struct mparse *curp, struct buf blk, int |
|
Line 310 mparse_buf_r(struct mparse *curp, struct buf blk, int |
|
|
|
memset(&ln, 0, sizeof(struct buf)); |
memset(&ln, 0, sizeof(struct buf)); |
|
|
lnn = curp->line; |
lnn = curp->line; |
pos = 0; |
pos = 0; |
|
|
for (i = 0; i < (int)blk.sz; ) { |
for (i = 0; i < (int)blk.sz; ) { |
if (0 == pos && '\0' == blk.buf[i]) |
if (0 == pos && '\0' == blk.buf[i]) |
Line 346 mparse_buf_r(struct mparse *curp, struct buf blk, int |
|
Line 348 mparse_buf_r(struct mparse *curp, struct buf blk, int |
|
if (pos + 2 >= (int)ln.sz) |
if (pos + 2 >= (int)ln.sz) |
resize_buf(&ln, 256); |
resize_buf(&ln, 256); |
|
|
/* |
/* |
* Warn about bogus characters. If you're using |
* Warn about bogus characters. If you're using |
* non-ASCII encoding, you're screwing your |
* non-ASCII encoding, you're screwing your |
* readers. Since I'd rather this not happen, |
* readers. Since I'd rather this not happen, |
Line 357 mparse_buf_r(struct mparse *curp, struct buf blk, int |
|
Line 359 mparse_buf_r(struct mparse *curp, struct buf blk, int |
|
|
|
c = (unsigned char) blk.buf[i]; |
c = (unsigned char) blk.buf[i]; |
|
|
if ( ! (isascii(c) && |
if ( ! (isascii(c) && |
(isgraph(c) || isblank(c)))) { |
(isgraph(c) || isblank(c)))) { |
mandoc_msg(MANDOCERR_BADCHAR, curp, |
mandoc_msg(MANDOCERR_BADCHAR, curp, |
curp->line, pos, NULL); |
curp->line, pos, NULL); |
i++; |
i++; |
ln.buf[pos++] = '?'; |
ln.buf[pos++] = '?'; |
continue; |
continue; |
Line 414 mparse_buf_r(struct mparse *curp, struct buf blk, int |
|
Line 416 mparse_buf_r(struct mparse *curp, struct buf blk, int |
|
|
|
c = (unsigned char) blk.buf[i+1]; |
c = (unsigned char) blk.buf[i+1]; |
|
|
if ( ! (isascii(c) && |
if ( ! (isascii(c) && |
(isgraph(c) || isblank(c)))) { |
(isgraph(c) || isblank(c)))) { |
mandoc_msg(MANDOCERR_BADCHAR, curp, |
mandoc_msg(MANDOCERR_BADCHAR, curp, |
curp->line, pos, NULL); |
curp->line, pos, NULL); |
i += 2; |
i += 2; |
ln.buf[pos++] = '?'; |
ln.buf[pos++] = '?'; |
continue; |
continue; |
Line 429 mparse_buf_r(struct mparse *curp, struct buf blk, int |
|
Line 431 mparse_buf_r(struct mparse *curp, struct buf blk, int |
|
ln.buf[pos++] = blk.buf[i++]; |
ln.buf[pos++] = blk.buf[i++]; |
} |
} |
|
|
if (pos >= (int)ln.sz) |
if (pos >= (int)ln.sz) |
resize_buf(&ln, 256); |
resize_buf(&ln, 256); |
|
|
ln.buf[pos] = '\0'; |
ln.buf[pos] = '\0'; |
Line 452 mparse_buf_r(struct mparse *curp, struct buf blk, int |
|
Line 454 mparse_buf_r(struct mparse *curp, struct buf blk, int |
|
*/ |
*/ |
|
|
if (curp->secondary) { |
if (curp->secondary) { |
curp->secondary->buf = |
curp->secondary->buf = mandoc_realloc( |
mandoc_realloc |
curp->secondary->buf, |
(curp->secondary->buf, |
curp->secondary->sz + pos + 2); |
curp->secondary->sz + pos + 2); |
memcpy(curp->secondary->buf + |
memcpy(curp->secondary->buf + |
curp->secondary->sz, |
curp->secondary->sz, |
ln.buf, pos); |
ln.buf, pos); |
|
curp->secondary->sz += pos; |
curp->secondary->sz += pos; |
curp->secondary->buf |
curp->secondary->buf |
[curp->secondary->sz] = '\n'; |
[curp->secondary->sz] = '\n'; |
Line 467 mparse_buf_r(struct mparse *curp, struct buf blk, int |
|
Line 468 mparse_buf_r(struct mparse *curp, struct buf blk, int |
|
[curp->secondary->sz] = '\0'; |
[curp->secondary->sz] = '\0'; |
} |
} |
rerun: |
rerun: |
rr = roff_parseln |
rr = roff_parseln(curp->roff, curp->line, |
(curp->roff, curp->line, |
&ln.buf, &ln.sz, of, &of); |
&ln.buf, &ln.sz, of, &of); |
|
|
|
switch (rr) { |
switch (rr) { |
case (ROFF_REPARSE): |
case ROFF_REPARSE: |
if (REPARSE_LIMIT >= ++curp->reparse_count) |
if (REPARSE_LIMIT >= ++curp->reparse_count) |
mparse_buf_r(curp, ln, 0); |
mparse_buf_r(curp, ln, 0); |
else |
else |
mandoc_msg(MANDOCERR_ROFFLOOP, curp, |
mandoc_msg(MANDOCERR_ROFFLOOP, curp, |
curp->line, pos, NULL); |
curp->line, pos, NULL); |
pos = 0; |
pos = 0; |
continue; |
continue; |
case (ROFF_APPEND): |
case ROFF_APPEND: |
pos = (int)strlen(ln.buf); |
pos = (int)strlen(ln.buf); |
continue; |
continue; |
case (ROFF_RERUN): |
case ROFF_RERUN: |
goto rerun; |
goto rerun; |
case (ROFF_IGN): |
case ROFF_IGN: |
pos = 0; |
pos = 0; |
continue; |
continue; |
case (ROFF_ERR): |
case ROFF_ERR: |
assert(MANDOCLEVEL_FATAL <= curp->file_status); |
assert(MANDOCLEVEL_FATAL <= curp->file_status); |
break; |
break; |
case (ROFF_SO): |
case ROFF_SO: |
if (0 == (MPARSE_SO & curp->options) && |
if (0 == (MPARSE_SO & curp->options) && |
(i >= (int)blk.sz || '\0' == blk.buf[i])) { |
(i >= (int)blk.sz || '\0' == blk.buf[i])) { |
curp->sodest = mandoc_strdup(ln.buf + of); |
curp->sodest = mandoc_strdup(ln.buf + of); |
|
|
* buffer because we're going to descend into |
* buffer because we're going to descend into |
* the file recursively. |
* the file recursively. |
*/ |
*/ |
if (curp->secondary) |
if (curp->secondary) |
curp->secondary->sz -= pos + 1; |
curp->secondary->sz -= pos + 1; |
mparse_readfd(curp, -1, ln.buf + of); |
mparse_readfd(curp, -1, ln.buf + of); |
if (MANDOCLEVEL_FATAL <= curp->file_status) |
if (MANDOCLEVEL_FATAL <= curp->file_status) |
|
|
if ( ! (curp->man || curp->mdoc)) |
if ( ! (curp->man || curp->mdoc)) |
pset(ln.buf + of, pos - of, curp); |
pset(ln.buf + of, pos - of, curp); |
|
|
/* |
/* |
* Lastly, push down into the parsers themselves. One |
* Lastly, push down into the parsers themselves. One |
* of these will have already been set in the pset() |
* of these will have already been set in the pset() |
* routine. |
* routine. |
|
|
if (ROFF_TBL == rr) |
if (ROFF_TBL == rr) |
while (NULL != (span = roff_span(curp->roff))) { |
while (NULL != (span = roff_span(curp->roff))) { |
rc = curp->man ? |
rc = curp->man ? |
man_addspan(curp->man, span) : |
man_addspan(curp->man, span) : |
mdoc_addspan(curp->mdoc, span); |
mdoc_addspan(curp->mdoc, span); |
if (0 == rc) |
if (0 == rc) |
break; |
break; |
} |
} |
else if (ROFF_EQN == rr) |
else if (ROFF_EQN == rr) |
rc = curp->mdoc ? |
rc = curp->mdoc ? |
mdoc_addeqn(curp->mdoc, |
mdoc_addeqn(curp->mdoc, |
roff_eqn(curp->roff)) : |
roff_eqn(curp->roff)) : |
man_addeqn(curp->man, |
man_addeqn(curp->man, |
roff_eqn(curp->roff)); |
roff_eqn(curp->roff)); |
else if (curp->man || curp->mdoc) |
else if (curp->man || curp->mdoc) |
rc = curp->man ? |
rc = curp->man ? |
man_parseln(curp->man, |
man_parseln(curp->man, |
curp->line, ln.buf, of) : |
curp->line, ln.buf, of) : |
mdoc_parseln(curp->mdoc, |
mdoc_parseln(curp->mdoc, |
curp->line, ln.buf, of); |
curp->line, ln.buf, of); |
|
|
if (0 == rc) { |
if (0 == rc) { |
assert(MANDOCLEVEL_FATAL <= curp->file_status); |
assert(MANDOCLEVEL_FATAL <= curp->file_status); |
Line 685 mparse_end(struct mparse *curp) |
|
Line 685 mparse_end(struct mparse *curp) |
|
} |
} |
|
|
if ( ! (curp->mdoc || curp->man || curp->sodest)) { |
if ( ! (curp->mdoc || curp->man || curp->sodest)) { |
mandoc_msg(MANDOCERR_NOTMANUAL, curp, 1, 0, NULL); |
mandoc_msg(MANDOCERR_NOTMANUAL, curp, 0, 0, NULL); |
curp->file_status = MANDOCLEVEL_FATAL; |
curp->file_status = MANDOCLEVEL_FATAL; |
return; |
return; |
} |
} |
Line 853 mandoc_vmsg(enum mandocerr t, struct mparse *m, |
|
Line 853 mandoc_vmsg(enum mandocerr t, struct mparse *m, |
|
va_list ap; |
va_list ap; |
|
|
va_start(ap, fmt); |
va_start(ap, fmt); |
vsnprintf(buf, sizeof(buf) - 1, fmt, ap); |
(void)vsnprintf(buf, sizeof(buf), fmt, ap); |
va_end(ap); |
va_end(ap); |
|
|
mandoc_msg(t, m, ln, pos, buf); |
mandoc_msg(t, m, ln, pos, buf); |
} |
} |
|
|
void |
void |
mandoc_msg(enum mandocerr er, struct mparse *m, |
mandoc_msg(enum mandocerr er, struct mparse *m, |
int ln, int col, const char *msg) |
int ln, int col, const char *msg) |
{ |
{ |
enum mandoclevel level; |
enum mandoclevel level; |