Landin library reference discuss source

Shared: available on every enabled target.

Concepts for reading, writing, files and the process, and world, which composes them; beside them directories, running programs, entropy, terminals and the rest of the file system; the memory world, a formatted write, a buffered writer and a line reader. Time is core/time, the network core/net, and fakes of a system's providers core/io/fake.

The capabilities are concepts, and a program names only the ones it uses:

Concept What it gives Part of world
reader, writer read, write, write_some yes
files open_read, open_write, same_file, close yes
process the standard streams input, out and err, and the arguments yes
directories listing, making and removing directories no
environment the process's variables no
programs running other programs no
file_system opening with options, renaming, status, temporary files and the rest no
terminals asking whether a stream is a terminal, and its size no
entropy fill_random no

The table below shows typical use, as in the tour's first program:

  • Every call takes host, the handle blocking.new() makes in hosted/blocking. A hosted program imports that module beside core/io.
  • A call written with try is in a function that can fail. main cannot fail, so it handles each failure with else instead, as the program at the end of the page does.
Task Calls
Print a formatted line try io.write_format(host, io.out(host), "{} of {}\n", [any(addr done), any(addr total)]) fills each {} with a value and writes the line.
Print many lines mut out := io.buffered_over(host, io.out(host), storage) makes a buffered writer over your buffer, an array such as mut storage: [4096]u8 = zeroed. defer io.flush(out) else none writes what waits on the way out. try io.format(out, "{} of {}\n", [any(addr done), any(addr total)]) fills each {} with a value.
Print bytes as they are try io.put(out, "done\n"), through the same writer.
Write straight to a stream io.write(host, io.err(host), bytes) writes all the bytes, or fails with io_failed.
Get an argument path := try io.argument(host, 0) lends the first argument after the program's name as an OS string, or fails with mem.not_found. io.arguments_length(host) counts the arguments.
Read a file line by line file := try io.open_read(host, path) opens it. mut lines := io.lines_over(host, file, buffer) makes a line reader over your buffer. item := try io.read_line(lines) gives each line in item.bytes until item.at_end. try io.close(host, file) closes the file.

Providers

Use the smallest concept a consumer needs.

A provider is a handle passed by value, host in every signature. Using a handle locks nothing, so a program and its logger write through one handle, and two lent arguments and a query are live at once.

An erased capability is built from the address of a handle binding: any(addr host).

  • It is itself a provider of its concept and of every concept that concept builds on (D492). So an any io.world or an any net.network goes as it is, with no adapter, to io.buffered_over, io.lines_over or any function generic over a writer or a reader.
  • It converts to an any of each concept its own builds on (D1949), so an any io.world is passed where an any io.writer is wanted. It converts to no other any.

Providers retain authority over files and arguments.

process lends the three standard streams, input, out and err, and the argument table, as in io.out(host). Standard input is read through reader like any file, and a read of zero bytes into a nonempty buffer is its end.

A path and an argument are both OS strings, text.os_string, so an argument passed on as a path is never converted.

The memory world

memory_over makes the memory world, firmware's own:

  • It takes each argument as its bytes and lends it back as an OS string, so an argument table can be static data.
  • It fills the caller's memory_space, whose limits and counters stay readable and settable.
  • All storage stays with the caller, the bytes its standard input reads among them.

Its directories are its slots' names before a /, so it lists them and reports their sizes but changes nothing.

It behaves so that a test gets the same answer every run:

  • Files must already be in its table. Opening one for writing truncates it, and reads stop at a file's initialized length.
  • write and write_some keep the blocking provider's contract, which hosted/blocking describes.
  • The read and write limits cut each transfer into chunks.
  • fail_read_at, fail_write_at and fail_close_at fail the call with that one-based count: 1 is the first read, and 0 never fails. Together with the limits, they make short reads, partial writes and failed cleanup reproducible.
  • A zero read limit with data still unread is an error, not the end of the input.
  • written and written_errors lend what was written to standard output and standard error.
  • Its arguments, like the system provider's, never include the program's own name, argv[0].

Directories

directories is a fifth concept beside the four, and not part of world: a program that lists, makes or removes directories names it as well.

open_directory takes a buffer from the caller and returns an open directory. The caller passes that value inout to read_entry, which refills the buffer as entries are read.

  • Each entry's name is an OS string that need not be UTF-8, lent from the directory until the next read. The compiler refuses a name kept past that read [0800], so a program copies the names it keeps.
  • The entry read after the last one has at_end true, and its name is empty.
  • . and .. are never listed, and the order is the system's.
  • entry_size asks the open directory for an entry's size by name, as a u64. It does not follow a symbolic link.

make_directory, remove_file and remove_directory change the tree:

  • A missing path is mem.not_found, and so is a path through or to something that is not a directory.
  • A name already taken is already_exists.

Close a directory through the provider that opened it. A directory dropped unclosed keeps what its provider holds for it until the process ends.

The environment

The environment is a concept of its own, environment, outside world. So a provider without an environment carries none of it, and neither does a firmware image that never asks for one.

  • variables_length counts its variables.
  • variable lends the one at an index, a text.os_variable of a name and a value.
  • variable_named lends the value of the first variable with a name, compared as bytes, or reports mem.not_found.

Names and values are OS strings that need not be UTF-8, lent from the provider for as long as the process lives.

The environment is read-only. A process does not change its own environment, which is not safe beside threads. It gives a child the environment it builds instead.

variables_over makes a memory_variables: an environment provider over a table of NAME=value byte strings, which firmware can declare as data.

Running programs

programs is a seventh concept, outside world, for running other programs.

spawn starts the program at a path, an OS string:

  • The path is never looked up on a search path or handed to a shell.
  • The program sees the arguments after its own name, and gets exactly the variables the caller builds.
  • A missing program is mem.not_found, and one the caller may not run is no_access. On any failure no child exists.

Each of the program's three standard streams is connected as child_streams says:

Choice The child's stream is
inherited the parent's own, and closed when the parent has it closed
discarded connected to nothing
piped a pipe whose other end the parent holds

inheriting, capturing and discarding are the common three combinations.

spawn returns a child: a value the caller keeps, whose fields are the provider's. These calls take it:

  • write_child writes to its piped input, and close_input closes that input, so that the child reads to its end.
  • read_child reads its piped output and errors together. It waits until either has something and says which it read, so a child that writes more than a pipe holds to both never waits on a full one.
  • wait closes every pipe the child still has, waits for the child, and reports how it ended. The ending's variant part is exited with a code, or signalled, ended from outside, whose detail is the provider's. wait consumes the child.

A child never waited for keeps what its provider holds for it until the process ends. Close a child's pipes only through close_input and wait, never through files.

write_child, read_child and wait wait, while spawn and close_input do not. So, like reading and writing, programs comes from a provider that waits: hosted/blocking or hosted/events, not hosted/system.

Such a provider's pipes are its own files, which its reader and writer also take, a line reader over a child's output among them. The memory world's are not.

scripted_over makes scripted_programs: a programs provider beside a memory world, over the caller's scripted_program records. It is a provider of its own, so a world that runs no program carries none of its code. A scripted program at a given path:

  • writes the given output and errors when it starts, as each stream says, an inherited one into the world's own standard output or error;
  • keeps what its parent writes to its piped input with write_child;
  • records how many arguments and variables it was given and how often it started;
  • ends as it is told.

