]> git.mdlowis.com Git - projs/tide.git/commitdiff
Tweaked command execution routines to eliminate commands hanging the editor waiting...
authorMichael D. Lowis <mike.lowis@gentex.com>
Thu, 15 Dec 2016 20:41:50 +0000 (15:41 -0500)
committerMichael D. Lowis <mike.lowis@gentex.com>
Thu, 15 Dec 2016 20:41:50 +0000 (15:41 -0500)
inc/edit.h
libedit/exec.c
xedit.c

index cef36be9fbd38618d28f3b6c3ffda61e08da5b9e..cbe52303a2ffd9dd3de9d7937428e2a8f04097af 100644 (file)
@@ -166,6 +166,7 @@ void view_indent(View* view, int dir);
 
 /* Command Executions
  *****************************************************************************/
+void cmdreap(void);
 int cmdrun(char** cmd, char** err);
 char* cmdread(char** cmd, char** err);
 void cmdwrite(char** cmd, char* text, char** err);
index be0ee21dc4e1f5b75e636c885a2abbe0d1842fd4..099607864651ece664c9a32c24646dbe2ab3b4df 100644 (file)
@@ -5,6 +5,8 @@
 #include <unistd.h>
 #include <sys/wait.h>
 
+static uint NumChildren = 0;
+
 #define PIPE_READ 0
 #define PIPE_WRITE 1
 
@@ -51,10 +53,9 @@ static int execute(char** cmd, Process* proc) {
     return proc->pid;
 }
 
-static void detach(Process* proc) {
-    close(proc->in);
-    close(proc->out);
-    close(proc->err);
+void cmdreap(void) {
+    while(NumChildren && (waitpid(-1, NULL, WNOHANG) > 0))
+        NumChildren--;
 }
 
 int cmdrun(char** cmd, char** err) {
@@ -63,8 +64,11 @@ int cmdrun(char** cmd, char** err) {
         perror("failed to execute");
         return -1;
     }
+    NumChildren++;
     if (err) *err = fdgets(proc.err);
-    detach(&proc);
+    close(proc.in);
+    close(proc.out);
+    close(proc.err);
     return proc.pid;
 }
 
@@ -74,9 +78,11 @@ char* cmdread(char** cmd, char** err) {
         perror("failed to execute");
         return NULL;
     }
+    close(proc.in);
     char* str = fdgets(proc.out);
+    close(proc.out);
     if (err) *err = fdgets(proc.err);
-    detach(&proc);
+    close(proc.err);
     waitpid(proc.pid, NULL, 0);
     return str;
 }
@@ -91,8 +97,10 @@ void cmdwrite(char** cmd, char* text, char** err) {
         perror("failed to write");
         return;
     }
+    close(proc.in);
     if (err) *err = fdgets(proc.err);
-    detach(&proc);
+    close(proc.err);
+    close(proc.out);
     waitpid(proc.pid, NULL, 0);
 }
 
@@ -108,8 +116,9 @@ char* cmdwriteread(char** cmd, char* text, char** err) {
     }
     close(proc.in);
     char* str = fdgets(proc.out);
+    close(proc.out);
     if (err) *err = fdgets(proc.err);
-    detach(&proc);
+    close(proc.err);
     waitpid(proc.pid, NULL, 0);
     return str;
 }
diff --git a/xedit.c b/xedit.c
index d81ebc32e64af4125966f8a0d06d84a02a98a40d..f8ca2f09e06049ba0a48aa5adb2d154e510d16c0 100644 (file)
--- a/xedit.c
+++ b/xedit.c
@@ -376,6 +376,7 @@ static void redraw(int width, int height) {
     draw_status(CLR_BASE1, (width - 4) / x11_font_width(Font));
     draw_region(TAGS);
     draw_region(EDIT);
+    cmdreap(); // cleanup any zombie child processes
 }
 #endif