aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorroot <root@debian>2014-09-14 21:27:21 +0000
committerroot <root@debian>2014-09-14 21:27:21 +0000
commit93f7baae7fcd707f7080786a9c117fb92269d798 (patch)
tree8541792c5147afbde8aa6c01ea32677d07e8f09c
parenta4f8c7af3baba2179f6de2f198be23860f3803bc (diff)
use stdinHEADmaster
-rw-r--r--Makefile6
-rw-r--r--README2
-rw-r--r--sass.c85
-rw-r--r--test.css.correct14
-rw-r--r--test.scss18
5 files changed, 113 insertions, 12 deletions
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 <sass_interface.h>
#include <stdio.h>
#include <unistd.h>
+#include <strings.h>
+#include <errno.h>
#include <stdlib.h>
+// 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);
+}
+