The rest of the file system

The rest of the file system is file_system, a concept of its own beside files and directories and outside world, so an image that never renames or truncates carries none of it.

open_file opens a path as an open_options record says:

Option Meaning
read, write open to read, to write, or both
append every write goes at the end
create create the file when it is missing
exclusive with create, refuse a name that is taken, with already_exists
truncate cut the file to nothing

updating, appending and creating are the common three. valid_options says whether an open can have the options at all, and io_failed answers one that cannot.

The concept's other calls:

Call What it does
rename Give a file a new name in one step, replacing a file already called that.
status_of, status_of_file A path's or an open file's status.
set_read_only Make a file read-only, or writable by its owner again.
synchronize Have what the provider holds of a file written where it lasts, its data alone when asked.
truncate Give a file a size, cutting it or extending it with zeros.
read_at, write_at Read and write at an offset.
make_temporary Make and open a new file under a fresh name.
current_directory Put the working directory's path in the caller's buffer.

rename is how a whole file is replaced: write a make_temporary file beside it, synchronize it, close it and rename it onto the old one.

  • A missing name is mem.not_found.
  • A rename across file systems is io_failed, and so is a rename of a directory onto a file or of a file onto a directory.

A status holds the size, its entry_kind, the last modification as a wall_time and whether it is read_only. status_of follows a final symbolic link or not, as asked.

platform/posix uses the name differently: its status_of takes an open descriptor, as status_of_file does here, and its call for a path is status_at.

A read-only file refuses an open for writing with no_access. The exception is a process the system lets past every permission, as POSIX's root.

read_at and write_at do not move the position read and write use.

  • A read at or past the end reads nothing.
  • A write past the end leaves zeros between, a hole a file system may keep without storing it.
  • Never write at an offset to a file opened to append.

make_temporary creates the file in a directory under a name no other file has: .tmp- and sixteen hexadecimal digits, drawn from an entropy the caller passes. In an ordinary program that is the hosted provider itself: io.make_temporary(host, host, directory, buffer).

  • The file is created exclusively, readable and writable by its owner alone.
  • A name already taken is tried again with fresh bytes, up to temporary_tries times.
  • Nothing removes it: rename it into place or remove it.
  • temporary_path is the name it tries, for a provider in another module.

Nothing here changes the working directory, since it is the whole process's.

Terminals

Terminals are terminals, one more concept outside world.

  • is_terminal says whether a stream is one.
  • window_size gives its window, rows and columns, or not_a_terminal.

Entropy

entropy is another concept outside world, with one entry: fill_random fills all of a buffer with unpredictable bytes or fails io_failed.

Its fake is fake.entropy, made by entropy_over over the caller's bytes. It gives those bytes in order, and a request for more than is left fails, so a program that seeds itself from entropy runs the same in every test. It runs on Cortex-M0.

A formatted write

write_format writes a template and its values to a file in one call:

io.write_format(host, io.out(host), "{} of {}\n", [any(addr done), any(addr total)])

  • Each {} takes the next value, as text.format's does.
  • The template is checked against the values before anything is written, so a mismatch is text.invalid_format and writes nothing.
  • The text is formatted into format_store bytes on the stack, 128, and written whenever they fill and at the end. A line that fits is one write, and a value of any length goes out in pieces, so it never reports mem.out_of_memory.
  • A failed write is io_failed and may follow partial output, as write's may.
  • It needs no buffer or flush of the caller's, and works on every target that has a writer.

The buffered writer

A program that writes many pieces to one file gathers them in a buffered writer, which makes fewer, larger writes. A buffered_writer gathers the writes to one file in a buffer the caller supplies, and hands them to a writer when the buffer fills or the program flushes it.

Call What it does
buffered_over Make one from a writer's handle, the file and the buffer.
put Write bytes through it, flushing first when they do not fit. Bytes as large as the buffer go straight to the writer.
format Format a template straight into its buffer, flushing first when the text does not fit after what waits.
flush Write what waits.
buffered_length Say how many bytes wait.

A failed flush reports the writer's io_failed and keeps every byte the writer did not take, so the next flush writes each byte once.

format refuses a text larger than the whole buffer with mem.out_of_memory. Its values are text.formattable's types, as text.format's are: integers, bool, utf8, []u8 and OS strings. Floats are not formattable yet. To print one, scale it to an integer and print the parts, as append_hundredths in core/text does for two decimals.

There are no destructors: bytes still waiting when the program ends are never written. So flush in two places:

  • before every way out, defer io.flush(out) else ... being the idiom;
  • where the bytes must be out before something else happens.

The line reader

A line_reader reads a file through a buffer the caller supplies and lends it back a line at a time, as a directory lends its names.

  • lines_over makes one from a reader's handle, the file and the buffer.
  • read_line gives the next line, its bytes lent from the reader until the next read.

What counts as a line:

  • A line ends at \n, and a \r just before it is part of the ending, so text written with \r\n reads as text written with \n.
  • A lone \r is an ordinary byte, and the last line need not end.
  • The end of input is a line that has at_end, every time it is asked again; an empty line has not.
  • A line that does not fit the buffer with its ending is mem.out_of_memory, and the rest of it is skipped, so the next read gives the line after it.

Items

directories

concept core/io/io.ldn:202 ↑ index
public directories: type = concept (provider: type)

Open the directory at path and read it into the caller's buffer. A missing path and a path through or to something that is not a directory are mem.not_found.

    open_directory: (self: provider, path: text.os_string,
                     buffer: []mut u8)
                    -> (opened: directory from buffer)
                    ! mem.not_found | no_access | io_failed

The directory's next entry, lent from it until the next read, or an entry whose at_end is true at its end.

    read_entry: (self: provider, inout opened: directory)
                -> (item: entry from opened) ! io_failed

The size in bytes of the entry name in the open directory, not following a symbolic link. A name no entry can have is mem.not_found: an empty one, one with a / or a NUL, or one longer than directory_buffer allows. So is an entry removed since it was read.

    entry_size: (self: provider, opened: directory,
                 name: text.os_string)
                -> (size: u64) ! mem.not_found | no_access | io_failed

Close the directory, which is consumed even when this fails.

    close_directory: (self: provider, sink opened: directory)
                     -> none ! io_failed

Make a directory at path. A path whose parent is missing is mem.not_found; a name already taken is already_exists.

    make_directory: (self: provider, path: text.os_string)
                    -> none ! already_exists | mem.not_found | no_access | io_failed

Remove the file path names, not a directory.

    remove_file: (self: provider, path: text.os_string)
                 -> none ! mem.not_found | no_access | io_failed

Remove the empty directory path names.

    remove_directory: (self: provider, path: text.os_string)
                      -> none ! mem.not_found | no_access | io_failed
end directories

Directory listing and the names in a tree: open a directory and read its entries, ask an entry's size, make and remove directories, and remove files. Paths are OS strings, as files takes them, and an entry's name is one too, so a name read is given back unconverted. A listing leaves out . and .., which every directory holds and which are no entries of its own. It gives its entries in the system's order, and no provider promises an order.

