diff options
| author | Douglas B. Rumbaugh <doug@douglasrumbaugh.com> | 2022-12-31 17:21:08 -0500 |
|---|---|---|
| committer | Douglas B. Rumbaugh <doug@douglasrumbaugh.com> | 2022-12-31 17:21:08 -0500 |
| commit | b1df89ed947503c74a61f36a1b3a92f5f424352e (patch) | |
| tree | d3a529698171143929554aca7f150da242f52130 | |
| parent | 655ba6883b219befcc8aefdd6f50a4f678a7af98 (diff) | |
| download | st-b1df89ed947503c74a61f36a1b3a92f5f424352e.tar.gz | |
Applied plumber patch
| -rw-r--r-- | config.def.h | 6 | ||||
| -rw-r--r-- | patches/plumb.diff | 148 | ||||
| -rw-r--r-- | st.c | 21 | ||||
| -rw-r--r-- | st.h | 1 | ||||
| -rw-r--r-- | x.c | 36 |
5 files changed, 211 insertions, 1 deletions
diff --git a/config.def.h b/config.def.h index d22c571..340c430 100644 --- a/config.def.h +++ b/config.def.h @@ -520,3 +520,9 @@ static char ascii_printable[] = " !\"#$%&'()*+,-./0123456789:;<=>?" "@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_" "`abcdefghijklmnopqrstuvwxyz{|}~"; + +/* + * plumb_cmd is run on mouse button 3 click, with first NULL set to + * current selection and with cwd set to the cwd of the active shell + */ +static char *plumb_cmd[] = {"plumb", "-m", NULL, NULL}; diff --git a/patches/plumb.diff b/patches/plumb.diff new file mode 100644 index 0000000..f0eb5f3 --- /dev/null +++ b/patches/plumb.diff @@ -0,0 +1,148 @@ +diff --git a/config.def.h b/config.def.h +index 91ab8ca..d0343e3 100644 +--- a/config.def.h ++++ b/config.def.h +@@ -472,3 +472,9 @@ static char ascii_printable[] = + " !\"#$%&'()*+,-./0123456789:;<=>?" + "@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_" + "`abcdefghijklmnopqrstuvwxyz{|}~"; ++ ++/* ++ * plumb_cmd is run on mouse button 3 click, with first NULL set to ++ * current selection and with cwd set to the cwd of the active shell ++ */ ++static char *plumb_cmd[] = {"plumb", "-m", NULL, NULL}; +diff --git a/st.c b/st.c +index 51049ba..11756c4 100644 +--- a/st.c ++++ b/st.c +@@ -27,6 +27,9 @@ + #elif defined(__FreeBSD__) || defined(__DragonFly__) + #include <libutil.h> + #endif ++#if defined(__OpenBSD__) ++ #include <sys/sysctl.h> ++#endif + + /* Arbitrary sizes */ + #define UTF_INVALID 0xFFFD +@@ -231,6 +234,22 @@ static const uchar utfmask[UTF_SIZ + 1] = {0xC0, 0x80, 0xE0, 0xF0, 0xF8}; + static const Rune utfmin[UTF_SIZ + 1] = { 0, 0, 0x80, 0x800, 0x10000}; + static const Rune utfmax[UTF_SIZ + 1] = {0x10FFFF, 0x7F, 0x7FF, 0xFFFF, 0x10FFFF}; + ++int ++subprocwd(char *path) ++{ ++#if defined(__linux) ++ if (snprintf(path, PATH_MAX, "/proc/%d/cwd", pid) < 0) ++ return -1; ++ return 0; ++#elif defined(__OpenBSD__) ++ size_t sz = PATH_MAX; ++ int name[3] = {CTL_KERN, KERN_PROC_CWD, pid}; ++ if (sysctl(name, 3, path, &sz, 0, 0) == -1) ++ return -1; ++ return 0; ++#endif ++} ++ + ssize_t + xwrite(int fd, const char *s, size_t len) + { +@@ -810,7 +829,7 @@ ttynew(const char *line, char *cmd, const char *out, char **args) + break; + default: + #ifdef __OpenBSD__ +- if (pledge("stdio rpath tty proc", NULL) == -1) ++ if (pledge("stdio rpath tty proc ps exec", NULL) == -1) + die("pledge\n"); + #endif + close(s); +diff --git a/st.h b/st.h +index 519b9bd..8255556 100644 +--- a/st.h ++++ b/st.h +@@ -111,6 +111,8 @@ void *xmalloc(size_t); + void *xrealloc(void *, size_t); + char *xstrdup(const char *); + ++int subprocwd(char *); ++ + int xgetcolor(int x, unsigned char *r, unsigned char *g, unsigned char *b); + + /* config.h globals */ +diff --git a/x.c b/x.c +index cd96575..5d3948a 100644 +--- a/x.c ++++ b/x.c +@@ -5,6 +5,7 @@ + #include <locale.h> + #include <signal.h> + #include <sys/select.h> ++#include <sys/wait.h> + #include <time.h> + #include <unistd.h> + #include <libgen.h> +@@ -216,6 +217,7 @@ static void (*handler[LASTEvent])(XEvent *) = { + }; + + /* Globals */ ++static int plumbsel; + static DC dc; + static XWindow xw; + static XSelection xsel; +@@ -694,6 +696,37 @@ xsetsel(char *str) + setsel(str, CurrentTime); + } + ++void ++plumbinit() ++{ ++ for(plumbsel = 0; plumb_cmd[plumbsel]; plumbsel++); ++} ++ ++void ++plumb(char *sel) { ++ if (sel == NULL) ++ return; ++ char cwd[PATH_MAX]; ++ pid_t child; ++ if (subprocwd(cwd) != 0) ++ return; ++ ++ plumb_cmd[plumbsel] = sel; ++ ++ switch(child = fork()) { ++ case -1: ++ return; ++ case 0: ++ if (chdir(cwd) != 0) ++ exit(1); ++ if (execvp(plumb_cmd[0], plumb_cmd) == -1) ++ exit(1); ++ exit(0); ++ default: ++ waitpid(child, NULL, 0); ++ } ++} ++ + void + brelease(XEvent *e) + { +@@ -711,6 +744,8 @@ brelease(XEvent *e) + return; + if (e->xbutton.button == Button1) + mousesel(e, 1); ++ else if (e->xbutton.button == Button3) ++ plumb(xsel.primary); + } + + void +@@ -2076,6 +2111,7 @@ main(int argc, char *argv[]) + } ARGEND; + + run: ++ plumbinit(); + if (argc > 0) /* eat all remaining arguments */ + opt_cmd = argv; + @@ -27,6 +27,9 @@ #elif defined(__FreeBSD__) || defined(__DragonFly__) #include <libutil.h> #endif +#if defined(__OpenBSD__) + #include <sys/sysctl.h> +#endif /* Arbitrary sizes */ #define UTF_INVALID 0xFFFD @@ -232,6 +235,22 @@ static const uchar utfmask[UTF_SIZ + 1] = {0xC0, 0x80, 0xE0, 0xF0, 0xF8}; static const Rune utfmin[UTF_SIZ + 1] = { 0, 0, 0x80, 0x800, 0x10000}; static const Rune utfmax[UTF_SIZ + 1] = {0x10FFFF, 0x7F, 0x7FF, 0xFFFF, 0x10FFFF}; +int +subprocwd(char *path) +{ +#if defined(__linux) + if (snprintf(path, PATH_MAX, "/proc/%d/cwd", pid) < 0) + return -1; + return 0; +#elif defined(__OpenBSD__) + size_t sz = PATH_MAX; + int name[3] = {CTL_KERN, KERN_PROC_CWD, pid}; + if (sysctl(name, 3, path, &sz, 0, 0) == -1) + return -1; + return 0; +#endif +} + ssize_t xwrite(int fd, const char *s, size_t len) { @@ -803,7 +822,7 @@ ttynew(const char *line, char *cmd, const char *out, char **args) break; default: #ifdef __OpenBSD__ - if (pledge("stdio rpath tty proc", NULL) == -1) + if (pledge("stdio rpath tty proc ps exec", NULL) == -1) die("pledge\n"); #endif close(s); @@ -113,6 +113,7 @@ void *xmalloc(size_t); void *xrealloc(void *, size_t); char *xstrdup(const char *); +int subprocwd(char *); int isboxdraw(Rune); ushort boxdrawindex(const Glyph *); #ifdef XFT_VERSION @@ -5,6 +5,7 @@ #include <locale.h> #include <signal.h> #include <sys/select.h> +#include <sys/wait.h> #include <time.h> #include <unistd.h> #include <libgen.h> @@ -232,6 +233,7 @@ static void (*handler[LASTEvent])(XEvent *) = { }; /* Globals */ +static int plumbsel; static DC dc; static XWindow xw; static XSelection xsel; @@ -711,6 +713,37 @@ xsetsel(char *str) } void +plumbinit() +{ + for(plumbsel = 0; plumb_cmd[plumbsel]; plumbsel++); +} + +void +plumb(char *sel) { + if (sel == NULL) + return; + char cwd[PATH_MAX]; + pid_t child; + if (subprocwd(cwd) != 0) + return; + + plumb_cmd[plumbsel] = sel; + + switch(child = fork()) { + case -1: + return; + case 0: + if (chdir(cwd) != 0) + exit(1); + if (execvp(plumb_cmd[0], plumb_cmd) == -1) + exit(1); + exit(0); + default: + waitpid(child, NULL, 0); + } +} + +void brelease(XEvent *e) { int btn = e->xbutton.button; @@ -727,6 +760,8 @@ brelease(XEvent *e) return; if (btn == Button1) mousesel(e, 1); + else if (e->xbutton.button == Button3) + plumb(xsel.primary); } void @@ -2196,6 +2231,7 @@ main(int argc, char *argv[]) } ARGEND; run: + plumbinit(); if (argc > 0) /* eat all remaining arguments */ opt_cmd = argv; |