1 /* 2 * main - The shell's main routine 3 */ 4 int main(int argc, char **argv) 5 { 6 char c; 7 char cmdline[MAXLINE]; 8 int emit_prompt = 1; /* emit prompt (default) */ 9 10 /* Redirect stderr to stdout (so that driver will get all output 11 * on the pipe connected to stdout) */ 12 dup2(1, 2); 13 14 /* Parse the command line */ 15 while ((c = getopt(argc, argv, "hvp")) != EOF) { 16 switch (c) { 17 case 'h': /* print help message */ 18 usage(); 19 break; 20 case 'v': /* emit additional diagnostic info */ 21 verbose = 1; 22 break; 23 case 'p': /* don't print a prompt */ 24 emit_prompt = 0; /* handy for automatic testing */ 25 break; 26 default: 27 usage(); 28 } 29 } 30 31 /* Install the signal handlers */ 32 33 /* This is the signal handler you will need to implement for the shell */ 34 Signal(SIGCHLD, sigchld_handler); /* Terminated or stopped child */ 35 36 /* The signal handlers for SIGINT and SIGTSTP should just return */ 37 Signal(SIGINT, sigint_handler); /* ctrl-c */ 38 Signal(SIGTSTP, sigtstp_handler); /* ctrl-z */ 39 40 41 /* This one provides a clean way to kill the shell. Do not change this! */ 42 Signal(SIGQUIT, sigquit_handler); 43 44 /* Your shell will ignore these:*/ 45 Signal(SIGTTOU, SIG_IGN); 46 Signal(SIGTTIN, SIG_IGN); 47 48 49 50 /* Initialize the job list */ 51 initjobs(jobs); 52 53 /* Execute the shell's read/eval loop */ 54 while (1) { 55 56 /* Read command line */ 57 if (emit_prompt) { 58 printf("%s", prompt); 59 fflush(stdout); 60 } 61 if ((fgets(cmdline, MAXLINE, stdin) == NULL) && ferror(stdin)) { 62 app_error("fgets error"); 63 } 64 if (feof(stdin)) { /* End of file (ctrl-d) */ 65 fflush(stdout); 66 exit(0); 67 } 68 69 /* Evaluate the command line */ 70 eval(cmdline); 71 fflush(stdout); 72 fflush(stdout); 73 } 74 75 exit(0); /* control never reaches here */ 76 }