command: stylistic changes

This commit is contained in:
Mitchell Hashimoto
2023-11-05 18:03:22 -08:00
parent c9b7439354
commit 1a846597b1

View File

@ -112,7 +112,52 @@ pub fn start(self: *Command, alloc: Allocator) !void {
defer arena_allocator.deinit();
const arena = arena_allocator.allocator();
if (builtin.os.tag == .windows) {
switch (builtin.os.tag) {
.windows => try self.startWindows(arena),
else => try self.startPosix(arena),
}
}
fn startPosix(self: *Command, arena: Allocator) !void {
// Null-terminate all our arguments
const pathZ = try arena.dupeZ(u8, self.path);
const argsZ = try arena.allocSentinel(?[*:0]u8, self.args.len, null);
for (self.args, 0..) |arg, i| argsZ[i] = (try arena.dupeZ(u8, arg)).ptr;
// Determine our env vars
const envp = if (self.env) |env_map|
(try createNullDelimitedEnvMap(arena, env_map)).ptr
else if (builtin.link_libc)
std.c.environ
else
@compileError("missing env vars");
// Fork
const pid = try std.os.fork();
if (pid != 0) {
// Parent, return immediately.
self.pid = @intCast(pid);
return;
}
// We are the child.
// Setup our file descriptors for std streams.
if (self.stdin) |f| try setupFd(f.handle, os.STDIN_FILENO);
if (self.stdout) |f| try setupFd(f.handle, os.STDOUT_FILENO);
if (self.stderr) |f| try setupFd(f.handle, os.STDERR_FILENO);
// Setup our working directory
if (self.cwd) |cwd| try os.chdir(cwd);
// If the user requested a pre exec callback, call it now.
if (self.pre_exec) |f| f(self);
// Finally, replace our process.
_ = std.os.execveZ(pathZ, argsZ, envp) catch null;
}
fn startWindows(self: *Command, arena: Allocator) !void {
const application_w = try std.unicode.utf8ToUtf16LeWithNull(arena, self.path);
const cwd_w = if (self.cwd) |cwd| try std.unicode.utf8ToUtf16LeWithNull(arena, cwd) else null;
const command_line_w = if (self.args.len > 0) b: {
@ -133,8 +178,9 @@ pub fn start(self: *Command, alloc: Allocator) !void {
) else null;
defer if (null_fd) |fd| std.os.close(fd);
// TODO: In the case of having FDs instead of pty, need to set up attributes such that the
// child process only inherits these handles, then set bInheritsHandles below.
// TODO: In the case of having FDs instead of pty, need to set up
// attributes such that the child process only inherits these handles,
// then set bInheritsHandles below.
const attribute_list, const stdin, const stdout, const stderr = if (self.pseudo_console) |pseudo_console| b: {
var attribute_list_size: usize = undefined;
@ -213,44 +259,6 @@ pub fn start(self: *Command, alloc: Allocator) !void {
) == 0) return windows.unexpectedError(windows.kernel32.GetLastError());
self.pid = process_information.hProcess;
} else {
// Null-terminate all our arguments
const pathZ = try arena.dupeZ(u8, self.path);
const argsZ = try arena.allocSentinel(?[*:0]u8, self.args.len, null);
for (self.args, 0..) |arg, i| argsZ[i] = (try arena.dupeZ(u8, arg)).ptr;
// Determine our env vars
const envp = if (self.env) |env_map|
(try createNullDelimitedEnvMap(arena, env_map)).ptr
else if (builtin.link_libc)
std.c.environ
else
@compileError("missing env vars");
// Fork
const pid = try std.os.fork();
if (pid != 0) {
// Parent, return immediately.
self.pid = @intCast(pid);
return;
}
// We are the child.
// Setup our file descriptors for std streams.
if (self.stdin) |f| try setupFd(f.handle, os.STDIN_FILENO);
if (self.stdout) |f| try setupFd(f.handle, os.STDOUT_FILENO);
if (self.stderr) |f| try setupFd(f.handle, os.STDERR_FILENO);
// Setup our working directory
if (self.cwd) |cwd| try os.chdir(cwd);
// If the user requested a pre exec callback, call it now.
if (self.pre_exec) |f| f(self);
// Finally, replace our process.
_ = std.os.execveZ(pathZ, argsZ, envp) catch null;
}
}
fn setupFd(src: File.Handle, target: i32) !void {