2016-03-01 13:11:38 -08:00
|
|
|
const linux = @import("linux.zig");
|
2016-02-27 21:06:46 -08:00
|
|
|
const errno = @import("errno.zig");
|
|
|
|
const math = @import("math.zig");
|
2015-12-14 23:07:51 -08:00
|
|
|
|
2016-01-22 22:24:09 -08:00
|
|
|
pub const stdin_fileno = 0;
|
|
|
|
pub const stdout_fileno = 1;
|
|
|
|
pub const stderr_fileno = 2;
|
2016-01-02 20:56:33 -08:00
|
|
|
|
2016-01-20 17:18:50 -08:00
|
|
|
pub var stdin = InStream {
|
|
|
|
.fd = stdin_fileno,
|
|
|
|
};
|
|
|
|
|
|
|
|
pub var stdout = OutStream {
|
|
|
|
.fd = stdout_fileno,
|
2016-01-22 21:02:07 -08:00
|
|
|
.buffer = undefined,
|
2016-01-20 17:18:50 -08:00
|
|
|
.index = 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
pub var stderr = OutStream {
|
|
|
|
.fd = stderr_fileno,
|
2016-01-22 21:02:07 -08:00
|
|
|
.buffer = undefined,
|
2016-01-20 17:18:50 -08:00
|
|
|
.index = 0,
|
|
|
|
};
|
|
|
|
|
2016-01-24 21:53:00 -08:00
|
|
|
/// The function received invalid input at runtime. An Invalid error means a
|
|
|
|
/// bug in the program that called the function.
|
|
|
|
pub error Invalid;
|
|
|
|
|
|
|
|
/// When an Unexpected error occurs, code that emitted the error likely needs
|
|
|
|
/// a patch to recognize the unexpected case so that it can handle it and emit
|
|
|
|
/// a more specific error.
|
2016-01-24 00:34:48 -08:00
|
|
|
pub error Unexpected;
|
2016-01-24 21:53:00 -08:00
|
|
|
|
2016-01-24 00:34:48 -08:00
|
|
|
pub error DiskQuota;
|
|
|
|
pub error FileTooBig;
|
|
|
|
pub error SigInterrupt;
|
|
|
|
pub error Io;
|
|
|
|
pub error NoSpaceLeft;
|
|
|
|
pub error BadPerm;
|
|
|
|
pub error PipeFail;
|
2016-01-24 21:53:00 -08:00
|
|
|
pub error BadFd;
|
2016-04-08 13:13:33 -07:00
|
|
|
pub error IsDir;
|
|
|
|
pub error NotDir;
|
|
|
|
pub error SymLinkLoop;
|
|
|
|
pub error ProcessFdQuotaExceeded;
|
|
|
|
pub error SystemFdQuotaExceeded;
|
|
|
|
pub error NameTooLong;
|
|
|
|
pub error NoDevice;
|
|
|
|
pub error PathNotFound;
|
|
|
|
pub error NoMem;
|
2016-01-20 17:18:50 -08:00
|
|
|
|
2016-01-22 22:24:09 -08:00
|
|
|
const buffer_size = 4 * 1024;
|
|
|
|
const max_u64_base10_digits = 20;
|
2016-01-27 22:28:43 -08:00
|
|
|
const max_f64_digits = 65;
|
2016-01-20 17:18:50 -08:00
|
|
|
|
2016-04-08 13:13:33 -07:00
|
|
|
pub const OpenRead = 0b0001;
|
|
|
|
pub const OpenWrite = 0b0010;
|
|
|
|
pub const OpenCreate = 0b0100;
|
|
|
|
pub const OpenTruncate = 0b1000;
|
|
|
|
|
2016-01-20 17:18:50 -08:00
|
|
|
pub struct OutStream {
|
2016-07-26 20:40:11 -07:00
|
|
|
fd: i32,
|
2016-01-20 17:18:50 -08:00
|
|
|
buffer: [buffer_size]u8,
|
2016-07-26 20:40:11 -07:00
|
|
|
index: usize,
|
2016-01-20 17:18:50 -08:00
|
|
|
|
2016-07-26 18:13:22 -07:00
|
|
|
pub fn write_byte(os: &OutStream, b: u8) -> %void {
|
|
|
|
if (os.buffer.len == os.index) %return os.flush();
|
|
|
|
os.buffer[os.index] = b;
|
|
|
|
os.index += 1;
|
|
|
|
}
|
|
|
|
|
2016-07-26 20:40:11 -07:00
|
|
|
pub fn write(os: &OutStream, bytes: []const u8) -> %usize {
|
2016-04-08 13:13:33 -07:00
|
|
|
var src_bytes_left = bytes.len;
|
|
|
|
var src_index: @typeof(bytes.len) = 0;
|
2016-01-24 21:53:00 -08:00
|
|
|
const dest_space_left = os.buffer.len - os.index;
|
2016-01-20 17:18:50 -08:00
|
|
|
|
|
|
|
while (src_bytes_left > 0) {
|
2016-07-26 20:40:11 -07:00
|
|
|
const copy_amt = math.min(usize, dest_space_left, src_bytes_left);
|
2016-04-08 13:13:33 -07:00
|
|
|
@memcpy(&os.buffer[os.index], &bytes[src_index], copy_amt);
|
2016-01-20 17:18:50 -08:00
|
|
|
os.index += copy_amt;
|
|
|
|
if (os.index == os.buffer.len) {
|
|
|
|
%return os.flush();
|
|
|
|
}
|
|
|
|
src_bytes_left -= copy_amt;
|
|
|
|
}
|
2016-04-08 13:13:33 -07:00
|
|
|
return bytes.len;
|
2016-01-20 17:18:50 -08:00
|
|
|
}
|
|
|
|
|
2016-01-24 21:53:00 -08:00
|
|
|
/// Prints a byte buffer, flushes the buffer, then returns the number of
|
|
|
|
/// bytes printed. The "f" is for "flush".
|
2016-07-26 20:40:11 -07:00
|
|
|
pub fn printf(os: &OutStream, str: []const u8) -> %usize {
|
2016-04-08 13:13:33 -07:00
|
|
|
const byte_count = %return os.write(str);
|
2016-01-24 21:53:00 -08:00
|
|
|
%return os.flush();
|
|
|
|
return byte_count;
|
|
|
|
}
|
|
|
|
|
2016-07-26 20:40:11 -07:00
|
|
|
pub fn print_u64(os: &OutStream, x: u64) -> %usize {
|
2016-01-20 17:18:50 -08:00
|
|
|
if (os.index + max_u64_base10_digits >= os.buffer.len) {
|
|
|
|
%return os.flush();
|
|
|
|
}
|
2016-01-24 21:53:00 -08:00
|
|
|
const amt_printed = buf_print_u64(os.buffer[os.index...], x);
|
2016-01-20 17:18:50 -08:00
|
|
|
os.index += amt_printed;
|
|
|
|
|
|
|
|
return amt_printed;
|
|
|
|
}
|
|
|
|
|
2016-07-26 20:40:11 -07:00
|
|
|
pub fn print_i64(os: &OutStream, x: i64) -> %usize {
|
2016-01-20 17:18:50 -08:00
|
|
|
if (os.index + max_u64_base10_digits >= os.buffer.len) {
|
|
|
|
%return os.flush();
|
|
|
|
}
|
2016-01-24 21:53:00 -08:00
|
|
|
const amt_printed = buf_print_i64(os.buffer[os.index...], x);
|
2016-01-20 17:18:50 -08:00
|
|
|
os.index += amt_printed;
|
|
|
|
|
|
|
|
return amt_printed;
|
|
|
|
}
|
|
|
|
|
2016-01-25 16:06:19 -08:00
|
|
|
pub fn flush(os: &OutStream) -> %void {
|
2016-05-11 14:44:10 -07:00
|
|
|
const write_ret = linux.write(os.fd, &os.buffer[0], os.index);
|
|
|
|
const write_err = linux.get_errno(write_ret);
|
|
|
|
if (write_err > 0) {
|
|
|
|
return switch (write_err) {
|
2016-02-27 21:06:46 -08:00
|
|
|
errno.EINVAL => unreachable{},
|
|
|
|
errno.EDQUOT => error.DiskQuota,
|
|
|
|
errno.EFBIG => error.FileTooBig,
|
|
|
|
errno.EINTR => error.SigInterrupt,
|
|
|
|
errno.EIO => error.Io,
|
|
|
|
errno.ENOSPC => error.NoSpaceLeft,
|
|
|
|
errno.EPERM => error.BadPerm,
|
|
|
|
errno.EPIPE => error.PipeFail,
|
|
|
|
else => error.Unexpected,
|
2016-01-24 21:53:00 -08:00
|
|
|
}
|
2016-01-20 17:18:50 -08:00
|
|
|
}
|
2016-05-11 14:44:10 -07:00
|
|
|
os.index = 0;
|
2016-01-20 17:18:50 -08:00
|
|
|
}
|
2016-02-28 06:22:09 -08:00
|
|
|
|
|
|
|
pub fn close(os: &OutStream) -> %void {
|
2016-07-26 20:40:11 -07:00
|
|
|
const close_ret = linux.close(os.fd);
|
|
|
|
const close_err = linux.get_errno(close_ret);
|
|
|
|
if (close_err > 0) {
|
|
|
|
return switch (close_err) {
|
|
|
|
errno.EIO => error.Io,
|
2016-03-01 13:11:38 -08:00
|
|
|
errno.EBADF => error.BadFd,
|
|
|
|
errno.EINTR => error.SigInterrupt,
|
2016-07-26 20:40:11 -07:00
|
|
|
else => error.Unexpected,
|
2016-02-28 06:22:09 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-20 17:18:50 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct InStream {
|
2016-07-26 20:40:11 -07:00
|
|
|
fd: i32,
|
2016-01-20 17:18:50 -08:00
|
|
|
|
2016-04-08 13:13:33 -07:00
|
|
|
pub fn open(path: []u8) -> %InStream {
|
|
|
|
const fd = linux.open(path, linux.O_LARGEFILE|linux.O_RDONLY, 0);
|
2016-07-26 20:40:11 -07:00
|
|
|
const fd_err = linux.get_errno(fd);
|
|
|
|
if (fd_err > 0) {
|
|
|
|
return switch (fd_err) {
|
2016-04-08 13:13:33 -07:00
|
|
|
errno.EFAULT => unreachable{},
|
|
|
|
errno.EINVAL => unreachable{},
|
|
|
|
errno.EACCES => error.BadPerm,
|
|
|
|
errno.EFBIG, errno.EOVERFLOW => error.FileTooBig,
|
|
|
|
errno.EINTR => error.SigInterrupt,
|
|
|
|
errno.EISDIR => error.IsDir,
|
|
|
|
errno.ELOOP => error.SymLinkLoop,
|
|
|
|
errno.EMFILE => error.ProcessFdQuotaExceeded,
|
|
|
|
errno.ENAMETOOLONG => error.NameTooLong,
|
|
|
|
errno.ENFILE => error.SystemFdQuotaExceeded,
|
|
|
|
errno.ENODEV => error.NoDevice,
|
|
|
|
errno.ENOENT => error.PathNotFound,
|
|
|
|
errno.ENOMEM => error.NoMem,
|
|
|
|
errno.ENOSPC => error.NoSpaceLeft,
|
|
|
|
errno.ENOTDIR => error.NotDir,
|
|
|
|
errno.EPERM => error.BadPerm,
|
|
|
|
else => error.Unexpected,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-26 20:40:11 -07:00
|
|
|
return InStream { .fd = i32(fd), };
|
2016-04-08 13:13:33 -07:00
|
|
|
}
|
|
|
|
|
2016-07-26 20:40:11 -07:00
|
|
|
pub fn read(is: &InStream, buf: []u8) -> %usize {
|
2016-03-01 13:11:38 -08:00
|
|
|
const amt_read = linux.read(is.fd, &buf[0], buf.len);
|
2016-07-26 20:40:11 -07:00
|
|
|
const read_err = linux.get_errno(amt_read);
|
|
|
|
if (read_err > 0) {
|
|
|
|
return switch (read_err) {
|
2016-02-27 21:06:46 -08:00
|
|
|
errno.EINVAL => unreachable{},
|
|
|
|
errno.EFAULT => unreachable{},
|
|
|
|
errno.EBADF => error.BadFd,
|
|
|
|
errno.EINTR => error.SigInterrupt,
|
|
|
|
errno.EIO => error.Io,
|
|
|
|
else => error.Unexpected,
|
2016-01-20 17:18:50 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return amt_read;
|
|
|
|
}
|
2016-02-28 06:22:09 -08:00
|
|
|
|
|
|
|
pub fn close(is: &InStream) -> %void {
|
2016-07-26 20:40:11 -07:00
|
|
|
const close_ret = linux.close(is.fd);
|
|
|
|
const close_err = linux.get_errno(close_ret);
|
|
|
|
if (close_err > 0) {
|
|
|
|
return switch (close_err) {
|
2016-03-01 13:11:38 -08:00
|
|
|
errno.EIO => error.Io,
|
|
|
|
errno.EBADF => error.BadFd,
|
|
|
|
errno.EINTR => error.SigInterrupt,
|
2016-02-28 06:22:09 -08:00
|
|
|
else => error.Unexpected,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-20 17:18:50 -08:00
|
|
|
}
|
|
|
|
|
2016-07-24 18:35:50 -07:00
|
|
|
pub fn parse_unsigned(inline T: type, buf: []u8, radix: u8) -> %T {
|
2016-05-13 11:11:55 -07:00
|
|
|
var x: T = 0;
|
2016-01-08 02:59:37 -08:00
|
|
|
|
2016-02-05 16:15:19 -08:00
|
|
|
for (buf) |c| {
|
2016-07-24 18:35:50 -07:00
|
|
|
const digit = %return char_to_digit(c, radix);
|
|
|
|
x = %return math.mul_overflow(T, x, radix);
|
|
|
|
x = %return math.add_overflow(T, x, digit);
|
2016-01-07 02:23:38 -08:00
|
|
|
}
|
2016-01-08 02:59:37 -08:00
|
|
|
|
2016-01-25 12:53:40 -08:00
|
|
|
return x;
|
2016-01-07 02:23:38 -08:00
|
|
|
}
|
|
|
|
|
2016-07-24 18:35:50 -07:00
|
|
|
pub error InvalidChar;
|
|
|
|
fn char_to_digit(c: u8, radix: u8) -> %u8 {
|
|
|
|
const value = if ('0' <= c && c <= '9') {
|
2016-01-08 02:59:37 -08:00
|
|
|
c - '0'
|
|
|
|
} else if ('A' <= c && c <= 'Z') {
|
|
|
|
c - 'A' + 10
|
|
|
|
} else if ('a' <= c && c <= 'z') {
|
|
|
|
c - 'a' + 10
|
|
|
|
} else {
|
2016-07-24 18:35:50 -07:00
|
|
|
return error.InvalidChar;
|
|
|
|
};
|
|
|
|
return if (value >= radix) error.InvalidChar else value;
|
2016-01-02 20:56:33 -08:00
|
|
|
}
|
2016-01-02 02:38:45 -08:00
|
|
|
|
2016-07-26 20:40:11 -07:00
|
|
|
pub fn buf_print_signed(inline T: type, out_buf: []u8, x: T) -> usize {
|
2016-05-14 20:45:08 -07:00
|
|
|
const uint = @int_type(false, T.bit_count, false);
|
2016-01-05 05:30:49 -08:00
|
|
|
if (x < 0) {
|
|
|
|
out_buf[0] = '-';
|
2016-07-24 18:35:50 -07:00
|
|
|
return 1 + buf_print_unsigned(uint, out_buf[1...], uint(-(x + 1)) + 1);
|
2016-01-05 05:30:49 -08:00
|
|
|
} else {
|
2016-07-24 18:35:50 -07:00
|
|
|
return buf_print_unsigned(uint, out_buf, uint(x));
|
2016-01-05 05:30:49 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-26 20:40:11 -07:00
|
|
|
pub fn buf_print_i64(out_buf: []u8, x: i64) -> usize {
|
2016-07-24 18:35:50 -07:00
|
|
|
buf_print_signed(i64, out_buf, x)
|
|
|
|
}
|
2016-05-14 20:45:08 -07:00
|
|
|
|
2016-07-26 20:40:11 -07:00
|
|
|
pub fn buf_print_unsigned(inline T: type, out_buf: []u8, x: T) -> usize {
|
2016-01-25 22:56:46 -08:00
|
|
|
var buf: [max_u64_base10_digits]u8 = undefined;
|
2016-01-02 02:38:45 -08:00
|
|
|
var a = x;
|
2016-07-26 20:40:11 -07:00
|
|
|
var index: usize = buf.len;
|
2016-01-02 02:38:45 -08:00
|
|
|
|
|
|
|
while (true) {
|
|
|
|
const digit = a % 10;
|
|
|
|
index -= 1;
|
2016-01-14 01:52:33 -08:00
|
|
|
buf[index] = '0' + u8(digit);
|
2016-01-02 02:38:45 -08:00
|
|
|
a /= 10;
|
|
|
|
if (a == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-01-05 21:47:47 -08:00
|
|
|
const len = buf.len - index;
|
2016-01-02 02:38:45 -08:00
|
|
|
|
2016-01-20 17:18:50 -08:00
|
|
|
@memcpy(&out_buf[0], &buf[index], len);
|
2016-01-02 02:38:45 -08:00
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
2016-01-24 21:53:00 -08:00
|
|
|
|
2016-07-26 20:40:11 -07:00
|
|
|
pub fn buf_print_u64(out_buf: []u8, x: u64) -> usize {
|
2016-07-24 18:35:50 -07:00
|
|
|
buf_print_unsigned(u64, out_buf, x)
|
|
|
|
}
|
2016-05-14 20:45:08 -07:00
|
|
|
|
2016-02-09 08:51:25 -08:00
|
|
|
#attribute("test")
|
|
|
|
fn parse_u64_digit_too_big() {
|
2016-07-24 18:35:50 -07:00
|
|
|
parse_unsigned(u64, "123a", 10) %% |err| {
|
2016-02-09 08:51:25 -08:00
|
|
|
if (err == error.InvalidChar) return;
|
|
|
|
unreachable{};
|
|
|
|
};
|
|
|
|
unreachable{};
|
|
|
|
}
|