summaryrefslogtreecommitdiff
path: root/src/shock.zig
blob: 90e6edf0dc9a6fb0d7cdc0f8822e57865bd489ea (plain) (blame)
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
const std = @import("std");
const elfo = @import("elfo.zig");

pub fn main(init: std.process.Init.Minimal) !void {
    var args = init.args.iterate();
    _ = args.skip();
    const subject = args.next() orelse "./study-samples/frog";

    std.debug.print("arg: {s}\n", .{subject});

    var debug_alloc: std.heap.DebugAllocator(.{}) = .init;
    const alloc = debug_alloc.allocator();

    var threaded: std.Io.Threaded = .init(alloc, .{
        .argv0 = .init(init.args),
        .environ = init.environ,
    });
    defer threaded.deinit();
    const io = threaded.io();

    // var evented: std.Io.Evented = undefined;
    // try evented.init(alloc, .{
    //     .argv0 = .init(init.args),
    //     .environ = init.environ,
    //     .backing_allocator_needs_mutex = false,
    // });
    // defer evented.deinit();
    // const io = evented.io();

    // const f = try std.Io.Dir.cwd().openFile(io, subject, .{ .mode = .read_only });
    // std.debug.print("file: {any}\n", .{f});
    var child = try std.process.spawn(io, .{
        .argv = &.{subject},
        .stdout = .pipe,
        .stdin = .inherit,
    });

    const buf = try alloc.alloc(u8, 0x400);
    defer alloc.free(buf);
    _ = try child.stdout.?.readStreaming(io, &.{buf});

    std.debug.print("spawned child: {any}\n", .{child});

    // io.async(function: anytype, args: (unknown type))
    var child_fut = io.async(std.process.Child.wait, .{ &child, io });
    const child_out = try child_fut.await(io);
    // const child_out = try child.wait(io);
    std.debug.print("stdout: {s}\n", .{buf});

    {
        var buffer: [64]u8 = undefined;
        const stderr = try io.lockStderr(&buffer, .escape_codes);
        defer io.unlockStderr();
        const term = stderr.terminal();
        try elfo.printHexdump(u8, term.writer, term, buf, 0);
    }

    std.debug.print("waited term: {any}\n", .{child_out});
}