aboutsummaryrefslogtreecommitdiff
path: root/sass.c
blob: 4e2da6a49b5f673aaecdb6e1bd20f13e95891baa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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) {
 
  // 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) 
			fprintf(stderr,"%s\n", ctx->error_message);
		else 
			fprintf(stderr,"An error occured; no error message available\n");

		return ctx->error_status;
	}
	else if (ctx->output_string) {
    fprintf(stdout, "%s", ctx->output_string);
  }

  sass_free_context(ctx);
  free(input);

  return 0;
}