X-Git-Url: http://git.roojs.org/?a=blobdiff_plain;f=src%2FSpawn.vala;h=d3b9428b9f96ef5d841aed9c3892683eb9bdfc26;hb=e1ae4dd4fe92d7104274c4935db55fd0d5e80857;hp=553bce4f98e8da5c9cfd6b9b2189fc65b429c908;hpb=f8897ac9daab6c1df835fff5519f281c83b41bf0;p=app.Builder.js diff --git a/src/Spawn.vala b/src/Spawn.vala index 553bce4f9..d3b9428b9 100644 --- a/src/Spawn.vala +++ b/src/Spawn.vala @@ -7,12 +7,17 @@ using GLib; /** * Revised version? * - * x = new Spawn( working dir, args) + * x = new Spawn( "/tmp", {"ls", "-l" }) + * + * // these are optionall.. * x.env = ..... (if you need to set one... - * x.output_line.connect((string str) => { ... }); + * x.output_line.connect((string str) => { + * if ( Gtk.events_pending()) { Gtk.main_iteration(); } + * }); * x.input_line.connect(() => { return string }); - * x.finish.connect((int res, string output, string stderr) => { ... }); - * x.run(); + * + * x.run((int res, string output, string stderr) => { ... }); + * * */ @@ -38,10 +43,7 @@ public errordomain SpawnError { * */ - - -void SpawnFinish (int res, string str, string stderr); - + public class Spawn : Object { /** @@ -49,6 +51,11 @@ public class Spawn : Object * @return the string or null */ public signal string? input(); + /** + * @signal complete called at when the command has completed. + * + */ + public signal void complete(int res, string str, string stderr); /** * @signal output_line called when a line is recieved from the process. * Note you may want to connect this and run @@ -57,20 +64,14 @@ public class Spawn : Object * @param {string} str */ public signal void output_line(string str); - /** - * @signal finish called when the process has completed. - * @param {int} result_id (the unix return) - * @param {string} str (the output string) - * @param {string} stderr (the stderr output) - */ - public signal void finish(int res, string str, string stderr); - + public string cwd; public string[] args; public string[] env; public bool is_async = true; public bool throw_exceptions = false; + public bool detach = false; public Spawn(string cwd, string[] args) throws Error { @@ -139,7 +140,7 @@ public class Spawn : Object * result is applied to object properties (eg. '?' or 'stderr') * @returns {Object} self. */ - public void run() throws SpawnError, GLib.SpawnError, GLib.IOChannelError + public void run( ) throws SpawnError, GLib.SpawnError, GLib.IOChannelError { @@ -153,11 +154,27 @@ public class Spawn : Object GLib.debug("cd %s; %s" , this.cwd , string.joinv(" ", this.args)); - + if (this.detach) { + Process.spawn_async_with_pipes ( + this.cwd, + this.args, + this.env.length > 0 ? this.env : null, + SpawnFlags.SEARCH_PATH | SpawnFlags.DO_NOT_REAP_CHILD, + null, + out this.pid); + ChildWatch.add (this.pid, (pid, status) => { + // Triggered when the child indicated by child_pid exits + Process.close_pid (pid); + + }); + + return; + + } Process.spawn_async_with_pipes ( this.cwd, this.args, - this.env, + this.env.length > 0 ? this.env : null, SpawnFlags.SEARCH_PATH | SpawnFlags.DO_NOT_REAP_CHILD, null, out this.pid, @@ -166,7 +183,7 @@ public class Spawn : Object out standard_error); // stdout: - + //print(JSON.stringify(gret)); @@ -209,7 +226,7 @@ public class Spawn : Object this.tidyup(); //print("DONE TIDYUP"); - this.finish(this.result, this.output, this.stderr); + this.complete(this.result, this.output, this.stderr); }); @@ -354,7 +371,7 @@ public class Spawn : Object */ private bool read(IOChannel ch) { - string prop = (ch == this.out_ch) ? "output" : "stderr"; + // string prop = (ch == this.out_ch) ? "output" : "stderr"; // print("prop: " + prop); @@ -388,10 +405,11 @@ public class Spawn : Object } else { this.stderr += buffer; + this.output_line( buffer); } //_this[prop] += x.str_return; //if (this.cfg.debug) { - GLib.debug("%s : %s", prop , buffer); + //GLib.debug("%s : %s", prop , buffer); //} if (this.is_async) { @@ -421,6 +439,7 @@ public class Spawn : Object } } +/* int main (string[] args) { GLib.Log.set_handler(null, @@ -431,14 +450,14 @@ int main (string[] args) { var ctx = new GLib.MainLoop (); var a = new Spawn("", { "ls" , "-l"}); - a.finish.connect((res, str, stderr) => { + a.run((res, str, stderr) => { print(str); ctx.quit(); }); - a.run(); + ctx.run(); // wait for exit? return 0; } - + */