Spawn.vala
[gitlive] / Spawn.vala
index 8763492..ce5c3c3 100644 (file)
@@ -1,7 +1,10 @@
 
+/// # valac  --pkg gio-2.0 --pkg gtk+-3.0  --pkg posix Spawn.vala -o /tmp/Spawn
+
+using GLib;
+using Gtk;
 // compile valac 
 
-/// # valac  --pkg gio-2.0  --pkg posix Spawn.vala -o /tmp/Spawn
 
 
 ///using Gee; // for array list?
@@ -19,7 +22,7 @@ static int main (string[] args) {
 
 //var Gio      = imports.gi.Gio;
 //var GLib      = imports.gi.GLib;
-using Glib;
+
 
 /**
 * @namespace Spawn
@@ -98,6 +101,10 @@ public class  SpawnConfig {
     
 }
 
+errordomain SpawnError {
+    NO_ARGS
+}
+
 /**
  * @class Spawn
  * @param cfg {SpawnConfig} settings - see properties.
@@ -128,16 +135,16 @@ public class Spawn : Object
         this.cfg = cfg;
      
     
-        this.cfg.cwd =  this.cfg.cwd.length || GLib.get_home_dir();
-        if (!this.cfg.args.length) {
-            throw new Error("No arguments");
+        this.cfg.cwd =  this.cfg.cwd.length  < 1 ? GLib.Environment.get_home_dir() : this.cfg.cwd;
+        if (this.cfg.args.length < 0) {
+            throw new SpawnError.NO_ARGS("No arguments");
         }
         this.run();
     
     }
 
     
-    bool ctx = false; // the mainloop ctx.
+    MainLoop ctx = null; // the mainloop ctx.
     
     /**
      * @property output {String} resulting output
@@ -195,10 +202,9 @@ public class Spawn : Object
         int standard_error;
 
 
-        var ret = {};
         
         if (this.cfg.debug) {
-            print("cd " + this.cfg.cwd +";" + string.joinv(" ", this.cfg.args));
+           stdout.printf("cd %s; %s" , this.cfg.cwd , string.joinv(" ", this.cfg.args));
         }
         
         Process.spawn_async_with_pipes (
@@ -218,27 +224,29 @@ public class Spawn : Object
        //print(JSON.stringify(gret));    
          
         if (this.cfg.debug) {
-            print("PID: " + this.pid);
+            
+            stdout.printf("PID: %d" ,this.pid);
         }
          
         ChildWatch.add (this.pid, (w_pid, result) => {
            
-           this.result = result;
-            if (_this.debug) {
-                print("child_watch_add : result: " + result);
+            this.result = result;
+            if (this.cfg.debug) {
+                stdout.printf("child_watch_add : result:%d ", result);
             }
-           
+           
             this.read(this.out_ch);
             this.read(this.err_ch);
             
-                       
+            
             Process.close_pid(this.pid);
             this.pid = -1;
-            if (this.ctx) {
+            if (this.ctx != null) {
                 this.ctx.quit();
+                this.ctx = null;
             }
             this.tidyup();
-           //print("DONE TIDYUP");
+        //print("DONE TIDYUP");
             if (this.cfg.finish) {
                 this.cfg.finish(this.result);
             }
@@ -247,37 +255,37 @@ public class Spawn : Object
                          
         
         
-        this.in_ch = new GLib.IOChannel.unix_new(ret.standard_input);
-        this.out_ch = new GLib.IOChannel.unix_new(ret.standard_output);
-        this.err_ch = new GLib.IOChannel.unix_new(ret.standard_error);
+        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);
         
         // make everything non-blocking!
         
         
+            
+                  // 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);
+          this.err_ch.set_flags (GLib.IOFlags.NONBLOCK);
+                   
       
-                       // 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);
-       this.err_ch.set_flags (GLib.IOFlags.NONBLOCK);
-                    
-
-      
-        // add handlers for output and stderr.
-       
-       this.out_src = this.out_ch.add_watch (
-           IOCondition.OUT | IOCondition.IN  | IOCondition.PRI |  IOCondition.HUP |  IOCondition.ERR  ,
-           (channel, condition) => {
-              return this.read(_this.out_ch);
-           }
-       );
+            
+            // add handlers for output and stderr.
+        
+        this.out_src = this.out_ch.add_watch (
+            IOCondition.OUT | IOCondition.IN  | IOCondition.PRI |  IOCondition.HUP |  IOCondition.ERR  ,
+            (channel, condition) => {
+               return this.read(_this.out_ch);
+            }
+        );
         this.err_src = this.err_ch.add_watch (
            IOCondition.OUT | IOCondition.IN  | IOCondition.PRI |  IOCondition.HUP |  IOCondition.ERR  ,
-           (channel, condition) => {
-              return this.read(_this.err_ch);
-           }
-       );
-          
+            (channel, condition) => {
+               return this.read(_this.err_ch);
+            }
+        );
+              
         
         // call input.. 
         if (this.pid > -1) {
@@ -338,13 +346,13 @@ public class Spawn : Object
             Process.close_pid(this.pid); // hopefully kills it..
             this.pid = -1;
         }
-        if (this.in_ch)  this.in_ch.close();
-        if (this.out_ch)  this.out_ch.close();
-        if (this.err_ch)  this.err_ch.close();
+        if (this.in_ch)  this.in_ch.shudown(true);
+        if (this.out_ch)  this.out_ch.shudown(true);
+        if (this.err_ch)  this.err_ch.shudown(true);
         // blank out channels
-        this.in_ch = false;
-        this.err_ch = false;
-        this.out_ch = false;
+        this.in_ch = null;
+        this.err_ch = null;
+        this.out_ch = null;
         // rmeove listeners !! important otherwise we kill the CPU
         if (this.err_src > -1 ) GLib.source_remove(this.err_src);
         if (this.out_src > -1 ) GLib.source_remove(this.out_src);
@@ -372,8 +380,8 @@ public class Spawn : Object
         
         //print("write_char retunred:" + JSON.stringify(res) +  ' ' +JSON.stringify(ret)  );
         
-            if (res != GLib.IOStatus.NORMAL) {
-                throw "Write failed";
+        if (res != GLib.IOStatus.NORMAL) {
+            throw "Write failed";
         }
         //return ret.value;
         return str.length;
@@ -390,30 +398,43 @@ public class Spawn : Object
         string prop = (ch == this.out_ch) ? "output" : "stderr";
        // print("prop: " + prop);
         var _this = this;
+        string str_return;
         
-       
         //print(JSON.stringify(ch, null,4));
         while (true) {
-            var x =   {};
-            var status = ch.read_line( x);
+            string buffer;
+            size_t term_pos;
+            size_t len;
+            IOStatus status;
+            try {
+                status = ch.read_line( out buffer,  out len,  out term_pos );
+            } catch (Error e) {
+                //FIXme
+                break; // ??
+                
+            }
             // print('status: '  +JSON.stringify(status));
             // print(JSON.stringify(x));
              switch(status) {
                 case GLib.IOStatus.NORMAL:
                
                     //write(fn, x.str);
-                    if (this.listeners[prop]) {
-                        this.listeners[prop].call(this, x.str_return);
+                    //if (this.listeners[prop]) {
+                    //    this.listeners[prop].call(this, x.str_return);
+                    //}
+                    if (ch == this.out_ch) {
+                        this.output += buffer;
+                    } else {
+                        this.stderr += buffer;
                     }
-                    _this[prop] += x.str_return;
-                    if (_this.debug) {
-                        print(prop + ':' + x.str_return.replace(/\n/, ''));
+                    //_this[prop] += x.str_return;
+                    if (this.cfg.debug) {
+                        stdout.printf("%s : %s", prop , str_return);
                     }
-                    if (this.async) {
+                    if (this.cfg.async) {
                         try {
-                            if (imports.gi.Gtk.events_pending()) {
-                                imports.gi.Gtk.main_iteration();
+                            if ( Gtk.events_pending()) {
+                                 Gtk.main_iteration();
                             }
                         } catch(Error e) {