summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--html4_strict.c62
-rw-r--r--libmdocml.c47
-rw-r--r--libmdocml.h2
-rw-r--r--mdocml.c37
4 files changed, 86 insertions, 62 deletions
diff --git a/html4_strict.c b/html4_strict.c
index f46b3373..9ad1dafb 100644
--- a/html4_strict.c
+++ b/html4_strict.c
@@ -39,12 +39,10 @@ enum rofftype {
ROFF_LAYOUT
};
-#define ROFFCALL_ARGS const struct md_args *arg, \
- struct md_mbuf *out, \
- const struct md_rbuf *in, \
- const char *buf, size_t sz, \
- size_t pos, enum roffd type, \
- struct rofftree *tree
+#define ROFFCALL_ARGS \
+ const struct md_args *arg, struct md_mbuf *out, \
+ const struct md_rbuf *in, const char *buf, size_t sz, \
+ size_t pos, enum roffd type, struct rofftree *tree
struct rofftree;
@@ -54,9 +52,9 @@ struct rofftok {
int (*cb)(ROFFCALL_ARGS);
enum rofftype type;
int flags;
-#define ROFF_NESTED (1 << 0) /* FIXME: test. */
-#define ROFF_PARSED (1 << 1) /* FIXME: test. */
-#define ROFF_CALLABLE (1 << 2) /* FIXME: test. */
+#define ROFF_NESTED (1 << 0)
+#define ROFF_PARSED (1 << 1)
+#define ROFF_CALLABLE (1 << 2)
};
struct roffnode {
@@ -286,11 +284,13 @@ roffparse(const struct md_args *args, struct md_mbuf *out,
break;
}
assert(ROFF_PRELUDE & tree->state);
- warnx("%s: text token `%s' in prelude (line %zu)",
+ warnx("%s: body token `%s' in prelude (line %zu)",
in->name, tokens[tokid].name, in->line);
return(0);
- default:
+ case (ROFF_COMMENT):
return(1);
+ default:
+ abort();
}
/*
@@ -411,25 +411,42 @@ roffnode_free(int tokid, struct rofftree *tree)
}
-static int dbg_lvl = 0; /* FIXME: de-globalise. */
+static int dbg_lvl = 0;
static void
dbg_enter(const struct md_args *args, int tokid)
{
int i;
+ static char buf[72];
assert(args);
if ( ! (args->dbg & MD_DBG_TREE))
return;
-
assert(tokid >= 0 && tokid <= ROFF_Max);
+ buf[0] = 0;
+
+ switch (tokens[tokid].type) {
+ case (ROFF_LAYOUT):
+ /* FALLTHROUGH */
+ case (ROFF_TEXT):
+ (void)strlcat(buf, "body: ", sizeof(buf));
+ break;
+ case (ROFF_TITLE):
+ (void)strlcat(buf, "prelude: ", sizeof(buf));
+ break;
+ default:
+ abort();
+ }
+
/* LINTED */
for (i = 0; i < dbg_lvl; i++)
- (void)printf(" ");
+ (void)strlcat(buf, " ", sizeof(buf));
+
+ (void)strlcat(buf, tokens[tokid].name, sizeof(buf));
- (void)printf("%s\n", tokens[tokid].name);
+ (void)printf("%s\n", buf);
if (ROFF_LAYOUT == tokens[tokid].type)
dbg_lvl++;
@@ -439,8 +456,6 @@ dbg_enter(const struct md_args *args, int tokid)
static void
dbg_leave(const struct md_args *args, int tokid)
{
- int i;
-
assert(args);
if ( ! (args->dbg & MD_DBG_TREE))
return;
@@ -449,14 +464,7 @@ dbg_leave(const struct md_args *args, int tokid)
assert(tokid >= 0 && tokid <= ROFF_Max);
assert(dbg_lvl > 0);
-
dbg_lvl--;
-
- /* LINTED */
- for (i = 0; i < dbg_lvl; i++)
- (void)printf(" ");
-
- (void)printf("%s\n", tokens[tokid].name);
}
@@ -467,7 +475,7 @@ roff_Dd(ROFFCALL_ARGS)
assert(ROFF_PRELUDE & tree->state);
if (ROFF_PRELUDE_Dt & tree->state ||
ROFF_PRELUDE_Dd & tree->state) {
- warnx("%s: bad prelude ordering (line %zu)",
+ warnx("%s: prelude `Dd' out-of-order (line %zu)",
in->name, in->line);
return(0);
}
@@ -487,7 +495,7 @@ roff_Dt(ROFFCALL_ARGS)
assert(ROFF_PRELUDE & tree->state);
if ( ! (ROFF_PRELUDE_Dd & tree->state) ||
(ROFF_PRELUDE_Dt & tree->state)) {
- warnx("%s: bad prelude ordering (line %zu)",
+ warnx("%s: prelude `Dt' out-of-order (line %zu)",
in->name, in->line);
return(0);
}
@@ -513,7 +521,7 @@ roff_Os(ROFFCALL_ARGS)
assert(ROFF_PRELUDE & tree->state);
if ( ! (ROFF_PRELUDE_Dt & tree->state) ||
! (ROFF_PRELUDE_Dd & tree->state)) {
- warnx("%s: bad prelude ordering (line %zu)",
+ warnx("%s: prelude `Os' out-of-order (line %zu)",
in->name, in->line);
return(0);
}
diff --git a/libmdocml.c b/libmdocml.c
index a38678d0..bb83e43a 100644
--- a/libmdocml.c
+++ b/libmdocml.c
@@ -27,12 +27,13 @@
#include "libmdocml.h"
#include "private.h"
-#define BUFFER_LINE BUFSIZ
+#define BUFFER_LINE BUFSIZ /* Default line-buffer size. */
static int md_run_enter(const struct md_args *,
struct md_mbuf *, struct md_rbuf *, void *);
static int md_run_leave(const struct md_args *, struct md_mbuf *,
struct md_rbuf *, int, void *);
+
static ssize_t md_buf_fill(struct md_rbuf *);
static int md_buf_flush(struct md_mbuf *);
@@ -170,46 +171,42 @@ md_run_enter(const struct md_args *args, struct md_mbuf *mbuf,
case (MD_HTML4_STRICT):
fp = md_line_html4_strict;
break;
- case (MD_DUMMY):
+ default:
fp = md_line_dummy;
break;
- default:
- abort();
}
- /* LINTED */
- for (pos = 0; ; ) {
- if (-1 == (sz = md_buf_fill(rbuf)))
- return(md_run_leave(args, mbuf, rbuf, -1, p));
- else if (0 == sz)
- break;
-
- for (i = 0; i < sz; i++) {
- if ('\n' == rbuf->buf[i]) {
- if ( ! (*fp)(args, mbuf, rbuf, line, pos, p))
- return(md_run_leave(args, mbuf, rbuf,
- -1, p));
- rbuf->line++;
- pos = 0;
- continue;
- }
+ pos = 0;
+again:
+ if (-1 == (sz = md_buf_fill(rbuf))) {
+ return(md_run_leave(args, mbuf, rbuf, -1, p));
+ } else if (0 == sz && 0 != pos) {
+ warnx("%s: no newline at end of file", rbuf->name);
+ return(md_run_leave(args, mbuf, rbuf, -1, p));
+ } else if (0 == sz)
+ return(md_run_leave(args, mbuf, rbuf, 0, p));
+
+ for (i = 0; i < sz; i++) {
+ if ('\n' != rbuf->buf[i]) {
if (pos < BUFFER_LINE) {
/* LINTED */
line[pos++] = rbuf->buf[i];
continue;
}
-
warnx("%s: line %zu too long",
rbuf->name, rbuf->line);
return(md_run_leave(args, mbuf, rbuf, -1, p));
}
- }
- if (0 != pos && ! (*fp)(args, mbuf, rbuf, line, pos, p))
- return(md_run_leave(args, mbuf, rbuf, -1, p));
+ if ( ! (*fp)(args, mbuf, rbuf, line, pos, p))
+ return(md_run_leave(args, mbuf, rbuf, -1, p));
+ rbuf->line++;
+ pos = 0;
+ }
- return(md_run_leave(args, mbuf, rbuf, 0, p));
+ goto again;
+ /* NOTREACHED */
}
diff --git a/libmdocml.h b/libmdocml.h
index c25602fa..139fb4b1 100644
--- a/libmdocml.h
+++ b/libmdocml.h
@@ -43,7 +43,7 @@ struct md_args {
union md_params params;/* Parameters for parser. */
enum md_type type; /* Type of parser. */
int dbg; /* Debug level. */
-#define MD_DBG_TREE (1 << 0)/* Print the parse tree to stdout. */
+#define MD_DBG_TREE 1
};
struct md_buf {
diff --git a/mdocml.c b/mdocml.c
index 6bd6d8cf..1ba70fb8 100644
--- a/mdocml.c
+++ b/mdocml.c
@@ -30,15 +30,16 @@
#include "libmdocml.h"
-#define BUFFER_IN_DEF BUFSIZ
-#define BUFFER_OUT_DEF BUFSIZ
+#define BUFFER_IN_DEF BUFSIZ /* See begin_bufs. */
+#define BUFFER_OUT_DEF BUFSIZ /* See begin_bufs. */
-static void usage(void);
-static int begin_io(const struct md_args *,
+static void usage(void);
+
+static int begin_io(const struct md_args *,
char *, char *);
-static int leave_io(const struct md_buf *,
+static int leave_io(const struct md_buf *,
const struct md_buf *, int);
-static int begin_bufs(const struct md_args *,
+static int begin_bufs(const struct md_args *,
struct md_buf *, struct md_buf *);
static int leave_bufs(const struct md_buf *,
const struct md_buf *, int);
@@ -55,11 +56,14 @@ main(int argc, char *argv[])
out = in = NULL;
- while (-1 != (c = getopt(argc, argv, "o:")))
+ while (-1 != (c = getopt(argc, argv, "vo:")))
switch (c) {
case ('o'):
out = optarg;
break;
+ case ('v'):
+ args.dbg++;
+ break;
default:
usage();
return(1);
@@ -72,12 +76,15 @@ main(int argc, char *argv[])
in = *argv++;
args.type = MD_HTML4_STRICT;
- args.dbg = MD_DBG_TREE;
return(begin_io(&args, out ? out : "-", in ? in : "-"));
}
+/*
+ * Close out file descriptors opened in begin_io. If the descriptor
+ * refers to stdin/stdout, then do nothing.
+ */
static int
leave_io(const struct md_buf *out,
const struct md_buf *in, int c)
@@ -101,6 +108,10 @@ leave_io(const struct md_buf *out,
}
+/*
+ * Open file descriptors or assign stdin/stdout, if dictated by the "-"
+ * token instead of a filename.
+ */
static int
begin_io(const struct md_args *args, char *out, char *in)
{
@@ -139,6 +150,9 @@ begin_io(const struct md_args *args, char *out, char *in)
}
+/*
+ * Free buffers allocated in begin_bufs.
+ */
static int
leave_bufs(const struct md_buf *out,
const struct md_buf *in, int c)
@@ -153,6 +167,10 @@ leave_bufs(const struct md_buf *out,
}
+/*
+ * Allocate buffers to the maximum of either the input file's blocksize
+ * or BUFFER_IN_DEF/BUFFER_OUT_DEF, which should be around BUFSIZE.
+ */
static int
begin_bufs(const struct md_args *args,
struct md_buf *out, struct md_buf *in)
@@ -196,5 +214,6 @@ usage(void)
{
extern char *__progname;
- (void)printf("usage: %s [-o outfile] [infile]\n", __progname);
+ (void)printf("usage: %s [-v] [-o outfile] [infile]\n",
+ __progname);
}