documentation update, readme reformatted
[lunaix-os.git] / lunaix-os / usr / sh / sh.c
index 4a924db7a9130f49f7e8ee80e645389a17054470..c18b5edc0c435a690e5455bb9b4edf2ee1bce64f 100644 (file)
@@ -1,6 +1,6 @@
 #include <errno.h>
-#include <lunaix/ioctl.h>
-#include <lunaix/lunaix.h>
+#include <sys/ioctl.h>
+#include <sys/wait.h>
 #include <signal.h>
 #include <stdio.h>
 #include <string.h>
@@ -46,8 +46,8 @@ strltrim_safe(char* str)
     return strcpy(str, str + l);
 }
 
-void
-parse_cmdline(char* line, char** cmd, char** arg_part)
+int
+parse_cmdline(char* line, char** args)
 {
     strrtrim(line);
     line = strltrim_safe(line);
@@ -60,12 +60,15 @@ parse_cmdline(char* line, char** cmd, char** arg_part)
         }
         l++;
     }
-    *cmd = line;
-    if (c) {
-        *arg_part = strltrim_safe(line + l);
+
+    args[0] = line;
+    if (c && l) {
+        args[1] = strltrim_safe(line + l);
     } else {
-        *arg_part = NULL;
+        args[1] = NULL;
     }
+
+    return !!l;
 }
 
 void
@@ -108,23 +111,59 @@ sigint_handle(int signum)
 }
 
 void
-sh_exec(const char* name, const char** argv)
+sh_exec(const char** argv)
 {
+    static int prev_exit;
+    const char* envp[] = { 0 };
+    char* name = argv[0];
     if (!strcmp(name, "cd")) {
-        chdir(argv[0] ? argv[0] : ".");
+        chdir(argv[1] ? argv[1] : ".");
         sh_printerr();
         return;
     }
 
+    if (!strcmp(name, "?")) {
+        printf("%d\n", prev_exit);
+        return;
+    }
+
+    char buffer[1024];
+    strcpy(buffer, "/bin/");
+    strcpy(&buffer[5], name);
+
     pid_t p;
+    int res;
     if (!(p = fork())) {
-        if (execve(name, argv, NULL)) {
+        if (execve(buffer, argv, envp)) {
             sh_printerr();
         }
         _exit(1);
     }
     setpgid(p, getpgid());
-    waitpid(p, NULL, 0);
+    waitpid(p, &res, 0);
+
+    prev_exit = WEXITSTATUS(res);
+}
+
+static char*
+sanity_filter(char* buf)
+{
+    int off = 0, i = 0;
+    char c;
+    do {
+        c = buf[i];
+        
+        if ((32 <= c && c <= 126) || !c) {
+            buf[i - off] = c;
+        }
+        else {
+            off++;
+        }
+
+        i++;
+    } while(c);
+
+    return buf;
 }
 
 void
@@ -139,11 +178,11 @@ sh_loop()
     // stdout (by default, unless user did smth) is the tty we are currently at
     ioctl(stdout, TIOCSPGRP, getpgid());
 
-    char* argv[] = {0, 0};
+    char* argv[] = {0, 0, 0};
 
     while (1) {
         getcwd(pwd, 512);
-        printf("[\033[2m%s\033[39;49m]$ ", pwd);
+        printf("%s # ", pwd);
         int sz = read(stdin, buf, 511);
 
         if (sz < 0) {
@@ -152,23 +191,19 @@ sh_loop()
         }
 
         buf[sz] = '\0';
+        sanity_filter(buf);
 
         // currently, this shell only support single argument
-        parse_cmdline(buf, &cmd, &argv[0]);
-
-        if (cmd[0] == 0) {
-            printf("\n");
-            goto cont;
+        if (!parse_cmdline(buf, argv)) {
+             continue;
         }
 
         // cmd=="exit"
-        if (*(unsigned int*)cmd == 0x74697865U) {
+        if (*(unsigned int*)argv[0] == 0x74697865U) {
             break;
         }
 
-        sh_exec(cmd, (const char**)&argv);
-    cont:
-        printf("\n");
+        sh_exec((const char**)argv);       
     }
 }