st-openclipboard-20220217-0.8.5.diff (2268B)
1 From 1e752ae33791652d050e7d03e6d8e9df3fb459e3 Mon Sep 17 00:00:00 2001 2 From: Santtu Lakkala <inz@inz.fi> 3 Date: Thu, 17 Feb 2022 18:11:35 +0200 4 Subject: [PATCH] Open url from clipboard 5 6 Based on the previous versions of the patch, but uses double-fork/execlp 7 instead of system() to avoid potential shell escaping issues and make 8 the command line building unnecessary. 9 --- 10 config.def.h | 1 + 11 st.c | 2 +- 12 st.h | 1 + 13 x.c | 21 +++++++++++++++++++++ 14 4 files changed, 24 insertions(+), 1 deletion(-) 15 16 diff --git a/config.def.h b/config.def.h 17 index 91ab8ca..a696ec7 100644 18 --- a/config.def.h 19 +++ b/config.def.h 20 @@ -201,6 +201,7 @@ static Shortcut shortcuts[] = { 21 { TERMMOD, XK_Y, selpaste, {.i = 0} }, 22 { ShiftMask, XK_Insert, selpaste, {.i = 0} }, 23 { TERMMOD, XK_Num_Lock, numlock, {.i = 0} }, 24 + { MODKEY, XK_o, opencopied, {.v = "xdg-open"} }, 25 }; 26 27 /* 28 diff --git a/st.c b/st.c 29 index 51049ba..4fbc5c8 100644 30 --- a/st.c 31 +++ b/st.c 32 @@ -810,7 +810,7 @@ ttynew(const char *line, char *cmd, const char *out, char **args) 33 break; 34 default: 35 #ifdef __OpenBSD__ 36 - if (pledge("stdio rpath tty proc", NULL) == -1) 37 + if (pledge("stdio rpath tty proc exec", NULL) == -1) 38 die("pledge\n"); 39 #endif 40 close(s); 41 diff --git a/st.h b/st.h 42 index 519b9bd..3b4b395 100644 43 --- a/st.h 44 +++ b/st.h 45 @@ -81,6 +81,7 @@ void die(const char *, ...); 46 void redraw(void); 47 void draw(void); 48 49 +void opencopied(const Arg *); 50 void printscreen(const Arg *); 51 void printsel(const Arg *); 52 void sendbreak(const Arg *); 53 diff --git a/x.c b/x.c 54 index 8a16faa..3a4e5f0 100644 55 --- a/x.c 56 +++ b/x.c 57 @@ -5,6 +5,7 @@ 58 #include <locale.h> 59 #include <signal.h> 60 #include <sys/select.h> 61 +#include <sys/wait.h> 62 #include <time.h> 63 #include <unistd.h> 64 #include <libgen.h> 65 @@ -2078,3 +2079,23 @@ run: 66 67 return 0; 68 } 69 + 70 +void 71 +opencopied(const Arg *arg) 72 +{ 73 + char * const clip = xsel.clipboard; 74 + pid_t chpid; 75 + 76 + if(!clip) { 77 + fprintf(stderr, "Warning: nothing copied to clipboard\n"); 78 + return; 79 + } 80 + 81 + if ((chpid = fork()) == 0) { 82 + if (fork() == 0) 83 + execlp(arg->v, arg->v, clip, NULL); 84 + exit(1); 85 + } 86 + if (chpid > 0) 87 + waitpid(chpid, NULL, 0); 88 +} 89 -- 90 2.32.0 91