Enhance shell.
authorBen Pfaff <blp@cs.stanford.edu>
Wed, 6 Apr 2005 20:56:02 +0000 (20:56 +0000)
committerBen Pfaff <blp@cs.stanford.edu>
Wed, 6 Apr 2005 20:56:02 +0000 (20:56 +0000)
src/tests/userprog/shell.c

index 7cf5c607d7d431df8d6ad7cc517a27be910c3fcc..cfff4e37ea329f5fc1cef3eb0c7b2ca82b77c5c3 100644 (file)
@@ -1,9 +1,11 @@
 #include <stdio.h>
+#include <string.h>
 #include <syscall.h>
 
 int
 main (void)
 {
+  printf ("Shell starting...\n");
   for (;;) 
     {
       char command[80], *cp;
@@ -12,23 +14,59 @@ main (void)
       printf ("--");
 
       /* Read and echo command. */
-      for (cp = command; cp < command + sizeof command - 1; cp++)
+      cp = command;
+      for (;;)
         {
-          read (STDIN_FILENO, cp, 1);
-          putchar (*cp);
-          if (*cp == '\n')
-            break;
+          char c;
+          read (STDIN_FILENO, &c, 1);
+
+          switch (c) 
+            {
+            case '\n':
+              /* Done. */
+              goto got_cmd;
+
+            case '\b':
+              /* Back up cursor, overwrite character, back up again. */
+              printf ("\b \b");
+              break;
+
+            case 27:                    /* Escape. */
+            case ('U' - 'A') + 1:       /* Ctrl+U. */
+              /* Clear entire line. */
+              printf ("\n--");
+              cp = command;
+              break;
+
+            default:
+              /* Add character to line. */
+              *cp++ = c;
+              if (cp >= command + sizeof command - 1)
+                goto got_cmd;
+              break;
+            }
         }
+    got_cmd:
       *cp = '\0';
+      putchar ('\n');
       
       /* Execute command. */
-      if (cp > command) 
+      if (!strcmp (command, "exit"))
+        break;
+      else if (cp == command) 
+        {
+          /* Empty command. */
+        }
+      else
         {
           pid_t pid = exec (command);
           if (pid != PID_ERROR)
-            wait (pid);
+            printf ("\"%s\": exit code %d\n", command, wait (pid));
           else
             printf ("exec failed\n");
         }
     }
+
+  printf ("Shell exiting.");
+  return 0;
 }