Commit 02df7297 authored by Alexandru Dura's avatar Alexandru Dura
Browse files

Add a no prompt option (-n) to the shell

parent 8c0a3e01
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
#define MAXBUF (512) /* max length of input line. */ #define MAXBUF (512) /* max length of input line. */
#define MAX_ARG (100) /* max number of cmd line arguments. */ #define MAX_ARG (100) /* max number of cmd line arguments. */
typedef enum { typedef enum {
AMPERSAND, /* & */ AMPERSAND, /* & */
NEWLINE, /* end of line reached. */ NEWLINE, /* end of line reached. */
NORMAL, /* file name or command option. */ NORMAL, /* file name or command option. */
...@@ -113,19 +113,19 @@ int gettoken(char** outptr) ...@@ -113,19 +113,19 @@ int gettoken(char** outptr)
case '<': case '<':
type = INPUT; type = INPUT;
break; break;
case '>': case '>':
type = OUTPUT; type = OUTPUT;
break; break;
case '&': case '&':
type = AMPERSAND; type = AMPERSAND;
break; break;
case '|': case '|':
type = PIPE; type = PIPE;
break; break;
default: default:
type = NORMAL; type = NORMAL;
...@@ -134,7 +134,7 @@ int gettoken(char** outptr) ...@@ -134,7 +134,7 @@ int gettoken(char** outptr)
} }
*token++ = 0; /* null-terminate the string. */ *token++ = 0; /* null-terminate the string. */
return type; return type;
} }
...@@ -168,13 +168,13 @@ void run_program(char** argv, int argc, bool foreground, bool doing_pipe) ...@@ -168,13 +168,13 @@ void run_program(char** argv, int argc, bool foreground, bool doing_pipe)
* is true then basically you should wait but when we are * is true then basically you should wait but when we are
* running a command in a pipe such as PROG1 | PROG2 you might * running a command in a pipe such as PROG1 | PROG2 you might
* not want to wait for each of PROG1 and PROG2... * not want to wait for each of PROG1 and PROG2...
* *
* hints: * hints:
* snprintf is useful for constructing strings. * snprintf is useful for constructing strings.
* access is useful for checking wether a path refers to an * access is useful for checking wether a path refers to an
* executable program. * executable program.
* *
* *
*/ */
} }
...@@ -192,7 +192,7 @@ void parse_line(void) ...@@ -192,7 +192,7 @@ void parse_line(void)
argc = 0; argc = 0;
for (;;) { for (;;) {
foreground = true; foreground = true;
doing_pipe = false; doing_pipe = false;
...@@ -206,7 +206,7 @@ void parse_line(void) ...@@ -206,7 +206,7 @@ void parse_line(void)
case INPUT: case INPUT:
type = gettoken(&argv[argc]); type = gettoken(&argv[argc]);
if (type != NORMAL) { if (type != NORMAL) {
error("expected file name: but found %s", error("expected file name: but found %s",
argv[argc]); argv[argc]);
return; return;
} }
...@@ -221,7 +221,7 @@ void parse_line(void) ...@@ -221,7 +221,7 @@ void parse_line(void)
case OUTPUT: case OUTPUT:
type = gettoken(&argv[argc]); type = gettoken(&argv[argc]);
if (type != NORMAL) { if (type != NORMAL) {
error("expected file name: but found %s", error("expected file name: but found %s",
argv[argc]); argv[argc]);
return; return;
} }
...@@ -247,7 +247,7 @@ void parse_line(void) ...@@ -247,7 +247,7 @@ void parse_line(void)
if (argc == 0) if (argc == 0)
return; return;
argv[argc] = NULL; argv[argc] = NULL;
run_program(argv, argc, foreground, doing_pipe); run_program(argv, argc, foreground, doing_pipe);
...@@ -275,8 +275,8 @@ static void init_search_path(void) ...@@ -275,8 +275,8 @@ static void init_search_path(void)
path = getenv("PATH"); path = getenv("PATH");
/* path may look like "/bin:/usr/bin:/usr/local/bin" /* path may look like "/bin:/usr/bin:/usr/local/bin"
* and this function makes a list with strings * and this function makes a list with strings
* "/bin" "usr/bin" "usr/local/bin" * "/bin" "usr/bin" "usr/local/bin"
* *
*/ */
...@@ -320,7 +320,7 @@ static void init_search_path(void) ...@@ -320,7 +320,7 @@ static void init_search_path(void)
#if 0 #if 0
do { do {
printf("%s\n", (char*)p->data); printf("%s\n", (char*)p->data);
p = p->succ; p = p->succ;
} while (p != path_dir_list); } while (p != path_dir_list);
#endif #endif
} }
...@@ -328,11 +328,15 @@ static void init_search_path(void) ...@@ -328,11 +328,15 @@ static void init_search_path(void)
/* main: main program of simple shell. */ /* main: main program of simple shell. */
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
char* prompt = (argc >= 2 &&
!strncmp(argv[1], "-n", 3))
? "" : "% ";
progname = argv[0]; progname = argv[0];
init_search_path(); init_search_path();
while (fetch_line("% ") != EOF) while (fetch_line(prompt) != EOF)
parse_line(); parse_line();
return 0; return 0;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment