From 9aa8c8acf1b851986cf67650797d456cfa6ad1a5 Mon Sep 17 00:00:00 2001 From: Devin Torres Date: Sat, 18 Oct 2014 18:33:34 -0500 Subject: Use a stricter subset of C --- Makefile | 5 +++- src/buffer.c | 42 +++++++++++++++++++++++++++- src/buffer.h | 19 +++---------- src/document.c | 74 +++++++++++++++++++++++++++++++++----------------- src/html.c | 36 ++++++++++++++++-------- src/html_smartypants.c | 2 +- 6 files changed, 124 insertions(+), 54 deletions(-) diff --git a/Makefile b/Makefile index 56e1597..ae5f92c 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -CFLAGS = -g -O3 -Wall -Wextra -Wno-unused-parameter -Isrc +CFLAGS = -g -O3 -ansi -pedantic -Wall -Wextra -Wno-unused-parameter -Isrc ifneq ($(OS),Windows_NT) CFLAGS += -fPIC @@ -63,3 +63,6 @@ clean: %.o: %.c $(CC) $(CFLAGS) -c -o $@ $< + +src/html_blocks.o: src/html_blocks.c + $(CC) $(CFLAGS) -Wno-static-in-inline -c -o $@ $< diff --git a/src/buffer.c b/src/buffer.c index daba5fd..2ca0fd1 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -5,6 +5,45 @@ #include #include +void * +hoedown_malloc(size_t size) +{ + void *ret = malloc(size); + + if (!ret) { + fprintf(stderr, "Allocation failed.\n"); + abort(); + } + + return ret; +} + +void * +hoedown_calloc(size_t nmemb, size_t size) +{ + void *ret = calloc(nmemb, size); + + if (!ret) { + fprintf(stderr, "Allocation failed.\n"); + abort(); + } + + return ret; +} + +void * +hoedown_realloc(void *ptr, size_t size) +{ + void *ret = realloc(ptr, size); + + if (!ret) { + fprintf(stderr, "Allocation failed.\n"); + abort(); + } + + return ret; +} + void hoedown_buffer_init( hoedown_buffer *buf, @@ -133,9 +172,10 @@ hoedown_buffer_eqs(const hoedown_buffer *buf, const char *str) int hoedown_buffer_prefix(const hoedown_buffer *buf, const char *prefix) { - assert(buf && buf->unit); size_t i; + assert(buf && buf->unit); + for (i = 0; i < buf->size; ++i) { if (prefix[i] == 0) return 0; diff --git a/src/buffer.h b/src/buffer.h index 6d42385..fcda83c 100644 --- a/src/buffer.h +++ b/src/buffer.h @@ -37,19 +37,8 @@ struct hoedown_buffer { hoedown_free_callback data_free; hoedown_free_callback buffer_free; }; -typedef struct hoedown_buffer hoedown_buffer; -/* malloc / realloc / calloc wrappers */ -#define HOEDOWN_ALLOC_WRAPPER(sig, call) \ - static inline void *hoedown_##sig __attribute__ ((malloc)); \ - static inline void *hoedown_##sig { \ - void *ret = call; \ - if (!ret) { \ - fprintf(stderr, "Allocation failed.\n"); \ - abort(); \ - } \ - return ret; \ - } +typedef struct hoedown_buffer hoedown_buffer; /************* @@ -57,9 +46,9 @@ typedef struct hoedown_buffer hoedown_buffer; *************/ /* allocation wrappers */ -HOEDOWN_ALLOC_WRAPPER(malloc(size_t size), malloc(size)); -HOEDOWN_ALLOC_WRAPPER(calloc(size_t nmemb, size_t size), calloc(nmemb, size)); -HOEDOWN_ALLOC_WRAPPER(realloc(void *ptr, size_t size), realloc(ptr, size)); +void *hoedown_malloc(size_t size) __attribute__ ((malloc)); +void *hoedown_calloc(size_t nmemb, size_t size) __attribute__ ((malloc)); +void *hoedown_realloc(void *ptr, size_t size) __attribute__ ((malloc)); /* hoedown_buffer_init: initialize a buffer with custom allocators */ void hoedown_buffer_init( diff --git a/src/document.c b/src/document.c index f2ce139..3ce4416 100644 --- a/src/document.c +++ b/src/document.c @@ -132,7 +132,7 @@ struct hoedown_document { * HELPER FUNCTIONS * ***************************/ -static inline hoedown_buffer * +static hoedown_buffer * newbuf(hoedown_document *doc, int type) { static const size_t buf_size[2] = {256, 64}; @@ -151,7 +151,7 @@ newbuf(hoedown_document *doc, int type) return work; } -static inline void +static void popbuf(hoedown_document *doc, int type) { doc->work_bufs[type].size--; @@ -320,14 +320,14 @@ free_footnote_list(struct footnote_list *list, int free_refs) * should instead extract an Unicode codepoint from * this character and check for space properties. */ -static inline int +static int _isspace(int c) { return c == ' ' || c == '\n'; } /* is_empty_all: verify that all the data is spacing */ -static inline int +static int is_empty_all(const uint8_t *data, size_t size) { size_t i = 0; @@ -339,7 +339,7 @@ is_empty_all(const uint8_t *data, size_t size) * Replace all spacing characters in data with spaces. As a special * case, this collapses a newline with the previous space, if possible. */ -static inline void +static void replace_spacing(hoedown_buffer *ob, const uint8_t *data, size_t size) { size_t i = 0, mark; @@ -717,22 +717,30 @@ parse_emph3(hoedown_buffer *ob, hoedown_document *doc, uint8_t *data, size_t siz static size_t parse_math(hoedown_buffer *ob, hoedown_document *doc, uint8_t *data, size_t offset, size_t size, const char *end, size_t delimsz, int displaymode) { + hoedown_buffer text = { NULL, 0, 0, 0, NULL, NULL, NULL }; size_t i = delimsz; - if (!doc->md.math) return 0; + + if (!doc->md.math) + return 0; /* find ending delimiter */ while (1) { - while (i < size && data[i] != (uint8_t)end[0]) i++; - if (i >= size) return 0; + while (i < size && data[i] != (uint8_t)end[0]) + i++; + + if (i >= size) + return 0; if (!is_escaped(data, i) && !(i + delimsz > size) && memcmp(data + i, end, delimsz) == 0) break; + i++; } /* prepare buffers */ - hoedown_buffer text = { data + delimsz, i - delimsz, 0, 0, NULL, NULL, NULL }; + text.data = data + delimsz; + text.size = i - delimsz; /* if this is a $$ and MATH_EXPLICIT is not active, * guess whether displaymode should be enabled from the context */ @@ -743,6 +751,7 @@ parse_math(hoedown_buffer *ob, hoedown_document *doc, uint8_t *data, size_t offs /* call callback */ if (doc->md.math(ob, &text, displaymode, &doc->data)) return i; + return 0; } @@ -804,6 +813,7 @@ char_linebreak(hoedown_buffer *ob, hoedown_document *doc, uint8_t *data, size_t static size_t char_codespan(hoedown_buffer *ob, hoedown_document *doc, uint8_t *data, size_t offset, size_t size) { + hoedown_buffer work = { NULL, 0, 0, 0, NULL, NULL, NULL }; size_t end, nb = 0, i, f_begin, f_end; /* counting the number of backticks in the delimiter */ @@ -831,7 +841,9 @@ char_codespan(hoedown_buffer *ob, hoedown_document *doc, uint8_t *data, size_t o /* real code span */ if (f_begin < f_end) { - hoedown_buffer work = { data + f_begin, f_end - f_begin, 0, 0, NULL, NULL, NULL }; + work.data = data + f_begin; + work.size = f_end - f_begin; + if (!doc->md.codespan(ob, &work, &doc->data)) end = 0; } else { @@ -954,11 +966,14 @@ char_entity(hoedown_buffer *ob, hoedown_document *doc, uint8_t *data, size_t off static size_t char_langle_tag(hoedown_buffer *ob, hoedown_document *doc, uint8_t *data, size_t offset, size_t size) { + hoedown_buffer work = { NULL, 0, 0, 0, NULL, NULL, NULL }; hoedown_autolink_type altype = HOEDOWN_AUTOLINK_NONE; size_t end = tag_length(data, size, &altype); - hoedown_buffer work = { data, end, 0, 0, NULL, NULL, NULL }; int ret = 0; + work.data = data; + work.size = end; + if (end > 2) { if (doc->md.autolink && altype != HOEDOWN_AUTOLINK_NONE) { hoedown_buffer *u_link = newbuf(doc, BUFFER_SPAN); @@ -1618,9 +1633,11 @@ parse_htmlblock(hoedown_buffer *ob, hoedown_document *doc, uint8_t *data, size_t static size_t parse_paragraph(hoedown_buffer *ob, hoedown_document *doc, uint8_t *data, size_t size) { + hoedown_buffer work = { NULL, 0, 0, 0, NULL, NULL, NULL }; size_t i = 0, end = 0; int level = 0; - hoedown_buffer work = { data, 0, 0, 0, NULL, NULL, NULL }; + + work.data = data; while (i < size) { for (end = i + 1; end < size && data[end - 1] != '\n'; end++) /* empty */; @@ -1696,23 +1713,27 @@ parse_paragraph(hoedown_buffer *ob, hoedown_document *doc, uint8_t *data, size_t static size_t parse_fencedcode(hoedown_buffer *ob, hoedown_document *doc, uint8_t *data, size_t size) { + hoedown_buffer text = { 0, 0, 0, 0, NULL, NULL, NULL }; + hoedown_buffer lang = { 0, 0, 0, 0, NULL, NULL, NULL }; size_t i = 0, text_start, line_start; size_t w, w2; size_t width, width2; uint8_t chr, chr2; - hoedown_buffer text = { 0, 0, 0, 0, NULL, NULL, NULL }; - hoedown_buffer lang = { 0, 0, 0, 0, NULL, NULL, NULL }; - // parse codefence line - while (i < size && data[i] != '\n') i++; + /* parse codefence line */ + while (i < size && data[i] != '\n') + i++; + w = parse_codefence(data, i, &lang, &width, &chr); - if (!w) return 0; + if (!w) + return 0; - // search for end + /* search for end */ i++; text_start = i; while ((line_start = i) < size) { - while (i < size && data[i] != '\n') i++; + while (i < size && data[i] != '\n') + i++; w2 = is_codefence(data + line_start, i - line_start, &width2, &chr2); if (w == w2 && width == width2 && chr == chr2 && @@ -1721,6 +1742,7 @@ parse_fencedcode(hoedown_buffer *ob, hoedown_document *doc, uint8_t *data, size_ i++; } + text.data = data + text_start; text.size = line_start - text_start; @@ -2009,7 +2031,7 @@ parse_footnote_list(hoedown_buffer *ob, hoedown_document *doc, struct footnote_l /* htmlblock_is_end • check for end of HTML block : ( *)\n */ /* returns tag length on match, 0 otherwise */ /* assumes data starts with "<" */ -static inline size_t +static size_t htmlblock_is_end( const char *tag, size_t tag_len, @@ -2087,9 +2109,11 @@ htmlblock_find_end_strict( static size_t parse_htmlblock(hoedown_buffer *ob, hoedown_document *doc, uint8_t *data, size_t size, int do_render) { - size_t i, j = 0, tag_end; + hoedown_buffer work = { NULL, 0, 0, 0, NULL, NULL, NULL }; + size_t i, j = 0, tag_len, tag_end; const char *curtag = NULL; - hoedown_buffer work = { data, 0, 0, 0, NULL, NULL, NULL }; + + work.data = data; /* identification of the opening tag */ if (size < 2 || data[0] != '<') @@ -2148,7 +2172,7 @@ parse_htmlblock(hoedown_buffer *ob, hoedown_document *doc, uint8_t *data, size_t } /* looking for a matching closing tag in strict mode */ - size_t tag_len = strlen(curtag); + tag_len = strlen(curtag); tag_end = htmlblock_find_end_strict(curtag, tag_len, doc, data, size); /* if not found, trying a second pass looking for indented match */ @@ -2177,7 +2201,7 @@ parse_table_row( hoedown_table_flags *col_data, hoedown_table_flags header_flag) { - size_t i = 0, col; + size_t i = 0, col, len; hoedown_buffer *row_work = 0; if (!doc->md.table_cell || !doc->md.table_row) @@ -2199,7 +2223,7 @@ parse_table_row( cell_start = i; - size_t len = find_emph_char(data + i, size - i, '|'); + len = find_emph_char(data + i, size - i, '|'); i += len ? len : size - i; cell_end = i - 1; diff --git a/src/html.c b/src/html.c index d24bf8e..ddebff7 100644 --- a/src/html.c +++ b/src/html.c @@ -42,12 +42,12 @@ hoedown_html_is_tag(const uint8_t *data, size_t size, const char *tagname) return HOEDOWN_HTML_TAG_NONE; } -static inline void escape_html(hoedown_buffer *ob, const uint8_t *source, size_t length) +static void escape_html(hoedown_buffer *ob, const uint8_t *source, size_t length) { hoedown_escape_html(ob, source, length, 0); } -static inline void escape_href(hoedown_buffer *ob, const uint8_t *source, size_t length) +static void escape_href(hoedown_buffer *ob, const uint8_t *source, size_t length) { hoedown_escape_href(ob, source, length); } @@ -326,15 +326,25 @@ static void rndr_raw_block(hoedown_buffer *ob, const hoedown_buffer *text, const hoedown_renderer_data *data) { size_t org, sz; - if (!text) return; - //FIXME: do we *really* need to trim the HTML? - //how does that make a difference? + + if (!text) + return; + + /* FIXME: Do we *really* need to trim the HTML? How does that make a difference? */ sz = text->size; - while (sz > 0 && text->data[sz - 1] == '\n') sz--; + while (sz > 0 && text->data[sz - 1] == '\n') + sz--; + org = 0; - while (org < sz && text->data[org] == '\n') org++; - if (org >= sz) return; - if (ob->size) hoedown_buffer_putc(ob, '\n'); + while (org < sz && text->data[org] == '\n') + org++; + + if (org >= sz) + return; + + if (ob->size) + hoedown_buffer_putc(ob, '\n'); + hoedown_buffer_put(ob, text->data + org, sz - org); hoedown_buffer_putc(ob, '\n'); } @@ -591,8 +601,12 @@ toc_link(hoedown_buffer *ob, const hoedown_buffer *content, const hoedown_buffer static void toc_finalize(hoedown_buffer *ob, int inline_render, const hoedown_renderer_data *data) { - if (inline_render) return; - hoedown_html_renderer_state *state = data->opaque; + hoedown_html_renderer_state *state; + + if (inline_render) + return; + + state = data->opaque; while (state->toc_data.current_level > 0) { HOEDOWN_BUFPUTSL(ob, "\n\n"); diff --git a/src/html_smartypants.c b/src/html_smartypants.c index 9f9dcf0..bbe4fc5 100644 --- a/src/html_smartypants.c +++ b/src/html_smartypants.c @@ -60,7 +60,7 @@ static const uint8_t smartypants_cb_chars[UINT8_MAX+1] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }; -static inline int +static int word_boundary(uint8_t c) { return c == 0 || isspace(c) || ispunct(c); -- cgit v1.2.1