|
13 | 13 | % * opt.cwd: working directory to use while running command
|
14 | 14 | % * opt.stdin: string to pass to subprocess stdin pipe - OK to use with timeout
|
15 | 15 | % * opt.timeout: time to wait for process to complete before erroring (seconds)
|
16 |
| -% * opt.outpipe: logical to indicate whether to use pipe for stdout and stderr (mutually exclusive with timeout option) |
| 16 | +% * opt.stdout: logical to indicate whether to use pipe for stdout |
| 17 | +% * opt.stderr: logical to indicate whether to use pipe for stderr |
17 | 18 | %%% Outputs
|
18 | 19 | % * status: 0 is generally success. -1 if timeout. Other codes as per the
|
19 | 20 | % program / command run
|
|
38 | 39 | opt.cwd (1,1) string = ""
|
39 | 40 | opt.stdin (1,1) string = ""
|
40 | 41 | opt.timeout (1,1) int64 = 0
|
41 |
| - opt.outpipe (1,1) logical = true |
| 42 | + opt.stdout (1,1) logical = true |
| 43 | + opt.stderr (1,1) logical = true |
42 | 44 | end
|
43 | 45 |
|
44 |
| -if opt.outpipe && opt.timeout > 0 |
45 |
| - error("outpipe and timeout options are mutually exclusive") |
| 46 | +if (opt.stdout || opt.stderr) && opt.timeout > 0 |
| 47 | + error("stderr or stdout and timeout options are mutually exclusive") |
46 | 48 | end
|
47 | 49 |
|
48 | 50 | %% process instantiation
|
|
95 | 97 | % like Python subprocess.run, this may block or deadlock if the process writes
|
96 | 98 | % large amounts of data to stdout or stderr.
|
97 | 99 | % A better approach is to read each of the streams in a separate thread.
|
98 |
| -if opt.outpipe |
| 100 | + |
| 101 | +stdout = ""; |
| 102 | +stderr = ""; |
| 103 | +if opt.stdout && nargout > 1 |
99 | 104 | stdout = read_stream(h.getInputStream());
|
| 105 | +end |
| 106 | +if opt.stderr && nargout > 2 |
100 | 107 | stderr = read_stream(h.getErrorStream());
|
101 |
| -else |
102 |
| - stdout = ""; |
103 |
| - stderr = ""; |
104 | 108 | end
|
105 | 109 | %% wait for process to complete
|
106 | 110 | % https://docs.oracle.com/en/java/javase/23/docs/api/java.base/java/lang/Process.html#waitFor()
|
107 | 111 |
|
108 |
| -tmsg = ""; |
109 | 112 | if opt.timeout > 0
|
110 | 113 | % returns true if process completed successfully
|
111 | 114 | % returns false if process did not complete within timeout
|
112 | 115 | b = h.waitFor(opt.timeout, java.util.concurrent.TimeUnit.SECONDS);
|
113 | 116 | if b
|
114 | 117 | status = 0;
|
115 | 118 | else
|
116 |
| - tmsg = "Subprocess timeout"; |
| 119 | + stderr = "Subprocess timeout"; |
117 | 120 | status = -1;
|
118 | 121 | end
|
119 | 122 | else
|
|
128 | 131 | setenv("GFORTRAN_STDERR_UNIT", errold);
|
129 | 132 | setenv("GFORTRAN_STDIN_UNIT", inold);
|
130 | 133 |
|
131 |
| -stderr = tmsg + stderr; |
132 |
| - |
133 |
| -if nargout < 2 && strlength(stdout) > 0 |
| 134 | +if nargout < 2 && opt.stdout && strlength(stdout) > 0 |
134 | 135 | disp(stdout)
|
135 | 136 | end
|
136 |
| -if nargout < 3 && strlength(stderr) > 0 |
| 137 | +if nargout < 3 && opt.stderr && strlength(stderr) > 0 |
137 | 138 | warning(stderr)
|
138 | 139 | end
|
139 | 140 |
|
|
0 commit comments