aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
-rw-r--r--private.h4
-rw-r--r--roff.c61
-rw-r--r--validate.c151
3 files changed, 132 insertions, 84 deletions
diff --git a/private.h b/private.h
index e843eb71..1bae0c72 100644
--- a/private.h
+++ b/private.h
@@ -1,4 +1,4 @@
-/* $Id: private.h,v 1.16 2008/11/29 14:14:21 kristaps Exp $ */
+/* $Id: private.h,v 1.17 2008/11/29 16:11:42 kristaps Exp $ */
/*
* Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>
*
@@ -216,7 +216,7 @@ struct roffcb {
int (*roffin)(void *, int, int *, char **);
int (*roffdata)(void *, char *);
int (*roffout)(void *, int);
- int (*roffblkin)(void *, int);
+ int (*roffblkin)(void *, int, int *, char **);
int (*roffblkout)(void *, int);
int (*roffspecial)(void *, int);
};
diff --git a/roff.c b/roff.c
index a873a435..adf65d17 100644
--- a/roff.c
+++ b/roff.c
@@ -1,4 +1,4 @@
-/* $Id: roff.c,v 1.17 2008/11/29 14:14:21 kristaps Exp $ */
+/* $Id: roff.c,v 1.18 2008/11/29 16:11:42 kristaps Exp $ */
/*
* Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>
*
@@ -38,6 +38,8 @@
/* FIXME: NAME section needs specific elements. */
+/* FIXME: don't print Os, just do roffbegin. */
+
#define ROFF_MAXARG 32
enum roffd {
@@ -54,7 +56,7 @@ enum rofftype {
#define ROFFCALL_ARGS \
int tok, struct rofftree *tree, \
- const char *argv[], enum roffd type
+ char *argv[], enum roffd type
struct rofftree;
@@ -129,7 +131,7 @@ static int roffargs(const struct rofftree *,
int, char *, char **);
static int roffargok(int, int);
static int roffnextopt(const struct rofftree *,
- int, const char ***, char **);
+ int, char ***, char **);
static int roffparse(struct rofftree *, char *);
static int textparse(const struct rofftree *, char *);
@@ -233,7 +235,7 @@ static const struct rofftok tokens[ROFF_MAX] = {
{ roff_text, NULL, NULL, NULL, 0, ROFF_TEXT, ROFF_PARSED | ROFF_CALLABLE }, /* Bc */
{ NULL, NULL, NULL, NULL, 0, ROFF_TEXT, 0 }, /* Bf */ /* FIXME */
{ 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 }, /* Bx */
{roff_special, NULL, NULL, NULL, 0, ROFF_SPECIAL, 0 }, /* Db */
@@ -284,7 +286,7 @@ static const int tokenargs[ROFF_ARGMAX] = {
0, ROFF_VALUE, ROFF_VALUE, 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,
@@ -385,9 +387,6 @@ roff_free(struct rofftree *tree, int flush)
goto end;
}
- if ( ! (*tree->cb.rofftail)(tree->arg))
- goto end;
-
error = 0;
end:
@@ -417,11 +416,6 @@ roff_alloc(const struct roffcb *cb, void *args)
(void)memcpy(&tree->cb, cb, sizeof(struct roffcb));
- if ( ! (*tree->cb.roffhead)(args)) {
- free(tree);
- return(NULL);
- }
-
return(tree);
}
@@ -522,7 +516,7 @@ roffparse(struct rofftree *tree, char *buf)
int tok, t;
struct roffnode *n;
char *argv[ROFF_MAXARG];
- const char **argvp;
+ char **argvp;
if (ROFF_MAX == (tok = rofffindtok(buf + 1))) {
roff_err(tree, buf + 1, "bogus line macro");
@@ -537,7 +531,7 @@ roffparse(struct rofftree *tree, char *buf)
if ( ! roffargs(tree, tok, buf, argv))
return(0);
- argvp = (const char **)argv;
+ argvp = (char **)argv;
/*
* Prelude macros break some assumptions, so branch now.
@@ -564,9 +558,9 @@ roffparse(struct rofftree *tree, char *buf)
}
if ( ! roffscan(tok, tokens[tree->last->tok].children)) {
- roff_err(tree, *argvp, "`%s' is invalid child `%s'",
- toknames[tree->last->tok],
- toknames[tok]);
+ roff_err(tree, *argvp, "`%s' is invalid child of `%s'",
+ toknames[tok],
+ toknames[tree->last->tok]);
return(0);
}
@@ -761,9 +755,9 @@ roffnode_free(struct rofftree *tree)
static int
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;
*val = NULL;
@@ -870,8 +864,8 @@ roff_Os(ROFFCALL_ARGS)
{
if (ROFF_EXIT == type) {
- assert(ROFF_PRELUDE_Os & tree->state);
- return(roff_layout(tok, tree, argv, type));
+ roffnode_free(tree);
+ return((*tree->cb.rofftail)(tree->arg));
} else if (ROFF_BODY & tree->state) {
assert( ! (ROFF_PRELUDE & tree->state));
assert(ROFF_PRELUDE_Os & tree->state);
@@ -893,7 +887,10 @@ roff_Os(ROFFCALL_ARGS)
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));
}
@@ -934,19 +931,21 @@ roff_layout(ROFFCALL_ARGS)
if (NULL == roffnode_new(tok, tree))
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))
return(0);
if ( ! (ROFF_PARSED & tokens[tok].flags)) {
while (*argv) {
- if ( ! (*tree->cb.roffdata)
- (tree->arg, *argv++))
+ if ( ! (*tree->cb.roffdata)(tree->arg, *argv++))
return(0);
}
-
- if ( ! ((*tree->cb.roffout)(tree->arg, tok)))
- return(0);
- return((*tree->cb.roffblkin)(tree->arg, tok));
+ return((*tree->cb.roffout)(tree->arg, tok));
}
while (*argv) {
@@ -967,9 +966,7 @@ roff_layout(ROFFCALL_ARGS)
return(0);
}
- if ( ! ((*tree->cb.roffout)(tree->arg, tok)))
- return(0);
- return((*tree->cb.roffblkin)(tree->arg, tok));
+ return((*tree->cb.roffout)(tree->arg, tok));
}
diff --git a/validate.c b/validate.c
index 0f03b233..7eb665c3 100644
--- a/validate.c
+++ b/validate.c
@@ -1,4 +1,4 @@
-/* $Id: validate.c,v 1.1 2008/11/29 14:14:21 kristaps Exp $ */
+/* $Id: validate.c,v 1.2 2008/11/29 16:11:42 kristaps Exp $ */
/*
* Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>
*
@@ -28,8 +28,10 @@
#include "libmdocml.h"
#include "private.h"
+#define INDENT 4
+
#ifdef __linux__ /* FIXME */
-#define strlcat strncat
+#define strlcat strncat
#endif
struct md_valid {
@@ -52,7 +54,7 @@ static int rofftail(void *);
static int roffin(void *, int, int *, char **);
static int roffdata(void *, char *);
static int roffout(void *, int);
-static int roffblkin(void *, int);
+static int roffblkin(void *, int, int *, char **);
static int roffblkout(void *, int);
static int roffspecial(void *, int);
@@ -66,36 +68,26 @@ mbuf_indent(struct md_valid *p)
{
size_t i;
- assert(0 == p->pos);
+ assert(p->pos == 0);
- for (i = 0; i < MIN(p->indent, 4); i++)
+ for (i = 0; i < MIN(p->indent, INDENT); i++)
if ( ! md_buf_putstring(p->mbuf, " "))
return(0);
- p->pos = i * 4;
+ p->pos += i * INDENT;
return(1);
}
static int
-mbuf_atnewline(struct md_valid *p)
-{
-
- return(p->pos == MIN(4, p->indent));
-}
-
-
-static int
mbuf_newline(struct md_valid *p)
{
- if (mbuf_atnewline(p))
- return(1);
if ( ! md_buf_putchar(p->mbuf, '\n'))
return(0);
p->pos = 0;
- return(mbuf_indent(p));
+ return(1);
}
@@ -111,14 +103,6 @@ mbuf_data(struct md_valid *p, char *buf)
if (MD_LITERAL & p->flags)
return(md_buf_putstring(p->mbuf, buf));
- if (0 == p->pos)
- mbuf_indent(p);
-
- /*
- * Indent if we're at the beginning of a line. Don't indent
- * more than 16 or so characters.
- */
-
while (*buf) {
while (*buf && isspace(*buf))
buf++;
@@ -136,29 +120,38 @@ mbuf_data(struct md_valid *p, char *buf)
/* Process word. */
sz = strlen(bufp);
-
- if (sz + p->pos < 72) {
+
+ if (0 == p->pos) {
+ if ( ! mbuf_indent(p))
+ return(0);
if ( ! md_buf_putstring(p->mbuf, bufp))
return(0);
- /* FIXME: check punctuation. */
+ if (p->indent * INDENT + sz >= 72) {
+ if ( ! mbuf_newline(p))
+ return(0);
+ continue;
+ }
if ( ! md_buf_putchar(p->mbuf, ' '))
return(0);
+
p->pos += sz + 1;
continue;
}
- if ( ! mbuf_newline(p))
- return(0);
+ if (sz + p->pos >= 72) {
+ if ( ! mbuf_newline(p))
+ return(0);
+ if ( ! mbuf_indent(p))
+ return(0);
+ }
if ( ! md_buf_putstring(p->mbuf, bufp))
return(0);
-
- /* FIXME: check punctuation. */
-
if ( ! md_buf_putchar(p->mbuf, ' '))
return(0);
+
p->pos += sz + 1;
}
@@ -229,6 +222,16 @@ md_init_valid(const struct md_args *args,
static int
roffhead(void *arg)
{
+ struct md_valid *p;
+
+ assert(arg);
+ p = (struct md_valid *)arg;
+
+ if ( ! md_buf_putstring(p->mbuf, "BEGIN"))
+ return(0);
+ p->indent++;
+ if ( ! mbuf_newline(p))
+ return(0);
return(1);
}
@@ -242,10 +245,12 @@ rofftail(void *arg)
assert(arg);
p = (struct md_valid *)arg;
- if (mbuf_atnewline(p))
- return(1);
+ if (0 != p->pos && ! mbuf_newline(p))
+ return(0);
- return(md_buf_putchar(p->mbuf, '\n'));
+ if ( ! md_buf_putstring(p->mbuf, "END\n"))
+ return(0);
+ return(1);
}
@@ -258,31 +263,32 @@ roffspecial(void *arg, int tok)
static int
-roffblkin(void *arg, int tok)
+roffblkin(void *arg, int tok, int *argc, char **argv)
{
struct md_valid *p;
assert(arg);
p = (struct md_valid *)arg;
- if ( ! mbuf_atnewline(p)) {
- if ( ! md_buf_putchar(p->mbuf, '\n'))
+ if (0 != p->pos) {
+ if ( ! mbuf_newline(p))
return(0);
- p->pos = 0;
if ( ! mbuf_indent(p))
return(0);
- }
+ } else if ( ! mbuf_indent(p))
+ return(0);
+ if ( ! md_buf_putchar(p->mbuf, '<'))
+ return(0);
if ( ! md_buf_putstring(p->mbuf, toknames[tok]))
return(0);
-
- if ( ! md_buf_putchar(p->mbuf, '\n'))
+ if ( ! md_buf_putchar(p->mbuf, '>'))
+ return(0);
+ if ( ! mbuf_newline(p))
return(0);
- p->pos = 0;
p->indent++;
-
- return(mbuf_indent(p));
+ return(1);
}
@@ -294,19 +300,48 @@ roffblkout(void *arg, int tok)
assert(arg);
p = (struct md_valid *)arg;
- if ( ! md_buf_putchar(p->mbuf, '\n'))
+ p->indent--;
+
+ if (0 != p->pos) {
+ if ( ! mbuf_newline(p))
+ return(0);
+ if ( ! mbuf_indent(p))
+ return(0);
+ } else if ( ! mbuf_indent(p))
return(0);
- p->pos = 0;
- p->indent--;
+ if ( ! md_buf_putstring(p->mbuf, "</"))
+ return(0);
+ if ( ! md_buf_putstring(p->mbuf, toknames[tok]))
+ return(0);
+ if ( ! md_buf_putstring(p->mbuf, ">"))
+ return(0);
+ if ( ! mbuf_newline(p))
+ return(0);
- return(mbuf_indent(p));
+ return(1);
}
static int
roffin(void *arg, int tok, int *argcp, char **argvp)
{
+ struct md_valid *p;
+
+ assert(arg);
+ p = (struct md_valid *)arg;
+
+ if (0 == p->pos && ! mbuf_indent(p))
+ return(0);
+
+ if ( ! md_buf_putstring(p->mbuf, "<"))
+ return(0);
+ if ( ! md_buf_putstring(p->mbuf, toknames[tok]))
+ return(0);
+ if ( ! md_buf_putstring(p->mbuf, ">"))
+ return(0);
+
+ p->pos += strlen(toknames[tok]) + 2;
return(1);
}
@@ -315,6 +350,22 @@ roffin(void *arg, int tok, int *argcp, char **argvp)
static int
roffout(void *arg, int tok)
{
+ struct md_valid *p;
+
+ assert(arg);
+ p = (struct md_valid *)arg;
+
+ if (0 == p->pos && ! mbuf_indent(p))
+ return(0);
+
+ if ( ! md_buf_putstring(p->mbuf, "</"))
+ return(0);
+ if ( ! md_buf_putstring(p->mbuf, toknames[tok]))
+ return(0);
+ if ( ! md_buf_putstring(p->mbuf, "> "))
+ return(0);
+
+ p->pos += strlen(toknames[tok]) + 3;
return(1);
}