sites

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

surf-2.0-externalpipe.diff (2379B)


      1 diff --git a/surf.c b/surf.c
      2 index 93a1629..ba53b94 100644
      3 --- a/surf.c
      4 +++ b/surf.c
      5 @@ -217,6 +217,7 @@ static void togglefullscreen(Client *c, const Arg *a);
      6  static void togglecookiepolicy(Client *c, const Arg *a);
      7  static void toggleinspector(Client *c, const Arg *a);
      8  static void find(Client *c, const Arg *a);
      9 +static void externalpipe(Client *c, const Arg *a);
     10  
     11  /* Buttons */
     12  static void clicknavigate(Client *c, const Arg *a, WebKitHitTestResult *h);
     13 @@ -241,6 +242,80 @@ char *argv0;
     14  /* configuration, allows nested code to access above variables */
     15  #include "config.h"
     16  
     17 +static void
     18 +externalpipe_execute(char* buffer, Arg *arg) {
     19 +	int to[2];
     20 +	void (*oldsigpipe)(int);
     21 +
     22 +	if (pipe(to) == -1)
     23 +		return;
     24 +
     25 +	switch (fork()) {
     26 +	case -1:
     27 +		close(to[0]);
     28 +		close(to[1]);
     29 +		return;
     30 +	case 0:
     31 +		dup2(to[0], STDIN_FILENO); close(to[0]); close(to[1]);
     32 +		execvp(((char **)arg->v)[0], (char **)arg->v);
     33 +		fprintf(stderr, "st: execvp %s\n", ((char **)arg->v)[0]);
     34 +		perror("failed");
     35 +		exit(0);
     36 +	}
     37 +
     38 +	close(to[0]);
     39 +	oldsigpipe = signal(SIGPIPE, SIG_IGN);
     40 +	write(to[1], buffer, strlen(buffer));
     41 +	close(to[1]);
     42 +	signal(SIGPIPE, oldsigpipe);
     43 +}
     44 +
     45 +static void
     46 +externalpipe_resource_done(WebKitWebResource *r, GAsyncResult *s, Arg *arg)
     47 +{
     48 +	GError *gerr = NULL;
     49 +	guchar *buffer = webkit_web_resource_get_data_finish(r, s, NULL, &gerr);
     50 +	if (gerr == NULL) {
     51 +		externalpipe_execute((char *) buffer, arg);
     52 +	} else {
     53 +		g_error_free(gerr);
     54 +	}
     55 +	g_free(buffer);
     56 +}
     57 +
     58 +static void
     59 +externalpipe_js_done(WebKitWebView *wv, GAsyncResult *s, Arg *arg)
     60 +{
     61 +	WebKitJavascriptResult *j = webkit_web_view_run_javascript_finish(
     62 +		wv, s, NULL);
     63 +	if (!j) {
     64 +		return;
     65 +	}
     66 +	JSCValue *v = webkit_javascript_result_get_js_value(j);
     67 +	if (jsc_value_is_string(v)) {
     68 +		char *buffer = jsc_value_to_string(v);
     69 +		externalpipe_execute(buffer, arg);
     70 +		g_free(buffer);
     71 +	}
     72 +	webkit_javascript_result_unref(j);
     73 +}
     74 +
     75 +void
     76 +externalpipe(Client *c, const Arg *arg)
     77 +{
     78 +	if (curconfig[JavaScript].val.i) {
     79 +		webkit_web_view_run_javascript(
     80 +			c->view, "window.document.documentElement.outerHTML",
     81 +			NULL, externalpipe_js_done, arg);
     82 +	} else {
     83 +		WebKitWebResource *resource = webkit_web_view_get_main_resource(c->view);
     84 +		if (resource != NULL) {
     85 +			webkit_web_resource_get_data(
     86 +				resource, NULL, externalpipe_resource_done, arg);
     87 +		}
     88 +	}
     89 +}
     90 +
     91  void
     92  usage(void)
     93  {