public entropy: type = concept (provider: type)

Fill all of into with random bytes, or fail.

    fill_random: (self: provider, into: []mut u8) -> none ! io_failed
end entropy

The system's source of unpredictable bytes, fit for seeding a generator or making a key. fill_random fills all of into or fails io_failed, and after a failure what into holds is not to be used. It is a concept of its own, outside world, so a provider without a source, and a firmware image that never asks for one, carries none.

environment

concept core/io/io.ldn:136 ↑ index
public environment: type = concept (provider: type)

Return the count of the environment's variables.

    variables_length: (self: provider) -> (count: usize)

Lend the environment variable at an index; the provider retains its backing. Report mem.not_found when out of range.

    variable: (self: provider, index: usize)
              -> (item: text.os_variable from self) ! mem.not_found

Lend the value of the first variable named name, compared as the system stores names, with no normalization. Report mem.not_found when none has that name.

    variable_named: (self: provider, name: text.os_string)
                    -> (value: text.os_string from self) ! mem.not_found
end environment

The process's environment: its variables, each a name and a value that are OS strings whose storage belongs to the provider, in the order the provider keeps them. It is read-only: a process does not change its own environment, which is not safe beside threads, and gives a child the one it builds instead (programs). It is a concept of its own, outside world, so a provider that has no environment, and a firmware image that never asks for one, carries none.

public file_system: type = concept (provider: type) is files

Open the file at path as options say. A missing file without create, and a path through something that is not a directory, are mem.not_found; a file that exclusive finds is already_exists; a file the provider may not open that way is no_access; and a path that names a directory is is_a_directory.

    open_file: (self: provider, path: text.os_string,
                options: open_options)
               -> (opened: file)
               ! already_exists | mem.not_found | no_access | is_a_directory
               | io_failed

Give the file at old the name new in one step, replacing a file already named new; a missing old or parent of new is mem.not_found. A rename does not cross from one file system to another, which is io_failed, and never puts a directory where a file is or a file where a directory is.

    rename: (self: provider, old: text.os_string, new: text.os_string)
            -> none ! mem.not_found | no_access | io_failed

The status of the file at path. When the path ends in a symbolic link, following gives the status of the link's target and not_following that of the link itself.

    status_of: (self: provider, path: text.os_string,
                links: link_handling)
               -> (found: status) ! mem.not_found | no_access | io_failed

The status of an open file.

    status_of_file: (self: provider, opened: file) -> (found: status)
                    ! io_failed

Make the file at path read-only, or with read_only false writable by its owner again.

    set_read_only: (self: provider, path: text.os_string, read_only: bool)
                   -> none ! mem.not_found | no_access | io_failed

Write what the provider holds of an open file to lasting storage before returning: its data and its status, or with data_only its data and only the status that reading it back needs.

    synchronize: (self: provider, opened: file, writing: synchronizing)
                 -> none ! io_failed

Give a file open for writing the size size, cutting it or extending it with zeros.

    truncate: (self: provider, opened: file, size: u64) -> none ! io_failed

Read at most lenof into bytes of an open file from offset on, without moving the position read reads from. It returns the count, which is zero at or past the file's end.

    read_at: (self: provider, opened: file, offset: u64, into: []mut u8)
             -> (count: usize) ! io_failed

Write all of bytes into an open file from offset on, without moving the position write writes at; a write past the end extends the file, and what lies between reads as zeros. Never write at an offset to a file opened to append.

    write_at: (self: provider, opened: file, offset: u64, bytes: []u8)
              -> none ! io_failed

Make and open a new file in directory, empty for the working directory, and put its path in into. Its name, which no other file has, is made from bytes of names and tried again when it is taken. It is made exclusively, so no other program's file is ever opened in its place, and readable and writable by its owner alone.

    make_temporary: (self: provider, names: any entropy,
                     directory: text.os_string, into: []mut u8)
                    -> (made: temporary from into)
                    ! mem.not_found | no_access | io_failed

The working directory's path, put in into; a path too long for it is io_failed. Nothing here changes the working directory, which is the whole process's: make a path from this one instead.

    current_directory: (self: provider, into: []mut u8)
                       -> (path: text.os_string from into) ! io_failed
end file_system

The rest of the file system: a concept that refines files, so a provider of it opens with open_read, open_write and open_file and closes every one of them with files' close, the one close a file has. It is outside world, beside directories. A path is a host string, as files takes one, and a file is a file of the provider, which reader, writer and files take too.

public files: type = concept (provider: type)

Open a path for reading; return a handle owned by this provider. A path that names a directory is is_a_directory: a directory is read through directories.

    open_read: (self: provider, path: text.os_string)
               -> (opened: file)
               ! mem.not_found | no_access | is_a_directory | io_failed

Open a path for writing; creation and truncation follow the provider contract. A path that names a directory is is_a_directory.

    open_write: (self: provider, path: text.os_string)
                -> (opened: file)
                ! mem.not_found | no_access | is_a_directory | io_failed

Report whether two paths name the same file. The left path is an existing input: if it names no file the provider can examine, the call fails with io_failed. The right path may be an output not yet created: if it does not exist, the answer is false.

    same_file: (self: provider, left: text.os_string,
                right: text.os_string)
               -> (same: bool) ! io_failed

Consume a file handle, including when closure reports a failure.

    close: (self: provider, sink opened: file)
           -> none ! io_failed
end files

File lifecycle and identity capability. A path is an OS string, as the system gives and takes it (text.os_string); a literal one is text.os_string_over("name"). A successful open returns a provider-specific handle; close consumes the handle even when reporting failure.

process

concept core/io/io.ldn:113 ↑ index
public process: type = concept (provider: type)

Return the standard-input handle, which a reader reads to end of input.

    input: (self: provider) -> (stream: file)

Return the standard-output handle.

    out: (self: provider) -> (stream: file)

Return the standard-error handle.

    err: (self: provider) -> (stream: file)

Return the count of available arguments.

    arguments_length: (self: provider) -> (count: usize)

Lend an argument by index; the provider retains its backing. Report mem.not_found when out of range.

    argument: (self: provider, index: usize)
              -> (value: text.os_string from self) ! mem.not_found
end process

Standard streams and argument access. An argument is an OS string whose storage belongs to the provider; stream identifiers have provider-specific lifetime and closing rules.

public programs: type = concept (provider: type)

Start the program at path with arguments, which it sees after its own name, and exactly the variables of environment. A missing program is mem.not_found, one the caller may not run no_access; on any failure no child was started.

    spawn: (self: provider, path: text.os_string,
            arguments: []text.os_string, environment: []text.os_variable,
            streams: child_streams)
           -> (started: child) ! mem.not_found | no_access | io_failed

Write all of bytes to the child's piped input, waiting while its pipe is full.

    write_child: (self: provider, inout started: child, bytes: []u8)
                 -> none ! io_failed

Close the child's piped input, so that it reads to its end.

    close_input: (self: provider, inout started: child) -> none ! io_failed

Read into into the next bytes the child wrote, from whichever of its piped output and errors has something.

    read_child: (self: provider, inout started: child, into: []mut u8)
                -> (taken: child_read) ! io_failed

