diff --git a/examples/shell/shell.q b/examples/shell/shell.q new file mode 100644 index 0000000..e434a35 --- /dev/null +++ b/examples/shell/shell.q @@ -0,0 +1,31 @@ +import mem +import sys + +main() { + length := 256 + command := mem.alloc(length) + argv := mem.alloc(1) + envp := mem.alloc(1) + info := mem.alloc(24) + + loop { + sys.write(1, "$ ", 2) + n := sys.read(0, command, length) + + if n <= 0 { + return + } + + // TODO: Indexing by register + command[7] = '\0' + + pid := sys.fork() + + if pid == 0 { + sys.execve(command, argv, envp) + return + } + + sys.waitid(0, pid, info, 4) + } +} \ No newline at end of file diff --git a/lib/sys/sys_linux.q b/lib/sys/sys_linux.q index c28f77d..cc99c0c 100644 --- a/lib/sys/sys_linux.q +++ b/lib/sys/sys_linux.q @@ -26,6 +26,14 @@ clone(flags Int, stack Pointer) -> Int { return syscall(56, flags, stack) } +fork() -> Int { + return syscall(57) +} + +execve(path Pointer, argv Pointer, envp Pointer) -> Int { + return syscall(59, path, argv, envp) +} + exit(status Int) { syscall(60, status) } @@ -69,3 +77,7 @@ rmdir(path Pointer) -> Int { unlink(file Pointer) -> Int { return syscall(87, file) } + +waitid(type Int, id Int, info Pointer, options Int) -> Int { + return syscall(247, type, id, info, options) +}