Line data Source code
1 : #include "command_sequence.h" 2 : 3 20 : void command_sequence_init(char *input) 4 : { 5 : char *commands[MAX_COMMANDS]; 6 : char *operators[MAX_OPERATORS]; 7 : 8 20 : int command_count = 0; 9 20 : int operator_count = 0; 10 20 : char *currentCommand = NULL; 11 : 12 : CommandSequence sequence; 13 20 : sequence.commands = malloc(MAX_COMMANDS * sizeof(Command)); 14 20 : sequence.operators = malloc(MAX_OPERATORS * sizeof(char *)); 15 20 : sequence.num_commands = 0; 16 20 : sequence.num_operators = 0; 17 : 18 20 : char *token = strtok(input, INPUT_DELIMITERS); 19 89 : while (token != NULL) 20 : { 21 70 : if (command_count >= MAX_COMMANDS) 22 : { 23 1 : print_info("livl-shell$ is informing you that only the three first commands will be executed. Limit of commands reached."); 24 1 : token = NULL; 25 1 : break; 26 : } 27 : 28 69 : process_token(token, commands, operators, ¤tCommand, &command_count, &operator_count); 29 69 : token = strtok(NULL, INPUT_DELIMITERS); 30 : 31 69 : if (token == NULL || get_operator_type(token) != UNKNOWN) 32 : { 33 31 : store_command(commands, ¤tCommand, &command_count); 34 : } 35 : } 36 : 37 : // Build the command sequence with its commands 38 51 : for (int i = 0; i < command_count; i++) 39 : { 40 31 : sequence.commands[sequence.num_commands++] = evaluate_command(commands[i]); 41 31 : free(commands[i]); 42 : } 43 : 44 : // Build the command sequence with its operators 45 20 : sequence.num_operators = operator_count; 46 31 : for (int i = 0; i < operator_count; i++) 47 : { 48 11 : sequence.operators[i] = strdup(operators[i]); 49 11 : free(operators[i]); 50 : } 51 : 52 20 : evaluate_redirection(&sequence); 53 : 54 20 : int result = execute_command_sequence(&sequence); 55 : 56 18 : if (result < 0) 57 : { 58 0 : fprintf(stderr, "An error occurred while executing the commands\n"); 59 : } 60 : 61 18 : free(token); 62 18 : free_command_sequence(&sequence); 63 18 : } 64 : 65 69 : void process_token(char *token, char **commands, char **operators, char **currentCommand, int *cmdIndex, int *opIndex) 66 : { 67 69 : if (get_operator_type(token) != UNKNOWN) 68 : { 69 11 : operators[(*opIndex)++] = strdup(token); 70 11 : return; 71 : } 72 : 73 : // Token is a command 74 58 : if (*currentCommand == NULL) 75 : { 76 31 : *currentCommand = strdup(token); 77 : } 78 : else 79 : { 80 27 : size_t len = strlen(*currentCommand) + strlen(token) + 2; 81 27 : char *newCommand = malloc(len); 82 27 : if (newCommand == NULL) 83 : { 84 0 : perror("malloc"); 85 0 : return; 86 : } 87 27 : strcpy(newCommand, *currentCommand); 88 27 : strcat(newCommand, " "); 89 27 : strcat(newCommand, token); 90 27 : free(*currentCommand); 91 27 : *currentCommand = newCommand; 92 : } 93 : } 94 : 95 31 : void store_command(char **commands, char **currentCommand, int *cmdIndex) 96 : { 97 31 : if (*currentCommand != NULL) 98 : { 99 31 : commands[(*cmdIndex)++] = *currentCommand; 100 31 : *currentCommand = NULL; 101 : } 102 31 : } 103 : 104 20 : void free_command_sequence(CommandSequence *sequence) 105 : { 106 51 : for (int i = 0; i < sequence->num_commands; ++i) 107 : { 108 31 : free_command(&sequence->commands[i]); 109 : } 110 20 : free(sequence->commands); 111 20 : sequence->commands = NULL; 112 : 113 31 : for (int i = 0; i < sequence->num_operators; ++i) 114 : { 115 11 : free(sequence->operators[i]); 116 11 : sequence->operators[i] = NULL; 117 : } 118 20 : free(sequence->operators); 119 20 : sequence->operators = NULL; 120 20 : }