Wait for the child to end, close every pipe it still has, and report how it ended. The child is consumed even when this fails.

    wait: (self: provider, sink started: child)
          -> (ended: ending) ! io_failed
end programs

Starting programs and waiting for them. A program is named by its path, a host string, never looked up on a search path nor given to a shell; a child is given exactly the arguments and the environment its parent builds, and inherits no other open file. Each of its three standard streams is inherited, discarded or piped (child_streams).

A child that writes more than a pipe holds waits for its parent to read it, so read a piped output while the child runs. When output and errors are both piped, read them through read_child, which reads whichever has something: reading one to its end first can wait forever while the child waits to write the other. Close a piped input before waiting for a child that reads it to its end.

write_child, read_child and wait wait; spawn and close_input do not. So a provider supplies the concept whose waiting is its own: the hosted one that blocks the thread, or one that switches fibres while a child's pipe or its end is awaited (D479).

reader

concept core/io/io.ldn:72 ↑ index
public reader: type = concept (provider: type)

Read at most the destination length and return progress; zero into a nonempty destination is end of input, and an empty one always reads zero.

    read: (self: provider, opened: file, into: []mut u8)
          -> (count: usize) ! io_failed
end reader

Input capability. read puts at most lenof into bytes in the caller's slice and returns the count; zero for a nonempty slice means end of input. The slice's storage stays the caller's.

public terminals: type = concept (provider: type)

Whether stream is a terminal.

    is_terminal: (self: provider, stream: file) -> (yes: bool)

The size of the terminal stream, not_a_terminal for anything else. A terminal nothing has sized is zero by zero.

    window_size: (self: provider, stream: file) -> (size: window)
                 ! not_a_terminal | io_failed
end terminals

Terminals: whether a stream is one, and its size, so a program can tell a person from a pipe and fit what it writes to the screen. A concept of its own, outside world.

public world: type = concept (provider: type) is reader, writer, files, process
end world

Composition of reader, writer, files and process over one provider type. Consumers should request the narrowest capability they use. directories, environment, programs, clock, wall_clock, entropy, network, file_system and terminals are not part of it: a program that lists a directory, reads its environment, runs another program, reads the time, wants random bytes, uses the network, renames a file or asks for a terminal names that concept as well.

writer

concept core/io/io.ldn:58 ↑ index
public writer: type = concept (provider: type)

Write the entire byte slice or fail. Failure may follow partial output; do not blindly retry the whole slice.

    write: (self: provider, opened: file, bytes: []u8)
           -> none ! io_failed

Attempt one transfer. Nonempty success returns positive progress, empty success returns zero, and failure transfers no bytes.

    write_some: (self: provider, opened: file, bytes: []u8)
                -> (count: usize) ! io_failed
end writer

The capability is four narrow concepts over one provider type, so a provider supplies only what it has: a UART is a writer and a read-only image is a reader, while system and memory providers also supply files and process entries. A consumer names the narrowest concept it uses; diagnostics stream through any io.writer. world composes all four for roots that have everything. A provider is a handle: a small copyable value, passed by value, that refers to its state, so a program and its logger share one without locking it. An erased any io.world is built from the address of a handle binding and dispatches every entry, and it converts to an any of each concept it is built on: where an any io.writer is wanted, pass the world (D1952).

buffered_writer

type core/io/buffered.ldn:23 ↑ index
public buffered_writer: type (provider: type) = buffered_value(provider)

Gathers writes to one file in a buffer the caller supplies, and writes them through a writer when the buffer fills or flush is called: a value the caller keeps and edits, passing it inout. There are no destructors, so bytes still in the buffer when the program ends, or when the writer is dropped, are never written. Flush before every way out, with defer io.flush(out) else ... as the idiom, and flush again wherever the bytes must be out before something else happens, as before reading a reply. buffered_length says how many bytes are waiting.

public child: type = struct
    handle: usize
    input: file
    output: file
    errors: file
    input_open: bool
    output_open: bool
    errors_open: bool
end child

A started child: a value its caller keeps and edits, passing it inout to write_child, close_input and read_child, and sink to wait, which consumes it. Its fields are the provider's. handle names the child. input, output and errors are the parent's ends of its piped streams; each is open while its *_open flag says so, which it is only for a piped stream. The child owns its pipes: close_input closes its input, read_child closes an output it has read to the end, and wait closes the rest. Where the pipes are the provider's own files, as the hosted provider's are its descriptors, a program may read and write them through reader and writer as well, for example through a line reader over a child's output; never close them through files. A child that is never waited for keeps what its provider holds for it until the process ends: there are no destructors.

public child_read: type = struct
    count: usize
    stream: child_stream
    finished: bool
end child_read

What read_child read: count bytes from the child's stream, or nothing and finished, once every piped output stream of the child has reached its end.

public child_stream: type = child_output | child_errors

Which of a child's streams read_child read: child_output or child_errors.

child_streams

type core/io/programs.ldn:23 ↑ index
public child_streams: type = struct
    input: stream_choice
    output: stream_choice
    errors: stream_choice
end child_streams

How each of a child's three standard streams is connected.

directory

type core/io/io.ldn:175 ↑ index
public directory: type = struct
    handle: usize
    stream: usize
    position: usize
    buffer: []mut u8
    filled: usize
    consumed: usize
    at_end: bool
end directory

An open directory: a value its caller keeps and edits, passing it inout to read_entry, which refills buffer with entries as the caller reads them. The buffer is the caller's, so each entry's name is lent from the directory and is good until the next read; copy a name to keep it. The other fields belong to the provider that opened it: its own handle and stream for the directory, and how much of the buffer it has filled and read. Close it through the same provider. A directory dropped without closing keeps what its provider holds for it until the process ends: there are no destructors, and a defer closes it on every exit.

public ending: type = struct
    how: variant
        exited: (code: i32) |
        signalled
    end how
end ending

How a child ended: it exited with the code its program returned, or it was signalled, ended from outside before it returned one. What ended it is the system's detail, which its provider keeps; on POSIX it is a signal. A POSIX code is the low eight bits of the program's value, 0 to 255.

public entry: type = struct
    name: text.os_string
    kind: entry_kind
    at_end: bool
end entry

One entry of a directory: its name, an OS string as the system stores it, which need not be UTF-8, and what it is. read_entry returns an entry whose at_end is true at the end of the directory, its name then empty; every other entry's at_end is false.

entry_kind

type core/io/io.ldn:31 ↑ index
public entry_kind: type = regular | subdirectory | symbolic_link | special

What a directory entry names: regular, subdirectory, symbolic_link or special.

public file: type = file_value

Provider-defined file identifier. Use a handle only with the provider that created it and obey that provider's close rules.

public line: type = struct
    bytes: []u8
    at_end: bool
end line

One line, lent from its reader: its bytes without the line ending, and at_end, whether the input had already ended. When it had, there is no line and bytes is empty. An empty line has empty bytes too, and at_end false.

line_reader

type core/io/lines.ldn:20 ↑ index
public line_reader: type (provider: type) = lines_value(provider)

Reads a file a line at a time through a buffer the caller supplies: a value the caller keeps and edits, passing it inout to read_line. Each line is lent from the reader until the next read, which reuses the buffer it lies in, so a program that keeps a line copies it.

