From d77f364af4d685a614545120d4fa28e9e3770c5e Mon Sep 17 00:00:00 2001 From: sewn Date: Wed, 7 Jun 2023 14:46:03 +0300 Subject: [PATCH] Pass selected text to a plumber program --- config.def.h | 7 +++++++ st.c | 11 ++++++++++- st.h | 4 ++++ x.c | 28 ++++++++++++++++++++++++++++ 4 files changed, 49 insertions(+), 1 deletion(-) diff --git a/config.def.h b/config.def.h index 91ab8ca..1f5b17a 100644 --- a/config.def.h +++ b/config.def.h @@ -170,6 +170,12 @@ static unsigned int defaultattr = 11; */ static uint forcemousemod = ShiftMask; +/* + * Default plumber program. The selected text will be passed as an argument. + * No other arguments can be passed. + */ +static char plumber[] = "plumber"; + /* * Internal mouse shortcuts. * Beware that overloading Button1 will disable the selection. @@ -181,6 +187,7 @@ static MouseShortcut mshortcuts[] = { { XK_ANY_MOD, Button4, ttysend, {.s = "\031"} }, { ShiftMask, Button5, ttysend, {.s = "\033[6;2~"} }, { XK_ANY_MOD, Button5, ttysend, {.s = "\005"} }, + { XK_NO_MOD, Button3, plumb, {.i = 0}, }, }; /* Internal keyboard shortcuts. */ diff --git a/st.c b/st.c index 134e724..626efa3 100644 --- a/st.c +++ b/st.c @@ -225,7 +225,8 @@ static CSIEscape csiescseq; static STREscape strescseq; static int iofd = 1; static int cmdfd; -static pid_t pid; + +pid_t pid; static const uchar utfbyte[UTF_SIZ + 1] = {0x80, 0, 0xC0, 0xE0, 0xF0}; static const uchar utfmask[UTF_SIZ + 1] = {0xC0, 0x80, 0xE0, 0xF0, 0xF8}; @@ -1054,6 +1055,14 @@ tswapscreen(void) tfulldirt(); } +int +chdir_by_pid(pid_t pid) +{ + char buf[32]; + snprintf(buf, sizeof buf, "/proc/%ld/cwd", (long)pid); + return chdir(buf); +} + void tscrolldown(int orig, int n) { diff --git a/st.h b/st.h index fd3b0d8..cca6468 100644 --- a/st.h +++ b/st.h @@ -81,6 +81,8 @@ void die(const char *, ...); void redraw(void); void draw(void); +int chdir_by_pid(pid_t pid); + void printscreen(const Arg *); void printsel(const Arg *); void sendbreak(const Arg *); @@ -124,3 +126,5 @@ extern unsigned int tabspaces; extern unsigned int defaultfg; extern unsigned int defaultbg; extern unsigned int defaultcs; + +extern pid_t pid; diff --git a/x.c b/x.c index aa09997..1cca84e 100644 --- a/x.c +++ b/x.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -55,6 +56,7 @@ static void clipcopy(const Arg *); static void clippaste(const Arg *); static void numlock(const Arg *); static void selpaste(const Arg *); +static void plumb(const Arg *); static void zoom(const Arg *); static void zoomabs(const Arg *); static void zoomreset(const Arg *); @@ -292,6 +294,32 @@ numlock(const Arg *dummy) win.mode ^= MODE_NUMLOCK; } +void +plumb(const Arg *dummy) +{ + switch(fork()) { + case -1: + die("plumb failed: %s\n", strerror(errno)); + break; + case 0: + switch(fork()) { + case -1: + die("plumb failed: %s\n", strerror(errno)); + _exit(1); + break; + case 0: + chdir_by_pid(pid); + execlp(plumber, plumber, xsel.primary, NULL); + _exit(127); + break; + default: + _exit(0); + } + default: + wait(NULL); + } +} + void zoom(const Arg *arg) { -- 2.40.1