# src/npl/nipbox/nipbox.nim # --- 1. RAW IMPORTS (The Physics) --- # We talk directly to libc_shim.zig type cint = int32 csize_t = uint cptr = pointer # Standard POSIX-ish proc write(fd: cint, buf: cptr, count: csize_t): csize_t {.importc, cdecl.} proc read(fd: cint, buf: cptr, count: csize_t): csize_t {.importc, cdecl.} proc open(pathname: cstring, flags: cint): cint {.importc, cdecl.} proc close(fd: cint): cint {.importc, cdecl.} proc exit(status: cint) {.importc, cdecl.} proc list_files(buf: pointer, len: uint64): int64 {.importc, cdecl.} # Our Custom Syscalls (Defined in libc_shim) proc nexus_syscall(cmd: cint, arg: uint64): cint {.importc, cdecl.} proc nexus_yield() {.importc, cdecl.} const CMD_GPU_MATRIX = 0x100 const CMD_GPU_STATUS = 0x102 const CMD_GET_GPU_STATUS = 0x102 const CMD_SYS_EXEC = 0x400 # --- 2. MINIMAL RUNTIME (The Tools) --- # Helper: Print to Stdout (FD 1) proc print(s: string) = if s.len > 0: discard write(1, unsafeAddr s[0], csize_t(s.len)) var nl = "\n" discard write(1, unsafeAddr nl[0], 1) proc print_raw(s: string) = if s.len > 0: discard write(1, unsafeAddr s[0], csize_t(s.len)) # Helper: Read Line from Stdin (FD 0) # Returns true if line read, false if EOF var read_buffer: array[256, char] var read_pos: int = 0 var read_len: int = 0 proc my_readline(buf: var string): bool = buf.setLen(0) while true: # Buffer empty? Fill it. if read_pos >= read_len: let n = read(0, addr read_buffer[0], 256) if n <= 0: nexus_yield() continue read_len = int(n) read_pos = 0 # Process buffer while read_pos < read_len: let c = read_buffer[read_pos] read_pos += 1 # Handle Backspace if c == char(127) or c == char(8): if buf.len > 0: var seq = "\b \b" discard write(1, unsafeAddr seq[0], 3) buf.setLen(buf.len - 1) continue # Echo logic skipped (Host handles it mostly) if c == '\n' or c == '\r': return true buf.add(c) # --- 3. COMMAND LOGIC --- proc do_echo(arg: string) = print(arg) proc do_matrix(arg: string) = if arg == "on": print("[NipBox] Engaging Matrix...") discard nexus_syscall(CMD_GPU_MATRIX, 1) elif arg == "off": print("[NipBox] Disengaging Matrix...") discard nexus_syscall(CMD_GPU_MATRIX, 0) else: print("Usage: matrix on|off") proc do_cat(filename: string) = # 1. Open let fd = open(cstring(filename), 0) # O_RDONLY if fd < 0: print("cat: cannot open file") return # 2. Read Loop const BUF_SIZE = 1024 var buffer: array[BUF_SIZE, char] while true: let bytesRead = read(fd, addr buffer[0], BUF_SIZE) if bytesRead <= 0: break # Write to Stdout discard write(1, addr buffer[0], bytesRead) # 3. Close discard close(fd) print("") # Final newline proc do_ls() = var buf: array[2048, char] let n = list_files(addr buf[0], 2048) if n > 0: var s = newString(n) copyMem(addr s[0], addr buf[0], n) print(s) proc do_exec(filename: string) = if filename.len == 0: print("Usage: exec ") return print("[NipBox] Summoning " & filename & "...") let result = nexus_syscall(CMD_SYS_EXEC, cast[uint64](cstring(filename))) if result != 0: print("[NipBox] Syscall failed!") else: print("[NipBox] Syscall sent successfully") proc do_help() = print("NipBox v0.2 (Sovereign)") print("Commands: echo, cat, ls, matrix, exec, help, exit") # --- 4. MAIN LOOP --- proc main() = var inputBuffer = newStringOfCap(256) print("\n[NipBox] Interactive Shell Ready.") while true: print_raw("root@nexus:# ") if not my_readline(inputBuffer): break # EOF if inputBuffer.len == 0: continue # Simple manual parsing # Reset manual parsing var cmd = newStringOfCap(32) var arg = newStringOfCap(128) var spaceFound = false var i = 0 while i < inputBuffer.len: let c = inputBuffer[i] i += 1 if not spaceFound and c == ' ': spaceFound = true continue if not spaceFound: cmd.add(c) else: arg.add(c) if cmd == "exit": print("Exiting...") exit(0) elif cmd == "echo": do_echo(arg) elif cmd == "cat": do_cat(arg) elif cmd == "ls": do_ls() elif cmd == "matrix": do_matrix(arg) elif cmd == "exec": do_exec(arg) elif cmd == "help": do_help() else: print_raw("Unknown command: ") print(cmd) when isMainModule: main()