Spawn.js
authorAlan Knowles <alan@akbkhome.com>
Wed, 14 Apr 2010 08:57:26 +0000 (16:57 +0800)
committerAlan Knowles <alan@akbkhome.com>
Wed, 14 Apr 2010 08:57:26 +0000 (16:57 +0800)
Spawn.js

index b9ec950..6adcf1a 100644 (file)
--- a/Spawn.js
+++ b/Spawn.js
@@ -77,6 +77,9 @@ Spawn.prototype = {
      */
     done : false,
     
+    
+    
+    
     run : function()
     {
         var ret = {};
@@ -100,12 +103,12 @@ Spawn.prototype = {
             }
             
         });
-        var in_ch = GLib.io_channel_unix_new(ret.standard_input);
+        this.in_ch = GLib.io_channel_unix_new(ret.standard_input);
         var out_ch = GLib.io_channel_unix_new(ret.standard_output);
         var err_ch = GLib.io_channel_unix_new(ret.standard_error);
        
         // make everything non-blocking!
-        GLib.io_channel_set_flags (in_ch,GLib.IOFlags.NONBLOCK);
+        GLib.io_channel_set_flags (this.in_ch,GLib.IOFlags.NONBLOCK);
         GLib.io_channel_set_flags (out_ch,GLib.IOFlags.NONBLOCK);
         GLib.io_channel_set_flags (err_ch,GLib.IOFlags.NONBLOCK);
 
@@ -147,7 +150,9 @@ Spawn.prototype = {
         });
         if (!this.done) {
             // child can exit before we get this far..
-            
+            if (this.listeners.input) {
+                this.write(this.listeners.input.call(this));
+            }
             started = true;
             GLib.main_loop_run(ctx, false); // wait fore exit?
         }
@@ -158,7 +163,8 @@ Spawn.prototype = {
         // clean up.
         
         
-        GLib.io_channel_close(in_ch);
+        GLib.io_channel_close(this.in_ch);
+        this.in_ch = false;
         GLib.io_channel_close(out_ch);
         GLib.io_channel_close(err_ch);
         GLib.source_remove(err_src);
@@ -167,87 +173,15 @@ Spawn.prototype = {
         
         return retval;
     
-    
-    
-}
-
-    
-    
-    var ret = { };
-    var retval =  { output: '' , error : '', cmd : s.join(' ') , done : false};
-    GLib.spawn_async_with_pipes(cwd, s, null, 
-        GLib.SpawnFlags.DO_NOT_REAP_CHILD + GLib.SpawnFlags.SEARCH_PATH , 
-        null, null, ret);
-        
-    var ctx = GLib.main_loop_new (null, false);
-    var started = false;
-    
-    GLib.child_watch_add(GLib.PRIORITY_DEFAULT, ret.child_pid, function(pid, status) {
-        retval.status = status;
-        retval.done = true;
-        GLib.spawn_close_pid(ret.child_pid);
-        if (started) {
-            GLib.main_loop_quit(ctx);
-        }
-        
-    });
-    var in_ch = GLib.io_channel_unix_new(ret.standard_input);
-    var out_ch = GLib.io_channel_unix_new(ret.standard_output);
-    var err_ch = GLib.io_channel_unix_new(ret.standard_error);
-    // fixme - put non-blocking
-    GLib.io_channel_set_flags (in_ch,GLib.IOFlags.NONBLOCK);
-    GLib.io_channel_set_flags (out_ch,GLib.IOFlags.NONBLOCK);
-    GLib.io_channel_set_flags (err_ch,GLib.IOFlags.NONBLOCK);
-
-    function readstr(ch, prop) {
-        while (true) {
-            var x = new GLib.String();
-            var cstatus = GLib.io_channel_get_buffer_condition(ch);
-            cstatus = GLib.io_channel_get_flags (ch);
-            var status = GLib.io_channel_read_line_string (ch, x);
-            switch(status) {
-                case GLib.IOStatus.NORMAL:
-                    //write(fn, x.str);
-                    retval[prop] += x.str;
-                   continue
-                case GLib.IOStatus.AGAIN:   
-                    break;
-                case GLib.IOStatus.ERROR:    
-                case GLib.IOStatus.EOF:   
-                   break;
-                
-            }
-            break;
-        }
-    }
-    var out_src= GLib.io_add_watch(out_ch, GLib.PRIORITY_DEFAULT, 
-        GLib.IOCondition.OUT + GLib.IOCondition.IN  + GLib.IOCondition.PRI, function()
+    },
+    write : function(str) // write a line to 
     {
-        //Seed.print("GOT INOUT ON IN");
-        readstr(out_ch,  'output');
+        if (!this.in_ch) {
+            return; // input is closed
+        }
         
-    });
-    var err_src= GLib.io_add_watch(err_ch, GLib.PRIORITY_DEFAULT, 
-        GLib.IOCondition.ERR + GLib.IOCondition.IN + GLib.IOCondition.PRI + GLib.IOCondition.OUT, 
-        function()
-    {
-        // Seed.print("GOT INOUT ON ERR");
-         readstr(err_ch, 'error');
-         
-    });
-    if (!retval.done) {
-        started = true;
-        //console.log("STARTING LOOP");
-        GLib.main_loop_run(ctx, false); // wait fore exit?
     }
-    readstr(out_ch,  'output');
-    readstr(err_ch,  'error');
-    GLib.io_channel_close(in_ch);
-    GLib.io_channel_close(out_ch);
-    GLib.io_channel_close(err_ch);
-    GLib.source_remove(err_src);
-    GLib.source_remove(out_src);
     
-    
-    return retval;
-}
\ No newline at end of file
+}
+
+     
\ No newline at end of file