splinter: plugin ABI, widget sidebar, libsplinter

A plugin is a .so against libsplinter.so, exporting sp_plugin_describe().
The layout changes so there is exactly one copy of each symbol in the
process: all logic lives in libsplinter.so and both the binary and every
plugin link against it. Two copies would have meant two registries.

What the ABI offers:
- tools with the authorization classes the loop already enforces, so the
  decision stays in the harness and not in the plugin;
- widgets rendered in the sidebar, one line per widget (name: text);
- a worker thread per widget with tick(), which may block, read files, wait
  on sockets - and which cannot touch curses at all;
- sp_host_widget_text() as the only thread-safe write path: the host copies
  the string under a mutex, so a stack buffer is safe and the UI never holds
  a pointer a worker could free.

Error paths are part of the design: a wrong ABI number is refused with both
numbers in the message, a failed init is reported and skipped, a duplicate
widget name is refused, a tool without a run function is not registered, and
nothing is dlclose()d while a thread might be inside it.

Layout: chat on three quarters, sidebar on one (session, transport, theme,
last-turn numbers, widgets), status bar underneath. The clock example plugin
proves the path end to end - its widget publishes 11:45:23 into the sidebar
from its own thread.

Seven test binaries green; test_plugin loads the real clock.so with dlopen
and checks the worker actually published, plus every refusal above.
This commit is contained in:
Konrad committed 2026-11-07 11:47:47 +00:00
1 parent d2e06b316d
commit e4f910cc4e
14 files changed
+840 -76

No files matched your search

