/**
* 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) => { ... });
+
*
*
*/
*
*/
-
-
+
public class Spawn : Object
{
/**
*/
public signal string? input();
/**
- * @signal output_line called when a line is recieved from the process.
- * @param {string} str
+ * @signal complete called at when the command has completed.
+ *
*/
- public signal void output_line(string str);
- /**
- * @signal finish called when the process 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
* if ( Gtk.events_pending()) { Gtk.main_iteration(); }
*
- * @param {int} result_id (the unix return)
- * @param {string} str (the output string)
- * @param {string} stderr (the stderr output)
+ * @param {string} str
*/
- public signal void finish(int res, string str, string stderr);
-
+ public signal void output_line(string str);
+
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
{
* 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
{
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,
out standard_error);
// stdout:
-
+
//print(JSON.stringify(gret));
this.tidyup();
//print("DONE TIDYUP");
- this.finish(this.result, this.output, this.stderr);
+ this.complete(this.result, this.output, this.stderr);
});
*/
private bool read(IOChannel ch)
{
- string prop = (ch == this.out_ch) ? "output" : "stderr";
+ // string prop = (ch == this.out_ch) ? "output" : "stderr";
// print("prop: " + prop);
} 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) {
}
}
-
- /*
+/*
+
int main (string[] args) {
GLib.Log.set_handler(null,
GLib.LogLevelFlags.LEVEL_DEBUG | GLib.LogLevelFlags.LEVEL_WARNING,
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;
}
-
-*/
+ */