summaryrefslogtreecommitdiff
path: root/src/executor.zig
blob: cdddb4fef10f6de888627e1b3ced3d24ce882eed (plain)
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
const std = @import("std");

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);
    }
}