Skip to content

Commit ad7122d

Browse files
linusgalimpfard
authored andcommitted
std.os -> std.posix
See: ziglang/zig#19354
1 parent e18781d commit ad7122d

File tree

1 file changed

+50
-51
lines changed

1 file changed

+50
-51
lines changed

src/main.zig

Lines changed: 50 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ const should_enable_signal_handling = !is_windows and builtin.os.tag != .wasi;
1515

1616
const logger = std.log.scoped(.zigline);
1717

18-
// std.os is a LIE!
1918
const SystemCapabilities = switch (builtin.os.tag) {
2019
// FIXME: Windows' console handling is a mess, and std doesn't have
2120
// the necessary bindings to emulate termios on Windows.
@@ -70,50 +69,50 @@ const SystemCapabilities = switch (builtin.os.tag) {
7069
};
7170
}
7271

73-
pub const POLL_IN = std.os.system.POLL.RDNORM;
72+
pub const POLL_IN = std.posix.POLL.RDNORM;
7473

75-
pub fn setPollFd(p: *std.os.system.pollfd, f: *anyopaque) void {
74+
pub fn setPollFd(p: *std.posix.pollfd, f: *anyopaque) void {
7675
p.fd = @ptrCast(f);
7776
}
7877

79-
pub fn poll(fds: [*]std.os.system.pollfd, n: std.os.system.nfds_t, timeout: i32) c_int {
80-
// std.os.system.poll() doesn't actually exist on windows lul
78+
pub fn poll(fds: [*]std.posix.pollfd, n: std.posix.nfds_t, timeout: i32) c_int {
79+
// std.posix.poll() has a Windows implementation but doesn't accept the second arg, only a slice.
8180
_ = timeout;
8281
fds[n - 1].revents = Self.POLL_IN;
8382
return 1;
8483
}
8584

8685
const pipe = (if (is_windows) struct {
87-
pub fn pipe() ![2]std.os.fd_t {
86+
pub fn pipe() ![2]std.posix.fd_t {
8887
var rd: std.os.windows.HANDLE = undefined;
8988
var wr: std.os.windows.HANDLE = undefined;
9089
var attrs: std.os.windows.SECURITY_ATTRIBUTES = undefined;
9190
attrs.nLength = 0;
9291
try std.os.windows.CreatePipe(&rd, &wr, &attrs);
93-
return [2]std.os.fd_t{ @ptrCast(rd), @ptrCast(wr) };
92+
return [2]std.posix.fd_t{ @ptrCast(rd), @ptrCast(wr) };
9493
}
9594
} else struct {
96-
pub fn pipe() ![2]std.os.fd_t {
97-
return std.os.pipe();
95+
pub fn pipe() ![2]std.posix.fd_t {
96+
return std.posix.pipe();
9897
}
9998
}).pipe;
10099
},
101100
else => struct {
102101
const Self = @This();
103-
pub const Sigaction = std.os.Sigaction;
102+
pub const Sigaction = std.posix.Sigaction;
104103

105-
pub const termios = std.os.termios;
104+
pub const termios = std.posix.termios;
106105

107-
pub const V = std.os.V;
106+
pub const V = std.posix.V;
108107

109108
pub const default_operation_mode = Configuration.OperationMode.Full;
110109

111110
pub fn getTermios() !Self.termios {
112-
return try std.os.tcgetattr(std.os.STDIN_FILENO);
111+
return try std.posix.tcgetattr(std.posix.STDIN_FILENO);
113112
}
114113

115114
pub fn setTermios(t: Self.termios) !void {
116-
try std.os.tcsetattr(std.os.STDIN_FILENO, std.os.system.TCSA.NOW, t);
115+
try std.posix.tcsetattr(std.posix.STDIN_FILENO, std.posix.TCSA.NOW, t);
117116
}
118117

119118
pub fn clearEchoAndICanon(t: *Self.termios) void {
@@ -126,18 +125,18 @@ const SystemCapabilities = switch (builtin.os.tag) {
126125
return t.cc[@intFromEnum(cc)];
127126
}
128127

129-
pub const POLL_IN = std.os.system.POLL.IN;
128+
pub const POLL_IN = std.posix.POLL.IN;
130129

131-
pub fn setPollFd(p: *std.os.system.pollfd, f: std.os.fd_t) void {
130+
pub fn setPollFd(p: *std.posix.pollfd, f: std.posix.fd_t) void {
132131
p.fd = f;
133132
}
134133

135134
const PollReturnType = if (builtin.link_libc) c_int else usize; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
136-
pub fn poll(fds: [*]std.os.system.pollfd, n: std.os.system.nfds_t, timeout: i32) PollReturnType {
137-
return std.os.system.poll(fds, n, timeout);
135+
pub fn poll(fds: [*]std.posix.pollfd, n: std.posix.nfds_t, timeout: i32) PollReturnType {
136+
return std.posix.system.poll(fds, n, timeout);
138137
}
139138

140-
pub const pipe = std.os.pipe;
139+
pub const pipe = std.posix.pipe;
141140
},
142141
};
143142

@@ -619,8 +618,8 @@ fn vtMoveRelative(row: i64, col: i64) !void {
619618

620619
var signalHandlingData: ?struct {
621620
pipe: struct {
622-
write: std.os.fd_t,
623-
read: std.os.fd_t,
621+
write: std.posix.fd_t,
622+
read: std.posix.fd_t,
624623
},
625624
old_sigint: ?SystemCapabilities.Sigaction = null,
626625
old_sigwinch: ?SystemCapabilities.Sigaction = null,
@@ -634,14 +633,14 @@ var signalHandlingData: ?struct {
634633
pub const Editor = struct {
635634
pub const Error =
636635
std.mem.Allocator.Error ||
637-
std.os.MMapError ||
638-
std.os.OpenError ||
639-
std.os.PipeError ||
640-
std.os.ReadError ||
641-
std.os.RealPathError ||
642-
std.os.TermiosGetError ||
643-
std.os.TermiosSetError ||
644-
std.os.WriteError ||
636+
std.posix.MMapError ||
637+
std.posix.OpenError ||
638+
std.posix.PipeError ||
639+
std.posix.ReadError ||
640+
std.posix.RealPathError ||
641+
std.posix.TermiosGetError ||
642+
std.posix.TermiosSetError ||
643+
std.posix.WriteError ||
645644
error{ CodepointTooLarge, Utf8CannotEncodeSurrogateHalf } ||
646645
error{ Empty, Eof, ReadFailure } ||
647646
error{ EndOfStream, StreamTooLong, OperationNotSupported } ||
@@ -827,8 +826,8 @@ pub const Editor = struct {
827826
control_thread: ?Thread = null,
828827
control_thread_exited: bool = false,
829828
thread_kill_pipe: ?struct {
830-
write: std.os.fd_t,
831-
read: std.os.fd_t,
829+
write: std.posix.fd_t,
830+
read: std.posix.fd_t,
832831
} = null,
833832

834833
queue_cond_mutex: Mutex = .{},
@@ -1022,7 +1021,7 @@ pub const Editor = struct {
10221021

10231022
// In the absence of way to interrupt threads, we're just gonna write to it and hope it dies on its own pace.
10241023
if (self.thread_kill_pipe) |pipes| {
1025-
_ = std.os.write(pipes.write, "x") catch 0;
1024+
_ = std.posix.write(pipes.write, "x") catch 0;
10261025
}
10271026
}
10281027

@@ -1056,14 +1055,14 @@ pub const Editor = struct {
10561055

10571056
signalHandlingData.?.old_sigint = @as(SystemCapabilities.Sigaction, undefined);
10581057
signalHandlingData.?.old_sigwinch = @as(SystemCapabilities.Sigaction, undefined);
1059-
try std.os.sigaction(
1060-
std.os.SIG.INT,
1061-
&SystemCapabilities.Sigaction{ .handler = .{ .handler = @TypeOf(signalHandlingData.?).handleSignal }, .mask = std.os.empty_sigset, .flags = 0 },
1058+
try std.posix.sigaction(
1059+
std.posix.SIG.INT,
1060+
&SystemCapabilities.Sigaction{ .handler = .{ .handler = @TypeOf(signalHandlingData.?).handleSignal }, .mask = std.posix.empty_sigset, .flags = 0 },
10621061
&signalHandlingData.?.old_sigint.?,
10631062
);
1064-
try std.os.sigaction(
1065-
std.os.SIG.WINCH,
1066-
&SystemCapabilities.Sigaction{ .handler = .{ .handler = @TypeOf(signalHandlingData.?).handleSignal }, .mask = std.os.empty_sigset, .flags = 0 },
1063+
try std.posix.sigaction(
1064+
std.posix.SIG.WINCH,
1065+
&SystemCapabilities.Sigaction{ .handler = .{ .handler = @TypeOf(signalHandlingData.?).handleSignal }, .mask = std.posix.empty_sigset, .flags = 0 },
10671066
&signalHandlingData.?.old_sigwinch.?,
10681067
);
10691068
}
@@ -1175,14 +1174,14 @@ pub const Editor = struct {
11751174

11761175
std.debug.assert(self.thread_kill_pipe != null);
11771176

1178-
var pollfds = [_]std.os.system.pollfd{ undefined, undefined, undefined };
1177+
var pollfds = [_]std.posix.pollfd{ undefined, undefined, undefined };
11791178
SystemCapabilities.setPollFd(&pollfds[0], stdin.handle);
11801179
SystemCapabilities.setPollFd(&pollfds[1], self.thread_kill_pipe.?.read);
11811180
pollfds[0].events = SystemCapabilities.POLL_IN;
11821181
pollfds[1].events = SystemCapabilities.POLL_IN;
11831182
pollfds[2].events = 0;
11841183

1185-
var nfds: std.os.nfds_t = 2;
1184+
var nfds: std.posix.nfds_t = 2;
11861185

11871186
if (self.configuration.enable_signal_handling) {
11881187
SystemCapabilities.setPollFd(&pollfds[2], signalHandlingData.?.pipe.read);
@@ -1199,7 +1198,7 @@ pub const Editor = struct {
11991198
defer self.logic_cond_mutex.unlock();
12001199
const rc = SystemCapabilities.poll(&pollfds, nfds, std.math.maxInt(i32));
12011200
if (rc < 0) {
1202-
self.input_error = switch (std.os.errno(rc)) {
1201+
self.input_error = switch (std.posix.errno(rc)) {
12031202
.INTR => {
12041203
continue;
12051204
},
@@ -1219,7 +1218,7 @@ pub const Editor = struct {
12191218
if (pollfds[1].revents & SystemCapabilities.POLL_IN != 0) {
12201219
// We're supposed to die...after draining the pipe.
12211220
var buf = [_]u8{0} ** 8;
1222-
_ = std.os.read(self.thread_kill_pipe.?.read, &buf) catch 0;
1221+
_ = std.posix.read(self.thread_kill_pipe.?.read, &buf) catch 0;
12231222
break;
12241223
}
12251224

@@ -1231,7 +1230,7 @@ pub const Editor = struct {
12311230
break :no_read;
12321231
};
12331232
switch (signo) {
1234-
std.os.SIG.WINCH => {
1233+
std.posix.SIG.WINCH => {
12351234
self.signal_queue.enqueue(.SIGWINCH) catch {
12361235
break :no_read;
12371236
};
@@ -2046,9 +2045,9 @@ pub const Editor = struct {
20462045
var buf = [16]u8{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
20472046
var more_junk_to_read = false;
20482047
var stdin = std.io.getStdIn();
2049-
var pollfds = [1]std.os.system.pollfd{undefined};
2048+
var pollfds = [1]std.posix.pollfd{undefined};
20502049
{
2051-
var pollfd: std.os.system.pollfd = undefined;
2050+
var pollfd: std.posix.pollfd = undefined;
20522051
SystemCapabilities.setPollFd(&pollfd, stdin.handle);
20532052
pollfd.events = SystemCapabilities.POLL_IN;
20542053
pollfd.revents = 0;
@@ -2654,13 +2653,13 @@ pub const Editor = struct {
26542653
self.num_columns = 80;
26552654
self.num_lines = 24;
26562655
if (!is_windows) {
2657-
const system = if (builtin.link_libc and builtin.os.tag == .linux) std.os.linux else std.os.system;
2658-
var ws: system.winsize = undefined;
2659-
if (std.os.system.ioctl(std.io.getStdIn().handle, system.T.IOCGWINSZ, @intFromPtr(&ws)) != 0) {
2660-
const fd = std.os.system.open("/dev/tty", .{ .ACCMODE = .RDONLY }, @as(std.os.mode_t, 0));
2656+
const ioctl = if (builtin.os.tag == .linux) std.os.linux.ioctl else std.c.ioctl;
2657+
var ws: std.posix.winsize = undefined;
2658+
if (ioctl(std.io.getStdIn().handle, std.posix.T.IOCGWINSZ, @intFromPtr(&ws)) != 0) {
2659+
const fd = std.posix.open("/dev/tty", .{ .ACCMODE = .RDONLY }, @as(std.posix.mode_t, 0)) catch return;
26612660
if (fd != -1) {
2662-
_ = std.os.system.ioctl(@intCast(fd), system.T.IOCGWINSZ, @intFromPtr(&ws));
2663-
_ = std.os.system.close(@intCast(fd));
2661+
_ = ioctl(@intCast(fd), std.posix.T.IOCGWINSZ, @intFromPtr(&ws));
2662+
_ = std.posix.close(@intCast(fd));
26642663
} else {
26652664
return;
26662665
}

0 commit comments

Comments
 (0)