README.txt
[gitlive] / Spawn.js
index ad3a158..84d5a26 100644 (file)
--- a/Spawn.js
+++ b/Spawn.js
@@ -108,7 +108,7 @@ Spawn.prototype = {
     cwd: false,
     args: false,
     exceptions : false,
-    debug : true,
+    debug : false,
     /**
      * @property output {String} resulting output
      */
@@ -125,8 +125,6 @@ Spawn.prototype = {
      * @property pid {Number} pid of child process (of false if it's not running)
      */
     pid : false,
-    
-    
     /**
      * @property in_ch {GLib.IOChannel} input io channel
      */
@@ -139,15 +137,13 @@ Spawn.prototype = {
      * @property err_ch {GLib.IOChannel} stderr io channel
      */
     err_ch : false,
-    
     /**
      * 
      * @method run
      * Run the configured command.
-     * 
+     * result is applied to object properties (eg. 'output' or 'stderr')
+     * @returns {Object} self.
      */
-    
-    
     run : function()
     {
         
@@ -165,7 +161,7 @@ Spawn.prototype = {
             GLib.SpawnFlags.DO_NOT_REAP_CHILD + GLib.SpawnFlags.SEARCH_PATH , 
             null, null, ret);
             
-       print(JSON.stringify(ret));    
+       //print(JSON.stringify(ret));    
         this.pid = ret.child_pid;
         
         if (this.debug) {
@@ -188,7 +184,7 @@ Spawn.prototype = {
                 _this.ctx.quit();
             }
             tidyup();
-           print("DONE TIDYUP");
+           //print("DONE TIDYUP");
             if (_this.listeners.finish) {
                 _this.listeners.finish.call(this, _this.result);
             }
@@ -216,22 +212,20 @@ Spawn.prototype = {
         }
         
         
-        this.in_ch = GLib.io_channel_unix_new(ret.standard_input);
-        this.out_ch = GLib.io_channel_unix_new(ret.standard_output);
-        this.err_ch = GLib.io_channel_unix_new(ret.standard_error);
+        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);
         
         // make everything non-blocking!
         
         
       
-        if ( this.async) {
-            // this seems to hang our  input.
-            this.in_ch.set_flags (GLib.IOFlags.SET_MASK);
-        
-            this.out_ch.set_flags (GLib.IOFlags.SET_MASK);
-            this.err_ch.set_flags (GLib.IOFlags.SET_MASK);
-        }
-       
+         // 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.
@@ -257,8 +251,18 @@ Spawn.prototype = {
         if (this.pid !== false) {
             // child can exit before we get this far..
             if (this.listeners.input) {
+               print("Trying to call listeners");
                 try {
                     this.write(this.listeners.input.call(this));
+                    // this probably needs to be a bit smarter...
+                   //but... let's close input now..
+                   this.in_ch.close();
+                   _this.in_ch = false;
+                  
+                   
+                   
+                   
+                   
                 } catch (e) {
                     tidyup();
                     throw e;
@@ -283,7 +287,7 @@ Spawn.prototype = {
             this.ctx = new GLib.MainLoop.c_new (null, false);
             this.ctx.run(false); // wait fore exit?
             
-            print("main_loop done!");
+            //print("main_loop done!");
         } else {
             tidyup(); // tidyup get's called in main loop. 
         }
@@ -308,12 +312,19 @@ Spawn.prototype = {
         if (!this.in_ch) {
             return 0; // input is closed
         }
-        var ret = {};
-        var res = this.in_ch.write_chars(str, str.length);
+       //print("write: " + str);
+       // NEEDS GIR FIX! for return value.. let's ignore for the time being..
+       //var ret = {};
+        //var res = this.in_ch.write_chars(str, str.length, ret);
+       var res = this.in_ch.write_chars(str, str.length);
+       
+       //print("write_char retunred:" + JSON.stringify(res) +  ' ' +JSON.stringify(ret)  );
+       
         if (res != GLib.IOStatus.NORMAL) {
             throw "Write failed";
         }
-        return ret.bytes_written;
+        //return ret.value;
+        return str.length;
         
     },
     
@@ -334,8 +345,8 @@ Spawn.prototype = {
  
             var x =   {};
             var status = ch.read_line( x);
-            //print(JSON.stringify(status));
-           // print(JSON.stringify(x));
+            // print('status: '  +JSON.stringify(status));
+            // print(JSON.stringify(x));
              switch(status) {
                 case GLib.IOStatus.NORMAL:
                
@@ -359,10 +370,13 @@ Spawn.prototype = {
                     
                     //this.ctx.iteration(true);
                    continue;
-                case GLib.IOStatus.AGAIN:   
+                case GLib.IOStatus.AGAIN:
+                   //print("Should be called again.. waiting for more data..");
+                   return true;
                     break;
                 case GLib.IOStatus.ERROR:    
-                case GLib.IOStatus.EOF:   
+                case GLib.IOStatus.EOF:
+                   return false;
                    break;
                 
             }
@@ -370,7 +384,7 @@ Spawn.prototype = {
         }
        
         //print("RETURNING");
-         return true;
+         return false; // allow it to be called again..
     }
     
 };