Example #1
0
int compile_stdin(struct sass_options options, char* outfile) {
    int ret;
    struct sass_context* ctx;
    char buffer[BUFSIZE];
    size_t size = 1;
    char *source_string = malloc(sizeof(char) * BUFSIZE);

    if(source_string == NULL) {
        perror("Allocation failed");
        exit(1);
    }

    source_string[0] = '\0';

    while(fgets(buffer, BUFSIZE, stdin)) {
        char *old = source_string;
        size += strlen(buffer);
        source_string = realloc(source_string, size);
        if(source_string == NULL) {
            perror("Reallocation failed");
            free(old);
            exit(2);
        }
        strcat(source_string, buffer);
    }

    if(ferror(stdin)) {
        free(source_string);
        perror("Error reading standard input");
        exit(2);
    }

    ctx = sass_new_context();
    ctx->options = options;
    ctx->source_string = source_string;
    sass_compile(ctx);
    ret = output(ctx->error_status, ctx->error_message, ctx->output_string, outfile);

    sass_free_context(ctx);
    free(source_string);
    return ret;
}
char *sass_compile_emscripten(
  char *source_string,
  int output_style,
  int source_comments,
  char *include_paths,
  char **error_message
) {
  char *output_string;
  struct sass_options options;
  struct sass_context *ctx = sass_new_context();

  options.source_comments = source_comments;
  options.output_style = output_style;
  options.image_path = NULL;
  options.include_paths = include_paths;
  options.precision = 0; // 0 => use sass default numeric precision

  ctx->options = options;
  ctx->source_string = source_string;

  sass_compile(ctx);

  if (ctx->output_string) {
    output_string = strdup(ctx->output_string);
  } else {
    output_string = NULL;
  }

  if (ctx->error_status) {
    *error_message = strdup(ctx->error_message);
  } else {
    *error_message = NULL;
  }

  sass_free_context(ctx);
  return output_string;
}