sites

public wiki contents of suckless.org
git clone git://git.suckless.org/sites
Log | Files | Refs

st-externalpipe-externalpipein-20200418-5e6a125.diff (3567B)


      1 From 5e6a125f14d050945be6add49bf5940277b4278f Mon Sep 17 00:00:00 2001
      2 From: Christian Tenllado <ctenllado@gmail.com>
      3 Date: Sat, 18 Apr 2020 20:45:40 +0200
      4 Subject: [PATCH] externalpipe and externalpipein
      5 
      6 This patch must be applied on the externalpipe patch. It adds the
      7 function externalpipein to redirect the standard output of the external
      8 command to the slave size of the pty, that is, as if the external
      9 program had been manually executed on the terminal. It can be used to
     10 send desired escape sequences to the terminal with a shortcut.
     11 
     12 I created the patch to make use of the dynamic-colors program
     13 (https://github.com/sos4nt/dynamic-colors) that uses the OSC escape
     14 sequences to change the colors of the terminal. The program keeps the
     15 last colorscheme selected in a file, so you can use it to select the
     16 colorscheme for all newly opened terminals from that moment on. If you
     17 want to change the color of the background and foreground independently
     18 from the palette, you have to merge in the patch for the OSC escape
     19 sequences 10, 11, and 12.
     20 
     21 This patch includes the changes of the externalpipe sigaction patch to
     22 prevent reseting the signal handler for SIGCHLD when the proces of the
     23 external command exits.
     24 ---
     25  st.c | 28 ++++++++++++++++++++++++----
     26  st.h |  1 +
     27  2 files changed, 25 insertions(+), 4 deletions(-)
     28 
     29 diff --git a/st.c b/st.c
     30 index ab291ac..ac19ebf 100644
     31 --- a/st.c
     32 +++ b/st.c
     33 @@ -225,6 +225,7 @@ static CSIEscape csiescseq;
     34  static STREscape strescseq;
     35  static int iofd = 1;
     36  static int cmdfd;
     37 +static int csdfd;
     38  static pid_t pid;
     39  
     40  static uchar utfbyte[UTF_SIZ + 1] = {0x80,    0, 0xC0, 0xE0, 0xF0};
     41 @@ -712,12 +713,14 @@ sigchld(int a)
     42  	int stat;
     43  	pid_t p;
     44  
     45 -	if ((p = waitpid(pid, &stat, WNOHANG)) < 0)
     46 +	if ((p = waitpid(-1, &stat, WNOHANG)) < 0)
     47  		die("waiting for pid %hd failed: %s\n", pid, strerror(errno));
     48  
     49  	if (pid != p)
     50  		return;
     51  
     52 +	close(csdfd);
     53 +
     54  	if (WIFEXITED(stat) && WEXITSTATUS(stat))
     55  		die("child exited with status %d\n", WEXITSTATUS(stat));
     56  	else if (WIFSIGNALED(stat))
     57 @@ -753,6 +756,7 @@ int
     58  ttynew(char *line, char *cmd, char *out, char **args)
     59  {
     60  	int m, s;
     61 +	struct sigaction sa;
     62  
     63  	if (out) {
     64  		term.mode |= MODE_PRINT;
     65 @@ -802,9 +806,12 @@ ttynew(char *line, char *cmd, char *out, char **args)
     66  		if (pledge("stdio rpath tty proc", NULL) == -1)
     67  			die("pledge\n");
     68  #endif
     69 -		close(s);
     70 +		csdfd = s;
     71  		cmdfd = m;
     72 -		signal(SIGCHLD, sigchld);
     73 +		memset(&sa, 0, sizeof(sa));
     74 +		sigemptyset(&sa.sa_mask);
     75 +		sa.sa_handler = sigchld;
     76 +		sigaction(SIGCHLD, &sa, NULL);
     77  		break;
     78  	}
     79  	return cmdfd;
     80 @@ -1920,7 +1927,7 @@ strparse(void)
     81  }
     82  
     83  void
     84 -externalpipe(const Arg *arg)
     85 +extpipe(const Arg *arg, int in)
     86  {
     87  	int to[2];
     88  	char buf[UTF_SIZ];
     89 @@ -1940,6 +1947,9 @@ externalpipe(const Arg *arg)
     90  		dup2(to[0], STDIN_FILENO);
     91  		close(to[0]);
     92  		close(to[1]);
     93 +		if (in)
     94 +			dup2(csdfd, STDOUT_FILENO);
     95 +		close(csdfd);
     96  		execvp(((char **)arg->v)[0], (char **)arg->v);
     97  		fprintf(stderr, "st: execvp %s\n", ((char **)arg->v)[0]);
     98  		perror("failed");
     99 @@ -1972,6 +1982,16 @@ externalpipe(const Arg *arg)
    100  	signal(SIGPIPE, oldsigpipe);
    101  }
    102  
    103 +void
    104 +externalpipe(const Arg *arg) {
    105 +	extpipe(arg, 0);
    106 +}
    107 +
    108 +void
    109 +externalpipein(const Arg *arg) {
    110 +	extpipe(arg, 1);
    111 +}
    112 +
    113  void
    114  strdump(void)
    115  {
    116 diff --git a/st.h b/st.h
    117 index fe84fda..bbf38fa 100644
    118 --- a/st.h
    119 +++ b/st.h
    120 @@ -82,6 +82,7 @@ void redraw(void);
    121  void draw(void);
    122  
    123  void externalpipe(const Arg *);
    124 +void externalpipein(const Arg *);
    125  void printscreen(const Arg *);
    126  void printsel(const Arg *);
    127  void sendbreak(const Arg *);
    128 -- 
    129 2.20.1
    130