diff --git a/Main.c b/Main.c index 3d0a214b533445d23b2db1da39d7576b886bdd12..b73490997bcc49f086a9b53f41efba71a790ff13 100644 --- a/Main.c +++ b/Main.c @@ -33,12 +33,16 @@ // Variables. +typedef struct{ + int argc; + char*** argv; +} makeargv_struct; // Method Declaration. int makeargv(); // Creates argv using passed line. void parse_line(); // Parses provided line into commands. -char** parse_command(); // Parses provided command into subsections. +makeargv_struct* parse_command(); // Parses provided command into subsections. void execute_command(); // Executes provided command. void make_arg_example(); @@ -67,100 +71,81 @@ int main(int argc, char* argv[]) { */ void parse_line(char* command_line) { int index; - int array_index; - int string_index; - int token_number; - char*** commands; - char*** line_tokens; + int index2; + makeargv_struct* line_argv; // Pointer to makeargv struct. + makeargv_struct* command_argv; // Pointer to makeargv struct. printf("Beginning command line parse... "); - line_tokens = calloc(1, sizeof(char***)); - token_number = makeargv(command_line, "|", line_tokens); - if (token_number < 0) { + // Parse line into struct. Seperates by each found "|" in argument. + // Each seperated value should be an individual commmand. + line_argv = calloc(1, sizeof(makeargv_struct)); + line_argv->argv = calloc(1, sizeof(char***)); + line_argv->argc = makeargv(command_line, "|", line_argv->argv); + + // Check for errors. + if (line_argv->argc < 0) { err_sys("Unexpected error parsing command line."); } - printf("%d tokens found.\n", token_number); - commands = calloc(token_number, sizeof(char***)); - - // Check for multiple tokens within command line. - if (token_number == 1) { - // Only one command line token. Probably can be added into the - // "multi-token" section below, but it's easier to separate so - // that "simple" case logic can be worked out first. - - // Get arguments of individual command. - *commands = parse_command(**line_tokens); - printf("%s ", **commands); - if (*(*commands + 1) != NULL) { - printf("%s\n", *(*commands + 1)); - } else { - printf("\n"); - } + printf("%d tokens found.\n", line_argv->argc); + command_argv = calloc(1, sizeof(makeargv_struct)); + command_argv->argv = calloc(1, sizeof(char***)); - execute_command(*commands); + // For each argument above, parse again. + // This will separate the individual arguments of each given command. + index = 0; + while (index < line_argv->argc) { - free(**commands); - free(*(*commands + 1)); - free(*commands); - } else if (token_number > 1) { - // Multiple command line tokens detected. - index = 0; + printf("%s\n", (**(line_argv->argv + index))); + command_argv->argc = makeargv((**(line_argv->argv)), " ", command_argv->argv); - // Separate token values and hold within array of arrays of strings. - while (*(*line_tokens + index) != NULL) { - //printf("%s ", (*(*line_tokens + index))); + // Check for errors. + if (command_argv->argc < 0) { + err_sys("Unexpected error parsing command line."); + } - *(commands + index) = parse_command(*(*line_tokens + index)); - printf("%s ", **(commands + index)); - printf("%s\n", *(*(commands + index) + 1)); - index++; + index2 = 0; + while (index2 < command_argv->argc) { + printf("%s ", (*(*command_argv->argv + index2))); + index2++; } printf("\n"); - // Free used memory. - array_index = 0; - while (array_index < token_number) { - string_index = 0; - while (string_index < 2) { - free(*(*(commands + array_index) + string_index)); - //printf("%s\n", *(*(commands + array_index) + string_index)); - string_index++; - } - free(*(commands + array_index)); - // printf("%s\n", *(commands + array_index)); - array_index++; - } + execute_command(*command_argv->argv); + index++; } + // Free memory. + index = 0; + while (index < line_argv->argc) { + free(*(*(line_argv->argv + index))); + free(*(line_argv->argv + index)); + index2 = 0; + // // Not needed? + // while (index2 < command_argv->argc) { + // //printf("%s ", (*(*command_argv->argv + index2))); + // free(*(*command_argv->argv + index2)); + // index2++; + // } + free(*(*command_argv->argv + index2)); + free(*(command_argv->argv + index)); + index++; + } + if (line_argv->argc == 0) { + free(*line_argv->argv); + } - free(commands); - free(**line_tokens); - free(*line_tokens); - free(line_tokens); - printf("\n"); - printf("\n"); + free(command_argv->argv); + free(line_argv->argv); + free(command_argv); + free(line_argv); } /** - * Parses an individual command by separating command from args. - * - * Returns a string array of parsed command items. + * Takes provided command and runs appropriately. */ -char** parse_command(char* command) { - char** command_arguments; - printf("Beginning command parse...\n"); - - command_arguments = calloc(2, sizeof(char**)); - *command_arguments = copy_string(strtok(command, " ")); - *(command_arguments + 1) = copy_string(strtok(NULL, "")); - - return command_arguments; -} - - void execute_command(char** command) { int status; int fd[2]; @@ -181,11 +166,14 @@ void execute_command(char** command) { // ls with one arg example. //execlp("ls", "ls", "-l", (char*) NULL); - if (*(command + 1) != NULL) { - execlp(*command, *command, *(command + 1), (char*) NULL); - } else { - execlp(*command, *command, (char*) NULL); - } + // execlp example. + // if (*(command + 1) != NULL) { + // execlp(*command, *command, *(command + 1), (char*) NULL); + // } else { + // execlp(*command, *command, (char*) NULL); + // } + + execvp(*command, command); err_sys("Child execution did not work."); break; @@ -196,7 +184,7 @@ void execute_command(char** command) { close(fd[0]); // Parent does not need this. printf("IMA PARENT.\n"); wait(&status); // Wait for child process to end. - printf("\nOh, he ded. No longer a parent.\n"); + printf("\nOh, he ded. No longer a parent.\n\n"); break; } }