From a0ea659446749644e6efe4a4ac21b65ccd317d4c Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Wed, 6 Apr 2005 20:56:02 +0000 Subject: [PATCH] Enhance shell. --- src/tests/userprog/shell.c | 52 +++++++++++++++++++++++++++++++++----- 1 file changed, 45 insertions(+), 7 deletions(-) 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; } -- 2.30.2