> For the complete documentation index, see [llms.txt](https://daggy.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://daggy.gitbook.io/docs/daggy-core/ansi-c11-interface.md).

# ANSI C11 interface

## ANSI C functions

{% code title="Core.h" %}

```c
#include "daggycore_export.h"

#include "Types.h"
#include "Errors.h"

#ifdef __cplusplus
extern "C" {
#endif

DAGGYCORE_EXPORT DaggyErrors libdaggy_core_create(const char* sources,
                                                  DaggySourcesTextTypes text_type,
                                                  DaggyCore* core);
DAGGYCORE_EXPORT DaggyErrors libdaggy_core_start(DaggyCore core);
DAGGYCORE_EXPORT DaggyErrors libdaggy_core_stop(DaggyCore core);
DAGGYCORE_EXPORT void libdaggy_core_destroy(DaggyCore* core);

DAGGYCORE_EXPORT DaggyErrors libdaggy_connect_aggregator(DaggyCore core,
                                                         libdaggy_on_daggy_state_changed on_daggy_state_changed,
                                                         libdaggy_on_provider_state_changed on_provider_state_changed,
                                                         libdaggy_on_provider_error on_provider_error,
                                                         libdaggy_on_command_state_changed on_command_state_changed,
                                                         libdaggy_on_command_stream on_command_stream,
                                                         libdaggy_on_command_error on_command_error);


DAGGYCORE_EXPORT void libdaggy_app_create(int argc, char** argv);
DAGGYCORE_EXPORT int libdaggy_app_exec();
DAGGYCORE_EXPORT void libdaggy_app_stop();

DAGGYCORE_EXPORT void libdaggy_run_in_thread(libdaggy_thread_function function, void* parameter);

#ifdef __cplusplus
}
#endif

```

{% endcode %}

## Usage Example

{% code title="test.c" %}

```c
#include <stdio.h>
#ifdef _WIN32
#include <Windows.h>
#else
#include <unistd.h>
#endif

#include <DaggyCore/Core.h>

const char* json_data =
"{\
    \"sources\": {\
        \"localhost\" : {\
            \"type\": \"local\",\
            \"commands\": {\
                \"ping1\": {\
                    \"exec\": \"ping 127.0.0.1\",\
                    \"extension\": \"log\"\
                },\
                \"ping2\": {\
                    \"exec\": \"ping 127.0.0.1\",\
                    \"extension\": \"log\"\
                    }\
            }\
        }\
    }\
}"
;

void sleep_ms(int milliseconds)
{
    #ifdef WIN32
        Sleep(milliseconds);
    #elif _POSIX_C_SOURCE >= 199309L
        struct timespec ts;
        ts.tv_sec = milliseconds / 1000;
        ts.tv_nsec = (milliseconds % 1000) * 1000000;
        nanosleep(&ts, NULL);
    #else
        usleep(milliseconds * 1000);
    #endif
}

int quit_after_time(void* msec)
{
    sleep_ms(*(int*)(msec));
    libdaggy_app_stop();
    return 0;
}

void on_daggy_state_changed(DaggyCore core, DaggyStates state);

void on_provider_state_changed(DaggyCore core, const char* provider_id, DaggyProviderStates state);
void on_provider_error(DaggyCore core, const char* provider_id, DaggyError error);

void on_command_state_changed(DaggyCore core, const char* provider_id, const char* command_id, DaggyCommandStates state, int exit_code);
void on_command_stream(DaggyCore core, const char* provider_id, const char* command_id, DaggyStream stream);
void on_command_error(DaggyCore core, const char* provider_id, const char* command_id, DaggyError error);

int main(int argc, char** argv)
{
    DaggyCore core;
    libdaggy_app_create(argc, argv);
    libdaggy_core_create(json_data, Json, &core);
    libdaggy_connect_aggregator(core,
                                on_daggy_state_changed,
                                on_provider_state_changed,
                                on_provider_error,
                                on_command_state_changed,
                                on_command_stream,
                                on_command_error);
    libdaggy_core_start(core);
    int time = 5000;
    libdaggy_run_in_thread(quit_after_time, &time);
    return libdaggy_app_exec();
}

void on_daggy_state_changed(DaggyCore core, DaggyStates state)
{
    printf("Daggy state changed: %d\n", state);
}

void on_provider_state_changed(DaggyCore core, const char* provider_id, DaggyProviderStates state)
{
    printf("Provider %s state changed: %d\n", provider_id, state);
}

void on_provider_error(DaggyCore core, const char* provider_id, DaggyError error)
{
    printf("Provider %s error. Code: %d, Category: %s\n", provider_id, error.error, error.category);
}

void on_command_state_changed(DaggyCore core, const char* provider_id, const char* command_id, DaggyCommandStates state, int exit_code)
{
    printf("Command %s in provider %s state changed: %d\n", command_id, provider_id, state);
}

void on_command_stream(DaggyCore core, const char* provider_id, const char* command_id, DaggyStream stream)
{
    printf("Command %s in provider %s has stream from session %s: %li\n", command_id, provider_id, stream.session, stream.seq_num);
}

void on_command_error(DaggyCore core, const char* provider_id, const char* command_id, DaggyError error)
{
    printf("Command %s in provider %s has error. Code: %d, Category: %s\n", command_id, provider_id, error.error, error.category);
}

```

{% endcode %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://daggy.gitbook.io/docs/daggy-core/ansi-c11-interface.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
