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

pub const ParsedCommand = struct {
    name: []const u8,
    args: ?[]const u8,
    output_redirect: ?[]const u8,
    error_redirect: ?[]const u8,
};

pub fn parseCommand(input: []const u8) ParsedCommand {
    var i: usize = 0;
    var cmd_buf = std.ArrayList(u8){};
    defer cmd_buf.deinit(std.heap.page_allocator);

    while (i < input.len and input[i] == ' ') : (i += 1) {}

    if (i >= input.len) {
        return .{ .name = "", .args = null, .output_redirect = null, .error_redirect = null };
    }

    if (input[i] == '\'' or input[i] == '"') {
        const quote = input[i];
        i += 1;
        while (i < input.len and input[i] != quote) : (i += 1) {
            _ = cmd_buf.append(std.heap.page_allocator, input[i]) catch {};
        }
        if (i < input.len) i += 1;
    } else {
        while (i < input.len and input[i] != ' ') : (i += 1) {
            if (input[i] == '\\' and i + 1 < input.len) {
                i += 1;
                _ = cmd_buf.append(std.heap.page_allocator, input[i]) catch {};
            } else {
                _ = cmd_buf.append(std.heap.page_allocator, input[i]) catch {};
            }
        }
    }

    while (i < input.len and input[i] == ' ') : (i += 1) {}

    var redirect_pos: ?usize = null;
    var error_redirect_pos: ?usize = null;
    var j = i;
    while (j < input.len) : (j += 1) {
        if (j + 1 < input.len and input[j] == '2' and input[j + 1] == '>') {
            error_redirect_pos = j;
            j += 1;
        } else if (input[j] == '1' and j + 1 < input.len and input[j + 1] == '>') {
            redirect_pos = j;
            j += 1;
        } else if (input[j] == '>') {
            redirect_pos = j;
        }
    }

    const cmd_name = cmd_buf.toOwnedSlice(std.heap.page_allocator) catch "";
    var args: ?[]const u8 = null;
    var output_redirect: ?[]const u8 = null;
    var error_redirect: ?[]const u8 = null;

    var args_end = input.len;
    if (redirect_pos) |pos| {
        if (pos < args_end) args_end = pos;
    }
    if (error_redirect_pos) |pos| {
        if (pos < args_end) args_end = pos;
    }

    while (args_end > i and input[args_end - 1] == ' ') : (args_end -= 1) {}

    if (args_end > i) {
        args = input[i..args_end];
    }

    if (redirect_pos) |pos| {
        var k = pos;
        if (input[k] == '1') k += 1;
        if (k < input.len and input[k] == '>') k += 1;

        while (k < input.len and input[k] == ' ') : (k += 1) {}

        if (k < input.len) {
            var redir_buf = std.ArrayList(u8){};
            defer redir_buf.deinit(std.heap.page_allocator);

            while (k < input.len and input[k] != ' ' and !(k + 1 < input.len and input[k] == '2' and input[k + 1] == '>')) : (k += 1) {
                _ = redir_buf.append(std.heap.page_allocator, input[k]) catch {};
            }
            output_redirect = redir_buf.toOwnedSlice(std.heap.page_allocator) catch null;
        }
    }

    if (error_redirect_pos) |pos| {
        var k = pos + 2;
        while (k < input.len and input[k] == ' ') : (k += 1) {}

        if (k < input.len) {
            var redir_buf = std.ArrayList(u8){};
            defer redir_buf.deinit(std.heap.page_allocator);

            while (k < input.len and input[k] != ' ') : (k += 1) {
                _ = redir_buf.append(std.heap.page_allocator, input[k]) catch {};
            }
            error_redirect = redir_buf.toOwnedSlice(std.heap.page_allocator) catch null;
        }
    }

    return .{ .name = cmd_name, .args = args, .output_redirect = output_redirect, .error_redirect = error_redirect };
}

pub fn parseArgs(allocator: std.mem.Allocator, cmd_name: []const u8, args_str: ?[]const u8) ![]const []const u8 {
    var args_list = std.ArrayList([]const u8){};
    try args_list.ensureTotalCapacity(allocator, 16);
    errdefer args_list.deinit(allocator);

    try args_list.append(allocator, cmd_name);

    if (args_str) |args| {
        var i: usize = 0;
        var arg_buf = std.ArrayList(u8){};
        defer arg_buf.deinit(allocator);

        while (i < args.len) {
            if (args[i] == '\'') {
                i += 1;
                while (i < args.len and args[i] != '\'') : (i += 1) {
                    try arg_buf.append(allocator, args[i]);
                }
                if (i < args.len) i += 1;
            } else if (args[i] == '"') {
                i += 1;
                while (i < args.len and args[i] != '"') : (i += 1) {
                    if (args[i] == '\\' and i + 1 < args.len) {
                        const next = args[i + 1];
                        if (next == '"' or next == '\\') {
                            i += 1;
                            try arg_buf.append(allocator, args[i]);
                        } else {
                            try arg_buf.append(allocator, args[i]);
                        }
                    } else {
                        try arg_buf.append(allocator, args[i]);
                    }
                }
                if (i < args.len) i += 1;
            } else if (args[i] == '\\' and i + 1 < args.len) {
                i += 1;
                try arg_buf.append(allocator, args[i]);
                i += 1;
            } else if (args[i] == ' ') {
                if (arg_buf.items.len > 0) {
                    try args_list.append(allocator, try arg_buf.toOwnedSlice(allocator));
                }
                i += 1;
            } else {
                try arg_buf.append(allocator, args[i]);
                i += 1;
            }
        }

        if (arg_buf.items.len > 0) {
            try args_list.append(allocator, try arg_buf.toOwnedSlice(allocator));
        }
    }

    return args_list.toOwnedSlice(allocator);
}