public memory: type = memory_value

In-memory world as a handle over caller-owned state, file slots, standard-input bytes, output buffers and argument bytes: copies share one world. No host handles or implicit allocator are used.

memory_file

type core/io/memory.ldn:21 ↑ index
public memory_file: type = struct
    name: []u8
    contents: []mut u8
    length: usize
    cursor: usize
    readable: bool
    writable: bool
    state: slot_state
end memory_file

Caller-supplied in-memory file slot holding its path, readable bytes and writable capacity. The caller gives it closed_slot. Keep all referenced ranges alive and initialize the fields before constructing the provider.

The provider over these slots is finite and caller-backed. Handles are indexes, not ownership tokens. Clients preserve length <= capacity and supply all retained backing.

memory_space

type core/io/memory.ldn:37 ↑ index
public memory_space: type = struct
    input_used: usize
    output_used: usize
    errors_used: usize
    input_open: bool
    output_open: bool
    errors_open: bool
    read_limit: usize
    write_limit: usize
    fail_read_at: usize
    fail_write_at: usize
    fail_close_at: usize
    reads: usize
    writes: usize
    closes: usize
    opens: usize
end memory_space

Caller-owned state of an in-memory world: how much standard input, output and error it has used, which standard streams are open, the injected limits and failures, and call counts. Declare it zeroed and give it to memory_over, which fills it; it must outlive the world's handle. Its fields may be read, and the limits and failure points set, while the handle is in use.

memory_variables

type core/io/memory.ldn:541 ↑ index
public memory_variables: type = variables_value

An environment over the caller's entries, each the bytes of one NAME=value as a process is given them, so a firmware test declares them as data: a provider of environment alone, as a test's process has one. The name is the bytes before the first =, and an entry with no = is a name with an empty value. Copies share the entries.

public open_options: type = struct
    read: bool
    write: bool
    append: bool
    create: bool
    exclusive: bool
    truncate: bool
end open_options

How open_file opens a file. read and write open it to read, to write, or both. append sends every write to the end of the file, and truncate cuts it to nothing as it is opened; each needs write. create makes the file when it is missing. exclusive with create refuses a file that is there, already_exists, and no other program can come in between. Any other combination, and one that neither reads nor writes, is io_failed before the provider is asked.

scripted_program

type core/io/memory.ldn:612 ↑ index
public scripted_program: type = struct
    path: []u8
    output: []u8
    errors: []u8
    ending: ending
    input: []mut u8
    input_length: usize
    starts: usize
    arguments: usize
    variables: usize
    output_read: usize
    errors_read: usize
    running: bool
end scripted_program

A scripted child: a program a scripted provider runs in place of a real one. What it writes and how it ends are given, and what it was given is recorded. spawn of a path equal to path starts it. It writes output and errors all at once when it starts: to the world's own standard output and error when a stream is inherited, to nothing when one is discarded, and into its pipe when one is piped, from which read_child reads them, output first. wait reports ending, that it exited with a code or was signalled. What the parent writes to its piped input with write_child is kept in input, its first input_length bytes, and a write that does not fit is io_failed, where a real pipe would wait. The child's pipes are no files of the world: reader and writer refuse them. The caller sets path, output, errors, ending and input and zeroes the rest, which the provider keeps: how many times it was started, how many arguments and variables its last start gave it, how much of its output and errors the parent has read, and whether it is running. One record is one child at a time: starting a running one is io_failed.

scripted_programs

type core/io/memory.ldn:631 ↑ index
public scripted_programs: type = scripted_value

A provider of programs whose children are the caller's scripted records. It writes an inherited stream into a memory world's own standard output or error, and counts its reads in that world's state, whose read limit and failure point apply to them. Copies share the records.

public slot_state: type = closed_slot | reading_slot | writing_slot

What a memory file slot is open for: closed_slot, reading_slot or writing_slot, the provider's to change.

public status: type = struct
    size: u64
    kind: entry_kind
    modified: time.wall_time
    read_only: bool
end status

The little that every system says alike about a file: its size in bytes, what it is, when it was last written, as the wall clock read then, and whether it is read_only, which on POSIX means that no permission bit lets anyone write it. A directory's own size differs between file systems; a symbolic link's is the length of its target.

stream_choice

type core/io/programs.ldn:20 ↑ index
public stream_choice: type = inherited | discarded | piped

How one of a child's standard streams is connected: inherited, discarded or piped.

public synchronizing: type = data_and_status | data_only

What synchronize writes to lasting storage.

public temporary: type = struct
    opened: file
    path: text.os_string
end temporary

A temporary file make_temporary made: the file, open for reading and writing, and its path, the directory's path and a name of its own, lent from the caller's buffer. Nothing removes it: on every way out, close it, then rename it into place or remove it.

public window: type = struct
    rows: u16
    columns: u16
end window

A terminal's size in characters: its rows and columns.

already_exists

atom core/io/io.ldn:18 ↑ index
public already_exists: atom

The name a file or directory was to be made at is already taken.

public child_errors: atom

A read from the child's errors.

public child_output: atom

A read from the child's output.

closed_slot

atom core/io/memory.ldn:5 ↑ index
public closed_slot: atom

A memory file slot that no file is open on.

data_and_status

atom core/io/file_system.ldn:52 ↑ index
public data_and_status: atom

Write a file's data and its status to lasting storage.

public data_only: atom

Write a file's data and only the status that reading it back needs.

public discarded: atom

A child's standard stream is connected to nothing: its input reads at once to its end, and what it writes is thrown away.

public following: atom

Follow a symbolic link that ends a path to what it names.

public inherited: atom

A child's standard stream is the parent's own: it reads or writes where the parent does.

io_failed

atom core/io/io.ldn:14 ↑ index
public io_failed: atom

An I/O operation failed. Provider-specific diagnostic detail, if offered, must be read from that provider.

is_a_directory

atom core/io/io.ldn:16 ↑ index
public is_a_directory: atom

The path names a directory where a file was to be opened.

no_access

atom core/io/io.ldn:11 ↑ index
public no_access: atom

The provider refused access to the requested file.

public not_a_terminal: atom

The file is not a terminal.

public not_following: atom

Take a symbolic link that ends a path as itself.

public piped: atom

A child's standard stream is a pipe the parent holds the other end of: the parent writes the child's input, or reads its output or errors.

reading_slot

atom core/io/memory.ldn:7 ↑ index
public reading_slot: atom

A memory file slot open for reading.

public regular: atom

A directory entry that is a regular file.

public special: atom

A directory entry that is anything else: a device, a pipe or a socket.

subdirectory

atom core/io/io.ldn:23 ↑ index
public subdirectory: atom

A directory entry that is itself a directory.

writing_slot

atom core/io/memory.ldn:9 ↑ index
public writing_slot: atom

A memory file slot open for writing.

public appending: open_options = (read: false, write: true, append: true,
                                  create: true, exclusive: false,
                                  truncate: false)

Write at the end of a file, making it when it is missing: a log.

public capturing: child_streams = (input: discarded, output: piped,
                                   errors: piped)

The child's output and errors piped to the parent, which reads them with read_child, and its input discarded.

