delegates and cli_env: hardcoded environment per cli program; swarm, provider, planner, scheduler, tmux
env=NAME=VALUE on a delegate line (repeatable) is set in the child right before exec and beats what the parent exported, so two delegates can run the same cli with two accounts. Remotely it goes in front as `env 'NAME=VALUE'`, quoted like the rest. cli_env = NAME=VALUE does the same for the main cli transport. A bad name is an error with the line. Also in this commit, the work in progress from the tree: swarm and provider state, the provider/scheduler/planner/tmux plugins, the ui test, whose tmux session name is now per run so parallel runs do not collide. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
25a616a87f
commit
75d8beeaa4
44 files changed
+5721
-1674
No files matched your search
@@ -0,0 +1,377 @@
|
||||
/*
|
||||
* tmux proxy plugin: drives a CLI tool in an underlying tmux session.
|
||||
* - init checks `tmux` is installed; refuses to load if not.
|
||||
* - background watcher process detects new pane output and extracts/appends deltas
|
||||
* to <session>/tmux_watch.log (no fixed sleep in ask; ask waits on the feed).
|
||||
* Commands: /tmux start <cmd>|attach <t>|send <text>|keys <k>|capture [n]|ask <text>|read [wait_ms]|watch start|stop|status|stop
|
||||
* Tools: tmux_start, tmux_send, tmux_capture, tmux_read, tmux_ask, tmux_status, tmux_watch
|
||||
*/
|
||||
#define _DEFAULT_SOURCE
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <signal.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "splinter.h"
|
||||
#include "splinter_plugin.h"
|
||||
|
||||
static struct sp_host *host_ref;
|
||||
static char target[128];
|
||||
static char sess_dir[512];
|
||||
|
||||
static int have_tmux(void){
|
||||
char *a[]={"tmux","-V",NULL}; struct sp_buf o; int rc;
|
||||
if(sp_buf_init(&o)!=0) return 0;
|
||||
{ int p[2]; pid_t pid; int st=0; char b[64]; ssize_t n;
|
||||
if(pipe(p)!=0){ sp_buf_free(&o); return 0; }
|
||||
pid=fork();
|
||||
if(pid==0){ dup2(p[1],1); dup2(p[1],2); close(p[0]); close(p[1]); execvp("tmux",a); _exit(127); }
|
||||
close(p[1]); while((n=read(p[0],b,sizeof(b)))>0) sp_buf_add(&o,b,(size_t)n); close(p[0]); waitpid(pid,&st,0);
|
||||
rc = (WIFEXITED(st) && WEXITSTATUS(st)==0);
|
||||
}
|
||||
sp_buf_free(&o); return rc;
|
||||
}
|
||||
|
||||
static void ensure_sess(void){ const char *d=sp_host_session_dir(host_ref); if(d&&*d) snprintf(sess_dir,sizeof(sess_dir),"%s",d); }
|
||||
static void spath(const char *name, char *out, size_t cap){ ensure_sess(); if(sess_dir[0]) snprintf(out,cap,"%s/%s",sess_dir,name); else snprintf(out,cap,"/tmp/splinter-%s",name); }
|
||||
static void state_path(char *out,size_t cap){ spath("tmux_target",out,cap); }
|
||||
static void feed_path(char *out,size_t cap){ spath("tmux_watch.log",out,cap); }
|
||||
static void wpid_path(char *out,size_t cap){ spath("tmux_watch.pid",out,cap); }
|
||||
|
||||
static void save_target(void){ char p[600]; FILE *f; state_path(p,sizeof(p)); f=fopen(p,"w"); if(f){ fprintf(f,"%s\n",target); fclose(f);} }
|
||||
static void load_target(void){ char p[600]; FILE *f; target[0]='\0'; state_path(p,sizeof(p)); f=fopen(p,"r"); if(f){ if(fgets(target,sizeof(target),f)){ size_t n=strlen(target); while(n&&(target[n-1]=='\n'||target[n-1]=='\r')) target[--n]='\0'; } fclose(f);} }
|
||||
|
||||
static int run_argv(char *const argv[], struct sp_buf *out){
|
||||
int pfd[2]; pid_t pid; char buf[4096]; ssize_t n; int st=0;
|
||||
if(pipe(pfd)!=0) return -1;
|
||||
pid=fork();
|
||||
if(pid<0){ close(pfd[0]); close(pfd[1]); return -1; }
|
||||
if(pid==0){ dup2(pfd[1],1); dup2(pfd[1],2); close(pfd[0]); close(pfd[1]); execvp(argv[0],argv); _exit(127); }
|
||||
close(pfd[1]); while((n=read(pfd[0],buf,sizeof(buf)))>0) sp_buf_add(out,buf,(size_t)n); close(pfd[0]);
|
||||
waitpid(pid,&st,0); return WIFEXITED(st)?WEXITSTATUS(st):-1;
|
||||
}
|
||||
|
||||
static int tmux_capture_n(const char *tgt, int lines, struct sp_buf *out){
|
||||
char sarg[32]; snprintf(sarg,sizeof(sarg),"-%d", lines>0?lines:300);
|
||||
{ char *a[]={"tmux","capture-pane","-t",(char*)tgt,"-p","-S",sarg,NULL}; return run_argv(a,out); }
|
||||
}
|
||||
static int tmux_has_session(const char *name){
|
||||
char nm[128],*dot; char *a[]={"tmux","has-session","-t",nm,NULL}; struct sp_buf o; int rc;
|
||||
snprintf(nm,sizeof(nm),"%.120s",name); dot=strchr(nm,':'); if(dot)*dot='\0';
|
||||
if(sp_buf_init(&o)!=0) return 0;
|
||||
rc=run_argv(a,&o); sp_buf_free(&o); return rc==0;
|
||||
}
|
||||
static int tmux_send_lit(const char *tgt, const char *text, struct sp_buf *out){
|
||||
char *a1[]={"tmux","send-keys","-t",(char*)tgt,"-l","--",(char*)text,NULL};
|
||||
char *a2[]={"tmux","send-keys","-t",(char*)tgt,"Enter",NULL};
|
||||
int rc=run_argv(a1,out); if(rc==0) rc=run_argv(a2,out); return rc;
|
||||
}
|
||||
|
||||
/* delta: bytes in neu after the longest common prefix with old (simple, robust enough) */
|
||||
static void extract_delta(const char *old, const char *neu, struct sp_buf *out){
|
||||
size_t i=0; size_t ol=old?strlen(old):0, nl=neu?strlen(neu):0;
|
||||
while(i<ol && i<nl && old[i]==neu[i]) i++;
|
||||
if(i<nl) sp_buf_add(out, neu+i, nl-i);
|
||||
}
|
||||
|
||||
static long file_size(const char *p){ struct stat st; if(stat(p,&st)!=0) return 0; return (long)st.st_size; }
|
||||
|
||||
static pid_t watcher_pid(void){
|
||||
char p[600], buf[64]; FILE *f; wpid_path(p,sizeof(p));
|
||||
f=fopen(p,"r"); if(!f) return -1;
|
||||
if(!fgets(buf,sizeof(buf),f)){ fclose(f); return -1; }
|
||||
fclose(f);
|
||||
{ pid_t pid=(pid_t)atoi(buf); if(pid>0 && kill(pid,0)==0) return pid; }
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* watcher child: polls capture-pane, detects new output, appends delta to feed */
|
||||
static void watcher_loop(const char *tgt, const char *sd){
|
||||
char prev[65536]; size_t prevn=0; char fp[600], sp[600], pp[600];
|
||||
prev[0]='\0';
|
||||
snprintf(fp,sizeof(fp),"%s/tmux_watch.log",sd);
|
||||
snprintf(pp,sizeof(pp),"%s/tmux_watch.pid",sd);
|
||||
snprintf(sp,sizeof(sp),"%s/tmux_watch.state",sd);
|
||||
{ FILE *f=fopen(pp,"w"); if(f){ fprintf(f,"%d\n",(int)getpid()); fclose(f);} }
|
||||
/* seed prev from current pane so we don't dump history */
|
||||
{
|
||||
struct sp_buf o; if(sp_buf_init(&o)==0){ tmux_capture_n(tgt,300,&o); if(o.p){ size_t n=o.len; if(n>=sizeof(prev)) n=sizeof(prev)-1; memcpy(prev,o.p,n); prev[n]='\0'; prevn=n; } sp_buf_free(&o); }
|
||||
}
|
||||
for(;;){
|
||||
struct sp_buf cur, d; struct timespec ts;
|
||||
ts.tv_sec=0; ts.tv_nsec=300*1000*1000; nanosleep(&ts,NULL);
|
||||
if(!tmux_has_session(tgt)) continue;
|
||||
if(sp_buf_init(&cur)!=0||sp_buf_init(&d)!=0){ if(cur.p) sp_buf_free(&cur); continue; }
|
||||
tmux_capture_n(tgt,300,&cur);
|
||||
if(cur.p && (prevn!=cur.len || memcmp(prev,cur.p, cur.len<prevn?cur.len:prevn)!=0)){
|
||||
extract_delta(prev, cur.p, &d);
|
||||
if(d.len>0){
|
||||
FILE *f=fopen(fp,"a");
|
||||
if(f){
|
||||
/* marker helps consumers; also write raw delta */
|
||||
fwrite(d.p,1,d.len,f);
|
||||
if(d.len==0 || ((char*)d.p)[d.len-1]!='\n') fputc('\n',f);
|
||||
fflush(f); fclose(f);
|
||||
}
|
||||
}
|
||||
{ size_t n=cur.len; if(n>=sizeof(prev)) n=sizeof(prev)-1; memcpy(prev,cur.p,n); prev[n]='\0'; prevn=n; }
|
||||
{ FILE *f=fopen(sp,"w"); if(f){ fprintf(f,"%ld %zu\n",(long)time(NULL),prevn); fclose(f);} }
|
||||
}
|
||||
sp_buf_free(&cur); sp_buf_free(&d);
|
||||
}
|
||||
}
|
||||
|
||||
static int watcher_start(struct sp_buf *out){
|
||||
pid_t pid; char p[600];
|
||||
load_target(); ensure_sess();
|
||||
if(!target[0]) return sp_buf_puts(out,"error: no tmux target\n");
|
||||
if(!sess_dir[0]) return sp_buf_puts(out,"error: no session dir\n");
|
||||
if(watcher_pid()>0) return sp_buf_printf(out,"watcher already running (pid %d) for %s\n",(int)watcher_pid(),target);
|
||||
/* clear feed for a fresh watch session? keep; append is fine */
|
||||
pid=fork();
|
||||
if(pid<0) return sp_buf_puts(out,"error: fork failed\n");
|
||||
if(pid==0){
|
||||
/* detach */
|
||||
setsid();
|
||||
{ int fd=open("/dev/null",O_RDWR); if(fd>=0){ dup2(fd,0); dup2(fd,1); dup2(fd,2); if(fd>2) close(fd);} }
|
||||
watcher_loop(target, sess_dir);
|
||||
_exit(0);
|
||||
}
|
||||
wpid_path(p,sizeof(p));
|
||||
sp_buf_printf(out,"watcher started (pid %d) for %s -> %s/tmux_watch.log\n",(int)pid,target,sess_dir);
|
||||
return 0;
|
||||
}
|
||||
static int watcher_stop(struct sp_buf *out){
|
||||
pid_t pid=watcher_pid(); char p[600];
|
||||
if(pid<=0){ sp_buf_puts(out,"watcher not running\n"); }
|
||||
else {
|
||||
kill(pid,SIGTERM);
|
||||
sp_buf_printf(out,"watcher stopped (pid %d)\n",(int)pid);
|
||||
}
|
||||
wpid_path(p,sizeof(p)); unlink(p);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* read new extracted content from watcher feed. offset tracked in tmux_watch.off */
|
||||
static long read_off(void){ char p[600],b[64]; FILE *f; long o=0; spath("tmux_watch.off",p,sizeof(p)); f=fopen(p,"r"); if(f){ if(fgets(b,sizeof(b),f)) o=strtol(b,NULL,10); fclose(f);} return o; }
|
||||
static void write_off(long o){ char p[600]; FILE *f; spath("tmux_watch.off",p,sizeof(p)); f=fopen(p,"w"); if(f){ fprintf(f,"%ld\n",o); fclose(f);} }
|
||||
|
||||
static int tool_read(const char *args, struct sp_buf *out, void *ctx){
|
||||
char fp[600]; FILE *f; long off, sz; int wait_ms=0; long start; struct timespec t0;
|
||||
(void)ctx; ensure_sess(); feed_path(fp,sizeof(fp));
|
||||
if(args && strstr(args,"\"wait_ms\"")){ const char *p=strstr(args,"\"wait_ms\""); p=strchr(p,':');
|
||||
if(p) wait_ms=atoi(p+1);
|
||||
}
|
||||
clock_gettime(CLOCK_MONOTONIC,&t0); start=t0.tv_sec*1000+t0.tv_nsec/1000000;
|
||||
off=read_off(); sz=file_size(fp);
|
||||
while(sz<=off && wait_ms>0){
|
||||
struct timespec ts; long now;
|
||||
ts.tv_sec=0; ts.tv_nsec=200*1000*1000; nanosleep(&ts,NULL);
|
||||
clock_gettime(CLOCK_MONOTONIC,&t0); now=t0.tv_sec*1000+t0.tv_nsec/1000000;
|
||||
if(now-start >= wait_ms) break;
|
||||
sz=file_size(fp);
|
||||
}
|
||||
if(sz<=off) return sp_buf_puts(out,"(no new watcher output)\n");
|
||||
f=fopen(fp,"r"); if(!f) return sp_buf_puts(out,"(no feed)\n");
|
||||
fseek(f,off,SEEK_SET);
|
||||
{
|
||||
char buf[4096]; size_t n; size_t total=0;
|
||||
sp_buf_puts(out,"[tmux watcher output]\n");
|
||||
while((n=fread(buf,1,sizeof(buf),f))>0){ sp_buf_add(out,buf,n); total+=n; }
|
||||
write_off(off+(long)total);
|
||||
}
|
||||
fclose(f);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int tool_send(const char *args, struct sp_buf *out, void *ctx){
|
||||
char text[4096]; const char *p,*e; size_t n=0;
|
||||
(void)ctx; load_target();
|
||||
if(!target[0]) return sp_buf_puts(out,"error: no tmux target\n");
|
||||
text[0]='\0';
|
||||
if(args){
|
||||
p=strstr(args,"\"text\""); if(!p) p=strstr(args,"\"input\"");
|
||||
if(p){ p=strchr(p,':'); if(p) p=strchr(p,'"'); if(p){ p++; e=p; while(*e && !(*e=='"'&&e[-1]!='\\') && n+1<sizeof(text)){ if(*e=='\\'&&e[1]){e++; text[n++]=(*e=='n')?'\n':(*e=='t')?'\t':*e;} else text[n++]=*e; e++; } text[n]='\0'; } }
|
||||
if(!text[0] && args[0] && args[0]!='{') snprintf(text,sizeof(text),"%.4000s",args);
|
||||
}
|
||||
if(!text[0]) return sp_buf_puts(out,"error: text required\n");
|
||||
if(tmux_send_lit(target,text,out)!=0) sp_buf_puts(out,"(tmux send-keys failed)\n");
|
||||
else sp_buf_printf(out,"sent to %s: %.120s%s\n", target, text, strlen(text)>120?"...":"");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int tool_capture(const char *args, struct sp_buf *out, void *ctx){
|
||||
int lines=200; const char *p;
|
||||
(void)ctx; load_target();
|
||||
if(!target[0]) return sp_buf_puts(out,"error: no tmux target\n");
|
||||
if(args){ p=strstr(args,"\"lines\""); if(p){ p=strchr(p,':');
|
||||
if(p) lines=atoi(p+1);
|
||||
}
|
||||
}
|
||||
if(lines<=0||lines>5000) lines=200;
|
||||
sp_buf_printf(out,"[tmux pane %s last %d]\n",target,lines);
|
||||
tmux_capture_n(target,lines,out);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int tool_ask(const char *args, struct sp_buf *out, void *ctx){
|
||||
/* proxy: send, then wait for watcher feed (background process extracts) */
|
||||
char text[4096]; const char *p,*e; size_t n=0; int wait_ms=15000, quiet_ms=1200;
|
||||
long base_off, last_sz, last_change, start; struct timespec t0;
|
||||
char fp[600];
|
||||
(void)ctx; load_target(); ensure_sess(); feed_path(fp,sizeof(fp));
|
||||
if(!target[0]) return sp_buf_puts(out,"error: no tmux target\n");
|
||||
if(watcher_pid()<=0){ sp_buf_puts(out,"(starting watcher...)\n"); watcher_start(out); }
|
||||
text[0]='\0';
|
||||
if(args){
|
||||
p=strstr(args,"\"wait_ms\""); if(p){ p=strchr(p,':');
|
||||
if(p) wait_ms=atoi(p+1);}
|
||||
p=strstr(args,"\"quiet_ms\""); if(p){ p=strchr(p,':');
|
||||
if(p) quiet_ms=atoi(p+1);}
|
||||
p=strstr(args,"\"text\""); if(!p) p=strstr(args,"\"input\"");
|
||||
if(p){ p=strchr(p,':'); if(p) p=strchr(p,'"'); if(p){ p++; e=p; while(*e && !(*e=='"'&&e[-1]!='\\') && n+1<sizeof(text)){ if(*e=='\\'&&e[1]){e++; text[n++]=(*e=='n')?'\n':(*e=='t')?'\t':*e;} else text[n++]=*e; e++; } text[n]='\0'; } }
|
||||
if(!text[0] && args[0] && args[0]!='{') snprintf(text,sizeof(text),"%.4000s",args);
|
||||
}
|
||||
if(!text[0]) return sp_buf_puts(out,"error: text required\n");
|
||||
sp_buf_printf(out,"[proxy -> %s] %.120s%s\n", target, text, strlen(text)>120?"...":"");
|
||||
base_off = file_size(fp); /* consume from here */
|
||||
tmux_send_lit(target,text,out);
|
||||
sp_buf_puts(out,"[waiting on watcher for new output...]\n");
|
||||
clock_gettime(CLOCK_MONOTONIC,&t0); start=t0.tv_sec*1000+t0.tv_nsec/1000000; last_change=start; last_sz=base_off;
|
||||
while(1){
|
||||
struct timespec ts; long now, sz;
|
||||
ts.tv_sec=0; ts.tv_nsec=200*1000*1000; nanosleep(&ts,NULL);
|
||||
clock_gettime(CLOCK_MONOTONIC,&t0); now=t0.tv_sec*1000+t0.tv_nsec/1000000;
|
||||
sz=file_size(fp);
|
||||
if(sz>last_sz){ last_sz=sz; last_change=now; }
|
||||
if(last_sz>base_off && (now-last_change)>=quiet_ms) break; /* stable new output */
|
||||
if((now-start)>=wait_ms) break;
|
||||
}
|
||||
if(last_sz>base_off){
|
||||
FILE *f=fopen(fp,"r");
|
||||
if(f){
|
||||
char buf[4096]; size_t nr;
|
||||
fseek(f,base_off,SEEK_SET);
|
||||
sp_buf_puts(out,"\n--- watcher extracted (new) ---\n");
|
||||
while((nr=fread(buf,1,sizeof(buf),f))>0) sp_buf_add(out,buf,nr);
|
||||
fclose(f);
|
||||
write_off(last_sz);
|
||||
}
|
||||
} else {
|
||||
sp_buf_puts(out,"(timeout: no new watcher output; /tmux capture to inspect pane)\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int tool_start(const char *args, struct sp_buf *out, void *ctx){
|
||||
char cmd[1024], name[64]; const char *p,*e; size_t n=0; struct sp_buf o;
|
||||
(void)ctx; cmd[0]='\0'; name[0]='\0';
|
||||
if(args){
|
||||
p=strstr(args,"\"cmd\""); if(!p) p=strstr(args,"\"command\"");
|
||||
if(p){ p=strchr(p,':'); if(p){ p=strchr(p,'"'); if(p){ p++; e=p; while(*e && !(*e=='"'&&e[-1]!='\\') && n+1<sizeof(cmd)){ if(*e=='\\'&&e[1]){e++; cmd[n++]=*e;} else cmd[n++]=*e; e++; } cmd[n]='\0'; } } }
|
||||
p=strstr(args,"\"name\""); if(p){ p=strchr(p,':'); if(p){ p=strchr(p,'"'); if(p){ char *q=name; p++; while(*p&&*p!='"'&&q<name+sizeof(name)-1) *q++=*p++; *q='\0'; } } }
|
||||
}
|
||||
if(!cmd[0]) return sp_buf_puts(out,"error: cmd required\n");
|
||||
if(!name[0]) snprintf(name,sizeof(name),"splinter-%d",(int)getpid());
|
||||
if(sp_buf_init(&o)!=0) return -1;
|
||||
if(!tmux_has_session(name)){
|
||||
char *a[]={"tmux","new-session","-d","-s",name,"sh","-lc",cmd,NULL};
|
||||
if(run_argv(a,&o)!=0){ sp_buf_puts(out,"error: tmux new-session failed: "); if(o.p) sp_buf_puts(out,o.p); sp_buf_free(&o); return 0; }
|
||||
} else { sp_buf_puts(out,"(session exists, attaching)\n"); }
|
||||
snprintf(target,sizeof(target),"%s",name); save_target(); sp_buf_free(&o);
|
||||
sp_buf_printf(out,"target set to %s (cmd: %s)\n", name, cmd);
|
||||
watcher_start(out);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int tool_watch(const char *args, struct sp_buf *out, void *ctx){
|
||||
(void)ctx; load_target(); ensure_sess();
|
||||
if(args && strstr(args,"stop")) return watcher_stop(out);
|
||||
if(args && strstr(args,"status")){ pid_t pid=watcher_pid(); sp_buf_printf(out,"watcher %s (pid %d) target=%s\n", pid>0?"running":"stopped",(int)pid,target); return 0; }
|
||||
return watcher_start(out);
|
||||
}
|
||||
|
||||
static int tool_status(const char *args, struct sp_buf *out, void *ctx){
|
||||
(void)args;(void)ctx; load_target();
|
||||
sp_buf_printf(out,"tmux target: %s\n", target[0]?target:"(none)");
|
||||
sp_buf_printf(out,"tmux installed: %s, watcher: %s (pid %d)\n", have_tmux()?"yes":"no", watcher_pid()>0?"running":"stopped",(int)watcher_pid());
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int cmd_tmux(struct sp_host *h, const char *args, struct sp_buf *out, void *vctx){
|
||||
(void)vctx; host_ref=h; load_target();
|
||||
if(!args||!*args||strcmp(args,"status")==0) return tool_status(NULL,out,NULL);
|
||||
if(strncmp(args,"attach ",7)==0){ const char *t=args+7; while(*t==' ') t++; snprintf(target,sizeof(target),"%.120s",t); save_target(); sp_buf_printf(out,"target=%s\n",target); watcher_start(out); return 0; }
|
||||
if(strncmp(args,"start ",6)==0){ struct sp_buf a; char esc[1200]; size_t i,j=0; const char *c=args+6; while(*c==' ') c++; for(i=0;c[i]&&j+2<sizeof(esc);i++){ char ch=c[i]; if(ch=='"'||ch=='\\'){esc[j++]='\\';esc[j++]=ch;} else esc[j++]=ch;} esc[j]='\0'; if(sp_buf_init(&a)!=0) return -1; sp_buf_printf(&a,"{\"cmd\":\"%s\"}",esc); {
|
||||
int rc=tool_start(a.p,out,NULL);
|
||||
sp_buf_free(&a);
|
||||
return rc;
|
||||
} }
|
||||
if(strncmp(args,"send ",5)==0){ struct sp_buf a; char esc[4200]; size_t i,j=0; const char *t=args+5; for(i=0;t[i]&&j+2<sizeof(esc);i++){ char c=t[i]; if(c=='"'||c=='\\'){esc[j++]='\\';esc[j++]=c;} else if(c=='\n'){esc[j++]='\\';esc[j++]='n';} else esc[j++]=c;} esc[j]='\0'; if(sp_buf_init(&a)!=0) return -1; sp_buf_printf(&a,"{\"text\":\"%s\"}",esc); {
|
||||
int rc=tool_send(a.p,out,NULL);
|
||||
sp_buf_free(&a);
|
||||
return rc;
|
||||
} }
|
||||
if(strncmp(args,"keys ",5)==0){ if(!target[0]) return sp_buf_puts(out,"no target\n"); { char *a[]={"tmux","send-keys","-t",target,(char*)(args+5),NULL}; run_argv(a,out); }
|
||||
return sp_buf_puts(out,"keys sent\n"); }
|
||||
if(strcmp(args,"capture")==0||strncmp(args,"capture ",8)==0){ struct sp_buf a; int l=200; if(strncmp(args,"capture ",8)==0) l=atoi(args+8); if(sp_buf_init(&a)!=0) return -1; sp_buf_printf(&a,"{\"lines\":%d}",l); {
|
||||
int rc=tool_capture(a.p,out,NULL);
|
||||
sp_buf_free(&a);
|
||||
return rc;
|
||||
} }
|
||||
if(strcmp(args,"read")==0||strncmp(args,"read ",5)==0){ struct sp_buf a; int w=0; if(strncmp(args,"read ",5)==0) w=atoi(args+5); if(sp_buf_init(&a)!=0) return -1; sp_buf_printf(&a,"{\"wait_ms\":%d}",w); {
|
||||
int rc=tool_read(a.p,out,NULL);
|
||||
sp_buf_free(&a);
|
||||
return rc;
|
||||
} }
|
||||
if(strncmp(args,"ask ",4)==0){ struct sp_buf a; char esc[4200]; size_t i,j=0; const char *t=args+4; for(i=0;t[i]&&j+2<sizeof(esc);i++){ char c=t[i]; if(c=='"'||c=='\\'){esc[j++]='\\';esc[j++]=c;} else esc[j++]=c;} esc[j]='\0'; if(sp_buf_init(&a)!=0) return -1; sp_buf_printf(&a,"{\"text\":\"%s\"}",esc); {
|
||||
int rc=tool_ask(a.p,out,NULL);
|
||||
sp_buf_free(&a);
|
||||
return rc;
|
||||
} }
|
||||
if(strncmp(args,"watch",5)==0){ struct sp_buf a; if(strstr(args,"stop")) { if(sp_buf_init(&a)!=0) return -1; sp_buf_puts(&a,"{\"cmd\":\"stop\"}"); } else if(strstr(args,"status")) { if(sp_buf_init(&a)!=0) return -1; sp_buf_puts(&a,"{\"cmd\":\"status\"}"); } else { if(sp_buf_init(&a)!=0) return -1; sp_buf_puts(&a,"{\"cmd\":\"start\"}"); } {
|
||||
int rc=tool_watch(a.p,out,NULL);
|
||||
sp_buf_free(&a);
|
||||
return rc;
|
||||
} }
|
||||
if(strcmp(args,"stop")==0){ watcher_stop(out); if(target[0]){ char *a[]={"tmux","kill-session","-t",target,NULL}; struct sp_buf o; sp_buf_init(&o); run_argv(a,&o); sp_buf_free(&o); sp_buf_printf(out,"killed %s\n",target); target[0]='\0'; save_target(); } return 0; }
|
||||
return sp_buf_puts(out,"usage: /tmux start <cmd>|attach <t>|send <text>|keys <k>|capture [n]|read [wait_ms]|ask <text>|watch [start|stop|status]|status|stop\n watcher: background process detects new text & extracts to feed; ask waits on feed (no fixed sleep)\n");
|
||||
}
|
||||
|
||||
static int tick(struct sp_host *h, const char *name, void *ctx){
|
||||
(void)name;(void)ctx; host_ref=h; load_target();
|
||||
{ char b[128]; if(target[0]) snprintf(b,sizeof(b),"tmux:%.100s %s", target, watcher_pid()>0?"[watch]":"[no-watch]"); else snprintf(b,sizeof(b),"tmux:-"); sp_host_widget_text(h,"tmux",b); }
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const struct sp_tool Tstatus={"tmux_status","tmux proxy target+watcher status.","{\"type\":\"object\",\"properties\":{}}",SP_AUTH_READ,tool_status,NULL};
|
||||
static const struct sp_tool Tcap ={"tmux_capture","Snapshot tmux pane (direct). Args: lines.","{\"type\":\"object\",\"properties\":{\"lines\":{\"type\":\"integer\"}}}",SP_AUTH_READ,tool_capture,NULL};
|
||||
static const struct sp_tool Tread ={"tmux_read","Read NEW output extracted by the background watcher feed. Args: wait_ms (wait for new data).","{\"type\":\"object\",\"properties\":{\"wait_ms\":{\"type\":\"integer\"}}}",SP_AUTH_READ,tool_read,NULL};
|
||||
static const struct sp_tool Tsend ={"tmux_send","Send literal text+Enter to tmux target CLI.","{\"type\":\"object\",\"properties\":{\"text\":{\"type\":\"string\"}},\"required\":[\"text\"]}",SP_AUTH_MUTATE,tool_send,NULL};
|
||||
static const struct sp_tool Task ={"tmux_ask","Proxy: send text then wait on WATCHER feed for new extracted output (quiet-stable), no fixed sleep. Args: text, wait_ms, quiet_ms.","{\"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 Tstart ={"tmux_start","Start cmd in detached tmux session, set target, start watcher. Args: cmd,name.","{\"type\":\"object\",\"properties\":{\"cmd\":{\"type\":\"string\"},\"name\":{\"type\":\"string\"}},\"required\":[\"cmd\"]}",SP_AUTH_DESTRUCTIVE,tool_start,NULL};
|
||||
static const struct sp_tool Twatch ={"tmux_watch","Control background watcher process (detects/extracts new output). Args: cmd start|stop|status.","{\"type\":\"object\",\"properties\":{\"cmd\":{\"type\":\"string\"}}}",SP_AUTH_MUTATE,tool_watch,NULL};
|
||||
static const struct sp_widget W={"tmux",tick,NULL,NULL,5000};
|
||||
|
||||
static int init(struct sp_host *h){
|
||||
if(!have_tmux()){
|
||||
/* tmux not installed -> don't load plugin */
|
||||
return -1;
|
||||
}
|
||||
host_ref=h; load_target();
|
||||
sp_host_add_tool(h,&Tstatus); sp_host_add_tool(h,&Tcap); sp_host_add_tool(h,&Tread);
|
||||
sp_host_add_tool(h,&Tsend); sp_host_add_tool(h,&Task); sp_host_add_tool(h,&Tstart); sp_host_add_tool(h,&Twatch);
|
||||
sp_host_add_command(h,"tmux",cmd_tmux,NULL);
|
||||
sp_host_add_widget(h,&W);
|
||||
return 0;
|
||||
}
|
||||
static const struct sp_plugin plugin={SP_PLUGIN_ABI,"tmux","0.2",init,NULL,NULL};
|
||||
const struct sp_plugin *sp_plugin_describe(void){ return &plugin; }
|
||||
Reference in new issue
Block a user