smu.c: Format source code

This commit is contained in:
Enno Tensing 2025-01-20 18:49:00 +01:00
parent 82048dd49e
commit c702617acd
Signed by: tenno
GPG key ID: 95265603BD36E66C

240
smu.c
View file

@ -10,7 +10,13 @@
#include <ctype.h> #include <ctype.h>
#define LENGTH(x) sizeof(x) / sizeof(x[0]) #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 failed."); } b[i] #define ADDC(b, i) \
if (i % BUFSIZ == 0) { \
b = realloc(b, (i + BUFSIZ) * sizeof(char)); \
if (!b) \
eprint("Malloc failed."); \
} \
b[i]
typedef int (*Parser)(const char *, const char *, int); typedef int (*Parser)(const char *, const char *, int);
typedef struct { typedef struct {
@ -19,26 +25,40 @@ typedef struct {
char *before, *after; char *before, *after;
} Tag; } Tag;
static int doamp(const char *begin, const char *end, int newblock); /* Parser for & */ static int doamp(const char *begin, const char *end,
static int docomment(const char *begin, const char *end, int newblock); /* Parser for html-comments */ int newblock); /* Parser for & */
static int dogtlt(const char *begin, const char *end, int newblock); /* Parser for < and > */ static int docomment(const char *begin, const char *end,
static int dohtml(const char *begin, const char *end, int newblock); /* Parser for html */ int newblock); /* Parser for html-comments */
static int dolineprefix(const char *begin, const char *end, int newblock);/* Parser for line prefix tags */ static int dogtlt(const char *begin, const char *end,
static int dolink(const char *begin, const char *end, int newblock); /* Parser for links and images */ int newblock); /* Parser for < and > */
static int dolist(const char *begin, const char *end, int newblock); /* Parser for lists */ static int dohtml(const char *begin, const char *end,
static int doparagraph(const char *begin, const char *end, int newblock); /* Parser for paragraphs */ int newblock); /* Parser for html */
static int doreplace(const char *begin, const char *end, int newblock); /* Parser for simple replaces */ static int dolineprefix(const char *begin, const char *end,
static int doshortlink(const char *begin, const char *end, int newblock); /* Parser for links and images */ int newblock); /* Parser for line prefix tags */
static int dosurround(const char *begin, const char *end, int newblock); /* Parser for surrounding tags */ static int dolink(const char *begin, const char *end,
static int dounderline(const char *begin, const char *end, int newblock); /* Parser for underline tags */ int newblock); /* Parser for links and images */
static int dolist(const char *begin, const char *end,
int newblock); /* Parser for lists */
static int doparagraph(const char *begin, const char *end,
int newblock); /* Parser for paragraphs */
static int doreplace(const char *begin, const char *end,
int newblock); /* Parser for simple replaces */
static int doshortlink(const char *begin, const char *end,
int newblock); /* Parser for links and images */
static int dosurround(const char *begin, const char *end,
int newblock); /* Parser for surrounding tags */
static int dounderline(const char *begin, const char *end,
int newblock); /* Parser for underline tags */
static void *ereallocz(void *p, size_t size); static void *ereallocz(void *p, size_t size);
static void eprint(const char *format, ...); static void eprint(const char *format, ...);
static void hprint(const char *begin, const char *end); /* escapes HTML and prints it to output */ static void hprint(const char *begin,
static void process(const char *begin, const char *end, int isblock); /* Processes range between begin and end. */ const char *end); /* escapes HTML and prints it to output */
static void process(const char *begin, const char *end,
int isblock); /* Processes range between begin and end. */
/* list of parsers */ /* list of parsers */
static Parser parsers[] = { dounderline, docomment, dolineprefix, static Parser parsers[] = { dounderline, docomment, dolineprefix, dolist,
dolist, doparagraph, dogtlt, dosurround, dolink, doparagraph, dogtlt, dosurround, dolink,
doshortlink, dohtml, doamp, doreplace }; doshortlink, dohtml, doamp, doreplace };
static int nohtml = 0; static int nohtml = 0;
@ -72,29 +92,18 @@ static Tag surround[] = {
}; };
static const char *replace[][2] = { static const char *replace[][2] = {
{ "\\\\", "\\" }, { "\\\\", "\\" }, { "\\`", "`" }, { "\\*", "*" }, { "\\_", "_" },
{ "\\`", "`" }, { "\\{", "{" }, { "\\}", "}" }, { "\\[", "[" }, { "\\]", "]" },
{ "\\*", "*" }, { "\\(", "(" }, { "\\)", ")" }, { "\\#", "#" }, { "\\+", "+" },
{ "\\_", "_" }, { "\\-", "-" }, { "\\.", "." }, { "\\!", "!" },
{ "\\{", "{" },
{ "\\}", "}" },
{ "\\[", "[" },
{ "\\]", "]" },
{ "\\(", "(" },
{ "\\)", ")" },
{ "\\#", "#" },
{ "\\+", "+" },
{ "\\-", "-" },
{ "\\.", "." },
{ "\\!", "!" },
}; };
static const char *insert[][2] = { static const char *insert[][2] = {
{ " \n", "<br />" }, { " \n", "<br />" },
}; };
void void eprint(const char *format, ...)
eprint(const char *format, ...) { {
va_list ap; va_list ap;
va_start(ap, format); va_start(ap, format);
@ -103,14 +112,15 @@ eprint(const char *format, ...) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
int int doamp(const char *begin, const char *end, int newblock)
doamp(const char *begin, const char *end, int newblock) { {
const char *p; const char *p;
if (*begin != '&') if (*begin != '&')
return 0; return 0;
if (!nohtml) { if (!nohtml) {
for(p = begin + 1; p != end && !strchr("; \\\n\t", *p); p++); for (p = begin + 1; p != end && !strchr("; \\\n\t", *p); p++)
;
if (p == end || *p == ';') if (p == end || *p == ';')
return 0; return 0;
} }
@ -118,8 +128,8 @@ doamp(const char *begin, const char *end, int newblock) {
return 1; return 1;
} }
int int dogtlt(const char *begin, const char *end, int newblock)
dogtlt(const char *begin, const char *end, int newblock) { {
int brpos; int brpos;
char c; char c;
@ -132,16 +142,16 @@ dogtlt(const char *begin, const char *end, int newblock) {
if (!brpos && (c < 'a' || c > 'z') && (c < 'A' || c > 'Z')) { if (!brpos && (c < 'a' || c > 'z') && (c < 'A' || c > 'Z')) {
fputs("&lt;", stdout); fputs("&lt;", stdout);
return 1; return 1;
} } else if (brpos && (c < 'a' || c > 'z') && (c < 'A' || c > 'Z') &&
else if(brpos && (c < 'a' || c > 'z') && (c < 'A' || c > 'Z') && !strchr("/\"'",c)) { !strchr("/\"'", c)) {
fprintf(stdout, "%c&gt;", c); fprintf(stdout, "%c&gt;", c);
return 2; return 2;
} }
return 0; return 0;
} }
int int docomment(const char *begin, const char *end, int newblock)
docomment(const char *begin, const char *end, int newblock) { {
char *p; char *p;
if (nohtml || strncmp("<!--", begin, 4)) if (nohtml || strncmp("<!--", begin, 4))
@ -153,8 +163,8 @@ docomment(const char *begin, const char *end, int newblock) {
return (p + 3 - begin) * (newblock ? -1 : 1); return (p + 3 - begin) * (newblock ? -1 : 1);
} }
int int dohtml(const char *begin, const char *end, int newblock)
dohtml(const char *begin, const char *end, int newblock) { {
const char *p, *tag, *tagend; const char *p, *tag, *tagend;
if (nohtml || begin + 2 >= end) if (nohtml || begin + 2 >= end)
@ -164,15 +174,18 @@ dohtml(const char *begin, const char *end, int newblock) {
return 0; return 0;
p++; p++;
tag = p; tag = p;
for(; isalnum(*p) && p < end; p++); for (; isalnum(*p) && p < end; p++)
;
tagend = p; tagend = p;
if (p > end || tag == tagend) if (p > end || tag == tagend)
return 0; return 0;
while ((p = strstr(p, "</")) && p < end) { while ((p = strstr(p, "</")) && p < end) {
p += 2; p += 2;
if(strncmp(p, tag, tagend - tag) == 0 && p[tagend - tag] == '>') { if (strncmp(p, tag, tagend - tag) == 0 &&
p[tagend - tag] == '>') {
p++; p++;
fwrite(begin, sizeof(char), p - begin + tagend - tag - 1, stdout); fwrite(begin, sizeof(char),
p - begin + tagend - tag - 1, stdout);
return p - begin + tagend - tag - 1; return p - begin + tagend - tag - 1;
} }
} }
@ -180,13 +193,12 @@ dohtml(const char *begin, const char *end, int newblock) {
if (p) { if (p) {
fwrite(begin, sizeof(char), p - begin + 1, stdout); fwrite(begin, sizeof(char), p - begin + 1, stdout);
return p - begin + 1; return p - begin + 1;
} } else
else
return 0; return 0;
} }
int int dolineprefix(const char *begin, const char *end, int newblock)
dolineprefix(const char *begin, const char *end, int newblock) { {
unsigned int i, j, l; unsigned int i, j, l;
char *buffer; char *buffer;
const char *p; const char *p;
@ -216,7 +228,8 @@ dolineprefix(const char *begin, const char *end, int newblock) {
/* Collect lines into buffer while they start with the prefix */ /* Collect lines into buffer while they start with the prefix */
j = 0; 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; p += l;
/* Special case for blockquotes: optional space after > */ /* Special case for blockquotes: optional space after > */
@ -239,7 +252,8 @@ dolineprefix(const char *begin, const char *end, int newblock) {
ADDC(buffer, j) = '\0'; ADDC(buffer, j) = '\0';
if (lineprefix[i].process) if (lineprefix[i].process)
process(buffer, buffer + strlen(buffer), lineprefix[i].process >= 2); process(buffer, buffer + strlen(buffer),
lineprefix[i].process >= 2);
else else
hprint(buffer, buffer + strlen(buffer)); hprint(buffer, buffer + strlen(buffer));
puts(lineprefix[i].after); puts(lineprefix[i].after);
@ -249,8 +263,8 @@ dolineprefix(const char *begin, const char *end, int newblock) {
return 0; return 0;
} }
int int dolink(const char *begin, const char *end, int newblock)
dolink(const char *begin, const char *end, int newblock) { {
int img, len, sep, parens_depth = 1; int img, len, sep, parens_depth = 1;
const char *desc, *link, *p, *q, *descend, *linkend; const char *desc, *link, *p, *q, *descend, *linkend;
const char *title = NULL, *titleend = NULL; const char *title = NULL, *titleend = NULL;
@ -264,7 +278,8 @@ dolink(const char *begin, const char *end, int newblock) {
p = desc = begin + 1 + img; p = desc = begin + 1 + img;
if (!(p = strstr(desc, "](")) || p > end) if (!(p = strstr(desc, "](")) || p > end)
return 0; return 0;
for(q = strstr(desc, "!["); q && q < end && q < p; q = strstr(q + 1, "![")) for (q = strstr(desc, "!["); q && q < end && q < p;
q = strstr(q + 1, "!["))
if (!(p = strstr(p + 1, "](")) || p > end) if (!(p = strstr(p + 1, "](")) || p > end)
return 0; return 0;
descend = p; descend = p;
@ -287,13 +302,16 @@ dolink(const char *begin, const char *end, int newblock) {
sep = p[0]; /* separator: can be " or ' */ sep = p[0]; /* separator: can be " or ' */
title = p + 1; title = p + 1;
/* strip trailing whitespace */ /* strip trailing whitespace */
for(linkend = p; linkend > link && isspace(*(linkend - 1)); linkend--); for (linkend = p; linkend > link && isspace(*(linkend - 1));
for(titleend = q - 1; titleend > title && isspace(*(titleend)); titleend--); linkend--)
;
for (titleend = q - 1; titleend > title && isspace(*(titleend));
titleend--)
;
if (*titleend != sep) { if (*titleend != sep) {
return 0; return 0;
} }
} } else {
else {
linkend = q; linkend = q;
} }
@ -316,8 +334,7 @@ dolink(const char *begin, const char *end, int newblock) {
fputs("\" ", stdout); fputs("\" ", stdout);
} }
fputs("/>", stdout); fputs("/>", stdout);
} } else {
else {
fputs("<a href=\"", stdout); fputs("<a href=\"", stdout);
hprint(link, linkend); hprint(link, linkend);
fputs("\"", stdout); fputs("\"", stdout);
@ -333,8 +350,8 @@ dolink(const char *begin, const char *end, int newblock) {
return len; return len;
} }
int 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; unsigned int i, j, indent, run, ul, isblock;
const char *p, *q; const char *p, *q;
char *buffer = NULL; char *buffer = NULL;
@ -353,14 +370,16 @@ dolist(const char *begin, const char *end, int newblock) {
marker = *p; marker = *p;
} else { } else {
ul = 0; ul = 0;
for(; p < end && *p >= '0' && *p <= '9'; p++); for (; p < end && *p >= '0' && *p <= '9'; p++)
;
if (p >= end || *p != '.') if (p >= end || *p != '.')
return 0; return 0;
} }
p++; p++;
if (p >= end || !(*p == ' ' || *p == '\t')) if (p >= end || !(*p == ' ' || *p == '\t'))
return 0; return 0;
for(p++; p != end && (*p == ' ' || *p == '\t'); p++); for (p++; p != end && (*p == ' ' || *p == '\t'); p++)
;
indent = p - q; indent = p - q;
buffer = ereallocz(buffer, BUFSIZ); buffer = ereallocz(buffer, BUFSIZ);
if (!newblock) if (!newblock)
@ -374,7 +393,11 @@ dolist(const char *begin, const char *end, int newblock) {
break; break;
else { else {
/* Handle empty lines */ /* Handle empty lines */
for(q = p + 1; (*q == ' ' || *q == '\t') && q < end; q++); for (q = p + 1;
(*q == ' ' || *q == '\t') &&
q < end;
q++)
;
if (*q == '\n') { if (*q == '\n') {
ADDC(buffer, i) = '\n'; ADDC(buffer, i) = '\n';
i++; i++;
@ -388,7 +411,10 @@ dolist(const char *begin, const char *end, int newblock) {
if (ul && *q == marker) if (ul && *q == marker)
j = 1; j = 1;
else if (!ul) { else if (!ul) {
for(; q + j != end && q[j] >= '0' && q[j] <= '9' && j < indent; j++); for (; q + j != end && q[j] >= '0' &&
q[j] <= '9' && j < indent;
j++)
;
if (q + j == end) if (q + j == end)
break; break;
if (j > 0 && q[j] == '.') if (j > 0 && q[j] == '.')
@ -397,7 +423,10 @@ dolist(const char *begin, const char *end, int newblock) {
j = 0; j = 0;
} }
if (q + indent < end) if (q + indent < end)
for(; (q[j] == ' ' || q[j] == '\t') && j < indent; j++); for (; (q[j] == ' ' || q[j] == '\t') &&
j < indent;
j++)
;
if (j == indent) { if (j == indent) {
ADDC(buffer, i) = '\n'; ADDC(buffer, i) = '\n';
i++; i++;
@ -407,26 +436,27 @@ dolist(const char *begin, const char *end, int newblock) {
p++; p++;
else else
break; break;
} } else if (j < indent)
else if (j < indent)
run = 0; run = 0;
} }
ADDC(buffer, i) = *p; ADDC(buffer, i) = *p;
} }
ADDC(buffer, i) = '\0'; ADDC(buffer, i) = '\0';
fputs("<li>", stdout); fputs("<li>", stdout);
process(buffer, buffer + i, isblock > 1 || (isblock == 1 && run)); process(buffer, buffer + i,
isblock > 1 || (isblock == 1 && run));
fputs("</li>\n", stdout); fputs("</li>\n", stdout);
} }
fputs(ul ? "</ul>\n" : "</ol>\n", stdout); fputs(ul ? "</ul>\n" : "</ol>\n", stdout);
free(buffer); free(buffer);
p--; p--;
while(*(--p) == '\n'); while (*(--p) == '\n')
;
return -(p - begin + 1); return -(p - begin + 1);
} }
int int doparagraph(const char *begin, const char *end, int newblock)
doparagraph(const char *begin, const char *end, int newblock) { {
const char *p; const char *p;
if (!newblock) if (!newblock)
@ -442,8 +472,8 @@ doparagraph(const char *begin, const char *end, int newblock) {
return -(p - begin); return -(p - begin);
} }
int int doreplace(const char *begin, const char *end, int newblock)
doreplace(const char *begin, const char *end, int newblock) { {
unsigned int i, l; unsigned int i, l;
for (i = 0; i < LENGTH(insert); i++) for (i = 0; i < LENGTH(insert); i++)
@ -461,8 +491,8 @@ doreplace(const char *begin, const char *end, int newblock) {
return 0; return 0;
} }
int int doshortlink(const char *begin, const char *end, int newblock)
doshortlink(const char *begin, const char *end, int newblock) { {
const char *p, *c; const char *p, *c;
int ismail = 0; int ismail = 0;
@ -488,14 +518,14 @@ doshortlink(const char *begin, const char *end, int newblock) {
fputs("<a href=\"", stdout); fputs("<a href=\"", stdout);
if (ismail == 1) { if (ismail == 1) {
/* mailto: */ /* mailto: */
fputs("&#x6D;&#x61;i&#x6C;&#x74;&#x6F;:", stdout); fputs("&#x6D;&#x61;i&#x6C;&#x74;&#x6F;:",
stdout);
for (c = begin + 1; *c != '>'; c++) for (c = begin + 1; *c != '>'; c++)
fprintf(stdout, "&#%u;", *c); fprintf(stdout, "&#%u;", *c);
fputs("\">", stdout); fputs("\">", stdout);
for (c = begin + 1; *c != '>'; c++) for (c = begin + 1; *c != '>'; c++)
fprintf(stdout, "&#%u;", *c); fprintf(stdout, "&#%u;", *c);
} } else {
else {
hprint(begin + 1, p); hprint(begin + 1, p);
fputs("\">", stdout); fputs("\">", stdout);
hprint(begin + 1, p); hprint(begin + 1, p);
@ -507,14 +537,15 @@ doshortlink(const char *begin, const char *end, int newblock) {
return 0; return 0;
} }
int int dosurround(const char *begin, const char *end, int newblock)
dosurround(const char *begin, const char *end, int newblock) { {
unsigned int i, l; unsigned int i, l;
const char *p, *start, *stop; const char *p, *start, *stop;
for (i = 0; i < LENGTH(surround); i++) { for (i = 0; i < LENGTH(surround); i++) {
l = strlen(surround[i].search); 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; continue;
start = begin + l; start = begin + l;
p = start - 1; p = start - 1;
@ -545,20 +576,24 @@ dosurround(const char *begin, const char *end, int newblock) {
return 0; return 0;
} }
int int dounderline(const char *begin, const char *end, int newblock)
dounderline(const char *begin, const char *end, int newblock) { {
unsigned int i, j, l; unsigned int i, j, l;
const char *p; const char *p;
if (!newblock) if (!newblock)
return 0; return 0;
p = begin; p = begin;
for(l = 0; p + l + 1 != end && p[l] != '\n'; l++); for (l = 0; p + l + 1 != end && p[l] != '\n'; l++)
;
p += l + 1; p += l + 1;
if (l == 0) if (l == 0)
return 0; return 0;
for (i = 0; i < LENGTH(underline); i++) { for (i = 0; i < LENGTH(underline); i++) {
for(j = 0; p + j != end && p[j] != '\n' && p[j] == underline[i].search[0]; j++); for (j = 0; p + j != end && p[j] != '\n' &&
p[j] == underline[i].search[0];
j++)
;
if (j >= l) { if (j >= l) {
fputs(underline[i].before, stdout); fputs(underline[i].before, stdout);
if (underline[i].process) if (underline[i].process)
@ -572,8 +607,8 @@ dounderline(const char *begin, const char *end, int newblock) {
return 0; return 0;
} }
void * void *ereallocz(void *p, size_t size)
ereallocz(void *p, size_t size) { {
void *res; void *res;
if (p) if (p)
res = realloc(p, size); res = realloc(p, size);
@ -585,8 +620,8 @@ ereallocz(void *p, size_t size) {
return res; return res;
} }
void void hprint(const char *begin, const char *end)
hprint(const char *begin, const char *end) { {
const char *p; const char *p;
for (p = begin; p != end; p++) { for (p = begin; p != end; p++) {
@ -603,8 +638,8 @@ hprint(const char *begin, const char *end) {
} }
} }
void void process(const char *begin, const char *end, int newblock)
process(const char *begin, const char *end, int newblock) { {
const char *p, *q; const char *p, *q;
int affected; int affected;
unsigned int i; unsigned int i;
@ -625,7 +660,8 @@ process(const char *begin, const char *end, int newblock) {
fputc(*p, stdout); fputc(*p, stdout);
p++; p++;
} }
for(q = p; q != end && *q == '\n'; q++); for (q = p; q != end && *q == '\n'; q++)
;
if (q == end) if (q == end)
return; return;
else if (p[0] == '\n' && p + 1 != end && p[1] == '\n') else if (p[0] == '\n' && p + 1 != end && p[1] == '\n')
@ -635,8 +671,8 @@ process(const char *begin, const char *end, int newblock) {
} }
} }
int int main(int argc, char *argv[])
main(int argc, char *argv[]) { {
char *buffer = NULL; char *buffer = NULL;
int s, i; int s, i;
unsigned long len, bsize; unsigned long len, bsize;
@ -652,9 +688,9 @@ main(int argc, char *argv[]) {
else if (!strcmp("--", argv[i])) { else if (!strcmp("--", argv[i])) {
i++; i++;
break; break;
} } else
else eprint("Usage %s [-n] [file]\n -n escape html strictly\n",
eprint("Usage %s [-n] [file]\n -n escape html strictly\n", argv[0]); argv[0]);
} }
if (i < argc && !(source = fopen(argv[i], "r"))) if (i < argc && !(source = fopen(argv[i], "r")))
eprint("Cannot open file `%s`\n", argv[i]); eprint("Cannot open file `%s`\n", argv[i]);