version 1.14, 2008/11/28 11:21:12 |
version 1.18, 2008/11/29 16:11:42 |
|
|
|
|
/* FIXME: NAME section needs specific elements. */ |
/* FIXME: NAME section needs specific elements. */ |
|
|
|
/* FIXME: don't print Os, just do roffbegin. */ |
|
|
#define ROFF_MAXARG 32 |
#define ROFF_MAXARG 32 |
|
|
enum roffd { |
enum roffd { |
|
|
|
|
#define ROFFCALL_ARGS \ |
#define ROFFCALL_ARGS \ |
int tok, struct rofftree *tree, \ |
int tok, struct rofftree *tree, \ |
const char *argv[], enum roffd type |
char *argv[], enum roffd type |
|
|
struct rofftree; |
struct rofftree; |
|
|
|
|
struct roffnode { |
struct roffnode { |
int tok; /* Token id. */ |
int tok; /* Token id. */ |
struct roffnode *parent; /* Parent (or NULL). */ |
struct roffnode *parent; /* Parent (or NULL). */ |
size_t line; /* Parsed at line. */ |
|
}; |
}; |
|
|
struct rofftree { |
struct rofftree { |
Line 100 struct rofftree { |
|
Line 101 struct rofftree { |
|
#define ROFF_PRELUDE_Dd (1 << 4) /* `Dd' is parsed. */ |
#define ROFF_PRELUDE_Dd (1 << 4) /* `Dd' is parsed. */ |
#define ROFF_BODY (1 << 5) /* In roff body. */ |
#define ROFF_BODY (1 << 5) /* In roff body. */ |
|
|
struct md_mbuf *mbuf; /* Output (or NULL). */ |
struct roffcb cb; |
const struct md_args *args; /* Global args. */ |
void *arg; |
const struct md_rbuf *rbuf; /* Input. */ |
|
const struct roffcb *cb; |
|
}; |
}; |
|
|
static int roff_Dd(ROFFCALL_ARGS); |
static int roff_Dd(ROFFCALL_ARGS); |
Line 117 static int roff_close(ROFFCALL_ARGS); |
|
Line 116 static int roff_close(ROFFCALL_ARGS); |
|
static int roff_special(ROFFCALL_ARGS); |
static int roff_special(ROFFCALL_ARGS); |
|
|
static struct roffnode *roffnode_new(int, struct rofftree *); |
static struct roffnode *roffnode_new(int, struct rofftree *); |
static void roffnode_free(int, struct rofftree *); |
static void roffnode_free(struct rofftree *); |
|
|
static void roff_warn(const struct rofftree *, |
static void roff_warn(const struct rofftree *, |
const char *, char *, ...); |
const char *, char *, ...); |
Line 132 static int roffargs(const struct rofftree *, |
|
Line 131 static int roffargs(const struct rofftree *, |
|
int, char *, char **); |
int, char *, char **); |
static int roffargok(int, int); |
static int roffargok(int, int); |
static int roffnextopt(const struct rofftree *, |
static int roffnextopt(const struct rofftree *, |
int, const char ***, char **); |
int, char ***, char **); |
static int roffparse(struct rofftree *, char *, size_t); |
static int roffparse(struct rofftree *, char *); |
static int textparse(const struct rofftree *, |
static int textparse(const struct rofftree *, char *); |
const char *, size_t); |
|
|
|
|
|
static const int roffarg_An[] = { ROFF_Split, ROFF_Nosplit, |
static const int roffarg_An[] = { ROFF_Split, ROFF_Nosplit, |
Line 237 static const struct rofftok tokens[ROFF_MAX] = { |
|
Line 235 static const struct rofftok tokens[ROFF_MAX] = { |
|
{ roff_text, NULL, NULL, NULL, 0, ROFF_TEXT, ROFF_PARSED | ROFF_CALLABLE }, /* Bc */ |
{ roff_text, NULL, NULL, NULL, 0, ROFF_TEXT, ROFF_PARSED | ROFF_CALLABLE }, /* Bc */ |
{ NULL, NULL, NULL, NULL, 0, ROFF_TEXT, 0 }, /* Bf */ /* FIXME */ |
{ NULL, NULL, NULL, NULL, 0, ROFF_TEXT, 0 }, /* Bf */ /* FIXME */ |
{ roff_text, NULL, NULL, NULL, 0, ROFF_TEXT, ROFF_PARSED | ROFF_CALLABLE }, /* Bo */ |
{ roff_text, NULL, NULL, NULL, 0, ROFF_TEXT, ROFF_PARSED | ROFF_CALLABLE }, /* Bo */ |
{ NULL, NULL, NULL, NULL, 0, ROFF_TEXT, ROFF_PARSED | ROFF_CALLABLE }, /* Bq */ |
{ roff_text, NULL, NULL, NULL, 0, ROFF_TEXT, ROFF_PARSED | ROFF_CALLABLE }, /* Bq */ |
{ roff_text, NULL, NULL, NULL, 0, ROFF_TEXT, ROFF_PARSED }, /* Bsx */ |
{ roff_text, NULL, NULL, NULL, 0, ROFF_TEXT, ROFF_PARSED }, /* Bsx */ |
{ roff_text, NULL, NULL, NULL, 0, ROFF_TEXT, ROFF_PARSED }, /* Bx */ |
{ roff_text, NULL, NULL, NULL, 0, ROFF_TEXT, ROFF_PARSED }, /* Bx */ |
{roff_special, NULL, NULL, NULL, 0, ROFF_SPECIAL, 0 }, /* Db */ |
{roff_special, NULL, NULL, NULL, 0, ROFF_SPECIAL, 0 }, /* Db */ |
Line 288 static const int tokenargs[ROFF_ARGMAX] = { |
|
Line 286 static const int tokenargs[ROFF_ARGMAX] = { |
|
0, ROFF_VALUE, ROFF_VALUE, 0, |
0, ROFF_VALUE, ROFF_VALUE, 0, |
0, 0, 0, 0, |
0, 0, 0, 0, |
0, 0, 0, 0, |
0, 0, 0, 0, |
|
0, 0, ROFF_VALUE, 0, |
0, 0, 0, 0, |
0, 0, 0, 0, |
0, 0, 0, 0, |
0, 0, 0, 0, |
0, 0, 0, 0, |
0, 0, 0, 0, |
Line 298 static const int tokenargs[ROFF_ARGMAX] = { |
|
Line 297 static const int tokenargs[ROFF_ARGMAX] = { |
|
0, 0, 0, 0, |
0, 0, 0, 0, |
0, 0, 0, 0, |
0, 0, 0, 0, |
0, 0, 0, 0, |
0, 0, 0, 0, |
0, 0, 0, 0, |
|
}; |
}; |
|
|
const char *const toknamesp[ROFF_MAX] = { |
const char *const toknamesp[ROFF_MAX] = { |
Line 360 const char *const *tokargnames = tokargnamesp; |
|
Line 358 const char *const *tokargnames = tokargnamesp; |
|
int |
int |
roff_free(struct rofftree *tree, int flush) |
roff_free(struct rofftree *tree, int flush) |
{ |
{ |
int error, tok; |
int error, t; |
|
struct roffnode *n; |
|
|
assert(tree->mbuf); |
error = 0; |
|
|
if ( ! flush) |
if ( ! flush) |
tree->mbuf = NULL; |
goto end; |
|
|
/* LINTED */ |
error = 1; |
|
|
|
if (ROFF_PRELUDE & tree->state) { |
|
roff_warn(tree, NULL, "prelude never finished"); |
|
goto end; |
|
} |
|
|
|
for (n = tree->last; n->parent; n = n->parent) { |
|
if (0 != tokens[n->tok].ctx) |
|
continue; |
|
roff_warn(tree, NULL, "closing explicit scope `%s'", |
|
toknames[n->tok]); |
|
goto end; |
|
} |
|
|
while (tree->last) { |
while (tree->last) { |
if (tree->last->parent) { |
t = tree->last->tok; |
tok = tree->last->tok; |
if ( ! (*tokens[t].cb)(t, tree, NULL, ROFF_EXIT)) |
if (tokens[tok].ctx == 0) { |
goto end; |
warnx("%s: closing out explicit scope " |
|
"of `%s' from line %zu", |
|
tree->rbuf->name, |
|
toknames[tok], |
|
tree->last->line); |
|
tree->mbuf = NULL; |
|
} |
|
} |
|
if ( ! (*tokens[tree->last->tok].cb) |
|
(tree->last->tok, tree, NULL, ROFF_EXIT)) |
|
/* Disallow flushing. */ |
|
tree->mbuf = NULL; |
|
} |
} |
|
|
error = tree->mbuf ? 0 : 1; |
error = 0; |
|
|
if (tree->mbuf && (ROFF_PRELUDE & tree->state)) { |
end: |
warnx("%s: prelude never finished", |
|
tree->rbuf->name); |
|
error = 1; |
|
} |
|
|
|
|
while (tree->last) |
|
roffnode_free(tree); |
|
|
free(tree); |
free(tree); |
|
|
return(error ? 0 : 1); |
return(error ? 0 : 1); |
} |
} |
|
|
|
|
struct rofftree * |
struct rofftree * |
roff_alloc(const struct md_args *args, struct md_mbuf *out, |
roff_alloc(const struct roffcb *cb, void *args) |
const struct md_rbuf *in, const struct roffcb *cb) |
|
{ |
{ |
struct rofftree *tree; |
struct rofftree *tree; |
|
|
|
assert(args); |
|
assert(cb); |
|
|
if (NULL == (tree = calloc(1, sizeof(struct rofftree)))) |
if (NULL == (tree = calloc(1, sizeof(struct rofftree)))) |
err(1, "calloc"); |
err(1, "calloc"); |
|
|
tree->state = ROFF_PRELUDE; |
tree->state = ROFF_PRELUDE; |
tree->args = args; |
tree->arg = args; |
tree->mbuf = out; |
|
tree->rbuf = in; |
|
tree->cb = cb; |
|
|
|
|
(void)memcpy(&tree->cb, cb, sizeof(struct roffcb)); |
|
|
return(tree); |
return(tree); |
} |
} |
|
|
|
|
int |
int |
roff_engine(struct rofftree *tree, char *buf, size_t sz) |
roff_engine(struct rofftree *tree, char *buf) |
{ |
{ |
|
|
tree->cur = NULL; |
tree->cur = buf; |
|
assert(buf); |
|
|
if (0 == sz) { |
if (0 == *buf) { |
roff_warn(tree, buf, "blank line"); |
roff_warn(tree, buf, "blank line"); |
return(0); |
return(0); |
} else if ('.' != *buf) |
} else if ('.' != *buf) |
return(textparse(tree, buf, sz)); |
return(textparse(tree, buf)); |
|
|
return(roffparse(tree, buf, sz)); |
return(roffparse(tree, buf)); |
} |
} |
|
|
|
|
static int |
static int |
textparse(const struct rofftree *tree, const char *buf, size_t sz) |
textparse(const struct rofftree *tree, char *buf) |
{ |
{ |
|
|
/* Print text. */ |
return((*tree->cb.roffdata)(tree->arg, buf)); |
return(1); |
|
} |
} |
|
|
|
|
Line 508 roffscan(int tok, const int *tokv) |
|
Line 511 roffscan(int tok, const int *tokv) |
|
|
|
|
|
static int |
static int |
roffparse(struct rofftree *tree, char *buf, size_t sz) |
roffparse(struct rofftree *tree, char *buf) |
{ |
{ |
int tok, t; |
int tok, t; |
struct roffnode *n; |
struct roffnode *n; |
char *argv[ROFF_MAXARG]; |
char *argv[ROFF_MAXARG]; |
const char **argvp; |
char **argvp; |
|
|
assert(sz > 0); |
|
|
|
if (ROFF_MAX == (tok = rofffindtok(buf + 1))) { |
if (ROFF_MAX == (tok = rofffindtok(buf + 1))) { |
roff_err(tree, buf + 1, "bogus line macro"); |
roff_err(tree, buf + 1, "bogus line macro"); |
return(0); |
return(0); |
Line 530 roffparse(struct rofftree *tree, char *buf, size_t sz) |
|
Line 531 roffparse(struct rofftree *tree, char *buf, size_t sz) |
|
if ( ! roffargs(tree, tok, buf, argv)) |
if ( ! roffargs(tree, tok, buf, argv)) |
return(0); |
return(0); |
|
|
argvp = (const char **)argv; |
argvp = (char **)argv; |
|
|
/* |
/* |
* Prelude macros break some assumptions, so branch now. |
* Prelude macros break some assumptions, so branch now. |
Line 550 roffparse(struct rofftree *tree, char *buf, size_t sz) |
|
Line 551 roffparse(struct rofftree *tree, char *buf, size_t sz) |
|
*/ |
*/ |
|
|
if ( ! roffscan(tree->last->tok, tokens[tok].parents)) { |
if ( ! roffscan(tree->last->tok, tokens[tok].parents)) { |
roff_err(tree, *argvp, "`%s' has invalid parent `%s' " |
roff_err(tree, *argvp, "`%s' has invalid parent `%s'", |
"from line %zu", toknames[tok], |
toknames[tok], |
toknames[tree->rbuf->line], |
toknames[tree->last->tok]); |
tree->rbuf->line); |
|
return(0); |
return(0); |
} |
} |
|
|
if ( ! roffscan(tok, tokens[tree->last->tok].children)) { |
if ( ! roffscan(tok, tokens[tree->last->tok].children)) { |
roff_err(tree, *argvp, "`%s' is invalid child for " |
roff_err(tree, *argvp, "`%s' is invalid child of `%s'", |
"`%s' from line %zu", toknames[tok], |
toknames[tok], |
toknames[tree->rbuf->line], |
toknames[tree->last->tok]); |
tree->rbuf->line); |
|
return(0); |
return(0); |
} |
} |
|
|
Line 717 roffnode_new(int tokid, struct rofftree *tree) |
|
Line 716 roffnode_new(int tokid, struct rofftree *tree) |
|
if (NULL == (p = malloc(sizeof(struct roffnode)))) |
if (NULL == (p = malloc(sizeof(struct roffnode)))) |
err(1, "malloc"); |
err(1, "malloc"); |
|
|
p->line = tree->rbuf->line; |
|
p->tok = tokid; |
p->tok = tokid; |
p->parent = tree->last; |
p->parent = tree->last; |
tree->last = p; |
tree->last = p; |
Line 743 roffargok(int tokid, int argid) |
|
Line 741 roffargok(int tokid, int argid) |
|
|
|
|
|
static void |
static void |
roffnode_free(int tokid, struct rofftree *tree) |
roffnode_free(struct rofftree *tree) |
{ |
{ |
struct roffnode *p; |
struct roffnode *p; |
|
|
assert(tree->last); |
assert(tree->last); |
assert(tree->last->tok == tokid); |
|
|
|
p = tree->last; |
p = tree->last; |
tree->last = tree->last->parent; |
tree->last = tree->last->parent; |
Line 758 roffnode_free(int tokid, struct rofftree *tree) |
|
Line 755 roffnode_free(int tokid, struct rofftree *tree) |
|
|
|
static int |
static int |
roffnextopt(const struct rofftree *tree, int tok, |
roffnextopt(const struct rofftree *tree, int tok, |
const char ***in, char **val) |
char ***in, char **val) |
{ |
{ |
const char *arg, **argv; |
char *arg, **argv; |
int v; |
int v; |
|
|
*val = NULL; |
*val = NULL; |
Line 867 roff_Os(ROFFCALL_ARGS) |
|
Line 864 roff_Os(ROFFCALL_ARGS) |
|
{ |
{ |
|
|
if (ROFF_EXIT == type) { |
if (ROFF_EXIT == type) { |
assert(ROFF_PRELUDE_Os & tree->state); |
roffnode_free(tree); |
return(roff_layout(tok, tree, argv, type)); |
return((*tree->cb.rofftail)(tree->arg)); |
} else if (ROFF_BODY & tree->state) { |
} else if (ROFF_BODY & tree->state) { |
assert( ! (ROFF_PRELUDE & tree->state)); |
assert( ! (ROFF_PRELUDE & tree->state)); |
assert(ROFF_PRELUDE_Os & tree->state); |
assert(ROFF_PRELUDE_Os & tree->state); |
Line 890 roff_Os(ROFFCALL_ARGS) |
|
Line 887 roff_Os(ROFFCALL_ARGS) |
|
|
|
assert(NULL == tree->last); |
assert(NULL == tree->last); |
|
|
return(roff_layout(tok, tree, argv, type)); |
if (NULL == roffnode_new(tok, tree)) |
|
return(0); |
|
|
|
return((*tree->cb.roffhead)(tree->arg)); |
} |
} |
|
|
|
|
Line 908 roff_layout(ROFFCALL_ARGS) |
|
Line 908 roff_layout(ROFFCALL_ARGS) |
|
} |
} |
|
|
if (ROFF_EXIT == type) { |
if (ROFF_EXIT == type) { |
roffnode_free(tok, tree); |
roffnode_free(tree); |
return((*tree->cb->roffblkout)(tree->args, tok)); |
return((*tree->cb.roffblkout)(tree->arg, tok)); |
} |
} |
|
|
assert(tree->mbuf); |
|
|
|
i = 0; |
i = 0; |
argv++; |
argv++; |
|
|
Line 933 roff_layout(ROFFCALL_ARGS) |
|
Line 931 roff_layout(ROFFCALL_ARGS) |
|
if (NULL == roffnode_new(tok, tree)) |
if (NULL == roffnode_new(tok, tree)) |
return(0); |
return(0); |
|
|
if ( ! (*tree->cb->roffin)(tree->args, tok, argcp, argvp)) |
if ( ! (*tree->cb.roffblkin)(tree->arg, tok, argcp, argvp)) |
return(0); |
return(0); |
|
|
|
if (NULL == *argv) |
|
return(1); |
|
|
|
if ( ! (*tree->cb.roffin)(tree->arg, tok, argcp, argvp)) |
|
return(0); |
|
|
if ( ! (ROFF_PARSED & tokens[tok].flags)) { |
if ( ! (ROFF_PARSED & tokens[tok].flags)) { |
while (*argv) { |
while (*argv) { |
if ( ! md_buf_putstring(tree->mbuf, *argv++)) |
if ( ! (*tree->cb.roffdata)(tree->arg, *argv++)) |
return(0); |
return(0); |
if ( ! md_buf_putchar(tree->mbuf, ' ')) |
|
return(0); |
|
} |
} |
|
return((*tree->cb.roffout)(tree->arg, tok)); |
if ( ! md_buf_putchar(tree->mbuf, '\n')) |
|
return(0); |
|
|
|
if ( ! ((*tree->cb->roffout)(tree->args, tok))) |
|
return(0); |
|
return((*tree->cb->roffblkin)(tree->args, tok)); |
|
} |
} |
|
|
while (*argv) { |
while (*argv) { |
Line 965 roff_layout(ROFFCALL_ARGS) |
|
Line 961 roff_layout(ROFFCALL_ARGS) |
|
break; |
break; |
} |
} |
|
|
if ( ! md_buf_putstring(tree->mbuf, *argv++)) |
assert(tree->arg); |
|
if ( ! (*tree->cb.roffdata)(tree->arg, *argv++)) |
return(0); |
return(0); |
if ( ! md_buf_putchar(tree->mbuf, ' ')) |
|
return(0); |
|
} |
} |
|
|
if (NULL == *argv && ! md_buf_putchar(tree->mbuf, '\n')) |
return((*tree->cb.roffout)(tree->arg, tok)); |
return(0); |
|
|
|
if ( ! ((*tree->cb->roffout)(tree->args, tok))) |
|
return(0); |
|
return((*tree->cb->roffblkin)(tree->args, tok)); |
|
} |
} |
|
|
|
|
Line 987 roff_text(ROFFCALL_ARGS) |
|
Line 977 roff_text(ROFFCALL_ARGS) |
|
int i, c, argcp[ROFF_MAXARG]; |
int i, c, argcp[ROFF_MAXARG]; |
char *v, *argvp[ROFF_MAXARG]; |
char *v, *argvp[ROFF_MAXARG]; |
|
|
assert(tree->mbuf); |
|
|
|
if (ROFF_PRELUDE & tree->state) { |
if (ROFF_PRELUDE & tree->state) { |
roff_err(tree, *argv, "`%s' disallowed in prelude", |
roff_err(tree, *argv, "`%s' disallowed in prelude", |
toknames[tok]); |
toknames[tok]); |
Line 1011 roff_text(ROFFCALL_ARGS) |
|
Line 999 roff_text(ROFFCALL_ARGS) |
|
argcp[i] = ROFF_ARGMAX; |
argcp[i] = ROFF_ARGMAX; |
argvp[i] = NULL; |
argvp[i] = NULL; |
|
|
if ( ! (*tree->cb->roffin)(tree->args, tok, argcp, argvp)) |
if ( ! (*tree->cb.roffin)(tree->arg, tok, argcp, argvp)) |
return(0); |
return(0); |
|
|
if ( ! (ROFF_PARSED & tokens[tok].flags)) { |
if ( ! (ROFF_PARSED & tokens[tok].flags)) { |
while (*argv) { |
while (*argv) { |
if ( ! md_buf_putstring(tree->mbuf, *argv++)) |
if ( ! (*tree->cb.roffdata)(tree->arg, *argv++)) |
return(0); |
return(0); |
if ( ! md_buf_putchar(tree->mbuf, ' ')) |
|
return(0); |
|
} |
} |
if ( ! md_buf_putchar(tree->mbuf, '\n')) |
return((*tree->cb.roffout)(tree->arg, tok)); |
return(0); |
|
return((*tree->cb->roffout)(tree->args, tok)); |
|
} |
} |
|
|
while (*argv) { |
while (*argv) { |
Line 1039 roff_text(ROFFCALL_ARGS) |
|
Line 1023 roff_text(ROFFCALL_ARGS) |
|
return(0); |
return(0); |
break; |
break; |
} |
} |
|
if ( ! (*tree->cb.roffdata)(tree->arg, *argv++)) |
if ( ! md_buf_putstring(tree->mbuf, *argv++)) |
|
return(0); |
return(0); |
if ( ! md_buf_putchar(tree->mbuf, ' ')) |
|
return(0); |
|
} |
} |
|
|
if (NULL == *argv && ! md_buf_putchar(tree->mbuf, '\n')) |
return((*tree->cb.roffout)(tree->arg, tok)); |
return(0); |
|
|
|
return((*tree->cb->roffout)(tree->args, tok)); |
|
} |
} |
|
|
|
|
|
|
roff_special(ROFFCALL_ARGS) |
roff_special(ROFFCALL_ARGS) |
{ |
{ |
|
|
return((*tree->cb->roffspecial)(tree->args, tok)); |
return((*tree->cb.roffspecial)(tree->arg, tok)); |
} |
} |
|
|
|
|
Line 1090 roff_warn(const struct rofftree *tree, const char *pos |
|
Line 1068 roff_warn(const struct rofftree *tree, const char *pos |
|
(void)vsnprintf(buf, sizeof(buf), fmt, ap); |
(void)vsnprintf(buf, sizeof(buf), fmt, ap); |
va_end(ap); |
va_end(ap); |
|
|
(*tree->cb->roffmsg)(tree->args, ROFF_WARN, tree->cur, pos, |
(*tree->cb.roffmsg)(tree->arg, |
tree->rbuf->name, tree->rbuf->line, buf); |
ROFF_WARN, tree->cur, pos, buf); |
} |
} |
|
|
|
|
Line 1105 roff_err(const struct rofftree *tree, const char *pos, |
|
Line 1083 roff_err(const struct rofftree *tree, const char *pos, |
|
(void)vsnprintf(buf, sizeof(buf), fmt, ap); |
(void)vsnprintf(buf, sizeof(buf), fmt, ap); |
va_end(ap); |
va_end(ap); |
|
|
(*tree->cb->roffmsg)(tree->args, ROFF_ERROR, tree->cur, pos, |
(*tree->cb.roffmsg)(tree->arg, |
tree->rbuf->name, tree->rbuf->line, buf); |
ROFF_ERROR, tree->cur, pos, buf); |
} |
} |