Spawn.vala
[gitlive] / Spawn.vala
index cd7a244..ce5c3c3 100644 (file)
@@ -1,9 +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?
@@ -143,7 +144,7 @@ public class Spawn : Object
     }
 
     
-    bool ctx = false; // the mainloop ctx.
+    MainLoop ctx = null; // the mainloop ctx.
     
     /**
      * @property output {String} resulting output
@@ -224,14 +225,14 @@ public class Spawn : Object
          
         if (this.cfg.debug) {
             
-            stdout.print("PID: %d" ,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:%d ", result);
+            if (this.cfg.debug) {
+                stdout.printf("child_watch_add : result:%d ", result);
             }
            
             this.read(this.out_ch);
@@ -240,8 +241,9 @@ public class Spawn : Object
             
             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");
@@ -344,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);
@@ -378,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;
@@ -396,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) {