more (semi-)proper HTML-escaping

This commit is contained in:
gottox@rootkit.lan 2007-12-12 05:42:38 +01:00
parent a6a434a400
commit 1f030b1126

View file

@ -12,8 +12,7 @@
#define BUFFERSIZE 512 #define BUFFERSIZE 512
#define LENGTH(x) sizeof(x)/sizeof(x[0]) #define LENGTH(x) sizeof(x)/sizeof(x[0])
#define ADDC(b,i) if((i + 1) % BUFFERSIZE == 0) \ #define ADDC(b,i) if((i + 1) % BUFFERSIZE == 0) \
{ b = realloc(b,((i + 1)+ BUFFERSIZE) * sizeof(b)); if(!b) eprint("Malloc failed."); } \ { b = realloc(b,((i + 1)+ BUFFERSIZE) * sizeof(b)); if(!b) eprint("Malloc failed."); }; b[i]
b[i]
typedef unsigned int (*Parser)(const char *, const char *); typedef unsigned int (*Parser)(const char *, const char *);
@ -27,6 +26,8 @@ struct Tag {
void eprint(const char *format, ...); /* Prints error and exits */ void eprint(const char *format, ...); /* Prints error and exits */
void hprint(const char *begin, const char *end); /* escapes HTML and prints it to stdout*/ void hprint(const char *begin, const char *end); /* escapes HTML and prints it to stdout*/
unsigned int doamp(const char *begin, const char *end); /* Parser for & */ unsigned int doamp(const char *begin, const char *end); /* Parser for & */
unsigned int dohtml(const char *begin, const char *end);
/* Parser for html */
unsigned int dolineprefix(const char *begin, const char *end); unsigned int dolineprefix(const char *begin, const char *end);
/* Parser for line prefix tags */ /* Parser for line prefix tags */
unsigned int dolink(const char *begin, const char *end); unsigned int dolink(const char *begin, const char *end);
@ -45,7 +46,7 @@ unsigned int dounderline(const char *begin, const char *end);
/* Parser for underline tags */ /* Parser for underline tags */
void process(const char *begin, const char *end); /* Processes range between begin and end. */ void process(const char *begin, const char *end); /* Processes range between begin and end. */
Parser parsers[] = { dounderline, dolineprefix, dolist, doparagraph, Parser parsers[] = { dounderline, dohtml, dolineprefix, dolist, doparagraph,
dosurround, dolink, doshortlink, doamp, doreplace }; /* list of parsers */ dosurround, dolink, doshortlink, doamp, doreplace }; /* list of parsers */
FILE *source; FILE *source;
unsigned int bsize = 0, nohtml = 0; unsigned int bsize = 0, nohtml = 0;
@ -127,12 +128,37 @@ doamp(const char *begin, const char *end) {
return 1; return 1;
} }
unsigned int unsigned int
dohtml(const char *begin, const char *end) {
const char *p, *tag, *tagend;
if(nohtml || *begin != '\n' || !*begin)
return 0;
p = begin;
if(p[1] == '\n')
p++;
if(p[1] != '<' || strchr(" /\n\t\\",p[2]))
return 0;
tag = p + 2;
p += 2;
for(; !strchr(" >",*p);p++);
tagend = p;
while((p = strstr(p,"\n</")) && p < end) {
p+=3;
if(strncmp(p, tag, tagend-tag) == 0 && p[tagend-tag] == '>') {
p++;
fwrite(begin, sizeof(char), p - begin + tagend - tag,stdout);
puts("\n");
return p - begin + tagend - tag;
}
}
return 0;
}
unsigned int
dolineprefix(const char *begin, const char *end) { dolineprefix(const char *begin, const char *end) {
unsigned int i, j, l; unsigned int i, j, l;
char *buffer; char *buffer;
const char *p; const char *p;
if(*begin != '\n') if(*begin != '\n' || !*begin)
return 0; return 0;
p = begin; p = begin;
if(p[1] == '\n') if(p[1] == '\n')
@ -243,12 +269,14 @@ dolist(const char *begin, const char *end) {
if(*p == '\n') { if(*p == '\n') {
if(p[1] == '\n') { if(p[1] == '\n') {
run = 0; run = 0;
ADDC(buffer,i++) = '\n'; ADDC(buffer,i) = '\n';
i++;
p++; p++;
} }
if(p[1] == ' ') { if(p[1] == ' ') {
run = 1; run = 1;
ADDC(buffer,i++) = '\n'; ADDC(buffer,i) = '\n';
i++;
p += indent + 1; p += indent + 1;
} }
else if(p[1] >= '0' && p[1] <= '9' || strchr("+-*",p[1])) { else if(p[1] >= '0' && p[1] <= '9' || strchr("+-*",p[1])) {
@ -261,10 +289,10 @@ dolist(const char *begin, const char *end) {
} }
ADDC(buffer,i) = *p; ADDC(buffer,i) = *p;
} }
buffer[i] = '\0'; ADDC(buffer,i) = '\0';
while(buffer[--i] == '\n') buffer[i] = '\0'; while(buffer[--i] == '\n') buffer[i] = '\0';
fputs("<li>",stdout); fputs("<li>",stdout);
process(buffer,i+2+buffer); process(buffer,i+1+buffer);
fputs("</li>\n",stdout); fputs("</li>\n",stdout);
} }
puts(ul ? "</ul>" : "</ol>"); puts(ul ? "</ul>" : "</ol>");
@ -420,7 +448,7 @@ process(const char *begin, const char *end) {
int affected; int affected;
unsigned int i; unsigned int i;
for(p = begin; *p && p != end;) { for(p = begin; *p && p < end;) {
affected = 0; affected = 0;
for(i = 0; i < LENGTH(parsers) && affected == 0; i++) for(i = 0; i < LENGTH(parsers) && affected == 0; i++)
affected = parsers[i](p, end); affected = parsers[i](p, end);