simpler_plumb-0.9.diff (3389B)
1 From d77f364af4d685a614545120d4fa28e9e3770c5e Mon Sep 17 00:00:00 2001 2 From: sewn <sewn@disroot.org> 3 Date: Wed, 7 Jun 2023 14:46:03 +0300 4 Subject: [PATCH] Pass selected text to a plumber program 5 6 --- 7 config.def.h | 7 +++++++ 8 st.c | 11 ++++++++++- 9 st.h | 4 ++++ 10 x.c | 28 ++++++++++++++++++++++++++++ 11 4 files changed, 49 insertions(+), 1 deletion(-) 12 13 diff --git a/config.def.h b/config.def.h 14 index 91ab8ca..1f5b17a 100644 15 --- a/config.def.h 16 +++ b/config.def.h 17 @@ -170,6 +170,12 @@ static unsigned int defaultattr = 11; 18 */ 19 static uint forcemousemod = ShiftMask; 20 21 +/* 22 + * Default plumber program. The selected text will be passed as an argument. 23 + * No other arguments can be passed. 24 + */ 25 +static char plumber[] = "plumber"; 26 + 27 /* 28 * Internal mouse shortcuts. 29 * Beware that overloading Button1 will disable the selection. 30 @@ -181,6 +187,7 @@ static MouseShortcut mshortcuts[] = { 31 { XK_ANY_MOD, Button4, ttysend, {.s = "\031"} }, 32 { ShiftMask, Button5, ttysend, {.s = "\033[6;2~"} }, 33 { XK_ANY_MOD, Button5, ttysend, {.s = "\005"} }, 34 + { XK_NO_MOD, Button3, plumb, {.i = 0}, }, 35 }; 36 37 /* Internal keyboard shortcuts. */ 38 diff --git a/st.c b/st.c 39 index 134e724..626efa3 100644 40 --- a/st.c 41 +++ b/st.c 42 @@ -225,7 +225,8 @@ static CSIEscape csiescseq; 43 static STREscape strescseq; 44 static int iofd = 1; 45 static int cmdfd; 46 -static pid_t pid; 47 + 48 +pid_t pid; 49 50 static const uchar utfbyte[UTF_SIZ + 1] = {0x80, 0, 0xC0, 0xE0, 0xF0}; 51 static const uchar utfmask[UTF_SIZ + 1] = {0xC0, 0x80, 0xE0, 0xF0, 0xF8}; 52 @@ -1054,6 +1055,14 @@ tswapscreen(void) 53 tfulldirt(); 54 } 55 56 +int 57 +chdir_by_pid(pid_t pid) 58 +{ 59 + char buf[32]; 60 + snprintf(buf, sizeof buf, "/proc/%ld/cwd", (long)pid); 61 + return chdir(buf); 62 +} 63 + 64 void 65 tscrolldown(int orig, int n) 66 { 67 diff --git a/st.h b/st.h 68 index fd3b0d8..cca6468 100644 69 --- a/st.h 70 +++ b/st.h 71 @@ -81,6 +81,8 @@ void die(const char *, ...); 72 void redraw(void); 73 void draw(void); 74 75 +int chdir_by_pid(pid_t pid); 76 + 77 void printscreen(const Arg *); 78 void printsel(const Arg *); 79 void sendbreak(const Arg *); 80 @@ -124,3 +126,5 @@ extern unsigned int tabspaces; 81 extern unsigned int defaultfg; 82 extern unsigned int defaultbg; 83 extern unsigned int defaultcs; 84 + 85 +extern pid_t pid; 86 diff --git a/x.c b/x.c 87 index aa09997..1cca84e 100644 88 --- a/x.c 89 +++ b/x.c 90 @@ -5,6 +5,7 @@ 91 #include <locale.h> 92 #include <signal.h> 93 #include <sys/select.h> 94 +#include <sys/wait.h> 95 #include <time.h> 96 #include <unistd.h> 97 #include <libgen.h> 98 @@ -55,6 +56,7 @@ static void clipcopy(const Arg *); 99 static void clippaste(const Arg *); 100 static void numlock(const Arg *); 101 static void selpaste(const Arg *); 102 +static void plumb(const Arg *); 103 static void zoom(const Arg *); 104 static void zoomabs(const Arg *); 105 static void zoomreset(const Arg *); 106 @@ -292,6 +294,32 @@ numlock(const Arg *dummy) 107 win.mode ^= MODE_NUMLOCK; 108 } 109 110 +void 111 +plumb(const Arg *dummy) 112 +{ 113 + switch(fork()) { 114 + case -1: 115 + die("plumb failed: %s\n", strerror(errno)); 116 + break; 117 + case 0: 118 + switch(fork()) { 119 + case -1: 120 + die("plumb failed: %s\n", strerror(errno)); 121 + _exit(1); 122 + break; 123 + case 0: 124 + chdir_by_pid(pid); 125 + execlp(plumber, plumber, xsel.primary, NULL); 126 + _exit(127); 127 + break; 128 + default: 129 + _exit(0); 130 + } 131 + default: 132 + wait(NULL); 133 + } 134 +} 135 + 136 void 137 zoom(const Arg *arg) 138 { 139 -- 140 2.40.1 141