The standard library
Slide 60 of 85
60startThe standard library

Programs alongside

exec and attach wait for the program they run. hive.term.start(command, arguments, directory) starts one and answers at once with a Result<hive.term.Process, hive.term.ExecError>, and startWith adds an environment overlay, as execWith does. While it runs:

  • processReceiveLine(process) is the next line it wrote, as soon as it has written it, and processReceive(process, bytes) at most that many bytes of whatever has arrived. Its output and its errors are one stream, in the order written.
  • processSend(process, text) writes to its input, and processCloseInput(process) ends that input, for a program that reads until there is no more.
  • processWait(process) waits for it to end and answers its status; with timeout gives up the wait and loses nothing.
  • processKill(process) stops it and everything it started, and answers once it has ended. Its status is then -1.

A call that fails answers a hive.term.ProcessError: Closed once its output has ended or it was stopped, Send or Receive otherwise. A started program belongs to the one that started it: whatever is left of it is stopped when that one ends — a panic, exit and an interrupt included.

The example starts itself again — hive.file.executable() is where it is — told by its first argument which part to play, so it needs nothing installed.

A conversation with another program
// The program started is this one again, told which part to play.
proc main(): void {
	args := hive.term.args()
	if args bounds 0 {
		if args[0] == "--echo" {
			for line := hive.term.read(); line != ""; line = hive.term.read() {
				echo "heard {line}"
			}
		}
		return
	}
	if hive.file.executable() is Result.Ok(me) {
		if hive.term.start(me, ["--echo"], "") is Result.Ok(other) {
			hive.term.processSend(other, "hello\n")
			if hive.term.processReceiveLine(other) is Result.Ok(answer) {
				echo answer
			}
			hive.term.processCloseInput(other)
			echo "it ended with status {hive.term.processWait(other)}"
		}
	}
}