public creating: open_options = (read: true, write: true, append: false,
                                 create: true, exclusive: true,
                                 truncate: false)

Read and write a new file, refusing a name that is taken with already_exists: the file is this program's alone.

directory_buffer

value core/io/io.ldn:163 ↑ index
public directory_buffer: usize = 1024

The fewest bytes a directory's buffer may have: room for any entry any provider here gives, with its record and its terminator. The hosted provider refuses a smaller buffer when the directory is opened.

public discarding: child_streams = (input: discarded, output: discarded,
                                    errors: discarded)

Every stream connected to nothing: a child run for how it ends.

public inheriting: child_streams = (input: inherited, output: inherited,
                                    errors: inherited)

Every stream the parent's own: the child reads and writes where the parent does.

temporary_tries

value core/io/file_system.ldn:264 ↑ index
public temporary_tries: usize = 16

How many times make_temporary tries a name before it gives up with io_failed: a name is taken only when the bytes names gives repeat.

public updating: open_options = (read: true, write: true, append: false,
                                 create: false, exclusive: false,
                                 truncate: false)

Read and write a file that exists, from its start, keeping what it holds.

argument

function core/io/io.ldn:434 ↑ index
public argument: (provider: type is process, host: provider, index: usize)
                 -> (value: text.os_string from host) ! mem.not_found

Lend the process argument at index as an OS string; index zero is the first argument after the program's name, C's argv[1]. An index that names no argument reports mem.not_found. The provider's argument backing must outlive the string's use. Convert it to utf8 with the checked text.from_os_string, or take its bytes with text.os_string_bytes; passed on as a path, it is not converted at all.

arguments_length

function core/io/io.ldn:423 ↑ index
public arguments_length: (provider: type is process, host: provider)
                         -> (count: usize)

Return the number of arguments after the program's name, which is not one of them: C's argc - 1.

buffered_length

function core/io/buffered.ldn:101 ↑ index
public buffered_length: (provider: type, out: buffered_writer(provider))
                        -> (count: usize)

How many bytes are waiting in the buffer for the next flush.

buffered_over

function core/io/buffered.ldn:28 ↑ index
public buffered_over: (provider: type is writer, host: provider,
                       opened: file, buffer: []mut u8)
                      -> (out: buffered_writer(provider) from buffer, host)

A buffered writer over the caller's buffer, writing to opened through host. It holds a copy of the writer's handle; keep the handle's state and the buffer alive while it is used.

close

function core/io/io.ldn:268 ↑ index
public close: (provider: type is files, host: provider,
               sink opened: file) -> none ! io_failed

Close a provider handle. Treat it as consumed even if closing reports io_failed; do not retry a consumed handle.

close_directory

function core/io/io.ldn:302 ↑ index
public close_directory: (provider: type is directories, host: provider,
                         sink opened: directory) -> none ! io_failed

Close a directory. Treat it as consumed even if closing reports io_failed.

close_input

function core/io/programs.ldn:159 ↑ index
public close_input: (provider: type is programs, host: provider,
                     inout started: child) -> none ! io_failed

Close a child's piped input, which it then reads to its end. Closing an input that is not open is io_failed.

current_directory

function core/io/file_system.ldn:255 ↑ index
public current_directory: (provider: type is file_system, host: provider,
                           into: []mut u8)
                          -> (path: text.os_string from into)
                          ! io_failed

The working directory's path, put in into.

entry_size

function core/io/io.ldn:294 ↑ index
public entry_size: (provider: type is directories, host: provider,
                    opened: directory, name: text.os_string)
                   -> (size: u64) ! mem.not_found | no_access | io_failed

The size in bytes of the entry name in an open directory, without following a symbolic link.

public err: (provider: type is process, host: provider)
            -> (stream: file)

Return the provider's standard-error identifier.

fill_random

function core/io/entropy.ldn:19 ↑ index
public fill_random: (provider: type is entropy, host: provider,
                     into: []mut u8) -> none ! io_failed

Fill all of into with random bytes from the provider, through entropy evidence. On a hosted system they are the system's own, getrandom on Linux and arc4random_buf on Darwin and FreeBSD.

public flush: (provider: type is writer,
               inout out: buffered_writer(provider)) -> none ! io_failed

Write the waiting bytes to the file. A failure reports the writer's io_failed and keeps every byte the writer did not take at the front of the buffer, so a later flush writes each byte exactly once.

Flushing goes through write_some, whose failure transfers nothing, so what the writer took is known exactly; a writer that makes no progress on nonempty bytes breaks the writer contract and is reported as failing rather than asked forever.

public format: (provider: type is writer,
                inout out: buffered_writer(provider), template: utf8,
                values: []any text.formattable)
               -> none ! mem.out_of_memory | text.invalid_format | io_failed

Format template with values, as text.format does, straight into the buffer: the caller needs no builder and nothing is copied. The text goes into the room after the waiting bytes; when it does not fit there, the buffer is flushed and the text goes into the whole buffer. A text larger than the whole buffer is mem.out_of_memory and leaves nothing of itself waiting; a template that does not fit its values is text.invalid_format; a failed flush is the writer's io_failed. The values are text.formattable's types. Floats are not formattable yet: scale one to an integer and print the parts, as core/text's reference program does for two decimals. The buffered writer and the builder are the byte sinks a template is formatted into.

input

function core/io/io.ldn:404 ↑ index
public input: (provider: type is process, host: provider)
              -> (stream: file)

Return the provider's standard-input identifier. Read it through reader evidence; zero from a nonempty read is end of input.

is_terminal

function core/io/file_system.ldn:328 ↑ index
public is_terminal: (provider: type is terminals, host: provider,
                     stream: file) -> (yes: bool)

Whether stream is a terminal, through terminals evidence: false for a file, a pipe and a socket, whatever a program writes to it.

lines_over

function core/io/lines.ldn:36 ↑ index
public lines_over: (provider: type is reader, host: provider, opened: file,
                    buffer: []mut u8)
                   -> (lines: line_reader(provider) from buffer, host)

A line reader over the caller's buffer, reading opened through host. It holds a copy of the reader's handle; keep the handle's state and the buffer alive while it is used. A line's \n must fit in the buffer too, so the longest line it reads with one is one byte shorter than the buffer. An unterminated final line may fill it.

make_directory

function core/io/io.ldn:308 ↑ index
public make_directory: (provider: type is directories, host: provider,
                        path: text.os_string)
                       -> none
                       ! already_exists | mem.not_found | no_access | io_failed

Make a directory at a path.

make_temporary

function core/io/file_system.ldn:245 ↑ index
public make_temporary: (provider: type is file_system, host: provider,
                        source: type is entropy, names: source,
                        directory: text.os_string, into: []mut u8)
                       -> (made: temporary from into)
                       ! mem.not_found | no_access | io_failed

Make and open a new file in directory under a name no other file has, made from bytes the entropy names gives, and put its path in into. into must hold the directory, a / and the 21 bytes of the name: .tmp- and sixteen hexadecimal digits, which an ordinary listing hides. The hosted provider is its own entropy too, io.make_temporary(host, host, directory, buffer), and a test passes scripted bytes to see a name that is taken tried again.

memory_over

