Add shortcut to spawn new terminal in the current dir - st - personal variant of st (HTM) git clone https://git.drkhsh.at/st.git (DIR) Log (DIR) Files (DIR) Refs (DIR) README (DIR) LICENSE --- (DIR) commit bc2834356ed1bbf2cdfb7bb3cf307568e6235539 (DIR) parent 60ff062e41862ada6d57645a94bd7931aee87e37 (HTM) Author: drkhsh <me@drkhsh.at> Date: Wed, 10 Jan 2024 10:11:10 +0100 Add shortcut to spawn new terminal in the current dir This commit is inspired by Santtu's st-newterm-20220221-0.8.5.diff. It removes the unused res variable, it makes use of _exit() instead of exit() and it replaces execlp() with execl() (PATH searching is useless when the path is absolute). https://st.suckless.org/patches/newterm/ Diffstat: M config.def.h | 1 + M st.c | 38 +++++++++++++++++++++++++++++++ M st.h | 1 + 3 files changed, 40 insertions(+), 0 deletions(-) --- (DIR) diff --git a/config.def.h b/config.def.h @@ -203,6 +203,7 @@ static Shortcut shortcuts[] = { { TERMMOD, XK_Num_Lock, numlock, {.i = 0} }, { ShiftMask, XK_Page_Up, kscrollup, {.i = -1} }, { ShiftMask, XK_Page_Down, kscrolldown, {.i = -1} }, + { TERMMOD, XK_Return, newterm, {.i = 0} }, }; /* (DIR) diff --git a/st.c b/st.c @@ -20,6 +20,8 @@ #include "st.h" #include "win.h" +extern char *argv0; + #if defined(__linux) #include <pty.h> #elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__) @@ -160,6 +162,7 @@ typedef struct { } STREscape; static void execsh(char *, char **); +static int chdir_by_pid(pid_t pid); static void stty(char **); static void sigchld(int); static void ttywriteraw(const char *, size_t); @@ -813,6 +816,7 @@ ttynew(const char *line, char *cmd, const char *out, char **args) if (pledge("stdio rpath tty proc", NULL) == -1) die("pledge\n"); #endif + fcntl(m, F_SETFD, FD_CLOEXEC); close(s); cmdfd = m; signal(SIGCHLD, sigchld); @@ -1098,6 +1102,40 @@ kscrollup(const Arg* a) } void +newterm(const Arg* a) +{ + switch (fork()) { + case -1: + die("fork failed: %s\n", strerror(errno)); + break; + case 0: + switch (fork()) { + case -1: + fprintf(stderr, "fork failed: %s\n", strerror(errno)); + _exit(1); + break; + case 0: + chdir_by_pid(pid); + execl("/proc/self/exe", argv0, NULL); + _exit(1); + break; + default: + _exit(0); + } + default: + wait(NULL); + } +} + +static int +chdir_by_pid(pid_t pid) +{ + char buf[32]; + snprintf(buf, sizeof buf, "/proc/%ld/cwd", (long)pid); + return chdir(buf); +} + +void tscrolldown(int orig, int n, int copyhist) { int i; (DIR) diff --git a/st.h b/st.h @@ -83,6 +83,7 @@ void draw(void); void kscrolldown(const Arg *); void kscrollup(const Arg *); +void newterm(const Arg *); void printscreen(const Arg *); void printsel(const Arg *); void sendbreak(const Arg *);