/* ========= BEGINNING OF MAIN ================= */ int main(int argc, char **argv){ int is_pipe_command; /* Bool: 1 if command contains pipes, 0 otherwise */ char *shell_prompt; /* The shell prompt */ char buf[1000]; /* For grabbing shell prompt from command line */ char **arg_array; /* Arguments to command to be executed */ IS mis; /* My input struct */ Command *com; /* Arbitrary command */ Pipe_Command *pip_com; /* Arbitrary pipe command */ /* Get shell prompt */ if (argc == 1) shell_prompt = strdup("PowerJ >> "); else if (argc == 2){ sscanf(argv[1], "%s", buf); if (strcmp(buf, "-") == 0) shell_prompt = NULL; else shell_prompt = strdup(buf); } else{ printf("Usage: ./jsh prompt\n"); exit(1); } /* Initialize input struct and print shell prompt */ mis = new_inputstruct(NULL); if (shell_prompt != NULL) printf("%s", shell_prompt); /* Wait on user input */ while (get_line(mis) >= 0){ if (mis->NF > 0){ /* Use fields library to parse command */ is_pipe_command = check_for_pipes(mis); if (is_pipe_command){ pip_com = create_pip_com(mis); exec_pip_com(pip_com); } else{ com = grab_com(mis); execute_com(com); } } if (shell_prompt != NULL) printf("%s", shell_prompt); } return 0; }
/* * The main shell function */ main() { int i; char **args; int result; int block; int output; int input; int append; int child_id; int status; char *output_filename; char *input_filename; // Set up the signal handler sigset(SIGCHLD, sig_handler); // memset(&sa, 0, sizeof(sa)); // sa.sa_handler = delete_zombies; // if(sigaction(SIGCHLD, &sa, 0)) { // perror("sigaction"); // return 1; // } // Loop forever while(1) { // Print out the prompt and get the input printf("prompt->"); args = getaline(); // No input, continue if(args[0] == NULL) continue; // Check for internal shell commands, such as exit if(internal_command(args)) continue; // Check for an ampersand int block = should_block(args); if(check_for_pipes(args)) { int in = 0; char **tmp_args = args; char **ptr; // loop over args, set each | to NULL for(ptr = args; *ptr != NULL; ptr++) { //printf("tmpargs[0]: %s [1]: %s [2]: %s\n", tmp_args[0], tmp_args[1], tmp_args[2]); if(strcmp(*ptr, "|") == 0) { free(*ptr); *ptr = NULL; // do stuff with tmp_args as your new "args" int pipefd[2]; pipe(pipefd); child_id = do_command(tmp_args, in, pipefd[1], 1, block); close(pipefd[1]); if(child_id < 0) { printf("syntax error\n"); continue; } in = pipefd[0]; tmp_args = ptr + 1; } } //printf("tmpargs[0]: %s [1]: %s\n", tmp_args[0], tmp_args[1]); child_id = do_command(tmp_args, in, 1, 1, block); if(child_id < 0) { printf("syntax error\n"); continue; } } else { child_id = do_command(args, 0, 0, 0, block); if(child_id < 0) { printf("syntax error\n"); continue; } } // Wait for the child process to complete, if necessary if(block) { printf("Waiting for child, pid = %d\n", child_id); result = waitpid(child_id, &status, 0); } } }