-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_exe.zig
More file actions
140 lines (129 loc) · 4.5 KB
/
test_exe.zig
File metadata and controls
140 lines (129 loc) · 4.5 KB
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
const std = @import("std");
pub fn main() !void {
const allocator = std.heap.page_allocator;
var it = try std.process.argsWithAllocator(allocator);
defer it.deinit();
var args_list = std.array_list.Managed([]const u8).init(allocator);
defer args_list.deinit();
while (it.next()) |arg| {
try args_list.append(arg);
}
const args = args_list.items;
// detect --stderr, --exit N, --abort and collect positional args (excluding program name)
var pos_list = std.array_list.Managed([]const u8).init(allocator);
defer pos_list.deinit();
var use_stderr = false;
var i: usize = 1;
var exit_requested: ?u8 = null;
var abort_requested = false;
var print_cwd = false;
var getenv_name: ?[]const u8 = null;
var interactive = false;
while (i < args.len) : (i += 1) {
const s = args[i];
if (std.mem.eql(u8, s, "--stderr")) {
use_stderr = true;
continue;
}
if (std.mem.eql(u8, s, "--interactive")) {
interactive = true;
continue;
}
if (std.mem.eql(u8, s, "--abort")) {
abort_requested = true;
continue;
}
if (std.mem.eql(u8, s, "--print-cwd")) {
print_cwd = true;
continue;
}
if (std.mem.eql(u8, s, "--getenv")) {
if (i + 1 >= args.len) {
std.debug.print("missing getenv name\n", .{});
std.process.exit(1);
}
getenv_name = args[i + 1];
i += 1;
continue;
}
if (std.mem.eql(u8, s, "--exit")) {
// next argument must be the exit code
if (i + 1 >= args.len) {
std.debug.print("missing exit code\n", .{});
std.process.exit(1);
}
const code_s = args[i + 1];
// parse as unsigned 8-bit integer
const parsed = std.fmt.parseInt(u8, code_s, 10) catch {
std.debug.print("invalid exit code: {s}\n", .{code_s});
std.process.exit(1);
};
exit_requested = parsed;
i += 1; // skip the numeric argument
continue;
}
try pos_list.append(s);
}
const pos = pos_list.items;
// single writer buffer used for whichever stream we pick
var buf: [1024]u8 = undefined;
var writer = if (use_stderr) std.fs.File.stderr().writer(&buf) else std.fs.File.stdout().writer(&buf);
const io = &writer.interface;
if (interactive) {
var stdin_buffer: [1024]u8 = undefined;
var stdin_reader = std.fs.File.stdin().reader(&stdin_buffer);
var stdin = &stdin_reader.interface;
while (stdin.takeDelimiterExclusive('\n')) |line| {
const trimmed = std.mem.trimRight(u8, line, "\r");
if (std.mem.eql(u8, trimmed, "PING")) {
try io.writeAll("PONG\n");
} else if (std.mem.startsWith(u8, trimmed, "ECHO ")) {
try io.writeAll(trimmed[5..]);
try io.writeAll("\n");
} else if (std.mem.eql(u8, trimmed, "EXIT")) {
break;
} else {
try io.print("UNKNOWN: {s}\n", .{trimmed});
}
try io.flush();
// NOTE: this is required in order to prevents the program loop forever
_ = try stdin.take(1);
} else |err| switch (err) {
error.EndOfStream => {
std.debug.print("END OF STREAM\n", .{});
},
else => |e| return e,
}
return;
}
if (getenv_name) |name| {
var env_map = std.process.getEnvMap(allocator) catch @panic("fails to get map");
defer env_map.deinit();
if (env_map.get(name)) |v| {
try io.writeAll(v);
}
try io.writeAll("\n");
} else if (print_cwd) {
const cwd = try std.process.getCwdAlloc(allocator);
defer allocator.free(cwd);
try io.writeAll(cwd);
try io.writeAll("\n");
} else if (pos.len == 0) {
try io.writeAll("OK\n");
} else {
var j: usize = 0;
while (j < pos.len) : (j += 1) {
try io.writeAll(pos[j]);
try io.writeAll("\n");
}
}
try io.flush();
// Handle special termination flags after producing output.
if (abort_requested) {
// abort will terminate the process with SIGABRT on POSIX.
std.process.abort();
}
if (exit_requested) |code| {
std.process.exit(code);
}
}