From 742bdd74d2f56ebe7b82fc384592a68a393af6f3 Mon Sep 17 00:00:00 2001 From: p-w-rs Date: Thu, 15 Sep 2022 14:40:38 -0500 Subject: [PATCH] stuff --- 02-TeenyTinyShell/ttsh.c | 118 ++++++++++++++++++++------------------- 1 file changed, 62 insertions(+), 56 deletions(-) diff --git a/02-TeenyTinyShell/ttsh.c b/02-TeenyTinyShell/ttsh.c index f6c92af..e93c0f5 100644 --- a/02-TeenyTinyShell/ttsh.c +++ b/02-TeenyTinyShell/ttsh.c @@ -1,9 +1,9 @@ /************************************* - * + * * Class: CS3840 - YOUR SECTION HERE - * + * * Name(s): YOUR NAME(S) HERE - * + * * Lab: Teeny Tiny Shell * *************************************/ @@ -17,24 +17,26 @@ /* read_cmd_string() * Reads a line of text from the user * Parameters: dest - target location for the input - * Returns: int - 0 on success or + * Returns: int - 0 on success or * -1 on error */ int read_cmd_string(char dest[INPUT_MAX]) { - // Read user input - if(fgets(dest, INPUT_MAX, stdin) == NULL) { - fprintf(stderr, "Unable to read user input\n"); - return -1; - } + // Read user input + if (fgets(dest, INPUT_MAX, stdin) == NULL) + { + fprintf(stderr, "Unable to read user input\n"); + return -1; + } - // Remove trailing return character - int len = strlen(dest); - if(dest[len-1] == '\n') { - dest[len - 1] = '\0'; - } + // Remove trailing return character + int len = strlen(dest); + if (dest[len - 1] == '\n') + { + dest[len - 1] = '\0'; + } - return 0; + return 0; } /* parse_commands() @@ -46,57 +48,61 @@ int read_cmd_string(char dest[INPUT_MAX]) */ int parse_commands(char input[INPUT_MAX], char cmd_strs[CMD_MAX][INPUT_MAX]) { - // Chop the input into command strings - int cmd_count = 0; - char* cmd_ptr = strtok(input, ";"); - while(cmd_ptr) { - if(cmd_count >= CMD_MAX) { - fprintf(stderr, "Too many commands\n"); - return -1; - } - strncpy(cmd_strs[cmd_count], cmd_ptr, INPUT_MAX); - cmd_count++; - cmd_ptr = strtok(NULL, ";"); - } + // Chop the input into command strings + int cmd_count = 0; + char *cmd_ptr = strtok(input, ";"); + while (cmd_ptr) + { + if (cmd_count >= CMD_MAX) + { + fprintf(stderr, "Too many commands\n"); + return -1; + } + strncpy(cmd_strs[cmd_count], cmd_ptr, INPUT_MAX); + cmd_count++; + cmd_ptr = strtok(NULL, ";"); + } - return cmd_count; + return cmd_count; } int main() { - char user_input[INPUT_MAX]; - char cmd_strs[CMD_MAX][INPUT_MAX]; + char user_input[INPUT_MAX]; + char cmd_strs[CMD_MAX][INPUT_MAX]; - // TODO need to be able to get input from - // the user in a loop + // TODO need to be able to get input from + // the user in a loop - // Print the input prompt - printf("$> "); + // Print the input prompt + printf("$> "); - // Read user input - if(read_cmd_string(user_input) == -1) { - return 1; - } + // Read user input + if (read_cmd_string(user_input) == -1) + { + return 1; + } - // TODO: Figure out how to handle the 'quit' command + // TODO: Figure out how to handle the 'quit' command - // Chop the input into command strings - int cmd_count = parse_commands(user_input, cmd_strs); - if(cmd_count == -1) { - return 1; - } + // Chop the input into command strings + int cmd_count = parse_commands(user_input, cmd_strs); + if (cmd_count == -1) + { + return 1; + } - // Chop the commands into arguments and execute one at a time - for(int i = 0; i < cmd_count; i++) { - - // TODO: - // 1) Chop the command into command line arguments - // need to handle up to 10 arguments - // NOTE: the command name is always the first argument - // 2) fork a process - // 3) execute the command with execvp + // Chop the commands into arguments and execute one at a time + for (int i = 0; i < cmd_count; i++) + { - } - - return 0; + // TODO: + // 1) Chop the command into command line arguments + // need to handle up to 10 arguments + // NOTE: the command name is always the first argument + // 2) fork a process + // 3) execute the command with execvp + } + + return 0; }