From: Ben Pfaff Date: Wed, 6 Apr 2005 20:56:02 +0000 (+0000) Subject: Enhance shell. X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ca5e8cf5caf2dadcd0e36ad1ecd5e1dd67088392;p=pintos-anon Enhance shell. --- diff --git a/src/tests/userprog/shell.c b/src/tests/userprog/shell.c index 7cf5c60..cfff4e3 100644 --- a/src/tests/userprog/shell.c +++ b/src/tests/userprog/shell.c @@ -1,9 +1,11 @@ #include +#include #include 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; }