int main(int argc, char *argv[]) { int ret = EXIT_SUCCESS; char cmdline[MAXLINE]; /* command line */ while (1) { /* issue prompt and read command line */ printf("%s%% ", argv[0]); fgets(cmdline, MAXLINE, stdin); /* cmdline includes trailing newline */ if (feof(stdin)) /* end of file */ { break; } ret = eval_line(cmdline); } return ret; }
int main(int argc, char* argv[]) { FILE* command_file = stdin; int quiet = 0; // Check for '-q' option: be quiet (print no prompts) if (argc > 1 && strcmp(argv[1], "-q") == 0) { quiet = 1; --argc, ++argv; } // Check for filename option: read commands from file if (argc > 1) { command_file = fopen(argv[1], "rb"); if (!command_file) { perror(argv[1]); exit(1); } } // - Put the shell into the foreground // - Ignore the SIGTTOU signal, which is sent when the shell is put back // into the foreground set_foreground(0); handle_signal(SIGTTOU, SIG_IGN); char buf[BUFSIZ]; int bufpos = 0; int needprompt = 1; while (!feof(command_file)) { // Print the prompt at the beginning of the line if (needprompt && !quiet) { printf("sh61[%d]$ ", getpid()); fflush(stdout); needprompt = 0; } // Read a string, checking for error or EOF if (fgets(&buf[bufpos], BUFSIZ - bufpos, command_file) == NULL) { if (ferror(command_file) && errno == EINTR) { // ignore EINTR errors clearerr(command_file); buf[bufpos] = 0; } else { if (ferror(command_file)) perror("sh61"); break; } } // If a complete command line has been provided, run it bufpos = strlen(buf); if (bufpos == BUFSIZ - 1 || (bufpos > 0 && buf[bufpos - 1] == '\n')) { eval_line(buf); bufpos = 0; needprompt = 1; } // Handle zombie processes and/or interrupt requests // Your code here! } return 0; }
int main(int argc, char *argv[]) { prog = argv[0]; int ret = EXIT_SUCCESS; char cmdline[MAXLINE]; /* command line */ // File pointer: extern FILE *f_p; extern char *filename; extern char *startfile; extern char *prompt; extern int pr7_debug; pr7_debug = 0; // Getopt declarations: extern int optind; extern char *optarg; int i_flag = 0; int e_flag = 0; int s_flag = 0; extern int verbose; // This was initialized to 0 in cmpsc311.c int ch; // Getopt switch statement to locate command line options: optind = 1; while((ch = (getopt(argc, argv, ":hvdies:"))) != -1) { switch(ch) { case 'h': usage(ret); break; case 'v': verbose++; break; case 'd': pr7_debug++; break; case 'i': i_flag++; break; case 'e': e_flag++; break; case 's': s_flag++; startfile = Strdup(optarg); break; default: usage(EXIT_FAILURE); break; } } // start the signal handlers: install_signal_handler(SIGINT, SIGINT_handler); install_signal_handler(SIGCHLD, SIGCHLD_handler); // create the process table: process_table = allocate_table(); if(s_flag > 0) { f_p = fopen(startfile, "r"); if(f_p == NULL) { fprintf(stderr, "%s: Cannot open file %s: %s\n", prog, filename, strerror(errno)); exit(EXIT_FAILURE); } } else // open the default init file, if it exists { f_p = fopen("pr7.init", "r"); if(f_p == NULL) { fprintf(stderr, "%s: Cannot open file %s: %s\n", prog, filename, strerror(errno)); exit(EXIT_FAILURE); } } // STARTFILE LOOP: while(fgets(cmdline, MAXLINE, f_p) != NULL) { /* issue prompt and read command line */ if (feof(stdin)) /* end of file */ { break; } if((ret = eval_line(cmdline, e_flag)) == EXIT_FAILURE) fprintf(stderr, "%s: failure in eval_line, on line %s. %s\n", prog, cmdline, strerror(errno)); } fclose(f_p); // Now read from either a file or stdin // If a file is given, open it here if(argv[optind] != NULL) { // there is a filename given, so read from that only. f_p = fopen(argv[optind], "r"); if(f_p == NULL) { fprintf(stderr, "%s: Cannot open file %s: %s\n", prog, filename, strerror(errno)); exit(EXIT_FAILURE); } filename = argv[optind]; } // Else, no file was given, so just read from stdin. else { f_p = stdin; filename = "[stdin]"; } // MAIN PROGRAM LOOP: prompt = argv[0]; if(f_p == stdin && i_flag > 0) printf("%s%% ", prompt); while(fgets(cmdline, MAXLINE, f_p) != NULL) { /* issue prompt and read command line */ if (feof(stdin)) /* end of file */ { break; } if((ret = eval_line(cmdline, e_flag)) == EXIT_FAILURE) fprintf(stderr, "%s: failure in eval_line, on line %s. %s\n", prog, cmdline, strerror(errno)); if(f_p == stdin && i_flag > 0) printf("%s%% ", prompt); } free(startfile); return ret; }
int main(int argc, char *argv[]) { int ret = EXIT_SUCCESS; int ch; char cmdline[MAX_LINE]; /* command line */ int interactive = 0; char* infile_name = NULL; char* program_name = argv[0]; install_signal_handler(SIGINT, INThandler); FILE* infile = NULL; /* parse the command line options */ while ((ch = getopt(argc, argv, "hvids:")) != -1) { switch (ch) { case 'h': usage(argv[0], EXIT_SUCCESS); break; case 'i': /* interactive mode */ interactive = 1; break; case 's': infile_name = strdup(optarg); interactive = 0; break; case 'd': pr7_debug = 1; break; case 'v': verbose = 1; break; case '?': printf("%s: invalid option '%c'\n", argv[0], optopt); usage(argv[0], EXIT_FAILURE); break; case ':': printf("%s: invalid option '%c' (missing argument)\n", argv[0], optopt); usage(argv[0], EXIT_FAILURE); break; default: usage(argv[0], EXIT_FAILURE); break; } } program_name = argv[0]; if (interactive || (optind >= argc && infile_name == NULL)) { infile = stdin; infile_name = "[stdin]"; } else { if (NULL == infile_name) { infile_name = strdup(argv[optind]); } infile = fopen(infile_name, "r"); if (NULL == infile) { fprintf(stderr, "%s: cannot open file %s: %s\n", program_name, infile_name, strerror(errno)); exit(EXIT_FAILURE); } } printf("%s\n", infile_name); line_num = 0; while (1) { char* succ; ++line_num; /* issue prompt and read command line */ if (infile == stdin) printf("%s %d%% ", program_name, line_num); succ = fgets(cmdline, MAX_LINE, infile); cleanup_terminated_children(); if (succ != NULL) { ret = eval_line(cmdline); } if (feof(infile)) /* end of file */ { break; } } return ret; }
int main(int argc, char *argv[]) { int ret = EXIT_SUCCESS; char cmdline[MAXLINE]; /* command line */ int ch; int interactive = 0; /* -i, give a prompt? */ int echo = 0; /* -e, echo the command? */ char *startup = "pr7.init"; /* -s, startup file */ int s_flag = 0; FILE *infile = stdin; while ((ch = getopt(argc, argv, ":hvies:")) != -1) { switch (ch) { case 'h': usage(argv[0], EXIT_SUCCESS); break; case 'v': verbose = 1; break; case 'i': interactive = 1; break; case 'e': echo = 1; break; case 's': startup = optarg; s_flag = 1; break; case '?': printf("%s: invalid option '%c'\n", argv[0], optopt); usage(argv[0], EXIT_FAILURE); break; case ':': printf("%s: invalid option '%c' (missing argument)\n", argv[0], optopt); usage(argv[0], EXIT_FAILURE); break; default: usage(argv[0], EXIT_FAILURE); break; } } while (1) { if (interactive) /* issue prompt */ { printf("%s%% ", argv[0]); } if (fgets(cmdline, sizeof(cmdline), infile) == NULL) { break; } if (feof(infile)) /* end of file */ { break; } ret = eval_line(cmdline); } return ret; }
int main(int argc, char *argv[]) { prog = argv[0]; int ret = EXIT_SUCCESS; char cmdline[MAXLINE]; /* command line */ // File pointer: extern FILE *f_p; extern char *filename; extern char *startfile; extern char *prompt; // Getopt section: extern int optind; extern char *optarg; int v_flag = 0; int i_flag = 0; int e_flag = 0; int s_flag = 0; int ch; optind = 1; while((ch = (getopt(argc, argv, ":hvies:"))) != -1) { switch(ch) { case 'h': usage(ret); break; case 'v': v_flag++; break; case 'i': i_flag++; break; case 'e': e_flag++; break; case 's': s_flag++; startfile = Strdup(optarg); // Make sure to call free on startfile now break; default: usage(EXIT_FAILURE); break; } } install_signal_handler(SIGINT, SIGINT_handler); if(s_flag > 0) { f_p = fopen(startfile, "r"); if(f_p == NULL) { fprintf(stderr, "%s: Cannot open file %s: %s\n", prog, filename, strerror(errno)); exit(EXIT_FAILURE); } } // STARTFILE LOOP: if(s_flag > 0) { while(fgets(cmdline, MAXLINE, f_p) != NULL) { /* issue prompt and read command line */ if (feof(stdin)) /* end of file */ { break; } if((ret = eval_line(cmdline, e_flag)) == EXIT_FAILURE) fprintf(stderr, "%s: failure in eval_line, on line %s. %s\n", prog, cmdline, strerror(errno)); } } if(argv[optind] != NULL) { // there is a filename given, so read from that only. f_p = fopen(argv[optind], "r"); if(f_p == NULL) { fprintf(stderr, "%s: Cannot open file %s: %s\n", prog, filename, strerror(errno)); exit(EXIT_FAILURE); } filename = argv[optind]; // ATTN: watch for leaks! might need to strdup. } else { f_p = stdin; filename = "[stdin]"; } // MAIN PROGRAM LOOP: prompt = argv[0]; if(f_p == stdin && i_flag > 0) printf("%s%% ", prompt); while(fgets(cmdline, MAXLINE, f_p) != NULL) { /* issue prompt and read command line */ if (feof(stdin)) /* end of file */ { break; } if((ret = eval_line(cmdline, e_flag)) == EXIT_FAILURE) fprintf(stderr, "%s: failure in eval_line, on line %s. %s\n", prog, cmdline, strerror(errno)); if(f_p == stdin && i_flag > 0) printf("%s%% ", prompt); } free(startfile); return ret; }