+61 -62
View File
@@ -1,32 +1,40 @@
# splinter -- POSIX make for our own code, GNU make only for the vendored curses.
#
# make builds the binary (with the curses TUI)
# make libsplinter.so, the splinter binary, the example plugin
# make check builds and runs every test binary
# make curses builds the vendored NetBSD curses explicitly
# make clean removes generated files only
#
# Generated files never mix with handwritten ones, and nothing here needs a
# configure step: the dependency set is libc, the vendored NetBSD curses, and
# curl(1) at runtime if you use --endpoint.
# The layout is deliberate: all logic lives in libsplinter.so. The binary and
# every plugin link against that one library, so there is exactly one copy of
# each symbol in the process. Two copies would mean two registries.
CC ?= cc
CFLAGS ?= -std=c99 -O2 -Wall -Wextra -Werror -D_POSIX_C_SOURCE=200809L \
-Iinclude -Isrc -Ivendor/netbsd-curses/libcurses
-fPIC -Iinclude -Isrc -Ivendor/netbsd-curses/libcurses
LDFLAGS ?=
LIBS ?=
LIBS ?= -lpthread
PREFIX ?= /usr/local
CURSES_DIR = vendor/netbsd-curses
CURSES_A = $(CURSES_DIR)/libcurses/libcurses.a \
$(CURSES_DIR)/libterminfo/libterminfo.a
LIB = libsplinter.so
LIBOBJ = src/buf.o src/tool.o src/session.o src/transport.o \
src/transport_curl.o src/builtin.o src/agent.o src/theme.o \
src/palettes.o src/plugin.o
BIN = splinter
OBJ = src/buf.o src/tool.o src/session.o src/transport.o src/transport_curl.o \
src/builtin.o src/agent.o src/theme.o src/palettes.o src/ui.o
BINOBJ = src/main.o src/ui.o
PLUGIN = plugins/clock/clock.so
TESTS = tests/test_buf tests/test_tool tests/test_session tests/test_transport \
tests/test_agent tests/test_theme
tests/test_agent tests/test_theme tests/test_plugin
all: $(BIN)
RPATH = -Wl,-rpath,'$$ORIGIN'
PLUGIN_RPATH = -Wl,-rpath,'$$ORIGIN/../..'
TEST_RPATH = -Wl,-rpath,'$$ORIGIN/..'
all: $(LIB) $(BIN) $(PLUGIN)
# The vendored tree ships a GNUmakefile; this is the only place that needs GNU
# make, and it is why the comment above says so out loud.
@@ -36,69 +44,60 @@ $(CURSES_A):
curses: $(CURSES_A)
$(BIN): $(OBJ) src/main.o $(CURSES_A)
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(OBJ) src/main.o $(CURSES_A) $(LIBS)
$(LIB): $(LIBOBJ) $(CURSES_A)
$(CC) -shared -o $@ $(LIBOBJ) $(CURSES_A) $(LIBS)
src/buf.o: src/buf.c include/splinter.h
$(CC) $(CFLAGS) -c -o $@ src/buf.c
# curses belongs to the front end, not to the library: a plugin that drew into
# a WINDOW would be a plugin holding the UI thread's state.
$(BIN): $(BINOBJ) $(LIB)
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(BINOBJ) -L. -lsplinter $(RPATH) \
$(CURSES_A) $(LIBS)
src/tool.o: src/tool.c include/splinter.h
$(CC) $(CFLAGS) -c -o $@ src/tool.c
$(PLUGIN): plugins/clock/clock.c $(LIB)
$(CC) $(CFLAGS) -shared -o $@ plugins/clock/clock.c -L. -lsplinter \
$(PLUGIN_RPATH) $(LIBS)
src/session.o: src/session.c include/splinter.h
$(CC) $(CFLAGS) -c -o $@ src/session.c
.c.o:
$(CC) $(CFLAGS) -c -o $@ $<
src/transport.o: src/transport.c include/splinter.h
$(CC) $(CFLAGS) -c -o $@ src/transport.c
src/agent.o: include/splinter.h
src/buf.o: include/splinter.h
src/tool.o: include/splinter.h
src/session.o: include/splinter.h
src/transport.o: include/splinter.h
src/transport_curl.o: include/splinter.h
src/builtin.o: include/splinter.h
src/theme.o: src/theme.h src/palettes.h include/splinter.h
src/palettes.o: src/palettes.h
src/plugin.o: include/splinter.h include/splinter_plugin.h
src/ui.o: src/ui.h src/theme.h include/splinter.h include/splinter_plugin.h
src/main.o: include/splinter.h include/splinter_plugin.h src/ui.h src/theme.h
src/transport_curl.o: src/transport_curl.c include/splinter.h
$(CC) $(CFLAGS) -c -o $@ src/transport_curl.c
tests/test_buf: tests/test_buf.c $(LIB)
$(CC) $(CFLAGS) -o $@ tests/test_buf.c -L. -lsplinter $(TEST_RPATH) $(LIBS)
tests/test_tool: tests/test_tool.c $(LIB)
$(CC) $(CFLAGS) -o $@ tests/test_tool.c -L. -lsplinter $(TEST_RPATH) $(LIBS)
tests/test_session: tests/test_session.c $(LIB)
$(CC) $(CFLAGS) -o $@ tests/test_session.c -L. -lsplinter $(TEST_RPATH) $(LIBS)
tests/test_transport: tests/test_transport.c $(LIB)
$(CC) $(CFLAGS) -o $@ tests/test_transport.c -L. -lsplinter $(TEST_RPATH) $(LIBS)
tests/test_agent: tests/test_agent.c $(LIB)
$(CC) $(CFLAGS) -o $@ tests/test_agent.c -L. -lsplinter $(TEST_RPATH) $(LIBS)
tests/test_theme: tests/test_theme.c $(LIB)
$(CC) $(CFLAGS) -o $@ tests/test_theme.c -L. -lsplinter $(TEST_RPATH) $(LIBS)
tests/test_plugin: tests/test_plugin.c $(LIB) $(PLUGIN)
$(CC) $(CFLAGS) -o $@ tests/test_plugin.c -L. -lsplinter $(TEST_RPATH) $(LIBS)
src/builtin.o: src/builtin.c include/splinter.h
$(CC) $(CFLAGS) -c -o $@ src/builtin.c
src/agent.o: src/agent.c include/splinter.h
$(CC) $(CFLAGS) -c -o $@ src/agent.c
src/theme.o: src/theme.c src/theme.h src/palettes.h include/splinter.h
$(CC) $(CFLAGS) -c -o $@ src/theme.c
src/palettes.o: src/palettes.c src/palettes.h
$(CC) $(CFLAGS) -c -o $@ src/palettes.c
src/ui.o: src/ui.c src/ui.h src/theme.h include/splinter.h
$(CC) $(CFLAGS) -c -o $@ src/ui.c
src/main.o: src/main.c include/splinter.h src/ui.h src/theme.h
$(CC) $(CFLAGS) -c -o $@ src/main.c
tests/test_buf: tests/test_buf.c $(OBJ)
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ tests/test_buf.c $(OBJ) $(CURSES_A) $(LIBS)
tests/test_tool: tests/test_tool.c $(OBJ)
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ tests/test_tool.c $(OBJ) $(CURSES_A) $(LIBS)
tests/test_session: tests/test_session.c $(OBJ)
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ tests/test_session.c $(OBJ) $(CURSES_A) $(LIBS)
tests/test_transport: tests/test_transport.c $(OBJ)
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ tests/test_transport.c $(OBJ) $(CURSES_A) $(LIBS)
tests/test_agent: tests/test_agent.c $(OBJ)
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ tests/test_agent.c $(OBJ) $(CURSES_A) $(LIBS)
tests/test_theme: tests/test_theme.c $(OBJ)
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ tests/test_theme.c $(OBJ) $(CURSES_A) $(LIBS)
check: $(BIN) $(TESTS)
check: $(LIB) $(BIN) $(PLUGIN) $(TESTS)
@set -e; for t in $(TESTS); do echo "== $$t"; ./$$t; done; \
echo "all tests passed"
install: $(BIN)
mkdir -p $(DESTDIR)$(PREFIX)/bin
install: all
mkdir -p $(DESTDIR)$(PREFIX)/bin $(DESTDIR)$(PREFIX)/lib
cp $(BIN) $(DESTDIR)$(PREFIX)/bin/
cp $(LIB) $(DESTDIR)$(PREFIX)/lib/
clean:
rm -f $(BIN) $(OBJ) src/main.o $(TESTS)
rm -f $(BIN) $(LIB) $(LIBOBJ) $(BINOBJ) $(PLUGIN) $(TESTS)
.PHONY: all check curses install clean
+35 -5
View File
@@ -1,10 +1,12 @@
# splinter
A small C99/POSIX agent harness: one turn, a tool registry, explicit
authorization classes, an append-only session log.
A C99/POSIX agent harness with a curses interface: chat on three quarters of
the screen, a sidebar on one quarter, plugins that can put tools and widgets
into both.
make
make check
make # libsplinter.so, the binary, the example plugin
make check # seven test binaries
./splinter --chat --plugin plugins/clock/clock.so --script replies.txt
## What it does
@@ -36,7 +38,17 @@ transport replays replies.
## Layout
include/splinter.h the whole interface
libsplinter.so every symbol, once; binary and plugins link to it
include/splinter.h the core interface
include/splinter_plugin.h plugin ABI and host API
src/plugin.c dlopen loader, widget registry, worker threads
src/ui.c curses UI: chat 3/4, sidebar 1/4, status bar
src/theme.c CDE palettes -> terminal colours
src/palettes.c generated from CDE's *.dp files
tools/genpalettes.py the generator
plugins/clock/ example plugin: sidebar clock + a tool
vendor/netbsd-curses/ NetBSD curses and terminfo, vendored
docs/PLUGINS.md the ABI, the threading rules
src/buf.c growable buffer, JSON escaping
src/tool.c registry
src/session.c append-only JSONL session
@@ -49,6 +61,19 @@ transport replays replies.
docs/splinter.1 manual page
src/www/index.html HTML 4.01 static page
## Plugins and themes
A plugin is a `.so` against `libsplinter.so`, exporting
`sp_plugin_describe()`. It can register tools (with authorization classes the
loop enforces) and widgets (rendered in the sidebar). A widget's `tick()` runs
on its own thread and publishes text; only the UI thread draws. See
`docs/PLUGINS.md`.
Themes are CDE palettes: 42 of them, generated from the desktop's own `.dp`
files. Roles are derived from the eight colour sets — darkest is the
background, lightest the foreground, the most saturated the accent. Default is
Delphinium. `--list-themes` prints the names, `--theme NAME` picks one.
## Limits, stated
- Tool arguments reach the tool as one string; each tool validates its own.
@@ -56,6 +81,11 @@ transport replays replies.
the loop stays testable; an OpenAI adapter is the next piece.
- No compaction, no retrieval tiers. When a turn does not fit the budget it
fails loudly, which is the honest behaviour until compaction exists.
- A `tick()` that never returns hangs the exit: the host can stop a thread only
between ticks, not inside one.
- Plugins are not sandboxed. They run in the process with full rights; the
authorization classes protect the harness from the *model*, not from code you
chose to load.
## License
+92
View File
@@ -0,0 +1,92 @@
# Plugins
A splinter plugin is a shared object that links against `libsplinter.so` and
exports one symbol:
```c
const struct sp_plugin *sp_plugin_describe(void);
```
The struct, the ABI number and the host functions are in
`include/splinter_plugin.h`. Nothing else is stable. `SP_PLUGIN_ABI` changes
whenever that header changes in a way that breaks an old plugin, and a plugin
with the wrong number is refused at load time with the two numbers in the
message.
## Build
```sh
cc -std=c99 -shared -fPIC -o myplugin.so myplugin.c -L. -lsplinter \
-Wl,-rpath,'$ORIGIN'
```
`splinter --plugin ./myplugin.so --chat` loads it before the first turn. A
plugin that fails to load is reported on stderr and skipped; the harness keeps
working.
## What a plugin may do
| hook | thread | may block | may touch curses |
|------------|---------------|-----------|------------------|
| `init()` | main | no | no |
| `run()` | main | yes | no |
| `tick()` | its own | yes | no |
| `draw()` | main (UI) | no | no |
`tick()` runs on a thread the host creates and can stop. It may read files,
wait on sockets, sleep. It must not assume it is the only thread.
Anything that should appear in the sidebar is published:
```c
sp_host_widget_text(host, "mywidget", "42 items");
```
The host copies the string under a mutex, so a stack buffer is fine and the
worker never hands the UI a pointer it could free.
## Widgets
```c
static const struct sp_widget w = {
.name = "clock",
.tick = clock_tick, /* worker thread, publishes text */
.draw = NULL, /* NULL: render the published text */
.ctx = NULL,
.interval_ms = 1000, /* 0: tick once */
};
sp_host_add_widget(host, &w);
```
The sidebar lists one line per widget, `name: text`. `draw()` exists for
widgets that need more than one line; it runs on the UI thread and must be
fast, because it holds up every other pane while it runs.
## Threads, honestly
- One thread per widget with a `tick`. A widget without `tick` gets no thread.
- The host joins every thread in `sp_host_free()`, setting a stop flag first.
A `tick` that never returns therefore hangs the exit; that is a bug in the
plugin, not something the host can fix from outside.
- No plugin is `dlclose()`d. Unmapping code that a thread might still be
inside is how a harness dies in a way nobody can debug.
- The host pointer is thread-safe only for `sp_host_widget_text()` and
`sp_host_note()`. Everything else belongs to the main thread. That is a
rule, not an accident.
## Tools
A plugin registers tools with the same authorization classes the builtins use:
```c
struct sp_tool t = { 0 };
t.name = "clock";
t.description = "Report the local date and time.";
t.auth = SP_AUTH_READ;
t.run = clock_tool;
sp_host_add_tool(host, &t);
```
`SP_AUTH_READ` runs. `SP_AUTH_MUTATE` and `SP_AUTH_DESTRUCTIVE` ask the
operator unless the harness was started with `--autonomous`. The loop decides
this, not the plugin.
+78
View File
@@ -0,0 +1,78 @@
/*
* splinter -- plugin ABI.
*
* Copyright (c) 2026 Konrad Lother
* SPDX-License-Identifier: BSD-3-Clause
*
* A plugin is a shared object loaded with dlopen(). It links against
* libsplinter.so, never against the binary, so there is exactly one copy of
* every symbol in the process.
*
* What a plugin may do, and where:
*
* init() called once, on the main thread, before any turn. Register
* tools and widgets here. May fail; a failed plugin is unloaded
* from the set and reported, never half-used.
* run() called on the main thread while a turn is being handled.
* tick() called on the widget's own thread, so it may block, read
* files, wait on sockets. It must NOT touch curses and must NOT
* assume it is the only thread.
* draw() called on the UI thread only, must be fast and non-blocking.
* A widget without draw() is rendered from its published text.
*
* Text that should appear in the sidebar is published with
* sp_host_widget_text(); the host copies it under a mutex, so the string does
* not have to outlive the call.
*/
#ifndef SPLINTER_PLUGIN_H
#define SPLINTER_PLUGIN_H
#include <stdint.h>
#include "splinter.h"
#define SP_PLUGIN_ABI 1u
struct sp_host;
struct sp_widget {
const char *name; /* unique in the sidebar */
int (*tick)(struct sp_host *, const char *name, void *ctx);
void (*draw)(struct sp_host *, const char *name, void *ctx);
void *ctx;
unsigned interval_ms; /* 0: tick once, then stop */
};
struct sp_plugin {
uint32_t abi; /* must equal SP_PLUGIN_ABI */
const char *name;
const char *version;
int (*init)(struct sp_host *);
int (*run)(struct sp_host *, const char *input,
struct sp_buf *out);
void (*shutdown)(struct sp_host *);
};
/* Every plugin exports exactly this. */
const struct sp_plugin *sp_plugin_describe(void);
/* ---- host API, implemented by splinter, called by plugins ---- */
uint32_t sp_host_abi(struct sp_host *);
int sp_host_add_tool(struct sp_host *, const struct sp_tool *);
int sp_host_add_widget(struct sp_host *, const struct sp_widget *);
int sp_host_widget_text(struct sp_host *, const char *widget,
const char *text);
int sp_host_note(struct sp_host *, const char *text);
/* ---- host side, implemented by splinter, called by the harness ---- */
struct sp_host *sp_host_new(struct sp_registry *, struct sp_agent *,
struct sp_session_info *);
void sp_host_free(struct sp_host *);
int sp_plugin_load(struct sp_host *, const char *path,
struct sp_buf *err);
size_t sp_host_widget_snapshot(struct sp_host *, char *buf, size_t len);
size_t sp_host_widget_count(struct sp_host *);
#endif /* SPLINTER_PLUGIN_H */
Executable
BIN
View File
Binary file not shown.
+83
View File
@@ -0,0 +1,83 @@
/*
* splinter -- example plugin: a clock widget in the sidebar.
*
* Copyright (c) 2026 Konrad Lother
* SPDX-License-Identifier: BSD-3-Clause
*
* Shows the three parts of the ABI that matter:
* - init() registers a widget,
* - tick() runs on the host's worker thread and only publishes text,
* - run() adds a turn-level tool the harness can call.
*
* Build: cc -std=c99 -shared -fPIC -o clock.so clock.c -lsplinter
*/
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "splinter.h"
#include "splinter_plugin.h"
static int
clock_tick(struct sp_host *h, const char *name, void *ctx)
{
char out[128];
time_t now = time(NULL);
struct tm tm;
(void)ctx;
localtime_r(&now, &tm);
snprintf(out, sizeof(out), "%02d:%02d:%02d", tm.tm_hour, tm.tm_min,
tm.tm_sec);
return sp_host_widget_text(h, name, out);
}
static int
clock_tool(const char *args, struct sp_buf *out, void *ctx)
{
char line[64];
time_t now = time(NULL);
struct tm tm;
(void)args;
(void)ctx;
localtime_r(&now, &tm);
snprintf(line, sizeof(line), "%04d-%02d-%02d %02d:%02d:%02d",
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min,
tm.tm_sec);
return sp_buf_puts(out, line);
}
static int
clock_init(struct sp_host *h)
{
static const struct sp_widget w = {
"clock", clock_tick, NULL, NULL, 1000
};
struct sp_tool t;
if (sp_host_add_widget(h, &w) != 0)
return -1;
memset(&t, 0, sizeof(t));
t.name = "clock";
t.description = "Report the local date and time.";
t.auth = SP_AUTH_READ;
t.run = clock_tool;
return sp_host_add_tool(h, &t);
}
static const struct sp_plugin clock_plugin = {
SP_PLUGIN_ABI,
"clock",
"1.0.0",
clock_init,
NULL,
NULL
};
const struct sp_plugin *
sp_plugin_describe(void)
{
return &clock_plugin;
}
BIN
View File
Binary file not shown.
+43 -1
View File
@@ -15,6 +15,7 @@
#include <unistd.h>
#include "splinter.h"
#include "splinter_plugin.h"
#include "theme.h"
#include "ui.h"
@@ -27,6 +28,10 @@ usage(FILE *f)
" --endpoint URL transport: POST the line protocol to URL\n"
" --script FILE transport: replay replies from FILE, one\n"
" per line (no network; for tests and demos)\n"
" --plugin PATH load a plugin (.so) before the first turn\n"
" --chat interactive curses UI instead of one turn\n"
" --theme NAME CDE palette for the UI (default Delphinium)\n"
" --list-themes print the palette names and exit\n"
" --autonomous run mutating tools without asking\n"
" --steps N tool rounds per turn (default 8)\n"
" --session DIR session directory (default ~/.splinter)\n"
@@ -89,6 +94,9 @@ main(int argc, char **argv)
char default_id[64];
struct script_ctx sctx = { NULL, 0 };
int i, rc, chatmode = 0;
const char *plugins[16];
size_t nplugins = 0;
struct sp_host *host = NULL;
memset(&reg, 0, sizeof(reg));
memset(&ag, 0, sizeof(ag));
@@ -116,6 +124,14 @@ main(int argc, char **argv)
if (!need_value(argc, i, argv[i]))
return 2;
script = argv[++i];
} else if (!strcmp(argv[i], "--plugin")) {
if (!need_value(argc, i, argv[i]))
return 2;
if (nplugins == 16) {
fprintf(stderr, "splinter: at most 16 plugins\n");
return 2;
}
plugins[nplugins++] = argv[++i];
} else if (!strcmp(argv[i], "--chat")) {
chatmode = 1;
} else if (!strcmp(argv[i], "--autonomous")) {
@@ -218,10 +234,35 @@ main(int argc, char **argv)
ag.reg = &reg;
ag.cfg = &cfg;
/*
* Plugins are loaded after the registry and the session exist, before the
* first turn. A plugin that fails to load is reported and skipped: the
* harness keeps working, the operator sees why.
*/
if (nplugins > 0) {
size_t k;
host = sp_host_new(&reg, &ag, &sess);
if (host == NULL) {
fprintf(stderr, "splinter: cannot create plugin host\n");
return 3;
}
for (k = 0; k < nplugins; k++) {
sp_buf_clear(&err);
if (sp_plugin_load(host, plugins[k], &err) != 0)
fprintf(stderr, "splinter: %s\n",
err.p != NULL ? err.p : "plugin failed");
else
fprintf(stderr, "splinter: plugin %s loaded\n",
plugins[k]);
}
}
if (chatmode) {
rc = sp_ui_run(&ag, endpoint, theme);
rc = sp_ui_run(&ag, endpoint, theme, host);
if (sctx.f != NULL)
fclose(sctx.f);
sp_host_free(host);
sp_buf_free(&answer);
sp_buf_free(&err);
sp_registry_free(&reg);
@@ -236,6 +277,7 @@ main(int argc, char **argv)
}
if (sctx.f != NULL)
fclose(sctx.f);
sp_host_free(host);
sp_buf_free(&answer);
sp_buf_free(&err);
sp_registry_free(&reg);
+296
View File
@@ -0,0 +1,296 @@
/*
* splinter -- plugin host: loading, tools, widgets, worker threads.
*
* Copyright (c) 2026 Konrad Lother
* SPDX-License-Identifier: BSD-3-Clause
*
* Three rules keep this safe without a sandbox:
*
* 1. A plugin is only ever touched on the main thread, except its tick(),
* which the host calls on a thread the host owns and can stop.
* 2. Anything crossing from a worker thread to the UI goes through a copy
* under a mutex. Workers never hold a pointer the UI can see.
* 3. Nothing is dlclose()d while threads might still run. A stopped
* widget's thread is joined first; the handle itself stays mapped for
* the life of the process, which is the only honest option in C.
*/
#include <dlfcn.h>
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "splinter.h"
#include "splinter_plugin.h"
#define MAX_WIDGETS 32
#define MAX_NOTE 2048
struct widget {
struct sp_host *host;
char name[64];
int (*tick)(struct sp_host *, const char *, void *);
void (*draw)(struct sp_host *, const char *, void *);
void *ctx;
unsigned interval_ms;
pthread_t thread;
int running;
int stop;
char text[MAX_NOTE];
pthread_mutex_t lock;
};
struct sp_host {
struct sp_registry *reg;
struct widget widgets[MAX_WIDGETS];
size_t nwidgets;
char note[MAX_NOTE];
pthread_mutex_t lock; /* guards note and the registry */
struct sp_session_info *sess;
struct sp_agent *agent;
};
static void *
widget_thread(void *arg)
{
struct widget *w = arg;
for (;;) {
struct timespec ts;
pthread_mutex_lock(&w->lock);
if (w->stop) {
pthread_mutex_unlock(&w->lock);
break;
}
pthread_mutex_unlock(&w->lock);
if (w->tick != NULL)
(void)w->tick(w->host, w->name, w->ctx);
if (w->interval_ms == 0)
break;
ts.tv_sec = w->interval_ms / 1000;
ts.tv_nsec = (long)(w->interval_ms % 1000) * 1000000L;
nanosleep(&ts, NULL);
}
return NULL;
}
uint32_t
sp_host_abi(struct sp_host *h)
{
(void)h;
return SP_PLUGIN_ABI;
}
int
sp_host_add_tool(struct sp_host *h, const struct sp_tool *t)
{
int rc;
if (h == NULL || t == NULL || t->name == NULL || t->run == NULL)
return -1;
pthread_mutex_lock(&h->lock);
rc = sp_registry_add(h->reg, t);
pthread_mutex_unlock(&h->lock);
return rc;
}
int
sp_host_add_widget(struct sp_host *h, const struct sp_widget *w)
{
struct widget *slot;
size_t i;
if (h == NULL || w == NULL || w->name == NULL)
return -1;
if (h->nwidgets == MAX_WIDGETS)
return -1;
for (i = 0; i < h->nwidgets; i++)
if (strcmp(h->widgets[i].name, w->name) == 0)
return -1;
slot = &h->widgets[h->nwidgets++];
slot->host = h;
snprintf(slot->name, sizeof(slot->name), "%s", w->name);
slot->tick = w->tick;
slot->draw = w->draw;
slot->ctx = w->ctx;
slot->interval_ms = w->interval_ms;
slot->stop = 0;
pthread_mutex_init(&slot->lock, NULL);
snprintf(slot->text, sizeof(slot->text), "starting...");
if (slot->tick != NULL) {
/*
* A worker gets the host pointer, but only the publish path is
* thread-safe: sp_host_widget_text() and sp_host_note() take locks.
* Everything else on the host belongs to the main thread.
*/
if (pthread_create(&slot->thread, NULL, widget_thread, slot) != 0) {
slot->running = 0;
h->nwidgets--;
return -1;
}
slot->running = 1;
}
return 0;
}
int
sp_host_widget_text(struct sp_host *h, const char *widget, const char *text)
{
size_t i;
if (h == NULL || widget == NULL || text == NULL)
return -1;
for (i = 0; i < h->nwidgets; i++) {
struct widget *w = &h->widgets[i];
if (strcmp(w->name, widget) != 0)
continue;
pthread_mutex_lock(&w->lock);
snprintf(w->text, sizeof(w->text), "%s", text);
pthread_mutex_unlock(&w->lock);
return 0;
}
return -1;
}
int
sp_host_note(struct sp_host *h, const char *text)
{
if (h == NULL || text == NULL)
return -1;
pthread_mutex_lock(&h->lock);
snprintf(h->note, sizeof(h->note), "%s", text);
pthread_mutex_unlock(&h->lock);
return 0;
}
size_t
sp_host_widget_snapshot(struct sp_host *h, char *buf, size_t len)
{
size_t i, used = 0;
if (h == NULL || buf == NULL || len == 0)
return 0;
buf[0] = '\0';
for (i = 0; i < h->nwidgets; i++) {
struct widget *w = &h->widgets[i];
int n;
pthread_mutex_lock(&w->lock);
n = snprintf(buf + used, len - used, "%s: %s\n", w->name, w->text);
pthread_mutex_unlock(&w->lock);
if (n < 0 || (size_t)n >= len - used)
break;
used += (size_t)n;
}
return used;
}
struct sp_plugin_handle {
void *dl;
const struct sp_plugin *p;
char path[512];
};
int
sp_plugin_load(struct sp_host *h, const char *path, struct sp_buf *err)
{
struct sp_plugin_handle *hd;
const struct sp_plugin *(*describe)(void);
if (h == NULL || path == NULL)
return -1;
hd = dlopen(path, RTLD_NOW | RTLD_LOCAL);
if (hd == NULL) {
sp_buf_printf(err, "plugin %s: %s", path, dlerror());
return -1;
}
{
void *sym = dlsym(hd, "sp_plugin_describe");
if (sym == NULL) {
sp_buf_printf(err, "plugin %s: no sp_plugin_describe()",
path);
/* the handle stays mapped: dlclose after a failed init is
* how a half-initialised plugin takes the process with it */
return -1;
}
{
union { void *p; const struct sp_plugin *(*f)(void); } cast;
cast.p = sym;
describe = cast.f;
}
}
{
const struct sp_plugin *p = describe();
if (p == NULL) {
sp_buf_printf(err, "plugin %s: describe() returned NULL",
path);
return -1;
}
if (p->abi != SP_PLUGIN_ABI) {
sp_buf_printf(err, "plugin %s: abi %u, host speaks %u",
path, p->abi, SP_PLUGIN_ABI);
return -1;
}
if (p->init != NULL && p->init(h) != 0) {
sp_buf_printf(err, "plugin %s: init failed", path);
return -1;
}
return 0;
}
}
struct sp_host *
sp_host_new(struct sp_registry *reg, struct sp_agent *agent,
struct sp_session_info *sess)
{
struct sp_host *h = calloc(1, sizeof(*h));
if (h == NULL)
return NULL;
h->reg = reg;
h->agent = agent;
h->sess = sess;
pthread_mutex_init(&h->lock, NULL);
return h;
}
void
sp_host_free(struct sp_host *h)
{
size_t i;
if (h == NULL)
return;
for (i = 0; i < h->nwidgets; i++) {
struct widget *w = &h->widgets[i];
if (!w->running)
continue;
pthread_mutex_lock(&w->lock);
w->stop = 1;
pthread_mutex_unlock(&w->lock);
pthread_join(w->thread, NULL);
w->running = 0;
pthread_mutex_destroy(&w->lock);
}
pthread_mutex_destroy(&h->lock);
free(h);
}
size_t
sp_host_widget_count(struct sp_host *h)
{
return h != NULL ? h->nwidgets : 0;
}
+22 -6
View File
@@ -27,6 +27,7 @@
#include <time.h>
#include "splinter.h"
#include "splinter_plugin.h"
#include "theme.h"
#include "ui.h"
@@ -144,7 +145,8 @@ sidebar_heading(int *y, int cols, const char *text)
}
static void
draw_sidebar(struct ui_state *s, const char *id, const char *dir)
draw_sidebar(struct ui_state *s, const char *id, const char *dir,
struct sp_host *host)
{
int cols, y = 0;
@@ -175,8 +177,21 @@ draw_sidebar(struct ui_state *s, const char *id, const char *dir)
y++;
sidebar_heading(&y, cols, "widgets");
wattron(ui_side, COLOR_PAIR(PAIR_DIM));
mvwaddnstr(ui_side, y, 0, "none loaded", cols - 1);
if (host == NULL || sp_host_widget_count(host) == 0) {
wattron(ui_side, COLOR_PAIR(PAIR_DIM));
mvwaddnstr(ui_side, y++, 0, "none loaded", cols - 1);
} else {
char snap[1024];
char *line, *save = NULL;
sp_host_widget_snapshot(host, snap, sizeof(snap));
for (line = strtok_r(snap, "\n", &save); line != NULL;
line = strtok_r(NULL, "\n", &save)) {
if (y >= getmaxy(ui_side))
break;
mvwaddnstr(ui_side, y++, 0, line, cols - 1);
}
}
wattrset(ui_side, A_NORMAL);
wnoutrefresh(ui_side);
}
@@ -238,7 +253,8 @@ layout(void)
}
int
sp_ui_run(struct sp_agent *ag, const char *endpoint, const char *theme_name)
sp_ui_run(struct sp_agent *ag, const char *endpoint, const char *theme_name,
struct sp_host *host)
{
struct ui_state s;
struct sp_buf answer = { 0 }, err = { 0 };
@@ -271,7 +287,7 @@ sp_ui_run(struct sp_agent *ag, const char *endpoint, const char *theme_name)
draw_history(&s);
draw_sidebar(&s, ag->sess != NULL ? ag->sess->id : NULL,
ag->sess != NULL ? ag->sess->dir : NULL);
ag->sess != NULL ? ag->sess->dir : NULL, host);
draw_status(&s);
werase(ui_input);
mvwaddstr(ui_input, 0, 0, "> ");
@@ -329,7 +345,7 @@ sp_ui_run(struct sp_agent *ag, const char *endpoint, const char *theme_name)
add_entry(&s, "you", input);
draw_history(&s);
draw_sidebar(&s, ag->sess != NULL ? ag->sess->id : NULL,
ag->sess != NULL ? ag->sess->dir : NULL);
ag->sess != NULL ? ag->sess->dir : NULL, host);
draw_status(&s);
werase(ui_input);
mvwaddstr(ui_input, 0, 0, " working...");
+4 -2
View File
@@ -3,12 +3,14 @@
#define SPLINTER_UI_H
#include "splinter.h"
#include "splinter_plugin.h"
/*
* Run the interactive chat loop until the user quits. `theme_name` is a CDE
* palette name; NULL selects the default. Returns 0 on a clean quit.
* palette name; NULL selects the default. `host` may be NULL (no plugins).
* Returns 0 on a clean quit.
*/
int sp_ui_run(struct sp_agent *ag, const char *endpoint,
const char *theme_name);
const char *theme_name, struct sp_host *host);
#endif /* SPLINTER_UI_H */
BIN
View File
Binary file not shown.
+126
View File
@@ -0,0 +1,126 @@
/*
* splinter -- plugin host tests.
*
* These run against the real libsplinter.so and the real clock.so built by
* make: the loader is exercised with dlopen, not simulated. What is proven
* here is that a plugin loads, registers a tool and a widget, that the widget's
* worker thread actually runs and publishes, and that the error paths fail
* loudly instead of half-loading.
*/
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "splinter.h"
#include "splinter_plugin.h"
static int failures;
#define CHECK(cond) do { \
if (!(cond)) { \
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
failures++; \
} \
} while (0)
static void
nap_ms(long ms)
{
struct timespec ts;
ts.tv_sec = ms / 1000;
ts.tv_nsec = (ms % 1000) * 1000000L;
nanosleep(&ts, NULL);
}
static int
tick_nothing(struct sp_host *h, const char *name, void *ctx)
{
(void)h;
(void)name;
(void)ctx;
return 0;
}
int
main(void)
{
struct sp_registry reg;
struct sp_agent ag;
struct sp_host *host;
struct sp_buf err;
char snap[1024];
struct sp_widget w;
memset(&reg, 0, sizeof(reg));
memset(&ag, 0, sizeof(ag));
if (sp_buf_init(&err) != 0)
return 1;
host = sp_host_new(&reg, &ag, NULL);
CHECK(host != NULL);
CHECK(sp_host_abi(host) == SP_PLUGIN_ABI);
CHECK(sp_host_widget_count(host) == 0);
/* a shared object that is not a plugin fails, and says why */
sp_buf_clear(&err);
CHECK(sp_plugin_load(host, "libsplinter.so", &err) == -1);
CHECK(strstr(err.p, "sp_plugin_describe") != NULL);
/* the real plugin: one widget and one tool */
sp_buf_clear(&err);
CHECK(sp_plugin_load(host, "plugins/clock/clock.so", &err) == 0);
CHECK(sp_host_widget_count(host) == 1);
CHECK(sp_registry_find(&reg, "clock") != NULL);
/* its worker thread publishes within a second (interval is 1000 ms) */
nap_ms(1300);
snap[0] = '\0';
CHECK(sp_host_widget_snapshot(host, snap, sizeof(snap)) > 0);
CHECK(strstr(snap, "clock: ") != NULL);
{
const char *p = strchr(snap, ':');
/* HH:MM:SS, so at least two more colons after the label */
CHECK(p != NULL);
if (p != NULL) {
CHECK(strchr(p + 1, ':') != NULL);
CHECK(strchr(strchr(p + 1, ':') + 1, ':') != NULL);
}
}
/* a second widget, added by hand, and a duplicate name refused */
memset(&w, 0, sizeof(w));
w.name = "manual";
w.tick = tick_nothing;
w.interval_ms = 50;
CHECK(sp_host_add_widget(host, &w) == 0);
CHECK(sp_host_add_widget(host, &w) == -1);
CHECK(sp_host_widget_count(host) == 2);
/* publishing from the main thread works and is visible in the snapshot */
CHECK(sp_host_widget_text(host, "manual", "hello from main") == 0);
CHECK(sp_host_widget_text(host, "nope", "x") == -1);
snap[0] = '\0';
sp_host_widget_snapshot(host, snap, sizeof(snap));
CHECK(strstr(snap, "manual: hello from main") != NULL);
/* a bad tool is refused, not registered half-way */
{
struct sp_tool t;
memset(&t, 0, sizeof(t));
t.name = "broken";
CHECK(sp_host_add_tool(host, &t) == -1);
CHECK(sp_registry_find(&reg, "broken") == NULL);
}
/* freeing the host stops the threads: no hang, no crash */
sp_host_free(host);
sp_buf_free(&err);
sp_registry_free(&reg);
if (failures == 0)
printf("test_plugin: ok\n");
return failures == 0 ? 0 : 1;
}
BIN
View File
Binary file not shown.