commit ef4e3f0f54cac9fbe82fb3f26f4726fe67a10bfc
parent 689de59fd28cf425d3b649cfcc68d01619d88ff5
Author: gottox@rootkit.lan <gottox@rootkit.lan>
Date: Mon, 10 Dec 2007 11:23:11 +0100
renaming errstr to format
adding hprint
Diffstat:
M | cmarkdown.c | | | 40 | +++++++++++++++++++++++++--------------- |
1 file changed, 25 insertions(+), 15 deletions(-)
diff --git a/cmarkdown.c b/cmarkdown.c
@@ -36,7 +36,8 @@ struct Tag {
char *tag;
};
-void eprint(const char *errstr, ...); /* 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 process(const char *begin, const char *end);
/* Processes range between begin and end with parser (NULL = all parsers) */
unsigned int doreplace(const char *begin, const char *end);
@@ -55,9 +56,9 @@ Parser parsers[] = { dounderline, dolineprefix, dosurround, dolink, doreplace };
FILE *source;
unsigned int bsize = 0;
struct Tag lineprefix[] = {
- { " ", 0, "pre" },
- { "\t", 0, "pre" },
- { "> ", 1, "blockquote" },
+ { " ", 0, "pre" },
+ { "\t", 0, "pre" },
+ { "> ", 1, "blockquote" },
};
struct Tag underline[] = {
{ "=", 1, "h1" },
@@ -74,22 +75,31 @@ struct Tag surround[] = {
char * replace[][2] = {
{ "\n---\n", "\n<hr />\n" },
{ "\n\n", "<br />\n<br />\n" },
- { "<", "<" },
- { ">", ">" },
- { "&", "&" },
- { "\"", """ },
};
void
-eprint(const char *errstr, ...) {
+eprint(const char *format, ...) {
va_list ap;
- va_start(ap, errstr);
- vfprintf(stderr, errstr, ap);
+ va_start(ap, format);
+ vfprintf(stderr, format, ap);
va_end(ap);
exit(EXIT_FAILURE);
}
+void
+hprint(const char *begin, const char *end) {
+ const char *p;
+
+ for(p = begin; p && p != end; p++) {
+ if(*p == '&') fputs("&",stdout);
+ else if(*p == '"') fputs(""",stdout);
+ else if(*p == '>') fputs(">",stdout);
+ else if(*p == '<') fputs("<",stdout);
+ else putchar(*p);
+ }
+}
+
unsigned int
dolink(const char *begin, const char *end) {
int img;
@@ -148,7 +158,7 @@ dosurround(const char *begin, const char *end) {
if(surround[i].process)
process(begin + strlen(surround[i].search), p);
else
- fwrite(begin + strlen(surround[i].search), p - begin - l, sizeof(char), stdout);
+ hprint(begin + strlen(surround[i].search), p);
printf("</%s>",surround[i].tag);
return p - begin + l;
}
@@ -199,7 +209,7 @@ dolineprefix(const char *begin, const char *end) {
if(lineprefix[i].process)
process(buffer,buffer+strlen(buffer));
else
- fwrite(buffer,strlen(buffer), sizeof(char),stdout);
+ hprint(buffer,buffer+strlen(buffer));
printf("</%s>",surround[i].tag);
free(buffer);
return p - begin;
@@ -226,7 +236,7 @@ dounderline(const char *begin, const char *end) {
if(underline[i].process)
process(begin+1, begin + l + 1);
else
- fwrite(begin+1,l,sizeof(char),stdout);
+ hprint(begin+1, begin + l + 1);
printf("</%s>",underline[i].tag);
return j + l + 2;
}
@@ -245,7 +255,7 @@ process(const char *begin, const char *end) {
for(i = 0; i < LENGTH(parsers) && affected == 0; i++)
affected = parsers[i](p, end);
if(affected == 0) {
- putchar(*p);
+ hprint(p,p+1);
p++;
}
else