plumb selected text with right click - st - simple terminal
 (HTM) git clone https://git.parazyd.org/st
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 97988681134630a84c9824d83027d0f429d427e8
 (DIR) parent 953e60e96aca92491703ff1cb6aec33f91c0e945
 (HTM) Author: parazyd <parazyd@dyne.org>
       Date:   Tue, 26 Apr 2022 20:45:45 +0200
       
       plumb selected text with right click
       
       Diffstat:
         M config.def.h                        |       6 ++++++
         M st.c                                |       9 +++++++++
         M st.h                                |       2 ++
         M x.c                                 |      28 ++++++++++++++++++++++++++++
       
       4 files changed, 45 insertions(+), 0 deletions(-)
       ---
 (DIR) diff --git a/config.def.h b/config.def.h
       @@ -476,3 +476,9 @@ static char ascii_printable[] =
                " !\"#$%&'()*+,-./0123456789:;<=>?"
                "@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_"
                "`abcdefghijklmnopqrstuvwxyz{|}~";
       +
       +/*
       + * plumb_cmd is run on mouse button 3 click, with argument set to
       + * current selection and with cwd set to be the cwd of the active shell
       + */
       +static char *plumb_cmd = "/usr/local/bin/plumber";
 (DIR) diff --git a/st.c b/st.c
       @@ -232,6 +232,15 @@ 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 (snprintf(path, PATH_MAX, "/proc/%d/cwd", pid) < 0)
       +                return -1;
       +
       +        return 0;
       +}
       +
        ssize_t
        xwrite(int fd, const char *s, size_t len)
        {
 (DIR) diff --git 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 *);
       +
        /* config.h globals */
        extern char *utmp;
        extern char *scroll;
 (DIR) diff --git 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>
       @@ -721,6 +722,31 @@ xsetsel(char *str)
        }
        
        void
       +plumb(char *sel)
       +{
       +        if (sel == NULL)
       +                return;
       +
       +        char cwd[PATH_MAX];
       +        pid_t child;
       +        if (subprocwd(cwd) != 0)
       +                return;
       +
       +        switch (child = fork()) {
       +        case -1:
       +                return;
       +        case 0:
       +                if (chdir(cwd) != 0)
       +                        exit(1);
       +                if (execvp(plumb_cmd, (char *const []){plumb_cmd, sel, 0}) == -1)
       +                        exit(1);
       +                exit(0);
       +        default:
       +                waitpid(child, NULL, 0);
       +        }
       +}
       +
       +void
        brelease(XEvent *e)
        {
                int btn = e->xbutton.button;
       @@ -737,6 +763,8 @@ brelease(XEvent *e)
                        return;
                if (btn == Button1)
                        mousesel(e, 1);
       +        else if (btn == Button3)
       +                plumb(xsel.primary);
        }
        
        void