system
The system module gives access to the command line, the environment, the exit code, other programs and the operating system. It is written in Koji (std/system.kj), on the C library of the system (posix_spawnp, waitpid, poll, getenv, uname).
Functions
args() -> string[]
The command-line arguments of the program: withkojic run main.kj a b,['a', 'b'].env(name: string) -> Option[string]
The value of the environment variablename, orNoneif it is not set.currentDirectory() -> Option[string]
The current directory of the program, orNoneif the system cannot tell it.executable() -> Option[string]
The path of the executable of the running program (withkojic run, the temporary executable it built), orNonewhere the system cannot tell it.exit(code: int) -> never
Ends the program with the exit codecode(0means success). The code after it does not run, and values are not dropped.run(command: string, args: string[]) -> Result[int, SystemError]
Runs the programcommandwith the argumentsargsand waits for it to end. It uses the terminal of this program: it reads the same input and writes to the same outputs, as if it were typed in the terminal. Gives its exit code (-1if it was stopped by a signal); a program that ends with an error code is stillOk.capture(command: string, args: string[]) -> Result[Output, SystemError]
Likerun, but keeps what the program prints instead of showing it, and gives it with the exit code as anOutput.command(program: string, args: string[]) -> Command
A program to run, which can be given an input, a directory and variables of the environment before it runs (see TheCommandType).import system mut sorting = system.command('sort', []) sorting.input = 'pear\napple\n' sorting.environment['LC_ALL'] = 'C' match (sorting.output()) { Ok(result) => print(result.stdout) Err(error) => print(error) }readInput(count: int) -> Option[string]
The nextcountbytes of the standard input, as a text (fewer at its end; bytes that are not UTF-8 become U+FFFD), orNonewhen nothing is left. It reads from the same buffer asinput(), so that a protocol can read its header lines withinput()and a body of a known size withreadInput(as the language server ofkojic lspdoes).writeOutput(text: string)
Writestexton the standard output without a line break after it (printadds one): a prompt, a line drawn piece by piece, the messages of a protocol.
Constants
os: OperatingSystem
The operating system the program runs on:type OperatingSystem = { Linux MacOS Windows Other(name: string) // another system, by its name: 'freebsd' }Since it is a sum type, a
matchon it must handle every system, or have a_arm:import system immut separator = match (system.os) { Windows => ';' _ => ':' } print(separator)The compiler knows the system it compiles for, so a condition on
system.os(the value of amatch, orsystem.os == Linuxin anif) is decided when the program is compiled: the branches of the other systems are not compiled, nor the functions only they call. A branch can call a foreign function of its system that the others do not have:epoll_create1on Linux,kqueueon macOS. The condition must be in the function that makes the call (a function that callskqueuewith no condition is compiled on every system).architecture: Architecture
The processor the program runs on:type Architecture = { X64 Arm64 OtherArchitecture(name: string) // another processor, by its name: 'riscv64' }Like
os, a condition on it is decided when the program is compiled (the size of a record of C may depend on the processor).
The Command Type
program: string,args: string[]: the program and its arguments.input: string: whatoutput()gives it on its standard input (empty: nothing, ascapture).directory: string: its current directory ('': that of this program).environment: Map[string, string]: variables set in its environment, over those of this program.cleared: boolean: whentrue, its environment has only the variables ofenvironment.
Its methods run it:
output() -> Result[Output, SystemError]: likecapture, with its input written to it while what it prints is read (a program that stops reading its input does not stop this one).run() -> Result[int, SystemError]: likerun, with the terminal of this program.
The Output Type
status: int: the exit code of the program (-1if it was stopped by a signal).stdout: string: what it printed.stderr: string: what it printed as errors.
Errors
type SystemError = {
NotFound(command: string) // there is no such program
PermissionDenied(command: string) // it cannot be run
Io(command: string, message: string) // any other failure of the operating system
}
Example
import system
immut args = system.args()
if (args.length() == 0) {
print('usage: kojic run greet.kj <name>')
system.exit(2)
}
print(`Hello, ${args[0]}!`)
match (system.capture('git', ['--version'])) {
Ok(output) => print(output.stdout.trim())
Err(error) => print(error) // command 'git' not found
}
match (system.run('git', ['status'])) {
Ok(0) => print('done')
Ok(code) => print(`git ended with the code ${code}`)
Err(error) => print(error)
}