diff options
| author | Lucas Faria Mendes <lucas.oliveira1676@etec.sp.gov.br> | 2025-12-05 05:53:37 +0000 |
|---|---|---|
| committer | Lucas Faria Mendes <lucas.oliveira1676@etec.sp.gov.br> | 2025-12-05 05:53:37 +0000 |
| commit | 2515de92a5ae28edda56deba40c852f36928b294 (patch) | |
| tree | cc048d51ba7dd6fc78376f02e1c12af1161c7d38 /src/shell.zig | |
| parent | 695b1fa1dc95d62016978a517b8e8544e486d9b2 (diff) | |
| download | shell-zig-2515de92a5ae28edda56deba40c852f36928b294.tar.gz shell-zig-2515de92a5ae28edda56deba40c852f36928b294.zip | |
codecrafters submit [skip ci]
Diffstat (limited to 'src/shell.zig')
| -rw-r--r-- | src/shell.zig | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/shell.zig b/src/shell.zig new file mode 100644 index 0000000..c5aded1 --- /dev/null +++ b/src/shell.zig @@ -0,0 +1,37 @@ +const std = @import("std"); +const parser = @import("parser.zig"); +const builtins = @import("builtins.zig"); +const path = @import("path.zig"); +const executor = @import("executor.zig"); + +pub fn executeCommand( + allocator: std.mem.Allocator, + stdout: anytype, + cmd_name: []const u8, + args: ?[]const u8, +) !builtins.CommandResult { + if (std.mem.eql(u8, cmd_name, "exit")) return builtins.executeExit(); + + if (std.mem.eql(u8, cmd_name, "echo")) { + try builtins.executeEcho(stdout, args); + return .continue_loop; + } + + if (std.mem.eql(u8, cmd_name, "type")) { + try builtins.executeType(allocator, stdout, args); + return .continue_loop; + } + + if (try path.findInPath(allocator, cmd_name)) |program_path| { + defer allocator.free(program_path); + + const argv = try parser.parseArgs(allocator, cmd_name, args); + defer allocator.free(argv); + + try executor.runExternalProgram(allocator, program_path, argv); + return .continue_loop; + } + + try stdout.print("{s}: command not found\n", .{cmd_name}); + return .continue_loop; +} |