aboutsummaryrefslogtreecommitdiff
path: root/ext_internal.c
blob: 3199eb7049088041cdea81b374ef4bb14aad3d52 (plain) (blame)
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
//
// Created by jhunk on 7/2/24.
//
#include "ext_internal.h"
#include "extensions/ext.h"

void asdfapi_ext_show_descriptor(struct ASDFExtension *info) {
    printf("Extension name: %s\n", info->name);
    printf("Extension description: %s\n", info->desc);
}

void *asdfapi_ext_load(struct ASDFExtension **info, const char *filename) {
    fnptr_ext_init ext_init;

    printf("Loading extension: %s\n", filename);
    void *handle_ext = dlopen(filename, RTLD_LAZY);
    if (handle_ext) {
        dlerror();
        ext_init = dlsym(handle_ext, "asdfapi_ext_init");
        if (ext_init) {
            (*ext_init)();
            dlerror();
            *info = dlsym(handle_ext, "asdfapi_ext_descriptor");
            if (!*info) {
                fprintf(stderr, "error reading extension descriptor: %s\n", dlerror());
                goto fail;
            }
        } else {
            fprintf(stderr, "error reading extension function: %s\n", dlerror());
            goto fail;
        }
    } else {
        fprintf(stderr, "error opening extension: %s\n", dlerror());
        goto fail;
    }
    return handle_ext;
    fail:
    return NULL;
}

void *asdfapi_ext_call(void *handle, const char *sym) {
    const char *prefix = "asdfapi_ext_";
    char name[255] = {0};
    snprintf(name, sizeof(name) - 1, "%s%s", prefix, sym);
    return dlsym(handle, name);
}