=================================================================== RCS file: /cvs/mandoc/Attic/validate.c,v retrieving revision 1.2 retrieving revision 1.6 diff -u -p -r1.2 -r1.6 --- mandoc/Attic/validate.c 2008/11/29 16:11:42 1.2 +++ mandoc/Attic/validate.c 2008/11/30 20:53:34 1.6 @@ -1,4 +1,4 @@ -/* $Id: validate.c,v 1.2 2008/11/29 16:11:42 kristaps Exp $ */ +/* $Id: validate.c,v 1.6 2008/11/30 20:53:34 kristaps Exp $ */ /* * Copyright (c) 2008 Kristaps Dzonsons * @@ -29,30 +29,39 @@ #include "private.h" #define INDENT 4 +#define COLUMNS 72 #ifdef __linux__ /* FIXME */ #define strlcat strncat #endif +enum md_tok { + MD_BLKIN, + MD_BLKOUT, + MD_IN, + MD_OUT, + MD_TEXT +}; + struct md_valid { const struct md_args *args; const struct md_rbuf *rbuf; + struct md_mbuf *mbuf; struct rofftree *tree; - size_t indent; size_t pos; - + enum md_tok last; int flags; -#define MD_LITERAL (1 << 0) +#define MD_LITERAL (1 << 0) /* FIXME */ }; static void roffmsg(void *arg, enum roffmsg, const char *, const char *, char *); static int roffhead(void *); static int rofftail(void *); -static int roffin(void *, int, int *, char **); -static int roffdata(void *, char *); +static int roffin(void *, int, int, int *, char **); +static int roffdata(void *, int, char *); static int roffout(void *, int); static int roffblkin(void *, int, int *, char **); static int roffblkout(void *, int); @@ -60,16 +69,38 @@ static int roffspecial(void *, int); static int mbuf_newline(struct md_valid *); static int mbuf_indent(struct md_valid *); -static int mbuf_data(struct md_valid *, char *); +static int mbuf_data(struct md_valid *, int, char *); +static int mbuf_putstring(struct md_valid *, + const char *); +static int mbuf_nputstring(struct md_valid *, + const char *, size_t); static int +mbuf_putstring(struct md_valid *p, const char *buf) +{ + + return(mbuf_nputstring(p, buf, strlen(buf))); +} + + +static int +mbuf_nputstring(struct md_valid *p, const char *buf, size_t sz) +{ + + p->pos += sz; + return(md_buf_puts(p->mbuf, buf, sz)); +} + + +static int mbuf_indent(struct md_valid *p) { size_t i; assert(p->pos == 0); + /* LINTED */ for (i = 0; i < MIN(p->indent, INDENT); i++) if ( ! md_buf_putstring(p->mbuf, " ")) return(0); @@ -92,7 +123,7 @@ mbuf_newline(struct md_valid *p) static int -mbuf_data(struct md_valid *p, char *buf) +mbuf_data(struct md_valid *p, int space, char *buf) { size_t sz; char *bufp; @@ -100,8 +131,13 @@ mbuf_data(struct md_valid *p, char *buf) assert(p->mbuf); assert(0 != p->indent); + /* + * FIXME: punctuation/no-space stuff shouldn't have a newline + * before it. + */ + if (MD_LITERAL & p->flags) - return(md_buf_putstring(p->mbuf, buf)); + return(mbuf_putstring(p, buf)); while (*buf) { while (*buf && isspace(*buf)) @@ -117,42 +153,32 @@ mbuf_data(struct md_valid *p, char *buf) if (0 != *buf) *buf++ = 0; - /* Process word. */ - sz = strlen(bufp); if (0 == p->pos) { if ( ! mbuf_indent(p)) return(0); - if ( ! md_buf_putstring(p->mbuf, bufp)) + if ( ! mbuf_nputstring(p, bufp, sz)) return(0); - - if (p->indent * INDENT + sz >= 72) { + if (p->indent * INDENT + sz >= COLUMNS) { if ( ! mbuf_newline(p)) return(0); continue; } - - if ( ! md_buf_putchar(p->mbuf, ' ')) - return(0); - - p->pos += sz + 1; continue; } - if (sz + p->pos >= 72) { + if (sz + p->pos >= COLUMNS) { if ( ! mbuf_newline(p)) return(0); if ( ! mbuf_indent(p)) return(0); - } + } else if (space) + if ( ! mbuf_nputstring(p, " ", 1)) + return(0); - if ( ! md_buf_putstring(p->mbuf, bufp)) + if ( ! mbuf_nputstring(p, bufp, sz)) return(0); - if ( ! md_buf_putchar(p->mbuf, ' ')) - return(0); - - p->pos += sz + 1; } return(1); @@ -227,13 +253,14 @@ roffhead(void *arg) assert(arg); p = (struct md_valid *)arg; - if ( ! md_buf_putstring(p->mbuf, "BEGIN")) + if ( ! mbuf_putstring(p, "\n")) return(0); - p->indent++; - if ( ! mbuf_newline(p)) + if ( ! mbuf_nputstring(p, "", 6)) return(0); - return(1); + p->indent++; + return(mbuf_newline(p)); } @@ -248,12 +275,13 @@ rofftail(void *arg) if (0 != p->pos && ! mbuf_newline(p)) return(0); - if ( ! md_buf_putstring(p->mbuf, "END\n")) + if ( ! mbuf_nputstring(p, "", 7)) return(0); - return(1); + return(mbuf_newline(p)); } +/* ARGSUSED */ static int roffspecial(void *arg, int tok) { @@ -266,6 +294,7 @@ static int roffblkin(void *arg, int tok, int *argc, char **argv) { struct md_valid *p; + int i; assert(arg); p = (struct md_valid *)arg; @@ -278,11 +307,25 @@ roffblkin(void *arg, int tok, int *argc, char **argv) } else if ( ! mbuf_indent(p)) return(0); - if ( ! md_buf_putchar(p->mbuf, '<')) + if ( ! mbuf_nputstring(p, "<", 1)) return(0); - if ( ! md_buf_putstring(p->mbuf, toknames[tok])) + if ( ! mbuf_putstring(p, toknames[tok])) return(0); - if ( ! md_buf_putchar(p->mbuf, '>')) + + for (i = 0; ROFF_ARGMAX != argc[i]; i++) { + if ( ! mbuf_nputstring(p, " ", 1)) + return(0); + if ( ! mbuf_putstring(p, tokargnames[argc[i]])) + return(0); + if ( ! mbuf_nputstring(p, "=\"", 2)) + return(0); + if ( ! mbuf_putstring(p, argv[i] ? argv[i] : "true")) + return(0); + if ( ! mbuf_nputstring(p, "\"", 1)) + return(0); + } + + if ( ! mbuf_nputstring(p, ">", 1)) return(0); if ( ! mbuf_newline(p)) return(0); @@ -310,11 +353,11 @@ roffblkout(void *arg, int tok) } else if ( ! mbuf_indent(p)) return(0); - if ( ! md_buf_putstring(p->mbuf, "mbuf, toknames[tok])) + if ( ! mbuf_putstring(p, toknames[tok])) return(0); - if ( ! md_buf_putstring(p->mbuf, ">")) + if ( ! mbuf_nputstring(p, ">", 1)) return(0); if ( ! mbuf_newline(p)) return(0); @@ -324,9 +367,10 @@ roffblkout(void *arg, int tok) static int -roffin(void *arg, int tok, int *argcp, char **argvp) +roffin(void *arg, int tok, int space, int *argc, char **argv) { struct md_valid *p; + int i; assert(arg); p = (struct md_valid *)arg; @@ -334,16 +378,31 @@ roffin(void *arg, int tok, int *argcp, char **argvp) if (0 == p->pos && ! mbuf_indent(p)) return(0); - if ( ! md_buf_putstring(p->mbuf, "<")) + /* + * FIXME: put into a buffer before writing (check line length). + */ + + if (space && ! mbuf_nputstring(p, " ", 1)) return(0); - if ( ! md_buf_putstring(p->mbuf, toknames[tok])) + if ( ! mbuf_nputstring(p, "<", 1)) return(0); - if ( ! md_buf_putstring(p->mbuf, ">")) + if ( ! mbuf_putstring(p, toknames[tok])) return(0); - p->pos += strlen(toknames[tok]) + 2; + for (i = 0; ROFF_ARGMAX != argc[i]; i++) { + if ( ! mbuf_nputstring(p, " ", 1)) + return(0); + if ( ! mbuf_putstring(p, tokargnames[argc[i]])) + return(0); + if ( ! mbuf_nputstring(p, "=\"", 2)) + return(0); + if ( ! mbuf_putstring(p, argv[i] ? argv[i] : "true")) + return(0); + if ( ! mbuf_nputstring(p, "\"", 1)) + return(0); + } - return(1); + return(mbuf_nputstring(p, ">", 1)); } @@ -358,20 +417,14 @@ roffout(void *arg, int tok) if (0 == p->pos && ! mbuf_indent(p)) return(0); - if ( ! md_buf_putstring(p->mbuf, "mbuf, toknames[tok])) + if ( ! mbuf_putstring(p, toknames[tok])) return(0); - if ( ! md_buf_putstring(p->mbuf, "> ")) - return(0); - - p->pos += strlen(toknames[tok]) + 3; - - return(1); + return(mbuf_nputstring(p, ">", 1)); } - static void roffmsg(void *arg, enum roffmsg lvl, const char *buf, const char *pos, char *msg) @@ -396,8 +449,9 @@ roffmsg(void *arg, enum roffmsg lvl, } if (pos) - (void)fprintf(stderr, "%s:%zu: %s: %s\n", - p->rbuf->name, p->rbuf->line, level, msg); + (void)fprintf(stderr, "%s:%zu: %s: %s (column %zu)\n", + p->rbuf->name, p->rbuf->line, level, + msg, pos - buf); else (void)fprintf(stderr, "%s: %s: %s\n", p->rbuf->name, level, msg); @@ -406,11 +460,11 @@ roffmsg(void *arg, enum roffmsg lvl, static int -roffdata(void *arg, char *buf) +roffdata(void *arg, int space, char *buf) { struct md_valid *p; assert(arg); p = (struct md_valid *)arg; - return(mbuf_data(p, buf)); + return(mbuf_data(p, space, buf)); }