author | Olivier Brunel
<jjk@jjacky.com> 2022-12-25 08:12:33 UTC |
committer | Olivier Brunel
<jjk@jjacky.com> 2022-12-25 08:12:33 UTC |
parent | 0fefad679d266816c3cb32b784cf10d9ee5a675e |
main.c | +40 | -30 |
diff --git a/main.c b/main.c index 886bdb1..a94ba86 100644 --- a/main.c +++ b/main.c @@ -17,7 +17,6 @@ enum { ERR_MEM = -7, ERR_NOTSUPP = -8, ERR_INVALID = -9, - ERR_WRITE = -10, }; enum { @@ -39,25 +38,28 @@ struct ctx { } buf; }; -static int raw_text(FILE *f, const char *text, size_t size) +static int +raw_text(FILE *f, const char *text, size_t size) { - if (fwrite(text, 1, size, f) != size) return ERR_WRITE; + if (fwrite(text, 1, size, f) != size) return ERR_IO; else return 0; } #define raw_str(f,s) raw_text(f, s, strlen(s)) -#define raw_or_ret(f,s) if (raw_str(f, s) < 0) return ERR_WRITE +#define RAW_OR_RET(f,s) if (raw_str(f, s) < 0) return ERR_IO -static int raw_printf(FILE *f, const char *fmt, ...) +static int +raw_printf(FILE *f, const char *fmt, ...) { va_list va; va_start(va, fmt); int r = vfprintf(f, fmt, va); va_end(va); - return (r < 0) ? ERR_WRITE : 0; + return (r < 0) ? ERR_IO : 0; } -static int escape_text(FILE *f, const char *text, size_t size) +static int +escape_text(FILE *f, const char *text, size_t size) { size_t last = 0; for (size_t n = 0; n < size; ++n) { @@ -75,18 +77,19 @@ static int escape_text(FILE *f, const char *text, size_t size) case '"': if (!esc) esc = """; if (raw_text(f, text + last, n - last) < 0 || raw_str(f, esc) < 0) - return ERR_WRITE; + return ERR_IO; last = n + 1; break; } } if (raw_text(f, text + last, size - last) < 0) - return ERR_WRITE; + return ERR_IO; return 0; } -static int highlight_escape_text(FILE *f, const char *text, size_t size) +static int +highlight_escape_text(FILE *f, const char *text, size_t size) { const char *end = text + size; @@ -100,17 +103,18 @@ static int highlight_escape_text(FILE *f, const char *text, size_t size) || raw_str(f, "<span class=\"highlighted\">") < 0 || escape_text(f, open + 4, close - open - 4) < 0 || raw_str(f, "</span>") < 0) - return ERR_WRITE; + return ERR_IO; text = close + 5; size = end - text; } if (escape_text(f, text, size) < 0) - return ERR_WRITE ; + return ERR_IO ; return 0; } -static int attribute(FILE *f, MD_ATTRIBUTE *attr) +static int +attribute(FILE *f, MD_ATTRIBUTE *attr) { int n = 0; MD_SIZE l, cur = 0, size = attr->size; @@ -121,7 +125,7 @@ static int attribute(FILE *f, MD_ATTRIBUTE *attr) case MD_TEXT_NORMAL: case MD_TEXT_ENTITY: if (escape_text(f, attr->text, l) < 0) - return ERR_WRITE; + return ERR_IO; break; case MD_TEXT_NULLCHAR: @@ -141,7 +145,8 @@ static int attribute(FILE *f, MD_ATTRIBUTE *attr) } -static int enter_block(MD_BLOCKTYPE type, void *details, void *ctx_) +static int +enter_block(MD_BLOCKTYPE type, void *details, void *ctx_) { struct ctx *ctx = ctx_; FILE *f = ctx->out; @@ -247,7 +252,8 @@ static int enter_block(MD_BLOCKTYPE type, void *details, void *ctx_) return 0; } -static int leave_block(MD_BLOCKTYPE type, void *details, void *ctx_) +static int +leave_block(MD_BLOCKTYPE type, void *details, void *ctx_) { struct ctx *ctx = ctx_; FILE *f = ctx->out; @@ -292,18 +298,18 @@ static int leave_block(MD_BLOCKTYPE type, void *details, void *ctx_) const char *s = ctx->buf.str; size_t l = ctx->buf.len; - raw_or_ret(f, "<pre class=\"lineno\">"); + RAW_OR_RET(f, "<pre class=\"lineno\">"); for (int n = ctx->code.from; s; ++n) { const char *e = memchr(s, '\n', l); if (!e) break; l -= ++e - s; s = e; if (raw_printf(f, "%d\n", n) < 0) - return ERR_WRITE; + return ERR_IO; } - raw_or_ret(f, "</pre><code>"); + RAW_OR_RET(f, "</pre><code>"); } else { - raw_or_ret(f, "<pre>"); + RAW_OR_RET(f, "<pre>"); } int r; @@ -311,13 +317,13 @@ static int leave_block(MD_BLOCKTYPE type, void *details, void *ctx_) r = highlight_escape_text(f, ctx->buf.str, ctx->buf.len); else r = escape_text(f, ctx->buf.str, ctx->buf.len); - if (r < 0) return ERR_WRITE; + if (r < 0) return ERR_IO; if (ctx->code.flags & CODE_LINES) r = raw_str(f, "</code>"); else r = raw_str(f, "</pre>"); - if (r < 0) return ERR_WRITE; + if (r < 0) return ERR_IO; free(ctx->buf.str); ctx->code.flags = 0; @@ -343,7 +349,8 @@ static int leave_block(MD_BLOCKTYPE type, void *details, void *ctx_) return 0; } -static int enter_span(MD_SPANTYPE type, void *details, void *ctx_) +static int +enter_span(MD_SPANTYPE type, void *details, void *ctx_) { struct ctx *ctx = ctx_; FILE *f = ctx->out; @@ -360,12 +367,12 @@ static int enter_span(MD_SPANTYPE type, void *details, void *ctx_) { MD_SPAN_A_DETAIL *d = details; - raw_or_ret(f, "<a href=\""); + RAW_OR_RET(f, "<a href=\""); r = attribute(f, &d->href); if (r < 0) return r; if (d->title.text) { - raw_or_ret(f, "\" title=\""); + RAW_OR_RET(f, "\" title=\""); r = attribute(f, &d->title); if (r < 0) return r; } @@ -395,7 +402,8 @@ static int enter_span(MD_SPANTYPE type, void *details, void *ctx_) return 0; } -static int leave_span(MD_SPANTYPE type, void *details, void *ctx_) +static int +leave_span(MD_SPANTYPE type, void *details, void *ctx_) { struct ctx *ctx = ctx_; FILE *f = ctx->out; @@ -432,7 +440,8 @@ static int leave_span(MD_SPANTYPE type, void *details, void *ctx_) return 0; } -static int text(MD_TEXTTYPE type, const MD_CHAR *text, MD_SIZE size, void *ctx_) +static int +text(MD_TEXTTYPE type, const MD_CHAR *text, MD_SIZE size, void *ctx_) { struct ctx *ctx = ctx_; FILE *f = ctx->out; @@ -491,12 +500,12 @@ static int text(MD_TEXTTYPE type, const MD_CHAR *text, MD_SIZE size, void *ctx_) int i, n; for (i = 0, n = sizeof(tags) / sizeof(*tags); i < n; ++i) { if (size == tags[i].len && !strncmp(text, tags[i].name, tags[i].len)) { - raw_or_ret(f, (tags[i].repl) ? tags[i].repl : tags[i].name); + RAW_OR_RET(f, (tags[i].repl) ? tags[i].repl : tags[i].name); break; } } if (i == n && escape_text(f, text, size) < 0) - return ERR_WRITE; + return ERR_IO; break; case MD_TEXT_LATEXMATH: @@ -507,7 +516,8 @@ static int text(MD_TEXTTYPE type, const MD_CHAR *text, MD_SIZE size, void *ctx_) } -int main (int argc, char *argv[]) +int +main (int argc, char *argv[]) { if (argc != 2) return -ERR_USAGE; const char *file = argv[1];