version 1.16, 2008/11/28 18:15:29 |
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; |
|
|
Line 129 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 234 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 285 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 295 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 roff_free(struct rofftree *tree, int flush) |
|
Line 361 roff_free(struct rofftree *tree, int flush) |
|
int error, t; |
int error, t; |
struct roffnode *n; |
struct roffnode *n; |
|
|
|
error = 0; |
|
|
if ( ! flush) |
if ( ! flush) |
goto end; |
goto end; |
|
|
Line 372 roff_free(struct rofftree *tree, int flush) |
|
Line 375 roff_free(struct rofftree *tree, int flush) |
|
|
|
for (n = tree->last; n->parent; n = n->parent) { |
for (n = tree->last; n->parent; n = n->parent) { |
if (0 != tokens[n->tok].ctx) |
if (0 != tokens[n->tok].ctx) |
break; |
continue; |
roff_warn(tree, NULL, "closing explicit scope `%s'", |
roff_warn(tree, NULL, "closing explicit scope `%s'", |
toknames[n->tok]); |
toknames[n->tok]); |
goto end; |
goto end; |
Line 384 roff_free(struct rofftree *tree, int flush) |
|
Line 387 roff_free(struct rofftree *tree, int flush) |
|
goto end; |
goto end; |
} |
} |
|
|
if ( ! (*tree->cb.roffhead)(tree->arg)) |
|
goto end; |
|
|
|
error = 0; |
error = 0; |
|
|
end: |
end: |
|
|
roffnode_free(tree); |
roffnode_free(tree); |
|
|
free(tree); |
free(tree); |
|
|
return(error ? 0 : 1); |
return(error ? 0 : 1); |
} |
} |
|
|
Line 404 roff_alloc(const struct roffcb *cb, void *args) |
|
Line 405 roff_alloc(const struct roffcb *cb, void *args) |
|
{ |
{ |
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"); |
|
|
Line 412 roff_alloc(const struct roffcb *cb, void *args) |
|
Line 416 roff_alloc(const struct roffcb *cb, void *args) |
|
|
|
(void)memcpy(&tree->cb, cb, sizeof(struct roffcb)); |
(void)memcpy(&tree->cb, cb, sizeof(struct roffcb)); |
|
|
if ( ! (*tree->cb.roffhead)(args)) { |
|
free(tree); |
|
return(NULL); |
|
} |
|
|
|
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 512 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 534 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 561 roffparse(struct rofftree *tree, char *buf, size_t sz) |
|
Line 558 roffparse(struct rofftree *tree, char *buf, size_t sz) |
|
} |
} |
|
|
if ( ! roffscan(tok, tokens[tree->last->tok].children)) { |
if ( ! roffscan(tok, tokens[tree->last->tok].children)) { |
roff_err(tree, *argvp, "`%s' is invalid child `%s'", |
roff_err(tree, *argvp, "`%s' is invalid child of `%s'", |
toknames[tree->last->tok], |
toknames[tok], |
toknames[tok]); |
toknames[tree->last->tok]); |
return(0); |
return(0); |
} |
} |
|
|
Line 758 roffnode_free(struct rofftree *tree) |
|
Line 755 roffnode_free(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 931 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.roffblkin)(tree->arg, tok, argcp, argvp)) |
|
return(0); |
|
|
|
if (NULL == *argv) |
|
return(1); |
|
|
if ( ! (*tree->cb.roffin)(tree->arg, 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 ( ! (*tree->cb.roffdata) |
if ( ! (*tree->cb.roffdata)(tree->arg, *argv++)) |
(tree->arg, *argv++)) |
|
return(0); |
return(0); |
} |
} |
|
return((*tree->cb.roffout)(tree->arg, tok)); |
if ( ! ((*tree->cb.roffout)(tree->arg, tok))) |
|
return(0); |
|
return((*tree->cb.roffblkin)(tree->arg, tok)); |
|
} |
} |
|
|
while (*argv) { |
while (*argv) { |
Line 959 roff_layout(ROFFCALL_ARGS) |
|
Line 961 roff_layout(ROFFCALL_ARGS) |
|
break; |
break; |
} |
} |
|
|
|
assert(tree->arg); |
if ( ! (*tree->cb.roffdata)(tree->arg, *argv++)) |
if ( ! (*tree->cb.roffdata)(tree->arg, *argv++)) |
return(0); |
return(0); |
} |
} |
|
|
if ( ! ((*tree->cb.roffout)(tree->arg, tok))) |
return((*tree->cb.roffout)(tree->arg, tok)); |
return(0); |
|
return((*tree->cb.roffblkin)(tree->arg, tok)); |
|
} |
} |
|
|
|
|