function core/io/memory.ldn:76 ↑ index
public memory_over: (inout state: memory_space, files: []mut memory_file,
                     arguments: [][]u8, input: []u8,
                     output: []mut u8, errors: []mut u8)
                    -> (host: memory from state, files, arguments, input,
                        output, errors)

Fill state for an in-memory world over the supplied records and buffers and return its handle. The handle retains their references; keep them alive and avoid conflicting access while it is used. arguments are the bytes of each process argument, which the world lends as host strings, so a firmware test can declare its table as data. input is what standard input reads, from its first byte to end of input; pass "" for a world whose standard input is empty.

open_directory

function core/io/io.ldn:276 ↑ index
public open_directory: (provider: type is directories, host: provider,
                        path: text.os_string, buffer: []mut u8)
                       -> (opened: directory from buffer)
                       ! mem.not_found | no_access | io_failed

Open the directory at a path through directories evidence, reading it into the caller's buffer; close a successful directory through the same provider.

public open_file: (provider: type is file_system, host: provider,
                   path: text.os_string, options: open_options)
                  -> (opened: file)
                  ! already_exists | mem.not_found | no_access | is_a_directory
                  | io_failed

Open the file at path as options say, through file_system evidence; close it through the same provider's files.

open_read

function core/io/io.ldn:240 ↑ index
public open_read: (provider: type is files, host: provider,
                   path: text.os_string)
                  -> (opened: file)
                  ! mem.not_found | no_access | is_a_directory | io_failed

Open a path for reading through files evidence. The caller must close a successful handle using the same provider. A path that names a directory fails is_a_directory.

open_write

function core/io/io.ldn:251 ↑ index
public open_write: (provider: type is files, host: provider,
                    path: text.os_string)
                   -> (opened: file)
                   ! mem.not_found | no_access | is_a_directory | io_failed

Open a path for writing through files evidence. Creation and truncation behavior belongs to the provider; close a successful handle through that provider. A path that names a directory fails is_a_directory.

public out: (provider: type is process, host: provider)
            -> (stream: file)

Return the provider's standard-output identifier.

public put: (provider: type is writer, inout out: buffered_writer(provider),
             bytes: []u8) -> none ! io_failed

Write bytes to the file through the buffer. Bytes that fit in its free room wait there. Otherwise the waiting bytes are flushed first; then bytes wait in the buffer if they are fewer than it holds, and go straight to the writer in one write if they are not. A failure is the writer's io_failed: a failed flush keeps its bytes, as flush does, and a failed direct write may follow partial output, as io.write may.

public read: (provider: type is reader, host: provider,
              opened: file, into: []mut u8)
             -> (count: usize) ! io_failed

Read up to the destination length through reader evidence. Returns the number of bytes placed in the buffer, or zero at end of input.

public read_at: (provider: type is file_system, host: provider,
                 opened: file, offset: u64, into: []mut u8)
                -> (count: usize) ! io_failed

Read at most lenof into bytes of an open file from offset on: the count, which may be short, and zero at or past its end.

read_child

function core/io/programs.ldn:167 ↑ index
public read_child: (provider: type is programs, host: provider,
                    inout started: child, into: []mut u8)
                   -> (taken: child_read) ! io_failed

Read the next bytes a child wrote, from whichever of its piped output and errors has something, waiting until one has. Once both have ended, it reads zero bytes and reports finished.

read_entry

function core/io/io.ldn:286 ↑ index
public read_entry: (provider: type is directories, host: provider,
                    inout opened: directory)
                   -> (item: entry from opened) ! io_failed

Read the directory's next entry, whose name is lent from the directory until the next read; an entry whose at_end is true is the end of the directory.

read_line

function core/io/lines.ldn:85 ↑ index
public read_line: (provider: type is reader,
                   inout lines: line_reader(provider))
                  -> (item: line from lines)
                  ! mem.out_of_memory | io_failed

Read the next line, lent from the reader until the next read. A line ends at \n, and a \r just before it is part of the ending, so text written with \r\n reads the same as text written with \n; a lone \r is an ordinary byte. The last line need not end: input that ends after it gives it, and the next read is at_end. End of input is a line that has at_end, as an empty read is the end of a file.

A line that does not fit the buffer with its ending reports mem.out_of_memory, and the rest of that line is skipped, so the read after it gives the line that follows. A reader's failure is its io_failed, and keeps every byte already read for the next call. A line reader over an empty buffer has room for no line, and reports mem.out_of_memory on every call without reading: a read into it returns zero, which it would take for the end of the input.

remove_directory

function core/io/io.ldn:323 ↑ index
public remove_directory: (provider: type is directories, host: provider,
                          path: text.os_string)
                         -> none ! mem.not_found | no_access | io_failed

Remove the empty directory a path names.

remove_file

function core/io/io.ldn:316 ↑ index
public remove_file: (provider: type is directories, host: provider,
                     path: text.os_string)
                    -> none ! mem.not_found | no_access | io_failed

Remove the file a path names.

public rename: (provider: type is file_system, host: provider,
                old: text.os_string, new: text.os_string)
               -> none ! mem.not_found | no_access | io_failed

Give the file at old the name new, replacing a file already named new in the same step: no program sees both names or neither. The way to replace a file whole is to write a make_temporary file beside it, synchronize it, close it and rename it onto the file.

same_file

function core/io/io.ldn:260 ↑ index
public same_file: (provider: type is files, host: provider,
                   left: text.os_string, right: text.os_string)
                  -> (same: bool) ! io_failed

Ask whether two paths name the same file. The provider applies its own identity rules; a failure is io_failed.

scripted_over

function core/io/memory.ldn:635 ↑ index
public scripted_over: (world: memory, programs: []mut scripted_program)
                      -> (host: scripted_programs from world, programs)

Scripted programs over programs, beside the memory world world, which the handle copies; keep the records alive while it is used.

set_read_only

function core/io/file_system.ldn:201 ↑ index
public set_read_only: (provider: type is file_system, host: provider,
                       path: text.os_string, read_only: bool)
                      -> none ! mem.not_found | no_access | io_failed

Make the file at path read-only, or writable by its owner again. Opening a read-only file for writing is refused, no_access, except for a process the system lets past every permission, as POSIX's root.

public spawn: (provider: type is programs, host: provider,
               path: text.os_string, arguments: []text.os_string,
               environment: []text.os_variable, streams: child_streams)
              -> (started: child) ! mem.not_found | no_access | io_failed

Start the program at path through programs evidence, with arguments after its name and exactly the variables of environment, its standard streams connected as streams says. Wait for every child started.

public status_of: (provider: type is file_system, host: provider,
                   path: text.os_string, links: link_handling)
                  -> (found: status) ! mem.not_found | no_access | io_failed

The status of the file at path. When the path ends in a symbolic link, following gives the status of the link's target and not_following that of the link itself.

status_of_file

function core/io/file_system.ldn:193 ↑ index
public status_of_file: (provider: type is file_system, host: provider,
                        opened: file) -> (found: status) ! io_failed

The status of an open file.

synchronize

function core/io/file_system.ldn:209 ↑ index
public synchronize: (provider: type is file_system, host: provider,
                     opened: file, writing: synchronizing)
                    -> none ! io_failed

