Blame docs/api-function-example.md

Packit bfcc33
## Example main.c
Packit bfcc33
Packit bfcc33
```C
Packit bfcc33
#include <stdio.h>
Packit bfcc33
#include <stdint.h>
Packit bfcc33
#include "sass/context.h"
Packit bfcc33
Packit bfcc33
union Sass_Value* call_fn_foo(const union Sass_Value* s_args, Sass_Function_Entry cb, struct Sass_Compiler* comp)
Packit bfcc33
{
Packit bfcc33
  // get context/option struct associated with this compiler
Packit bfcc33
  struct Sass_Context* ctx = sass_compiler_get_context(comp);
Packit bfcc33
  struct Sass_Options* opts = sass_compiler_get_options(comp);
Packit bfcc33
  // get information about previous importer entry from the stack
Packit bfcc33
  Sass_Import_Entry import = sass_compiler_get_last_import(comp);
Packit bfcc33
  const char* prev_abs_path = sass_import_get_abs_path(import);
Packit bfcc33
  const char* prev_imp_path = sass_import_get_imp_path(import);
Packit bfcc33
  // get the cookie from function descriptor
Packit bfcc33
  void* cookie = sass_function_get_cookie(cb);
Packit bfcc33
  // we actually abuse the void* to store an "int"
Packit bfcc33
  return sass_make_number((intptr_t)cookie, "px");
Packit bfcc33
}
Packit bfcc33
Packit bfcc33
int main( int argc, const char* argv[] )
Packit bfcc33
{
Packit bfcc33
Packit bfcc33
  // get the input file from first argument or use default
Packit bfcc33
  const char* input = argc > 1 ? argv[1] : "styles.scss";
Packit bfcc33
Packit bfcc33
  // create the file context and get all related structs
Packit bfcc33
  struct Sass_File_Context* file_ctx = sass_make_file_context(input);
Packit bfcc33
  struct Sass_Context* ctx = sass_file_context_get_context(file_ctx);
Packit bfcc33
  struct Sass_Options* ctx_opt = sass_context_get_options(ctx);
Packit bfcc33
Packit bfcc33
  // allocate a custom function caller
Packit bfcc33
  Sass_Function_Entry fn_foo =
Packit bfcc33
    sass_make_function("foo()", call_fn_foo, (void*)42);
Packit bfcc33
Packit bfcc33
  // create list of all custom functions
Packit bfcc33
  Sass_Function_List fn_list = sass_make_function_list(1);
Packit bfcc33
  sass_function_set_list_entry(fn_list, 0, fn_foo);
Packit bfcc33
  sass_option_set_c_functions(ctx_opt, fn_list);
Packit bfcc33
Packit bfcc33
  // context is set up, call the compile step now
Packit bfcc33
  int status = sass_compile_file_context(file_ctx);
Packit bfcc33
Packit bfcc33
  // print the result or the error to the stdout
Packit bfcc33
  if (status == 0) puts(sass_context_get_output_string(ctx));
Packit bfcc33
  else puts(sass_context_get_error_message(ctx));
Packit bfcc33
Packit bfcc33
  // release allocated memory
Packit bfcc33
  sass_delete_file_context(file_ctx);
Packit bfcc33
Packit bfcc33
  // exit status
Packit bfcc33
  return status;
Packit bfcc33
Packit bfcc33
}
Packit bfcc33
```
Packit bfcc33
Packit bfcc33
### Compile main.c
Packit bfcc33
Packit bfcc33
```bash
Packit bfcc33
gcc -c main.c -o main.o
Packit bfcc33
gcc -o sample main.o -lsass
Packit bfcc33
echo "foo { margin: foo(); }" > foo.scss
Packit bfcc33
./sample foo.scss => "foo { margin: 42px }"
Packit bfcc33
```
Packit bfcc33