aboutsummaryrefslogtreecommitdiff
path: root/sass.c
diff options
context:
space:
mode:
Diffstat (limited to 'sass.c')
-rw-r--r--sass.c85
1 files changed, 75 insertions, 10 deletions
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;
}