StructOp Handbook
Contents Examples Commands

Lib curl

Console program123 lines of .st198 lines of generated C
Program output
fetched:
Hello from a local file,
fetched by libcurl.
On this page

The .st files#

intent.st
structop {
    type_app {
        exec = {
            lang     = "C"
            standard = "c11"
        }
    }

    lib_provider {
        C_lang = {
            source  = "StructOp/c"
            version = "@latest"
        }
        Libc = {
            source  = "C/libc"
            version = "@latest"
        }
        Curl = {
            source  = "C/curl"
            version = "@latest"
        }
    }
}

Input, plan and generated C#

bind/libc.bind.st · hints for structop bind
bind/libc.bind.st · hints for structop bind
# The C standard library: macOS and Linux spell these parameters differently (or not at all),
# so names gives every kept function the same attribute names on both.
bind "libc" {
    header  = "stdlib.h"
    version = "1.0.0"
    prefix  = ["qsort", "abs", "atoi", "getenv"]
    skip    = ["qsort_r", "qsort_b", "getenv_r"]
}

function "qsort" {
    names     = ["base", "count", "width", "compar"]
    array     = [{ data = "base", count = "count", size = "width", of = Int32 }]
    points_to = Int32
}

function "abs" {
    names = ["value"]
}

function "atoi" {
    names = ["text"]
}

function "getenv" {
    names        = ["name"]
    returns_text = "library"
}
bind/curl.bind.st · hints for structop bind
bind/curl.bind.st · hints for structop bind
# libcurl: curl_easy_setopt takes its value through "...", so each kind of value is a variant
# with its own ability kind; the write callback gets the program state through CURLOPT_WRITEDATA.
bind "curl" {
    header  = "curl/curl.h"
    package = "libcurl"
    prefix  = ["curl_easy_"]
}

function "curl_easy_setopt" {
    variant "curl_setopt_text" {
        value = String
    }
    variant "curl_setopt_long" {
        value = Int64
    }
    variant "curl_setopt_write" {
        value     = callback("size_t (*)(char *, size_t, size_t, void *)")
        user_data = "CURLOPT_WRITEDATA"
        arg_names = { value = ["ptr", "size", "nmemb", ""] }
        text      = [{ data = "ptr", length = ["size", "nmemb"] }]
    }
}
structop plan
structop plan
  # program.app will be created
  + program.app (.structop/build/app)
      + ability.value.curl
      + ability.value.dir
      + ability.value.body
      + ability.value.code
      + ability.value.reason
      + ability.getenv.Dir
      + ability.curl_easy_init.Init
      + ability.curl_setopt_text.Address
      + ability.curl_setopt_write.Collect
      + ability.append.Chunk
      + ability.curl_easy_perform.Fetch
      + ability.if.Fetched
      + ability.print.Body
      + ability.curl_easy_strerror.Why
      + ability.print.Failed
      + ability.curl_easy_cleanup.Done

Plan: 1 to add, 0 to change, 0 to destroy.
Generated main.c

To inspect this generated file, select C in intent.st, initialize the project and export its source.

Export locally
structop init
structop apply -source -no-run
Try it
structop bind bind/libc.bind.st    # from the repository root
structop bind bind/curl.bind.st    # from the repository root
cd lib-curl    # a folder holding the .st files above
structop init
structop plan
structop apply