Fixed debugging messages.
-/* $Id: html4_strict.c,v 1.3 2008/11/23 22:30:53 kristaps Exp $ */
+/* $Id: html4_strict.c,v 1.4 2008/11/23 23:12:47 kristaps Exp $ */
/*
* Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>
*
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;
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 {
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();
}
/*
}
-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++;
static void
dbg_leave(const struct md_args *args, int tokid)
{
- int i;
-
assert(args);
if ( ! (args->dbg & MD_DBG_TREE))
return;
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);
}
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);
}
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);
}
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);
}
-/* $Id: libmdocml.c,v 1.6 2008/11/23 22:30:53 kristaps Exp $ */
+/* $Id: libmdocml.c,v 1.7 2008/11/23 23:12:47 kristaps Exp $ */
/*
* Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>
*
#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 *);
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 */
}
-/* $Id: libmdocml.h,v 1.5 2008/11/23 22:30:53 kristaps Exp $ */
+/* $Id: libmdocml.h,v 1.6 2008/11/23 23:12:47 kristaps Exp $ */
/*
* Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>
*
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 {
-/* $Id: mdocml.c,v 1.8 2008/11/23 22:30:53 kristaps Exp $ */
+/* $Id: mdocml.c,v 1.9 2008/11/23 23:12:47 kristaps Exp $ */
/*
* Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>
*
#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);
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);
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)
}
+/*
+ * 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)
{
}
+/*
+ * Free buffers allocated in begin_bufs.
+ */
static int
leave_bufs(const struct md_buf *out,
const struct md_buf *in, int c)
}
+/*
+ * 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)
{
extern char *__progname;
- (void)printf("usage: %s [-o outfile] [infile]\n", __progname);
+ (void)printf("usage: %s [-v] [-o outfile] [infile]\n",
+ __progname);
}