Write what the provider holds of an open file to lasting storage before returning, its status too unless writing is data_only.

temporary_path

function core/io/file_system.ldn:270 ↑ index
public temporary_path: (directory: text.os_string, bytes: [8]u8,
                        into: []mut u8)
                       -> (path: text.os_string from into) ! io_failed

The path make_temporary tries, from eight bytes of names: the directory, a / unless it is empty or ends in one, .tmp- and the bytes as sixteen lower-case hexadecimal digits, put in into; a path that does not fit is io_failed. For a provider in another module.

public truncate: (provider: type is file_system, host: provider,
                  opened: file, size: u64) -> none ! io_failed

Give a file open for writing the size size, cutting it or extending it with zeros.

valid_options

function core/io/file_system.ldn:156 ↑ index
public valid_options: (options: open_options) -> (yes: bool)

Whether options is a combination an open can have: it reads or writes, it appends or truncates only when it writes, and it is exclusive only when it may create. A provider refuses any other combination with io_failed before it asks anything.

variable

function core/io/io.ldn:450 ↑ index
public variable: (provider: type is environment, host: provider,
                  index: usize)
                 -> (item: text.os_variable from host) ! mem.not_found

Lend the environment variable at index, from zero, its name and value OS strings. Reports mem.not_found for an index at or past the count; the provider's backing must outlive its use. Every index below variables_length gives one, so a loop over them lists the whole environment.

variable_named

function core/io/io.ldn:460 ↑ index
public variable_named: (provider: type is environment, host: provider,
                        name: text.os_string)
                       -> (value: text.os_string from host)
                       ! mem.not_found

Lend the value of the environment variable named name, the first one when the environment names it twice. Reports mem.not_found when no variable has that name. text.from_os_string is the value's checked conversion to utf8.

variables_length

function core/io/io.ldn:440 ↑ index
public variables_length: (provider: type is environment, host: provider)
                         -> (count: usize)

Return the number of variables in the provider's environment.

variables_over

function core/io/memory.ldn:545 ↑ index
public variables_over: (entries: [][]u8)
                       -> (host: memory_variables from entries)

An environment over entries, which the handle lends; keep them alive while it is used.

public wait: (provider: type is programs, host: provider,
              sink started: child) -> (ended: ending) ! io_failed

Wait for a child to end and report how it did. The child is consumed, with every pipe it still had, even when this reports io_failed.

window_size

function core/io/file_system.ldn:334 ↑ index
public window_size: (provider: type is terminals, host: provider,
                     stream: file) -> (size: window)
                    ! not_a_terminal | io_failed

The size of the terminal stream in rows and columns.

write

function core/io/io.ldn:340 ↑ index
public write: (provider: type is writer, host: provider,
               opened: file, bytes: []u8) -> none ! io_failed

Write the complete byte slice through writer evidence. io_failed may follow partial output; callers must not assume retrying the whole buffer is safe.

public write_at: (provider: type is file_system, host: provider,
                  opened: file, offset: u64, bytes: []u8)
                 -> none ! io_failed

Write all of bytes into an open file from offset on; a write past the end leaves zeros between. A failure may follow part of them.

write_child

function core/io/programs.ldn:152 ↑ index
public write_child: (provider: type is programs, host: provider,
                     inout started: child, bytes: []u8) -> none ! io_failed

Write all of bytes to a child's piped input. A child that is not reading makes its parent wait here once the pipe is full, so write a large input only to a child that reads it as it comes. Writing to an input that is not open is io_failed.

write_format

function core/io/io.ldn:381 ↑ index
public write_format: (provider: type is writer, host: provider,
                      opened: file, template: utf8,
                      values: []any text.formattable)
                     -> none ! io_failed | text.invalid_format

Write template to the file with each {} replaced by the next of values, as text.format writes them: io.write_format(host, io.out(host), "{} of {}\n", [any(addr done), any(addr total)]). The caller supplies no buffer and has nothing to flush: the text is formatted into 128 bytes on the stack, which are written whenever they fill and at the end. So a line that fits is one write, and a value of any length is written in pieces. The template is checked against its values first, so text.invalid_format writes nothing. A failed write is io_failed and may follow partial output, as write's may. A conformance of the program's own that asks for more room at once than the 128 bytes, or right-aligns a piece of a value longer than them that was already written, is io_failed too, since its text cannot be written. The values are text.formattable's types; floats are not formattable yet. For many writes to one file, a buffered_writer gathers them into fewer.

write_some

function core/io/io.ldn:397 ↑ index
public write_some: (provider: type is writer, host: provider,
                    opened: file, bytes: []u8) -> (count: usize) ! io_failed

Make one write attempt and return its progress. Nonempty success is positive; empty input returns zero; failure transfers no bytes.

public written: (host: memory) -> (bytes: []u8 from host)

Lend the bytes written to standard output so far: the initialized prefix of the memory provider's output buffer.

written_errors

function core/io/memory.ldn:524 ↑ index
public written_errors: (host: memory) -> (bytes: []u8 from host)

Lend the bytes written to standard error so far: the initialized prefix of the memory provider's error buffer.

Example program

The program numbers the lines of the file named on the command line, reading it through a line reader and printing through a buffered writer. When the file cannot be read it says why on standard error, in failure.words' phrase, and exits with 1.

This complete program is one of the compiler's runtime tests. View source.

compiler/tests/fixtures/runtime/library-common-tasks/main.ldn48 lines
--  Number the lines of the file named on the command line, as `cat -n`
--  does, and say in words why when it cannot be read.
import core/failure
import core/io
import core/text
import hosted/blocking

--- Write each line of the file at `path` to `out` after its number.
numbered: (inout out: io.buffered_writer(blocking.host), host: blocking.host,
           path: text.os_string) -> none ! ... =
    file := try io.open_read(host, path)
    defer io.close(host, file) else none
    mut buffer: [4096]u8 = zeroed
    mut lines := io.lines_over(host, file, buffer)
    mut number: u64 = 0
    loop do
        item := try io.read_line(lines)
        break when item.at_end
        inc number
        try io.format(out, "{}: {}\n", [any(addr number), any(addr item.bytes)])
    end loop
end numbered

public main: () -> (code: i32) =
    host := blocking.new()
    mut storage: [256]u8 = zeroed
    mut out := io.buffered_over(host, io.out(host), storage)
    defer io.flush(out) else none
    code = 1
    path := io.argument(host, 0) else begin
        io.write(host, io.err(host), "usage: number FILE\n") else none
        return
    end
    numbered(out, host, path) else (problem)
        io.write(host, io.err(host), text.os_string_bytes(path)) else none
        io.write(host, io.err(host), ": ") else none
        io.write(host, io.err(host), failure.words(problem)) else none
        io.write(host, io.err(host), "\n") else none
        return
    end
    io.flush(out) else (problem)
        io.write(host, io.err(host), "standard output: ") else none
        io.write(host, io.err(host), failure.words(problem)) else none
        io.write(host, io.err(host), "\n") else none
        return
    end
    code = 0
end main
input.txt holds
first line
second line

last, without a newline
in a terminalprints on standard output
$ refine compile main.ldn -o common-tasks && ./common-tasks input.txt
1: first line
2: second line
3: 
4: last, without a newline