st-newterm-0.9.diff - sites - public wiki contents of suckless.org
 (HTM) git clone git://git.suckless.org/sites
 (DIR) Log
 (DIR) Files
 (DIR) Refs
       ---
       st-newterm-0.9.diff (2756B)
       ---
            1 From e1cf625f4f225a007a5835421896089669195e02 Mon Sep 17 00:00:00 2001
            2 From: meator <meator.dev@gmail.com>
            3 Date: Wed, 26 Oct 2022 13:05:38 +0200
            4 Subject: [PATCH] Add shortcut to spawn new terminal in the current dir
            5 
            6 This commit is inspired by Santtu's st-newterm-20220221-0.8.5.diff. It
            7 removes the unused res variable, it makes use of _exit() instead of
            8 exit() and it replaces execlp() with execl() (PATH searching is useless
            9 when the path is absolute).
           10 ---
           11  config.def.h |  1 +
           12  st.c         | 38 ++++++++++++++++++++++++++++++++++++++
           13  st.h         |  1 +
           14  3 files changed, 40 insertions(+)
           15 
           16 diff --git a/config.def.h b/config.def.h
           17 index 91ab8ca..7c75246 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 +        { TERMMOD,              XK_Return,      newterm,        {.i =  0} },
           25  };
           26  
           27  /*
           28 diff --git a/st.c b/st.c
           29 index 62def59..0261283 100644
           30 --- a/st.c
           31 +++ b/st.c
           32 @@ -20,6 +20,8 @@
           33  #include "st.h"
           34  #include "win.h"
           35  
           36 +extern char *argv0;
           37 +
           38  #if   defined(__linux)
           39   #include <pty.h>
           40  #elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
           41 @@ -153,6 +155,7 @@ typedef struct {
           42  } STREscape;
           43  
           44  static void execsh(char *, char **);
           45 +static int chdir_by_pid(pid_t pid);
           46  static void stty(char **);
           47  static void sigchld(int);
           48  static void ttywriteraw(const char *, size_t);
           49 @@ -806,6 +809,7 @@ ttynew(const char *line, char *cmd, const char *out, char **args)
           50                  if (pledge("stdio rpath tty proc", NULL) == -1)
           51                          die("pledge\n");
           52  #endif
           53 +                fcntl(m, F_SETFD, FD_CLOEXEC);
           54                  close(s);
           55                  cmdfd = m;
           56                  signal(SIGCHLD, sigchld);
           57 @@ -1054,6 +1058,40 @@ tswapscreen(void)
           58          tfulldirt();
           59  }
           60  
           61 +void
           62 +newterm(const Arg* a)
           63 +{
           64 +        switch (fork()) {
           65 +        case -1:
           66 +                die("fork failed: %s\n", strerror(errno));
           67 +                break;
           68 +        case 0:
           69 +                switch (fork()) {
           70 +                case -1:
           71 +                        fprintf(stderr, "fork failed: %s\n", strerror(errno));
           72 +                        _exit(1);
           73 +                        break;
           74 +                case 0:
           75 +                        chdir_by_pid(pid);
           76 +                        execl("/proc/self/exe", argv0, NULL);
           77 +                        _exit(1);
           78 +                        break;
           79 +                default:
           80 +                        _exit(0);
           81 +                }
           82 +        default:
           83 +                wait(NULL);
           84 +        }
           85 +}
           86 +
           87 +static int
           88 +chdir_by_pid(pid_t pid)
           89 +{
           90 +        char buf[32];
           91 +        snprintf(buf, sizeof buf, "/proc/%ld/cwd", (long)pid);
           92 +        return chdir(buf);
           93 +}
           94 +
           95  void
           96  tscrolldown(int orig, int n)
           97  {
           98 diff --git a/st.h b/st.h
           99 index fd3b0d8..f2b03b0 100644
          100 --- a/st.h
          101 +++ b/st.h
          102 @@ -81,6 +81,7 @@ void die(const char *, ...);
          103  void redraw(void);
          104  void draw(void);
          105  
          106 +void newterm(const Arg *);
          107  void printscreen(const Arg *);
          108  void printsel(const Arg *);
          109  void sendbreak(const Arg *);
          110 -- 
          111 2.38.0
          112