| 1 |
// SPDX-License-Identifier: MPL-2.0 |
| 2 |
// (c) Hare authors <https://harelang.org> |
| 3 |
|
| 4 |
use bufio; |
| 5 |
use io; |
| 6 |
use rt; |
| 7 |
|
| 8 |
let stdin_bufio: bufio::stream = bufio::stream { |
| 9 |
// Will be overwritten, but must be initialized |
| 10 |
vtable = null: io::stream, |
| 11 |
source = 0, |
| 12 |
... |
| 13 |
}; |
| 14 |
|
| 15 |
let stdout_bufio: bufio::stream = bufio::stream { |
| 16 |
// Will be overwritten, but must be initialized |
| 17 |
vtable = null: io::stream, |
| 18 |
source = 1, |
| 19 |
... |
| 20 |
}; |
| 21 |
|
| 22 |
// The standard input. This handle is buffered. |
| 23 |
export let stdin: io::handle = rt::STDIN_FILENO; // initialized by init_stdfd |
| 24 |
|
| 25 |
// The standard input, as an [[io::file]]. This handle is unbuffered. |
| 26 |
export let stdin_file: io::file = rt::STDIN_FILENO; |
| 27 |
|
| 28 |
// The standard output. This handle is buffered. |
| 29 |
export let stdout: io::handle = rt::STDOUT_FILENO; // initialized by init_stdfd |
| 30 |
|
| 31 |
// The standard output, as an [[io::file]]. This handle is unbuffered. |
| 32 |
export let stdout_file: io::file = rt::STDOUT_FILENO; |
| 33 |
|
| 34 |
// The standard error. This handle is unbuffered. |
| 35 |
export let stderr: io::handle = rt::STDERR_FILENO; |
| 36 |
|
| 37 |
// The standard error, as an [[io::file]]. This handle is unbuffered. |
| 38 |
export let stderr_file: io::file = rt::STDERR_FILENO; |
| 39 |
|
| 40 |
// The recommended buffer size for reading from disk. |
| 41 |
export def BUFSZ: size = 4096; // 4 KiB |
| 42 |
|
| 43 |
@init fn init_stdfd() void = { |
| 44 |
static let stdinbuf: [BUFSZ]u8 = [0...]; |
| 45 |
stdin_bufio = bufio::init(stdin_file, stdinbuf, [], |
| 46 |
bufio::flag::MANAGED_HANDLE); |
| 47 |
stdin = &stdin_bufio; |
| 48 |
|
| 49 |
static let stdoutbuf: [BUFSZ]u8 = [0...]; |
| 50 |
stdout_bufio = bufio::init(stdout_file, [], stdoutbuf, |
| 51 |
bufio::flag::MANAGED_HANDLE); |
| 52 |
stdout = &stdout_bufio; |
| 53 |
}; |
| 54 |
|
| 55 |
@fini fn fini_stdfd() void = { |
| 56 |
// Flush any pending writes |
| 57 |
_ = io::close(stdout); |
| 58 |
}; |