sites

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

simple_plumb-0.8.5.diff (3335B)


      1 diff --git a/config.def.h b/config.def.h
      2 index 91ab8ca..d0343e3 100644
      3 --- a/config.def.h
      4 +++ b/config.def.h
      5 @@ -472,3 +472,9 @@ static char ascii_printable[] =
      6  	" !\"#$%&'()*+,-./0123456789:;<=>?"
      7  	"@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_"
      8  	"`abcdefghijklmnopqrstuvwxyz{|}~";
      9 +
     10 +/*
     11 + * plumb_cmd is run on mouse button 3 click, with first NULL set to
     12 + * current selection and with cwd set to the cwd of the active shell
     13 + */
     14 +static char *plumb_cmd[] = {"plumb", "-m", NULL, NULL};
     15 diff --git a/st.c b/st.c
     16 index 51049ba..11756c4 100644
     17 --- a/st.c
     18 +++ b/st.c
     19 @@ -27,6 +27,9 @@
     20  #elif defined(__FreeBSD__) || defined(__DragonFly__)
     21   #include <libutil.h>
     22  #endif
     23 +#if defined(__OpenBSD__)
     24 + #include <sys/sysctl.h>
     25 +#endif
     26  
     27  /* Arbitrary sizes */
     28  #define UTF_INVALID   0xFFFD
     29 @@ -231,6 +234,22 @@ static const uchar utfmask[UTF_SIZ + 1] = {0xC0, 0x80, 0xE0, 0xF0, 0xF8};
     30  static const Rune utfmin[UTF_SIZ + 1] = {       0,    0,  0x80,  0x800,  0x10000};
     31  static const Rune utfmax[UTF_SIZ + 1] = {0x10FFFF, 0x7F, 0x7FF, 0xFFFF, 0x10FFFF};
     32  
     33 +int
     34 +subprocwd(char *path)
     35 +{
     36 +#if   defined(__linux)
     37 +	if (snprintf(path, PATH_MAX, "/proc/%d/cwd", pid) < 0)
     38 +		return -1;
     39 +	return 0;
     40 +#elif defined(__OpenBSD__)
     41 +	size_t sz = PATH_MAX;
     42 +	int name[3] = {CTL_KERN, KERN_PROC_CWD, pid};
     43 +	if (sysctl(name, 3, path, &sz, 0, 0) == -1)
     44 +		return -1;
     45 +	return 0;
     46 +#endif
     47 +}
     48 +
     49  ssize_t
     50  xwrite(int fd, const char *s, size_t len)
     51  {
     52 @@ -810,7 +829,7 @@ ttynew(const char *line, char *cmd, const char *out, char **args)
     53  		break;
     54  	default:
     55  #ifdef __OpenBSD__
     56 -		if (pledge("stdio rpath tty proc", NULL) == -1)
     57 +		if (pledge("stdio rpath tty proc ps exec", NULL) == -1)
     58  			die("pledge\n");
     59  #endif
     60  		close(s);
     61 diff --git a/st.h b/st.h
     62 index 519b9bd..8255556 100644
     63 --- a/st.h
     64 +++ b/st.h
     65 @@ -111,6 +111,8 @@ void *xmalloc(size_t);
     66  void *xrealloc(void *, size_t);
     67  char *xstrdup(const char *);
     68  
     69 +int subprocwd(char *);
     70 +
     71  int xgetcolor(int x, unsigned char *r, unsigned char *g, unsigned char *b);
     72  
     73  /* config.h globals */
     74 diff --git a/x.c b/x.c
     75 index cd96575..5d3948a 100644
     76 --- a/x.c
     77 +++ b/x.c
     78 @@ -5,6 +5,7 @@
     79  #include <locale.h>
     80  #include <signal.h>
     81  #include <sys/select.h>
     82 +#include <sys/wait.h>
     83  #include <time.h>
     84  #include <unistd.h>
     85  #include <libgen.h>
     86 @@ -216,6 +217,7 @@ static void (*handler[LASTEvent])(XEvent *) = {
     87  };
     88  
     89  /* Globals */
     90 +static int plumbsel;
     91  static DC dc;
     92  static XWindow xw;
     93  static XSelection xsel;
     94 @@ -694,6 +696,37 @@ xsetsel(char *str)
     95  	setsel(str, CurrentTime);
     96  }
     97  
     98 +void
     99 +plumbinit()
    100 +{
    101 +	for(plumbsel = 0; plumb_cmd[plumbsel]; plumbsel++);
    102 +}
    103 +
    104 +void
    105 +plumb(char *sel) {
    106 +	if (sel == NULL)
    107 +		return;
    108 +	char cwd[PATH_MAX];
    109 +	pid_t child;
    110 +	if (subprocwd(cwd) != 0)
    111 +		return;
    112 +
    113 +	plumb_cmd[plumbsel] = sel;
    114 +
    115 +	switch(child = fork()) {
    116 +		case -1:
    117 +			return;
    118 +		case 0:
    119 +			if (chdir(cwd) != 0)
    120 +				exit(1);
    121 +			if (execvp(plumb_cmd[0], plumb_cmd) == -1)
    122 +				exit(1);
    123 +			exit(0);
    124 +		default:
    125 +			waitpid(child, NULL, 0);
    126 +	}
    127 +}
    128 +
    129  void
    130  brelease(XEvent *e)
    131  {
    132 @@ -711,6 +744,8 @@ brelease(XEvent *e)
    133  		return;
    134  	if (e->xbutton.button == Button1)
    135  		mousesel(e, 1);
    136 +	else if (e->xbutton.button == Button3)
    137 +		plumb(xsel.primary);
    138  }
    139  
    140  void
    141 @@ -2076,6 +2111,7 @@ main(int argc, char *argv[])
    142  	} ARGEND;
    143  
    144  run:
    145 +	plumbinit();
    146  	if (argc > 0) /* eat all remaining arguments */
    147  		opt_cmd = argv;
    148