commit e63f4c08c82217c2abe3cda05d0941902207680d
parent 7efef6512e73e42bc5233c6ceda3bdcd32b8fda9
Author: Sebastian <sebastian@sebsite.pw>
Date: Sun, 12 Jun 2022 19:29:41 -0400
rt: add STD*_FILENO constants
Signed-off-by: Sebastian <sebastian@sebsite.pw>
Diffstat:
8 files changed, 36 insertions(+), 26 deletions(-)
diff --git a/os/+freebsd/stdfd.ha b/os/+freebsd/stdfd.ha
@@ -3,6 +3,7 @@
// (c) 2021 Drew DeVault <sir@cmpwn.com>
use bufio;
use io;
+use rt;
export let stdin_bufio: bufio::bufstream = bufio::bufstream {
source = 0,
@@ -15,30 +16,30 @@ export let stdout_bufio: bufio::bufstream = bufio::bufstream {
};
// The standard input. This handle is buffered.
-export let stdin: io::handle = 0;
+export let stdin: io::handle = rt::STDIN_FILENO; // initialized by init_stdfd
// The standard input, as an [[io::file]]. This handle is unbuffered.
-export let stdin_file: io::file = 0;
+export let stdin_file: io::file = rt::STDIN_FILENO;
// The standard output. This handle is buffered.
-export let stdout: io::handle = 1;
+export let stdout: io::handle = rt::STDOUT_FILENO; // initialized by init_stdfd
// The standard output, as an [[io::file]]. This handle is unbuffered.
-export let stdout_file: io::file = 1;
+export let stdout_file: io::file = rt::STDOUT_FILENO;
// The standard error.
-export let stderr: io::file = 2;
+export let stderr: io::file = rt::STDERR_FILENO;
// The recommended buffer size for reading from disk.
export def BUFSIZ: size = 4096; // 4 KiB
@init fn init_stdfd() void = {
static let stdinbuf: [BUFSIZ]u8 = [0...];
- stdin_bufio = bufio::buffered(0, stdinbuf, []);
+ stdin_bufio = bufio::buffered(stdin_file, stdinbuf, []);
stdin = &stdin_bufio;
static let stdoutbuf: [BUFSIZ]u8 = [0...];
- stdout_bufio = bufio::buffered(1, [], stdoutbuf);
+ stdout_bufio = bufio::buffered(stdout_file, [], stdoutbuf);
stdout = &stdout_bufio;
};
diff --git a/os/+linux/stdfd.ha b/os/+linux/stdfd.ha
@@ -3,6 +3,7 @@
// (c) 2021 Drew DeVault <sir@cmpwn.com>
use bufio;
use io;
+use rt;
let stdin_bufio: bufio::bufstream = bufio::bufstream {
source = 0,
@@ -15,30 +16,30 @@ let stdout_bufio: bufio::bufstream = bufio::bufstream {
};
// The standard input. This handle is buffered.
-export let stdin: io::handle = 0;
+export let stdin: io::handle = rt::STDIN_FILENO; // initialized by init_stdfd
// The standard input, as an [[io::file]]. This handle is unbuffered.
-export let stdin_file: io::file = 0;
+export let stdin_file: io::file = rt::STDIN_FILENO;
// The standard output. This handle is buffered.
-export let stdout: io::handle = 1;
+export let stdout: io::handle = rt::STDOUT_FILENO; // initialized by init_stdfd
// The standard output, as an [[io::file]]. This handle is unbuffered.
-export let stdout_file: io::file = 1;
+export let stdout_file: io::file = rt::STDOUT_FILENO;
// The standard error.
-export let stderr: io::file = 2;
+export let stderr: io::file = rt::STDERR_FILENO;
// The recommended buffer size for reading from disk.
export def BUFSIZ: size = 4096; // 4 KiB
@init fn init_stdfd() void = {
static let stdinbuf: [BUFSIZ]u8 = [0...];
- stdin_bufio = bufio::buffered(0, stdinbuf, []);
+ stdin_bufio = bufio::buffered(stdin_file, stdinbuf, []);
stdin = &stdin_bufio;
static let stdoutbuf: [BUFSIZ]u8 = [0...];
- stdout_bufio = bufio::buffered(1, [], stdoutbuf);
+ stdout_bufio = bufio::buffered(stdout_file, [], stdoutbuf);
stdout = &stdout_bufio;
};
diff --git a/rt/+freebsd/abort.ha b/rt/+freebsd/abort.ha
@@ -4,9 +4,9 @@
fn platform_abort(msg: str) void = {
const prefix = "Abort: ";
const linefeed = "\n";
- write(2, *(&prefix: **void): *const char, len(prefix)): void;
- write(2, *(&msg: **void): *const char, len(msg)): void;
- write(2, *(&linefeed: **void): *const char, 1): void;
+ write(STDERR_FILENO, *(&prefix: **void): *const char, len(prefix)): void;
+ write(STDERR_FILENO, *(&msg: **void): *const char, len(msg)): void;
+ write(STDERR_FILENO, *(&linefeed: **void): *const char, 1): void;
kill(getpid(), SIGABRT): void;
for (true) void;
};
diff --git a/rt/+freebsd/types.ha b/rt/+freebsd/types.ha
@@ -419,3 +419,7 @@ export def WCONTINUED: int = 4;
export def WNOWAIT: int = 8;
export def WEXITED: int = 16;
export def WTRAPPED: int = 32;
+
+export def STDIN_FILENO: int = 0;
+export def STDOUT_FILENO: int = 1;
+export def STDERR_FILENO: int = 2;
diff --git a/rt/+linux/abort.ha b/rt/+linux/abort.ha
@@ -4,9 +4,9 @@
fn platform_abort(msg: str) void = {
const prefix = "Abort: ";
const linefeed = "\n";
- write(2, *(&prefix: **void): *const char, len(prefix)): void;
- write(2, *(&msg: **void): *const char, len(msg)): void;
- write(2, *(&linefeed: **void): *const char, 1): void;
+ write(STDERR_FILENO, *(&prefix: **void): *const char, len(prefix)): void;
+ write(STDERR_FILENO, *(&msg: **void): *const char, len(msg)): void;
+ write(STDERR_FILENO, *(&linefeed: **void): *const char, 1): void;
kill(getpid(), SIGABRT): void;
for (true) void;
};
diff --git a/rt/+linux/types.ha b/rt/+linux/types.ha
@@ -822,3 +822,7 @@ export type ptrace_syscall_info = struct {
},
},
};
+
+export def STDIN_FILENO: int = 0;
+export def STDOUT_FILENO: int = 1;
+export def STDERR_FILENO: int = 2;
diff --git a/rt/+test/run.ha b/rt/+test/run.ha
@@ -96,7 +96,7 @@ export fn tests_main() size = {
};
fn print(msg: str) void = {
- write(1, *(&msg: **void): *const char, len(msg))!;
+ write(STDOUT_FILENO, *(&msg: **void): *const char, len(msg))!;
};
fn dots(n: size) void = {
diff --git a/rt/abort.ha b/rt/abort.ha
@@ -19,11 +19,11 @@ export @noreturn fn abort_fixed(loc: str, i: int) void = {
const prefix = "Abort: ";
const sep = ": ";
const linefeed = "\n";
- write(2, *(&prefix: **void): *const char, len(prefix)): void;
- write(2, *(&loc: **void): *const char, len(loc)): void;
- write(2, *(&sep: **void): *const char, len(sep)): void;
- write(2, *(&reasons[i]: **void): *const char, len(reasons[i])): void;
- write(2, *(&linefeed: **void): *const char, 1): void;
+ write(STDERR_FILENO, *(&prefix: **void): *const char, len(prefix)): void;
+ write(STDERR_FILENO, *(&loc: **void): *const char, len(loc)): void;
+ write(STDERR_FILENO, *(&sep: **void): *const char, len(sep)): void;
+ write(STDERR_FILENO, *(&reasons[i]: **void): *const char, len(reasons[i])): void;
+ write(STDERR_FILENO, *(&linefeed: **void): *const char, 1): void;
kill(getpid(), SIGABRT): void;
for (true) void;
};