null/demo.local.php
[gitlive] / Spawn.vala
index f11ad7b..a8db579 100644 (file)
@@ -20,26 +20,9 @@ var GLib      = imports.gi.GLib;
 * Library to wrap GLib.spawn_async_with_pipes
 * 
 * usage:
-* 
-* Spawn = import.Spawn;
-* 
-* simple version..
-* var output = Spawn.run({
-*   cwd : '/home',
-*   args : [ 'ls', '-l' ],
-*   env : [], // optional
-*   listeners : {
-        output : function (line) { Seed.print(line); },
-*       stderr :  function (line) {Seed.print("ERROR" + line);  },
-*       input : function() { return 'xxx' },
-*   }
-*  });
-*
-*
-*
+* v 
 *
-*
-*var output = Spawn.run( SpawnConfig() {
+*var output = new Spawn( SpawnConfig() {
     cwd = "/home",  // empty string to default to homedirectory.
     args = {"ls", "-l" },
     evn = {},
@@ -55,8 +38,7 @@ delegate void SpawnErr(string line);
 delegate string SpawnInput();
 
 
-
-struct SpawnConfig
 struct SpawnConfig {
     public string cwd;
     public string[] args;
@@ -117,6 +99,7 @@ public class Spawn : Object
        if (!this.cfg.args.length) {
            throw "No arguments";
        }
+       this.run();
     
     }
 
@@ -168,7 +151,7 @@ public class Spawn : Object
      * result is applied to object properties (eg. 'output' or 'stderr')
      * @returns {Object} self.
      */
-    public run : function()
+    public void run()
     {
         
          
@@ -267,20 +250,17 @@ public class Spawn : Object
         if (this.pid > -1) {
             // child can exit before 1we get this far..
             if (this.cfg.input != null) {
-               print("Trying to call listeners");
+               if (this.cfg.debug) print("Trying to call listeners");
                 try {
                     this.write(this.cfg.input());
                     // this probably needs to be a bit smarter...
                    //but... let's close input now..
                    this.in_ch.close();
                    this.in_ch = -1;
-                  
-                   
-                   
-                   
+                    
                    
                 } catch (e) {
-                    tidyup();
+                    this.tidyup();
                     throw e;
                     
                 }
@@ -288,35 +268,34 @@ public class Spawn : Object
             }
         }
         // async - if running - return..
-        if (this.async && this.pid) {
-            return this;
+        if (this.cfg.async && this.pid > -1) {
+            return;
         }
          
         // start mainloop if not async..
         
-        if (this.pid !== false) {
-            if (this.debug) {
+        if (this.pid > -1) {
+            if (this.cfg.debug) {
                 print("starting main loop");
             }
-           
-            this.ctx = isSeed ? new GLib.MainLoop.c_new (null, false) : GLib.MainLoop.new (null, false);;
-            this.ctx.run(false); // wait fore exit?
+           this.ctx = new MainLoop ();
+            loop.run(); // wait fore exit?
             
             //print("main_loop done!");
         } else {
-            tidyup(); // tidyup get's called in main loop. 
+            this.tidyup(); // tidyup get's called in main loop. 
         }
         
-        if (this.exceptions && this.result != 0) {
-            this.toString = function() { return this.stderr; };
-            throw this; // we throw self...
+        if (this.cfg.exceptions && this.result != 0) {
+            //this.toString = function() { return this.stderr; };
+            ///throw new Exception this; // we throw self...
         }
         
         // finally throw, or return self..
         
-        return this;
+        return;
     
-    },
+    }
     
     
 
@@ -347,9 +326,9 @@ public class Spawn : Object
      * @arg str {String} string to write to stdin of process
      * @returns GLib.IOStatus (0 == error, 1= NORMAL)
      */
-    write : function(str) // write a line to 
+    integer write(str) // write a line to 
     {
-        if (!this.in_ch) {
+        if (this.in_ch is null) {
             return 0; // input is closed
         }
        //print("write: " + str);
@@ -366,7 +345,7 @@ public class Spawn : Object
         //return ret.value;
         return str.length;
         
-    },
+    }
     
     /**
      * read from pipe and call appropriate listerner and add to output or stderr string.
@@ -428,21 +407,7 @@ public class Spawn : Object
     }
     
 };
-/**
- * @function run 
- * 
- * simple run a process - returns result, or throws stderr result...
- * @param cfg {Object}  see spawn
- * @return {string} stdout output.
- */
-function run(cfg) {
-    cfg.exceptions = true;
-    cfg.async = false;
-    var s = new Spawn(cfg);
-    var ret = s.run();
-    return s.output;
-}
- /*
+  /*
 // test
 try { 
     Seed.print(run({