#include #include #include #include #include #include #include #include #include #include #include #define MAXLINE 1024 /* max size of inputed command line*/ #define MAXARGS 128 /* max args on a command line */ /* Global variables */ extern char **environ; /* defined in libc */ char prompt[] = "myshell> "; /* command line prompt */ char *shellname; /* Function Declarations: */ int parseline(const char *cmdline, char **argv); int checkGrammar(const char*cmdline); void usage(); void app_error(char *msg); int main(int argc, char **argv) { char pName[MAXLINE]; char cmdline[MAXLINE]; int emit_prompt = 1; /* emit prompt (default) */ int pid, bg; char *argvexec[MAXARGS]; shellname = argv[0]; /* Execute the shell's read/eval loop */ while (1) { /* Read command line */ if (emit_prompt) { printf("%s", prompt); fflush(stdout); } strcpy(cmdline, ""); if ((fgets(cmdline, MAXLINE, stdin) == NULL) && ferror(stdin)) app_error("fgets error"); if (feof(stdin)) { /* End of file (ctrl-d) */ fflush(stdout); exit(0); } /* Evaluate the command line */ bg = parseline(cmdline, argvexec); if(bg == -1) { /* ignore command for whatever reason.. */ continue; } /* Insert SYSCALLS that make the shell execute programs and wait correctlly here! */ /* STOP INSERTING HERE! */ fflush(stdout); fflush(stderr); } exit(0); /* control never reaches here */ } /* * parseline - Parse the command line and build the argv array. * * * This function takes in a long string which is our command line and parses it * into tokens that can be sent directly to exec. Also calls checkGrammar(). * * Parameters: * cmdline: The command line, in the form: * * command [arguments...] [&] * * argv: will become populated with the tokens that the command line was * parsed into, much like the real argv that you get in main. * * NOTE: for this argv, you do NOT need an argc. After * the last argument in the array there will be a NULL. That is how you know * you have reached the end of the array. * * Returns: * 1: if the user has requested a BG job * 0: if the user has requested a FG job * -1: if cmdline is incorrectly formatted * */ int parseline(const char *cmdline, char **argv) { return 0; } /* Checks that & appears at the end of the line if it appears at all */ int checkGrammar(const char* cmdline) { return 1; } /* Prints the error message if the command line is not well formed. */ void usage() { printf("%s: Parse error! Invalid command line.\n", shellname); } /* Prints whatever error message is supplied to msg to standard out. */ void app_error(char *msg) { fprintf(stderr, "%s\n", msg); exit(1); }