summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main.zig44
1 files changed, 42 insertions, 2 deletions
diff --git a/src/main.zig b/src/main.zig
index 30aacca..1511740 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -7,16 +7,56 @@ const stdin = &stdin_reader.interface;
var stdout_writer = std.fs.File.stdout().writerStreaming(&.{});
const stdout = &stdout_writer.interface;
+const CommandResult = enum {
+ continue_loop,
+ exit_shell,
+};
+
+fn parseCommand(input: []const u8) struct { name: []const u8, args: ?[]const u8 } {
+ const space_pos = std.mem.indexOfScalar(u8, input, ' ');
+ if (space_pos) |pos| {
+ return .{
+ .name = input[0..pos],
+ .args = input[pos + 1 ..],
+ };
+ }
+ return .{
+ .name = input,
+ .args = null,
+ };
+}
+
+fn executeCommand(cmd_name: []const u8, args: ?[]const u8) !CommandResult {
+ if (std.mem.eql(u8, cmd_name, "exit")) {
+ return .exit_shell;
+ }
+
+ if (std.mem.eql(u8, cmd_name, "echo")) {
+ if (args) |a| {
+ try stdout.print("{s}\n", .{a});
+ } else {
+ try stdout.print("\n", .{});
+ }
+ return .continue_loop;
+ }
+
+ // Command not found
+ try stdout.print("{s}: command not found\n", .{cmd_name});
+ return .continue_loop;
+}
+
pub fn main() !void {
while (true) {
try stdout.print("$ ", .{});
const command = try stdin.takeDelimiter('\n');
if (command) |cmd| {
- if (std.mem.eql(u8, cmd, "exit")) {
+ const parsed = parseCommand(cmd);
+ const result = try executeCommand(parsed.name, parsed.args);
+
+ if (result == .exit_shell) {
break;
}
- try stdout.print("{s}: command not found\n", .{cmd});
} else {
break;
}