From 07f1d5bb8cf0cf8a7fb61fff1644be354867b04f Mon Sep 17 00:00:00 2001 From: Brandon Rodriguez <brodriguez8774@gmail.com> Date: Fri, 20 Oct 2017 12:20:40 -0400 Subject: [PATCH] Add logic to toss commands into makeargv and then separate command arguments Very basic. Only works on static, predefined strings. --- Main.c | 230 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 227 insertions(+), 3 deletions(-) diff --git a/Main.c b/Main.c index a6e42bf..415ac33 100644 --- a/Main.c +++ b/Main.c @@ -1,8 +1,8 @@ /** * Brandon Rodriguez * CS 3240 - * - * a (Assignment ) + * 10-19-17 + * a3 (Assignment 4) */ @@ -24,16 +24,22 @@ #include <stdlib.h> #include <ctype.h> #include "apue.h" +#include "HelperHeader.h" // Define Vars. -//#define ??? +#define BUFFER_SIZE 4096 // Variables. // 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. + +void make_arg_example(); /** @@ -42,4 +48,222 @@ */ int main(int argc, char* argv[]) { + parse_line(""); + 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"); + + return 0; +} + + +/** + * Parses given command into subsections, separated by spaces. + * Generally speaking, this will fall into either "command" or "argument". + */ +void parse_line(char* command_line) { + int index; + int array_index; + int string_index; + int token_number; + char*** commands; + char*** line_tokens; + + printf("Beginning command line parse...\n"); + + line_tokens = calloc(1, sizeof(char***)); + token_number = makeargv(command_line, "|", line_tokens); + if (token_number < 0) { + err_sys("Unexpected error parsing command line."); + } + + printf("%d\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); + printf("%s\n", *(*commands + 1)); + + free(**commands); + free(*(*commands + 1)); + free(*commands); + } else if (token_number > 1) { + // Multiple command line tokens detected. + index = 0; + + // Separate token values and hold within array of arrays of strings. + while (*(*line_tokens + index) != NULL) { + //printf("%s ", (*(*line_tokens + index))); + + *(commands + index) = parse_command(*(*line_tokens + index)); + printf("%s ", **(commands + index)); + printf("%s\n", *(*(commands + index) + 1)); + index++; + } + 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++; + } + } + + + free(commands); + free(**line_tokens); + free(*line_tokens); + free(line_tokens); + printf("\n"); + printf("\n"); +} + + +/** + * Parses an individual command by separating command from args. + * + * Returns a string array of parsed command items. + */ +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; +} + + +/** + * Example of using makeargv. + */ +void make_arg_example() { + int index; + int resultInt; + char*** argvp; + + // Result: 0 + index = 0; + argvp = calloc(1, sizeof(char***)); + resultInt = makeargv("", "|", argvp); + if (resultInt < 0) { + err_sys("Unexpected error parsing command."); + } + printf("%d - ", resultInt); + while (*(*argvp + index) != NULL) { + printf("%s ", *(*argvp + index)); + index++; + } + printf("\n"); + free(**argvp); + free(*argvp); + free(argvp); + + + // Result: 1 + index = 0; + argvp = calloc(1, sizeof(char***)); + resultInt = makeargv("ls", "|", argvp); + if (resultInt < 0) { + err_sys("Unexpected error parsing command."); + } + printf("%d - ", resultInt); + while (*(*argvp + index) != NULL) { + printf("%s ", *(*argvp + index)); + index++; + } + printf("\n"); + free(**argvp); + free(*argvp); + free(argvp); + + + // Result: 2 + index = 0; + argvp = calloc(1, sizeof(char***)); + resultInt = makeargv("ls | grep a", "|", argvp); + if (resultInt < 0) { + err_sys("Unexpected error parsing command."); + } + printf("%d - ", resultInt); + while (*(*argvp + index) != NULL) { + printf("%s ", *(*argvp + index)); + index++; + } + printf("\n"); + free(**argvp); + free(*argvp); + free(argvp); + + + // Result: 3 + index = 0; + argvp = calloc(1, sizeof(char***)); + resultInt = makeargv("ls | grep a | grep b", "|", argvp); + if (resultInt < 0) { + err_sys("Unexpected error parsing command."); + } + printf("%d - ", resultInt); + while (*(*argvp + index) != NULL) { + printf("%s ", *(*argvp + index)); + index++; + } + printf("\n"); + free(**argvp); + free(*argvp); + free(argvp); + + + // Result: 4 + index = 0; + argvp = calloc(1, sizeof(char***)); + resultInt = makeargv("ls | grep a | grep b | grep c", "|", argvp); + if (resultInt < 0) { + err_sys("Unexpected error parsing command."); + } + printf("%d - ", resultInt); + while (*(*argvp + index) != NULL) { + printf("%s ", *(*argvp + index)); + index++; + } + printf("\n"); + free(**argvp); + free(*argvp); + free(argvp); + + + // Result: 5 + index = 0; + argvp = calloc(1, sizeof(char***)); + resultInt = makeargv("ls | grep a | grep b | grep c | grep d", "|", argvp); + if (resultInt < 0) { + err_sys("Unexpected error parsing command."); + } + printf("%d - ", resultInt); + while (*(*argvp + index) != NULL) { + printf("%s ", *(*argvp + index)); + index++; + } + printf("\n"); + free(**argvp); + free(*argvp); + free(argvp); } -- GitLab