Przeglądaj źródła

ncd: examples: Add run_process_output.

Ambroz Bizjak 11 lat temu
rodzic
commit
e42fecf17c
1 zmienionych plików z 46 dodań i 0 usunięć
  1. 46 0
      ncd/examples/run_process_output.ncdi

+ 46 - 0
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();
+    };
+}