diff options
author | Ambroz Bizjak <ambrop7@gmail.com> | 2014-12-21 01:12:39 +0300 |
---|---|---|
committer | Ambroz Bizjak <ambrop7@gmail.com> | 2014-12-21 01:12:39 +0300 |
commit | e42fecf17c1dbeb77b72d3b0b340af222baac261 (patch) | |
tree | 8c38dc98f7f8f6dcc24d3c16c895e98f50d626e8 | |
parent | 3dab8c024b70c002373a77870b4af1f28394c1cf (diff) |
ncd: examples: Add run_process_output.
-rw-r--r-- | ncd/examples/run_process_output.ncdi | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/ncd/examples/run_process_output.ncdi b/ncd/examples/run_process_output.ncdi new file mode 100644 index 0000000..e1e2e71 --- /dev/null +++ b/ncd/examples/run_process_output.ncdi @@ -0,0 +1,46 @@ +include_guard "run_process_output" + +template run_process_output { + alias(@_arg0) command; + + # We collect the results here. + var(@false) succeeded; + value("") output; + + var(@true) do; + backtrack_point() done; + If (do) { + do->set(@false); + + # Start child process. + sys.start_process(command, "r", ["keep_stderr":"true"]) proc; + If (proc.is_error) { + done->go(); + }; + + # Get read pipe handle. + proc->read_pipe() read_pipe; + If (read_pipe.is_error) { + done->go(); + }; + + # Read all contents. + backtrack_point() read_again; + read_pipe->read() read; + If (read.not_eof) { + output->append(read); + read_again->go(); + }; + + # Wait for the process to terminate. + proc->wait() wait; + val_different(wait.exit_status, "0") not_ok; + If (not_ok) { + done->go(); + }; + + # Assume success. + succeeded->set(@true); + done->go(); + }; +} |