Skip to content

Commit b00dab7

Browse files
committed
std.os -> std.posix
See: ziglang/zig#19354
1 parent fbb7b43 commit b00dab7

File tree

1 file changed

+64
-67
lines changed

1 file changed

+64
-67
lines changed

src/main.zig

Lines changed: 64 additions & 67 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,39 +69,39 @@ 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
.macos => 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

107106
pub const V = struct {
108107
const EOF = 0;
@@ -124,11 +123,11 @@ const SystemCapabilities = switch (builtin.os.tag) {
124123
pub const default_operation_mode = Configuration.OperationMode.Full;
125124

126125
pub fn getTermios() !Self.termios {
127-
return try std.os.tcgetattr(std.os.STDIN_FILENO);
126+
return try std.posix.tcgetattr(std.posix.STDIN_FILENO);
128127
}
129128

130129
pub fn setTermios(t: Self.termios) !void {
131-
try std.os.tcsetattr(std.os.STDIN_FILENO, std.os.system.TCSA.NOW, t);
130+
try std.posix.tcsetattr(std.posix.STDIN_FILENO, std.posix.TCSA.NOW, t);
132131
}
133132

134133
pub fn clearEchoAndICanon(t: *Self.termios) void {
@@ -139,61 +138,59 @@ const SystemCapabilities = switch (builtin.os.tag) {
139138
return t.cc[cc];
140139
}
141140

142-
pub const POLL_IN = std.os.system.POLL.IN;
141+
pub const POLL_IN = std.posix.POLL.IN;
143142

144-
pub fn setPollFd(p: *std.os.system.pollfd, f: std.os.fd_t) void {
143+
pub fn setPollFd(p: *std.posix.pollfd, f: std.posix.fd_t) void {
145144
p.fd = f;
146145
}
147146

148147
const PollReturnType = if (builtin.link_libc) c_int else usize; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
149-
pub fn poll(fds: [*]std.os.system.pollfd, n: std.os.system.nfds_t, timeout: i32) PollReturnType {
150-
return std.os.system.poll(fds, n, timeout);
148+
pub fn poll(fds: [*]std.posix.pollfd, n: std.posix.nfds_t, timeout: i32) PollReturnType {
149+
return std.posix.system.poll(fds, n, timeout);
151150
}
152151

153-
pub const pipe = std.os.pipe;
152+
pub const pipe = std.posix.pipe;
154153
},
155154
else => struct {
156155
const Self = @This();
157-
pub const Sigaction = std.os.Sigaction;
156+
pub const Sigaction = std.posix.Sigaction;
158157

159-
pub const termios = std.os.termios;
158+
pub const termios = std.posix.termios;
160159

161-
// FIXME: Non-linux systems...?
162-
pub const V = std.os.linux.V;
163-
const ECHO = std.os.linux.ECHO;
164-
const ICANON = std.os.linux.ICANON;
165-
const ISIG = std.os.linux.ISIG;
160+
pub const V = std.posix.V;
166161

167162
pub const default_operation_mode = Configuration.OperationMode.Full;
168163

169164
pub fn getTermios() !Self.termios {
170-
return try std.os.tcgetattr(std.os.STDIN_FILENO);
165+
return try std.posix.tcgetattr(std.posix.STDIN_FILENO);
171166
}
172167

173168
pub fn setTermios(t: Self.termios) !void {
174-
try std.os.tcsetattr(std.os.STDIN_FILENO, std.os.system.TCSA.NOW, t);
169+
try std.posix.tcsetattr(std.posix.STDIN_FILENO, std.posix.TCSA.NOW, t);
175170
}
176171

177172
pub fn clearEchoAndICanon(t: *Self.termios) void {
178-
t.lflag &= ~ECHO & ~ICANON & ~ISIG;
173+
t.lflag.ECHO = false;
174+
t.lflag.ICANON = false;
175+
t.lflag.ISIG = false;
179176
}
180177

181-
pub fn getTermiosCC(t: Self.termios, cc: u32) u8 {
182-
return t.cc[cc];
178+
pub fn getTermiosCC(t: Self.termios, cc: Self.V) u8 {
179+
return t.cc[@intFromEnum(cc)];
183180
}
184181

185-
pub const POLL_IN = std.os.system.POLL.IN;
182+
pub const POLL_IN = std.posix.POLL.IN;
186183

187-
pub fn setPollFd(p: *std.os.system.pollfd, f: std.os.fd_t) void {
184+
pub fn setPollFd(p: *std.posix.pollfd, f: std.posix.fd_t) void {
188185
p.fd = f;
189186
}
190187

191188
const PollReturnType = if (builtin.link_libc) c_int else usize; // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
192-
pub fn poll(fds: [*]std.os.system.pollfd, n: std.os.system.nfds_t, timeout: i32) PollReturnType {
193-
return std.os.system.poll(fds, n, timeout);
189+
pub fn poll(fds: [*]std.posix.pollfd, n: std.posix.nfds_t, timeout: i32) PollReturnType {
190+
return std.posix.system.poll(fds, n, timeout);
194191
}
195192

196-
pub const pipe = std.os.pipe;
193+
pub const pipe = std.posix.pipe;
197194
},
198195
};
199196

@@ -675,8 +672,8 @@ fn vtMoveRelative(row: i64, col: i64) !void {
675672

676673
var signalHandlingData: ?struct {
677674
pipe: struct {
678-
write: std.os.fd_t,
679-
read: std.os.fd_t,
675+
write: std.posix.fd_t,
676+
read: std.posix.fd_t,
680677
},
681678
old_sigint: ?SystemCapabilities.Sigaction = null,
682679
old_sigwinch: ?SystemCapabilities.Sigaction = null,
@@ -690,14 +687,14 @@ var signalHandlingData: ?struct {
690687
pub const Editor = struct {
691688
pub const Error =
692689
std.mem.Allocator.Error ||
693-
std.os.MMapError ||
694-
std.os.OpenError ||
695-
std.os.PipeError ||
696-
std.os.ReadError ||
697-
std.os.RealPathError ||
698-
std.os.TermiosGetError ||
699-
std.os.TermiosSetError ||
700-
std.os.WriteError ||
690+
std.posix.MMapError ||
691+
std.posix.OpenError ||
692+
std.posix.PipeError ||
693+
std.posix.ReadError ||
694+
std.posix.RealPathError ||
695+
std.posix.TermiosGetError ||
696+
std.posix.TermiosSetError ||
697+
std.posix.WriteError ||
701698
error{ CodepointTooLarge, Utf8CannotEncodeSurrogateHalf } ||
702699
error{ Empty, Eof, ReadFailure } ||
703700
error{ EndOfStream, StreamTooLong, OperationNotSupported } ||
@@ -883,8 +880,8 @@ pub const Editor = struct {
883880
control_thread: ?Thread = null,
884881
control_thread_exited: bool = false,
885882
thread_kill_pipe: ?struct {
886-
write: std.os.fd_t,
887-
read: std.os.fd_t,
883+
write: std.posix.fd_t,
884+
read: std.posix.fd_t,
888885
} = null,
889886

890887
queue_cond_mutex: Mutex = .{},
@@ -1078,7 +1075,7 @@ pub const Editor = struct {
10781075

10791076
// In the absence of way to interrupt threads, we're just gonna write to it and hope it dies on its own pace.
10801077
if (self.thread_kill_pipe) |pipes| {
1081-
_ = std.os.write(pipes.write, "x") catch 0;
1078+
_ = std.posix.write(pipes.write, "x") catch 0;
10821079
}
10831080
}
10841081

@@ -1112,14 +1109,14 @@ pub const Editor = struct {
11121109

11131110
signalHandlingData.?.old_sigint = @as(SystemCapabilities.Sigaction, undefined);
11141111
signalHandlingData.?.old_sigwinch = @as(SystemCapabilities.Sigaction, undefined);
1115-
try std.os.sigaction(
1116-
std.os.SIG.INT,
1117-
&SystemCapabilities.Sigaction{ .handler = .{ .handler = @TypeOf(signalHandlingData.?).handleSignal }, .mask = std.os.empty_sigset, .flags = 0 },
1112+
try std.posix.sigaction(
1113+
std.posix.SIG.INT,
1114+
&SystemCapabilities.Sigaction{ .handler = .{ .handler = @TypeOf(signalHandlingData.?).handleSignal }, .mask = std.posix.empty_sigset, .flags = 0 },
11181115
&signalHandlingData.?.old_sigint.?,
11191116
);
1120-
try std.os.sigaction(
1121-
std.os.SIG.WINCH,
1122-
&SystemCapabilities.Sigaction{ .handler = .{ .handler = @TypeOf(signalHandlingData.?).handleSignal }, .mask = std.os.empty_sigset, .flags = 0 },
1117+
try std.posix.sigaction(
1118+
std.posix.SIG.WINCH,
1119+
&SystemCapabilities.Sigaction{ .handler = .{ .handler = @TypeOf(signalHandlingData.?).handleSignal }, .mask = std.posix.empty_sigset, .flags = 0 },
11231120
&signalHandlingData.?.old_sigwinch.?,
11241121
);
11251122
}
@@ -1231,14 +1228,14 @@ pub const Editor = struct {
12311228

12321229
std.debug.assert(self.thread_kill_pipe != null);
12331230

1234-
var pollfds = [_]std.os.system.pollfd{ undefined, undefined, undefined };
1231+
var pollfds = [_]std.posix.pollfd{ undefined, undefined, undefined };
12351232
SystemCapabilities.setPollFd(&pollfds[0], stdin.handle);
12361233
SystemCapabilities.setPollFd(&pollfds[1], self.thread_kill_pipe.?.read);
12371234
pollfds[0].events = SystemCapabilities.POLL_IN;
12381235
pollfds[1].events = SystemCapabilities.POLL_IN;
12391236
pollfds[2].events = 0;
12401237

1241-
var nfds: std.os.nfds_t = 2;
1238+
var nfds: std.posix.nfds_t = 2;
12421239

12431240
if (self.configuration.enable_signal_handling) {
12441241
SystemCapabilities.setPollFd(&pollfds[2], signalHandlingData.?.pipe.read);
@@ -1255,7 +1252,7 @@ pub const Editor = struct {
12551252
defer self.logic_cond_mutex.unlock();
12561253
const rc = SystemCapabilities.poll(&pollfds, nfds, std.math.maxInt(i32));
12571254
if (rc < 0) {
1258-
self.input_error = switch (std.os.errno(rc)) {
1255+
self.input_error = switch (std.posix.errno(rc)) {
12591256
.INTR => {
12601257
continue;
12611258
},
@@ -1275,7 +1272,7 @@ pub const Editor = struct {
12751272
if (pollfds[1].revents & SystemCapabilities.POLL_IN != 0) {
12761273
// We're supposed to die...after draining the pipe.
12771274
var buf = [_]u8{0} ** 8;
1278-
_ = std.os.read(self.thread_kill_pipe.?.read, &buf) catch 0;
1275+
_ = std.posix.read(self.thread_kill_pipe.?.read, &buf) catch 0;
12791276
break;
12801277
}
12811278

@@ -1287,7 +1284,7 @@ pub const Editor = struct {
12871284
break :no_read;
12881285
};
12891286
switch (signo) {
1290-
std.os.SIG.WINCH => {
1287+
std.posix.SIG.WINCH => {
12911288
self.signal_queue.enqueue(.SIGWINCH) catch {
12921289
break :no_read;
12931290
};
@@ -2102,9 +2099,9 @@ pub const Editor = struct {
21022099
var buf = [16]u8{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
21032100
var more_junk_to_read = false;
21042101
var stdin = std.io.getStdIn();
2105-
var pollfds = [1]std.os.system.pollfd{undefined};
2102+
var pollfds = [1]std.posix.pollfd{undefined};
21062103
{
2107-
var pollfd: std.os.system.pollfd = undefined;
2104+
var pollfd: std.posix.pollfd = undefined;
21082105
SystemCapabilities.setPollFd(&pollfd, stdin.handle);
21092106
pollfd.events = SystemCapabilities.POLL_IN;
21102107
pollfd.revents = 0;
@@ -2710,13 +2707,13 @@ pub const Editor = struct {
27102707
self.num_columns = 80;
27112708
self.num_lines = 24;
27122709
if (!is_windows) {
2713-
const system = if (builtin.link_libc and builtin.os.tag == .linux) std.os.linux else std.os.system;
2714-
var ws: system.winsize = undefined;
2715-
if (std.os.system.ioctl(std.io.getStdIn().handle, system.T.IOCGWINSZ, @intFromPtr(&ws)) != 0) {
2716-
const fd = std.os.system.open("/dev/tty", .{ .ACCMODE = .RDONLY }, @as(std.os.mode_t, 0));
2710+
const ioctl = if (builtin.os.tag == .linux) std.os.linux.ioctl else std.c.ioctl;
2711+
var ws: std.posix.winsize = undefined;
2712+
if (ioctl(std.io.getStdIn().handle, std.posix.T.IOCGWINSZ, @intFromPtr(&ws)) != 0) {
2713+
const fd = std.posix.open("/dev/tty", .{ .ACCMODE = .RDONLY }, @as(std.posix.mode_t, 0)) catch return;
27172714
if (fd != -1) {
2718-
_ = std.os.system.ioctl(@intCast(fd), system.T.IOCGWINSZ, @intFromPtr(&ws));
2719-
_ = std.os.system.close(@intCast(fd));
2715+
_ = ioctl(@intCast(fd), std.posix.T.IOCGWINSZ, @intFromPtr(&ws));
2716+
_ = std.posix.close(@intCast(fd));
27202717
} else {
27212718
return;
27222719
}

0 commit comments

Comments
 (0)