diff --git a/templates/Makefile b/templates/Makefile new file mode 100644 index 0000000..d19663d --- /dev/null +++ b/templates/Makefile @@ -0,0 +1,24 @@ +# +# Makefile +# acastro, 2007-08-21 18:41 +# + +all_t := $(wildcard *.t) +all_inc := $(patsubst %.t,%.inc,$(all_t)) + + +all: $(all_inc) + +t2inc: t2inc.c + +.INTERMEDIATE: t2inc + +$(all_inc): t2inc + +%.inc: %.t + ./t2inc < $< > $@ + + +# vim:ft=make +# + diff --git a/templates/t2inc.c b/templates/t2inc.c new file mode 100644 index 0000000..9871566 --- /dev/null +++ b/templates/t2inc.c @@ -0,0 +1,62 @@ +/* + * t2h.c + * Copyright (C) 2007 acastro + * + * Distributed under terms of the MIT license. + */ + +#include +#include +#include +#include +#include + +#define MAX_W 70 + +int +main(int argc, char **argv) +{ + (void) argc; /* unused */ + (void) argv; /* unused */ + + int c = 0; + int w = 0; + + putchar('"'); + while ((c = getchar()) != EOF) { + /* skip whitespace */ + if ((c != ' ') && isspace(c)) continue; + + /* take care of long lines */ + if (w++ >= MAX_W) { + printf("\"\n\""); + w = 0; + } + + /* escape non-printable characters */ + if (!isprint(c)) { + printf("\\x%02x", c); + continue; + } + +#define C(_m, _p) case _m : printf(_p); break + + switch (c) { + C('\\', "\\\\"); /* backslashes */ + C('"' , "\\\""); /* double quotes */ + default: + putchar(c); + } + } + printf("\"\n"); + + /* check whether an error was produced */ + if ((c == EOF) && ferror(stdin)) { + fprintf(stderr, "%s: error reading input, %s\n", argv[0], strerror(errno)); + fflush(stderr); + exit(EXIT_FAILURE); + } + + return EXIT_SUCCESS; +} +