feat: a file system mapping for pci devices
[lunaix-os.git] / lunaix-os / kernel / demos / simple_sh.c
index 036550a4fe0ab6ccb7363e83bbdde3cc6c79f3f8..4ea9711a925438232e4fbc14386f13ee3b6c6554 100644 (file)
@@ -8,6 +8,7 @@
 #include <ulibc/stdio.h>
 
 char pwd[512];
+char cat_buf[1024];
 
 /*
     Simple shell - (actually this is not even a shell)
@@ -55,6 +56,9 @@ sh_printerr()
         case ENOMEM:
             printf("Error: Out of memory\n");
             break;
+        case EISDIR:
+            printf("Error: This is a directory\n");
+            break;
         default:
             printf("Error: Fail to open (%d)\n", errno);
             break;
@@ -67,9 +71,11 @@ sh_main()
     char buf[512];
     char *cmd, *argpart;
 
+    printf("\n Simple shell. Use <PG_UP> or <PG_DOWN> to scroll.\n\n");
+
     while (1) {
         getcwd(pwd, 512);
-        printf("%s> ", pwd);
+        printf("%s$ ", pwd);
         size_t sz = read(stdin, buf, 512);
         if (sz < 0) {
             printf("fail to read user input (%d)\n", geterrno());
@@ -77,6 +83,9 @@ sh_main()
         }
         buf[sz - 1] = '\0';
         parse_cmdline(buf, &cmd, &argpart);
+        if (cmd[0] == 0) {
+            goto cont;
+        }
         if (streq(cmd, "cd")) {
             if (chdir(argpart) < 0) {
                 sh_printerr();
@@ -97,9 +106,26 @@ sh_main()
 
                 close(fd);
             }
+        } else if (streq(cmd, "cat")) {
+            int fd = open(argpart, 0);
+            if (fd < 0) {
+                sh_printerr();
+            } else {
+                int sz;
+                while ((sz = read(fd, cat_buf, 1024)) == 1024) {
+                    write(stdout, cat_buf, 1024);
+                }
+                if (sz < 0) {
+                    sh_printerr();
+                } else {
+                    write(stdout, cat_buf, sz);
+                }
+                close(fd);
+            }
         } else {
             printf("unknow command");
         }
+    cont:
         printf("\n");
     }
 }
\ No newline at end of file