[19:20:25] <lotherk> was ist ein semaphor? ein satz
[19:20:35] * Qwen3.6-35B-A3B-UD-Q8_K_XL is thinking: The user is asking in German
[19:20:38] <Qwen3.6-35B-A3B-UD-Q8_K_XL> Ein Semaphore ist eine Datenstruktur
The name comes from the environment and the configuration, not from a role label:
'<you>' was a literal, 'thinking is thinking' was a label talking about itself.
PgDn moved the scroll counter and never repainted - the screen agreed with
nothing. PgUp/PgDn now scroll ten lines, Shift+Up/Down one, and both repaint.
A tool's stderr went to the terminal and landed in the middle of the curses
screen. It is captured into the answer now ('{ cmd\n} 2>&1'), where the model can
read it and the UI stays intact.
Delegates for this machine (drop-in, keys by name only):
kontron 5 local 35B ocgo 10 opencode-go deepseek-v4.1-flash
ollama 8 cloud same model claudecli 9 claude -p
occli 9 opencode run --model big-pickle
A cli delegate no longer needs endpoint and model: claude and opencode bring
their own. The wrapper loads ~/.hermes/.env so keys are read, never copied.
961 lines
23 KiB
C
961 lines
23 KiB
C
/*
|
|
* splinter -- builtin tools.
|
|
*
|
|
* Copyright (c) 2026 Konrad Lother
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
*
|
|
* Two tools, on purpose: one that cannot change anything, one that can. Every
|
|
* new tool has to declare which class it belongs to, and the loop enforces it.
|
|
* Shell execution goes through popen("/bin/sh -c") and its exit status is
|
|
* reported; a command that fails is an observation, not an exception.
|
|
*/
|
|
#include <errno.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <dirent.h>
|
|
#include <sys/stat.h>
|
|
#include <sys/wait.h>
|
|
#include <unistd.h>
|
|
|
|
#include "splinter.h"
|
|
#include "mcp.h"
|
|
|
|
|
|
/*
|
|
* The model sends arguments as a JSON object ("{\"command\":\"ls\"}").
|
|
* Handing that string to popen() runs the JSON as a shell command - which is
|
|
* exactly the bug this function exists to prevent. It accepts both shapes: a
|
|
* JSON object with the named field, or a bare string (the mock transport and a
|
|
* hand-written script send the second).
|
|
*/
|
|
static const char *
|
|
arg_text(const char *args, const char *key, struct sp_buf *keep)
|
|
{
|
|
mcp_json *v;
|
|
const char *val = NULL;
|
|
|
|
if (args == NULL || *args == '\0')
|
|
return NULL;
|
|
if (args[0] != '{')
|
|
return args;
|
|
v = mcp_parse(args);
|
|
if (v == NULL)
|
|
return args;
|
|
val = mcp_arg_string(v, key);
|
|
if (val != NULL && sp_buf_puts(keep, val) == 0)
|
|
val = keep->p;
|
|
else
|
|
val = NULL;
|
|
mcp_json_free(v);
|
|
return val;
|
|
}
|
|
|
|
static int
|
|
tool_echo(const char *args, struct sp_buf *out, void *ctx)
|
|
{
|
|
struct sp_buf keep;
|
|
const char *text;
|
|
int rc;
|
|
|
|
(void)ctx;
|
|
if (sp_buf_init(&keep) != 0)
|
|
return -1;
|
|
text = arg_text(args, "text", &keep);
|
|
rc = sp_buf_puts(out, text != NULL ? text : "");
|
|
sp_buf_free(&keep);
|
|
return rc;
|
|
}
|
|
|
|
static int
|
|
tool_run(const char *args, struct sp_buf *out, void *ctx)
|
|
{
|
|
FILE *p;
|
|
char chunk[4096];
|
|
size_t got, total = 0;
|
|
int status;
|
|
|
|
struct sp_buf keep;
|
|
const char *cmd;
|
|
int rc;
|
|
|
|
(void)ctx;
|
|
if (sp_buf_init(&keep) != 0)
|
|
return -1;
|
|
cmd = arg_text(args, "command", &keep);
|
|
if (cmd == NULL || *cmd == '\0') {
|
|
sp_buf_free(&keep);
|
|
return sp_buf_puts(out, "error: empty command");
|
|
}
|
|
/*
|
|
* stderr of the child belongs in the answer, not on the terminal: a curses
|
|
* screen that suddenly contains a shell error is broken, and the error is
|
|
* exactly what the model needs to see.
|
|
*/
|
|
{
|
|
struct sp_buf wrapped = { 0 };
|
|
|
|
if (sp_buf_init(&wrapped) == 0 &&
|
|
sp_buf_printf(&wrapped, "{ %s\n} 2>&1", cmd) == 0) {
|
|
p = popen(wrapped.p, "r");
|
|
sp_buf_free(&wrapped);
|
|
} else
|
|
p = popen(cmd, "r");
|
|
}
|
|
if (p == NULL) {
|
|
rc = sp_buf_printf(out, "error: popen: %s", strerror(errno));
|
|
sp_buf_free(&keep);
|
|
return rc;
|
|
}
|
|
while ((got = fread(chunk, 1, sizeof(chunk), p)) > 0) {
|
|
if (total > 1u << 20) { /* 1 MiB cap, stated not silent */
|
|
sp_buf_puts(out, "\n[output truncated at 1 MiB]");
|
|
break;
|
|
}
|
|
if (sp_buf_add(out, chunk, got) != 0) {
|
|
pclose(p);
|
|
return -1;
|
|
}
|
|
total += got;
|
|
}
|
|
status = pclose(p);
|
|
sp_buf_free(&keep);
|
|
if (status < 0) {
|
|
return sp_buf_printf(out, "\n[exit status unknown: %s]",
|
|
strerror(errno));
|
|
}
|
|
if (WIFEXITED(status))
|
|
return sp_buf_printf(out, "\n[exit status %d]",
|
|
WEXITSTATUS(status));
|
|
return sp_buf_printf(out, "\n[killed by signal %d]", WTERMSIG(status));
|
|
}
|
|
|
|
|
|
/*
|
|
* File tools.
|
|
*
|
|
* The harness had echo, run and clock - a chat with a shell, not a coding
|
|
* agent. These five are the minimum for real work: look at a file, change one,
|
|
* find something, list a directory, create something.
|
|
*
|
|
* Nothing here is allowed to be clever. Writes are mutate, edits refuse an
|
|
* ambiguous match, reads are capped and say when they cut, and a binary file is
|
|
* reported instead of sprayed into the conversation.
|
|
*/
|
|
#define TOOL_MAX_READ 65536
|
|
#define TOOL_MAX_LINES 2000
|
|
#define TOOL_MAX_FILE (4 * 1024 * 1024)
|
|
#define TOOL_MAX_HITS 100
|
|
|
|
static int count_lines(const char *s, size_t upto);
|
|
static char *slurp(const char *path, size_t *lenp, struct sp_buf *err);
|
|
|
|
static int
|
|
arg_int(const char *args, const char *key, int dflt)
|
|
{
|
|
mcp_json *v;
|
|
int n;
|
|
|
|
if (args == NULL || args[0] != '{')
|
|
return dflt;
|
|
v = mcp_parse(args);
|
|
if (v == NULL)
|
|
return dflt;
|
|
n = mcp_arg_int(v, key, dflt);
|
|
mcp_json_free(v);
|
|
return n;
|
|
}
|
|
|
|
|
|
/*
|
|
* A diff the operator can read.
|
|
*
|
|
* One hunk, with the common prefix and suffix as context. A general diff
|
|
* algorithm would be more correct and is not worth it here: a tool changes one
|
|
* place, and the point is to see which place. The text goes into the observation,
|
|
* so the model sees what it did too - not just the harness.
|
|
*/
|
|
#define DIFF_CONTEXT 3
|
|
#define DIFF_MAX_LINES 200
|
|
|
|
struct lines {
|
|
char **v;
|
|
size_t n;
|
|
};
|
|
|
|
static int
|
|
split_lines(const char *text, struct lines *out)
|
|
{
|
|
char *copy, *p;
|
|
size_t cap = 16;
|
|
|
|
out->v = malloc(cap * sizeof(*out->v));
|
|
out->n = 0;
|
|
if (out->v == NULL)
|
|
return -1;
|
|
if (text == NULL || *text == '\0')
|
|
return 0;
|
|
copy = strdup(text);
|
|
if (copy == NULL) {
|
|
free(out->v);
|
|
return -1;
|
|
}
|
|
for (p = copy; *p != '\0'; ) {
|
|
char *nl = strchr(p, '\n');
|
|
if (out->n == cap) {
|
|
char **bigger = realloc(out->v, cap * 2 * sizeof(*bigger));
|
|
if (bigger == NULL) {
|
|
free(copy);
|
|
free(out->v);
|
|
return -1;
|
|
}
|
|
out->v = bigger;
|
|
cap *= 2;
|
|
}
|
|
out->v[out->n++] = strdup(p);
|
|
if (out->v[out->n - 1] == NULL) {
|
|
free(copy);
|
|
return -1;
|
|
}
|
|
if (nl == NULL)
|
|
break;
|
|
*strchr(out->v[out->n - 1], '\n') = '\0';
|
|
p = nl + 1;
|
|
}
|
|
free(copy);
|
|
return 0;
|
|
}
|
|
|
|
static void
|
|
free_lines(struct lines *l)
|
|
{
|
|
size_t i;
|
|
|
|
for (i = 0; i < l->n; i++)
|
|
free(l->v[i]);
|
|
free(l->v);
|
|
l->v = NULL;
|
|
l->n = 0;
|
|
}
|
|
|
|
static void
|
|
emit_diff(struct sp_buf *out, const char *path, const char *old,
|
|
const char *new)
|
|
{
|
|
struct lines a = { NULL, 0 }, b = { NULL, 0 };
|
|
size_t pre = 0, suf = 0, i;
|
|
long start_a, start_b;
|
|
|
|
if (split_lines(old, &a) != 0 || split_lines(new, &b) != 0)
|
|
goto done;
|
|
if (a.n == b.n) {
|
|
int same = 1;
|
|
|
|
for (i = 0; i < a.n; i++)
|
|
if (strcmp(a.v[i], b.v[i]) != 0)
|
|
same = 0;
|
|
if (same)
|
|
goto done; /* nothing changed: no diff, no noise */
|
|
}
|
|
while (pre < a.n && pre < b.n && strcmp(a.v[pre], b.v[pre]) == 0)
|
|
pre++;
|
|
while (suf < a.n - pre && suf < b.n - pre &&
|
|
strcmp(a.v[a.n - 1 - suf], b.v[b.n - 1 - suf]) == 0)
|
|
suf++;
|
|
|
|
start_a = (long)pre - DIFF_CONTEXT;
|
|
start_b = (long)pre - DIFF_CONTEXT;
|
|
if (start_a < 0) {
|
|
start_a = 0;
|
|
start_b = 0;
|
|
}
|
|
sp_buf_printf(out, "--- a/%s\n+++ b/%s\n", path, path);
|
|
sp_buf_printf(out, "@@ -%ld,%zu +%ld,%zu @@\n",
|
|
start_a + 1, a.n - (size_t)start_a - suf,
|
|
start_b + 1, b.n - (size_t)start_b - suf);
|
|
for (i = (size_t)start_a; i < pre && i < a.n; i++)
|
|
sp_buf_printf(out, " %s\n", a.v[i]);
|
|
{
|
|
size_t mid_a_end = a.n - suf;
|
|
size_t n = 0;
|
|
|
|
for (i = pre; i < mid_a_end && n < DIFF_MAX_LINES; i++, n++)
|
|
sp_buf_printf(out, "-%s\n", a.v[i]);
|
|
for (i = pre; i < b.n - suf && n < DIFF_MAX_LINES; i++, n++)
|
|
sp_buf_printf(out, "+%s\n", b.v[i]);
|
|
if (n >= DIFF_MAX_LINES)
|
|
sp_buf_puts(out, "[diff truncated]\n");
|
|
}
|
|
for (i = a.n - suf; i < a.n && i < a.n - suf + DIFF_CONTEXT; i++)
|
|
sp_buf_printf(out, " %s\n", a.v[i]);
|
|
done:
|
|
free_lines(&a);
|
|
free_lines(&b);
|
|
}
|
|
|
|
static int
|
|
looks_binary(const unsigned char *b, size_t n)
|
|
{
|
|
size_t i;
|
|
|
|
for (i = 0; i < n; i++)
|
|
if (b[i] == 0)
|
|
return 1;
|
|
return 0;
|
|
}
|
|
|
|
static int
|
|
tool_read(const char *args, struct sp_buf *out, void *ctx)
|
|
{
|
|
struct sp_buf keep;
|
|
const char *path;
|
|
char line[2048];
|
|
FILE *f;
|
|
int offset, limit, no = 0, printed = 0;
|
|
size_t total = 0;
|
|
|
|
(void)ctx;
|
|
if (sp_buf_init(&keep) != 0)
|
|
return -1;
|
|
path = arg_text(args, "path", &keep);
|
|
if (path == NULL) {
|
|
sp_buf_free(&keep);
|
|
return sp_buf_puts(out, "error: 'path' is required");
|
|
}
|
|
offset = arg_int(args, "offset", 1);
|
|
limit = arg_int(args, "limit", 200);
|
|
if (offset < 1)
|
|
offset = 1;
|
|
if (limit < 1 || limit > TOOL_MAX_LINES)
|
|
limit = TOOL_MAX_LINES;
|
|
|
|
f = fopen(path, "r");
|
|
if (f == NULL) {
|
|
sp_buf_printf(out, "error: %s: %s", path, strerror(errno));
|
|
sp_buf_free(&keep);
|
|
return 0;
|
|
}
|
|
{
|
|
unsigned char probe[512];
|
|
size_t n = fread(probe, 1, sizeof(probe), f);
|
|
|
|
if (looks_binary(probe, n)) {
|
|
sp_buf_printf(out, "error: %s is binary (%zu bytes read)", path, n);
|
|
fclose(f);
|
|
sp_buf_free(&keep);
|
|
return 0;
|
|
}
|
|
rewind(f);
|
|
}
|
|
while (fgets(line, sizeof(line), f) != NULL) {
|
|
no++;
|
|
if (no < offset)
|
|
continue;
|
|
if (printed >= limit || total + strlen(line) > TOOL_MAX_READ) {
|
|
sp_buf_printf(out, "[%s: stopped at line %d]\n", path, no);
|
|
break;
|
|
}
|
|
sp_buf_printf(out, "%6d\t%s", no, line);
|
|
total += strlen(line);
|
|
printed++;
|
|
}
|
|
fclose(f);
|
|
if (printed == 0 && no < offset)
|
|
sp_buf_printf(out, "[%s: no lines at offset %d (file has %d)]\n",
|
|
path, offset, no);
|
|
sp_buf_free(&keep);
|
|
return 0;
|
|
}
|
|
|
|
static int
|
|
tool_write(const char *args, struct sp_buf *out, void *ctx)
|
|
{
|
|
struct sp_buf keep, content;
|
|
const char *path, *text;
|
|
FILE *f;
|
|
size_t n;
|
|
|
|
(void)ctx;
|
|
if (sp_buf_init(&keep) != 0 || sp_buf_init(&content) != 0)
|
|
return -1;
|
|
path = arg_text(args, "path", &keep);
|
|
text = arg_text(args, "content", &content);
|
|
if (path == NULL || text == NULL) {
|
|
sp_buf_free(&keep);
|
|
sp_buf_free(&content);
|
|
return sp_buf_puts(out, "error: 'path' and 'content' are required");
|
|
}
|
|
n = strlen(text);
|
|
if (n > TOOL_MAX_FILE) {
|
|
sp_buf_printf(out, "error: refusing to write %zu bytes (limit %d)",
|
|
n, TOOL_MAX_FILE);
|
|
sp_buf_free(&keep);
|
|
sp_buf_free(&content);
|
|
return 0;
|
|
}
|
|
{
|
|
/* the previous content, only to show what changed */
|
|
size_t oldlen = 0;
|
|
char *old = NULL;
|
|
FILE *prev = fopen(path, "r");
|
|
|
|
if (prev != NULL) {
|
|
old = slurp(path, &oldlen, out);
|
|
fclose(prev);
|
|
}
|
|
f = fopen(path, "w");
|
|
if (f == NULL) {
|
|
sp_buf_printf(out, "error: %s: %s", path, strerror(errno));
|
|
free(old);
|
|
sp_buf_free(&keep);
|
|
sp_buf_free(&content);
|
|
return 0;
|
|
}
|
|
if (n > 0 && fwrite(text, 1, n, f) != n) {
|
|
sp_buf_printf(out, "error: short write: %s", strerror(errno));
|
|
fclose(f);
|
|
free(old);
|
|
sp_buf_free(&keep);
|
|
sp_buf_free(&content);
|
|
return 0;
|
|
}
|
|
fclose(f);
|
|
sp_buf_printf(out, "wrote %zu bytes to %s\n", n, path);
|
|
emit_diff(out, path, old != NULL ? old : "", text);
|
|
free(old);
|
|
sp_buf_free(&keep);
|
|
sp_buf_free(&content);
|
|
return 0;
|
|
}
|
|
if (n > 0 && fwrite(text, 1, n, f) != n) {
|
|
sp_buf_printf(out, "error: short write: %s", strerror(errno));
|
|
fclose(f);
|
|
sp_buf_free(&keep);
|
|
sp_buf_free(&content);
|
|
return 0;
|
|
}
|
|
fclose(f);
|
|
sp_buf_printf(out, "wrote %zu bytes to %s", n, path);
|
|
sp_buf_free(&keep);
|
|
sp_buf_free(&content);
|
|
return 0;
|
|
}
|
|
|
|
/* Read a whole file into memory; NULL when it is too large or binary. */
|
|
static char *
|
|
slurp(const char *path, size_t *lenp, struct sp_buf *err)
|
|
{
|
|
FILE *f;
|
|
struct sp_buf buf;
|
|
char chunk[4096];
|
|
size_t got;
|
|
|
|
f = fopen(path, "r");
|
|
if (f == NULL) {
|
|
sp_buf_printf(err, "error: %s: %s", path, strerror(errno));
|
|
return NULL;
|
|
}
|
|
if (sp_buf_init(&buf) != 0) {
|
|
fclose(f);
|
|
return NULL;
|
|
}
|
|
while ((got = fread(chunk, 1, sizeof(chunk), f)) > 0) {
|
|
if (buf.len + got > TOOL_MAX_FILE) {
|
|
sp_buf_printf(err, "error: %s is larger than %d bytes",
|
|
path, TOOL_MAX_FILE);
|
|
sp_buf_free(&buf);
|
|
fclose(f);
|
|
return NULL;
|
|
}
|
|
if (sp_buf_add(&buf, chunk, got) != 0) {
|
|
sp_buf_free(&buf);
|
|
fclose(f);
|
|
return NULL;
|
|
}
|
|
}
|
|
fclose(f);
|
|
if (buf.p == NULL) {
|
|
sp_buf_free(&buf);
|
|
sp_buf_printf(err, "error: %s is empty", path);
|
|
return NULL;
|
|
}
|
|
*lenp = buf.len;
|
|
return buf.p; /* caller owns p */
|
|
}
|
|
|
|
static int
|
|
count_occurrences(const char *hay, const char *needle)
|
|
{
|
|
int n = 0;
|
|
size_t nl = strlen(needle);
|
|
const char *p = hay;
|
|
|
|
if (nl == 0)
|
|
return -1;
|
|
while ((p = strstr(p, needle)) != NULL) {
|
|
n++;
|
|
p += nl;
|
|
}
|
|
return n;
|
|
}
|
|
|
|
static int
|
|
tool_edit(const char *args, struct sp_buf *out, void *ctx)
|
|
{
|
|
struct sp_buf keep, bufs[2];
|
|
const char *path, *old, *new;
|
|
char *text, *hit, *result;
|
|
size_t len, oldlen, newlen, prefix;
|
|
int count;
|
|
FILE *f;
|
|
|
|
(void)ctx;
|
|
if (sp_buf_init(&keep) != 0 || sp_buf_init(&bufs[0]) != 0 ||
|
|
sp_buf_init(&bufs[1]) != 0)
|
|
return -1;
|
|
path = arg_text(args, "path", &keep);
|
|
old = arg_text(args, "old", &bufs[0]);
|
|
new = arg_text(args, "new", &bufs[1]);
|
|
if (path == NULL || old == NULL || new == NULL) {
|
|
sp_buf_free(&keep);
|
|
sp_buf_free(&bufs[0]);
|
|
sp_buf_free(&bufs[1]);
|
|
return sp_buf_puts(out, "error: 'path', 'old' and 'new' are required");
|
|
}
|
|
oldlen = strlen(old);
|
|
newlen = strlen(new);
|
|
if (oldlen == 0) {
|
|
sp_buf_free(&keep);
|
|
sp_buf_free(&bufs[0]);
|
|
sp_buf_free(&bufs[1]);
|
|
return sp_buf_puts(out, "error: 'old' is empty");
|
|
}
|
|
|
|
text = slurp(path, &len, out);
|
|
if (text == NULL) {
|
|
sp_buf_free(&keep);
|
|
sp_buf_free(&bufs[0]);
|
|
sp_buf_free(&bufs[1]);
|
|
return 0;
|
|
}
|
|
count = count_occurrences(text, old);
|
|
if (count != 1) {
|
|
/* an ambiguous edit is a wrong edit waiting to happen */
|
|
sp_buf_printf(out, "error: 'old' occurs %d times in %s; "
|
|
"make it unique", count, path);
|
|
free(text);
|
|
sp_buf_free(&keep);
|
|
sp_buf_free(&bufs[0]);
|
|
sp_buf_free(&bufs[1]);
|
|
return 0;
|
|
}
|
|
hit = strstr(text, old);
|
|
prefix = (size_t)(hit - text);
|
|
result = malloc(len - oldlen + newlen + 1);
|
|
if (result == NULL) {
|
|
sp_buf_puts(out, "error: out of memory");
|
|
free(text);
|
|
sp_buf_free(&keep);
|
|
sp_buf_free(&bufs[0]);
|
|
sp_buf_free(&bufs[1]);
|
|
return 0;
|
|
}
|
|
memcpy(result, text, prefix);
|
|
memcpy(result + prefix, new, newlen);
|
|
memcpy(result + prefix + newlen, text + prefix + oldlen,
|
|
len - prefix - oldlen);
|
|
result[len - oldlen + newlen] = '\0';
|
|
|
|
f = fopen(path, "w");
|
|
if (f == NULL) {
|
|
sp_buf_printf(out, "error: %s: %s", path, strerror(errno));
|
|
} else {
|
|
size_t outlen = len - oldlen + newlen;
|
|
|
|
if (fwrite(result, 1, outlen, f) != outlen)
|
|
sp_buf_printf(out, "error: short write: %s",
|
|
strerror(errno));
|
|
else {
|
|
sp_buf_printf(out, "edited %s (line %d)\n", path,
|
|
1 + count_lines(text, prefix));
|
|
emit_diff(out, path, text, result);
|
|
}
|
|
fclose(f);
|
|
}
|
|
free(result);
|
|
free(text);
|
|
sp_buf_free(&keep);
|
|
sp_buf_free(&bufs[0]);
|
|
sp_buf_free(&bufs[1]);
|
|
return 0;
|
|
}
|
|
|
|
static int
|
|
count_lines(const char *s, size_t upto)
|
|
{
|
|
size_t i;
|
|
int n = 0;
|
|
|
|
for (i = 0; i < upto; i++)
|
|
if (s[i] == '\n')
|
|
n++;
|
|
return n;
|
|
}
|
|
|
|
static int
|
|
skip_name(const char *name)
|
|
{
|
|
return strcmp(name, ".git") == 0 || strcmp(name, ".") == 0 ||
|
|
strcmp(name, "..") == 0 || strcmp(name, "node_modules") == 0;
|
|
}
|
|
|
|
static void
|
|
search_dir(const char *dir, const char *pattern, struct sp_buf *out, int *hits,
|
|
int depth)
|
|
{
|
|
DIR *d;
|
|
struct dirent *e;
|
|
char path[1024], line[2048];
|
|
struct stat st;
|
|
|
|
if (depth > 12 || *hits >= TOOL_MAX_HITS)
|
|
return;
|
|
d = opendir(dir);
|
|
if (d == NULL)
|
|
return;
|
|
while ((e = readdir(d)) != NULL && *hits < TOOL_MAX_HITS) {
|
|
FILE *f;
|
|
|
|
if (skip_name(e->d_name))
|
|
continue;
|
|
snprintf(path, sizeof(path), "%s/%s", dir, e->d_name);
|
|
if (stat(path, &st) != 0)
|
|
continue;
|
|
if (S_ISDIR(st.st_mode)) {
|
|
search_dir(path, pattern, out, hits, depth + 1);
|
|
continue;
|
|
}
|
|
if (!S_ISREG(st.st_mode) || st.st_size > TOOL_MAX_FILE)
|
|
continue;
|
|
f = fopen(path, "r");
|
|
if (f == NULL)
|
|
continue;
|
|
while (fgets(line, sizeof(line), f) != NULL) {
|
|
if (strstr(line, pattern) == NULL)
|
|
continue;
|
|
sp_buf_printf(out, "%s: %s", path, line);
|
|
if (out->len > 0 && out->p[out->len - 1] != '\n')
|
|
sp_buf_puts(out, "\n");
|
|
(*hits)++;
|
|
if (*hits >= TOOL_MAX_HITS) {
|
|
sp_buf_puts(out, "[stopped at the hit limit]\n");
|
|
break;
|
|
}
|
|
}
|
|
fclose(f);
|
|
}
|
|
closedir(d);
|
|
}
|
|
|
|
static int
|
|
tool_search(const char *args, struct sp_buf *out, void *ctx)
|
|
{
|
|
struct sp_buf keep, pbuf;
|
|
const char *pattern, *root;
|
|
int hits = 0;
|
|
|
|
(void)ctx;
|
|
if (sp_buf_init(&keep) != 0 || sp_buf_init(&pbuf) != 0)
|
|
return -1;
|
|
pattern = arg_text(args, "pattern", &keep);
|
|
root = arg_text(args, "path", &pbuf);
|
|
if (pattern == NULL || *pattern == '\0') {
|
|
sp_buf_free(&keep);
|
|
sp_buf_free(&pbuf);
|
|
return sp_buf_puts(out, "error: 'pattern' is required");
|
|
}
|
|
search_dir(root != NULL ? root : ".", pattern, out, &hits, 0);
|
|
if (hits == 0)
|
|
sp_buf_puts(out, "(no match)");
|
|
sp_buf_free(&keep);
|
|
sp_buf_free(&pbuf);
|
|
return 0;
|
|
}
|
|
|
|
static int
|
|
tool_list(const char *args, struct sp_buf *out, void *ctx)
|
|
{
|
|
struct sp_buf keep;
|
|
const char *path;
|
|
DIR *d;
|
|
struct dirent *e;
|
|
int n = 0;
|
|
|
|
(void)ctx;
|
|
if (sp_buf_init(&keep) != 0)
|
|
return -1;
|
|
path = arg_text(args, "path", &keep);
|
|
if (path == NULL)
|
|
path = ".";
|
|
d = opendir(path);
|
|
if (d == NULL) {
|
|
sp_buf_printf(out, "error: %s: %s", path, strerror(errno));
|
|
sp_buf_free(&keep);
|
|
return 0;
|
|
}
|
|
while ((e = readdir(d)) != NULL && n < 300) {
|
|
char full[1024];
|
|
struct stat st;
|
|
|
|
if (strcmp(e->d_name, ".") == 0 || strcmp(e->d_name, "..") == 0)
|
|
continue;
|
|
snprintf(full, sizeof(full), "%s/%s", path, e->d_name);
|
|
if (stat(full, &st) == 0 && S_ISDIR(st.st_mode))
|
|
sp_buf_printf(out, "%s/\n", e->d_name);
|
|
else
|
|
sp_buf_printf(out, "%s\n", e->d_name);
|
|
n++;
|
|
}
|
|
closedir(d);
|
|
if (n == 0)
|
|
sp_buf_puts(out, "(empty)");
|
|
sp_buf_free(&keep);
|
|
return 0;
|
|
}
|
|
|
|
|
|
/*
|
|
* Web access through curl, because curl is what every machine already has and a
|
|
* second HTTP stack in the harness would only be a second set of bugs.
|
|
*
|
|
* Search backend: a SearXNG instance or anything that answers JSON:
|
|
*
|
|
* SPLINTER_SEARCH_URL=http://search.hiddenbox.org
|
|
*
|
|
* Without it DuckDuckGo's lite page is used - no key, no account, and its HTML is
|
|
* simple enough to strip honestly. Whatever comes back loses its tags and is
|
|
* capped; an answer that silently drops half a page is worse than a short one.
|
|
*/
|
|
static void
|
|
url_encode(const char *in, char *out, size_t len)
|
|
{
|
|
static const char hex[] = "0123456789ABCDEF";
|
|
size_t o = 0;
|
|
|
|
for (; *in != '\0' && o + 4 < len; in++) {
|
|
unsigned char c = (unsigned char)*in;
|
|
|
|
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
|
|
(c >= '0' && c <= '9') || c == '-' || c == '_' || c == '.' ||
|
|
c == '~') {
|
|
out[o++] = (char)c;
|
|
} else if (c == ' ') {
|
|
out[o++] = '+';
|
|
} else {
|
|
out[o++] = '%';
|
|
out[o++] = hex[c >> 4];
|
|
out[o++] = hex[c & 0x0f];
|
|
}
|
|
}
|
|
out[o] = '\0';
|
|
}
|
|
|
|
static int
|
|
curl_text(const char *url, struct sp_buf *out)
|
|
{
|
|
struct sp_buf cmd = { 0 };
|
|
FILE *p;
|
|
char chunk[4096];
|
|
size_t got, total = 0;
|
|
|
|
if (sp_buf_init(&cmd) != 0)
|
|
return -1;
|
|
if (sp_buf_printf(&cmd,
|
|
"curl -sSL --max-time 25 -A 'splinter/0.1' '%s' "
|
|
"| sed -e 's/<[^>]*>/ /g' -e 's/ / /g' "
|
|
"| tr -s ' \t' ' ' | grep -v '^ *$' | head -c 20000", url) != 0) {
|
|
sp_buf_free(&cmd);
|
|
return -1;
|
|
}
|
|
p = popen(cmd.p, "r");
|
|
sp_buf_free(&cmd);
|
|
if (p == NULL)
|
|
return sp_buf_printf(out, "error: curl: %s", strerror(errno));
|
|
while ((got = fread(chunk, 1, sizeof(chunk), p)) > 0) {
|
|
total += got;
|
|
(void)sp_buf_add(out, chunk, got);
|
|
}
|
|
(void)pclose(p);
|
|
if (total == 0)
|
|
sp_buf_puts(out, "(no answer - check the connection or "
|
|
"SPLINTER_SEARCH_URL)");
|
|
return 0;
|
|
}
|
|
|
|
static int
|
|
tool_websearch(const char *args, struct sp_buf *out, void *ctx)
|
|
{
|
|
struct sp_buf keep = { 0 }, url = { 0 };
|
|
const char *q, *base;
|
|
char enc[1024];
|
|
int rc;
|
|
|
|
(void)ctx;
|
|
if (sp_buf_init(&keep) != 0 || sp_buf_init(&url) != 0)
|
|
return -1;
|
|
q = arg_text(args, "query", &keep);
|
|
if (q == NULL || *q == '\0') {
|
|
sp_buf_free(&keep);
|
|
sp_buf_free(&url);
|
|
return sp_buf_puts(out, "error: empty query");
|
|
}
|
|
url_encode(q, enc, sizeof(enc));
|
|
base = getenv("SPLINTER_SEARCH_URL");
|
|
if (base != NULL && *base != '\0')
|
|
(void)sp_buf_printf(&url, "%s/search?q=%s&format=json", base, enc);
|
|
else
|
|
(void)sp_buf_printf(&url,
|
|
"https://lite.duckduckgo.com/lite/?q=%s", enc);
|
|
rc = curl_text(url.p, out);
|
|
sp_buf_free(&keep);
|
|
sp_buf_free(&url);
|
|
return rc;
|
|
}
|
|
|
|
static int
|
|
tool_webfetch(const char *args, struct sp_buf *out, void *ctx)
|
|
{
|
|
struct sp_buf keep = { 0 };
|
|
const char *u;
|
|
int rc;
|
|
|
|
(void)ctx;
|
|
if (sp_buf_init(&keep) != 0)
|
|
return -1;
|
|
u = arg_text(args, "url", &keep);
|
|
if (u == NULL || *u == '\0') {
|
|
sp_buf_free(&keep);
|
|
return sp_buf_puts(out, "error: empty url");
|
|
}
|
|
rc = curl_text(u, out);
|
|
sp_buf_free(&keep);
|
|
return rc;
|
|
}
|
|
|
|
|
|
int
|
|
sp_builtin_register(struct sp_registry *r)
|
|
{
|
|
struct sp_tool t;
|
|
|
|
memset(&t, 0, sizeof(t));
|
|
t.name = "echo";
|
|
t.description = "Return the argument unchanged.";
|
|
t.params = "{\"type\":\"object\",\"properties\":{\"text\":"
|
|
"{\"type\":\"string\",\"description\":\"text to return\"}},"
|
|
"\"required\":[\"text\"]}";
|
|
t.auth = SP_AUTH_READ;
|
|
t.run = tool_echo;
|
|
if (sp_registry_add(r, &t) != 0)
|
|
return -1;
|
|
|
|
memset(&t, 0, sizeof(t));
|
|
t.name = "websearch";
|
|
t.description = "Search the web and return the results as plain text. "
|
|
"Use it before inventing an answer about anything current.";
|
|
t.params = "{\"type\":\"object\",\"properties\":{\"query\":"
|
|
"{\"type\":\"string\",\"description\":\"what to search for\"}},"
|
|
"\"required\":[\"query\"]}";
|
|
t.auth = SP_AUTH_READ;
|
|
t.run = tool_websearch;
|
|
if (sp_registry_add(r, &t) != 0)
|
|
return -1;
|
|
|
|
memset(&t, 0, sizeof(t));
|
|
t.name = "webfetch";
|
|
t.description = "Fetch one URL and return it as plain text.";
|
|
t.params = "{\"type\":\"object\",\"properties\":{\"url\":"
|
|
"{\"type\":\"string\",\"description\":\"http or https URL\"}},"
|
|
"\"required\":[\"url\"]}";
|
|
t.auth = SP_AUTH_READ;
|
|
t.run = tool_webfetch;
|
|
if (sp_registry_add(r, &t) != 0)
|
|
return -1;
|
|
|
|
memset(&t, 0, sizeof(t));
|
|
t.name = "run";
|
|
t.description = "Run a shell command and return its combined output.";
|
|
t.params = "{\"type\":\"object\",\"properties\":{\"command\":"
|
|
"{\"type\":\"string\",\"description\":\"shell command\"}},"
|
|
"\"required\":[\"command\"]}";
|
|
t.auth = SP_AUTH_DESTRUCTIVE;
|
|
t.run = tool_run;
|
|
if (sp_registry_add(r, &t) != 0)
|
|
return -1;
|
|
|
|
memset(&t, 0, sizeof(t));
|
|
t.name = "read";
|
|
t.description = "Read a text file. Returns numbered lines and says "
|
|
"where it stopped.";
|
|
t.params = "{\"type\":\"object\",\"properties\":{"
|
|
"\"path\":{\"type\":\"string\",\"description\":\"file to read\"},"
|
|
"\"offset\":{\"type\":\"integer\",\"description\":\"first line, 1-based\"},"
|
|
"\"limit\":{\"type\":\"integer\",\"description\":\"max lines\"}},"
|
|
"\"required\":[\"path\"]}";
|
|
t.auth = SP_AUTH_READ;
|
|
t.run = tool_read;
|
|
if (sp_registry_add(r, &t) != 0)
|
|
return -1;
|
|
|
|
memset(&t, 0, sizeof(t));
|
|
t.name = "write";
|
|
t.description = "Write a file, replacing it if it exists.";
|
|
t.params = "{\"type\":\"object\",\"properties\":{"
|
|
"\"path\":{\"type\":\"string\"},"
|
|
"\"content\":{\"type\":\"string\"}},"
|
|
"\"required\":[\"path\",\"content\"]}";
|
|
t.auth = SP_AUTH_MUTATE;
|
|
t.run = tool_write;
|
|
if (sp_registry_add(r, &t) != 0)
|
|
return -1;
|
|
|
|
memset(&t, 0, sizeof(t));
|
|
t.name = "edit";
|
|
t.description = "Replace one occurrence of 'old' with 'new'. Refuses "
|
|
"when 'old' is not unique.";
|
|
t.params = "{\"type\":\"object\",\"properties\":{"
|
|
"\"path\":{\"type\":\"string\"},"
|
|
"\"old\":{\"type\":\"string\",\"description\":\"exact text to replace\"},"
|
|
"\"new\":{\"type\":\"string\",\"description\":\"replacement\"}},"
|
|
"\"required\":[\"path\",\"old\",\"new\"]}";
|
|
t.auth = SP_AUTH_MUTATE;
|
|
t.run = tool_edit;
|
|
if (sp_registry_add(r, &t) != 0)
|
|
return -1;
|
|
|
|
memset(&t, 0, sizeof(t));
|
|
t.name = "search";
|
|
t.description = "Search files under a directory for a literal string.";
|
|
t.params = "{\"type\":\"object\",\"properties\":{"
|
|
"\"pattern\":{\"type\":\"string\"},"
|
|
"\"path\":{\"type\":\"string\",\"description\":\"directory, default .\"}},"
|
|
"\"required\":[\"pattern\"]}";
|
|
t.auth = SP_AUTH_READ;
|
|
t.run = tool_search;
|
|
if (sp_registry_add(r, &t) != 0)
|
|
return -1;
|
|
|
|
memset(&t, 0, sizeof(t));
|
|
t.name = "list";
|
|
t.description = "List a directory.";
|
|
t.params = "{\"type\":\"object\",\"properties\":{"
|
|
"\"path\":{\"type\":\"string\",\"description\":\"directory, default .\"}}}";
|
|
t.auth = SP_AUTH_READ;
|
|
t.run = tool_list;
|
|
if (sp_registry_add(r, &t) != 0)
|
|
return -1;
|
|
|
|
return 0;
|
|
}
|
|
|