Spawn.vala
[gitlive] / Spawn.vala
index c1f4e63..708c98c 100644 (file)
@@ -60,7 +60,7 @@ static int main (string[] args) {
 public delegate void SpawnOutput(string line);
 public delegate void SpawnErr(string line);
 public delegate string SpawnInput();
-public delegate void SpawnFinish(int result);
+public delegate void SpawnFinish(int result, string output);
  
 
 public class  SpawnConfig {
@@ -74,7 +74,7 @@ public class  SpawnConfig {
     public SpawnOutput output;
     public SpawnErr stderr;
     public SpawnInput input;
-    public SpawnFinish finish;
     // defaults..
     public SpawnConfig(string cwd,
             string[] args,
@@ -93,18 +93,22 @@ public class  SpawnConfig {
         input = null;
         
     }
+    
+    public void onFinish( SpawnFinish? finish ) {
+               this.finish = finish;
+    }
 
  
     public void setHandlers(
             SpawnOutput? output,
             SpawnErr? stderr,
-            SpawnInput? input,
-            SpawnFinish? finish
+            SpawnInput? input 
          ) {
         this.output = output;
         this.stderr = stderr;
         this.input = input;
-        this.finish = finish;
+        
     }
     
     
@@ -152,7 +156,9 @@ public class Spawn : Object
         if (this.cfg.args.length < 0) {
             throw new SpawnError.NO_ARGS("No arguments");
         }
-        this.run();
+        if (!this.cfg.async) {
+               this.run((res, output) => { });
+        }
     
     }
 
@@ -204,7 +210,7 @@ public class Spawn : Object
      * result is applied to object properties (eg. '?' or 'stderr')
      * @returns {Object} self.
      */
-    public void run() throws SpawnError, GLib.SpawnError, GLib.IOChannelError
+    public void run(unowned SpawnFinish  finished_cb) throws SpawnError, GLib.SpawnError, GLib.IOChannelError
     {
         
          
@@ -318,7 +324,7 @@ public class Spawn : Object
                     //but... let's close input now..
                     this.in_ch.shutdown(true);
                     this.in_ch = null;
-                     
+                    
                     
                 } catch (Error e) {
                     this.tidyup();
@@ -342,7 +348,7 @@ public class Spawn : Object
              //if (this.cfg.debug) {
              //  
              // }
-               this.ctx = new MainLoop ();
+               this.ctx =   MainLoop ();
             this.ctx.run(); // wait fore exit?
             
             //print("main_loop done!");
@@ -352,17 +358,16 @@ public class Spawn : Object
         
         
         if (this.cfg.exceptions && this.result != 0) {
-                       
-                       var errstr = this.output;
-                       errstr += errstr.length > 0 ? "\n" : "";
+                       var errstr = string.joinv(" ", this.cfg.args) + "\n";
+                       errstr += this.output;
+                       errstr += this.output.length > 0 ? "\n" : "";
                        errstr += this.stderr;
-            throw new SpawnError.EXECUTE_ERROR(errstr);
+                       //print("Throwing execute error:%s\n", errstr);
+            throw   SpawnError.EXECUTE_ERROR(errstr);
             //this.toString = function() { return this.stderr; };
             ///throw new Exception this; // we throw self...
         }
-        
         // finally throw, or return self..
-        
         return;
     
     }
@@ -457,8 +462,10 @@ public class Spawn : Object
                return false;
                 
             }
-            
-                       //print("got buffer of %s\n", buffer);
+            if (buffer == null) {
+                       return false;
+               }
+            //print("got buffer of %s\n", buffer);
             // print('status: '  +JSON.stringify(status));
             // print(JSON.stringify(x));
              switch(status) {
@@ -479,7 +486,7 @@ public class Spawn : Object
                     }
                     //_this[prop] += x.str_return;
                     //if (this.cfg.debug) {
-                        stdout.printf("%s : %s", prop , buffer);
+                       // stdout.printf("%s : %s", prop , buffer);
                     //}
                     if (this.cfg.async) {