drop tmux/planner/sysinfo, tasks becomes processes, no capitalised calls
- plugins/tmux, plugins/planner and plugins/sysinfo are gone (plugin list, Makefile rules, tests, docs). The config no longer loads them; the stale .so files were removed from the install tree. - plugins/tasks is plugins/processes: plugin name "processes", widget "processes", tools process_start/process_list/process_result/process_kill. The sidebar now lists only running work: a finished task keeps its output in process_result but stops taking a row forever. - the dead planner link in the delegate (link_plan_quota -> command "tasks") is removed with the planner. - tool call rows are no longer capitalised: "run ls -l", "read src/x.c" as the model sent them, not "Run "/"Read ". - tests updated (delegate panel expects no finished row, engine/UI expect the lowercase call row, secrets/delegate use the processes plugin and tools). - the user's config gets a Delegate/Scheduler section: the three providers carry review/plan/docs tags and keep max_parallel=2; plugin scheduler stays loaded so waiting jobs are retried. make check: all tests passed.
This commit is contained in:
1 parent
1e15c91e4e
commit
8aee12e9d4
17 files changed
+73
-2074
No files matched your search
@@ -1,633 +0,0 @@
|
||||
/*
|
||||
* splinter -- the plan of a session (/tasks).
|
||||
*
|
||||
* Copyright (c) 2026 Konrad Lother
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*
|
||||
* A list of things the session means to do, kept in <session>/plan_tasks.json.
|
||||
* It is not a process table: a shell command with a pid and a clock belongs to
|
||||
* the tasks plugin (task_start). Here a task is a sentence and a state:
|
||||
*
|
||||
* todo -> doing -> done blocked waiting_quota
|
||||
*
|
||||
* The file is the contract. Every operation reads it, changes it and writes it
|
||||
* back under one lock, so the tools, the /tasks command, the sidebar tick and a
|
||||
* second splinter looking at the same session all see the same list. Making a
|
||||
* task "doing" sets /goal to its text (the focus plugin), which is what keeps a
|
||||
* long session from drifting.
|
||||
*
|
||||
* Other code reaches this through the command, not through this file:
|
||||
* the delegate parks a task with "/tasks wait <id> <when> <note>" when its
|
||||
* provider is out of quota, and the tick below puts it back to todo when the
|
||||
* time has come.
|
||||
*/
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "splinter.h"
|
||||
#include "mcp.h"
|
||||
#include "splinter_plugin.h"
|
||||
#include "i18n.h"
|
||||
|
||||
#define MAX_TASKS 128
|
||||
#define DESC_LEN 1024
|
||||
#define NOTE_LEN 256
|
||||
#define WHEN_LEN 64
|
||||
#define STATUS_LEN 24
|
||||
#define FILE_MAX (256 * 1024)
|
||||
|
||||
struct task {
|
||||
int id;
|
||||
char status[STATUS_LEN];
|
||||
char desc[DESC_LEN];
|
||||
char run_after[WHEN_LEN]; /* unix time, or empty */
|
||||
char note[NOTE_LEN];
|
||||
long created;
|
||||
long updated;
|
||||
};
|
||||
|
||||
static const char *const states[] = {
|
||||
"todo", "doing", "done", "blocked", "waiting_quota", NULL
|
||||
};
|
||||
|
||||
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
|
||||
static struct sp_host *host_ref;
|
||||
static struct task tasks[MAX_TASKS];
|
||||
static size_t ntasks;
|
||||
static int next_id = 1;
|
||||
static int current_id = -1; /* the task in "doing" */
|
||||
|
||||
/* ------------------------------------------------------------- the file */
|
||||
|
||||
static int
|
||||
store_path(char *path, size_t len)
|
||||
{
|
||||
const char *dir = sp_host_session_dir(host_ref);
|
||||
|
||||
if (dir == NULL || dir[0] == '\0')
|
||||
return -1;
|
||||
snprintf(path, len, "%s/plan_tasks.json", dir);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
copy_str(char *dst, size_t len, const char *src)
|
||||
{
|
||||
snprintf(dst, len, "%s", src != NULL ? src : "");
|
||||
}
|
||||
|
||||
static void
|
||||
load(void)
|
||||
{
|
||||
char path[600];
|
||||
char *text;
|
||||
FILE *f;
|
||||
size_t n, i;
|
||||
mcp_json *root;
|
||||
|
||||
ntasks = 0;
|
||||
next_id = 1;
|
||||
current_id = -1;
|
||||
if (store_path(path, sizeof(path)) != 0 || (f = fopen(path, "r")) == NULL)
|
||||
return;
|
||||
if ((text = malloc(FILE_MAX + 1)) == NULL) {
|
||||
fclose(f);
|
||||
return;
|
||||
}
|
||||
n = fread(text, 1, FILE_MAX, f);
|
||||
fclose(f);
|
||||
text[n] = '\0';
|
||||
root = mcp_parse(text);
|
||||
free(text);
|
||||
if (root == NULL)
|
||||
return;
|
||||
for (i = 0; i < mcp_array_len(root) && ntasks < MAX_TASKS; i++) {
|
||||
const mcp_json *o = mcp_array_at(root, i);
|
||||
struct task *t = &tasks[ntasks];
|
||||
|
||||
memset(t, 0, sizeof(*t));
|
||||
t->id = mcp_arg_int(o, "id", 0);
|
||||
if (t->id <= 0)
|
||||
t->id = next_id;
|
||||
copy_str(t->status, sizeof(t->status),
|
||||
mcp_arg_string(o, "status"));
|
||||
if (t->status[0] == '\0')
|
||||
copy_str(t->status, sizeof(t->status), "todo");
|
||||
copy_str(t->desc, sizeof(t->desc), mcp_arg_string(o, "desc"));
|
||||
copy_str(t->run_after, sizeof(t->run_after),
|
||||
mcp_arg_string(o, "run_after"));
|
||||
copy_str(t->note, sizeof(t->note), mcp_arg_string(o, "note"));
|
||||
t->created = (long)mcp_arg_num(o, "created", 0);
|
||||
t->updated = (long)mcp_arg_num(o, "updated", 0);
|
||||
if (t->id >= next_id)
|
||||
next_id = t->id + 1;
|
||||
if (strcmp(t->status, "doing") == 0)
|
||||
current_id = t->id;
|
||||
ntasks++;
|
||||
}
|
||||
mcp_json_free(root);
|
||||
}
|
||||
|
||||
static void
|
||||
put_field(struct sp_buf *b, const char *key, const char *val)
|
||||
{
|
||||
sp_buf_printf(b, "\"%s\":\"", key);
|
||||
sp_json_escape(b, val);
|
||||
sp_buf_puts(b, "\"");
|
||||
}
|
||||
|
||||
/* Write to a temporary name and rename: a reader never sees half a file. */
|
||||
static void
|
||||
save(void)
|
||||
{
|
||||
char path[600], tmp[620];
|
||||
struct sp_buf b;
|
||||
size_t i;
|
||||
FILE *f;
|
||||
|
||||
if (store_path(path, sizeof(path)) != 0 || sp_buf_init(&b) != 0)
|
||||
return;
|
||||
sp_buf_puts(&b, "[\n");
|
||||
for (i = 0; i < ntasks; i++) {
|
||||
const struct task *t = &tasks[i];
|
||||
|
||||
sp_buf_printf(&b, " {\"id\":%d,", t->id);
|
||||
put_field(&b, "status", t->status);
|
||||
sp_buf_puts(&b, ",");
|
||||
put_field(&b, "desc", t->desc);
|
||||
sp_buf_puts(&b, ",");
|
||||
put_field(&b, "run_after", t->run_after);
|
||||
sp_buf_puts(&b, ",");
|
||||
put_field(&b, "note", t->note);
|
||||
sp_buf_printf(&b, ",\"created\":%ld,\"updated\":%ld}%s\n",
|
||||
t->created, t->updated, i + 1 < ntasks ? "," : "");
|
||||
}
|
||||
sp_buf_puts(&b, "]\n");
|
||||
snprintf(tmp, sizeof(tmp), "%s.tmp", path);
|
||||
if ((f = fopen(tmp, "w")) != NULL) {
|
||||
int ok = fwrite(b.p, 1, b.len, f) == b.len;
|
||||
|
||||
if (fclose(f) != 0)
|
||||
ok = 0;
|
||||
if (!ok || rename(tmp, path) != 0)
|
||||
unlink(tmp);
|
||||
}
|
||||
sp_buf_free(&b);
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------- state */
|
||||
|
||||
static struct task *
|
||||
find(int id)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < ntasks; i++)
|
||||
if (tasks[i].id == id)
|
||||
return &tasks[i];
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static struct task *
|
||||
first_todo(void)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < ntasks; i++)
|
||||
if (strcmp(tasks[i].status, "todo") == 0)
|
||||
return &tasks[i];
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int
|
||||
valid_state(const char *s)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
for (i = 0; states[i] != NULL; i++)
|
||||
if (strcmp(states[i], s) == 0)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* "+90" is ninety seconds from now; anything else is taken as written. */
|
||||
static void
|
||||
when(const char *in, char *out, size_t len)
|
||||
{
|
||||
out[0] = '\0';
|
||||
if (in == NULL || in[0] == '\0')
|
||||
return;
|
||||
if (in[0] == '+') {
|
||||
long d = strtol(in + 1, NULL, 10);
|
||||
|
||||
if (d <= 0)
|
||||
d = 3600;
|
||||
snprintf(out, len, "%ld", (long)time(NULL) + d);
|
||||
return;
|
||||
}
|
||||
snprintf(out, len, "%s", in);
|
||||
}
|
||||
|
||||
static void
|
||||
widget(void)
|
||||
{
|
||||
char b[256];
|
||||
size_t i;
|
||||
int todo = 0, doing = 0, done = 0;
|
||||
|
||||
for (i = 0; i < ntasks; i++) {
|
||||
if (strcmp(tasks[i].status, "todo") == 0)
|
||||
todo++;
|
||||
else if (strcmp(tasks[i].status, "doing") == 0)
|
||||
doing++;
|
||||
else if (strcmp(tasks[i].status, "done") == 0)
|
||||
done++;
|
||||
}
|
||||
if (ntasks == 0)
|
||||
b[0] = '\0'; /* no plan: nothing on the sidebar */
|
||||
else if (current_id > 0)
|
||||
snprintf(b, sizeof(b), sp_td("plan.widget_active",
|
||||
"#%d active - todo %d, doing %d, done %d"), current_id,
|
||||
todo, doing, done);
|
||||
else
|
||||
snprintf(b, sizeof(b), sp_td("plan.widget",
|
||||
"todo %d, doing %d, done %d"), todo, doing, done);
|
||||
sp_host_widget_text(host_ref, "plan", b);
|
||||
}
|
||||
|
||||
/* One task is "doing" at a time; its text becomes the session goal. */
|
||||
static void
|
||||
start(struct task *t)
|
||||
{
|
||||
size_t i;
|
||||
struct sp_buf ignored;
|
||||
|
||||
for (i = 0; i < ntasks; i++)
|
||||
if (strcmp(tasks[i].status, "doing") == 0)
|
||||
copy_str(tasks[i].status, STATUS_LEN, "todo");
|
||||
copy_str(t->status, STATUS_LEN, "doing");
|
||||
t->updated = (long)time(NULL);
|
||||
current_id = t->id;
|
||||
if (sp_buf_init(&ignored) == 0) {
|
||||
(void)sp_host_command(host_ref, "goal", t->desc, &ignored);
|
||||
sp_buf_free(&ignored);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
changed(void)
|
||||
{
|
||||
save();
|
||||
widget();
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ operations */
|
||||
|
||||
static int
|
||||
op_add(const char *desc, const char *run_after, struct sp_buf *out)
|
||||
{
|
||||
struct task *t;
|
||||
|
||||
if (desc == NULL || desc[0] == '\0')
|
||||
return sp_buf_puts(out, "error: description required\n");
|
||||
if (ntasks >= MAX_TASKS)
|
||||
return sp_buf_printf(out, "error: the plan holds %d tasks\n",
|
||||
MAX_TASKS);
|
||||
t = &tasks[ntasks++];
|
||||
memset(t, 0, sizeof(*t));
|
||||
t->id = next_id++;
|
||||
copy_str(t->status, sizeof(t->status), "todo");
|
||||
copy_str(t->desc, sizeof(t->desc), desc);
|
||||
when(run_after, t->run_after, sizeof(t->run_after));
|
||||
t->created = t->updated = (long)time(NULL);
|
||||
changed();
|
||||
return sp_buf_printf(out, "added plan task #%d: %s\n", t->id, t->desc);
|
||||
}
|
||||
|
||||
static int
|
||||
op_list(struct sp_buf *out)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
if (ntasks == 0)
|
||||
return sp_buf_puts(out, "(no plan tasks) use plan_task_add\n");
|
||||
sp_buf_puts(out, "plan tasks:\n");
|
||||
for (i = 0; i < ntasks; i++) {
|
||||
const struct task *t = &tasks[i];
|
||||
|
||||
sp_buf_printf(out, " #%d [%s]%s %s", t->id, t->status,
|
||||
t->id == current_id ? " *" : "", t->desc);
|
||||
if (t->run_after[0] != '\0')
|
||||
sp_buf_printf(out, " (run_after=%s)", t->run_after);
|
||||
if (t->note[0] != '\0')
|
||||
sp_buf_printf(out, " # %s", t->note);
|
||||
sp_buf_puts(out, "\n");
|
||||
}
|
||||
return sp_buf_puts(out, "use /tasks work <id> | work all | next\n");
|
||||
}
|
||||
|
||||
static int
|
||||
op_done(int id, struct sp_buf *out)
|
||||
{
|
||||
struct task *t = find(id);
|
||||
|
||||
if (t == NULL)
|
||||
return sp_buf_puts(out, "no current task to mark done\n");
|
||||
copy_str(t->status, sizeof(t->status), "done");
|
||||
t->updated = (long)time(NULL);
|
||||
if (current_id == id)
|
||||
current_id = -1;
|
||||
changed();
|
||||
return sp_buf_printf(out, "task #%d done\n", id);
|
||||
}
|
||||
|
||||
/* Park a task until a time: the delegate does this when a quota runs out. */
|
||||
static int
|
||||
op_wait(int id, const char *at, const char *note, struct sp_buf *out)
|
||||
{
|
||||
struct task *t = find(id);
|
||||
|
||||
if (t == NULL)
|
||||
return sp_buf_printf(out, "no task #%d\n", id);
|
||||
if (at != NULL && at[0] != '\0')
|
||||
when(at, t->run_after, sizeof(t->run_after));
|
||||
else
|
||||
when("+3600", t->run_after, sizeof(t->run_after));
|
||||
copy_str(t->status, sizeof(t->status), "waiting_quota");
|
||||
copy_str(t->note, sizeof(t->note),
|
||||
note != NULL && note[0] != '\0' ? note : "quota wait");
|
||||
t->updated = (long)time(NULL);
|
||||
if (current_id == id)
|
||||
current_id = -1;
|
||||
changed();
|
||||
return sp_buf_printf(out, "task #%d waiting_quota run_after=%s\n", id,
|
||||
t->run_after);
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------- tools */
|
||||
|
||||
static int
|
||||
arg_id(const mcp_json *a, const char *key)
|
||||
{
|
||||
const char *s = mcp_arg_string(a, key);
|
||||
|
||||
return s != NULL ? atoi(s) : mcp_arg_int(a, key, 0);
|
||||
}
|
||||
|
||||
static int
|
||||
tool_add(const char *args, struct sp_buf *out, void *ctx)
|
||||
{
|
||||
mcp_json *a;
|
||||
const char *desc;
|
||||
int rc;
|
||||
|
||||
(void)ctx;
|
||||
if ((a = mcp_parse(args != NULL ? args : "{}")) == NULL)
|
||||
return sp_buf_puts(out, "error: arguments are not JSON\n");
|
||||
desc = mcp_arg_string(a, "description");
|
||||
pthread_mutex_lock(&lock);
|
||||
load();
|
||||
rc = op_add(desc, mcp_arg_string(a, "run_after"), out);
|
||||
pthread_mutex_unlock(&lock);
|
||||
mcp_json_free(a);
|
||||
return rc;
|
||||
}
|
||||
|
||||
static int
|
||||
tool_list(const char *args, struct sp_buf *out, void *ctx)
|
||||
{
|
||||
int rc;
|
||||
|
||||
(void)args;
|
||||
(void)ctx;
|
||||
pthread_mutex_lock(&lock);
|
||||
load();
|
||||
rc = op_list(out);
|
||||
pthread_mutex_unlock(&lock);
|
||||
return rc;
|
||||
}
|
||||
|
||||
static int
|
||||
tool_update(const char *args, struct sp_buf *out, void *ctx)
|
||||
{
|
||||
mcp_json *a;
|
||||
struct task *t;
|
||||
const char *s;
|
||||
int id;
|
||||
|
||||
(void)ctx;
|
||||
if ((a = mcp_parse(args != NULL ? args : "{}")) == NULL)
|
||||
return sp_buf_puts(out, "error: arguments are not JSON\n");
|
||||
id = arg_id(a, "id");
|
||||
pthread_mutex_lock(&lock);
|
||||
load();
|
||||
if (id <= 0 || (t = find(id)) == NULL) {
|
||||
sp_buf_printf(out, id <= 0 ? "error: id required\n" :
|
||||
"error: no task #%d\n", id);
|
||||
goto out;
|
||||
}
|
||||
if ((s = mcp_arg_string(a, "status")) != NULL && s[0] != '\0') {
|
||||
if (!valid_state(s)) {
|
||||
sp_buf_printf(out, "error: status is one of todo, "
|
||||
"doing, done, blocked, waiting_quota, not '%s'\n", s);
|
||||
goto out;
|
||||
}
|
||||
if (strcmp(s, "doing") == 0)
|
||||
start(t);
|
||||
else {
|
||||
copy_str(t->status, sizeof(t->status), s);
|
||||
if (current_id == t->id)
|
||||
current_id = -1;
|
||||
}
|
||||
}
|
||||
if ((s = mcp_arg_string(a, "description")) != NULL && s[0] != '\0')
|
||||
copy_str(t->desc, sizeof(t->desc), s);
|
||||
if (mcp_arg(a, "run_after") != NULL) /* present but empty: clear it */
|
||||
when(mcp_arg_string(a, "run_after"), t->run_after,
|
||||
sizeof(t->run_after));
|
||||
if ((s = mcp_arg_string(a, "note")) != NULL && s[0] != '\0')
|
||||
copy_str(t->note, sizeof(t->note), s);
|
||||
t->updated = (long)time(NULL);
|
||||
changed();
|
||||
sp_buf_printf(out, "task #%d -> [%s] %s\n", t->id, t->status, t->desc);
|
||||
out:
|
||||
pthread_mutex_unlock(&lock);
|
||||
mcp_json_free(a);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------- command */
|
||||
|
||||
static const char *
|
||||
skip(const char *s)
|
||||
{
|
||||
while (*s == ' ')
|
||||
s++;
|
||||
return s;
|
||||
}
|
||||
|
||||
static int
|
||||
command_locked(const char *args, struct sp_buf *out)
|
||||
{
|
||||
const char *p;
|
||||
struct task *t;
|
||||
|
||||
if (args == NULL || args[0] == '\0' || strcmp(args, "ls") == 0 ||
|
||||
strcmp(args, "list") == 0)
|
||||
return op_list(out);
|
||||
if (strncmp(args, "add ", 4) == 0)
|
||||
return op_add(skip(args + 4), NULL, out);
|
||||
if (strncmp(args, "work", 4) == 0) {
|
||||
p = skip(args + 4);
|
||||
if (strcmp(p, "all") == 0) {
|
||||
if ((t = first_todo()) == NULL)
|
||||
return sp_buf_puts(out, "no todo tasks left\n");
|
||||
} else if (*p != '\0') {
|
||||
if ((t = find(atoi(p))) == NULL)
|
||||
return sp_buf_printf(out, "no task #%d\n",
|
||||
atoi(p));
|
||||
} else
|
||||
return sp_buf_puts(out, "usage: /tasks work <id>|all\n");
|
||||
start(t);
|
||||
changed();
|
||||
return sp_buf_printf(out, "working on #%d: %s\n", t->id,
|
||||
t->desc);
|
||||
}
|
||||
if (strcmp(args, "choose") == 0 || strcmp(args, "next") == 0) {
|
||||
if ((t = first_todo()) == NULL)
|
||||
return sp_buf_puts(out, "no todo tasks\n");
|
||||
start(t);
|
||||
changed();
|
||||
return sp_buf_printf(out, "chose #%d: %s\n", t->id, t->desc);
|
||||
}
|
||||
if (strncmp(args, "done", 4) == 0) {
|
||||
p = skip(args + 4);
|
||||
return op_done(*p != '\0' ? atoi(p) : current_id, out);
|
||||
}
|
||||
if (strncmp(args, "wait", 4) == 0) {
|
||||
char at[WHEN_LEN];
|
||||
int id;
|
||||
size_t n = 0;
|
||||
|
||||
p = skip(args + 4);
|
||||
id = atoi(p);
|
||||
while (*p != '\0' && *p != ' ')
|
||||
p++;
|
||||
p = skip(p);
|
||||
while (*p != '\0' && *p != ' ' && n + 1 < sizeof(at))
|
||||
at[n++] = *p++;
|
||||
at[n] = '\0';
|
||||
return op_wait(id, at, skip(p), out);
|
||||
}
|
||||
return sp_buf_puts(out, "usage: /tasks [ls|add <text>|work <id>|work all"
|
||||
"|next|done [id]|wait <id> <+secs|epoch> [note]]\n");
|
||||
}
|
||||
|
||||
static int
|
||||
command(struct sp_host *h, const char *args, struct sp_buf *out, void *ctx)
|
||||
{
|
||||
int rc;
|
||||
|
||||
(void)ctx;
|
||||
host_ref = h;
|
||||
pthread_mutex_lock(&lock);
|
||||
load();
|
||||
rc = command_locked(args, out);
|
||||
pthread_mutex_unlock(&lock);
|
||||
return rc;
|
||||
}
|
||||
|
||||
/* The clock: a parked task whose time has come is todo again. */
|
||||
static int
|
||||
tick(struct sp_host *h, const char *name, void *ctx)
|
||||
{
|
||||
long now = (long)time(NULL);
|
||||
size_t i;
|
||||
int moved = 0;
|
||||
|
||||
(void)name;
|
||||
(void)ctx;
|
||||
host_ref = h;
|
||||
pthread_mutex_lock(&lock);
|
||||
load();
|
||||
for (i = 0; i < ntasks; i++) {
|
||||
struct task *t = &tasks[i];
|
||||
long at = strtol(t->run_after, NULL, 10);
|
||||
|
||||
if (strcmp(t->status, "waiting_quota") != 0 || at <= 0 ||
|
||||
now < at)
|
||||
continue;
|
||||
copy_str(t->status, sizeof(t->status), "todo");
|
||||
copy_str(t->note, sizeof(t->note), "quota is back");
|
||||
t->run_after[0] = '\0';
|
||||
t->updated = now;
|
||||
moved = 1;
|
||||
}
|
||||
if (moved)
|
||||
save();
|
||||
widget();
|
||||
pthread_mutex_unlock(&lock);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const struct sp_tool t_add = {
|
||||
"plan_task_add",
|
||||
"Add a task to the session's plan: a sentence to do, not a process "
|
||||
"(use task_start for shell commands). Optional run_after is a unix "
|
||||
"time, or +seconds from now.",
|
||||
"{\"type\":\"object\",\"properties\":{\"description\":{\"type\":"
|
||||
"\"string\"},\"run_after\":{\"type\":\"string\"}},\"required\":"
|
||||
"[\"description\"]}",
|
||||
SP_AUTH_MUTATE, tool_add, NULL
|
||||
};
|
||||
|
||||
static const struct sp_tool t_list = {
|
||||
"plan_task_list",
|
||||
"List the session's plan with state; the one marked * is active.",
|
||||
"{\"type\":\"object\",\"properties\":{}}",
|
||||
SP_AUTH_READ, tool_list, NULL
|
||||
};
|
||||
|
||||
static const struct sp_tool t_update = {
|
||||
"plan_task_update",
|
||||
"Change a plan task. status is todo, doing, done, blocked or "
|
||||
"waiting_quota; doing makes it the session goal. An empty run_after "
|
||||
"clears the wait.",
|
||||
"{\"type\":\"object\",\"properties\":{\"id\":{\"type\":\"integer\"},"
|
||||
"\"status\":{\"type\":\"string\"},\"description\":{\"type\":\"string\"},"
|
||||
"\"run_after\":{\"type\":\"string\"},\"note\":{\"type\":\"string\"}},"
|
||||
"\"required\":[\"id\"]}",
|
||||
SP_AUTH_MUTATE, tool_update, NULL
|
||||
};
|
||||
|
||||
static const struct sp_widget plan_widget = { "plan", tick, NULL, NULL, 5000 };
|
||||
|
||||
static int
|
||||
init(struct sp_host *h)
|
||||
{
|
||||
host_ref = h;
|
||||
pthread_mutex_lock(&lock);
|
||||
load();
|
||||
widget();
|
||||
pthread_mutex_unlock(&lock);
|
||||
sp_host_add_tool(h, &t_add);
|
||||
sp_host_add_tool(h, &t_list);
|
||||
sp_host_add_tool(h, &t_update);
|
||||
sp_host_add_command(h, "tasks", command, NULL);
|
||||
sp_host_add_widget(h, &plan_widget);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const struct sp_plugin plugin = {
|
||||
SP_PLUGIN_ABI, "planner", "0.2", init, NULL, NULL
|
||||
};
|
||||
|
||||
const struct sp_plugin *
|
||||
sp_plugin_describe(void)
|
||||
{
|
||||
return &plugin;
|
||||
}
|
||||
@@ -14,10 +14,10 @@
|
||||
*
|
||||
* The tools:
|
||||
*
|
||||
* task_start command, label? -> id, starts at once
|
||||
* task_list -> id, state, seconds, bytes, label
|
||||
* task_result id -> output, exit status; fails while running
|
||||
* task_kill id -> stop it
|
||||
* process_start command, label? -> id, starts at once
|
||||
* process_list -> id, state, seconds, bytes, label
|
||||
* process_result id -> output, exit status; fails while running
|
||||
* process_kill id -> stop it
|
||||
*
|
||||
* Tasks are shell commands, so starting one is destructive in the same sense that
|
||||
* `run` is: the gate and the approval path see it like any other change.
|
||||
@@ -73,7 +73,7 @@ running_count(void)
|
||||
}
|
||||
|
||||
static void
|
||||
set_task_fields(struct sp_meter *m, void *ctx)
|
||||
set_process_fields(struct sp_meter *m, void *ctx)
|
||||
{
|
||||
const unsigned *run = ctx;
|
||||
|
||||
@@ -91,7 +91,7 @@ publish_meter(struct sp_host *h)
|
||||
{
|
||||
unsigned run = (unsigned)running_count();
|
||||
|
||||
sp_host_meter_update(h, set_task_fields, &run);
|
||||
sp_host_meter_update(h, set_process_fields, &run);
|
||||
}
|
||||
|
||||
static void
|
||||
@@ -106,35 +106,37 @@ refresh_widget(struct sp_host *h)
|
||||
struct task *t = &tasks[i];
|
||||
int n;
|
||||
|
||||
if (t->running)
|
||||
n = snprintf(text + used, sizeof(text) - used,
|
||||
"#%d %s %lds\n", t->id, t->label,
|
||||
(long)(time(NULL) - t->started));
|
||||
else
|
||||
n = snprintf(text + used, sizeof(text) - used,
|
||||
"#%d %s exit %d %.2fs\n", t->id, t->label,
|
||||
t->status, difftime(t->ended, t->started));
|
||||
/*
|
||||
* The sidebar is for what is happening now. A finished task
|
||||
* stays in process_list/process_result (its output is still there),
|
||||
* but it does not take a row of the sidebar forever.
|
||||
*/
|
||||
if (!t->running)
|
||||
continue;
|
||||
n = snprintf(text + used, sizeof(text) - used,
|
||||
"#%d %s %lds\n", t->id, t->label,
|
||||
(long)(time(NULL) - t->started));
|
||||
if (n < 0 || (size_t)n >= sizeof(text) - used)
|
||||
break;
|
||||
used += (size_t)n;
|
||||
}
|
||||
pthread_mutex_unlock(&tasks_lock);
|
||||
if (used == 0)
|
||||
(void)sp_host_widget_text(h, "tasks", "");
|
||||
(void)sp_host_widget_text(h, "processes", "");
|
||||
else
|
||||
(void)sp_host_widget_text(h, "tasks", text);
|
||||
(void)sp_host_widget_text(h, "processes", text);
|
||||
}
|
||||
|
||||
extern char **environ;
|
||||
|
||||
/*
|
||||
* The end of a task, under tasks_lock: that is the lock task_list, the widget
|
||||
* The end of a task, under tasks_lock: that is the lock process_list, the widget
|
||||
* and running_count read 'running', 'status' and 'ended' under. They used to
|
||||
* be written under the task's own lock only - two locks guarding one field
|
||||
* guard nothing.
|
||||
*/
|
||||
static void
|
||||
task_finish(struct task *t, int status)
|
||||
process_finish(struct task *t, int status)
|
||||
{
|
||||
pthread_mutex_lock(&tasks_lock);
|
||||
t->status = status;
|
||||
@@ -144,14 +146,14 @@ task_finish(struct task *t, int status)
|
||||
}
|
||||
|
||||
/*
|
||||
* Run the task in its own process group, so task_kill can stop the shell and
|
||||
* Run the task in its own process group, so process_kill can stop the shell and
|
||||
* everything it started. popen() gave no pid: t->pid stayed 0, and
|
||||
* kill(-0, SIGTERM) is "every process in my group" - task_kill killed
|
||||
* kill(-0, SIGTERM) is "every process in my group" - process_kill killed
|
||||
* splinter itself. stdin is /dev/null (a task must not read the terminal), and
|
||||
* the environment loses splinter's keys, as for the 'run' tool.
|
||||
*/
|
||||
static void *
|
||||
task_run(void *arg)
|
||||
process_run(void *arg)
|
||||
{
|
||||
struct task *t = arg;
|
||||
char chunk[2048];
|
||||
@@ -169,7 +171,7 @@ task_run(void *arg)
|
||||
pthread_mutex_lock(&t->lock);
|
||||
sp_buf_printf(&t->out, "error: %s", strerror(errno));
|
||||
pthread_mutex_unlock(&t->lock);
|
||||
task_finish(t, 127);
|
||||
process_finish(t, 127);
|
||||
return NULL;
|
||||
}
|
||||
for (i = 0, j = 0; i < n; i++)
|
||||
@@ -200,7 +202,7 @@ task_run(void *arg)
|
||||
pthread_mutex_lock(&t->lock);
|
||||
sp_buf_printf(&t->out, "error: fork: %s", strerror(errno));
|
||||
pthread_mutex_unlock(&t->lock);
|
||||
task_finish(t, 127);
|
||||
process_finish(t, 127);
|
||||
return NULL;
|
||||
}
|
||||
(void)setpgid(child, child);
|
||||
@@ -224,7 +226,7 @@ task_run(void *arg)
|
||||
pthread_mutex_lock(&tasks_lock);
|
||||
t->pid = 0; /* reaped: the id may be reused */
|
||||
pthread_mutex_unlock(&tasks_lock);
|
||||
task_finish(t, st);
|
||||
process_finish(t, st);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -242,7 +244,7 @@ find(int id)
|
||||
/*
|
||||
* The value of an argument, with the parser the rest of splinter uses. The old
|
||||
* strstr() only matched "key":"..." - the schema declares 'id' an integer, so
|
||||
* {"id":3} came out empty and every task_kill said "no such task".
|
||||
* {"id":3} came out empty and every process_kill said "no such task".
|
||||
*/
|
||||
static void
|
||||
arg_str(const char *args, const char *key, char *buf, size_t len)
|
||||
@@ -325,7 +327,7 @@ ops_end(struct sp_host *h, int id, int status, size_t tokens)
|
||||
static const struct sp_task_ops task_ops = { ops_begin, ops_end };
|
||||
|
||||
static int
|
||||
tool_task_start(const char *args, struct sp_buf *out, void *ctx)
|
||||
tool_process_start(const char *args, struct sp_buf *out, void *ctx)
|
||||
{
|
||||
char cmd[600], label[80];
|
||||
struct task *t;
|
||||
@@ -362,7 +364,7 @@ tool_task_start(const char *args, struct sp_buf *out, void *ctx)
|
||||
return sp_buf_puts(out, "error: no memory");
|
||||
}
|
||||
id = t->id;
|
||||
if (pthread_create(&t->th, NULL, task_run, t) != 0) {
|
||||
if (pthread_create(&t->th, NULL, process_run, t) != 0) {
|
||||
t->running = 0;
|
||||
ntasks--;
|
||||
pthread_mutex_unlock(&tasks_lock);
|
||||
@@ -380,7 +382,7 @@ tool_task_start(const char *args, struct sp_buf *out, void *ctx)
|
||||
}
|
||||
|
||||
static int
|
||||
tool_task_list(const char *args, struct sp_buf *out, void *ctx)
|
||||
tool_process_list(const char *args, struct sp_buf *out, void *ctx)
|
||||
{
|
||||
int i, any = 0;
|
||||
|
||||
@@ -407,7 +409,7 @@ tool_task_list(const char *args, struct sp_buf *out, void *ctx)
|
||||
}
|
||||
|
||||
static int
|
||||
tool_task_result(const char *args, struct sp_buf *out, void *ctx)
|
||||
tool_process_result(const char *args, struct sp_buf *out, void *ctx)
|
||||
{
|
||||
char num[32];
|
||||
struct task *t;
|
||||
@@ -440,7 +442,7 @@ tool_task_result(const char *args, struct sp_buf *out, void *ctx)
|
||||
}
|
||||
|
||||
static int
|
||||
tool_task_kill(const char *args, struct sp_buf *out, void *ctx)
|
||||
tool_process_kill(const char *args, struct sp_buf *out, void *ctx)
|
||||
{
|
||||
char num[32];
|
||||
struct task *t;
|
||||
@@ -475,31 +477,31 @@ tool_task_kill(const char *args, struct sp_buf *out, void *ctx)
|
||||
}
|
||||
|
||||
static const struct sp_tool t_start = {
|
||||
"task_start",
|
||||
"process_start",
|
||||
"Start a shell command in the background and return its id. Use it for "
|
||||
"builds, tests or long runs instead of blocking the conversation.",
|
||||
"{\"type\":\"object\",\"properties\":{\"command\":{\"type\":\"string\"},"
|
||||
"\"label\":{\"type\":\"string\",\"description\":\"short name\"}},"
|
||||
"\"required\":[\"command\"]}",
|
||||
SP_AUTH_DESTRUCTIVE, tool_task_start, NULL
|
||||
SP_AUTH_DESTRUCTIVE, tool_process_start, NULL
|
||||
};
|
||||
|
||||
static const struct sp_tool t_list = {
|
||||
"task_list", "List background tasks: id, state, time, size, label.",
|
||||
"{\"type\":\"object\",\"properties\":{}}", SP_AUTH_READ, tool_task_list,
|
||||
"process_list", "List background tasks: id, state, time, size, label.",
|
||||
"{\"type\":\"object\",\"properties\":{}}", SP_AUTH_READ, tool_process_list,
|
||||
NULL
|
||||
};
|
||||
|
||||
static const struct sp_tool t_result = {
|
||||
"task_result", "Output and exit status of a finished task.",
|
||||
"process_result", "Output and exit status of a finished task.",
|
||||
"{\"type\":\"object\",\"properties\":{\"id\":{\"type\":\"integer\"}},"
|
||||
"\"required\":[\"id\"]}", SP_AUTH_READ, tool_task_result, NULL
|
||||
"\"required\":[\"id\"]}", SP_AUTH_READ, tool_process_result, NULL
|
||||
};
|
||||
|
||||
static const struct sp_tool t_kill = {
|
||||
"task_kill", "Stop a running task.",
|
||||
"process_kill", "Stop a running task.",
|
||||
"{\"type\":\"object\",\"properties\":{\"id\":{\"type\":\"integer\"}},"
|
||||
"\"required\":[\"id\"]}", SP_AUTH_DESTRUCTIVE, tool_task_kill, NULL
|
||||
"\"required\":[\"id\"]}", SP_AUTH_DESTRUCTIVE, tool_process_kill, NULL
|
||||
};
|
||||
|
||||
static int
|
||||
@@ -513,7 +515,7 @@ tasks_tick(struct sp_host *h, const char *name, void *ctx)
|
||||
}
|
||||
|
||||
static const struct sp_widget widget = {
|
||||
"tasks", /* name */
|
||||
"processes", /* name */
|
||||
tasks_tick, /* tick */
|
||||
NULL, /* draw */
|
||||
NULL, /* ctx */
|
||||
@@ -537,7 +539,7 @@ tasks_init(struct sp_host *h)
|
||||
(void)sp_host_add_tool(h, &t_result);
|
||||
(void)sp_host_add_tool(h, &t_kill);
|
||||
(void)sp_host_add_widget(h, &widget);
|
||||
(void)sp_host_widget_text(h, "tasks", "");
|
||||
(void)sp_host_widget_text(h, "processes", "");
|
||||
sp_host_set_task_ops(h, &task_ops);
|
||||
publish_meter(h);
|
||||
return 0;
|
||||
@@ -1,191 +0,0 @@
|
||||
/*
|
||||
* splinter -- sysinfo: RAM, CPU, swap and disk in the sidebar.
|
||||
*
|
||||
* Copyright (c) 2026 Konrad Lother
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*
|
||||
* Four numbers the operator checks before starting a long turn: is the box
|
||||
* swapping, is the CPU already busy, is the disk full. Linux exposes all of
|
||||
* them in /proc and statvfs(2); the widget thread samples them every two
|
||||
* seconds and publishes one compact line:
|
||||
*
|
||||
* RAM 62% [||| ] CPU 13% [| ] SWAP 0% [ ] DISK 71% [|||| ]
|
||||
*
|
||||
* The sidebar wraps it at spaces. A missing /proc entry is not an error:
|
||||
* the plugin still loads and says n/a for what it cannot read.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/statvfs.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "splinter.h"
|
||||
#include "splinter_plugin.h"
|
||||
|
||||
#define BAR_WIDTH 5
|
||||
|
||||
static int have_cpu;
|
||||
static unsigned long long prev_total, prev_idle;
|
||||
|
||||
/* one [||| ] gauge, width 5 */
|
||||
static void
|
||||
gauge(char *b, size_t cap, double pct)
|
||||
{
|
||||
int i, filled;
|
||||
|
||||
if (cap < BAR_WIDTH + 3)
|
||||
return;
|
||||
if (pct < 0)
|
||||
pct = 0;
|
||||
if (pct > 100)
|
||||
pct = 100;
|
||||
filled = (int)((pct * BAR_WIDTH + 50) / 100);
|
||||
b[0] = '[';
|
||||
for (i = 0; i < BAR_WIDTH; i++)
|
||||
b[i + 1] = i < filled ? '|' : ' ';
|
||||
b[BAR_WIDTH + 1] = ']';
|
||||
b[BAR_WIDTH + 2] = '\0';
|
||||
}
|
||||
|
||||
/* /proc/stat's first line: total and idle ticks */
|
||||
static int
|
||||
cpu_read(unsigned long long *total, unsigned long long *idle)
|
||||
{
|
||||
FILE *f = fopen("/proc/stat", "r");
|
||||
char line[512];
|
||||
unsigned long long v[10];
|
||||
int n;
|
||||
|
||||
if (f == NULL)
|
||||
return -1;
|
||||
if (fgets(line, sizeof(line), f) == NULL) {
|
||||
fclose(f);
|
||||
return -1;
|
||||
}
|
||||
fclose(f);
|
||||
memset(v, 0, sizeof(v));
|
||||
n = sscanf(line, "cpu %llu %llu %llu %llu %llu %llu %llu %llu %llu",
|
||||
&v[0], &v[1], &v[2], &v[3], &v[4], &v[5], &v[6], &v[7], &v[8]);
|
||||
if (n < 4)
|
||||
return -1;
|
||||
*total = v[0] + v[1] + v[2] + v[3] + v[4] + v[5] + v[6] + v[7] +
|
||||
v[8];
|
||||
*idle = v[3] + v[4]; /* idle + iowait */
|
||||
return *total > 0 ? 0 : -1;
|
||||
}
|
||||
|
||||
/* /proc/meminfo: MemTotal, MemAvailable or Free+Buffers+Cached, Swap */
|
||||
static int
|
||||
mem_read(unsigned long long *mt, unsigned long long *ma,
|
||||
unsigned long long *st, unsigned long long *sf)
|
||||
{
|
||||
FILE *f = fopen("/proc/meminfo", "r");
|
||||
char line[256];
|
||||
unsigned long long v;
|
||||
unsigned long long free_kb = 0, buffers = 0, cached = 0;
|
||||
|
||||
*mt = *ma = *st = *sf = 0;
|
||||
if (f == NULL)
|
||||
return -1;
|
||||
while (fgets(line, sizeof(line), f) != NULL) {
|
||||
if (sscanf(line, "MemTotal: %llu", &v) == 1)
|
||||
*mt = v;
|
||||
else if (sscanf(line, "MemAvailable: %llu", &v) == 1)
|
||||
*ma = v;
|
||||
else if (sscanf(line, "MemFree: %llu", &v) == 1)
|
||||
free_kb = v;
|
||||
else if (sscanf(line, "Buffers: %llu", &v) == 1)
|
||||
buffers = v;
|
||||
else if (sscanf(line, "Cached: %llu", &v) == 1)
|
||||
cached = v;
|
||||
else if (sscanf(line, "SwapTotal: %llu", &v) == 1)
|
||||
*st = v;
|
||||
else if (sscanf(line, "SwapFree: %llu", &v) == 1)
|
||||
*sf = v;
|
||||
}
|
||||
fclose(f);
|
||||
if (*ma == 0)
|
||||
*ma = free_kb + buffers + cached;
|
||||
return *mt > 0 ? 0 : -1;
|
||||
}
|
||||
|
||||
static double
|
||||
pct(unsigned long long used, unsigned long long total)
|
||||
{
|
||||
if (total == 0)
|
||||
return 0;
|
||||
if (used > total)
|
||||
used = total;
|
||||
return (double)used * 100.0 / (double)total;
|
||||
}
|
||||
|
||||
static int
|
||||
tick(struct sp_host *h, const char *name, void *ctx)
|
||||
{
|
||||
unsigned long long mt, ma, st, sf, total, idle;
|
||||
double ram = 0, swap = 0, disk = 0, cpu = 0;
|
||||
char rb[16], cb[16], sb[16], db[16], line[512];
|
||||
|
||||
(void)ctx;
|
||||
if (mem_read(&mt, &ma, &st, &sf) == 0) {
|
||||
ram = pct(mt - ma, mt);
|
||||
if (st > 0)
|
||||
swap = pct(st - sf, st);
|
||||
}
|
||||
if (cpu_read(&total, &idle) == 0) {
|
||||
if (have_cpu && total > prev_total &&
|
||||
idle >= prev_idle && total - prev_total > 0) {
|
||||
unsigned long long dt = total - prev_total;
|
||||
unsigned long long di = idle - prev_idle;
|
||||
|
||||
cpu = pct(dt > di ? dt - di : 0, dt);
|
||||
}
|
||||
prev_total = total;
|
||||
prev_idle = idle;
|
||||
have_cpu = 1;
|
||||
}
|
||||
{
|
||||
struct statvfs vfs;
|
||||
|
||||
if (statvfs("/", &vfs) == 0 && vfs.f_blocks > 0) {
|
||||
unsigned long long used = (unsigned long long)
|
||||
(vfs.f_blocks - vfs.f_bfree);
|
||||
|
||||
disk = pct(used, (unsigned long long)vfs.f_blocks);
|
||||
}
|
||||
}
|
||||
gauge(rb, sizeof(rb), ram);
|
||||
gauge(cb, sizeof(cb), cpu);
|
||||
gauge(sb, sizeof(sb), swap);
|
||||
gauge(db, sizeof(db), disk);
|
||||
snprintf(line, sizeof(line),
|
||||
"RAM %.0f%% %s\nCPU %.0f%% %s\nSWAP %.0f%% %s\nDISK %.0f%% %s",
|
||||
ram, rb, cpu, cb, swap, sb, disk, db);
|
||||
return sp_host_widget_text(h, name, line);
|
||||
}
|
||||
|
||||
static int
|
||||
init(struct sp_host *h)
|
||||
{
|
||||
static const struct sp_widget w = { "sysinfo", tick, NULL, NULL, 2000 };
|
||||
unsigned long long total, idle;
|
||||
|
||||
(void)cpu_read(&total, &idle);
|
||||
if (total > 0) {
|
||||
prev_total = total;
|
||||
prev_idle = idle;
|
||||
have_cpu = 1;
|
||||
}
|
||||
return sp_host_add_widget(h, &w);
|
||||
}
|
||||
|
||||
static const struct sp_plugin plugin = {
|
||||
SP_PLUGIN_ABI, "sysinfo", "0.1.0", init, NULL, NULL
|
||||
};
|
||||
|
||||
const struct sp_plugin *
|
||||
sp_plugin_describe(void)
|
||||
{
|
||||
return &plugin;
|
||||
}
|
||||
@@ -1,826 +0,0 @@
|
||||
/*
|
||||
* splinter -- drive a program that wants a terminal, through tmux.
|
||||
*
|
||||
* Copyright (c) 2026 Konrad Lother
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*
|
||||
* Some programs cannot be run with `run`: a REPL, an installer that asks
|
||||
* questions, another agent's TUI. They need a terminal. tmux gives them one,
|
||||
* detached, and gives splinter two things back: what is on the pane
|
||||
* (capture-pane) and a way to type into it (send-keys).
|
||||
*
|
||||
* The tools:
|
||||
*
|
||||
* tmux_start cmd, name? start cmd in a detached session; it is the target
|
||||
* tmux_send text? keys? type text and Enter, and/or named keys (C-c, Up)
|
||||
* tmux_ask text, wait_ms? send, then wait until the pane has new output and
|
||||
* quiet_ms? has been quiet for quiet_ms; return only the new lines
|
||||
* tmux_read wait_ms? the lines that are new since the last read
|
||||
* tmux_capture lines? the pane as it is, newest lines
|
||||
* tmux_status the target, and whether it is alive
|
||||
*
|
||||
* "New" is a diff of two captures, kept in memory: there is no helper process,
|
||||
* no pid file and nothing left behind when splinter exits. A pane is a screen,
|
||||
* not a stream, so the diff finds where the old capture ends inside the new one
|
||||
* (output scrolls up) and returns what follows; a program that repaints the
|
||||
* whole screen gives the lines that changed at the bottom, or, when the old
|
||||
* capture cannot be found at all, the last SHOWN_MAX lines.
|
||||
*
|
||||
* Starting a session runs a shell command, so tmux_start is destructive, and
|
||||
* typing into a terminal is whatever that terminal does: tmux_send and
|
||||
* tmux_ask are mutating.
|
||||
*/
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/wait.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "splinter.h"
|
||||
#include "mcp.h"
|
||||
#include "splinter_plugin.h"
|
||||
|
||||
#define NAME_MAX_LEN 64
|
||||
#define HISTORY 500 /* lines of scrollback looked at */
|
||||
#define SHOWN_MAX 200 /* lines handed to the model at once */
|
||||
#define POLL_MS 200
|
||||
#define WAIT_DEFAULT 15000
|
||||
#define WAIT_CAP 120000
|
||||
#define QUIET_DEFAULT 1200
|
||||
|
||||
static struct sp_host *host_ref;
|
||||
static char target[NAME_MAX_LEN * 2];
|
||||
static char *last; /* the previous capture */
|
||||
|
||||
/* ---------------------------------------------------------------- tmux */
|
||||
|
||||
/*
|
||||
* Run argv, collect stdout and stderr. The child does nothing between fork and
|
||||
* exec but dup2 and close, which is all a forked child of a threaded process may
|
||||
* safely do.
|
||||
*/
|
||||
static int
|
||||
run(char *const argv[], struct sp_buf *out)
|
||||
{
|
||||
int fd[2], st = 0;
|
||||
char buf[4096];
|
||||
ssize_t n;
|
||||
pid_t pid;
|
||||
|
||||
if (pipe(fd) != 0)
|
||||
return -1;
|
||||
if ((pid = fork()) < 0) {
|
||||
close(fd[0]);
|
||||
close(fd[1]);
|
||||
return -1;
|
||||
}
|
||||
if (pid == 0) {
|
||||
dup2(fd[1], 1);
|
||||
dup2(fd[1], 2);
|
||||
close(fd[0]);
|
||||
close(fd[1]);
|
||||
execvp(argv[0], argv);
|
||||
_exit(127);
|
||||
}
|
||||
close(fd[1]);
|
||||
while ((n = read(fd[0], buf, sizeof(buf))) > 0 ||
|
||||
(n < 0 && errno == EINTR))
|
||||
if (n > 0 && out != NULL)
|
||||
sp_buf_add(out, buf, (size_t)n);
|
||||
close(fd[0]);
|
||||
while (waitpid(pid, &st, 0) < 0 && errno == EINTR)
|
||||
;
|
||||
return WIFEXITED(st) ? WEXITSTATUS(st) : -1;
|
||||
}
|
||||
|
||||
static int
|
||||
have_tmux(void)
|
||||
{
|
||||
char *const argv[] = { "tmux", "-V", NULL };
|
||||
|
||||
return run(argv, NULL) == 0;
|
||||
}
|
||||
|
||||
/* A name that cannot be mistaken for an option or a second target. */
|
||||
static int
|
||||
safe_name(const char *s, size_t max)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
if (s == NULL || s[0] == '\0' || s[0] == '-' || strlen(s) >= max)
|
||||
return 0;
|
||||
for (i = 0; s[i] != '\0'; i++)
|
||||
if (!isalnum((unsigned char)s[i]) && strchr("_.-:%@", s[i]) == NULL)
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
alive(void)
|
||||
{
|
||||
char *const argv[] = { "tmux", "has-session", "-t", target, NULL };
|
||||
|
||||
return target[0] != '\0' && run(argv, NULL) == 0;
|
||||
}
|
||||
|
||||
static void
|
||||
target_file(char *path, size_t len)
|
||||
{
|
||||
const char *dir = sp_host_session_dir(host_ref);
|
||||
|
||||
if (dir != NULL && dir[0] != '\0')
|
||||
snprintf(path, len, "%s/tmux_target", dir);
|
||||
else
|
||||
path[0] = '\0';
|
||||
}
|
||||
|
||||
static void
|
||||
set_target(const char *name)
|
||||
{
|
||||
char path[600];
|
||||
FILE *f;
|
||||
|
||||
snprintf(target, sizeof(target), "%s", name);
|
||||
free(last);
|
||||
last = NULL;
|
||||
target_file(path, sizeof(path));
|
||||
if (path[0] != '\0' && (f = fopen(path, "w")) != NULL) {
|
||||
fprintf(f, "%s\n", target);
|
||||
fclose(f);
|
||||
}
|
||||
}
|
||||
|
||||
/* A resumed session finds its terminal again. */
|
||||
static void
|
||||
restore_target(void)
|
||||
{
|
||||
char path[600];
|
||||
FILE *f;
|
||||
|
||||
target_file(path, sizeof(path));
|
||||
if (path[0] == '\0' || (f = fopen(path, "r")) == NULL)
|
||||
return;
|
||||
if (fgets(target, sizeof(target), f) != NULL)
|
||||
target[strcspn(target, "\n")] = '\0';
|
||||
fclose(f);
|
||||
if (!safe_name(target, sizeof(target)))
|
||||
target[0] = '\0';
|
||||
}
|
||||
|
||||
static long
|
||||
now_ms(void)
|
||||
{
|
||||
struct timespec ts;
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||
return (long)ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
|
||||
}
|
||||
|
||||
static void
|
||||
pause_ms(long ms)
|
||||
{
|
||||
struct timespec ts;
|
||||
|
||||
ts.tv_sec = ms / 1000;
|
||||
ts.tv_nsec = (ms % 1000) * 1000000L;
|
||||
nanosleep(&ts, NULL);
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------- capture and diff */
|
||||
|
||||
/*
|
||||
* The pane with its history, joined lines, without the empty rows tmux pads
|
||||
* a screen with. NULL when there is no pane.
|
||||
*/
|
||||
static char *
|
||||
capture(int lines)
|
||||
{
|
||||
struct sp_buf b;
|
||||
char from[32];
|
||||
char *const argv[] = { "tmux", "capture-pane", "-p", "-J", "-t",
|
||||
target, "-S", from, NULL };
|
||||
char *s;
|
||||
size_t n;
|
||||
|
||||
snprintf(from, sizeof(from), "-%d", lines);
|
||||
if (sp_buf_init(&b) != 0)
|
||||
return NULL;
|
||||
if (run(argv, &b) != 0) {
|
||||
sp_buf_free(&b);
|
||||
return NULL;
|
||||
}
|
||||
n = b.len;
|
||||
while (n > 0 && (b.p[n - 1] == '\n' || b.p[n - 1] == ' '))
|
||||
n--;
|
||||
s = malloc(n + 1);
|
||||
if (s != NULL) {
|
||||
memcpy(s, b.p, n);
|
||||
s[n] = '\0';
|
||||
}
|
||||
sp_buf_free(&b);
|
||||
return s;
|
||||
}
|
||||
|
||||
/* Split in place; v gets the start of every line. */
|
||||
static size_t
|
||||
split(char *text, char ***v)
|
||||
{
|
||||
size_t n = 0, cap = 0;
|
||||
char *p = text;
|
||||
|
||||
*v = NULL;
|
||||
for (;;) {
|
||||
char *nl;
|
||||
|
||||
if (n == cap) {
|
||||
char **nv = realloc(*v, (cap ? cap * 2 : 64) * sizeof(**v));
|
||||
|
||||
if (nv == NULL) {
|
||||
free(*v);
|
||||
*v = NULL;
|
||||
return 0;
|
||||
}
|
||||
*v = nv;
|
||||
cap = cap ? cap * 2 : 64;
|
||||
}
|
||||
(*v)[n++] = p;
|
||||
if ((nl = strchr(p, '\n')) == NULL)
|
||||
break;
|
||||
*nl = '\0';
|
||||
p = nl + 1;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
/* Does b[0..k) equal a[from..from+k)? */
|
||||
static int
|
||||
same_run(char **a, size_t from, char **b, size_t k)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < k; i++)
|
||||
if (strcmp(a[from + i], b[i]) != 0)
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Where in `neu` do the lines that are new begin? The old capture is found as a
|
||||
* run at the start of the new one (nothing scrolled), or as a tail of the old one
|
||||
* at the start of the new one (output pushed the top off). The old last line is
|
||||
* tried last and dropped, because it is the prompt or the line being edited and is
|
||||
* the one most likely to differ. Returns the index of the first new line, or 0
|
||||
* when nothing of the old capture can be found.
|
||||
*/
|
||||
static size_t
|
||||
first_new(char **old, size_t no, char **neu, size_t nn)
|
||||
{
|
||||
size_t trim, s;
|
||||
|
||||
for (trim = 0; trim <= 1 && trim < no; trim++) {
|
||||
size_t have = no - trim;
|
||||
|
||||
for (s = 0; s < have; s++) {
|
||||
size_t k = have - s;
|
||||
|
||||
if (k <= nn && same_run(old, s, neu, k))
|
||||
return k;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* The lines of `now` that `before` did not have. */
|
||||
static void
|
||||
delta(const char *before, const char *now, struct sp_buf *out)
|
||||
{
|
||||
char *a = NULL, *b = NULL, **av = NULL, **bv = NULL;
|
||||
size_t na = 0, nb, from, i;
|
||||
|
||||
if ((b = strdup(now)) == NULL)
|
||||
return;
|
||||
nb = split(b, &bv);
|
||||
if (before != NULL && before[0] != '\0') {
|
||||
a = strdup(before);
|
||||
if (a != NULL)
|
||||
na = split(a, &av);
|
||||
}
|
||||
from = (na > 0 && bv != NULL) ? first_new(av, na, bv, nb) : 0;
|
||||
if (bv != NULL && nb - from > SHOWN_MAX) {
|
||||
sp_buf_printf(out, "[%zu earlier lines omitted]\n",
|
||||
nb - from - SHOWN_MAX);
|
||||
from = nb - SHOWN_MAX;
|
||||
}
|
||||
for (i = from; bv != NULL && i < nb; i++)
|
||||
sp_buf_printf(out, "%s\n", bv[i]);
|
||||
free(av);
|
||||
free(bv);
|
||||
free(a);
|
||||
free(b);
|
||||
}
|
||||
|
||||
/* Capture, report what is new since the last capture, remember this one. */
|
||||
static int
|
||||
report_new(struct sp_buf *out)
|
||||
{
|
||||
char *now = capture(HISTORY);
|
||||
struct sp_buf d;
|
||||
|
||||
if (now == NULL)
|
||||
return sp_buf_puts(out, "error: the tmux pane is gone\n");
|
||||
if (sp_buf_init(&d) != 0) {
|
||||
free(now);
|
||||
return -1;
|
||||
}
|
||||
delta(last, now, &d);
|
||||
free(last);
|
||||
last = now;
|
||||
if (d.len == 0)
|
||||
sp_buf_puts(out, "(no new output)\n");
|
||||
else
|
||||
sp_buf_add(out, d.p, d.len);
|
||||
sp_buf_free(&d);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Wait until the pane differs from `base`, then until it has stayed the same for
|
||||
* quiet_ms, or until wait_ms is over. Returns 1 when something changed.
|
||||
*/
|
||||
static int
|
||||
settle(const char *base, long wait_ms, long quiet_ms)
|
||||
{
|
||||
long start = now_ms(), changed_at = 0;
|
||||
char *prev = base != NULL ? strdup(base) : strdup("");
|
||||
int changed = 0;
|
||||
|
||||
while (prev != NULL && now_ms() - start < wait_ms) {
|
||||
char *cur;
|
||||
|
||||
pause_ms(POLL_MS);
|
||||
if ((cur = capture(HISTORY)) == NULL)
|
||||
break;
|
||||
if (strcmp(cur, prev) != 0) {
|
||||
changed = 1;
|
||||
changed_at = now_ms();
|
||||
free(prev);
|
||||
prev = cur;
|
||||
continue;
|
||||
}
|
||||
free(cur);
|
||||
if (changed && now_ms() - changed_at >= quiet_ms)
|
||||
break;
|
||||
}
|
||||
free(prev);
|
||||
return changed;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ operations */
|
||||
|
||||
static int
|
||||
need_target(struct sp_buf *out)
|
||||
{
|
||||
if (target[0] == '\0')
|
||||
return !sp_buf_puts(out, "error: no tmux target; use tmux_start "
|
||||
"or /tmux attach\n");
|
||||
if (!alive())
|
||||
return !sp_buf_printf(out, "error: tmux session '%s' is gone\n",
|
||||
target);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
op_start(const char *cmd, const char *name, struct sp_buf *out)
|
||||
{
|
||||
char auto_name[NAME_MAX_LEN];
|
||||
char *const argv[] = { "tmux", "new-session", "-d", "-x", "200", "-y",
|
||||
"50", "-s", auto_name, "sh", "-c", (char *)cmd, NULL };
|
||||
struct sp_buf o;
|
||||
int rc;
|
||||
|
||||
if (cmd == NULL || cmd[0] == '\0')
|
||||
return sp_buf_puts(out, "error: cmd required\n");
|
||||
if (name == NULL || name[0] == '\0')
|
||||
snprintf(auto_name, sizeof(auto_name), "splinter-%d",
|
||||
(int)getpid());
|
||||
else if (!safe_name(name, sizeof(auto_name)) || strchr(name, ':') != NULL)
|
||||
return sp_buf_puts(out, "error: name may use letters, digits "
|
||||
"and _ . - only\n");
|
||||
else
|
||||
snprintf(auto_name, sizeof(auto_name), "%s", name);
|
||||
if (sp_buf_init(&o) != 0)
|
||||
return -1;
|
||||
snprintf(target, sizeof(target), "%s", auto_name);
|
||||
if (alive()) {
|
||||
set_target(auto_name);
|
||||
sp_buf_free(&o);
|
||||
return sp_buf_printf(out, "session %s exists, attached\n",
|
||||
auto_name);
|
||||
}
|
||||
rc = run(argv, &o);
|
||||
if (rc != 0) {
|
||||
target[0] = '\0';
|
||||
sp_buf_printf(out, "error: tmux new-session failed (%d): %s",
|
||||
rc, o.p != NULL ? o.p : "\n");
|
||||
sp_buf_free(&o);
|
||||
return 0;
|
||||
}
|
||||
sp_buf_free(&o);
|
||||
set_target(auto_name);
|
||||
free(last);
|
||||
last = capture(HISTORY);
|
||||
return sp_buf_printf(out, "target is %s (cmd: %s)\n", target, cmd);
|
||||
}
|
||||
|
||||
/* Named keys only: letters, digits and - + _, so none can be an option. */
|
||||
static int
|
||||
safe_keys(const char *keys)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
for (i = 0; keys[i] != '\0'; i++)
|
||||
if (!isalnum((unsigned char)keys[i]) && keys[i] != ' ' &&
|
||||
keys[i] != '-' && keys[i] != '_' && keys[i] != '+')
|
||||
return 0;
|
||||
return keys[0] != '-';
|
||||
}
|
||||
|
||||
static int
|
||||
send_text(const char *text, int enter, struct sp_buf *out)
|
||||
{
|
||||
char *const lit[] = { "tmux", "send-keys", "-t", target, "-l", "--",
|
||||
(char *)text, NULL };
|
||||
char *const ret[] = { "tmux", "send-keys", "-t", target, "Enter", NULL };
|
||||
|
||||
if (text[0] != '\0' && run(lit, out) != 0)
|
||||
return -1;
|
||||
return enter ? run(ret, out) : 0;
|
||||
}
|
||||
|
||||
static int
|
||||
send_keys(const char *keys, struct sp_buf *out)
|
||||
{
|
||||
char *copy = strdup(keys), *argv[40], *save = NULL, *tok;
|
||||
int n = 0, rc;
|
||||
|
||||
if (copy == NULL)
|
||||
return -1;
|
||||
argv[n++] = "tmux";
|
||||
argv[n++] = "send-keys";
|
||||
argv[n++] = "-t";
|
||||
argv[n++] = target;
|
||||
for (tok = strtok_r(copy, " ", &save); tok != NULL && n < 38;
|
||||
tok = strtok_r(NULL, " ", &save))
|
||||
argv[n++] = tok;
|
||||
argv[n] = NULL;
|
||||
rc = n > 4 ? run(argv, out) : 0;
|
||||
free(copy);
|
||||
return rc;
|
||||
}
|
||||
|
||||
static int
|
||||
op_send(const char *text, const char *keys, int enter, struct sp_buf *out)
|
||||
{
|
||||
if (!need_target(out))
|
||||
return 0;
|
||||
if ((text == NULL || text[0] == '\0') && (keys == NULL || keys[0] == '\0'))
|
||||
return sp_buf_puts(out, "error: text or keys required\n");
|
||||
if (keys != NULL && !safe_keys(keys))
|
||||
return sp_buf_puts(out, "error: keys are names such as C-c, "
|
||||
"Up, Escape\n");
|
||||
if (text != NULL && text[0] != '\0' &&
|
||||
send_text(text, keys == NULL || keys[0] == '\0' ? enter : 0, out) != 0)
|
||||
return sp_buf_puts(out, "error: tmux send-keys failed\n");
|
||||
if (keys != NULL && keys[0] != '\0' && send_keys(keys, out) != 0)
|
||||
return sp_buf_puts(out, "error: tmux send-keys failed\n");
|
||||
return sp_buf_puts(out, "sent\n");
|
||||
}
|
||||
|
||||
static int
|
||||
op_ask(const char *text, long wait_ms, long quiet_ms, struct sp_buf *out)
|
||||
{
|
||||
char *before;
|
||||
|
||||
if (!need_target(out))
|
||||
return 0;
|
||||
if (text == NULL || text[0] == '\0')
|
||||
return sp_buf_puts(out, "error: text required\n");
|
||||
if ((before = capture(HISTORY)) == NULL)
|
||||
return sp_buf_puts(out, "error: the tmux pane is gone\n");
|
||||
free(last);
|
||||
last = before;
|
||||
if (send_text(text, 1, out) != 0)
|
||||
return sp_buf_puts(out, "error: tmux send-keys failed\n");
|
||||
if (!settle(before, wait_ms, quiet_ms))
|
||||
sp_buf_puts(out, "(timeout: the pane did not change; it may be "
|
||||
"busy or waiting; try tmux_read or tmux_capture)\n");
|
||||
return report_new(out);
|
||||
}
|
||||
|
||||
static int
|
||||
op_read(long wait_ms, long quiet_ms, struct sp_buf *out)
|
||||
{
|
||||
if (!need_target(out))
|
||||
return 0;
|
||||
if (wait_ms > 0 && last != NULL)
|
||||
(void)settle(last, wait_ms, quiet_ms);
|
||||
return report_new(out);
|
||||
}
|
||||
|
||||
static int
|
||||
op_capture(int lines, struct sp_buf *out)
|
||||
{
|
||||
char *s;
|
||||
|
||||
if (!need_target(out))
|
||||
return 0;
|
||||
if ((s = capture(lines)) == NULL)
|
||||
return sp_buf_puts(out, "error: the tmux pane is gone\n");
|
||||
sp_buf_printf(out, "[pane %s, last %d lines]\n%s\n", target, lines, s);
|
||||
free(s);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
op_status(struct sp_buf *out)
|
||||
{
|
||||
return sp_buf_printf(out, "tmux target: %s%s\n",
|
||||
target[0] != '\0' ? target : "(none)",
|
||||
target[0] == '\0' ? "" : alive() ? " (alive)" : " (gone)");
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------- tools */
|
||||
|
||||
static long
|
||||
clamp(long v, long lo, long hi, long dflt)
|
||||
{
|
||||
if (v == -1)
|
||||
return dflt;
|
||||
return v < lo ? lo : v > hi ? hi : v;
|
||||
}
|
||||
|
||||
static mcp_json *
|
||||
parse(const char *args)
|
||||
{
|
||||
return mcp_parse(args != NULL ? args : "{}");
|
||||
}
|
||||
|
||||
#define WITH_ARGS(a, out) \
|
||||
if (((a) = parse(args)) == NULL) \
|
||||
return sp_buf_puts((out), "error: arguments are not JSON\n")
|
||||
|
||||
static int
|
||||
tool_start(const char *args, struct sp_buf *out, void *ctx)
|
||||
{
|
||||
mcp_json *a;
|
||||
int rc;
|
||||
|
||||
(void)ctx;
|
||||
WITH_ARGS(a, out);
|
||||
rc = op_start(mcp_arg_string(a, "cmd"), mcp_arg_string(a, "name"), out);
|
||||
mcp_json_free(a);
|
||||
return rc;
|
||||
}
|
||||
|
||||
static int
|
||||
tool_send(const char *args, struct sp_buf *out, void *ctx)
|
||||
{
|
||||
mcp_json *a;
|
||||
int rc;
|
||||
|
||||
(void)ctx;
|
||||
WITH_ARGS(a, out);
|
||||
rc = op_send(mcp_arg_string(a, "text"), mcp_arg_string(a, "keys"),
|
||||
mcp_arg_bool(a, "enter", 1), out);
|
||||
mcp_json_free(a);
|
||||
return rc;
|
||||
}
|
||||
|
||||
static int
|
||||
tool_ask(const char *args, struct sp_buf *out, void *ctx)
|
||||
{
|
||||
mcp_json *a;
|
||||
int rc;
|
||||
|
||||
(void)ctx;
|
||||
WITH_ARGS(a, out);
|
||||
rc = op_ask(mcp_arg_string(a, "text"),
|
||||
clamp(mcp_arg_int(a, "wait_ms", -1), 1000, WAIT_CAP, WAIT_DEFAULT),
|
||||
clamp(mcp_arg_int(a, "quiet_ms", -1), 200, 30000, QUIET_DEFAULT),
|
||||
out);
|
||||
mcp_json_free(a);
|
||||
return rc;
|
||||
}
|
||||
|
||||
static int
|
||||
tool_read(const char *args, struct sp_buf *out, void *ctx)
|
||||
{
|
||||
mcp_json *a;
|
||||
int rc;
|
||||
|
||||
(void)ctx;
|
||||
WITH_ARGS(a, out);
|
||||
rc = op_read(clamp(mcp_arg_int(a, "wait_ms", -1), 0, WAIT_CAP, 0),
|
||||
QUIET_DEFAULT, out);
|
||||
mcp_json_free(a);
|
||||
return rc;
|
||||
}
|
||||
|
||||
static int
|
||||
tool_capture(const char *args, struct sp_buf *out, void *ctx)
|
||||
{
|
||||
mcp_json *a;
|
||||
int rc;
|
||||
|
||||
(void)ctx;
|
||||
WITH_ARGS(a, out);
|
||||
rc = op_capture((int)clamp(mcp_arg_int(a, "lines", -1), 1, 5000, 200),
|
||||
out);
|
||||
mcp_json_free(a);
|
||||
return rc;
|
||||
}
|
||||
|
||||
static int
|
||||
tool_status(const char *args, struct sp_buf *out, void *ctx)
|
||||
{
|
||||
(void)args;
|
||||
(void)ctx;
|
||||
return op_status(out);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------- command */
|
||||
|
||||
static const char *
|
||||
skip(const char *s)
|
||||
{
|
||||
while (*s == ' ')
|
||||
s++;
|
||||
return s;
|
||||
}
|
||||
|
||||
static int
|
||||
word(const char *args, const char *name, const char **rest)
|
||||
{
|
||||
size_t n = strlen(name);
|
||||
|
||||
if (strncmp(args, name, n) != 0 || (args[n] != '\0' && args[n] != ' '))
|
||||
return 0;
|
||||
*rest = skip(args + n);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int
|
||||
command(struct sp_host *h, const char *args, struct sp_buf *out, void *ctx)
|
||||
{
|
||||
const char *r;
|
||||
|
||||
(void)ctx;
|
||||
host_ref = h;
|
||||
if (args == NULL || args[0] == '\0' || word(args, "status", &r))
|
||||
return op_status(out);
|
||||
if (word(args, "start", &r))
|
||||
return op_start(r, NULL, out);
|
||||
if (word(args, "attach", &r)) {
|
||||
if (!safe_name(r, sizeof(target)))
|
||||
return sp_buf_puts(out, "error: not a tmux target name\n");
|
||||
set_target(r);
|
||||
return op_status(out);
|
||||
}
|
||||
if (word(args, "send", &r))
|
||||
return op_send(r, NULL, 1, out);
|
||||
if (word(args, "keys", &r))
|
||||
return op_send(NULL, r, 0, out);
|
||||
if (word(args, "ask", &r))
|
||||
return op_ask(r, WAIT_DEFAULT, QUIET_DEFAULT, out);
|
||||
if (word(args, "read", &r))
|
||||
return op_read(clamp(*r != '\0' ? atol(r) : -1, 0, WAIT_CAP, 0),
|
||||
QUIET_DEFAULT, out);
|
||||
if (word(args, "capture", &r))
|
||||
return op_capture((int)clamp(*r != '\0' ? atol(r) : -1, 1, 5000,
|
||||
200), out);
|
||||
if (word(args, "stop", &r)) {
|
||||
char *const argv[] = { "tmux", "kill-session", "-t", target, NULL };
|
||||
|
||||
if (target[0] == '\0')
|
||||
return sp_buf_puts(out, "no target\n");
|
||||
(void)run(argv, NULL);
|
||||
sp_buf_printf(out, "killed %s\n", target);
|
||||
target[0] = '\0';
|
||||
free(last);
|
||||
last = NULL;
|
||||
return 0;
|
||||
}
|
||||
return sp_buf_puts(out, "usage: /tmux start <cmd> | attach <target> | "
|
||||
"send <text> | keys <names> | ask <text> | read [wait_ms] | "
|
||||
"capture [lines] | status | stop\n");
|
||||
}
|
||||
|
||||
static int
|
||||
tick(struct sp_host *h, const char *name, void *ctx)
|
||||
{
|
||||
char b[160];
|
||||
|
||||
(void)name;
|
||||
(void)ctx;
|
||||
host_ref = h;
|
||||
snprintf(b, sizeof(b), "%.100s", target);
|
||||
sp_host_widget_text(h, "tmux", b);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const struct sp_tool t_start = {
|
||||
"tmux_start",
|
||||
"Start a shell command in a detached tmux terminal and make it the "
|
||||
"target. For programs that need a terminal: REPLs, installers, TUIs. "
|
||||
"Use run for anything that just prints and exits.",
|
||||
"{\"type\":\"object\",\"properties\":{\"cmd\":{\"type\":\"string\"},"
|
||||
"\"name\":{\"type\":\"string\",\"description\":\"session name, "
|
||||
"letters digits _ . -\"}},\"required\":[\"cmd\"]}",
|
||||
SP_AUTH_DESTRUCTIVE, tool_start, NULL
|
||||
};
|
||||
|
||||
static const struct sp_tool t_send = {
|
||||
"tmux_send",
|
||||
"Type into the target terminal. text is typed literally and followed by "
|
||||
"Enter (enter=false to leave it off); keys are tmux key names such as "
|
||||
"C-c, Escape, Up, separated by spaces.",
|
||||
"{\"type\":\"object\",\"properties\":{\"text\":{\"type\":\"string\"},"
|
||||
"\"keys\":{\"type\":\"string\"},\"enter\":{\"type\":\"boolean\"}}}",
|
||||
SP_AUTH_MUTATE, tool_send, NULL
|
||||
};
|
||||
|
||||
static const struct sp_tool t_ask = {
|
||||
"tmux_ask",
|
||||
"Type a line into the target terminal, wait until it has new output and "
|
||||
"has been quiet for quiet_ms, and return the new lines. Gives up after "
|
||||
"wait_ms (default 15000) and says so.",
|
||||
"{\"type\":\"object\",\"properties\":{\"text\":{\"type\":\"string\"},"
|
||||
"\"wait_ms\":{\"type\":\"integer\"},\"quiet_ms\":{\"type\":\"integer\"}},"
|
||||
"\"required\":[\"text\"]}",
|
||||
SP_AUTH_MUTATE, tool_ask, NULL
|
||||
};
|
||||
|
||||
static const struct sp_tool t_read = {
|
||||
"tmux_read",
|
||||
"The lines on the target terminal that are new since the last "
|
||||
"tmux_ask or tmux_read. wait_ms waits for something to appear first.",
|
||||
"{\"type\":\"object\",\"properties\":{\"wait_ms\":{\"type\":\"integer\"}}}",
|
||||
SP_AUTH_READ, tool_read, NULL
|
||||
};
|
||||
|
||||
static const struct sp_tool t_capture = {
|
||||
"tmux_capture",
|
||||
"The target terminal as it is now: the last `lines` lines (default 200).",
|
||||
"{\"type\":\"object\",\"properties\":{\"lines\":{\"type\":\"integer\"}}}",
|
||||
SP_AUTH_READ, tool_capture, NULL
|
||||
};
|
||||
|
||||
static const struct sp_tool t_status = {
|
||||
"tmux_status",
|
||||
"The current tmux target and whether its session is alive.",
|
||||
"{\"type\":\"object\",\"properties\":{}}",
|
||||
SP_AUTH_READ, tool_status, NULL
|
||||
};
|
||||
|
||||
static const struct sp_widget tmux_widget = { "tmux", tick, NULL, NULL, 5000 };
|
||||
|
||||
static int
|
||||
init(struct sp_host *h)
|
||||
{
|
||||
if (!have_tmux())
|
||||
return -1; /* no tmux: the plugin does not load */
|
||||
host_ref = h;
|
||||
restore_target();
|
||||
sp_host_add_tool(h, &t_start);
|
||||
sp_host_add_tool(h, &t_send);
|
||||
sp_host_add_tool(h, &t_ask);
|
||||
sp_host_add_tool(h, &t_read);
|
||||
sp_host_add_tool(h, &t_capture);
|
||||
sp_host_add_tool(h, &t_status);
|
||||
sp_host_add_command(h, "tmux", command, NULL);
|
||||
sp_host_add_widget(h, &tmux_widget);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
shutdown_plugin(struct sp_host *h)
|
||||
{
|
||||
(void)h;
|
||||
free(last);
|
||||
last = NULL;
|
||||
}
|
||||
|
||||
static const struct sp_plugin plugin = {
|
||||
SP_PLUGIN_ABI, "tmux", "0.3", init, NULL, shutdown_plugin
|
||||
};
|
||||
|
||||
const struct sp_plugin *
|
||||
sp_plugin_describe(void)
|
||||
{
|
||||
return &plugin;
|
||||
}
|
||||
Reference in new issue
Block a user