Another copy of my dotfiles. Because I don't completely trust GitHub.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.3 KiB

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