summaryrefslogtreecommitdiff
path: root/src/eval.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/eval.zig')
-rw-r--r--src/eval.zig247
1 files changed, 247 insertions, 0 deletions
diff --git a/src/eval.zig b/src/eval.zig
index ba7cff0..e320713 100644
--- a/src/eval.zig
+++ b/src/eval.zig
@@ -469,6 +469,253 @@ fn executeCommandWithOutput(command: Command, allocator: Allocator, output_captu
return CommandStatus{ .Code = 0 };
},
+ .Chdir => |chdir| {
+ if (chdir.path.len == 0) {
+ // No arguments - display current directory
+ const cwd = std.fs.cwd().realpathAlloc(allocator, ".") catch {
+ const error_msg = "Unable to determine current directory\n";
+ if (output_capture) |capture| {
+ try capture.write(error_msg);
+ } else {
+ print("{s}", .{error_msg});
+ }
+ return CommandStatus{ .Code = 1 };
+ };
+ defer allocator.free(cwd);
+
+ const formatted_path = try formatDosPath(allocator, cwd);
+ defer allocator.free(formatted_path);
+
+ const output = try std.fmt.allocPrint(allocator, "{s}\n", .{formatted_path});
+ defer allocator.free(output);
+
+ if (output_capture) |capture| {
+ try capture.write(output);
+ } else {
+ print("{s}", .{output});
+ }
+ return CommandStatus{ .Code = 0 };
+ } else {
+ // Change directory
+ const target_path = chdir.path;
+
+ // Handle special cases
+ if (std.mem.eql(u8, target_path, "..")) {
+ // Go to parent directory
+ std.process.changeCurDir("..") catch {
+ const error_msg = "The system cannot find the path specified.\n";
+ if (output_capture) |capture| {
+ try capture.write(error_msg);
+ } else {
+ print("{s}", .{error_msg});
+ }
+ return CommandStatus{ .Code = 1 };
+ };
+ } else if (std.mem.eql(u8, target_path, "\\") or std.mem.eql(u8, target_path, "/")) {
+ // Go to root directory - simplified to just go to "/"
+ std.process.changeCurDir("/") catch {
+ const error_msg = "The system cannot find the path specified.\n";
+ if (output_capture) |capture| {
+ try capture.write(error_msg);
+ } else {
+ print("{s}", .{error_msg});
+ }
+ return CommandStatus{ .Code = 1 };
+ };
+ } else {
+ // Regular directory change
+ // Make sure the path doesn't contain null bytes
+ for (target_path) |ch| {
+ if (ch == 0) {
+ const error_msg = "Invalid path: contains null character\n";
+ if (output_capture) |capture| {
+ try capture.write(error_msg);
+ } else {
+ print("{s}", .{error_msg});
+ }
+ return CommandStatus{ .Code = 1 };
+ }
+ }
+
+ std.process.changeCurDir(target_path) catch {
+ const error_msg = "The system cannot find the path specified.\n";
+ if (output_capture) |capture| {
+ try capture.write(error_msg);
+ } else {
+ print("{s}", .{error_msg});
+ }
+ return CommandStatus{ .Code = 1 };
+ };
+ }
+ return CommandStatus{ .Code = 0 };
+ }
+ },
+ .Copy => |copy| {
+ // Handle source file
+ const source_path = switch (copy.from) {
+ .Con => {
+ const error_msg = "Cannot copy from CON\n";
+ if (output_capture) |capture| {
+ try capture.write(error_msg);
+ } else {
+ print("{s}", .{error_msg});
+ }
+ return CommandStatus{ .Code = 1 };
+ },
+ .Lpt1, .Lpt2, .Lpt3, .Prn => {
+ const error_msg = "Cannot copy from device\n";
+ if (output_capture) |capture| {
+ try capture.write(error_msg);
+ } else {
+ print("{s}", .{error_msg});
+ }
+ return CommandStatus{ .Code = 1 };
+ },
+ .Path => |path| path,
+ };
+
+ // Handle destination
+ const dest_path = switch (copy.to) {
+ .Con => {
+ // Copy to console (display file contents)
+ const source_file = std.fs.cwd().openFile(source_path, .{}) catch |err| {
+ const error_msg = switch (err) {
+ error.FileNotFound => "File not found\n",
+ error.AccessDenied => "Access denied\n",
+ else => "Cannot access source file\n",
+ };
+ if (output_capture) |capture| {
+ try capture.write(error_msg);
+ } else {
+ print("{s}", .{error_msg});
+ }
+ return CommandStatus{ .Code = 1 };
+ };
+ defer source_file.close();
+
+ // Read and display file contents
+ var buffer: [4096]u8 = undefined;
+ while (true) {
+ const bytes_read = source_file.readAll(&buffer) catch {
+ const error_msg = "Error reading file\n";
+ if (output_capture) |capture| {
+ try capture.write(error_msg);
+ } else {
+ print("{s}", .{error_msg});
+ }
+ return CommandStatus{ .Code = 1 };
+ };
+ if (bytes_read == 0) break;
+
+ if (output_capture) |capture| {
+ try capture.write(buffer[0..bytes_read]);
+ } else {
+ print("{s}", .{buffer[0..bytes_read]});
+ }
+
+ if (bytes_read < buffer.len) break;
+ }
+
+ const msg = " 1 File(s) copied\n";
+ if (output_capture) |capture| {
+ try capture.write(msg);
+ } else {
+ print("{s}", .{msg});
+ }
+ return CommandStatus{ .Code = 0 };
+ },
+ .Lpt1, .Lpt2, .Lpt3, .Prn => {
+ const error_msg = "Cannot copy to device\n";
+ if (output_capture) |capture| {
+ try capture.write(error_msg);
+ } else {
+ print("{s}", .{error_msg});
+ }
+ return CommandStatus{ .Code = 1 };
+ },
+ .Path => |path| path,
+ };
+
+ // Regular file-to-file copy
+ std.fs.cwd().copyFile(source_path, std.fs.cwd(), dest_path, .{}) catch |err| {
+ const error_msg = switch (err) {
+ error.FileNotFound => "File not found\n",
+ error.AccessDenied => "Access denied\n",
+ error.PathAlreadyExists => "File already exists\n",
+ else => "Cannot copy file\n",
+ };
+ if (output_capture) |capture| {
+ try capture.write(error_msg);
+ } else {
+ print("{s}", .{error_msg});
+ }
+ return CommandStatus{ .Code = 1 };
+ };
+
+ const msg = " 1 File(s) copied\n";
+ if (output_capture) |capture| {
+ try capture.write(msg);
+ } else {
+ print("{s}", .{msg});
+ }
+ return CommandStatus{ .Code = 0 };
+ },
+ .Remove => |remove| {
+ const file_path = remove.path;
+
+ // Check for wildcards (basic support)
+ if (std.mem.indexOf(u8, file_path, "*") != null or std.mem.indexOf(u8, file_path, "?") != null) {
+ // Simple wildcard deletion - just show error for now
+ const error_msg = "Wildcard deletion not yet implemented\n";
+ if (output_capture) |capture| {
+ try capture.write(error_msg);
+ } else {
+ print("{s}", .{error_msg});
+ }
+ return CommandStatus{ .Code = 1 };
+ }
+
+ // Delete single file
+ std.fs.cwd().deleteFile(file_path) catch |err| {
+ const error_msg = switch (err) {
+ error.FileNotFound => "File not found\n",
+ error.AccessDenied => "Access denied\n",
+ error.IsDir => "Access denied - cannot delete directory\n",
+ else => "Cannot delete file\n",
+ };
+ if (output_capture) |capture| {
+ try capture.write(error_msg);
+ } else {
+ print("{s}", .{error_msg});
+ }
+ return CommandStatus{ .Code = 1 };
+ };
+
+ // No output for successful deletion (DOS style)
+ return CommandStatus{ .Code = 0 };
+ },
+ .Mkdir => |mkdir| {
+ const dir_path = mkdir.path;
+
+ std.fs.cwd().makeDir(dir_path) catch |err| {
+ const error_msg = switch (err) {
+ error.PathAlreadyExists => "A subdirectory or file already exists\n",
+ error.AccessDenied => "Access denied\n",
+ error.FileNotFound => "The system cannot find the path specified\n",
+ error.NotDir => "The system cannot find the path specified\n",
+ else => "Unable to create directory\n",
+ };
+ if (output_capture) |capture| {
+ try capture.write(error_msg);
+ } else {
+ print("{s}", .{error_msg});
+ }
+ return CommandStatus{ .Code = 1 };
+ };
+
+ // No output for successful creation (DOS style)
+ return CommandStatus{ .Code = 0 };
+ },
else => {
const error_msg = try std.fmt.allocPrint(allocator, "Command not implemented: {any}\n", .{builtin_cmd});
defer allocator.free(error_msg);