/* * Prepares i/o redirection for a single * i/o redirect. */ void prepare_one_io(char* command, int fd[]) { int n; int status = -1; char* tokens[CMD_MAX]; char copy[CMD_MAX]; //A copy of the command char* io[CMD_MAX]; //Make a copy of the command //It will be needed if the first condition fails. strcpy(copy,command); //Check to see if there is a '<' n = make_tokenlist(command,tokens, "<"); if(n < 1) { perror("Cannot tokenize"); exit(EXIT_FAILURE); } //If there is more than one token, then a '<' //is present. Parse the token. if(n>1) { status = 0; prepare_io(tokens[1], io); } //In this case, there must be a '>' present else { //Check to make sure this is true n = make_tokenlist(copy,tokens, ">"); if(n < 1) { perror("Cannot tokenize"); exit(EXIT_FAILURE); } if(n>1) { status = 1; prepare_io(tokens[1], io); } } //If there is a '<' //Redirect the input and execute command if (status == 0) { redir_in(io[0],fd); exec_pipes(tokens[0]); } //If there is a '>' //Redirect the output and execute command else if (status == 1) { redir_out(io[0],fd); exec_pipes(tokens[0]); } }
void main(void) { float x = 0.1; int policy; struct sched_param param; policy = SCHED_RR; param.sched_priority = sched_get_priority_max(policy); if (sched_setscheduler(0, policy, ¶m)) { perror("Error setting scheduler policy"); exit(EXIT_FAILURE); } char input_line[MAX], *tokens[CMD_MAX]; int i, n; printf("hanan> "); if (fgets(input_line, MAX, stdin) != NULL) n = make_tokenlist(input_line, tokens); else printf("huh?\n"); for (i = 0; i < n; i++) printf("extracted token is %s\n", tokens[i]); }
/* * Prepare i/o commands for execution * (ie. tokenize them) */ void prepare_io(char* command, char** output) { int n; n = make_tokenlist(command,output, " \t\n"); if(n < 1) { perror("Cannot tokenize"); exit(EXIT_FAILURE); } }
/* * Executes a command within a pipe */ void exec_pipes(char* command) { int n; char* tokens[CMD_MAX]; //Make a token list based on delimiters. n = make_tokenlist(command,tokens, " \t\n"); if(n < 1) { perror("Cannot tokenize"); exit(EXIT_FAILURE); } //Execute the command. execute(tokens, n); }
void shell_loop(char *prompt) { // Initiate parameters pid_t pid; char *p = strcat(prompt, "> "); int state = 1; int status = 0; char cmd[40]; char input_line[MAX], *tokens[CMD_MAX]; int i, n; // Initiate command history Hist hist[10]; int length = 10; int index = 0; // Initiate shell environment while (state) { // Print line prompt printf("%s", p); // Extract tokens from user input if (fgets(input_line, MAX, stdin) != NULL) n = make_tokenlist(input_line, tokens, cmd); // Run specified command if (strcmp(tokens[0], "exit") == 0) { // Exit to main() return; } else if (strcmp(tokens[0], "history") == 0) { // Print command history print_history(hist, &index); } else { // Execute command from tokens strcpy(hist[index % 10].cmd, cmd); index++; run_command(tokens, n); } // end of if strcpy(cmd, ""); } // end of while }
/* main */ int main() { bool exit_flag = false; int oldest_history = HISTORY_SIZE - 1, status; char *username, input[MAX_INPUT_SIZE], history[HISTORY_SIZE][MAX_INPUT_SIZE] = {'\0'}, *tokens[CMD_MAX]; signal(SIGINT, handle_sigint); //shell loop while(!exit_flag) { //print the prompt and get the input printf("%s> " , get_username()); fgets(input, MAX_INPUT_SIZE, stdin); format_input(input); //built-in exit function if(!strcmp(input, "exit")) exit_flag = true; //built-in history function else if(!strcmp(input, "history")) { int i; for(i = oldest_history+1; i!=oldest_history; i = (i+1)%HISTORY_SIZE) { if(history[i][0] != '\0') printf("%s\n", history[i]); } } //non-built-in function else if(strcmp(input, "")) { int token_index, num_tokens; pid_t pid; char *tokens[CMD_MAX]; //get the token list and size num_tokens = make_tokenlist(input, tokens); //fork a child process pid = fork(); //parent: wait for child to finish if(pid > 0) { pid = wait(&status); } //child: execute command if(pid == 0) { //redirect output token_index = contains_token(tokens, num_tokens, ">"); if(token_index >= 0 && token_index+1 < num_tokens) { int fd_o = open(tokens[token_index+1], O_CREAT|O_TRUNC|O_WRONLY, 0644); if(dup2(fd_o, STDOUT_FILENO)<0) { perror("dup2 error"); exit(EXIT_FAILURE); } close(fd_o); //remove ">" and the output file from the list of arguments tokens[token_index ] = '\0'; tokens[token_index+1] = '\0'; } //redirect input token_index = contains_token(tokens, num_tokens, "<"); if(token_index >= 0 && token_index+1 < num_tokens) { int fd_i = open(tokens[token_index+1], O_RDONLY); dup2(fd_i, STDIN_FILENO); close(fd_i); //remove "<" and the input file from the list of arguments tokens[token_index ] = '\0'; tokens[token_index+1] = '\0'; } //handle pipe int num_pipes = count_pipes(tokens, num_tokens); if(num_pipes > 0) { //get the indices of the pipes to extract the commands int pipe_indices[CMD_MAX]; get_pipe_indices(tokens, num_tokens, pipe_indices); //extract all the commands char *commands[CMD_MAX][CMD_MAX]; for(int i=0; i<=num_pipes; i++) { extract_tokens(tokens, commands[i], pipe_indices[i]+1, pipe_indices[i+1]); } //connect and run all the pipe commands int fds[2], in = STDIN_FILENO; for(int i=0; i<num_pipes; i++) { pipe(fds); execute_piped_process(in, fds[1], commands[i]); close(fds[1]); in = fds[0]; } //execute the last command if(in != STDIN_FILENO) dup2(in, STDIN_FILENO); execvp(commands[num_pipes][0], commands[num_pipes]); } //execute command execvp(tokens[0], tokens); printf("%s: invalid command\n", tokens[0]); exit(EXIT_FAILURE); } } //update history if(strcmp(input, "") && status==0) { strcpy( history[oldest_history], input ); oldest_history--; if(oldest_history < 0) oldest_history = HISTORY_SIZE - 1; } } }
csh_cmd *get_options(char *s) { csh_cmd* cmd = make_tokenlist(s); return cmd; }
mult_pipe(char *buf, int argNum, int numPipes) { int i = 0, status, n; pid_t pid; char *cmd[CMD_MAX]; int pipefds[2*numPipes]; while(i < numPipes) { if(pipe(pipefds + i*2) < 0) { perror("Couldn't pipe"); exit(-1); } i++; } char input_line[MAX], *argv[MAX]; char *line; i = 0; line = buf; cmd[i] = strtok(line, "|"); do { i++; line = NULL; cmd[i] = strtok(line, "|"); } while(cmd[i] != NULL); int cmdNum = i; int j = 0; i = 0; while(i < cmdNum) { pid = fork(); if (pid < 0) { perror("fork error"); exit(-1); } if(pid == 0) { if(i+1 < cmdNum) { if(dup2(pipefds[j+1], 1) < 0) { perror("Can't duplicate"); exit(-1); } } if(j != 0) { if(dup2(pipefds[j-2], 0) < 0) { perror("Can't duplicate"); exit(-1); } } int n = 0; while (n < 2*numPipes) { close(pipefds[n]); n++; } make_tokenlist(cmd[i], argv); if(execvp(argv[0], argv) < 0) { perror("Can't run command"); exit(-1); } } i++; j+=2; } n = 0; while(n < 2*numPipes) { close(pipefds[n]); n++; } n = 0; while (n < numPipes+1) { wait(&status); n++; } }
int main() { int argNum; char input[MAX], history[MAX], *tokens[CMD_MAX], buf[MAX]; char userName[20]; pid_t pid; memcpy(userName, getUserName(),20); int histCount = 0; char cmdHist[10][MAX]; // signal signal(SIGINT, inthand); struct sigaction psa; psa.sa_handler = inthand; sigfillset(&psa.sa_mask); psa.sa_flags = 0; sigaction(SIGINT, &psa, NULL); sigaction(SIGTSTP, &psa, NULL); while(1) // command line { argNum = 0; printf("%s> ",userName); fgets(input, MAX, stdin); strtok(input, "\n"); // Remove trailing newline strcpy(history, input); // copy for history before destroyed strcpy(buf, input); // copy for pipe before destroyed if(input != NULL) argNum = make_tokenlist(input, tokens); if(strcmp(tokens[0], "exit") == 0) exit(1); if(strcmp(tokens[0], "history") == 0) { pid = fork(); if (pid < 0) { perror("Problem forking"); exit(1); } if(pid > 0) { wait(0); } else { // returns proper number of hist items to show int count = (histCount <= 10) ? histCount : 10; // if more than 10 items, start off in the middle of the list else beginning int start = (histCount > 10) ? histCount % 10 : 0; int i = 0; for( ; i < count; i++) // prints history list { printf("%d. %s\n",i+1,cmdHist[(start+i)%10]); } return 0; } } strcpy(cmdHist[histCount%10], history); histCount++; // after history to skip current call being places in list char *argv1[10], *argv2[10]; // for seperating commands pid = fork(); if (pid < 0) { perror("Problem forking"); exit(1); } if(pid > 0) { wait(NULL); // waits until child changes state usleep(10000); // waits for 10 millisecond to complete child commands } else { int j = 1, m, n, fds[2], numBytes, status, in, out; char line[80], input[MAX], output[MAX]; while(j < argNum) { if(strcmp(tokens[j], "<") == 0) { pid = fork(); if (pid < 0) { perror("fork error"); exit(-1); } if (pid > 0) { // parent wait(NULL); } else { // child n = j+1; while(n < argNum) { if(strcmp(tokens[n],">") == 0) mult_io(tokens, argNum); n++; } n = 0; while(n < j) { argv2[n] = tokens[n]; n++; } argv2[n] = NULL; strcpy(input, tokens[j+1]); // first command after < in = open(input, O_RDONLY); if (dup2(in, STDIN_FILENO) < 0) { perror("Can't duplicate"); exit(1); } close(in); status = execvp(argv2[0], argv2); // second command if (status < 0) { perror("parent: exec problem"); exit(1); } } return 0; } else if(strcmp(tokens[j], ">") == 0) { pid = fork(); if (pid < 0) { perror("fork error"); exit(-1); } if (pid > 0) { // parent wait(NULL); } else { // child m = 0; while(m < j) { argv1[m] = tokens[m]; m++; } argv1[m] = NULL; strcpy(output, tokens[j+1]); out = open(output, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR); if (dup2(out, STDOUT_FILENO) < 0) { perror("Can't duplicate"); exit(1); } close(out); status = execvp(argv1[0], argv1); // first command if (status < 0) { perror("parent: exec problem"); exit(1); } } return 0; } else if(strcmp(tokens[j], "|") == 0) { int pipeNum = 1, n = j+1; while(n < argNum) { if(strcmp(tokens[n], "|") == 0) pipeNum++; n++; } mult_pipe(buf, argNum, pipeNum); exit(1); } j++; } execvp(tokens[0], tokens); exit(1); } } }