}
- public void onFinish( SpawnFinish? finish ) {
- this.finish = finish;
- }
-
+
public void setHandlers(
SpawnOutput? output,
/**
* @property result {Number} execution result.
*/
- int result= 0;
+ public int result= 0;
/**
* @property pid {Number} pid of child process (of false if it's not running)
*/
*/
int out_src = -1;
+
+ unowned SpawnFinish on_finished;
+
/**
*
* @method run
* result is applied to object properties (eg. '?' or 'stderr')
* @returns {Object} self.
*/
- public void run(unowned SpawnFinish? finished_cb) throws SpawnError, GLib.SpawnError, GLib.IOChannelError
+ public void run( SpawnFinish finished_cb) throws SpawnError, GLib.SpawnError, GLib.IOChannelError
{
-
+ this.on_finished = finished_cb;
err_src = -1;
out_src = -1;
int standard_input;
if (this.cfg.debug) {
- stdout.printf("cd %s; %s\n" , this.cfg.cwd , string.joinv(" ", this.cfg.args));
+ GLib.debug("cd %s; %s\n" , this.cfg.cwd , string.joinv(" ", this.cfg.args));
}
+ // stdout:
+ if (!this.cfg.async) {
+ string ls_stdout;
+ string ls_stderr;
+ int ls_status;
+
+ Process.spawn_sync (
+ this.cfg.cwd,
+ this.cfg.args,
+ this.cfg.env,
+ SpawnFlags.SEARCH_PATH,
+ null,
+ out ls_stdout,
+ out ls_stderr,
+ out ls_status
+ );
+ this.output = ls_stdout;
+ this.stderr = ls_stderr;
+ this.result = ls_status;
+ if (this.cfg.exceptions && this.result != 0) {
+ var errstr = string.joinv(" ", this.cfg.args) + "\n";
+ errstr += this.output;
+ errstr += this.output.length > 0 ? "\n" : "";
+ errstr += this.stderr;
+ //print("Throwing execute error:%s\n", errstr);
+ throw new SpawnError.EXECUTE_ERROR(errstr);
+ //this.toString = function() { return this.stderr; };
+ ///throw new Exception this; // we throw self...
+ }
+ return;
+
+ }
+
+
+
+
Process.spawn_async_with_pipes (
this.cfg.cwd,
this.cfg.args,
out standard_output,
out standard_error);
- // stdout:
+
//print(JSON.stringify(gret));
if (this.cfg.debug) {
- stdout.printf("PID: %d\n" ,this.pid);
+ GLib.debug("PID: %d\n" ,this.pid);
}
this.ref(); // additional ref - cleared on tidyup...
- ChildWatch.add (this.pid, (w_pid, result) => {
-
- this.result = result;
- if (this.cfg.debug) {
- stdout.printf("child_watch_add : result:%d\n", result);
- }
-
- this.read(this.out_ch);
- this.read(this.err_ch);
-
-
- Process.close_pid(this.pid);
- this.pid = -1;
- if (this.ctx != null) {
- this.ctx.quit();
- this.ctx = null;
- }
- this.tidyup();
- //print("DONE TIDYUP");
- if (this.cfg.finish != null) {
- this.cfg.finish(this.result);
- }
- });
+ ChildWatch.add (this.pid, this.on_child_watch);
if (this.pid > -1) {
// child can exit before we get this far..
if (this.cfg.input != null) {
- if (this.cfg.debug) print("Trying to call listeners");
+ if (this.cfg.debug) GLib.debug("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.shutdown(true);
this.in_ch = null;
-
+
} catch (Error e) {
this.tidyup();
}
// async - if running - return..
if (this.cfg.async && this.pid > -1) {
+ //this.ref();
return;
}
if (this.pid > -1) {
//print("starting main loop");
- //if (this.cfg.debug) {
- //
- // }
- this.ctx = new MainLoop ();
+ if (this.cfg.debug) {
+ GLib.debug("starting main loop");
+ }
+ this.ctx = new MainLoop ();
this.ctx.run(); // wait fore exit?
-
+ if (this.cfg.debug) {
+ GLib.debug(" main loop done");
+ }
//print("main_loop done!");
} else {
this.tidyup(); // tidyup get's called in main loop.
errstr += this.output.length > 0 ? "\n" : "";
errstr += this.stderr;
//print("Throwing execute error:%s\n", errstr);
- throw new SpawnError.EXECUTE_ERROR(errstr);
+ throw new SpawnError.EXECUTE_ERROR(errstr);
//this.toString = function() { return this.stderr; };
///throw new Exception this; // we throw self...
}
return;
}
+
+ void on_child_watch(GLib.Pid w_pid, int result) {
+
+ this.result = result;
+ if (this.cfg.debug) {
+ stdout.printf("child_watch_add : result:%d\n", result);
+ }
+
+ this.read(this.out_ch);
+ this.read(this.err_ch);
+
+
+ Process.close_pid(this.pid);
+ this.pid = -1;
+ if (this.ctx != null) {
+ this.ctx.quit();
+ this.ctx = null;
+
+ }
+ //print("child process done - running callback, then tidyup");
+ this.on_finished(this.result, this.output + (this.output.length > 0 ? "\n" : "") + this.stderr);
+ // this.unref();
+ this.tidyup();
+
+ //print("DONE TIDYUP");
+
+
+ }
+
+
private void tidyup()
{
+ if (this.cfg.debug) {
+ GLib.debug("tidyup");
+ }
+
//print("Tidyup\n");
if (this.pid > -1) {
Process.close_pid(this.pid); // hopefully kills it..
//if (this.out_src > -1 ) GLib.source_remove(this.out_src);
this.err_src = -1;
this.out_src = -1;
- this.unref();
+ //this.unref();
}