From e6e7f4c8ad9bffd536182923c8e556ca028cec04 Mon Sep 17 00:00:00 2001 From: bigfoot547 Date: Sun, 16 Nov 2025 13:01:59 -0600 Subject: [wip] plugin stuff --- connection.h | 15 ------------- include/connection.h | 15 +++++++++++++ include/pipeline.h | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++ main.c | 23 +++++++++++++++---- meson.build | 10 ++++++--- pipeline.h | 56 ---------------------------------------------- plugins/meson.build | 2 ++ plugins/skibidi.c | 7 ++++++ 8 files changed, 113 insertions(+), 78 deletions(-) delete mode 100644 connection.h create mode 100644 include/connection.h create mode 100644 include/pipeline.h delete mode 100644 pipeline.h create mode 100644 plugins/meson.build create mode 100644 plugins/skibidi.c diff --git a/connection.h b/connection.h deleted file mode 100644 index 416e076..0000000 --- a/connection.h +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef PTXMC_CONNECTION_H_INCLUDED -#define PTXMC_CONNECTION_H_INCLUDED - -#include "pipeline.h" - -struct ptx_connection -{ - ptx_pipeline_t *cli_read, *cli_write; - ptx_pipeline_t *srv_read, *srv_write; -}; - -struct ptx_connection *ptx_connection_new(void); -void ptx_connection_free(struct ptx_connection *conn); - -#endif /* include guard */ diff --git a/include/connection.h b/include/connection.h new file mode 100644 index 0000000..416e076 --- /dev/null +++ b/include/connection.h @@ -0,0 +1,15 @@ +#ifndef PTXMC_CONNECTION_H_INCLUDED +#define PTXMC_CONNECTION_H_INCLUDED + +#include "pipeline.h" + +struct ptx_connection +{ + ptx_pipeline_t *cli_read, *cli_write; + ptx_pipeline_t *srv_read, *srv_write; +}; + +struct ptx_connection *ptx_connection_new(void); +void ptx_connection_free(struct ptx_connection *conn); + +#endif /* include guard */ diff --git a/include/pipeline.h b/include/pipeline.h new file mode 100644 index 0000000..d662b09 --- /dev/null +++ b/include/pipeline.h @@ -0,0 +1,63 @@ +#ifndef PTXMC_PIPELINE_H_INCLUDED +#define PTXMC_PIPELINE_H_INCLUDED + +#include +#include +#include + +#include "config.h" + +#define PIPELINE_STAGE ATTR_WUR ATTR_ACCESS((read_only, 1)) +#define PIPELINE_INIT ATTR_ACCESS((read_only, 1)) +#define PIPELINE_CLEANUP ATTR_ACCESS((read_only, 1)) + +typedef struct ptx__pipeline_ctx_tag ptx_pipeline_ctx_t; +typedef struct ptx__pipeline_tag ptx_pipeline_t; + +typedef int (ptx_pipeline_proc_t)(const ptx_pipeline_ctx_t * /*ctx*/, const void * /*data*/, size_t /*len*/) PIPELINE_STAGE; +typedef int (ptx_pipeline_init_proc_t)(const char * /*name*/, void ** /*ppuser*/, va_list /*init_args*/) PIPELINE_INIT; +typedef void (ptx_pipeline_cleanup_proc_t)(const char * /*name*/, void * /*puser*/) PIPELINE_CLEANUP; + +struct ptx_pipeline_stage_funcs { + ptx_pipeline_proc_t *handler; + ptx_pipeline_init_proc_t *init; + ptx_pipeline_cleanup_proc_t *cleanup; +}; + +#ifdef MODULE +#define MODEXP extern +#else +#define MODEXP \ +__attribute__((visibility("protected"))) +#endif + +/* allocates a new pipeline, with space for `initial_size' new stages. */ +MODEXP ptx_pipeline_t *ptx_pipeline_new(size_t initial_size) ATTR_WUR ATTR_MALLOC() ATTR_MALLOC((ptx_pipeline_free, 1)); + +/* frees an existing pipeline stage. note that this will call the cleanup function in the stages as well. */ +void ptx_pipeline_free(ptx_pipeline_t *pl); + +/* adds `stage' with `name' to `pipeline' after `after' + * returns: number of stages added to pipeline (0 or 1), or -1 if there was an allocation or initialization error. */ +int ptx_pipeline_add_after(ptx_pipeline_t *pipeline, const struct ptx_pipeline_stage_funcs *stage, const char *name, const char *after, ...); +/* adds `stage' with `name' to `pipeline' before `before' + * returns: number of stages added to pipeline (0 or 1), or -1 if there was an allocation or initialization error. */ +int ptx_pipeline_add_before(ptx_pipeline_t *pipeline, const struct ptx_pipeline_stage_funcs *stage, const char *name, const char *before, ...); + +/* TODO: could theoretically have versions of the above functions that don't defensively copy `name' */ + +/* returns: number of stages removed from pipeline */ +int ptx_pipeline_remove_stage(ptx_pipeline_t *pipeline, const char *name); + +/* this pointer is guaranteed to be valid as long as the pipeline context is active (i.e., within this handler function) + * If you need to access user data of a stage from outside of a handler function, TODO */ +void **ptx_pipeline_ctx_get_user(const ptx_pipeline_ctx_t *ctx); +const char *ptx_pipeline_ctx_get_name(const ptx_pipeline_ctx_t *ctx); + +int ptx_pipeline_ctx_next(const ptx_pipeline_ctx_t *ctx, const void *nextdata, size_t nextsize); + +void ptx_pipeline_ctx_diag(const ptx_pipeline_ctx_t *ctx, int lvl, const char *fmt, ...) ATTR_FORMAT((printf, 3, 4)); /* TODO: pipeline stages should be able to send diagnostic messages */ + +int ptx_pipeline_handle(ptx_pipeline_t *pipeline, const void *data, size_t sz); /* TODO: diagnostic */ + +#endif /* include guard */ diff --git a/main.c b/main.c index 111424c..273d797 100644 --- a/main.c +++ b/main.c @@ -2,17 +2,18 @@ #include #include #include +#include #include "pipeline.h" #define UNUSED(_v) ((void)(_v)) -PIPELINE_STAGE int identity_stage_handle(const ptx_pipeline_ctx_t *ctx, const void *data, size_t sz) +PIPELINE_STAGE static int identity_stage_handle(const ptx_pipeline_ctx_t *ctx, const void *data, size_t sz) { return ptx_pipeline_ctx_next(ctx, data, sz); } -PIPELINE_INIT int debug_stage_init(const char *name, void **ppuser, va_list init_args) +PIPELINE_INIT static int debug_stage_init(const char *name, void **ppuser, va_list init_args) { static const char *const messages[] = { "no continue :(", "continue :)" }; @@ -26,12 +27,12 @@ PIPELINE_INIT int debug_stage_init(const char *name, void **ppuser, va_list init return 0; } -PIPELINE_CLEANUP void debug_stage_cleanup(const char *name, void *puser) +PIPELINE_CLEANUP static void debug_stage_cleanup(const char *name, void *puser) { printf("Debug stage %s (cleanup): %p\n", name, puser); } -PIPELINE_STAGE int debug_stage_handle(const ptx_pipeline_ctx_t *ctx, const void *data, size_t sz) +PIPELINE_STAGE static int debug_stage_handle(const ptx_pipeline_ctx_t *ctx, const void *data, size_t sz) { bool cont = !!(uintptr_t)(*ptx_pipeline_ctx_get_user(ctx)); printf("Debug stage %s:\n Data: %p\n Size: %zu\n", ptx_pipeline_ctx_get_name(ctx), data, sz); @@ -121,5 +122,19 @@ int main(void) { ptx_pipeline_free(pl); + void *mod; + if (!(mod = dlopen("plugins/libm_skibidi.so", RTLD_LAZY | RTLD_LOCAL))) { + printf("dlopen: %s\n", dlerror()); + return 1; + } + + int (*f)(void) = dlsym(mod, "do_something"); + if (!f) { + printf("dlsym: %s\n", dlerror()); + return 1; + } + + printf("f: %d\n", f()); + return 0; } diff --git a/meson.build b/meson.build index da4fe0e..03d51a4 100644 --- a/meson.build +++ b/meson.build @@ -1,4 +1,5 @@ project('ptxmc', 'c', default_options : ['c_std=c11']) +add_project_arguments('-D_XOPEN_SOURCE=700', language : [ 'c' ]) c_comp = meson.get_compiler('c') @@ -11,10 +12,13 @@ conf_data.set('HAS_RESTRICT', c_comp.has_type('int *restrict')) configure_file(input : 'config.h.in', output : 'config.h', configuration : conf_data) conf_include = include_directories('.') +src_include = include_directories('include') executable('ptxmc', - 'main.c', 'pipeline.c', 'connection.c', - include_directories : conf_include, - c_args : [ '-D_XOPEN_SOURCE=700' ]) + 'main.c', + include_directories : [ conf_include, src_include ], + link_args : [ '-rdynamic' ]) + +subdir('plugins') diff --git a/pipeline.h b/pipeline.h deleted file mode 100644 index c0feb56..0000000 --- a/pipeline.h +++ /dev/null @@ -1,56 +0,0 @@ -#ifndef PTXMC_PIPELINE_H_INCLUDED -#define PTXMC_PIPELINE_H_INCLUDED - -#include -#include -#include - -#include "config.h" - -#define PIPELINE_STAGE ATTR_WUR ATTR_ACCESS((read_only, 1)) -#define PIPELINE_INIT ATTR_ACCESS((read_only, 1)) -#define PIPELINE_CLEANUP ATTR_ACCESS((read_only, 1)) - -typedef struct ptx__pipeline_ctx_tag ptx_pipeline_ctx_t; -typedef struct ptx__pipeline_tag ptx_pipeline_t; - -typedef int (ptx_pipeline_proc_t)(const ptx_pipeline_ctx_t * /*ctx*/, const void * /*data*/, size_t /*len*/) PIPELINE_STAGE; -typedef int (ptx_pipeline_init_proc_t)(const char * /*name*/, void ** /*ppuser*/, va_list /*init_args*/) PIPELINE_INIT; -typedef void (ptx_pipeline_cleanup_proc_t)(const char * /*name*/, void * /*puser*/) PIPELINE_CLEANUP; - -struct ptx_pipeline_stage_funcs { - ptx_pipeline_proc_t *handler; - ptx_pipeline_init_proc_t *init; - ptx_pipeline_cleanup_proc_t *cleanup; -}; - -/* allocates a new pipeline, with space for `initial_size' new stages. */ -ptx_pipeline_t *ptx_pipeline_new(size_t initial_size) ATTR_WUR ATTR_MALLOC() ATTR_MALLOC((ptx_pipeline_free, 1)); - -/* frees an existing pipeline stage. note that this will call the cleanup function in the stages as well. */ -void ptx_pipeline_free(ptx_pipeline_t *pl); - -/* adds `stage' with `name' to `pipeline' after `after' - * returns: number of stages added to pipeline (0 or 1), or -1 if there was an allocation or initialization error. */ -int ptx_pipeline_add_after(ptx_pipeline_t *pipeline, const struct ptx_pipeline_stage_funcs *stage, const char *name, const char *after, ...); -/* adds `stage' with `name' to `pipeline' before `before' - * returns: number of stages added to pipeline (0 or 1), or -1 if there was an allocation or initialization error. */ -int ptx_pipeline_add_before(ptx_pipeline_t *pipeline, const struct ptx_pipeline_stage_funcs *stage, const char *name, const char *before, ...); - -/* TODO: could theoretically have versions of the above functions that don't defensively copy `name' */ - -/* returns: number of stages removed from pipeline */ -int ptx_pipeline_remove_stage(ptx_pipeline_t *pipeline, const char *name); - -/* this pointer is guaranteed to be valid as long as the pipeline context is active (i.e., within this handler function) - * If you need to access user data of a stage from outside of a handler function, TODO */ -void **ptx_pipeline_ctx_get_user(const ptx_pipeline_ctx_t *ctx); -const char *ptx_pipeline_ctx_get_name(const ptx_pipeline_ctx_t *ctx); - -int ptx_pipeline_ctx_next(const ptx_pipeline_ctx_t *ctx, const void *nextdata, size_t nextsize); - -void ptx_pipeline_ctx_diag(const ptx_pipeline_ctx_t *ctx, int lvl, const char *fmt, ...) ATTR_FORMAT((printf, 3, 4)); /* TODO: pipeline stages should be able to send diagnostic messages */ - -int ptx_pipeline_handle(ptx_pipeline_t *pipeline, const void *data, size_t sz); /* TODO: diagnostic */ - -#endif /* include guard */ diff --git a/plugins/meson.build b/plugins/meson.build new file mode 100644 index 0000000..efa8e5d --- /dev/null +++ b/plugins/meson.build @@ -0,0 +1,2 @@ +shared_library('m_skibidi', 'skibidi.c', + include_directories : [ conf_include, src_include ]) diff --git a/plugins/skibidi.c b/plugins/skibidi.c new file mode 100644 index 0000000..4fa4c33 --- /dev/null +++ b/plugins/skibidi.c @@ -0,0 +1,7 @@ +#define MODULE 1 +#include "pipeline.h" + +__attribute__((visibility("default"))) int do_something(void) +{ + return !!ptx_pipeline_new(4); +} -- cgit v1.2.3-70-g09d2