From 93f7baae7fcd707f7080786a9c117fb92269d798 Mon Sep 17 00:00:00 2001 From: root Date: Sun, 14 Sep 2014 21:27:21 +0000 Subject: use stdin --- Makefile | 6 +++- README | 2 +- sass.c | 85 +++++++++++++++++++++++++++++++++++++++++++++++++------- test.css.correct | 14 ++++++++++ test.scss | 18 ++++++++++++ 5 files changed, 113 insertions(+), 12 deletions(-) create mode 100644 test.css.correct create mode 100644 test.scss diff --git a/Makefile b/Makefile index 6922f76..3d0c08e 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,7 @@ all: - gcc sass.c -I ./libsass/ ./libsass/libsass.a -o sass -lstdc++ + gcc sass.c -I ./libsass/ ./libsass/libsass.a -o sass -lstdc++ -O2 -march=native + @echo "Testing Sass" + @./sass < test.scss > test.css + @diff test.css test.css.correct || echo -e "\e[0;31merror: sass isn't compiling properly" && echo "ok" + diff --git a/README b/README index c7268e6..4daa07a 100644 --- a/README +++ b/README @@ -2,4 +2,4 @@ simple sass wrapper. USAGE: -sass myfile.sass > myfile.css +sass < myfile.sass > myfile.css diff --git a/sass.c b/sass.c index dde8409..4e2da6a 100644 --- a/sass.c +++ b/sass.c @@ -1,27 +1,92 @@ #include #include #include +#include +#include #include +// a function to chomp stdin +char * chomp_stdin() { + + const size_t read_size = 256; + size_t pos = 0; + size_t space_left = read_size; + size_t buf_size = read_size; + char *buf = malloc(read_size); + + while(!feof(stdin)) { + size_t bytes_read = 0; + + bytes_read = fread(buf + pos, sizeof(char), space_left, stdin); + + if(ferror(stdin) || bytes_read == 0) { + break; + } + + pos += bytes_read; + space_left = buf_size - pos; + + + if(space_left == 0) { + buf = realloc(buf, buf_size + read_size); + if(buf == NULL) + fprintf(stderr, "%s\n", strerror(errno)); + buf_size += read_size; + space_left = read_size; + } + } + + // if we didn't read anything, just return NULL; + if(pos == 0) { + free(buf); + return NULL; + } + + // gotta throw a null terminator there + if (space_left != 0) { + buf[pos+1] = '\0'; + } + else { + buf = realloc(buf, buf_size + read_size); + if(buf == NULL) + fprintf(stderr, "%s\n", strerror(errno)); + buf[pos+1] = '\0'; + } + + return buf; + +} int main (int argc, char **argv) { - struct sass_file_context *ctx = sass_new_file_context(); - ctx->input_path = argv[1]; - sass_compile_file(ctx); + + // load up our stdin + char *input = chomp_stdin(); + if(input == NULL) { + exit(1); + } + + // setup our parsing context + struct sass_context *ctx = sass_new_context(); + ctx->source_string = input; + + // parse + sass_compile(ctx); + + // error check if (ctx->error_status) { - if (ctx->error_message) { + if (ctx->error_message) fprintf(stderr,"%s\n", ctx->error_message); - } - else { + else fprintf(stderr,"An error occured; no error message available\n"); - } - return 1; - } + return ctx->error_status; + } else if (ctx->output_string) { fprintf(stdout, "%s", ctx->output_string); } - sass_free_file_context(ctx); + sass_free_context(ctx); + free(input); + return 0; } diff --git a/test.css.correct b/test.css.correct new file mode 100644 index 0000000..c076b87 --- /dev/null +++ b/test.css.correct @@ -0,0 +1,14 @@ +div { + color: yellow; + background: #7b2d06; + flah: #111111; + grah: rgba(255, 0, 238, 0.5); + blah: rgba(1, 2, 3, 0.6); + floo: cyan; + bloo: rgba(0, 255, 255, 0.7); + groo: cyan; + hoo: 123; + moo: 45; + poo: 6; + goo: rgba(63, 0, 191, 0.75); + boo: #edcba9; } diff --git a/test.scss b/test.scss new file mode 100644 index 0000000..41f89c8 --- /dev/null +++ b/test.scss @@ -0,0 +1,18 @@ +$x: rgb(0, 255, 255); +div { +color: rgb(255, $blue: 0, $green: 255); +background: rgb(123, 45, 6); +flah: rgba(0, 0, 0, 1) + #111; +grah: rgba(#f0e, $alpha: .5); +blah: rgba(1,2,3,.6); +floo: $x; +bloo: rgba($x, 0.7); +groo: $x; +$x: rgb(123, 45, 6); +hoo: red($x); +moo: green($x); +poo: blue($x); +goo: mix(rgba(255, 0, 0, 0.5), #00f); +boo: invert(#123456); +} + -- cgit v1.2.1