sites

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

st-externalpipe-20170608-b331da5.diff (2413B)


      1 diff --git a/st.c b/st.c
      2 index 8d4a9f2..3b05f0b 100644
      3 --- a/st.c
      4 +++ b/st.c
      5 @@ -138,6 +138,7 @@ static void printscreen(const Arg *) ;
      6  static void iso14755(const Arg *);
      7  static void toggleprinter(const Arg *);
      8  static void sendbreak(const Arg *);
      9 +static void externalpipe(const Arg *);
     10  
     11  /* config.h for applying patches and the configuration. */
     12  #include "config.h"
     13 @@ -2344,6 +2345,59 @@ eschandle(uchar ascii)
     14  }
     15  
     16  void
     17 +externalpipe(const Arg *arg)
     18 +{
     19 +	int to[2];
     20 +	char buf[UTF_SIZ];
     21 +	void (*oldsigpipe)(int);
     22 +	Glyph *bp, *end;
     23 +	int lastpos, n, newline;
     24 +
     25 +	if (pipe(to) == -1)
     26 +		return;
     27 +
     28 +	switch (fork()) {
     29 +	case -1:
     30 +		close(to[0]);
     31 +		close(to[1]);
     32 +		return;
     33 +	case 0:
     34 +		dup2(to[0], STDIN_FILENO);
     35 +		close(to[0]);
     36 +		close(to[1]);
     37 +		execvp(((char **)arg->v)[0], (char **)arg->v);
     38 +		fprintf(stderr, "st: execvp %s ", ((char **)arg->v)[0]);
     39 +		perror("failed");
     40 +		exit(0);
     41 +	}
     42 +
     43 +	close(to[0]);
     44 +	/* ignore sigpipe for now, in case child exits early */
     45 +	oldsigpipe = signal(SIGPIPE, SIG_IGN);
     46 +	newline = 0;
     47 +	for (n = 0; n < term.row; n++) {
     48 +		bp = term.line[n];
     49 +		lastpos = MIN(tlinelen(n) + 1, term.col) - 1;
     50 +		if (lastpos < 0)
     51 +			break;
     52 +		end = &bp[lastpos + 1];
     53 +		for (; bp < end; ++bp)
     54 +			if (xwrite(to[1], buf, utf8encode(bp->u, buf)) < 0)
     55 +				break;
     56 +		if ((newline = term.line[n][lastpos].mode & ATTR_WRAP))
     57 +			continue;
     58 +		if (xwrite(to[1], "\n", 1) < 0)
     59 +			break;
     60 +		newline = 0;
     61 +	}
     62 +	if (newline)
     63 +		(void)xwrite(to[1], "\n", 1);
     64 +	close(to[1]);
     65 +	/* restore */
     66 +	signal(SIGPIPE, oldsigpipe);
     67 +}
     68 +
     69 +void
     70  tputc(Rune u)
     71  {
     72  	char c[UTF_SIZ];
     73 diff --git a/win.h b/win.h
     74 index 428111c..a012a24 100644
     75 --- a/win.h
     76 +++ b/win.h
     77 @@ -28,3 +28,5 @@ void xresize(int, int);
     78  void xselpaste(void);
     79  unsigned long xwinid(void);
     80  void xsetsel(char *, Time);
     81 +
     82 +extern char winid[64];
     83 diff --git a/x.c b/x.c
     84 index fbfd350..ff052e6 100644
     85 --- a/x.c
     86 +++ b/x.c
     87 @@ -139,6 +139,7 @@ static void (*handler[LASTEvent])(XEvent *) = {
     88  static DC dc;
     89  static XWindow xw;
     90  static XSelection xsel;
     91 +char winid[64];
     92  
     93  /* Font Ring Cache */
     94  enum {
     95 @@ -915,6 +916,7 @@ xinit(void)
     96  			win.w, win.h, 0, XDefaultDepth(xw.dpy, xw.scr), InputOutput,
     97  			xw.vis, CWBackPixel | CWBorderPixel | CWBitGravity
     98  			| CWEventMask | CWColormap, &xw.attrs);
     99 +	snprintf(winid, LEN(winid), "%lu", (unsigned long)xw.win);
    100  
    101  	memset(&gcvalues, 0, sizeof(gcvalues));
    102  	gcvalues.graphics_exposures = False;