Files
splinter/src/backend/buf.c
T
lotherk a9f175c25b backend/frontend: an engine every front end can talk to
The harness was two halves that could not see each other: main.c built the
agent, ui.c ran it -- with the curses thread model, the approval condition
variable, the slash commands and the provider switching inside the drawing
code. A second front end had to copy all of it.

Now:

  src/backend/   engine.c owns the session, the turn thread, the event queue
                 and every command; agent, transports, tools, session, config,
                 providers, plugins and the protocol servers move here.
  src/frontend/  curses.c draws and reads keys through ui_backend.h;
                 ui_local.c is the engine in this process, ui_remote.c is a
                 served engine behind a socket. protocol.c serves the engine
                 as JSON lines (protocol 2: history, approvals, steering);
                 client.c is splinterctl, the reference client.

splinter --serve / --attach: the same screen runs as a client of a served
engine, so the socket carries everything the front end needs. docs/PROTOCOL.md
and docs/FRONTENDS.md describe the contract; docs/proto-example.py is a
60-line client. tests/test_engine, test_protocol and test_attach cover the
event order, the socket and the attach mode.

Review fixes in the same tree: dynamic line buffers (9 KB prompts and 8 KB
events were truncated), a turn event no longer ends the busy window (steer
leftovers), the screen finalises every turn, /new keeps the profile, reopens
the log and gets unique ids, the profile is remembered in <session>.agent so
--resume comes back in it, agent statistics are a locked snapshot (no data
race), !shell keeps 64 KiB with a marker, the socket probes for a live server,
compares its inode and sets CLOEXEC/SNDTIMEO, exit 2 means configuration,
BSD peer checks use getpeereid, and the i18n checker scans the new
subdirectories again.

Includes the parallel session's SSE fix: the parser reports what a line
produced, so a tool name no longer lands in the answer ("readread").
2026-11-16 09:06:51 +00:00

172 lines
3.4 KiB
C

/*
* splinter -- growable buffers, JSON escaping.
*
* Copyright (c) 2026 Konrad Lother
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "splinter.h"
#define SP_MIN_CAP 64
int
sp_buf_reserve(struct sp_buf *b, size_t want)
{
size_t ncap;
char *np;
if (want <= b->cap)
return 0;
ncap = b->cap ? b->cap : SP_MIN_CAP;
while (ncap < want) {
if (ncap > (size_t)-1 / 2) {
errno = ENOMEM;
return -1;
}
ncap *= 2;
}
np = realloc(b->p, ncap);
if (np == NULL)
return -1;
b->p = np;
b->cap = ncap;
return 0;
}
int
sp_buf_init(struct sp_buf *b)
{
memset(b, 0, sizeof(*b));
if (sp_buf_reserve(b, SP_MIN_CAP) != 0)
return -1;
b->p[0] = '\0';
return 0;
}
int
sp_buf_add(struct sp_buf *b, const char *data, size_t n)
{
if (n == 0)
return 0;
/* keep one spare byte for the NUL we always maintain */
if (b->len + n + 1 < b->len) { /* overflow of the sum */
errno = ENOMEM;
return -1;
}
if (sp_buf_reserve(b, b->len + n + 1) != 0)
return -1;
memcpy(b->p + b->len, data, n);
b->len += n;
b->p[b->len] = '\0';
return 0;
}
int
sp_buf_puts(struct sp_buf *b, const char *s)
{
return sp_buf_add(b, s, strlen(s));
}
int
sp_buf_printf(struct sp_buf *b, const char *fmt, ...)
{
va_list ap, ap2;
int need;
va_start(ap, fmt);
va_copy(ap2, ap);
need = vsnprintf(NULL, 0, fmt, ap);
va_end(ap);
if (need < 0) {
va_end(ap2);
return -1;
}
if (sp_buf_reserve(b, b->len + (size_t)need + 1) != 0) {
va_end(ap2);
return -1;
}
vsnprintf(b->p + b->len, b->cap - b->len, fmt, ap2);
va_end(ap2);
b->len += (size_t)need;
return 0;
}
void
sp_buf_clear(struct sp_buf *b)
{
b->len = 0;
if (b->p != NULL)
b->p[0] = '\0';
}
void
sp_buf_free(struct sp_buf *b)
{
free(b->p);
memset(b, 0, sizeof(*b));
}
int
sp_json_escape(struct sp_buf *b, const char *s)
{
static const char hex[] = "0123456789abcdef";
const unsigned char *p = (const unsigned char *)s;
while (*p != '\0') {
unsigned char c = *p;
switch (c) {
case '"': if (sp_buf_puts(b, "\\\"") != 0) return -1; p++; break;
case '\\': if (sp_buf_puts(b, "\\\\") != 0) return -1; p++; break;
case '\n': if (sp_buf_puts(b, "\\n") != 0) return -1; p++; break;
case '\r': if (sp_buf_puts(b, "\\r") != 0) return -1; p++; break;
case '\t': if (sp_buf_puts(b, "\\t") != 0) return -1; p++; break;
default:
if (c < 0x20) {
char esc[7] = { '\\', 'u', '0', '0',
hex[c >> 4], hex[c & 0xf], '\0' };
if (sp_buf_puts(b, esc) != 0)
return -1;
p++;
} else if (c < 0x80) {
if (sp_buf_add(b, (const char *)p, 1) != 0)
return -1;
p++;
} else {
/* Valid UTF-8 passes through; invalid bytes become \u00XX
* so the JSON string stays well-formed UTF-8. */
size_t n = 0;
if ((c & 0xe0) == 0xc0 && (p[1] & 0xc0) == 0x80 &&
c >= 0xc2)
n = 2;
else if ((c & 0xf0) == 0xe0 && (p[1] & 0xc0) == 0x80 &&
(p[2] & 0xc0) == 0x80)
n = 3;
else if ((c & 0xf8) == 0xf0 && (p[1] & 0xc0) == 0x80 &&
(p[2] & 0xc0) == 0x80 && (p[3] & 0xc0) == 0x80 &&
c <= 0xf4)
n = 4;
if (n > 0) {
if (sp_buf_add(b, (const char *)p, n) != 0)
return -1;
p += n;
} else {
char esc[7] = { '\\', 'u', '0', '0',
hex[c >> 4], hex[c & 0xf], '\0' };
if (sp_buf_puts(b, esc) != 0)
return -1;
p++;
}
}
break;
}
}
return 0;
}