os

tape
import os from "os";
import io from "io";

for (a in os.args()) {
    io.println(a);                      // the program first, then its arguments
}
let home: ?string = os.env("HOME");
if (os.PLATFORM == os.Platform.Win64) { io.println("on Windows"); }

os is the running program and what surrounds it. It is a capability module.

Arguments

FunctionReturns
arg_count() -> i64The number of arguments, the program’s name included
arg(i: i64) -> ?[]const u8Argument i, or null when there is none
arg_list() -> []const []const u8All of them as one slice
args() -> ArgIterAn iterator for for (a in os.args())

Argument 0 is the program: the executable when you tape build it, the .tape file under tape run. Pass arguments to a program run in the VM after --:

plaintext
tape run tool.tape -- input.txt --verbose

The returned views stay valid for the whole run.

Environment

FunctionDoes
env(name) -> ?stringThe variable’s value, or null when it isn’t set
set_env(name, value) -> void or io.IoErrorSet it for this process and the processes it starts

An empty value is "", not null. A name can’t be empty or contain = (set_env reports InvalidInput).

The process

FunctionDoes
exit(code: i64)End the program now with code
cwd() -> string or io.IoErrorThe working directory
exe_path() -> string or io.IoErrorThe running executable’s absolute path, symlinks resolved (on Windows the path it started from; under tape run, tape’s own)
exe_dir() -> string or io.IoErrorThe directory exe_path() is in: where a program finds the files it ships with when it is double-clicked, or ../Resources from it in a macOS .app
chdir(path) -> void or io.IoErrorChange the working directory
pid() -> i64The process id
cpu_count() -> i64How many logical processors (hardware threads) the program may use, at least 1
random_bytes(dst: []u8) -> void or io.IoErrorFill dst from the OS’s secure random source

| hostname() -> string or io.IoError | The machine’s name | | user_name() -> string or io.IoError | The name of the user the program runs as | | uid() -> ?i64 | The user id the program runs as (the effective uid) on Linux and macOS, what fs.Metadata.uid is compared with; null on Windows | | home_dir() -> ?string | The user’s home directory: $USERPROFILE on Windows, $HOME elsewhere; null when unset or empty |

set_trap_log is under Trap log.

exit stops at once: defer blocks don’t run and buffered writers aren’t flushed, so close your BufWriters first. Returning from main is the usual way to finish.

tape
let user: string = os.user_name() or { return 1; };
let host: string = os.hostname() or { return 1; };
io.println("{user}@{host}");
let home: ?string = os.home_dir();
if (home != null) {
    let config: string = path.join(home!, ".myapp.ini");
}

home_dir reads the environment, so it is whatever the user set (and isn’t checked to exist).

random_bytes is for seeds, keys and nonces:

tape
var seed: [8]u8;
os.random_bytes(seed[0..8]) or { return 1; };

Trap log

A trap ends the program with a line on stderr and exit code 101. A @windowed game double-clicked has no terminal to show that line, so give it a file:

tape
import os from "os";
import path from "path";

pub fn main() -> i32 {
    let dir = os.exe_dir() or { return 1; };
    os.set_trap_log(path.join(dir, "trap.log"));
    // ...
}
FunctionDoes
set_trap_log(path: []const u8)From now on a trap also appends its line to this file. An empty path stops that

Call it early in main: a trap before the call is not logged. The program keeps its own copy of the path.

The file gets exactly the text stderr gets (trap: <what> at <file>:<line>:<col>), stderr still gets it, and the exit code is still 101. A trap on any thread is logged, in the VM and natively.

The file is opened only when a trap happens, so a clean run creates no file, and it is appended to, never replaced. If it can’t be opened or written, that is ignored and the trap ends the program as usual.

A relative path is resolved when the trap happens, against the working directory of that moment, which for a double-clicked program is of the system’s choosing. Build the path from os.exe_dir(), as above. Under tape run that is tape’s own directory, so a script usually wants a path of its own.

A trap inside a shared library written in tape (image’s and uwm’s among them) is that library’s, with its own copy of the runtime, and goes to stderr only. On Uusi there is no log: set_trap_log does nothing.

Signals

A server stops when it is told to and reloads when asked, from its own event loop rather than from a handler running on whatever thread the OS picked. os.signals queues the signals you list; you read them when your loop gets to it.

tape
pub enum Signal : u8 {
    Hangup = 1: "SIGHUP";          // reload, by convention
    Interrupt = 2: "SIGINT";       // Ctrl-C
    User1 = 10: "SIGUSR1";
    User2 = 12: "SIGUSR2";
    Terminate = 15: "SIGTERM";     // stop, gracefully
}
FunctionDoes
signals(which: []const Signal) -> Signals or io.IoErrorTake the listed signals away from their default actions and queue them
Signals.next() -> ?SignalThe oldest queued signal, or null; never waits
Signals.handle() -> i64A handle for net.poll or a net.Poller, readable while a signal is queued
Signals.close()Restore the default actions and release the queue

A signal you don’t list keeps its default: a Terminate not listed still ends the process. There is one Signals per process (AlreadyExists for a second one until the first is closed), made before the first worker thread starts so that every thread inherits the arrangement. No callbacks: nothing runs on another thread’s stack.

The handle is what makes signals fit a loop that waits on sockets. Poll it beside them and the loop wakes for a signal the same way:

tape
import net from "net";
import os from "os";

var sig: os.Signals = os.signals([os.Signal.Interrupt, os.Signal.Terminate, os.Signal.Hangup]) or { return 1; };
defer sig.close();

var items: [2]net.PollItem;
items[0] = listener.poll_item(net.READABLE);
items[1] = net.PollItem { h: sig.handle(); want: net.READABLE; got: 0; };
while (true) {
    net.poll(items[0..2], null) or { return 1; };
    let s: ?os.Signal = sig.next();
    if (s != null) {
        if (s! == os.Signal.Hangup) { reload(); continue; }
        break;                                   // Interrupt or Terminate: stop
    }
    if ((items[0].got & net.READABLE) != 0) { accept_one(); }
}

A signal that arrives twice before you read it may be seen once or twice, as the OS delivers it: treat a signal as a level (“stop was asked”), not a count.

Platform

tape
pub enum Platform : u8 { Win64 = 0; Linux = 1; MacOS = 2; Uusi = 3; }
pub enum Arch : u8 { X64 = 0; Arm64 = 1; }
pub const PLATFORM: Platform;
pub const ARCH: Arch;

PLATFORM and ARCH are the platform the program was compiled for: the --target of tape build, or the machine itself under tape run. They are constants, so code that tests them costs nothing at run time.

Deprecated

args_count() still works and will be removed in a later release; use arg_count(). arg(i) used to return a C string (*u8); it now returns a ?[]const u8 view, so cstr.as_view(os.arg(i)) becomes os.arg(i)!.

Platforms

All of os works on Windows, Linux and macOS. On Uusi (Leivos) the arguments are the ones the program was started with (argument 0 its name; none for a program started without them). There is no environment there yet: env and home_dir are null, pid() is -1, and set_env, cwd and chdir, exe_path and exe_dir, hostname and user_name are Unsupported. random_bytes asks the kernel (its random system call).

Signals: Linux and macOS deliver every listed signal. Windows delivers Interrupt for Ctrl-C and Ctrl-Break, and Terminate when the console closes, the user logs off or the system shuts down (the program then has about five seconds to end itself); Hangup, User1 and User2 are accepted and never arrive. On Uusi signals is Unsupported.

Last modified: