run_process_output.ncdi 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. include_guard "run_process_output"
  2. template run_process_output {
  3. alias(@_arg0) command;
  4. # We collect the results here.
  5. var(@false) succeeded;
  6. value("") output;
  7. var(@true) do;
  8. backtrack_point() done;
  9. If (do) {
  10. do->set(@false);
  11. # Start child process.
  12. sys.start_process(command, "r", ["keep_stderr":"true"]) proc;
  13. If (proc.is_error) {
  14. done->go();
  15. };
  16. # Get read pipe handle.
  17. proc->read_pipe() read_pipe;
  18. If (read_pipe.is_error) {
  19. done->go();
  20. };
  21. # Read all contents.
  22. backtrack_point() read_again;
  23. read_pipe->read() read;
  24. If (read.not_eof) {
  25. output->append(read);
  26. read_again->go();
  27. };
  28. # Wait for the process to terminate.
  29. proc->wait() wait;
  30. val_different(wait.exit_status, "0") not_ok;
  31. If (not_ok) {
  32. done->go();
  33. };
  34. # Assume success.
  35. succeeded->set(@true);
  36. done->go();
  37. };
  38. }