diff --git a/Main.c b/Main.c index 415ac3322b23c66cddd35258002d6cb65f2c316d..3d0a214b533445d23b2db1da39d7576b886bdd12 100644 --- a/Main.c +++ b/Main.c @@ -19,10 +19,11 @@ // Import headers. +#include <ctype.h> #include <stdio.h> -#include <string.h> #include <stdlib.h> -#include <ctype.h> +#include <string.h> +#include <sys/wait.h> #include "apue.h" #include "HelperHeader.h" @@ -38,6 +39,7 @@ int makeargv(); // Creates argv using passed line. void parse_line(); // Parses provided line into commands. char** parse_command(); // Parses provided command into subsections. +void execute_command(); // Executes provided command. void make_arg_example(); @@ -49,10 +51,11 @@ void make_arg_example(); int main(int argc, char* argv[]) { parse_line(""); + parse_line("ls"); parse_line("ls -l"); - parse_line("ls -l | grep a"); - parse_line("ls -l | grep a | grep b"); - parse_line("ls -l | grep a | grep b | grep c"); + // parse_line("ls -l | grep a"); + // parse_line("ls -l | grep a | grep b"); + // parse_line("ls -l | grep a | grep b | grep c"); return 0; } @@ -70,7 +73,7 @@ void parse_line(char* command_line) { char*** commands; char*** line_tokens; - printf("Beginning command line parse...\n"); + printf("Beginning command line parse... "); line_tokens = calloc(1, sizeof(char***)); token_number = makeargv(command_line, "|", line_tokens); @@ -78,7 +81,7 @@ void parse_line(char* command_line) { err_sys("Unexpected error parsing command line."); } - printf("%d\n", token_number); + printf("%d tokens found.\n", token_number); commands = calloc(token_number, sizeof(char***)); // Check for multiple tokens within command line. @@ -90,7 +93,13 @@ void parse_line(char* command_line) { // Get arguments of individual command. *commands = parse_command(**line_tokens); printf("%s ", **commands); - printf("%s\n", *(*commands + 1)); + if (*(*commands + 1) != NULL) { + printf("%s\n", *(*commands + 1)); + } else { + printf("\n"); + } + + execute_command(*commands); free(**commands); free(*(*commands + 1)); @@ -152,22 +161,63 @@ char** parse_command(char* command) { } +void execute_command(char** command) { + int status; + int fd[2]; + pid_t pid; + + printf("Executing Commands...\n"); + // Fork process. + pipe(fd); + switch (pid = fork()) { + + case 0: // Child process. + dup2(fd[0], STDIN_FILENO); + close(fd[1]); // Child does not need this. + printf("IMA CHILD.\n\n"); + + // Basic ls example. + //execlp("ls", "ls", (char*) NULL); + // 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); + } + + err_sys("Child execution did not work."); + break; + case -1: // Error. + err_sys("Fork Error."); + break; + default: // Parent Process. + 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"); + break; + } +} + + /** * Example of using makeargv. */ void make_arg_example() { int index; - int resultInt; + int result_int; char*** argvp; // Result: 0 index = 0; argvp = calloc(1, sizeof(char***)); - resultInt = makeargv("", "|", argvp); - if (resultInt < 0) { + result_int = makeargv("", "|", argvp); + if (result_int < 0) { err_sys("Unexpected error parsing command."); } - printf("%d - ", resultInt); + printf("%d - ", result_int); while (*(*argvp + index) != NULL) { printf("%s ", *(*argvp + index)); index++; @@ -181,11 +231,11 @@ void make_arg_example() { // Result: 1 index = 0; argvp = calloc(1, sizeof(char***)); - resultInt = makeargv("ls", "|", argvp); - if (resultInt < 0) { + result_int = makeargv("ls", "|", argvp); + if (result_int < 0) { err_sys("Unexpected error parsing command."); } - printf("%d - ", resultInt); + printf("%d - ", result_int); while (*(*argvp + index) != NULL) { printf("%s ", *(*argvp + index)); index++; @@ -199,11 +249,11 @@ void make_arg_example() { // Result: 2 index = 0; argvp = calloc(1, sizeof(char***)); - resultInt = makeargv("ls | grep a", "|", argvp); - if (resultInt < 0) { + result_int = makeargv("ls | grep a", "|", argvp); + if (result_int < 0) { err_sys("Unexpected error parsing command."); } - printf("%d - ", resultInt); + printf("%d - ", result_int); while (*(*argvp + index) != NULL) { printf("%s ", *(*argvp + index)); index++; @@ -217,11 +267,11 @@ void make_arg_example() { // Result: 3 index = 0; argvp = calloc(1, sizeof(char***)); - resultInt = makeargv("ls | grep a | grep b", "|", argvp); - if (resultInt < 0) { + result_int = makeargv("ls | grep a | grep b", "|", argvp); + if (result_int < 0) { err_sys("Unexpected error parsing command."); } - printf("%d - ", resultInt); + printf("%d - ", result_int); while (*(*argvp + index) != NULL) { printf("%s ", *(*argvp + index)); index++; @@ -235,11 +285,11 @@ void make_arg_example() { // Result: 4 index = 0; argvp = calloc(1, sizeof(char***)); - resultInt = makeargv("ls | grep a | grep b | grep c", "|", argvp); - if (resultInt < 0) { + result_int = makeargv("ls | grep a | grep b | grep c", "|", argvp); + if (result_int < 0) { err_sys("Unexpected error parsing command."); } - printf("%d - ", resultInt); + printf("%d - ", result_int); while (*(*argvp + index) != NULL) { printf("%s ", *(*argvp + index)); index++; @@ -253,11 +303,11 @@ void make_arg_example() { // Result: 5 index = 0; argvp = calloc(1, sizeof(char***)); - resultInt = makeargv("ls | grep a | grep b | grep c | grep d", "|", argvp); - if (resultInt < 0) { + result_int = makeargv("ls | grep a | grep b | grep c | grep d", "|", argvp); + if (result_int < 0) { err_sys("Unexpected error parsing command."); } - printf("%d - ", resultInt); + printf("%d - ", result_int); while (*(*argvp + index) != NULL) { printf("%s ", *(*argvp + index)); index++;