73 lines
1.6 KiB
Plaintext
73 lines
1.6 KiB
Plaintext
\test_pipe =
|
|
(\next
|
|
print "== test_pipe";nl;
|
|
|
|
### Handy
|
|
|
|
# Echo entire contents of stream fh to stdout.
|
|
\file_print ==
|
|
(\fh\next
|
|
fgetc fh \ch
|
|
long_lt ch 0 next;
|
|
putchar ch;
|
|
file_print fh next
|
|
)
|
|
|
|
# Show a stream with a descriptive label.
|
|
\show_stream =
|
|
(\label\fh\next
|
|
print "[ ";print label;print ":";nl;
|
|
file_print fh;
|
|
print "]";nl;
|
|
next
|
|
)
|
|
|
|
### Here is a child function to try with spawn.
|
|
|
|
\child_fn =
|
|
(\next
|
|
print "Hello from child.";nl;
|
|
get_stdin \stdin
|
|
show_stream "input from parent" stdin;
|
|
print "Good bye from child.";nl;
|
|
die "Oops the child had an error!";
|
|
next
|
|
)
|
|
|
|
# Spawn the child.
|
|
spawn child_fn \pid\child_in\child_out\child_err
|
|
|
|
# Now we can communicate with the child through its three file handles.
|
|
print "Hello from parent, child pid = ";print pid;print ".";nl;
|
|
|
|
# Say something to the child.
|
|
(
|
|
# Override print routines for convenience.
|
|
\print = (fwrite child_in)
|
|
\nl = (print NL)
|
|
|
|
# Start talking.
|
|
print "Hello child, I am your parent!";nl;
|
|
print "OK, nice talking with you.";nl;
|
|
);
|
|
|
|
print "The parent is now done talking to the child.";nl;
|
|
|
|
# Now show the child's stdout and stderr streams.
|
|
show_stream "output from child" child_out;
|
|
show_stream "error from child" child_err;
|
|
|
|
# Wait for child to finish.
|
|
wait \pid\status
|
|
# LATER shift and logical bit operators
|
|
# LATER WEXITSTATUS and other wait macros
|
|
\status = (long_div status 256)
|
|
|
|
print "Child ";print pid;print " exited with status ";
|
|
print status;print ".";nl;
|
|
print "Good bye from parent.";nl;
|
|
|
|
print "test_pipe completed successfully.";nl;
|
|
next
|
|
)
|