summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--build.zig22
-rw-r--r--src/gloves.zig67
2 files changed, 86 insertions, 3 deletions
diff --git a/build.zig b/build.zig
index 1b199d3..beacc7b 100644
--- a/build.zig
+++ b/build.zig
@@ -41,6 +41,12 @@ pub fn build(b: *std.Build) !void {
.target = target,
.optimize = optimize,
});
+ const gloves_module = b.createModule(.{
+ .root_source_file = b.path("src/gloves.zig"),
+ .target = target,
+ .optimize = optimize,
+ .link_libc = true,
+ });
const cs = capstone(b, .{
.target = target,
@@ -56,12 +62,22 @@ pub fn build(b: *std.Build) !void {
.root_module = elfo_module,
});
+ const gloves = b.addExecutable(.{
+ .name = "gloves",
+ .root_module = gloves_module,
+ });
+
elfo.linkLibrary(cs.artifact);
b.installArtifact(elfo);
+ b.installArtifact(gloves);
+
+ const run_elfo_cmd = b.addRunArtifact(elfo);
+ const run_elfo_step = b.step("elfo", "See the pretty elfo!");
+ run_elfo_step.dependOn(&run_elfo_cmd.step);
- const run_cmd = b.addRunArtifact(elfo);
- const run_step = b.step("elfo", "See the pretty elfo!");
- run_step.dependOn(&run_cmd.step);
+ const run_gloves_cmd = b.addRunArtifact(gloves);
+ const run_gloves_step = b.step("gloves", "See the pretty gloves!");
+ run_gloves_step.dependOn(&run_gloves_cmd.step);
// TODO: add flag to run on pwndbg
}
diff --git a/src/gloves.zig b/src/gloves.zig
new file mode 100644
index 0000000..b99df55
--- /dev/null
+++ b/src/gloves.zig
@@ -0,0 +1,67 @@
+const std = @import("std");
+
+// https://github.com/ghostty-org/ghostty/blob/9fb03ba55c9e53901193187d5c43341f5b1b430d/src/pty.zig#L11
+pub const winsize = extern struct {
+ ws_row: u16 = 100,
+ ws_col: u16 = 80,
+ ws_xpixel: u16 = 800,
+ ws_ypixel: u16 = 600,
+};
+
+var gpa: std.heap.GeneralPurposeAllocator(.{}) = .init;
+const alloc = gpa.allocator();
+const ArrayList = std.ArrayList;
+
+pub fn main() !void {
+ var child = std.process.Child.init(&.{"./study-samples/split"}, alloc);
+ child.stdin_behavior = .Pipe;
+ child.stdout_behavior = .Pipe;
+ child.stderr_behavior = .Pipe;
+
+ var stdin_buf: [0x100]u8 = undefined;
+ var stdin = std.fs.File.stdin().readerStreaming(&stdin_buf);
+
+ var stdout: ArrayList(u8) = .empty;
+ defer stdout.deinit(alloc);
+ var stderr: ArrayList(u8) = .empty;
+ defer stderr.deinit(alloc);
+
+ try child.spawn();
+
+ var child_writer_buf: [0x100]u8 = undefined;
+ var child_stdin_writer = child.stdin.?.writer(&child_writer_buf);
+
+ while (true) {
+ if (stdin.interface.streamDelimiter(&child_stdin_writer.interface, '\n')) |_| {} else |_| {
+ break;
+ }
+ // if (n == 0) break;
+ _ = try child_stdin_writer.interface.writeByte('\n');
+ if (child_stdin_writer.interface.flush()) |_| {} else |_| {
+ break;
+ }
+ try child.collectOutput(alloc, &stdout, &stderr, 0x1000);
+ std.debug.print("{s}\n {s}\n", .{ stdout.items, stderr.items });
+ }
+
+ errdefer {
+ _ = child.kill() catch {};
+ }
+
+ const term = try child.wait();
+ switch (term) {
+ .Signal => |s| {
+ inline for (@typeInfo(std.os.linux.SIG).@"struct".decls) |f| {
+ const field = @field(std.os.linux.SIG, f.name);
+ const t = @TypeOf(field);
+ if (t == comptime_int) {
+ if (field == s)
+ std.debug.print("Returned with Signal: {s} ({d})\n", .{ f.name, s });
+ }
+ }
+ },
+ else => std.debug.print("{any}\n", .{ .term = term }),
+ }
+
+ // std.debug.print("*** If you this message alone... Something probably went wrong\nIs this leaking? {any}\n", .{gpa.detectLeaks()});
+}