aboutsummaryrefslogtreecommitdiff
path: root/std/os/child_process.zig
diff options
context:
space:
mode:
authorAndrea Orru <andrea@orru.io>2018-08-06 01:43:19 -0400
committerAndrea Orru <andrea@orru.io>2018-08-06 01:43:19 -0400
commitd2f5e57b68da0b16e5789ca19045ccbcb4ecfa8d (patch)
treee9fa3caec533a0d1e2b434868b2fde1f9240e5c8 /std/os/child_process.zig
parent06614b3fa09954464c2e2f32756cacedc178a282 (diff)
parent63a23e848a62d5f167f8d5478de9766cb24aa6eb (diff)
downloadzig-d2f5e57b68da0b16e5789ca19045ccbcb4ecfa8d.tar.gz
zig-d2f5e57b68da0b16e5789ca19045ccbcb4ecfa8d.zip
Merge branch 'master' into zen_stdlib
Diffstat (limited to 'std/os/child_process.zig')
-rw-r--r--std/os/child_process.zig270
1 files changed, 137 insertions, 133 deletions
diff --git a/std/os/child_process.zig b/std/os/child_process.zig
index 8bb8b2d7e7..693129eea8 100644
--- a/std/os/child_process.zig
+++ b/std/os/child_process.zig
@@ -20,7 +20,7 @@ pub const ChildProcess = struct {
pub handle: if (is_windows) windows.HANDLE else void,
pub thread_handle: if (is_windows) windows.HANDLE else void,
- pub allocator: &mem.Allocator,
+ pub allocator: *mem.Allocator,
pub stdin: ?os.File,
pub stdout: ?os.File,
@@ -31,7 +31,7 @@ pub const ChildProcess = struct {
pub argv: []const []const u8,
/// Leave as null to use the current env map using the supplied allocator.
- pub env_map: ?&const BufMap,
+ pub env_map: ?*const BufMap,
pub stdin_behavior: StdIo,
pub stdout_behavior: StdIo,
@@ -47,9 +47,9 @@ pub const ChildProcess = struct {
pub cwd: ?[]const u8,
err_pipe: if (is_windows) void else [2]i32,
- llnode: if (is_windows) void else LinkedList(&ChildProcess).Node,
+ llnode: if (is_windows) void else LinkedList(*ChildProcess).Node,
- pub const SpawnError = error {
+ pub const SpawnError = error{
ProcessFdQuotaExceeded,
Unexpected,
NotDir,
@@ -84,11 +84,8 @@ pub const ChildProcess = struct {
/// First argument in argv is the executable.
/// On success must call deinit.
- pub fn init(argv: []const []const u8, allocator: &mem.Allocator) !&ChildProcess {
- const child = try allocator.create(ChildProcess);
- errdefer allocator.destroy(child);
-
- *child = ChildProcess {
+ pub fn init(argv: []const []const u8, allocator: *mem.Allocator) !*ChildProcess {
+ const child = try allocator.create(ChildProcess{
.allocator = allocator,
.argv = argv,
.pid = undefined,
@@ -99,27 +96,29 @@ pub const ChildProcess = struct {
.term = null,
.env_map = null,
.cwd = null,
- .uid = if (is_windows) {} else null,
- .gid = if (is_windows) {} else null,
+ .uid = if (is_windows) {} else
+ null,
+ .gid = if (is_windows) {} else
+ null,
.stdin = null,
.stdout = null,
.stderr = null,
.stdin_behavior = StdIo.Inherit,
.stdout_behavior = StdIo.Inherit,
.stderr_behavior = StdIo.Inherit,
- };
-
+ });
+ errdefer allocator.destroy(child);
return child;
}
- pub fn setUserName(self: &ChildProcess, name: []const u8) !void {
+ pub fn setUserName(self: *ChildProcess, name: []const u8) !void {
const user_info = try os.getUserInfo(name);
self.uid = user_info.uid;
self.gid = user_info.gid;
}
/// On success must call `kill` or `wait`.
- pub fn spawn(self: &ChildProcess) !void {
+ pub fn spawn(self: *ChildProcess) !void {
if (is_windows) {
return self.spawnWindows();
} else {
@@ -127,13 +126,13 @@ pub const ChildProcess = struct {
}
}
- pub fn spawnAndWait(self: &ChildProcess) !Term {
+ pub fn spawnAndWait(self: *ChildProcess) !Term {
try self.spawn();
return self.wait();
}
/// Forcibly terminates child process and then cleans up all resources.
- pub fn kill(self: &ChildProcess) !Term {
+ pub fn kill(self: *ChildProcess) !Term {
if (is_windows) {
return self.killWindows(1);
} else {
@@ -141,7 +140,7 @@ pub const ChildProcess = struct {
}
}
- pub fn killWindows(self: &ChildProcess, exit_code: windows.UINT) !Term {
+ pub fn killWindows(self: *ChildProcess, exit_code: windows.UINT) !Term {
if (self.term) |term| {
self.cleanupStreams();
return term;
@@ -154,10 +153,10 @@ pub const ChildProcess = struct {
};
}
try self.waitUnwrappedWindows();
- return ??self.term;
+ return self.term.?;
}
- pub fn killPosix(self: &ChildProcess) !Term {
+ pub fn killPosix(self: *ChildProcess) !Term {
if (self.term) |term| {
self.cleanupStreams();
return term;
@@ -173,11 +172,11 @@ pub const ChildProcess = struct {
};
}
self.waitUnwrapped();
- return ??self.term;
+ return self.term.?;
}
/// Blocks until child process terminates and then cleans up all resources.
- pub fn wait(self: &ChildProcess) !Term {
+ pub fn wait(self: *ChildProcess) !Term {
if (is_windows) {
return self.waitWindows();
} else {
@@ -193,9 +192,7 @@ pub const ChildProcess = struct {
/// Spawns a child process, waits for it, collecting stdout and stderr, and then returns.
/// If it succeeds, the caller owns result.stdout and result.stderr memory.
- pub fn exec(allocator: &mem.Allocator, argv: []const []const u8, cwd: ?[]const u8,
- env_map: ?&const BufMap, max_output_size: usize) !ExecResult
- {
+ pub fn exec(allocator: *mem.Allocator, argv: []const []const u8, cwd: ?[]const u8, env_map: ?*const BufMap, max_output_size: usize) !ExecResult {
const child = try ChildProcess.init(argv, allocator);
defer child.deinit();
@@ -212,52 +209,52 @@ pub const ChildProcess = struct {
defer Buffer.deinit(&stdout);
defer Buffer.deinit(&stderr);
- var stdout_file_in_stream = io.FileInStream.init(&??child.stdout);
- var stderr_file_in_stream = io.FileInStream.init(&??child.stderr);
+ var stdout_file_in_stream = io.FileInStream.init(&child.stdout.?);
+ var stderr_file_in_stream = io.FileInStream.init(&child.stderr.?);
try stdout_file_in_stream.stream.readAllBuffer(&stdout, max_output_size);
try stderr_file_in_stream.stream.readAllBuffer(&stderr, max_output_size);
- return ExecResult {
+ return ExecResult{
.term = try child.wait(),
.stdout = stdout.toOwnedSlice(),
.stderr = stderr.toOwnedSlice(),
};
}
- fn waitWindows(self: &ChildProcess) !Term {
+ fn waitWindows(self: *ChildProcess) !Term {
if (self.term) |term| {
self.cleanupStreams();
return term;
}
try self.waitUnwrappedWindows();
- return ??self.term;
+ return self.term.?;
}
- fn waitPosix(self: &ChildProcess) !Term {
+ fn waitPosix(self: *ChildProcess) !Term {
if (self.term) |term| {
self.cleanupStreams();
return term;
}
self.waitUnwrapped();
- return ??self.term;
+ return self.term.?;
}
- pub fn deinit(self: &ChildProcess) void {
+ pub fn deinit(self: *ChildProcess) void {
self.allocator.destroy(self);
}
- fn waitUnwrappedWindows(self: &ChildProcess) !void {
+ fn waitUnwrappedWindows(self: *ChildProcess) !void {
const result = os.windowsWaitSingle(self.handle, windows.INFINITE);
self.term = (SpawnError!Term)(x: {
var exit_code: windows.DWORD = undefined;
if (windows.GetExitCodeProcess(self.handle, &exit_code) == 0) {
- break :x Term { .Unknown = 0 };
+ break :x Term{ .Unknown = 0 };
} else {
- break :x Term { .Exited = @bitCast(i32, exit_code)};
+ break :x Term{ .Exited = @bitCast(i32, exit_code) };
}
});
@@ -267,7 +264,7 @@ pub const ChildProcess = struct {
return result;
}
- fn waitUnwrapped(self: &ChildProcess) void {
+ fn waitUnwrapped(self: *ChildProcess) void {
var status: i32 = undefined;
while (true) {
const err = posix.getErrno(posix.waitpid(self.pid, &status, 0));
@@ -283,17 +280,26 @@ pub const ChildProcess = struct {
}
}
- fn handleWaitResult(self: &ChildProcess, status: i32) void {
+ fn handleWaitResult(self: *ChildProcess, status: i32) void {
self.term = self.cleanupAfterWait(status);
}
- fn cleanupStreams(self: &ChildProcess) void {
- if (self.stdin) |*stdin| { stdin.close(); self.stdin = null; }
- if (self.stdout) |*stdout| { stdout.close(); self.stdout = null; }
- if (self.stderr) |*stderr| { stderr.close(); self.stderr = null; }
+ fn cleanupStreams(self: *ChildProcess) void {
+ if (self.stdin) |*stdin| {
+ stdin.close();
+ self.stdin = null;
+ }
+ if (self.stdout) |*stdout| {
+ stdout.close();
+ self.stdout = null;
+ }
+ if (self.stderr) |*stderr| {
+ stderr.close();
+ self.stderr = null;
+ }
}
- fn cleanupAfterWait(self: &ChildProcess, status: i32) !Term {
+ fn cleanupAfterWait(self: *ChildProcess, status: i32) !Term {
defer {
os.close(self.err_pipe[0]);
os.close(self.err_pipe[1]);
@@ -309,7 +315,7 @@ pub const ChildProcess = struct {
// Here we potentially return the fork child's error
// from the parent pid.
if (err_int != @maxValue(ErrInt)) {
- return SpawnError(err_int);
+ return @errSetCast(SpawnError, @intToError(err_int));
}
return statusToTerm(status);
@@ -317,25 +323,30 @@ pub const ChildProcess = struct {
fn statusToTerm(status: i32) Term {
return if (posix.WIFEXITED(status))
- Term { .Exited = posix.WEXITSTATUS(status) }
+ Term{ .Exited = posix.WEXITSTATUS(status) }
else if (posix.WIFSIGNALED(status))
- Term { .Signal = posix.WTERMSIG(status) }
+ Term{ .Signal = posix.WTERMSIG(status) }
else if (posix.WIFSTOPPED(status))
- Term { .Stopped = posix.WSTOPSIG(status) }
+ Term{ .Stopped = posix.WSTOPSIG(status) }
else
- Term { .Unknown = status }
- ;
+ Term{ .Unknown = status };
}
- fn spawnPosix(self: &ChildProcess) !void {
+ fn spawnPosix(self: *ChildProcess) !void {
const stdin_pipe = if (self.stdin_behavior == StdIo.Pipe) try makePipe() else undefined;
- errdefer if (self.stdin_behavior == StdIo.Pipe) { destroyPipe(stdin_pipe); };
+ errdefer if (self.stdin_behavior == StdIo.Pipe) {
+ destroyPipe(stdin_pipe);
+ };
const stdout_pipe = if (self.stdout_behavior == StdIo.Pipe) try makePipe() else undefined;
- errdefer if (self.stdout_behavior == StdIo.Pipe) { destroyPipe(stdout_pipe); };
+ errdefer if (self.stdout_behavior == StdIo.Pipe) {
+ destroyPipe(stdout_pipe);
+ };
const stderr_pipe = if (self.stderr_behavior == StdIo.Pipe) try makePipe() else undefined;
- errdefer if (self.stderr_behavior == StdIo.Pipe) { destroyPipe(stderr_pipe); };
+ errdefer if (self.stderr_behavior == StdIo.Pipe) {
+ destroyPipe(stderr_pipe);
+ };
const any_ignore = (self.stdin_behavior == StdIo.Ignore or self.stdout_behavior == StdIo.Ignore or self.stderr_behavior == StdIo.Ignore);
const dev_null_fd = if (any_ignore) blk: {
@@ -346,7 +357,9 @@ pub const ChildProcess = struct {
} else blk: {
break :blk undefined;
};
- defer { if (any_ignore) os.close(dev_null_fd); }
+ defer {
+ if (any_ignore) os.close(dev_null_fd);
+ }
var env_map_owned: BufMap = undefined;
var we_own_env_map: bool = undefined;
@@ -358,7 +371,9 @@ pub const ChildProcess = struct {
env_map_owned = try os.getEnvMap(self.allocator);
break :x &env_map_owned;
};
- defer { if (we_own_env_map) env_map_owned.deinit(); }
+ defer {
+ if (we_own_env_map) env_map_owned.deinit();
+ }
// This pipe is used to communicate errors between the time of fork
// and execve from the child process to the parent process.
@@ -375,17 +390,12 @@ pub const ChildProcess = struct {
}
if (pid_result == 0) {
// we are the child
-
- setUpChildIo(self.stdin_behavior, stdin_pipe[0], posix.STDIN_FILENO, dev_null_fd) catch
- |err| forkChildErrReport(err_pipe[1], err);
- setUpChildIo(self.stdout_behavior, stdout_pipe[1], posix.STDOUT_FILENO, dev_null_fd) catch
- |err| forkChildErrReport(err_pipe[1], err);
- setUpChildIo(self.stderr_behavior, stderr_pipe[1], posix.STDERR_FILENO, dev_null_fd) catch
- |err| forkChildErrReport(err_pipe[1], err);
+ setUpChildIo(self.stdin_behavior, stdin_pipe[0], posix.STDIN_FILENO, dev_null_fd) catch |err| forkChildErrReport(err_pipe[1], err);
+ setUpChildIo(self.stdout_behavior, stdout_pipe[1], posix.STDOUT_FILENO, dev_null_fd) catch |err| forkChildErrReport(err_pipe[1], err);
+ setUpChildIo(self.stderr_behavior, stderr_pipe[1], posix.STDERR_FILENO, dev_null_fd) catch |err| forkChildErrReport(err_pipe[1], err);
if (self.cwd) |cwd| {
- os.changeCurDir(self.allocator, cwd) catch
- |err| forkChildErrReport(err_pipe[1], err);
+ os.changeCurDir(self.allocator, cwd) catch |err| forkChildErrReport(err_pipe[1], err);
}
if (self.gid) |gid| {
@@ -396,12 +406,11 @@ pub const ChildProcess = struct {
os.posix_setreuid(uid, uid) catch |err| forkChildErrReport(err_pipe[1], err);
}
- os.posixExecve(self.argv, env_map, self.allocator) catch
- |err| forkChildErrReport(err_pipe[1], err);
+ os.posixExecve(self.argv, env_map, self.allocator) catch |err| forkChildErrReport(err_pipe[1], err);
}
// we are the parent
- const pid = i32(pid_result);
+ const pid = @intCast(i32, pid_result);
if (self.stdin_behavior == StdIo.Pipe) {
self.stdin = os.File.openHandle(stdin_pipe[1]);
} else {
@@ -420,40 +429,44 @@ pub const ChildProcess = struct {
self.pid = pid;
self.err_pipe = err_pipe;
- self.llnode = LinkedList(&ChildProcess).Node.init(self);
+ self.llnode = LinkedList(*ChildProcess).Node.init(self);
self.term = null;
- if (self.stdin_behavior == StdIo.Pipe) { os.close(stdin_pipe[0]); }
- if (self.stdout_behavior == StdIo.Pipe) { os.close(stdout_pipe[1]); }
- if (self.stderr_behavior == StdIo.Pipe) { os.close(stderr_pipe[1]); }
+ if (self.stdin_behavior == StdIo.Pipe) {
+ os.close(stdin_pipe[0]);
+ }
+ if (self.stdout_behavior == StdIo.Pipe) {
+ os.close(stdout_pipe[1]);
+ }
+ if (self.stderr_behavior == StdIo.Pipe) {
+ os.close(stderr_pipe[1]);
+ }
}
- fn spawnWindows(self: &ChildProcess) !void {
- const saAttr = windows.SECURITY_ATTRIBUTES {
+ fn spawnWindows(self: *ChildProcess) !void {
+ const saAttr = windows.SECURITY_ATTRIBUTES{
.nLength = @sizeOf(windows.SECURITY_ATTRIBUTES),
.bInheritHandle = windows.TRUE,
.lpSecurityDescriptor = null,
};
- const any_ignore = (self.stdin_behavior == StdIo.Ignore or
- self.stdout_behavior == StdIo.Ignore or
- self.stderr_behavior == StdIo.Ignore);
+ const any_ignore = (self.stdin_behavior == StdIo.Ignore or self.stdout_behavior == StdIo.Ignore or self.stderr_behavior == StdIo.Ignore);
const nul_handle = if (any_ignore) blk: {
const nul_file_path = "NUL";
var fixed_buffer_mem: [nul_file_path.len + 1]u8 = undefined;
var fixed_allocator = std.heap.FixedBufferAllocator.init(fixed_buffer_mem[0..]);
- break :blk try os.windowsOpen(&fixed_allocator.allocator, "NUL", windows.GENERIC_READ, windows.FILE_SHARE_READ,
- windows.OPEN_EXISTING, windows.FILE_ATTRIBUTE_NORMAL);
+ break :blk try os.windowsOpen(&fixed_allocator.allocator, "NUL", windows.GENERIC_READ, windows.FILE_SHARE_READ, windows.OPEN_EXISTING, windows.FILE_ATTRIBUTE_NORMAL);
} else blk: {
break :blk undefined;
};
- defer { if (any_ignore) os.close(nul_handle); }
+ defer {
+ if (any_ignore) os.close(nul_handle);
+ }
if (any_ignore) {
try windowsSetHandleInfo(nul_handle, windows.HANDLE_FLAG_INHERIT, 0);
}
-
var g_hChildStd_IN_Rd: ?windows.HANDLE = null;
var g_hChildStd_IN_Wr: ?windows.HANDLE = null;
switch (self.stdin_behavior) {
@@ -470,7 +483,9 @@ pub const ChildProcess = struct {
g_hChildStd_IN_Rd = null;
},
}
- errdefer if (self.stdin_behavior == StdIo.Pipe) { windowsDestroyPipe(g_hChildStd_IN_Rd, g_hChildStd_IN_Wr); };
+ errdefer if (self.stdin_behavior == StdIo.Pipe) {
+ windowsDestroyPipe(g_hChildStd_IN_Rd, g_hChildStd_IN_Wr);
+ };
var g_hChildStd_OUT_Rd: ?windows.HANDLE = null;
var g_hChildStd_OUT_Wr: ?windows.HANDLE = null;
@@ -488,7 +503,9 @@ pub const ChildProcess = struct {
g_hChildStd_OUT_Wr = null;
},
}
- errdefer if (self.stdin_behavior == StdIo.Pipe) { windowsDestroyPipe(g_hChildStd_OUT_Rd, g_hChildStd_OUT_Wr); };
+ errdefer if (self.stdin_behavior == StdIo.Pipe) {
+ windowsDestroyPipe(g_hChildStd_OUT_Rd, g_hChildStd_OUT_Wr);
+ };
var g_hChildStd_ERR_Rd: ?windows.HANDLE = null;
var g_hChildStd_ERR_Wr: ?windows.HANDLE = null;
@@ -506,12 +523,14 @@ pub const ChildProcess = struct {
g_hChildStd_ERR_Wr = null;
},
}
- errdefer if (self.stdin_behavior == StdIo.Pipe) { windowsDestroyPipe(g_hChildStd_ERR_Rd, g_hChildStd_ERR_Wr); };
+ errdefer if (self.stdin_behavior == StdIo.Pipe) {
+ windowsDestroyPipe(g_hChildStd_ERR_Rd, g_hChildStd_ERR_Wr);
+ };
const cmd_line = try windowsCreateCommandLine(self.allocator, self.argv);
defer self.allocator.free(cmd_line);
- var siStartInfo = windows.STARTUPINFOA {
+ var siStartInfo = windows.STARTUPINFOA{
.cb = @sizeOf(windows.STARTUPINFOA),
.hStdError = g_hChildStd_ERR_Wr,
.hStdOutput = g_hChildStd_OUT_Wr,
@@ -534,19 +553,11 @@ pub const ChildProcess = struct {
};
var piProcInfo: windows.PROCESS_INFORMATION = undefined;
- const cwd_slice = if (self.cwd) |cwd|
- try cstr.addNullByte(self.allocator, cwd)
- else
- null
- ;
+ const cwd_slice = if (self.cwd) |cwd| try cstr.addNullByte(self.allocator, cwd) else null;
defer if (cwd_slice) |cwd| self.allocator.free(cwd);
const cwd_ptr = if (cwd_slice) |cwd| cwd.ptr else null;
- const maybe_envp_buf = if (self.env_map) |env_map|
- try os.createWindowsEnvBlock(self.allocator, env_map)
- else
- null
- ;
+ const maybe_envp_buf = if (self.env_map) |env_map| try os.createWindowsEnvBlock(self.allocator, env_map) else null;
defer if (maybe_envp_buf) |envp_buf| self.allocator.free(envp_buf);
const envp_ptr = if (maybe_envp_buf) |envp_buf| envp_buf.ptr else null;
@@ -563,11 +574,8 @@ pub const ChildProcess = struct {
};
defer self.allocator.free(app_name);
- windowsCreateProcess(app_name.ptr, cmd_line.ptr, envp_ptr, cwd_ptr,
- &siStartInfo, &piProcInfo) catch |no_path_err|
- {
- if (no_path_err != error.FileNotFound)
- return no_path_err;
+ windowsCreateProcess(app_name.ptr, cmd_line.ptr, envp_ptr, cwd_ptr, &siStartInfo, &piProcInfo) catch |no_path_err| {
+ if (no_path_err != error.FileNotFound) return no_path_err;
const PATH = try os.getEnvVarOwned(self.allocator, "PATH");
defer self.allocator.free(PATH);
@@ -577,9 +585,7 @@ pub const ChildProcess = struct {
const joined_path = try os.path.join(self.allocator, search_path, app_name);
defer self.allocator.free(joined_path);
- if (windowsCreateProcess(joined_path.ptr, cmd_line.ptr, envp_ptr, cwd_ptr,
- &siStartInfo, &piProcInfo)) |_|
- {
+ if (windowsCreateProcess(joined_path.ptr, cmd_line.ptr, envp_ptr, cwd_ptr, &siStartInfo, &piProcInfo)) |_| {
break;
} else |err| if (err == error.FileNotFound) {
continue;
@@ -609,9 +615,15 @@ pub const ChildProcess = struct {
self.thread_handle = piProcInfo.hThread;
self.term = null;
- if (self.stdin_behavior == StdIo.Pipe) { os.close(??g_hChildStd_IN_Rd); }
- if (self.stderr_behavior == StdIo.Pipe) { os.close(??g_hChildStd_ERR_Wr); }
- if (self.stdout_behavior == StdIo.Pipe) { os.close(??g_hChildStd_OUT_Wr); }
+ if (self.stdin_behavior == StdIo.Pipe) {
+ os.close(g_hChildStd_IN_Rd.?);
+ }
+ if (self.stderr_behavior == StdIo.Pipe) {
+ os.close(g_hChildStd_ERR_Wr.?);
+ }
+ if (self.stdout_behavior == StdIo.Pipe) {
+ os.close(g_hChildStd_OUT_Wr.?);
+ }
}
fn setUpChildIo(stdio: StdIo, pipe_fd: i32, std_fileno: i32, dev_null_fd: i32) !void {
@@ -622,15 +634,10 @@ pub const ChildProcess = struct {
StdIo.Ignore => try os.posixDup2(dev_null_fd, std_fileno),
}
}
-
};
-fn windowsCreateProcess(app_name: &u8, cmd_line: &u8, envp_ptr: ?&u8, cwd_ptr: ?&u8,
- lpStartupInfo: &windows.STARTUPINFOA, lpProcessInformation: &windows.PROCESS_INFORMATION) !void
-{
- if (windows.CreateProcessA(app_name, cmd_line, null, null, windows.TRUE, 0,
- @ptrCast(?&c_void, envp_ptr), cwd_ptr, lpStartupInfo, lpProcessInformation) == 0)
- {
+fn windowsCreateProcess(app_name: [*]u8, cmd_line: [*]u8, envp_ptr: ?[*]u8, cwd_ptr: ?[*]u8, lpStartupInfo: *windows.STARTUPINFOA, lpProcessInformation: *windows.PROCESS_INFORMATION) !void {
+ if (windows.CreateProcessA(app_name, cmd_line, null, null, windows.TRUE, 0, @ptrCast(?*c_void, envp_ptr), cwd_ptr, lpStartupInfo, lpProcessInformation) == 0) {
const err = windows.GetLastError();
return switch (err) {
windows.ERROR.FILE_NOT_FOUND, windows.ERROR.PATH_NOT_FOUND => error.FileNotFound,
@@ -641,18 +648,16 @@ fn windowsCreateProcess(app_name: &u8, cmd_line: &u8, envp_ptr: ?&u8, cwd_ptr: ?
}
}
-
-
-
/// Caller must dealloc.
/// Guarantees a null byte at result[result.len].
-fn windowsCreateCommandLine(allocator: &mem.Allocator, argv: []const []const u8) ![]u8 {
+fn windowsCreateCommandLine(allocator: *mem.Allocator, argv: []const []const u8) ![]u8 {
var buf = try Buffer.initSize(allocator, 0);
defer buf.deinit();
+ var buf_stream = &io.BufferOutStream.init(&buf).stream;
+
for (argv) |arg, arg_i| {
- if (arg_i != 0)
- try buf.appendByte(' ');
+ if (arg_i != 0) try buf.appendByte(' ');
if (mem.indexOfAny(u8, arg, " \t\n\"") == null) {
try buf.append(arg);
continue;
@@ -663,18 +668,18 @@ fn windowsCreateCommandLine(allocator: &mem.Allocator, argv: []const []const u8)
switch (byte) {
'\\' => backslash_count += 1,
'"' => {
- try buf.appendByteNTimes('\\', backslash_count * 2 + 1);
+ try buf_stream.writeByteNTimes('\\', backslash_count * 2 + 1);
try buf.appendByte('"');
backslash_count = 0;
},
else => {
- try buf.appendByteNTimes('\\', backslash_count);
+ try buf_stream.writeByteNTimes('\\', backslash_count);
try buf.appendByte(byte);
backslash_count = 0;
},
}
}
- try buf.appendByteNTimes('\\', backslash_count * 2);
+ try buf_stream.writeByteNTimes('\\', backslash_count * 2);
try buf.appendByte('"');
}
@@ -686,12 +691,11 @@ fn windowsDestroyPipe(rd: ?windows.HANDLE, wr: ?windows.HANDLE) void {
if (wr) |h| os.close(h);
}
-
// TODO: workaround for bug where the `const` from `&const` is dropped when the type is
// a namespace field lookup
const SECURITY_ATTRIBUTES = windows.SECURITY_ATTRIBUTES;
-fn windowsMakePipe(rd: &windows.HANDLE, wr: &windows.HANDLE, sattr: &const SECURITY_ATTRIBUTES) !void {
+fn windowsMakePipe(rd: *windows.HANDLE, wr: *windows.HANDLE, sattr: *const SECURITY_ATTRIBUTES) !void {
if (windows.CreatePipe(rd, wr, sattr, 0) == 0) {
const err = windows.GetLastError();
return switch (err) {
@@ -709,24 +713,24 @@ fn windowsSetHandleInfo(h: windows.HANDLE, mask: windows.DWORD, flags: windows.D
}
}
-fn windowsMakePipeIn(rd: &?windows.HANDLE, wr: &?windows.HANDLE, sattr: &const SECURITY_ATTRIBUTES) !void {
+fn windowsMakePipeIn(rd: *?windows.HANDLE, wr: *?windows.HANDLE, sattr: *const SECURITY_ATTRIBUTES) !void {
var rd_h: windows.HANDLE = undefined;
var wr_h: windows.HANDLE = undefined;
try windowsMakePipe(&rd_h, &wr_h, sattr);
errdefer windowsDestroyPipe(rd_h, wr_h);
try windowsSetHandleInfo(wr_h, windows.HANDLE_FLAG_INHERIT, 0);
- *rd = rd_h;
- *wr = wr_h;
+ rd.* = rd_h;
+ wr.* = wr_h;
}
-fn windowsMakePipeOut(rd: &?windows.HANDLE, wr: &?windows.HANDLE, sattr: &const SECURITY_ATTRIBUTES) !void {
+fn windowsMakePipeOut(rd: *?windows.HANDLE, wr: *?windows.HANDLE, sattr: *const SECURITY_ATTRIBUTES) !void {
var rd_h: windows.HANDLE = undefined;
var wr_h: windows.HANDLE = undefined;
try windowsMakePipe(&rd_h, &wr_h, sattr);
errdefer windowsDestroyPipe(rd_h, wr_h);
try windowsSetHandleInfo(rd_h, windows.HANDLE_FLAG_INHERIT, 0);
- *rd = rd_h;
- *wr = wr_h;
+ rd.* = rd_h;
+ wr.* = wr_h;
}
fn makePipe() ![2]i32 {
@@ -741,15 +745,15 @@ fn makePipe() ![2]i32 {
return fds;
}
-fn destroyPipe(pipe: &const [2]i32) void {
- os.close((*pipe)[0]);
- os.close((*pipe)[1]);
+fn destroyPipe(pipe: *const [2]i32) void {
+ os.close((pipe.*)[0]);
+ os.close((pipe.*)[1]);
}
// Child of fork calls this to report an error to the fork parent.
// Then the child exits.
fn forkChildErrReport(fd: i32, err: ChildProcess.SpawnError) noreturn {
- _ = writeIntFd(fd, ErrInt(err));
+ _ = writeIntFd(fd, ErrInt(@errorToInt(err)));
posix.exit(1);
}