Core\Process\Handle::readStdout
$handle->readStdout(): ?bytesReads the output of a program that Core\Process::spawn started, while the program still runs. Each call waits until the program writes something, and returns it as bytes. Use as string to convert it to text.
A call returns one part of the output. A part is whatever has arrived. It can be less than one line, or many lines. Call readStdout() in a loop to read everything. At the end of the output the result is null. Other requests keep running while one request waits.
If you only need the whole output at the end, Core\Process::run is simpler.
Each part is tainted bytes, because another program wrote it. Write tainted bytes on the variable that keeps it.
The examples below read a whole output part by part, read from a program that prints nothing, and pass a program's output on to your own output while the program runs.
Return value
Returns ?bytes. The next part of the output, as tainted bytes, up to 64 KiB. A part is whatever has arrived: it can be less than one line or several lines. At the end of the output the result is null, and every later call also returns null. Use as string to convert a part to text.
Errors
TimeoutErrorThetimeoutgiven tospawnhas passed. The program is stopped, and every later call exceptkillthrows this error too.IOErrorThe operating system could not read the output. The output is then closed, so the next call returnsnull.
Changelog
| Version | Change |
|---|---|
0.0.6 | The output of a child process is tainted bytes. The version text of a database server and the name an LDAP server returns are tainted string. Another program or a server writes this text, so it may contain input from outside. |
Examples
Reading the whole output
<?nvs// Shows how to read the whole output of a program, one part at a time.// requires: unix
var $program = Core\Process::spawn('/usr/bin/seq', ['1', '5']);tainted string $text = '';// `readStdout()` returns the next part of the output. At the end, it returns `null`.?tainted bytes $part = $program->readStdout();while ($part != null) { // A part is `bytes`. `as string` converts it to text. $text = $text . ($part as string); $part = $program->readStdout();}$program->wait();
// This prints the numbers 1 to 5, one on each line.echo $text;12345A program that prints nothing
<?nvs// Shows what `readStdout()` returns when a program prints nothing.// requires: unix
// `/usr/bin/true` prints nothing and ends at once.var $program = Core\Process::spawn('/usr/bin/true', []);
// The output has ended, so the result is `null`.?tainted bytes $first = $program->readStdout();echo 'first read: ', $first == null ? 'null' : 'data', "\n";
// A second call also returns `null`. It does not throw an error.?tainted bytes $second = $program->readStdout();echo 'second read: ', $second == null ? 'null' : 'data', "\n";
// This prints `exit code 0`.echo 'exit code ', $program->wait()->exitCode(), "\n";first read: nullsecond read: nullexit code 0Passing the output on
<?nvs// Shows how to print the output of a long job while the job still runs.// requires: unix
// This job prints three lines, with a short pause before each one.var $job = Core\Process::spawn( '/bin/sh', ['-c', 'for step in 1 2 3; do sleep 0.2; echo "step $step of 3 is done"; done'],);
// Each part is printed as soon as it arrives. The loop does not wait for the end of the job.?tainted bytes $part = $job->readStdout();while ($part != null) { echo ($part as string); $part = $job->readStdout();}
// This prints `exit code 0`.echo 'exit code ', $job->wait()->exitCode(), "\n";step 1 of 3 is donestep 2 of 3 is donestep 3 of 3 is doneexit code 0