summaryrefslogtreecommitdiff
path: root/src/eval.zig
blob: 8676516311a250e6a12431cb96176d690f38b107 (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
const std = @import("std");
const print = std.debug.print;
const ArrayList = std.ArrayList;
const Allocator = std.mem.Allocator;
const Thread = std.Thread;
const Mutex = std.Thread.Mutex;

const c = @cImport({
    @cInclude("errno.h");
    @cInclude("stdio.h");
    if (@import("builtin").os.tag != .windows) {
        @cInclude("sys/statvfs.h");
    }
});

const cmd = @import("cmd.zig");
const Command = cmd.Command;
const BuiltinCommand = cmd.BuiltinCommand;

const syntax = @import("syntax.zig");
const FileSpec = syntax.FileSpec;
const RedirectType = syntax.RedirectType;
const Redirect = syntax.Redirect;

const paths = @import("paths.zig");
const formatDosPath = paths.formatDosPath;
const convertTo83 = paths.convertTo83;

const cmdTypes = @import("cmd/lib/types.zig");
pub const CommandStatus = cmdTypes.CommandStatus;
const OutputCapture = cmdTypes.OutputCapture;
const InputSource = cmdTypes.InputSource;

const STDOUT_BUFFER_SIZE: usize = 1024;
const STDERR_BUFFER_SIZE: usize = 1024;

pub fn executeCommand(command: Command, allocator: Allocator) !CommandStatus {
    return executeCommandWithOutput(command, allocator, null, null);
}

fn executeCommandWithOutput(command: Command, allocator: Allocator, output_capture: ?*OutputCapture, input_source: ?*InputSource) !CommandStatus {
    switch (command) {
        .Empty => return CommandStatus{ .Code = 0 },

        .Builtin => |builtin_cmd| {
            switch (builtin_cmd) {
                .EchoText => |echo_text| {
                    return echo_text.eval(allocator, output_capture, input_source);
                },
                .Cls => |cls| {
                    return cls.eval(allocator, output_capture, input_source);
                },
                .Exit => {
                    return CommandStatus.ExitShell;
                },
                .EchoPlain => |echo_plain| {
                    return echo_plain.eval(allocator, output_capture, input_source);
                },
                .EchoOn => |echo_on| {
                    return echo_on.eval(allocator, output_capture, input_source);
                },
                .EchoOff => |echo_off| {
                    return echo_off.eval(allocator, output_capture, input_source);
                },
                .Ver => |ver| {
                    return ver.eval(allocator, output_capture, input_source);
                },
                .Date => |date| {
                    return date.eval(allocator, output_capture, input_source);
                },
                .Time => |time| {
                    return time.eval(allocator, output_capture, input_source);
                },
                .Dir => |dir| {
                    return dir.eval(allocator, output_capture, input_source);
                },
                .Type => |type_cmd| {
                    return type_cmd.eval(allocator, output_capture, input_source);
                },
                .Sort => |sort| {
                    return sort.eval(allocator, output_capture, input_source);
                },
                .Chdir => |chdir| {
                    return chdir.eval(allocator, output_capture, input_source);
                },
                .Copy => |copy| {
                    return copy.eval(allocator, output_capture, input_source);
                },
                .Remove => |remove| {
                    return remove.eval(allocator, output_capture, input_source);
                },
                .Mkdir => |mkdir| {
                    return mkdir.eval(allocator, output_capture, input_source);
                },
                .Rmdir => |rmdir| {
                    return rmdir.eval(allocator, output_capture, input_source);
                },
                .Rename => |rename| {
                    return rename.eval(allocator, output_capture, input_source);
                },
                .Move => |move| {
                    return move.eval(allocator, output_capture, input_source);
                },
                .PathGet => |path_get| {
                    return path_get.eval(allocator, output_capture, input_source);
                },
                .PathSet => |path_set| {
                    return path_set.eval(allocator, output_capture, input_source);
                },
                else => {
                    const error_msg = try std.fmt.allocPrint(allocator, "Command not implemented: {any}\n", .{builtin_cmd});
                    defer allocator.free(error_msg);
                    if (output_capture) |capture| {
                        try capture.write(error_msg);
                    } else {
                        print("{s}", .{error_msg});
                    }
                    return CommandStatus{ .Code = 1 };
                },
            }
        },

        .External => |external| {
            // Try to execute external command
            var child_args = ArrayList([]const u8).init(allocator);
            defer child_args.deinit();

            try child_args.append(external.program);
            for (external.args.items) |arg| {
                try child_args.append(arg);
            }

            var child = std.process.Child.init(child_args.items, allocator);

            // Set up pipes for capturing output
            child.stdin_behavior = if (input_source != null) .Pipe else .Inherit;
            child.stdout_behavior = if (output_capture != null) .Pipe else .Inherit;
            child.stderr_behavior = if (output_capture != null) .Pipe else .Inherit;

            const spawn_result = child.spawn();
            if (spawn_result) |_| {
                // Spawn succeeded, continue with execution
            } else |err| switch (err) {
                error.FileNotFound => {
                    const error_msg = try std.fmt.allocPrint(allocator, "'{s}' is not recognized as an internal or external command,\noperable program or batch file.\n", .{external.program});
                    defer allocator.free(error_msg);

                    if (output_capture) |capture| {
                        try capture.write(error_msg);
                    } else {
                        print("{s}", .{error_msg});
                    }
                    return CommandStatus{ .Code = 1 };
                },
                error.AccessDenied => {
                    const error_msg = "Access is denied.\n";
                    if (output_capture) |capture| {
                        try capture.write(error_msg);
                    } else {
                        print("{s}", .{error_msg});
                    }
                    return CommandStatus{ .Code = 1 };
                },
                else => {
                    const error_msg = try std.fmt.allocPrint(allocator, "Cannot execute '{s}': {}\n", .{ external.program, err });
                    defer allocator.free(error_msg);

                    if (output_capture) |capture| {
                        try capture.write(error_msg);
                    } else {
                        print("{s}", .{error_msg});
                    }
                    return CommandStatus{ .Code = 1 };
                },
            }

            // Handle input redirection
            if (input_source) |source| {
                if (child.stdin) |stdin| {
                    const writer = stdin.writer();

                    // Reset source position for reading
                    var temp_source = source.*;
                    temp_source.position = 0;

                    while (try temp_source.readLine(allocator)) |line| {
                        defer allocator.free(line);
                        try writer.print("{s}\n", .{line});
                    }
                    child.stdin.?.close();
                    child.stdin = null;
                }
            }

            // Handle output capture
            if (output_capture) |capture| {
                // Read stdout
                if (child.stdout) |stdout| {
                    var buffer: [4096]u8 = undefined;
                    while (true) {
                        const bytes_read = stdout.read(&buffer) catch break;
                        if (bytes_read == 0) break;
                        try capture.write(buffer[0..bytes_read]);
                    }
                }

                // Read stderr
                if (child.stderr) |stderr| {
                    var buffer: [4096]u8 = undefined;
                    while (true) {
                        const bytes_read = stderr.read(&buffer) catch break;
                        if (bytes_read == 0) break;
                        try capture.write(buffer[0..bytes_read]);
                    }
                }
            }

            // Wait for process to complete
            const term = child.wait() catch |err| {
                const error_msg = switch (err) {
                    error.FileNotFound => try std.fmt.allocPrint(allocator, "'{s}' is not recognized as an internal or external command,\noperable program or batch file.\n", .{external.program}),
                    else => try std.fmt.allocPrint(allocator, "Error waiting for command: {}\n", .{err}),
                };
                defer allocator.free(error_msg);

                if (output_capture) |capture| {
                    try capture.write(error_msg);
                } else {
                    print("{s}", .{error_msg});
                }
                return CommandStatus{ .Code = 1 };
            };

            // Return exit code
            switch (term) {
                .Exited => |code| return CommandStatus{ .Code = @intCast(code) },
                .Signal => |_| return CommandStatus{ .Code = 1 },
                .Stopped => |_| return CommandStatus{ .Code = 1 },
                .Unknown => |_| return CommandStatus{ .Code = 1 },
            }
        },

        .Redirect => |redirect| {
            // Check if we have any output redirections
            var has_output_redirect = false;
            for (redirect.redirects.items) |redir| {
                if (redir.redirect_type == .OutputOverwrite or redir.redirect_type == .OutputAppend) {
                    has_output_redirect = true;
                    break;
                }
            }

            var captured_output = OutputCapture.init(allocator);
            defer captured_output.deinit();

            // Prepare input redirection if needed
            var input_data: ?[]const u8 = null;
            var redirect_input_source: ?InputSource = null;
            defer if (input_data) |data| allocator.free(data);

            // Process input redirections first
            for (redirect.redirects.items) |redir| {
                if (redir.redirect_type == .InputFrom) {
                    const file_path = switch (redir.target) {
                        .Con => {
                            print("Input redirection from CON not supported\n", .{});
                            return CommandStatus{ .Code = 1 };
                        },
                        .Lpt1, .Lpt2, .Lpt3, .Prn => {
                            print("Cannot redirect input from device\n", .{});
                            return CommandStatus{ .Code = 1 };
                        },
                        .Path => |path| path,
                    };

                    // Read input file
                    const file = std.fs.cwd().openFile(file_path, .{}) catch |err| {
                        switch (err) {
                            error.FileNotFound => print("The system cannot find the file specified.\n", .{}),
                            error.AccessDenied => print("Access is denied.\n", .{}),
                            else => print("Cannot open input file.\n", .{}),
                        }
                        return CommandStatus{ .Code = 1 };
                    };
                    defer file.close();

                    input_data = file.readToEndAlloc(allocator, std.math.maxInt(usize)) catch |err| {
                        switch (err) {
                            error.AccessDenied => print("Access is denied.\n", .{}),
                            else => print("Cannot read input file.\n", .{}),
                        }
                        return CommandStatus{ .Code = 1 };
                    };

                    redirect_input_source = InputSource.init(input_data.?);
                    break; // Only handle first input redirection
                }
            }

            // Execute the command with input and output capture (only capture output if needed)
            const status = try executeCommandWithOutput(redirect.command.*, allocator, if (has_output_redirect) &captured_output else null, if (redirect_input_source) |*source| source else null);

            // Handle output redirections
            for (redirect.redirects.items) |redir| {
                if (redir.redirect_type == .InputFrom) continue; // Already handled
                const file_path = switch (redir.target) {
                    .Con => {
                        // Redirect to console - just print normally
                        print("{s}", .{captured_output.getContents()});
                        continue;
                    },
                    .Lpt1, .Lpt2, .Lpt3, .Prn => {
                        print("Cannot redirect to device\n", .{});
                        return CommandStatus{ .Code = 1 };
                    },
                    .Path => |path| path,
                };

                // Handle different redirect types
                switch (redir.redirect_type) {
                    .OutputOverwrite => {
                        // Write to file, overwriting existing content
                        const file = std.fs.cwd().createFile(file_path, .{}) catch |err| {
                            switch (err) {
                                error.AccessDenied => print("Access is denied.\n", .{}),
                                else => print("Cannot create file.\n", .{}),
                            }
                            return CommandStatus{ .Code = 1 };
                        };
                        defer file.close();

                        file.writeAll(captured_output.getContents()) catch |err| {
                            switch (err) {
                                error.AccessDenied => print("Access is denied.\n", .{}),
                                else => print("Cannot write to file.\n", .{}),
                            }
                            return CommandStatus{ .Code = 1 };
                        };
                    },
                    .OutputAppend => {
                        // Append to file
                        const file = std.fs.cwd().openFile(file_path, .{ .mode = .write_only }) catch |err| {
                            switch (err) {
                                error.FileNotFound => {
                                    // Create new file if it doesn't exist
                                    const new_file = std.fs.cwd().createFile(file_path, .{}) catch |create_err| {
                                        switch (create_err) {
                                            error.AccessDenied => print("Access is denied.\n", .{}),
                                            else => print("Cannot create file.\n", .{}),
                                        }
                                        return CommandStatus{ .Code = 1 };
                                    };
                                    defer new_file.close();

                                    new_file.writeAll(captured_output.getContents()) catch {
                                        print("Cannot write to file.\n", .{});
                                        return CommandStatus{ .Code = 1 };
                                    };
                                    continue;
                                },
                                error.AccessDenied => {
                                    print("Access is denied.\n", .{});
                                    return CommandStatus{ .Code = 1 };
                                },
                                else => {
                                    print("Cannot open file.\n", .{});
                                    return CommandStatus{ .Code = 1 };
                                },
                            }
                        };
                        defer file.close();

                        // Seek to end for append
                        file.seekFromEnd(0) catch {
                            print("Cannot seek to end of file.\n", .{});
                            return CommandStatus{ .Code = 1 };
                        };

                        file.writeAll(captured_output.getContents()) catch |err| {
                            switch (err) {
                                error.AccessDenied => print("Access is denied.\n", .{}),
                                else => print("Cannot write to file.\n", .{}),
                            }
                            return CommandStatus{ .Code = 1 };
                        };
                    },
                    .InputFrom => {
                        // Input redirection already handled above
                        continue;
                    },
                }
            }

            return status;
        },

        else => {
            const error_msg = "Command type not implemented\n";
            if (output_capture) |capture| {
                try capture.write(error_msg);
            } else {
                print("{s}", .{error_msg});
            }
            return CommandStatus{ .Code = 1 };
        },
    }
}