plugins come from their own config block, with their own settings
plugin NAME { path = ... enabled = false key = value } loads the plugin
(shipped one for NAME unless path says otherwise) and hands everything else
to that plugin as its configuration, read with sp_host_plugin_config() or
walked with sp_host_plugin_config_at(). Nested blocks give dotted keys,
lists arrive comma-separated.
The mcp plugin's servers are now server ID { url | cmd | header } blocks
inside plugin mcp { }; the old top-level mcp NAME { } and the flat
plugin = path / plugins = [ ... ] forms keep loading.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
bddba6423b
commit
f3b832ae64
13 files changed
+554
-44
No files matched your search
+60
-21
@@ -29,9 +29,11 @@ core {
|
||||
steps = 60
|
||||
on_limit = wait # a usage limit is waited out (or: fail)
|
||||
max_wait = 2d
|
||||
plugins = [plugins/lcm/lcm.so, plugins/cli/cli.so]
|
||||
}
|
||||
|
||||
plugin lcm { } # one block per plugin, see "Plugins"
|
||||
plugin cli { }
|
||||
|
||||
telegram { allow = [123456789] }
|
||||
|
||||
provider ocgo {
|
||||
@@ -228,7 +230,7 @@ lies about what it does.
|
||||
| `api_key_env` | name of the env var holding the key (default `SPLINTER_API_KEY`) |
|
||||
| `transport` | a transport a plugin provides, e.g. `cli` |
|
||||
| `cli_cmd` | command for the `cli` transport, e.g. `claude -p` |
|
||||
| `plugin` | a plugin to load, one line each; absolute paths survive a `cd` |
|
||||
| `plugin` | a plugin to load, one line each: a shipped plugin's name (`lcm`) or a path; in the block form `plugin NAME { }`, see "Plugins" |
|
||||
| `theme` | CDE palette name (`splinter --list-themes`) |
|
||||
| `locale` | language code (`de`, `en`); catalogues live in `share/locale` |
|
||||
| `autonomous` | `1` runs mutating/destructive tools without asking |
|
||||
@@ -320,29 +322,66 @@ task moves to the next delegate in its own slot while the others keep running,
|
||||
unless that one is already at its `max_parallel`. Only when nothing else fits
|
||||
does the job wait (`waiting_quota`) for the reset.
|
||||
|
||||
## Plugins
|
||||
|
||||
Every plugin has its own block. The block loads it, and everything in it
|
||||
apart from `path` and `enabled` is that plugin's own configuration:
|
||||
|
||||
```
|
||||
plugin lcm {
|
||||
keep = 40
|
||||
}
|
||||
plugin tmux { }
|
||||
plugin mine {
|
||||
path = ~/my/plugin.so # not shipped: say where it is
|
||||
foo = bar
|
||||
}
|
||||
plugin status { enabled = false } # kept in the file, not loaded
|
||||
```
|
||||
|
||||
| field | meaning |
|
||||
|-------|---------|
|
||||
| `path` | the `.so` to load; without it the shipped plugin of that name |
|
||||
| `enabled` | `false` keeps the block but does not load the plugin (default on) |
|
||||
| anything else | the plugin's own setting, read with `sp_host_plugin_config()` |
|
||||
|
||||
Without `path`, `plugin NAME { }` finds the shipped plugin next to the
|
||||
binary: `plugins/NAME/NAME.so` in the build tree, then the installed
|
||||
`$(PREFIX)/lib/splinter/plugins/NAME.so`, then the build directory the binary
|
||||
was compiled in. A nested block gives dotted names: `server a { url = x }`
|
||||
inside `plugin foo` is `server.a.url`; a list `[a, b]` arrives as `a,b`.
|
||||
`docs/PLUGINS.md` has the API.
|
||||
|
||||
The old forms still load, so an existing file keeps working:
|
||||
`plugins = [plugins/lcm/lcm.so, ...]` in `core` (one path each) and the flat
|
||||
`plugin = /path/to/x.so`. They carry no plugin settings.
|
||||
|
||||
## MCP servers
|
||||
|
||||
An MCP server contributes tools. One line each, or a block, and it needs the
|
||||
`plugins/mcp/mcp.so` plugin:
|
||||
An MCP server contributes tools. The servers are the configuration of the
|
||||
`mcp` plugin, one `server` block each:
|
||||
|
||||
```
|
||||
plugin = /home/lotherk/src/splinter/plugins/mcp/mcp.so
|
||||
plugin mcp {
|
||||
# streamable HTTP: one POST per message
|
||||
server junction {
|
||||
url = http://172.25.206.16:8090/mcp
|
||||
header = "Authorization: Bearer ${JUNCTION_TOKEN}"
|
||||
}
|
||||
|
||||
# streamable HTTP: one POST per message
|
||||
mcp junction {
|
||||
url = http://172.25.206.16:8090/mcp
|
||||
header = "Authorization: Bearer ${JUNCTION_TOKEN}"
|
||||
# or a program that speaks MCP on its stdin/stdout
|
||||
server files {
|
||||
cmd = "npx -y @modelcontextprotocol/server-filesystem /home"
|
||||
}
|
||||
|
||||
# a server that exists but should not be asked
|
||||
server old { url = http://127.0.0.1:9000/mcp enabled = false }
|
||||
}
|
||||
|
||||
# or a program that speaks MCP on its stdin/stdout
|
||||
mcp files {
|
||||
cmd = "npx -y @modelcontextprotocol/server-filesystem /home"
|
||||
}
|
||||
|
||||
# a server that exists but should not be asked
|
||||
mcp old { url = http://127.0.0.1:9000/mcp enabled = false }
|
||||
```
|
||||
|
||||
The top-level `mcp NAME { ... }` block and the flat `mcp = ...` line from
|
||||
before still work (they need the plugin loaded as well).
|
||||
|
||||
| field | meaning |
|
||||
|-------|---------|
|
||||
| `url` | the endpoint, streamable-HTTP transport (`.../mcp`) |
|
||||
@@ -373,9 +412,9 @@ endpoint = http://127.0.0.1:8080/v1
|
||||
model = /models/unsloth/Qwen3.6-35B-A3B-MTP-GGUF/Qwen3.6-35B-A3B-UD-Q8_K_XL.gguf
|
||||
theme = Delphinium
|
||||
autonomous = 1
|
||||
plugin = /home/lotherk/src/splinter/plugins/lcm/lcm.so
|
||||
plugin = /home/lotherk/src/splinter/plugins/cps/cps.so
|
||||
plugin = /home/lotherk/src/splinter/plugins/clock/clock.so
|
||||
plugin = lcm
|
||||
plugin = cps
|
||||
plugin = clock
|
||||
session_dir = /home/lotherk/.splinter
|
||||
```
|
||||
|
||||
@@ -385,7 +424,7 @@ session_dir = /home/lotherk/.splinter
|
||||
transport = cli
|
||||
cli_cmd = claude -p
|
||||
cli_timeout = 1800
|
||||
plugin = /home/lotherk/src/splinter/plugins/cli/cli.so
|
||||
plugin = cli
|
||||
theme = Delphinium
|
||||
```
|
||||
|
||||
|
||||
@@ -24,6 +24,46 @@ cc -std=c99 -shared -fPIC -o myplugin.so myplugin.c -L. -lsplinter \
|
||||
plugin that fails to load is reported on stderr and skipped; the harness keeps
|
||||
working.
|
||||
|
||||
## Configuration
|
||||
|
||||
In the config file every plugin has its own block (`docs/CONFIG.md`,
|
||||
"Plugins"):
|
||||
|
||||
```
|
||||
plugin mine {
|
||||
path = ~/my/myplugin.so # omitted: the shipped plugin "mine"
|
||||
depth = 3
|
||||
hosts = [a, b]
|
||||
server a { url = http://a/mcp }
|
||||
}
|
||||
```
|
||||
|
||||
Everything but `path` and `enabled` is the plugin's own, and it reads it
|
||||
through the host by the block's name:
|
||||
|
||||
```c
|
||||
const char *d = sp_host_plugin_config(host, "mine", "depth"); /* "3" */
|
||||
const char *h = sp_host_plugin_config(host, "mine", "hosts"); /* "a,b" */
|
||||
const char *u = sp_host_plugin_config(host, "mine", "server.a.url");
|
||||
```
|
||||
|
||||
A missing key is NULL; the value is a string, parsing is the plugin's job.
|
||||
To see every setting (the servers it was given, say), walk them in file order:
|
||||
|
||||
```c
|
||||
const char *key, *val;
|
||||
size_t i;
|
||||
|
||||
for (i = 0; (val = sp_host_plugin_config_at(host, "mine", i, &key)) != NULL; i++)
|
||||
/* key = "depth", "hosts", "server.a.url", ... */;
|
||||
```
|
||||
|
||||
Both work from `init()` on. The config is read once at start; a value does
|
||||
not change while the plugin runs. The `mcp` plugin is the one exception to
|
||||
the dotted names: its `server NAME { }` blocks become the MCP server list it
|
||||
always read (`sp_host_config()`), so `plugin mcp { server ... }` and the old
|
||||
`mcp NAME { }` block mean the same.
|
||||
|
||||
## What a plugin may do
|
||||
|
||||
| hook | thread | may block | may touch curses |
|
||||
|
||||
@@ -355,6 +355,14 @@ int sp_delegate_prio(const struct sp_config *, const char *id);
|
||||
*/
|
||||
void sp_delegate_cli_plugin(const char *self, char *out, size_t len);
|
||||
|
||||
/*
|
||||
* A shipped plugin by name ("lcm", "mcp"): the build tree next to the binary
|
||||
* at 'self', the installed layout ($(PREFIX)/lib/splinter/plugins), then the
|
||||
* build directory compiled in. None found: the first candidate.
|
||||
*/
|
||||
void sp_plugin_path(const char *self, const char *name, char *out,
|
||||
size_t len);
|
||||
|
||||
/*
|
||||
* Limits. sp_limit_parse: is this error text a rate/usage limit (429, "weekly
|
||||
* limit", "quota", ...) and until when - the reset it names, else an hour.
|
||||
|
||||
@@ -224,6 +224,26 @@ void sp_host_turn(struct sp_host *);
|
||||
/* The loaded configuration, so plugins (the exit ring) can read it. */
|
||||
const void *sp_host_config(struct sp_host *);
|
||||
void sp_host_set_config(struct sp_host *, const void *);
|
||||
|
||||
/*
|
||||
* A plugin's own configuration, from its block in the config file:
|
||||
*
|
||||
* plugin lcm {
|
||||
* keep = 40
|
||||
* server junction { url = http://... }
|
||||
* }
|
||||
*
|
||||
* sp_host_plugin_config(h, "lcm", "keep") is "40", a nested block gives
|
||||
* dotted keys ("server.junction.url"), a list "a,b,c". NULL when the key is
|
||||
* not set (or no config is loaded). 'plugin' is the block's name.
|
||||
*
|
||||
* sp_host_plugin_config_at() walks every setting of 'plugin' in file order:
|
||||
* index 0, 1, ... until it returns NULL; *key gets the setting's name.
|
||||
*/
|
||||
const char *sp_host_plugin_config(struct sp_host *, const char *plugin,
|
||||
const char *key);
|
||||
const char *sp_host_plugin_config_at(struct sp_host *, const char *plugin,
|
||||
size_t i, const char **key);
|
||||
struct sp_registry *sp_host_registry(struct sp_host *);
|
||||
|
||||
/* ---- host side, implemented by splinter, called by the harness ---- */
|
||||
|
||||
+164
-4
@@ -18,8 +18,10 @@ struct val {
|
||||
|
||||
struct ent {
|
||||
char key[48];
|
||||
char sub[96]; /* key NAME { ... }: the NAME */
|
||||
struct val v;
|
||||
unsigned line;
|
||||
int block; /* a nested { } block: items are A=b */
|
||||
};
|
||||
|
||||
struct cb {
|
||||
@@ -459,6 +461,140 @@ done:
|
||||
sp_buf_free(&l);
|
||||
}
|
||||
|
||||
/* one setting of a plugin: plugin_conf = NAME | key=value */
|
||||
static void
|
||||
plugin_kv(struct cb *c, const struct ent *e, const char *plugin,
|
||||
const char *key, const char *val)
|
||||
{
|
||||
if (strchr(key, '=') != NULL || strchr(key, '|') != NULL) {
|
||||
fail(c, e->line, "bad plugin setting name '%s'", key);
|
||||
return;
|
||||
}
|
||||
if (!clean(c, e, val))
|
||||
return;
|
||||
emit(c, e->line, "plugin_conf = %s | %s=%s", plugin, key, val);
|
||||
}
|
||||
|
||||
/* plugin mcp { server ID { url = ... } }: the servers the mcp plugin runs */
|
||||
static void
|
||||
mcp_server(struct cb *c, const struct ent *e)
|
||||
{
|
||||
struct sp_buf l;
|
||||
size_t j;
|
||||
|
||||
if (e->sub[0] == '\0') {
|
||||
fail(c, e->line, "server needs a name: server NAME { url = ... }");
|
||||
return;
|
||||
}
|
||||
if (sp_buf_init(&l) != 0)
|
||||
return;
|
||||
sp_buf_printf(&l, "mcp = %s", e->sub);
|
||||
for (j = 0; j < e->v.n; j++) {
|
||||
const char *kv = e->v.items[j], *eq = strchr(kv, '=');
|
||||
size_t kl = eq != NULL ? (size_t)(eq - kv) : 0;
|
||||
|
||||
if (eq == NULL || !clean(c, e, kv))
|
||||
goto done;
|
||||
if ((kl == 3 && !strncmp(kv, "url", 3)) ||
|
||||
(kl == 3 && !strncmp(kv, "cmd", 3)) ||
|
||||
(kl == 6 && !strncmp(kv, "header", 6)))
|
||||
sp_buf_printf(&l, " | %s", kv);
|
||||
else if (kl == 7 && !strncmp(kv, "enabled", 7))
|
||||
sp_buf_printf(&l, " | enabled=%d", boolean(eq + 1));
|
||||
else {
|
||||
fail(c, e->line, "unknown mcp server field '%.*s'",
|
||||
(int)kl, kv);
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
emit(c, e->line, "%s", l.p);
|
||||
done:
|
||||
sp_buf_free(&l);
|
||||
}
|
||||
|
||||
/*
|
||||
* plugin NAME { path = ... enabled = false anything = else }
|
||||
*
|
||||
* The block loads the plugin -- the shipped one for NAME unless 'path' names
|
||||
* another -- and everything else in it is the plugin's own configuration,
|
||||
* which it reads with sp_host_plugin_config(host, NAME, key).
|
||||
*/
|
||||
static void
|
||||
do_plugin(struct cb *c, const char *name, unsigned line, struct ent *e,
|
||||
size_t n)
|
||||
{
|
||||
const char *path = NULL;
|
||||
char full[1024], list[2048], key[200];
|
||||
size_t i, j;
|
||||
int on = 1;
|
||||
|
||||
for (i = 0; name[i] != '\0'; i++)
|
||||
if (!isalnum((unsigned char)name[i]) && name[i] != '_' &&
|
||||
name[i] != '-') {
|
||||
fail(c, line, "plugin name '%s': letters, digits, _ and - "
|
||||
"only", name);
|
||||
return;
|
||||
}
|
||||
for (i = 0; i < n && !c->failed; i++) {
|
||||
if (!strcmp(e[i].key, "path") && !e[i].block) {
|
||||
path = one(c, &e[i]);
|
||||
if (path != NULL && !clean(c, &e[i], path))
|
||||
return;
|
||||
} else if (!strcmp(e[i].key, "enabled") && !e[i].block) {
|
||||
const char *s = one(c, &e[i]);
|
||||
|
||||
if (s == NULL)
|
||||
return;
|
||||
on = boolean(s);
|
||||
}
|
||||
}
|
||||
if (c->failed || !on)
|
||||
return;
|
||||
if (path != NULL && path[0] == '~' && path[1] == '/' &&
|
||||
getenv("HOME") != NULL) {
|
||||
snprintf(full, sizeof(full), "%s%s", getenv("HOME"), path + 1);
|
||||
path = full;
|
||||
}
|
||||
/* a bare name is the shipped plugin: main.c looks it up */
|
||||
emit(c, line, "plugin = %s", path != NULL ? path : name);
|
||||
for (i = 0; i < n && !c->failed; i++) {
|
||||
const struct ent *x = &e[i];
|
||||
|
||||
if ((!strcmp(x->key, "path") || !strcmp(x->key, "enabled")) &&
|
||||
!x->block)
|
||||
continue;
|
||||
if (x->block) {
|
||||
if (!strcmp(name, "mcp") && !strcmp(x->key, "server")) {
|
||||
mcp_server(c, x);
|
||||
continue;
|
||||
}
|
||||
for (j = 0; j < x->v.n && !c->failed; j++) {
|
||||
const char *kv = x->v.items[j];
|
||||
const char *eq = strchr(kv, '=');
|
||||
|
||||
if (eq == NULL)
|
||||
continue;
|
||||
if (x->sub[0] != '\0')
|
||||
snprintf(key, sizeof(key), "%s.%s.%.*s",
|
||||
x->key, x->sub, (int)(eq - kv), kv);
|
||||
else
|
||||
snprintf(key, sizeof(key), "%s.%.*s",
|
||||
x->key, (int)(eq - kv), kv);
|
||||
plugin_kv(c, x, name, key, eq + 1);
|
||||
}
|
||||
} else if (x->v.list) {
|
||||
joined(&x->v, list, sizeof(list));
|
||||
plugin_kv(c, x, name, x->key, list);
|
||||
} else {
|
||||
const char *s = one(c, x);
|
||||
|
||||
if (s == NULL)
|
||||
return;
|
||||
plugin_kv(c, x, name, x->key, s);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
do_exit(struct cb *c, const char *name, unsigned line, struct ent *e, size_t n)
|
||||
{
|
||||
@@ -533,10 +669,30 @@ body(struct cb *c, struct ent *e, int *closed)
|
||||
snprintf(e[n].key, sizeof(e[n].key), "%s", key);
|
||||
e[n].line = line;
|
||||
skip(c, 0);
|
||||
if (*c->p != '{' && *c->p != '=' && *c->p != '\0' &&
|
||||
*c->p != '\n') {
|
||||
/* key NAME { ... }: a named nested block (server junction) */
|
||||
size_t m = 0;
|
||||
|
||||
while (isalnum((unsigned char)*c->p) || *c->p == '_' ||
|
||||
*c->p == '-' || *c->p == '.') {
|
||||
if (m + 1 < sizeof(e[n].sub))
|
||||
e[n].sub[m++] = *c->p;
|
||||
c->p++;
|
||||
}
|
||||
e[n].sub[m] = '\0';
|
||||
skip(c, 0);
|
||||
if (m == 0 || *c->p != '{') {
|
||||
fail(c, line, "expected '{' after %s %s", key,
|
||||
e[n].sub);
|
||||
return n;
|
||||
}
|
||||
}
|
||||
if (*c->p == '{') {
|
||||
/* env { A = b } */
|
||||
/* env { A = b }, server junction { url = ... } */
|
||||
c->p++;
|
||||
e[n].v.list = 1;
|
||||
e[n].block = 1;
|
||||
for (;;) {
|
||||
char nm[96];
|
||||
size_t m = 0;
|
||||
@@ -554,7 +710,8 @@ body(struct cb *c, struct ent *e, int *closed)
|
||||
return n;
|
||||
}
|
||||
vl = c->line;
|
||||
while (isalnum((unsigned char)*c->p) || *c->p == '_') {
|
||||
while (isalnum((unsigned char)*c->p) || *c->p == '_' ||
|
||||
*c->p == '-' || *c->p == '.') {
|
||||
if (m + 1 < sizeof(nm))
|
||||
nm[m++] = *c->p;
|
||||
c->p++;
|
||||
@@ -699,7 +856,7 @@ sp_confblock_translate(const char *text, const char *path, struct sp_buf *out,
|
||||
if (!c.failed && closed) {
|
||||
int needs_name = !strcmp(kind, "provider") ||
|
||||
!strcmp(kind, "pool") || !strcmp(kind, "exit") ||
|
||||
!strcmp(kind, "mcp");
|
||||
!strcmp(kind, "mcp") || !strcmp(kind, "plugin");
|
||||
|
||||
if (needs_name && name[0] == '\0')
|
||||
fail(&c, line, "%s needs a name: %s NAME { ... }",
|
||||
@@ -727,11 +884,14 @@ sp_confblock_translate(const char *text, const char *path, struct sp_buf *out,
|
||||
do_pool(&c, name, line, e, n);
|
||||
else if (!strcmp(kind, "exit"))
|
||||
do_exit(&c, name, line, e, n);
|
||||
else if (!strcmp(kind, "plugin"))
|
||||
do_plugin(&c, name, line, e, n);
|
||||
else if (!strcmp(kind, "mcp"))
|
||||
/* deprecated: plugin mcp { server ID { ... } } */
|
||||
do_mcp(&c, name, line, e, n);
|
||||
else
|
||||
fail(&c, line, "unknown block '%s' (core, telegram, "
|
||||
"provider, pool, exit, mcp)", kind);
|
||||
"provider, pool, exit, plugin)", kind);
|
||||
}
|
||||
ents_free(e, n);
|
||||
if (c.failed)
|
||||
|
||||
+47
-2
@@ -776,6 +776,29 @@ load_stream(struct sp_config *c, FILE *f, const char *path,
|
||||
}
|
||||
else if (!strcmp(key, "default"))
|
||||
set_str(c->default_id, sizeof(c->default_id), val);
|
||||
else if (!strcmp(key, "plugin_conf")) {
|
||||
/* plugin_conf = NAME | key=value (from a plugin { } block) */
|
||||
struct sp_plugin_kv *kv;
|
||||
char *bar = strstr(val, " | "), *eq2;
|
||||
|
||||
if (bar == NULL || (eq2 = strchr(bar + 3, '=')) == NULL) {
|
||||
sp_buf_printf(err, "config: %s:%u: plugin_conf "
|
||||
"wants NAME | key=value", path, lineno);
|
||||
return -1;
|
||||
}
|
||||
if (c->nplugin_conf == SP_CONFIG_MAX_PLUGIN_KV) {
|
||||
sp_buf_printf(err, "config: %s:%u: more than %d "
|
||||
"plugin settings", path, lineno,
|
||||
SP_CONFIG_MAX_PLUGIN_KV);
|
||||
return -1;
|
||||
}
|
||||
kv = &c->plugin_conf[c->nplugin_conf++];
|
||||
*bar = '\0';
|
||||
*eq2 = '\0';
|
||||
set_str(kv->plugin, sizeof(kv->plugin), trim(val));
|
||||
set_str(kv->key, sizeof(kv->key), trim(bar + 3));
|
||||
set_str(kv->val, sizeof(kv->val), eq2 + 1);
|
||||
}
|
||||
else if (!strcmp(key, "plugin")) {
|
||||
if (c->nplugins == SP_CONFIG_MAX_PLUGINS) {
|
||||
sp_buf_printf(err, "config: %s:%u: more than %d "
|
||||
@@ -848,10 +871,14 @@ sp_config_write_example(const char *path, struct sp_buf *err)
|
||||
" # A usage limit is waited out: splinter finds the reset and goes on.\n"
|
||||
" #on_limit = wait # or fail\n"
|
||||
" #max_wait = 12h\n"
|
||||
"\n"
|
||||
" #plugins = [plugins/lcm/lcm.so, plugins/cps/cps.so]\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"# One block per plugin; what is in it is that plugin's own setting.\n"
|
||||
"#plugin lcm { }\n"
|
||||
"#plugin mcp {\n"
|
||||
"# server junction { url = http://127.0.0.1:8090/mcp }\n"
|
||||
"#}\n"
|
||||
"\n"
|
||||
"#telegram { allow = [123456789] }\n"
|
||||
"\n"
|
||||
"provider local {\n"
|
||||
@@ -1086,3 +1113,21 @@ sp_cli_command(const struct sp_delegate *d, const char *model, char *out,
|
||||
snprintf(out, cap, "%s --model %s", cmd, full);
|
||||
return 0;
|
||||
}
|
||||
|
||||
const char *
|
||||
sp_config_plugin_value(const struct sp_config *c, const char *plugin,
|
||||
const char *key)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
if (c == NULL || plugin == NULL || key == NULL)
|
||||
return NULL;
|
||||
/* the last one wins, like every other setting */
|
||||
for (i = c->nplugin_conf; i > 0; i--) {
|
||||
const struct sp_plugin_kv *kv = &c->plugin_conf[i - 1];
|
||||
|
||||
if (strcmp(kv->plugin, plugin) == 0 && strcmp(kv->key, key) == 0)
|
||||
return kv->val;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
@@ -22,6 +22,18 @@
|
||||
#include "splinter.h"
|
||||
|
||||
#define SP_CONFIG_MAX_PLUGINS 32
|
||||
#define SP_CONFIG_MAX_PLUGIN_KV 96
|
||||
|
||||
/*
|
||||
* One setting of a plugin's own configuration: plugin NAME { key = value }.
|
||||
* A nested block (server junction { url = ... }) gives dotted keys,
|
||||
* "server.junction.url"; a list becomes "a,b,c".
|
||||
*/
|
||||
struct sp_plugin_kv {
|
||||
char plugin[48];
|
||||
char key[96];
|
||||
char val[512];
|
||||
};
|
||||
|
||||
/*
|
||||
* A delegate is another model splinter may hand work to: a name, what it is
|
||||
@@ -136,6 +148,8 @@ struct sp_config {
|
||||
char locale[32]; /* language code, exported as SPLINTER_LOCALE */
|
||||
char plugins[SP_CONFIG_MAX_PLUGINS][512];
|
||||
size_t nplugins;
|
||||
struct sp_plugin_kv plugin_conf[SP_CONFIG_MAX_PLUGIN_KV];
|
||||
size_t nplugin_conf;
|
||||
struct sp_mcpserver mcp[SP_CONFIG_MAX_MCP];
|
||||
size_t nmcp;
|
||||
size_t steps; /* 0: not set */
|
||||
@@ -182,6 +196,13 @@ const char *sp_config_loaded_path(void);
|
||||
/* Write a commented starter file. Returns 0 or -1. */
|
||||
int sp_config_write_example(const char *path, struct sp_buf *err);
|
||||
|
||||
/*
|
||||
* A plugin's own setting (plugin NAME { key = value }), or NULL. 'key' may be
|
||||
* dotted for a nested block: "server.junction.url".
|
||||
*/
|
||||
const char *sp_config_plugin_value(const struct sp_config *,
|
||||
const char *plugin, const char *key);
|
||||
|
||||
/* Set every NAME=VALUE of 'env' in this process, overwriting. */
|
||||
void sp_config_apply_env(const char (*env)[SP_CONFIG_ENV_LEN], size_t n);
|
||||
|
||||
|
||||
+26
-8
@@ -569,15 +569,30 @@ shell_quote(struct sp_buf *b, const char *s)
|
||||
void
|
||||
sp_delegate_cli_plugin(const char *self, char *out, size_t len)
|
||||
{
|
||||
char dir[512], cand[3][600];
|
||||
const char *env = getenv("SPLINTER_CLI_PLUGIN");
|
||||
const char *slash;
|
||||
size_t n = 0, i;
|
||||
const char *env = getenv("SPLINTER_CLI_PLUGIN");
|
||||
|
||||
if (env != NULL && *env != '\0') {
|
||||
snprintf(out, len, "%s", env);
|
||||
return;
|
||||
}
|
||||
sp_plugin_path(self, "cli", out, len);
|
||||
}
|
||||
|
||||
/*
|
||||
* A shipped plugin by name, for a binary at 'self'. Order:
|
||||
* <dir of self>/plugins/NAME/NAME.so the build tree
|
||||
* <dir of self>/../lib/splinter/plugins/NAME/NAME.so
|
||||
* <dir of self>/../lib/splinter/plugins/NAME.so what 'make install' writes
|
||||
* SPLINTER_PLUGIN_DIR/plugins/NAME/NAME.so the build tree compiled in
|
||||
* The first readable one wins; none: the first, so the error names a place.
|
||||
*/
|
||||
void
|
||||
sp_plugin_path(const char *self, const char *name, char *out, size_t len)
|
||||
{
|
||||
char dir[512], cand[4][700];
|
||||
const char *slash;
|
||||
size_t n = 0, i;
|
||||
|
||||
snprintf(dir, sizeof(dir), "%s",
|
||||
self != NULL && *self != '\0' ? self : "splinter");
|
||||
slash = strrchr(dir, '/');
|
||||
@@ -585,12 +600,15 @@ sp_delegate_cli_plugin(const char *self, char *out, size_t len)
|
||||
dir[slash - dir] = '\0';
|
||||
else
|
||||
snprintf(dir, sizeof(dir), ".");
|
||||
snprintf(cand[n++], sizeof(cand[0]), "%s/plugins/cli/cli.so", dir);
|
||||
snprintf(cand[n++], sizeof(cand[0]), "%s/plugins/%s/%s.so", dir, name,
|
||||
name);
|
||||
snprintf(cand[n++], sizeof(cand[0]),
|
||||
"%s/../lib/splinter/plugins/cli/cli.so", dir);
|
||||
"%s/../lib/splinter/plugins/%s/%s.so", dir, name, name);
|
||||
snprintf(cand[n++], sizeof(cand[0]),
|
||||
"%s/../lib/splinter/plugins/%s.so", dir, name);
|
||||
#ifdef SPLINTER_PLUGIN_DIR
|
||||
snprintf(cand[n++], sizeof(cand[0]), "%s/plugins/cli/cli.so",
|
||||
SPLINTER_PLUGIN_DIR);
|
||||
snprintf(cand[n++], sizeof(cand[0]), "%s/plugins/%s/%s.so",
|
||||
SPLINTER_PLUGIN_DIR, name, name);
|
||||
#endif
|
||||
for (i = 0; i < n; i++)
|
||||
if (access(cand[i], R_OK) == 0)
|
||||
|
||||
+22
-9
@@ -794,8 +794,23 @@ load_plugins(const struct opts *o, struct sp_registry *reg, struct sp_agent *ag,
|
||||
ag->host = host;
|
||||
sp_host_set_config(host, conf);
|
||||
for (k = 0; k < o->nplugins; k++) {
|
||||
const char *p = o->plugins[k];
|
||||
char self[512], found[800];
|
||||
ssize_t sn;
|
||||
|
||||
/*
|
||||
* A bare name ("lcm", from plugin lcm { }) is a shipped plugin:
|
||||
* the tree next to the binary, then the installed layout.
|
||||
*/
|
||||
if (strchr(p, '/') == NULL &&
|
||||
(strlen(p) < 3 || strcmp(p + strlen(p) - 3, ".so") != 0)) {
|
||||
sn = readlink("/proc/self/exe", self, sizeof(self) - 1);
|
||||
self[sn > 0 ? sn : 0] = '\0';
|
||||
sp_plugin_path(self, p, found, sizeof(found));
|
||||
p = found;
|
||||
}
|
||||
sp_buf_clear(err);
|
||||
if (sp_plugin_load(host, o->plugins[k], err) != 0)
|
||||
if (sp_plugin_load(host, p, err) != 0)
|
||||
fprintf(stderr, "splinter: %s\n", err->len > 0 ? err->p :
|
||||
sp_t("err.plugin_failed"));
|
||||
}
|
||||
@@ -973,16 +988,14 @@ apply_selection(struct opts *o, struct sp_config *conf)
|
||||
slash = strrchr(self, '/');
|
||||
if (slash != NULL)
|
||||
*slash = '\0';
|
||||
snprintf(plug, sizeof(plug), "%s/plugins/cli/cli.so", self);
|
||||
(void)plug;
|
||||
for (i = 0; i < o->nplugins; i++)
|
||||
if (strstr(o->plugins[i], "cli.so") != NULL)
|
||||
if (strstr(o->plugins[i], "cli.so") != NULL ||
|
||||
strcmp(o->plugins[i], "cli") == 0)
|
||||
have = 1;
|
||||
if (!have && o->nplugins < 32) {
|
||||
static char keep[600];
|
||||
|
||||
snprintf(keep, sizeof(keep), "%s", plug);
|
||||
o->plugins[o->nplugins++] = keep;
|
||||
}
|
||||
/* by name: load_plugins finds the tree or the installed one */
|
||||
if (!have && o->nplugins < 32)
|
||||
o->plugins[o->nplugins++] = "cli";
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
|
||||
#include "splinter.h"
|
||||
#include "splinter_plugin.h"
|
||||
#include "config.h"
|
||||
|
||||
#define MAX_WIDGETS 32
|
||||
#define MAX_COMPACTORS 8
|
||||
@@ -507,6 +508,33 @@ sp_host_set_config(struct sp_host *h, const void *conf)
|
||||
h->conf = conf;
|
||||
}
|
||||
|
||||
const char *
|
||||
sp_host_plugin_config(struct sp_host *h, const char *plugin, const char *key)
|
||||
{
|
||||
return sp_config_plugin_value(h != NULL ? h->conf : NULL, plugin, key);
|
||||
}
|
||||
|
||||
const char *
|
||||
sp_host_plugin_config_at(struct sp_host *h, const char *plugin, size_t i,
|
||||
const char **key)
|
||||
{
|
||||
const struct sp_config *c = h != NULL ? h->conf : NULL;
|
||||
size_t k, seen = 0;
|
||||
|
||||
if (c == NULL || plugin == NULL)
|
||||
return NULL;
|
||||
for (k = 0; k < c->nplugin_conf; k++) {
|
||||
if (strcmp(c->plugin_conf[k].plugin, plugin) != 0)
|
||||
continue;
|
||||
if (seen++ == i) {
|
||||
if (key != NULL)
|
||||
*key = c->plugin_conf[k].key;
|
||||
return c->plugin_conf[k].val;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void
|
||||
sp_host_turn(struct sp_host *h)
|
||||
{
|
||||
|
||||
@@ -136,6 +136,79 @@ main(void)
|
||||
sp_buf_clear(&err);
|
||||
CHECK(load("model = m\nsteps = 7\n", c, &err) == 0 && c->steps == 7);
|
||||
|
||||
/* plugin blocks: one per plugin, its own settings beside it */
|
||||
sp_buf_clear(&err);
|
||||
setenv("HOME", "/home/cb", 1);
|
||||
CHECK(load(
|
||||
"plugin lcm {\n"
|
||||
" keep = 40\n"
|
||||
" tags = [a, b]\n"
|
||||
" opts { depth = 3 }\n"
|
||||
"}\n"
|
||||
"plugin tmux { }\n"
|
||||
"plugin off { enabled = false\n x = 1 }\n"
|
||||
"plugin mine {\n"
|
||||
" path = ~/my/plugin.so\n"
|
||||
" foo = bar\n"
|
||||
" server s1 { url = http://h/mcp }\n"
|
||||
"}\n"
|
||||
"plugin mcp {\n"
|
||||
" server junction { url = http://172.25.206.16:8090/mcp }\n"
|
||||
" server local {\n"
|
||||
" cmd = \"tasksd --stdio\"\n"
|
||||
" header = \"Authorization: Bearer ${CB_TOKEN}\"\n"
|
||||
" enabled = no\n"
|
||||
" }\n"
|
||||
"}\n", c, &err) == 0);
|
||||
CHECK(c->nplugins == 4);
|
||||
CHECK(c->nplugins == 4 && strcmp(c->plugins[0], "lcm") == 0 &&
|
||||
strcmp(c->plugins[1], "tmux") == 0 &&
|
||||
strcmp(c->plugins[2], "/home/cb/my/plugin.so") == 0 &&
|
||||
strcmp(c->plugins[3], "mcp") == 0);
|
||||
CHECK(sp_config_plugin_value(c, "lcm", "keep") != NULL &&
|
||||
strcmp(sp_config_plugin_value(c, "lcm", "keep"), "40") == 0);
|
||||
CHECK(sp_config_plugin_value(c, "lcm", "tags") != NULL &&
|
||||
strcmp(sp_config_plugin_value(c, "lcm", "tags"), "a,b") == 0);
|
||||
CHECK(sp_config_plugin_value(c, "lcm", "opts.depth") != NULL &&
|
||||
strcmp(sp_config_plugin_value(c, "lcm", "opts.depth"), "3") == 0);
|
||||
CHECK(sp_config_plugin_value(c, "mine", "foo") != NULL &&
|
||||
strcmp(sp_config_plugin_value(c, "mine", "foo"), "bar") == 0);
|
||||
CHECK(sp_config_plugin_value(c, "mine", "server.s1.url") != NULL &&
|
||||
strcmp(sp_config_plugin_value(c, "mine", "server.s1.url"),
|
||||
"http://h/mcp") == 0);
|
||||
CHECK(sp_config_plugin_value(c, "mine", "path") == NULL);
|
||||
CHECK(sp_config_plugin_value(c, "off", "x") == NULL);
|
||||
CHECK(sp_config_plugin_value(c, "lcm", "nope") == NULL);
|
||||
/* plugin mcp { server ... } is what mcp ID { } was */
|
||||
CHECK(c->nmcp == 2);
|
||||
CHECK(c->nmcp == 2 && strcmp(c->mcp[0].id, "junction") == 0 &&
|
||||
strcmp(c->mcp[0].url, "http://172.25.206.16:8090/mcp") == 0 &&
|
||||
c->mcp[0].enabled == 1);
|
||||
CHECK(c->nmcp == 2 && strcmp(c->mcp[1].id, "local") == 0 &&
|
||||
strcmp(c->mcp[1].cmd, "tasksd --stdio") == 0 &&
|
||||
strcmp(c->mcp[1].header, "Authorization: Bearer sekrit") == 0 &&
|
||||
c->mcp[1].enabled == 0);
|
||||
|
||||
/* the old forms still load: plugins = [...] and mcp ID { } */
|
||||
sp_buf_clear(&err);
|
||||
CHECK(load("core { plugins = [x/a.so] }\n"
|
||||
"mcp old { url = http://o/mcp }\n", c, &err) == 0);
|
||||
CHECK(c->nplugins == 1 && strcmp(c->plugins[0], "x/a.so") == 0);
|
||||
CHECK(c->nmcp == 1 && strcmp(c->mcp[0].id, "old") == 0);
|
||||
|
||||
/* errors */
|
||||
sp_buf_clear(&err);
|
||||
CHECK(load("plugin { x = 1 }\n", c, &err) != 0);
|
||||
sp_buf_clear(&err);
|
||||
CHECK(load("plugin a/b { }\n", c, &err) != 0);
|
||||
sp_buf_clear(&err);
|
||||
CHECK(load("plugin mcp {\n server { url = x }\n}\n", c, &err) != 0);
|
||||
sp_buf_clear(&err);
|
||||
CHECK(load("plugin mcp {\n server s { nope = x }\n}\n", c, &err) != 0);
|
||||
CHECK(strstr(err.p, ":2:") != NULL && strstr(err.p, "nope") != NULL);
|
||||
sp_buf_clear(&err);
|
||||
CHECK(load("plugin p { v = \"a|b\" }\n", c, &err) != 0);
|
||||
|
||||
free(c);
|
||||
if (failures == 0)
|
||||
puts("test_confblock: ok");
|
||||
|
||||
@@ -93,6 +93,21 @@ test_cli_plugin_path(void)
|
||||
CHECK(strcmp(got, "/explicit/cli.so") == 0);
|
||||
(void)unsetenv("SPLINTER_CLI_PLUGIN");
|
||||
|
||||
/* any shipped plugin by name; 'make install' writes a flat layout */
|
||||
(void)unlink(tree);
|
||||
(void)unlink(inst);
|
||||
snprintf(expect, sizeof(expect), "%s/lib/splinter/plugins/lcm.so", root);
|
||||
f = fopen(expect, "w");
|
||||
CHECK(f != NULL);
|
||||
if (f != NULL)
|
||||
fclose(f);
|
||||
sp_plugin_path(dir, "lcm", got, sizeof(got));
|
||||
snprintf(expect, sizeof(expect), "%s/bin/../lib/splinter/plugins/lcm.so",
|
||||
root);
|
||||
CHECK(strcmp(got, expect) == 0);
|
||||
sp_plugin_path(dir, "nothere", got, sizeof(got));
|
||||
CHECK(strstr(got, "/plugins/nothere/nothere.so") != NULL);
|
||||
|
||||
/* nothing found: the answer still names a cli plugin, so the child's
|
||||
* own error points at a place one can put it in (or at the build tree
|
||||
* this binary was compiled in) */
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
|
||||
#include "splinter.h"
|
||||
#include "splinter_plugin.h"
|
||||
#include "config.h"
|
||||
|
||||
static int failures;
|
||||
|
||||
@@ -115,6 +116,35 @@ main(void)
|
||||
CHECK(sp_registry_find(®, "broken") == NULL);
|
||||
}
|
||||
|
||||
/* a plugin reads its own block: plugin lcm { keep = 40 ... } */
|
||||
{
|
||||
static struct sp_config conf;
|
||||
const char *k = NULL;
|
||||
|
||||
CHECK(sp_host_plugin_config(host, "lcm", "keep") == NULL);
|
||||
sp_config_init(&conf);
|
||||
snprintf(conf.plugin_conf[0].plugin, 48, "lcm");
|
||||
snprintf(conf.plugin_conf[0].key, 96, "keep");
|
||||
snprintf(conf.plugin_conf[0].val, 512, "40");
|
||||
snprintf(conf.plugin_conf[1].plugin, 48, "mcp");
|
||||
snprintf(conf.plugin_conf[1].key, 96, "x");
|
||||
snprintf(conf.plugin_conf[1].val, 512, "y");
|
||||
snprintf(conf.plugin_conf[2].plugin, 48, "lcm");
|
||||
snprintf(conf.plugin_conf[2].key, 96, "server.a.url");
|
||||
snprintf(conf.plugin_conf[2].val, 512, "http://a");
|
||||
conf.nplugin_conf = 3;
|
||||
sp_host_set_config(host, &conf);
|
||||
CHECK(sp_host_plugin_config(host, "lcm", "keep") != NULL &&
|
||||
strcmp(sp_host_plugin_config(host, "lcm", "keep"), "40") == 0);
|
||||
CHECK(sp_host_plugin_config(host, "lcm", "x") == NULL);
|
||||
CHECK(sp_host_plugin_config_at(host, "lcm", 0, &k) != NULL &&
|
||||
k != NULL && strcmp(k, "keep") == 0);
|
||||
CHECK(sp_host_plugin_config_at(host, "lcm", 1, &k) != NULL &&
|
||||
k != NULL && strcmp(k, "server.a.url") == 0);
|
||||
CHECK(sp_host_plugin_config_at(host, "lcm", 2, &k) == NULL);
|
||||
sp_host_set_config(host, NULL);
|
||||
}
|
||||
|
||||
/* freeing the host stops the threads: no hang, no crash */
|
||||
sp_host_free(host);
|
||||
|
||||
|
||||
Reference in new issue
Block a user