Spawn.vala
[gitlive] / Spawn.vala
index e927c4f..b5df8c8 100644 (file)
@@ -241,6 +241,8 @@ public class Spawn : Object
             stdout.printf("PID: %d" ,this.pid);
         }
         
+        this.ref(); // additional ref - cleared on tidyup...
+        
         this.in_ch = new GLib.IOChannel.unix_new(standard_input);
         this.out_ch = new GLib.IOChannel.unix_new(standard_output);
         this.err_ch = new GLib.IOChannel.unix_new(standard_error);
@@ -249,7 +251,7 @@ public class Spawn : Object
         
         
             
-                  // using NONBLOCKING only works if io_add_watch
+        // using NONBLOCKING only works if io_add_watch
           //returns true/false in right conditions
           this.in_ch.set_flags (GLib.IOFlags.NONBLOCK);
           this.out_ch.set_flags (GLib.IOFlags.NONBLOCK);
@@ -363,6 +365,7 @@ public class Spawn : Object
 
     private void tidyup()
     {
+        this.unref();
         if (this.pid > -1) {
             Process.close_pid(this.pid); // hopefully kills it..
             this.pid = -1;
@@ -412,8 +415,7 @@ public class Spawn : Object
         return str.length;
         
     }
-
-
     
     /**
      * read from pipe and call appropriate listerner and add to output or stderr string.
@@ -424,7 +426,9 @@ public class Spawn : Object
     {
         string prop = (ch == this.out_ch) ? "output" : "stderr";
        // print("prop: " + prop);
-
+        if (this.pid < 0) {
+            return false; // spawn complete + closed... can't read any more.
+        }
         
         //print(JSON.stringify(ch, null,4));
         while (true) {
@@ -433,10 +437,17 @@ public class Spawn : Object
             size_t len;
             IOStatus status;
             try {
+                               var cond = ch.get_buffer_condition();
+                               if ((cond & GLib.IOCondition.ERR) > 0) {
+                                       return false;
+                               }
+                               if ((cond & GLib.IOCondition.IN) < 1) {
+                                       return false;
+                               }
                 status = ch.read_line( out buffer,  out len,  out term_pos );
             } catch (Error e) {
                 //FIXme
-                break; // ??
+               return false;
                 
             }