Files
std/process/child.sakura
T
2026-12-06 13:12:06 -06:00

185 lines
6.1 KiB
Plaintext

#! @file child.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains `child`, a program that a command started, which is talked to
#! through its pipes, and waited for where it closes; `status`, how one stopped;
#! and `captured`, how one stopped with everything it wrote.
import std::io;
import system;
module std::process;
#! How a child stopped, which compares equal to another only where both say the
#! same.
#!
#! Windows stops a program by a signal only where `child::kill` stopped it,
#! which is `kill`, numbered 9, or where the console's Ctrl-C did, which is
#! `interrupt`, numbered 2; every other way it stops is an exit.
pub union status : equatable {
pub var exited: i32; #! It exited, with this status.
pub var signaled: i32; #! A signal stopped it, by the system's number.
}
#! Whether two statuses say the same.
#! @param this The status.
#! @param other The status to compare it with.
#! @returns Whether both are exits, or both signals, of the same number.
pub def status::equals(&this, other: &status): bool {
if this is exited(a) && other is exited(b) {
return a == b;
}
if this is signaled(a) && other is signaled(b) {
return a == b;
}
return false;
}
#! Whether the child exited with zero, which says it did what it was asked to.
#! @param this The status to read.
#! @returns Whether it exited, with zero.
pub val status::succeeded: bool {
get {
if this is exited(code) {
return code == 0;
}
return false;
}
}
#! A program that a command started, which runs beside this one, and which is
#! waited for where it closes.
#!
#! Each standard stream the command piped is an open `io::file` of the child,
#! which the program writes to or reads, and the rest are not open. Each may be
#! borrowed, as `io::reader_of(child.output())` borrows the output, or taken
#! over, as `io::reader_of(move(child.output()))` takes it, leaving the child a
#! file that is not open.
#!
#! A child cannot be copied, since only one value may wait for it, but it can be
#! moved. One moved from holds no program, and waits for nothing.
pub struct child : owning {
var _id: i64;
# Whether it is yet to be waited for, which one moved from is not, and how it
# stopped, once it has.
var _running: bool;
var _status: status;
var _input: io::file;
var _output: io::file;
var _error: io::file;
}
#! The number the system knows the child by.
#! @param this The child to read the number of.
#! @returns The number, which is zero for one moved from.
pub val child::id: u64 {
get => this._id as u64;
}
#! The pipe the child reads its standard input from, which is open where the
#! command piped it. Closing it ends the child's input.
#! @param this The child.
#! @returns The pipe's end the program writes.
pub def child::input(&mut this): &mut io::file {
return &mut this._input;
}
#! The pipe the child writes its standard output to, which is open where the
#! command piped it.
#! @param this The child.
#! @returns The pipe's end the program reads.
pub def child::output(&mut this): &mut io::file {
return &mut this._output;
}
#! The pipe the child writes its standard error to, which is open where the
#! command piped it.
#! @param this The child.
#! @returns The pipe's end the program reads.
pub def child::error(&mut this): &mut io::file {
return &mut this._error;
}
#! Waits for the child to stop. Its input is closed first, so a child reading
#! its input to the end can finish.
#! @param this The child to wait for.
#! @returns How it stopped, which waiting again gives again, or why it could not
#! be waited for: `io_error::invalid` for a child moved from, and
#! `io_error::interrupted` where a signal the program handles ended the
#! wait, after which it can be waited for again.
pub def child::wait(&mut this): result<status, io_error> {
this._input.close();
if !this._running {
if this._id == 0 {
return io_error::invalid;
}
return this._status;
}
var stopped: system::process_status;
val waited := system::wait(this._id, &mut stopped);
if waited < 0 {
return system::error_of(waited);
}
this._running = false;
this._status = stopped.exited ? status { exited = stopped.code } : status { signaled = stopped.signal };
return this._status;
}
#! Stops the child at once, which it cannot put off. Once the child has been
#! waited for, this does nothing, since the system may then have given its
#! number to another program.
#! @param this The child to stop.
#! @returns Nothing, or why it could not be stopped.
pub def child::kill(&mut this): result<void, io_error> {
if !this._running {
return;
}
val sent := system::kill(this._id, system::signal::kill);
if sent < 0 {
return system::error_of(sent);
}
return;
}
#! Closes the child's pipes, then waits for it to stop, as many times as a
#! signal ends the wait. A child that has been waited for, or moved from, is not
#! waited for again.
#! @param this The child to close.
pub def child::close(&mut this): void {
this._input.close();
this._output.close();
this._error.close();
while this._running {
if this.wait() is error(why) && why != io_error::interrupted {
return;
}
}
}
#! What `command::capture` gives: how the child stopped, and everything it
#! wrote.
pub struct captured {
pub var status: status; #! How the child stopped.
pub var output: string; #! What it wrote to its standard output.
pub var error: string; #! What it wrote to its standard error.
}
#! A variable of the environment the program started with.
#! @param name The name of the variable, as `PATH`, which Windows reads
#! whatever the case of its letters, as `Path`.
#! @returns Its value, which views memory that lasts as long as the program, or
#! `nothing` where the program started without it.
pub def variable(name: strview): maybe<strview> {
for each in system::environment() {
if each.length > name.length && each[name.length] == '=' && same_name(each.substr(0, name.length), name) {
return each.substr(name.length + 1, each.length - name.length - 1);
}
}
return nothing{};
}