ui_controls

source of std::io

1// Console and file input/output.2//3// This module is the standard library's FFI barrier (LANGUAGE_SPEC.md4// section 6.3): programs call these safe wrappers and never touch the5// system underneath. The externs are the compiler's al_sys_* functions,6// one C file a platform (alloyc/src/support/system.c); streams cross the7// boundary as opaque i64 handles (a C stream natively; the interpreter8// implements the same names over the host filesystem, with handles 1 and9// 2 as the standard streams).1011import std::option;12import std::dispose;1314extern al_sys_open(path: c_string, mode: Access) -> i64;15extern al_sys_close(file: i64) -> i32;16extern al_sys_read(file: i64, buffer: &var [u8], length: u64) -> u64;17extern al_sys_write(file: i64, bytes: &[u8], length: u64) -> u64;18extern al_sys_write_line(file: i64, bytes: &[u8], length: u64);19extern al_sys_size(file: i64) -> i64;20extern al_sys_sync(file: i64) -> i32;2122// The C stream for standard input (0), output (1) or error (2). A lang23// item (LANGUAGE_SPEC.md section 6.1a): the compiler supplies it for each24// target, so this body never runs.25exp fn standard_stream(index: u32) -> i64 {26    return 0;27}2829// Writes the text to standard output.30exp fn print(text: &[u8]) {31    al_sys_write(standard_stream(1), &text, text.length());32}3334// Writes the text and a newline to standard output, as one line no other35// task's output splits.36exp fn print_line(text: &[u8]) {37    al_sys_write_line(standard_stream(1), &text, text.length());38}3940// Writes the text to standard error.41exp fn print_error(text: &[u8]) {42    al_sys_write(standard_stream(2), &text, text.length());43}4445// Writes the text and a newline to standard error, as one line no other46// task's output splits.47exp fn print_error_line(text: &[u8]) {48    al_sys_write_line(standard_stream(2), &text, text.length());49}5051// An open file, closed by disposing it (section 5.2). Reads and writes go52// through the C library's buffering, so small ones are cheap.53exp type File : Disposable = struct {54    stream: i64,55};5657// Opens an existing file for reading, or `None` when it cannot be.58exp fn File::open(path: &[u8]) -> Option<File> {59    return open_as(&path, Access::Read);60}6162// Creates a file for writing, emptying it if it exists, or `None` when it63// cannot be.64exp fn File::create(path: &[u8]) -> Option<File> {65    return open_as(&path, Access::Write);66}6768// Opens a file for writing at its end, creating it if missing, or `None`69// when it cannot be.70exp fn File::extend(path: &[u8]) -> Option<File> {71    return open_as(&path, Access::Append);72}7374// How al_sys_open opens a file.75type Access = enum : i32 {76    Read = 0,77    Write = 1,78    Append = 279};8081fn open_as(path: &[u8], access: Access) -> Option<File> {82    const terminated = move null_terminated(&path);83    const stream = al_sys_open(&terminated, access);84    if (stream == 0) {85        return ::None;86    }87    return ::Some(File { .stream = stream });88}8990// Closes the file.91exp fn dispose(self file: &var File) {92    al_sys_close(file.stream);93    file.stream = 0;94}9596// Reads into the buffer and returns how many bytes it filled: fewer than97// its length only at the end of the file or on an error.98exp fn read(self file: &var File, buffer: &var [u8]) -> u64 {99    return al_sys_read(file.stream, &var buffer, buffer.length());100}101102// Writes all the bytes; false when they could not all be written.103exp fn write(self file: &var File, bytes: &[u8]) -> bool {104    return al_sys_write(file.stream, &bytes, bytes.length()) == bytes.length();105}106107// Writes what was written through to the disk, so it survives the108// machine stopping; false when it cannot be.109exp fn sync(self file: &var File) -> bool {110    return al_sys_sync(file.stream) == 0;111}112113// The file's size in bytes, leaving the position at the start.114exp fn size(self file: &var File) -> u64 {115    const size = al_sys_size(file.stream);116    return if (size <= 0) 0 else size to u64;117}118119// Reads from standard input into the buffer and returns how many bytes120// it filled: fewer than its length only at the end of the input.121exp fn read_input(buffer: &var [u8]) -> u64 {122    return al_sys_read(standard_stream(0), &var buffer, buffer.length());123}124125// Reads a whole file into an owned byte array, or `None` when the file126// cannot be opened.127exp fn read_file(path: &[u8]) -> Option<*[u8]> {128    var file = if (File::open(&path) is ::Some |move f|) move f else {129        return ::None;130    };131    defer file.dispose();132    var contents: *var [u8] = new [0 : file.size()];133    if (file.read(&var contents) != contents.length()) {134        return ::None;135    }136    // past its size, as /proc's files of size 0 hold more137    var probe: [u8 : 1] = [0];138    if (file.read(&var probe[..]) == 1) {139        contents = move rest_of(&var file, &contents, probe[0]);140    }141    return ::Some(move contents);142}143144// The bytes read so far, the next one, and the rest of the file.145fn rest_of(file: &var File, start: &[u8], next: u8) -> *var [u8] {146    var contents: *var [u8] = new [0 : start.length() * 2 + 4096];147    contents[..start.length()] = start;148    contents[start.length()] = next;149    var length = start.length() + 1;150    while (true) {151        length += file.read(&var contents[length..]);152        if (length < contents.length()) {153            break;154        }155        var grown: *var [u8] = new [0 : contents.length() * 2];156        grown[..length] = contents[..length];157        contents = move grown;158    }159    return new contents[..length];160}161162// Writes the bytes to the file, replacing any previous contents. False163// when the file cannot be created or written.164exp fn write_file(path: &[u8], contents: &[u8]) -> bool {165    var file = if (File::create(&path) is ::Some |move f|) move f else {166        return false;167    };168    defer file.dispose();169    return file.write(&contents);170}171172// C strings are NUL-terminated: paths handed to the system get an owned copy173// with the terminator appended (string literals already carry one, but a174// runtime-built path need not).175fn null_terminated(path: &[u8]) -> *var [u8] {176    var terminated: *var [u8] = new [0 : path.length() + 1];177    terminated[..path.length()] = path;178    return move terminated;179}