diff options
Diffstat (limited to 'system/ash/ash.zig')
| -rw-r--r-- | system/ash/ash.zig | 110 |
1 files changed, 25 insertions, 85 deletions
diff --git a/system/ash/ash.zig b/system/ash/ash.zig index 013de87..5149ab8 100644 --- a/system/ash/ash.zig +++ b/system/ash/ash.zig @@ -1,6 +1,11 @@ //! Ash - Akiba Shell -const akiba = @import("akiba"); +const colors = @import("colors"); +const format = @import("format"); +const io = @import("io"); +const kata = @import("kata"); +const string = @import("string"); +const sys = @import("sys"); const MAX_INPUT = 256; const MAX_ARGS = 16; @@ -14,41 +19,35 @@ var letter_buffer: [256]u8 = undefined; var arg_ptrs: [MAX_ARGS][*:0]const u8 = undefined; var arg_storage: [MAX_ARGS][128]u8 = undefined; -const Color = struct { - const white: u32 = 0x00FFFFFF; - const green: u32 = 0x0088FF88; - const red: u32 = 0x00FF4444; -}; - export fn main(pc: u32, pv: [*]const [*:0]const u8) u8 { _ = pc; _ = pv; while (true) { - const location = akiba.io.getlocation(&location_buffer) catch "/"; - const stack_name = get_stack_name(location); + const location = io.getlocation(&location_buffer) catch "/"; + const stack_name = string.getStackName(location); - _ = akiba.io.mark(akiba.io.stream, "(", Color.white) catch {}; - _ = akiba.io.mark(akiba.io.stream, stack_name, Color.green) catch {}; - _ = akiba.io.mark(akiba.io.stream, ") >>> ", Color.white) catch {}; + format.print("("); + format.color(stack_name, colors.green); + format.print(") >>> "); input_len = 0; while (true) { - const char = akiba.io.getchar() catch continue; + const char = io.getchar() catch continue; if (char == '\n') { - _ = akiba.io.mark(akiba.io.stream, "\n", Color.white) catch {}; + format.print("\n"); break; } else if (char == '\x08') { if (input_len > 0) { input_len -= 1; - _ = akiba.io.mark(akiba.io.stream, "\x08", Color.white) catch {}; + format.print("\x08"); } } else if (char >= 32 and char <= 126 and input_len < MAX_INPUT - 1) { input_buffer[input_len] = char; input_len += 1; char_buffer[0] = char; - _ = akiba.io.mark(akiba.io.stream, &char_buffer, Color.white) catch {}; + format.print(&char_buffer); } } @@ -87,99 +86,40 @@ fn execute_command(input: []const u8) void { if (argc == 0) return; - const cmd = arg_storage[0][0..find_null(&arg_storage[0])]; + const cmd = arg_storage[0][0..string.findNull(&arg_storage[0])]; var path_buf: [512]u8 = undefined; + const path = string.concat3(&path_buf, "/binaries/", cmd, ".akiba"); - const path = build_path(&path_buf, "/binaries/", cmd, ".akiba"); if (try_spawn_with_args(path, arg_ptrs[0..argc])) { process_letters(); return; } - _ = akiba.io.mark(akiba.io.stream, "ash: binary not found: ", Color.red) catch {}; - _ = akiba.io.mark(akiba.io.stream, cmd, Color.white) catch {}; - _ = akiba.io.mark(akiba.io.stream, ".\n", Color.white) catch {}; + format.color("ash: binary not found: ", colors.red); + format.print(cmd); + format.println("."); } fn try_spawn_with_args(path: []const u8, argv: [][*:0]const u8) bool { - const pid = akiba.kata.spawn_with_args(path, argv) catch { + const pid = kata.spawnWithArgs(path, argv) catch { return false; }; - _ = akiba.kata.wait(pid) catch {}; + _ = kata.wait(pid) catch {}; return true; } fn process_letters() void { - const letter_type = akiba.io.read_letter(&letter_buffer) catch return; + const letter_type = io.readLetter(&letter_buffer) catch return; - if (letter_type == akiba.io.Letter.NAVIGATE) { + if (letter_type == io.Letter.NAVIGATE) { var len: usize = 0; while (len < letter_buffer.len and letter_buffer[len] != 0) : (len += 1) {} if (len > 0) { - akiba.io.setlocation(letter_buffer[0..len]) catch {}; + io.setlocation(letter_buffer[0..len]) catch {}; } } } - -fn find_null(buf: []const u8) usize { - var len: usize = 0; - while (len < buf.len and buf[len] != 0) : (len += 1) {} - return len; -} - -fn build_path(buf: []u8, prefix: []const u8, name: []const u8, suffix: []const u8) []const u8 { - var pos: usize = 0; - for (prefix) |c| { - if (pos >= buf.len) break; - buf[pos] = c; - pos += 1; - } - for (name) |c| { - if (pos >= buf.len) break; - buf[pos] = c; - pos += 1; - } - for (suffix) |c| { - if (pos >= buf.len) break; - buf[pos] = c; - pos += 1; - } - return buf[0..pos]; -} - -fn get_stack_name(location: []const u8) []const u8 { - // Root case - if (location.len == 0 or (location.len == 1 and location[0] == '/')) { - return "/"; - } - - // Find last '/' and return everything after it - var last_slash: usize = 0; - for (location, 0..) |c, i| { - if (c == '/') { - last_slash = i; - } - } - - // If path ends with '/', look for second-to-last slash - if (last_slash == location.len - 1 and location.len > 1) { - var i: usize = location.len - 2; - while (i > 0) : (i -= 1) { - if (location[i] == '/') { - return location[i + 1 .. location.len - 1]; - } - } - return location[1 .. location.len - 1]; - } - - // Return part after last slash - if (last_slash + 1 < location.len) { - return location[last_slash + 1 ..]; - } - - return location; -} |
