pre2html

Unnamed repository; edit this file to name it for gitweb.
git clone https://logand.com/git/pre2html.git/
Log | Files | Refs

commit 91f84d1ab6d4bb78e9485ea6134401a5024de494
parent ed24e77c1efac6d659c1bd11d5e791d8e1328427
Author: Tomas Hlavaty <tom@logand.com>
Date:   Thu,  8 Sep 2011 02:37:52 +0200

build with dietlibc to cut bloat

Diffstat:
MMakefile | 3++-
Mpre2html.c | 72+++++++++++++++++++++++++++++++++++++++++-------------------------------
2 files changed, 43 insertions(+), 32 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,4 +1,5 @@ -CFLAGS = -O2 +CC = diet gcc -static -nostdinc +CFLAGS = -Os -fomit-frame-pointer all: pre2html diff --git a/pre2html.c b/pre2html.c @@ -1,15 +1,25 @@ #include <stdlib.h> -#include <stdio.h> +#include <string.h> enum { BLEN = 10240 }; +static inline void write_char(char x) { + write(1, &x, sizeof(char)); +} + +static inline void write_string(char *x) { + write(1, x, strlen(x)); +} + static int line(char *buf, char *end) { int n = 0; while(buf < end) { - int c = getchar(); - if(EOF == c) break; + char c; + int m = read(0, &c, sizeof(char)); + if(m <= 0) break; + if(m != sizeof(char)) exit(-1); *buf++ = c; n++; if('\n' == c) break; @@ -40,18 +50,18 @@ static char *find(char what, char *buf, char *end) { static void attr(char *buf, char *end) { for(; buf < end; buf++) switch(*buf) { - case '"': printf("&quot;"); break; - default: putchar(*buf); + case '"': write_string("&quot;"); break; + default: write_char(*buf); } } static escape(char c) { switch(c) { - case '<': printf("&lt;"); break; - case '>': printf("&gt;"); break; - case '&': printf("&amp;"); break; - case '"': printf("&quot;"); break; - default: putchar(c); + case '<': write_string("&lt;"); break; + case '>': write_string("&gt;"); break; + case '&': write_string("&amp;"); break; + case '"': write_string("&quot;"); break; + default: write_char(c); } } @@ -65,24 +75,24 @@ static void rest(char *b, char *e) { if(head("<http:", b, e) || head("<https:", b, e)) { escape(*b++); char *n = find('>', b, e); - printf("<a href=\""); + write_string("<a href=\""); attr(b, n); - printf("\">"); + write_string("\">"); text(b, n); - printf("</a>"); + write_string("</a>"); b = n; escape(*b++); } else if(head("<#", b, e)) { b += 2; char *n = find(' ', b, e); - printf("<a href=\"#"); + write_string("<a href=\"#"); attr(b, n); - printf("\">"); + write_string("\">"); b = n + 1; n = find('>', b, e); text(b, n); - printf("</a>"); + write_string("</a>"); b = n + 1; } else escape(*b++); @@ -94,50 +104,50 @@ int main() { char *end = buf + BLEN; int n; int bold = 0; - puts("<pre>"); + write_string("<pre>\n"); while(0 < (n = line(buf, end))) { char *b = buf; char *e = buf + n; if(head("CODE", b, e)) - printf("<div style=\"color:#080\">"); + write_string("<div style=\"color:#080\">"); else if(head("END", b, e)) - printf("</div>"); + write_string("</div>"); else if(head("\f", b, e)) { - if(bold) printf("</div>"); - printf("<div style=\"font-weight:bold\">"); + if(bold) write_string("</div>"); + write_string("<div style=\"font-weight:bold\">"); b++; bold = 1; if('\n' != *b) { - printf("<a name=\""); + write_string("<a name=\""); char *n = find('\n', b, e); attr(b, n); - printf("\"></a>"); + write_string("\"></a>"); b = n; } escape(*b++); } else if(bold && (tail("\n=", b, e) || tail("\n-", b, e))) { rest(b, e); - printf("</div>"); + write_string("</div>"); bold = 0; } else if(head("> ", b, e)) { - printf("<span style=\"color:red\">"); + write_string("<span style=\"color:red\">"); rest(b, e); - printf("</span>"); + write_string("</span>"); } else if(head(">> ", b, e)) { - printf("<span style=\"color:green\">"); + write_string("<span style=\"color:green\">"); rest(b, e); - printf("</span>"); + write_string("</span>"); } else if(head(">>> ", b, e)) { - printf("<span style=\"color:blue\">"); + write_string("<span style=\"color:blue\">"); rest(b, e); - printf("</span>"); + write_string("</span>"); } else rest(b, e); } - puts("</pre>"); + write_string("</pre>\n"); return 0; }