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/builtins.zig | |
| parent | 695b1fa1dc95d62016978a517b8e8544e486d9b2 (diff) | |
| download | shell-zig-2515de92a5ae28edda56deba40c852f36928b294.tar.gz shell-zig-2515de92a5ae28edda56deba40c852f36928b294.zip | |
codecrafters submit [skip ci]
Diffstat (limited to 'src/builtins.zig')
| -rw-r--r-- | src/builtins.zig | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/src/builtins.zig b/src/builtins.zig new file mode 100644 index 0000000..c989a35 --- /dev/null +++ b/src/builtins.zig @@ -0,0 +1,41 @@ +const std = @import("std"); +const path = @import("path.zig"); + +const BUILTINS = [_][]const u8{ "exit", "echo", "type" }; + +pub const CommandResult = enum { + continue_loop, + exit_shell, +}; + +pub fn isBuiltin(cmd_name: []const u8) bool { + for (BUILTINS) |builtin| { + if (std.mem.eql(u8, cmd_name, builtin)) return true; + } + return false; +} + +pub fn executeExit() CommandResult { + return .exit_shell; +} + +pub fn executeEcho(stdout: anytype, args: ?[]const u8) !void { + if (args) |a| { + try stdout.print("{s}\n", .{a}); + } else { + try stdout.print("\n", .{}); + } +} + +pub fn executeType(allocator: std.mem.Allocator, stdout: anytype, args: ?[]const u8) !void { + if (args) |a| { + if (isBuiltin(a)) { + try stdout.print("{s} is a shell builtin\n", .{a}); + } else if (try path.findInPath(allocator, a)) |cmd_path| { + defer allocator.free(cmd_path); + try stdout.print("{s} is {s}\n", .{ a, cmd_path }); + } else { + try stdout.print("{s}: not found\n", .{a}); + } + } +} |