1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
const std = @import("std");
const builtins = @import("builtins.zig");
pub fn runExternalProgram(allocator: std.mem.Allocator, program_path: []const u8, argv: []const []const u8) !void {
const argv_z = try allocator.allocSentinel(?[*:0]const u8, argv.len, null);
defer allocator.free(argv_z);
for (argv, 0..) |arg, i| {
argv_z[i] = (try allocator.dupeZ(u8, arg)).ptr;
}
defer {
for (argv_z[0..argv.len]) |arg_ptr| {
if (arg_ptr) |ptr| allocator.free(std.mem.span(ptr));
}
}
const program_path_z = try allocator.dupeZ(u8, program_path);
defer allocator.free(program_path_z);
const pid = try std.posix.fork();
if (pid == 0) {
_ = std.posix.execveZ(program_path_z, argv_z, std.c.environ) catch {
std.posix.exit(1);
};
unreachable;
} else {
_ = std.posix.waitpid(pid, 0);
}
}
pub fn runExternalProgramWithRedirect(allocator: std.mem.Allocator, program_path: []const u8, argv: []const []const u8, output_file: ?[]const u8, error_file: ?[]const u8, append_file: ?[]const u8, append_error_file: ?[]const u8) !void {
const argv_z = try allocator.allocSentinel(?[*:0]const u8, argv.len, null);
defer allocator.free(argv_z);
for (argv, 0..) |arg, i| {
argv_z[i] = (try allocator.dupeZ(u8, arg)).ptr;
}
defer {
for (argv_z[0..argv.len]) |arg_ptr| {
if (arg_ptr) |ptr| allocator.free(std.mem.span(ptr));
}
}
const program_path_z = try allocator.dupeZ(u8, program_path);
defer allocator.free(program_path_z);
const pid = try std.posix.fork();
if (pid == 0) {
if (output_file) |file| {
const cwd = std.fs.cwd();
const fd = cwd.createFile(file, .{}) catch {
std.posix.exit(1);
};
defer fd.close();
try std.posix.dup2(fd.handle, 1);
} else if (append_file) |file| {
const cwd = std.fs.cwd();
const fd = cwd.openFile(file, .{ .mode = .write_only }) catch |err| blk: {
if (err == error.FileNotFound) {
break :blk cwd.createFile(file, .{}) catch {
std.posix.exit(1);
};
}
std.posix.exit(1);
};
defer fd.close();
fd.seekFromEnd(0) catch {
std.posix.exit(1);
};
try std.posix.dup2(fd.handle, 1);
}
if (error_file) |file| {
const cwd = std.fs.cwd();
const fd = cwd.createFile(file, .{}) catch {
std.posix.exit(1);
};
defer fd.close();
try std.posix.dup2(fd.handle, 2);
} else if (append_error_file) |file| {
const cwd = std.fs.cwd();
const fd = cwd.openFile(file, .{ .mode = .write_only }) catch |err| blk: {
if (err == error.FileNotFound) {
break :blk cwd.createFile(file, .{}) catch {
std.posix.exit(1);
};
}
std.posix.exit(1);
};
defer fd.close();
fd.seekFromEnd(0) catch {
std.posix.exit(1);
};
try std.posix.dup2(fd.handle, 2);
}
_ = std.posix.execveZ(program_path_z, argv_z, std.c.environ) catch {
std.posix.exit(1);
};
unreachable;
} else {
_ = std.posix.waitpid(pid, 0);
}
}
fn runBuiltinInChild(
allocator: std.mem.Allocator,
cmd_name: []const u8,
args: ?[]const u8,
stdin_fd: std.posix.fd_t,
stdout_fd: std.posix.fd_t,
) noreturn {
// Redirect stdin/stdout to provided fds (already valid)
if (stdin_fd != 0) std.posix.dup2(stdin_fd, 0) catch {};
if (stdout_fd != 1) std.posix.dup2(stdout_fd, 1) catch {};
var out_file = std.fs.File{ .handle = 1 };
var out_writer = out_file.writerStreaming(&.{});
const w = &out_writer.interface;
if (std.mem.eql(u8, cmd_name, "echo")) {
builtins.executeEcho(w, args) catch {};
} else if (std.mem.eql(u8, cmd_name, "type")) {
builtins.executeType(allocator, w, args) catch {};
} else if (std.mem.eql(u8, cmd_name, "pwd")) {
builtins.executePwd(allocator, w) catch {};
} else if (std.mem.eql(u8, cmd_name, "cd")) {
builtins.executeCd(allocator, w, args) catch {};
} else if (std.mem.eql(u8, cmd_name, "exit")) {
// exit inside pipeline child just exits child
}
std.posix.exit(0);
}
pub fn runPipeline(
allocator: std.mem.Allocator,
stages: []const Stage,
) !void {
if (stages.len == 0) return;
if (stages.len == 1) {
const s = stages[0];
if (s.is_builtin) {
runBuiltinInChild(allocator, s.name, s.args, 0, 1);
} else {
const argv_z = try allocator.allocSentinel(?[*:0]const u8, s.argv.?.len, @as(?[*:0]const u8, null));
defer allocator.free(argv_z);
for (s.argv.?, 0..) |arg, i| argv_z[i] = (try allocator.dupeZ(u8, arg)).ptr;
defer {
for (argv_z[0..s.argv.?.len]) |arg_ptr| if (arg_ptr) |ptr| allocator.free(std.mem.span(ptr));
}
const path_z = try allocator.dupeZ(u8, s.path.?);
defer allocator.free(path_z);
_ = std.posix.execveZ(path_z, argv_z, std.c.environ) catch std.posix.exit(1);
}
}
const pipes = try allocator.alloc([2]std.posix.fd_t, stages.len - 1);
defer allocator.free(pipes);
for (pipes) |*p| p.* = try std.posix.pipe();
var pids = try allocator.alloc(std.posix.pid_t, stages.len);
defer allocator.free(pids);
for (stages, 0..) |s, idx| {
const pid = try std.posix.fork();
if (pid == 0) {
if (idx > 0) {
try std.posix.dup2(pipes[idx - 1][0], 0);
}
if (idx + 1 < stages.len) {
try std.posix.dup2(pipes[idx][1], 1);
}
for (pipes) |p| {
std.posix.close(p[0]);
std.posix.close(p[1]);
}
if (s.is_builtin) {
runBuiltinInChild(allocator, s.name, s.args, 0, 1);
} else {
const argv_z = try allocator.allocSentinel(?[*:0]const u8, s.argv.?.len, @as(?[*:0]const u8, null));
defer allocator.free(argv_z);
for (s.argv.?, 0..) |arg, i| argv_z[i] = (try allocator.dupeZ(u8, arg)).ptr;
defer {
for (argv_z[0..s.argv.?.len]) |arg_ptr| if (arg_ptr) |ptr| allocator.free(std.mem.span(ptr));
}
const path_z = try allocator.dupeZ(u8, s.path.?);
defer allocator.free(path_z);
_ = std.posix.execveZ(path_z, argv_z, std.c.environ) catch std.posix.exit(1);
}
unreachable;
}
pids[idx] = pid;
}
for (pipes) |p| {
std.posix.close(p[0]);
std.posix.close(p[1]);
}
for (pids) |pid| {
_ = std.posix.waitpid(pid, 0);
}
}
pub const Stage = struct {
is_builtin: bool,
name: []const u8,
args: ?[]const u8,
path: ?[]const u8,
argv: ?[]const []const u8,
};
|