smu

Simple markup processor
git clone https://git.sinitax.com/codemadness/smu
Log | Files | Refs | README | LICENSE | Upstream | sfeed.txt

commit cb1471bace36cbe8097e56367f0d07d04e553217
parent dc715a83638a2c710c99718d9ae7a95478913395
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date:   Tue, 11 May 2021 02:13:42 +0200

code-style pass #1, also using sizeof(char) is retarded

Diffstat:
Msmu.c | 360+++++++++++++++++++++++++++++++++++++++++++------------------------------------
1 file changed, 196 insertions(+), 164 deletions(-)

diff --git a/smu.c b/smu.c @@ -11,7 +11,7 @@ #endif #define LENGTH(x) sizeof(x)/sizeof(x[0]) -#define ADDC(b,i) if(i % BUFSIZ == 0) { b = realloc(b, (i + BUFSIZ) * sizeof(char)); if(!b) eprint("malloc"); } b[i] +#define ADDC(b,i) if (i % BUFSIZ == 0) { b = realloc(b, (i + BUFSIZ)); if (!b) eprint("malloc"); } b[i] typedef int (*Parser)(const char *, const char *, int); typedef struct { @@ -95,24 +95,27 @@ static const char *insert[][2] = { }; void -eprint(const char *format, ...) { +eprint(const char *format, ...) +{ va_list ap; va_start(ap, format); vfprintf(stderr, format, ap); va_end(ap); - exit(EXIT_FAILURE); + exit(1); } int -doamp(const char *begin, const char *end, int newblock) { +doamp(const char *begin, const char *end, int newblock) +{ const char *p; - if(*begin != '&') + if (*begin != '&') return 0; - if(!nohtml) { - for(p = begin + 1; p != end && !strchr("; \\\n\t", *p); p++); - if(p == end || *p == ';') + if (!nohtml) { + for (p = begin + 1; p != end && !strchr("; \\\n\t", *p); p++) + ; + if (p == end || *p == ';') return 0; } fputs("&amp;", stdout); @@ -120,21 +123,21 @@ doamp(const char *begin, const char *end, int newblock) { } int -dogtlt(const char *begin, const char *end, int newblock) { +dogtlt(const char *begin, const char *end, int newblock) +{ int brpos; char c; - if(nohtml || begin + 1 >= end) + if (nohtml || begin + 1 >= end) return 0; brpos = begin[1] == '>'; - if(!brpos && *begin != '<') + if (!brpos && *begin != '<') return 0; c = begin[brpos ? 0 : 1]; - if(!brpos && (c < 'a' || c > 'z') && (c < 'A' || c > 'Z')) { + if (!brpos && (c < 'a' || c > 'z') && (c < 'A' || c > 'Z')) { fputs("&lt;", stdout); return 1; - } - else if(brpos && (c < 'a' || c > 'z') && (c < 'A' || c > 'Z') && !strchr("/\"'",c)) { + } else if (brpos && (c < 'a' || c > 'z') && (c < 'A' || c > 'Z') && !strchr("/\"'", c)) { fprintf(stdout, "%c&gt;",c); return 2; } @@ -142,104 +145,106 @@ dogtlt(const char *begin, const char *end, int newblock) { } int -docomment(const char *begin, const char *end, int newblock) { +docomment(const char *begin, const char *end, int newblock) +{ char *p; - if(nohtml || strncmp("<!--", begin, 4)) + if (nohtml || strncmp("<!--", begin, 4)) return 0; p = strstr(begin, "-->"); - if(!p || p + 3 >= end) + if (!p || p + 3 >= end) return 0; fprintf(stdout, "%.*s\n", (int)(p + 3 - begin), begin); return (p + 3 - begin) * (newblock ? -1 : 1); } int -dohtml(const char *begin, const char *end, int newblock) { +dohtml(const char *begin, const char *end, int newblock) +{ const char *p, *tag, *tagend; - if(nohtml || begin + 2 >= end) + if (nohtml || begin + 2 >= end) return 0; p = begin; - if(p[0] != '<' || !isalpha(p[1])) + if (p[0] != '<' || !isalpha(p[1])) return 0; p++; tag = p; - for(; isalnum(*p) && p < end; p++); + for (; isalnum(*p) && p < end; p++) + ; tagend = p; - if(p > end || tag == tagend) + if (p > end || tag == tagend) return 0; - while((p = strstr(p, "</")) && p < end) { + while ((p = strstr(p, "</")) && p < end) { p += 2; - if(strncmp(p, tag, tagend - tag) == 0 && p[tagend - tag] == '>') { + if (strncmp(p, tag, tagend - tag) == 0 && p[tagend - tag] == '>') { p++; - fwrite(begin, sizeof(char), p - begin + tagend - tag + 1, stdout); + fwrite(begin, 1, p - begin + tagend - tag + 1, stdout); return p - begin + tagend - tag + 1; } } - p = strchr(tagend, '>'); - if(p) { - fwrite(begin, sizeof(char), p - begin + 2, stdout); + if ((p = strchr(tagend, '>'))) { + fwrite(begin, 1, p - begin + 2, stdout); return p - begin + 2; - } - else + } else { return 0; + } } int -dolineprefix(const char *begin, const char *end, int newblock) { +dolineprefix(const char *begin, const char *end, int newblock) +{ unsigned int i, j, l; char *buffer; const char *p; - if(newblock) + if (newblock) p = begin; - else if(*begin == '\n') + else if (*begin == '\n') p = begin + 1; else return 0; - for(i = 0; i < LENGTH(lineprefix); i++) { + for (i = 0; i < LENGTH(lineprefix); i++) { l = strlen(lineprefix[i].search); - if(end - p < l) + if (end - p < l) continue; - if(strncmp(lineprefix[i].search, p, l)) + if (strncmp(lineprefix[i].search, p, l)) continue; - if(*begin == '\n') + if (*begin == '\n') fputc('\n', stdout); fputs(lineprefix[i].before, stdout); - if(lineprefix[i].search[l-1] == '\n') { + if (lineprefix[i].search[l-1] == '\n') { fputc('\n', stdout); return l - 1; } - if(!(buffer = malloc(BUFSIZ))) + if (!(buffer = malloc(BUFSIZ))) eprint("malloc"); buffer[0] = '\0'; /* Collect lines into buffer while they start with the prefix */ j = 0; - while((strncmp(lineprefix[i].search, p, l) == 0) && p + l < end) { + while ((strncmp(lineprefix[i].search, p, l) == 0) && p + l < end) { p += l; /* Special case for blockquotes: optional space after > */ - if(lineprefix[i].search[0] == '>' && *p == ' ') { + if (lineprefix[i].search[0] == '>' && *p == ' ') { p++; } - while(p < end) { + while (p < end) { ADDC(buffer, j) = *p; j++; - if(*(p++) == '\n') + if (*(p++) == '\n') break; } } /* Skip empty lines in block */ - while(*(buffer + j - 1) == '\n') { + while (*(buffer + j - 1) == '\n') j--; - } ADDC(buffer, j) = '\0'; - if(lineprefix[i].process) + if (lineprefix[i].process) process(buffer, buffer + strlen(buffer), lineprefix[i].process >= 2); else hprint(buffer, buffer + strlen(buffer)); @@ -251,90 +256,93 @@ dolineprefix(const char *begin, const char *end, int newblock) { } int -dolink(const char *begin, const char *end, int newblock) { +dolink(const char *begin, const char *end, int newblock) +{ long width = 0, height = 0; int img, len, sep, parens_depth = 1; char *numend; const char *desc, *link, *p, *q, *descend, *linkend; const char *title = NULL, *titleend = NULL; - if(*begin == '[') + if (*begin == '[') img = 0; - else if(strncmp(begin, "![", 2) == 0) + else if (strncmp(begin, "![", 2) == 0) img = 1; else return 0; p = desc = begin + 1 + img; - if(!(p = strstr(desc, "](")) || p > end) + if (!(p = strstr(desc, "](")) || p > end) return 0; - for(q = strstr(desc, "!["); q && q < end && q < p; q = strstr(q + 1, "![")) - if(!(p = strstr(p + 1, "](")) || p > end) + for (q = strstr(desc, "!["); q && q < end && q < p; q = strstr(q + 1, "![")) + if (!(p = strstr(p + 1, "](")) || p > end) return 0; descend = p; link = p + 2; /* find end of link while handling nested parens */ q = link; - while(parens_depth) { - if(!(q = strpbrk(q, "()")) || q > end) + while (parens_depth) { + if (!(q = strpbrk(q, "()")) || q > end) return 0; - if(*q == '(') + if (*q == '(') parens_depth++; else parens_depth--; - if(parens_depth && q < end) + if (parens_depth && q < end) q++; } - if((p = strpbrk(link, "\"'")) && p < end && q > p) { + if ((p = strpbrk(link, "\"'")) && p < end && q > p) { sep = p[0]; /* separator: can be " or ' */ title = p + 1; /* strip trailing whitespace */ - for(linkend = p; linkend > link && isspace(*(linkend - 1)); linkend--); - for(titleend = title; titleend < q && *titleend != sep; titleend++); - for(p = titleend + 1; p < end && isspace(*p); p++); + for (linkend = p; linkend > link && isspace(*(linkend - 1)); linkend--) + ; + for (titleend = title; titleend < q && *titleend != sep; titleend++) + ; + for (p = titleend + 1; p < end && isspace(*p); p++) + ; /* image dimensions */ - if(*p == '=') { + if (*p == '=') { width = strtol(++p, &numend, 10); p = numend; - if(*numend == 'x') + if (*numend == 'x') height = strtol(++p, &numend, 10); } - } - else { + } else { /* strip trailing whitespace */ - for(linkend = q; linkend > link && isspace(*(linkend - 1)); linkend--); + for (linkend = q; linkend > link && isspace(*(linkend - 1)); linkend--) + ; } /* Links can be given in angular brackets */ - if(*link == '<' && *(linkend - 1) == '>') { + if (*link == '<' && *(linkend - 1) == '>') { link++; linkend--; } len = q + 1 - begin; - if(img) { + if (img) { fputs("<img src=\"", stdout); hprintattr(link, linkend); fputs("\" alt=\"", stdout); hprintattr(desc, descend); fputs("\" ", stdout); - if(title && titleend) { + if (title && titleend) { fputs("title=\"", stdout); hprintattr(title, titleend); fputs("\" ", stdout); } - if(width > 0) + if (width > 0) printf("width=\"%ld\" ", width); - if(height > 0) + if (height > 0) printf("height=\"%ld\" ", height); fputs("/>", stdout); - } - else { + } else { fputs("<a href=\"", stdout); hprintattr(link, linkend); fputs("\"", stdout); - if(title && titleend) { + if (title && titleend) { fputs(" title=\"", stdout); hprintattr(title, titleend); fputs("\"", stdout); @@ -347,48 +355,52 @@ dolink(const char *begin, const char *end, int newblock) { } int -dolist(const char *begin, const char *end, int newblock) { +dolist(const char *begin, const char *end, int newblock) +{ unsigned int i, j, indent, run, ul, isblock; const char *p, *q; char *buffer = NULL; char marker; isblock = 0; - if(newblock) + if (newblock) p = begin; - else if(*begin == '\n') + else if (*begin == '\n') p = begin + 1; else return 0; q = p; - if(*p == '-' || *p == '*' || *p == '+') { + if (*p == '-' || *p == '*' || *p == '+') { ul = 1; marker = *p; } else { ul = 0; - for(; p < end && *p >= '0' && *p <= '9'; p++); - if(p >= end || *p != '.') + for (; p < end && *p >= '0' && *p <= '9'; p++) + ; + if (p >= end || *p != '.') return 0; } p++; - if(p >= end || !(*p == ' ' || *p == '\t')) + if (p >= end || !(*p == ' ' || *p == '\t')) return 0; - for(p++; p != end && (*p == ' ' || *p == '\t'); p++); + for (p++; p != end && (*p == ' ' || *p == '\t'); p++) + ; indent = p - q; buffer = ereallocz(buffer, BUFSIZ); - if(!newblock) + if (!newblock) fputc('\n', stdout); fputs(ul ? "<ul>\n" : "<ol>\n", stdout); run = 1; - for(; p < end && run; p++) { - for(i = 0; p < end && run; p++, i++) { - if(*p == '\n') { - if(p + 1 == end) + for (; p < end && run; p++) { + for (i = 0; p < end && run; p++, i++) { + if (*p == '\n') { + if (p + 1 == end) break; else { /* Handle empty lines */ - for(q = p + 1; (*q == ' ' || *q == '\t') && q < end; q++); - if(*q == '\n') { + for (q = p + 1; (*q == ' ' || *q == '\t') && q < end; q++) + ; + if (*q == '\n') { ADDC(buffer, i) = '\n'; i++; run = 0; @@ -398,25 +410,27 @@ dolist(const char *begin, const char *end, int newblock) { } q = p + 1; j = 0; - if(ul && *q == marker) + if (ul && *q == marker) j = 1; - else if(!ul) { - for(; q + j != end && q[j] >= '0' && q[j] <= '9' && j < indent; j++); - if(q + j == end) + else if (!ul) { + for (; q + j != end && q[j] >= '0' && q[j] <= '9' && j < indent; j++) + ; + if (q + j == end) break; - if(j > 0 && q[j] == '.') + if (j > 0 && q[j] == '.') j++; else j = 0; } - if(q + indent < end) - for(; (q[j] == ' ' || q[j] == '\t') && j < indent; j++); - if(j == indent) { + if (q + indent < end) + for (; (q[j] == ' ' || q[j] == '\t') && j < indent; j++) + ; + if (j == indent) { ADDC(buffer, i) = '\n'; i++; p += indent; run = 1; - if(*q == ' ' || *q == '\t') + if (*q == ' ' || *q == '\t') p++; else break; @@ -434,39 +448,44 @@ dolist(const char *begin, const char *end, int newblock) { fputs(ul ? "</ul>\n" : "</ol>\n", stdout); free(buffer); p--; - while(*(--p) == '\n'); + while (*(--p) == '\n') + ; + return -(p - begin + 1); } int -doparagraph(const char *begin, const char *end, int newblock) { +doparagraph(const char *begin, const char *end, int newblock) +{ const char *p; - if(!newblock) + if (!newblock) return 0; p = strstr(begin, "\n\n"); - if(!p || p > end) + if (!p || p > end) p = end; - if(p - begin <= 1) + if (p - begin <= 1) return 0; fputs("<p>", stdout); process(begin, p, 0); fputs("</p>\n", stdout); + return -(p - begin); } int -doreplace(const char *begin, const char *end, int newblock) { +doreplace(const char *begin, const char *end, int newblock) +{ unsigned int i, l; - for(i = 0; i < LENGTH(insert); i++) - if(strncmp(insert[i][0], begin, strlen(insert[i][0])) == 0) + for (i = 0; i < LENGTH(insert); i++) + if (strncmp(insert[i][0], begin, strlen(insert[i][0])) == 0) fputs(insert[i][1], stdout); - for(i = 0; i < LENGTH(replace); i++) { + for (i = 0; i < LENGTH(replace); i++) { l = strlen(replace[i][0]); - if(end - begin < l) + if (end - begin < l) continue; - if(strncmp(replace[i][0], begin, l) == 0) { + if (strncmp(replace[i][0], begin, l) == 0) { fputs(replace[i][1], stdout); return l; } @@ -475,13 +494,14 @@ doreplace(const char *begin, const char *end, int newblock) { } int -doshortlink(const char *begin, const char *end, int newblock) { +doshortlink(const char *begin, const char *end, int newblock) +{ const char *p, *c; int ismail = 0; - if(*begin != '<') + if (*begin != '<') return 0; - for(p = begin + 1; p != end; p++) { + for (p = begin + 1; p != end; p++) { switch(*p) { case ' ': case '\t': @@ -492,20 +512,20 @@ doshortlink(const char *begin, const char *end, int newblock) { ismail = -1; break; case '@': - if(ismail == 0) + if (ismail == 0) ismail = 1; break; case '>': - if(ismail == 0) + if (ismail == 0) return 0; fputs("<a href=\"", stdout); - if(ismail == 1) { + if (ismail == 1) { /* mailto: */ fputs("&#x6D;&#x61;i&#x6C;&#x74;&#x6F;:", stdout); - for(c = begin + 1; *c != '>'; c++) + for (c = begin + 1; *c != '>'; c++) fprintf(stdout, "&#%u;", *c); fputs("\">", stdout); - for(c = begin + 1; *c != '>'; c++) + for (c = begin + 1; *c != '>'; c++) fprintf(stdout, "&#%u;", *c); } else { @@ -521,13 +541,14 @@ doshortlink(const char *begin, const char *end, int newblock) { } int -dosurround(const char *begin, const char *end, int newblock) { +dosurround(const char *begin, const char *end, int newblock) +{ unsigned int i, l; const char *p, *start, *stop; - for(i = 0; i < LENGTH(surround); i++) { + for (i = 0; i < LENGTH(surround); i++) { l = strlen(surround[i].search); - if(end - begin < 2*l || strncmp(begin, surround[i].search, l) != 0) + if (end - begin < 2*l || strncmp(begin, surround[i].search, l) != 0) continue; start = begin + l; p = start - 1; @@ -537,7 +558,7 @@ dosurround(const char *begin, const char *end, int newblock) { } while(p && p[-1] == '\\'); if (p && p[-1] != '\\') stop = p; - if(!stop || stop < start || stop >= end) + if (!stop || stop < start || stop >= end) continue; fputs(surround[i].before, stdout); @@ -548,7 +569,7 @@ dosurround(const char *begin, const char *end, int newblock) { l++; } - if(surround[i].process) + if (surround[i].process) process(start, stop, 0); else hprint(start, stop); @@ -559,22 +580,25 @@ dosurround(const char *begin, const char *end, int newblock) { } int -dounderline(const char *begin, const char *end, int newblock) { +dounderline(const char *begin, const char *end, int newblock) +{ unsigned int i, j, l; const char *p; - if(!newblock) + if (!newblock) return 0; p = begin; - for(l = 0; p + l != end && p[l] != '\n'; l++); + for (l = 0; p + l != end && p[l] != '\n'; l++) + ; p += l + 1; - if(l == 0) + if (l == 0) return 0; - for(i = 0; i < LENGTH(underline); i++) { - for(j = 0; p + j != end && p[j] != '\n' && p[j] == underline[i].search[0]; j++); - if(j >= l) { + for (i = 0; i < LENGTH(underline); i++) { + for (j = 0; p + j != end && p[j] != '\n' && p[j] == underline[i].search[0]; j++) + ; + if (j >= l) { fputs(underline[i].before, stdout); - if(underline[i].process) + if (underline[i].process) process(begin, begin + l, 0); else hprint(begin, begin + l); @@ -586,27 +610,29 @@ dounderline(const char *begin, const char *end, int newblock) { } void * -ereallocz(void *p, size_t size) { +ereallocz(void *p, size_t size) +{ void *res; res = realloc(p, size); - if(!res) + if (!res) eprint("realloc: %zu bytes\n", size); return res; } void -hprintattr(const char *begin, const char *end) { +hprintattr(const char *begin, const char *end) +{ const char *p; - for(p = begin; p != end; p++) { - if(*p == '&') + for (p = begin; p != end; p++) { + if (*p == '&') fputs("&amp;", stdout); - else if(*p == '"') + else if (*p == '"') fputs("&quot;", stdout); - else if(*p == '>') + else if (*p == '>') fputs("&gt;", stdout); - else if(*p == '<') + else if (*p == '<') fputs("&lt;", stdout); else fputc(*p, stdout); @@ -614,15 +640,16 @@ hprintattr(const char *begin, const char *end) { } void -hprint(const char *begin, const char *end) { +hprint(const char *begin, const char *end) +{ const char *p; - for(p = begin; p != end; p++) { - if(*p == '&') + for (p = begin; p != end; p++) { + if (*p == '&') fputs("&amp;", stdout); - else if(*p == '>') + else if (*p == '>') fputs("&gt;", stdout); - else if(*p == '<') + else if (*p == '<') fputs("&lt;", stdout); else fputc(*p, stdout); @@ -630,31 +657,33 @@ hprint(const char *begin, const char *end) { } void -process(const char *begin, const char *end, int newblock) { +process(const char *begin, const char *end, int newblock) +{ const char *p, *q; int affected; unsigned int i; - for(p = begin; p < end;) { - if(newblock) - while(*p == '\n') - if(++p == end) + for (p = begin; p < end;) { + if (newblock) + while (*p == '\n') + if (++p == end) return; affected = 0; - for(i = 0; i < LENGTH(parsers) && !affected; i++) + for (i = 0; i < LENGTH(parsers) && !affected; i++) affected = parsers[i](p, end, newblock); p += abs(affected); - if(!affected) { - if(nohtml) + if (!affected) { + if (nohtml) hprint(p, p + 1); else fputc(*p, stdout); p++; } - for(q = p; q != end && *q == '\n'; q++); - if(q == end) + for (q = p; q != end && *q == '\n'; q++) + ; + if (q == end) return; - else if(p[0] == '\n' && p + 1 != end && p[1] == '\n') + else if (p[0] == '\n' && p + 1 != end && p[1] == '\n') newblock = 1; else newblock = affected < 0; @@ -662,12 +691,14 @@ process(const char *begin, const char *end, int newblock) { } void -usage(char **argv) { +usage(char **argv) +{ eprint("usage: %s [-n]\n\t-n escape HTML strictly\n", argv[0]); } int -main(int argc, char *argv[]) { +main(int argc, char *argv[]) +{ char *buffer = NULL; int s, i; unsigned long len, bsize; @@ -676,14 +707,14 @@ main(int argc, char *argv[]) { if (pledge("stdio", NULL) == -1) eprint("pledge"); - for(i = 1; i < argc; i++) { - if(!strcmp("-v", argv[i])) + for (i = 1; i < argc; i++) { + if (!strcmp("-v", argv[i])) eprint("simple markup %s\n",VERSION); - else if(!strcmp("-n", argv[i])) + else if (!strcmp("-n", argv[i])) nohtml = 1; - else if(argv[i][0] != '-' || !strcmp("-h", argv[i])) + else if (argv[i][0] != '-' || !strcmp("-h", argv[i])) usage(argv); - else if(!strcmp("--", argv[i])) { + else if (!strcmp("--", argv[i])) { i++; break; } @@ -693,16 +724,17 @@ main(int argc, char *argv[]) { bsize = 2 * BUFSIZ; buffer = ereallocz(buffer, bsize); len = 0; - while((s = fread(buffer + len, 1, BUFSIZ, source))) { + while ((s = fread(buffer + len, 1, BUFSIZ, source))) { len += s; - if(BUFSIZ + len + 1 > bsize) { + if (BUFSIZ + len + 1 > bsize) { bsize += BUFSIZ; - if(!(buffer = realloc(buffer, bsize))) + if (!(buffer = realloc(buffer, bsize))) eprint("realloc failed."); } } buffer[len] = '\0'; process(buffer, buffer + len, 1); free(buffer); - return EXIT_SUCCESS; + + return 0; }