From: Daniel Liu Date: Sun, 7 Feb 2021 01:19:46 +0000 (-0500) Subject: applied plumb with keybind X-Git-Url: https://git.danieliu.xyz/?p=st.git;a=commitdiff_plain;h=4649bab45b4c23489b6dea7cd6aa719060f39ce4 applied plumb with keybind --- diff --git a/config.def.h b/config.def.h index b7f883f..db31681 100644 --- a/config.def.h +++ b/config.def.h @@ -258,6 +258,7 @@ static Shortcut shortcuts[] = { { TERMMOD, XK_Num_Lock, numlock, {.i = 0} }, { MODKEY, XK_c, normalMode, {.i = 0} }, { ControlMask, XK_Return, newterm, {.i = 0} }, + { ControlMask, XK_space, plumber, {.i = 0} }, }; /* @@ -529,3 +530,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 8c022b7..3abf1b0 100644 --- a/st.c +++ b/st.c @@ -28,6 +28,9 @@ #elif defined(__FreeBSD__) || defined(__DragonFly__) #include #endif +#if defined(__OpenBSD__) + #include +#endif /* Arbitrary sizes */ #define UTF_INVALID 0xFFFD @@ -243,6 +246,22 @@ static TCursor c[3]; static inline int rows() { return IS_SET(MODE_ALTSCREEN) ? term.row : buffSize;} static inline int rangeY(int i) { while (i < 0) i += rows(); return i % rows();} +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) { @@ -812,7 +831,7 @@ ttynew(char *line, char *cmd, 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 9404925..e1b5057 100644 --- a/st.h +++ b/st.h @@ -113,6 +113,8 @@ void *xmalloc(size_t); void *xrealloc(void *, size_t); char *xstrdup(char *); +int subprocwd(char *); + /* config.h globals */ extern char *utmp; extern char *scroll; diff --git a/x.c b/x.c index b278a34..5989947 100644 --- a/x.c +++ b/x.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -74,6 +75,7 @@ static void zoom(const Arg *); static void zoomabs(const Arg *); static void zoomreset(const Arg *); static void ttysend(const Arg *); +static void plumber(const Arg *); /* config.h for applying patches and the configuration. */ #include "config.h" @@ -196,11 +198,13 @@ static void selnotify(XEvent *); static void selclear_(XEvent *); static void selrequest(XEvent *); static void setsel(char *, Time); +static void plumb(char *sel); static void mousesel(XEvent *, int); static void mousereport(XEvent *); static char *kmap(KeySym, uint); static int match(uint, uint); + static void run(void); static void usage(void); @@ -231,6 +235,7 @@ static void (*handler[LASTEvent])(XEvent *) = { }; /* Globals */ +static int plumbsel; static DC dc; static XWindow xw; static XSelection xsel; @@ -345,6 +350,12 @@ ttysend(const Arg *arg) ttywrite(arg->s, strlen(arg->s), 1); } +void +plumber(const Arg *arg) +{ + plumb(xsel.primary); +} + int evcol(XEvent *e) { @@ -698,6 +709,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) { @@ -2130,6 +2172,7 @@ main(int argc, char *argv[]) } ARGEND; run: + plumbinit(); if (argc > 0) /* eat all remaining arguments */ opt_cmd = argv;