Skip to content

Core\Process\Handle::writeStdin

$handle->writeStdin(bytes $data): void$handle->writeStdin(
    bytes $data,
): void

Sends data to a program that Core\Process::spawn started, while the program runs. The program reads the data from its standard input, the same way it reads what a person types. The call waits until the program has taken all of the data.

You can call writeStdin() many times. The input stays open between the calls. wait() closes the input, so a program that reads until its input ends can then finish. The data is bytes. Use as bytes to convert a string. A tainted value from a user is allowed, because the program reads it as data and does not run it as a command.

The examples below give a list to sort, talk to a program one line at a time, and keep only the error lines of a log.

Parameters

  • $databytesThe data to send, as bytes. All of it is sent. Use as bytes to convert a string.

Return value

Returns void. Nothing. The input stays open, so you can call writeStdin again. wait closes the input, and then the program sees the end of it.

Errors

  • TimeoutErrorThe timeout given to spawn has passed. The program is stopped, and every later call except kill throws this error too.
  • IOErrorThe operating system could not send the data. This happens most often when the program has already ended, or after wait.

Examples

Sorting a list

<?nvs
// Shows how to give a list to another program and read its answer.
// requires: unix
// `sort` reads lines from its input and writes them back in order.
var $sorter = Core\Process::spawn('/usr/bin/sort', []);
// Each call sends one name. `as bytes` converts the text to the `bytes` that `writeStdin` needs.
// The input stays open between the calls.
array<string> $names = ['Charlie', 'Alice', 'Bob'];
foreach ($names as string $name) {
$sorter->writeStdin(($name . "\n") as bytes);
}
// `wait()` closes the input. `sort` then writes the sorted list and ends.
// This prints Alice, Bob and Charlie, one on each line.
echo $sorter->wait()->stdout() as string;
Output
Alice
Bob
Charlie

One line at a time

<?nvs
// Shows how to send a line to a program, read its reply, and then send the next line.
// requires: unix
// This program reads one line at a time and replies to each line at once.
var $program = Core\Process::spawn(
'/bin/sh',
['-c', 'while read line; do echo "got: $line"; done'],
);
// Each line is sent, and then the reply to it is read.
// This prints `got: hello` and then `got: goodbye`.
array<string> $lines = ['hello', 'goodbye'];
foreach ($lines as string $line) {
$program->writeStdin(($line . "\n") as bytes);
?tainted bytes $reply = $program->readStdout();
if ($reply != null) {
echo $reply as string;
}
}
// `wait()` closes the input, so the loop in the program ends. The exit code is `0`.
echo 'exit code: ', $program->wait()->exitCode(), "\n";
Output
got: hello
got: goodbye
exit code: 0

Keeping only the errors

<?nvs
// Shows how to pass the lines of a log to `grep`, and keep only the lines with errors.
// requires: unix
// `grep ERROR` prints only the lines of its input that contain `ERROR`.
var $filter = Core\Process::spawn('/usr/bin/grep', ['ERROR']);
// The log is sent one line at a time, as a program does while it reads a large file.
array<string> $log = [
'10:00 INFO server started',
'10:02 ERROR cannot connect to the database',
'10:03 INFO request finished',
'10:05 ERROR disk is full',
];
foreach ($log as string $entry) {
$filter->writeStdin(($entry . "\n") as bytes);
}
// This prints the two lines that contain `ERROR`.
echo $filter->wait()->stdout() as string;
Output
10:02 ERROR cannot connect to the database
10:05 ERROR disk is full