diff --git a/documents/references.md b/documents/references.md index 88477c44228c8d2476ee234c0717a363ea46449c..637d575545a83e3c0a6763cb63cb5d493af2b06c 100644 --- a/documents/references.md +++ b/documents/references.md @@ -8,21 +8,44 @@ Includes anything from stack overflow links to notes about logic from previous w ### All Printf Format Types <https://www.tutorialspoint.com/c_standard_library/c_function_printf.htm> +### Printing all Values in Argv +<https://stackoverflow.com/a/35861090> + +### Cast Argv Value to Int +<https://stackoverflow.com/a/4324417> + +### Printing to Stderr +<https://stackoverflow.com/a/39002243> + +### Booleans in C +<https://www.geeksforgeeks.org/bool-in-c/> + +### Compare Strings in C +<https://www.programiz.com/c-programming/library-function/string.h/strcmp> + + + + ## Makefile * Purpose of ".PHONY" - <https://stackoverflow.com/a/2145605> * Have one command execute another - <https://stackoverflow.com/a/48552668> * Surpress output messages - <https://stackoverflow.com/a/3148530> + ## Bash +### General Help +At times, I referenced bash scripts I created this semester in my compiler class assignment. Both projects function +similarly in that code is fed in one or more input files from a given directory.<br> +<https://git.brandon-rodriguez.com/assembly/cs6810/a2/> ### Getting Last Argument <https://www.cyberciti.biz/faq/linux-unix-bsd-apple-osx-bash-get-last-argument/> ### Color Output -https://stackoverflow.com/a/5947802 +<https://stackoverflow.com/a/5947802> ### Getting Script Directory -https://stackoverflow.com/a/337006 +<https://stackoverflow.com/a/337006> ### Manipulating Strings -https://stackoverflow.com/a/14703709 +<https://stackoverflow.com/a/14703709> diff --git a/main.c b/main.c index 81b8d73a62b8dc8fe85ad9952b3b8f535886fb68..2340ccf53f6266a02be47ff545ad51bf2288c6db 100644 --- a/main.c +++ b/main.c @@ -7,13 +7,18 @@ * * * Simulates system use of a cache for data storage. + * Cache Settings Arguments: + * * S: Number of sets in cache. + * * E: Lines per set. + * * B: Block size (number of bits per block). */ // Import headers. #include <ctype.h> -#include <stdio.h> +#include <stdbool.h> #include <stdlib.h> +#include <stdio.h> #include <string.h> @@ -33,5 +38,91 @@ int main(int argc, char* argv[]) { printf("Starting program.\n"); + char* file_name = NULL; + int total_sets = -1; + int lines_per_set = -1; + int bits_per_block = -1; + bool verbose = false; + bool setting_sets = false; + bool setting_lines = false; + bool setting_blocks = false; + bool setting_file = false; + + printf("argc: %d\n", argc); + // Loop through all passed args and process accordingly. + for (int index = 0; index < argc; index++) { + + char* arg = argv[index]; + + // Check arg. + if (strcmp(arg, "-v") == 0 || strcmp(arg, "-V") == 0) { + // Set verbose mode flag. + verbose = true; + + } else if (strcmp(arg, "-s") == 0 || strcmp(arg, "-S") == 0) { + // Prepare next arg read in for set value. + setting_sets = true; + setting_lines = false; + setting_blocks = false; + setting_file = false; + + } else if (strcmp(arg, "-e") == 0 || strcmp(arg, "-E") == 0) { + // Prepare next arg read in for line value. + setting_sets = false; + setting_lines = true; + setting_blocks = false; + setting_file = false; + + } else if (strcmp(arg, "-b") == 0 || strcmp(arg, "-B") == 0) { + // Prepare next arg read in for block value. + setting_sets = false; + setting_lines = false; + setting_blocks = true; + setting_file = false; + + } else if (strcmp(arg, "-t") == 0 || strcmp(arg, "-T") == 0) { + // Prepare next arg read in for file value. + setting_sets = false; + setting_lines = false; + setting_blocks = false; + setting_file = true; + + } else { + // Not a flag. Try to get value. + if (setting_sets) { + // Prior flag was sets. Set accordingly. + total_sets = atoi(arg); + printf("Set total_set value of %d.\n", total_sets); + + } else if (setting_lines) { + // Prior value was lines. Set accordingly. + lines_per_set = atoi(arg); + printf("Set lines_per_set value of %d.\n", lines_per_set); + + } else if (setting_blocks) { + // Prior flag was blocks. Set accordingly. + bits_per_block = atoi(arg); + printf("Set bits_per_block value of %d.\n", bits_per_block); + + } else if (setting_file) { + // Prior flag was blocks. Set accordingly. + file_name = arg; + printf("Set file_name value of %s.\n", file_name); + + } else { + // Could not determine what value is for. First index is file name so we can skip that. + if (index > 0) { + fprintf(stderr, "Could not determine flag for value \"%s\".\n", arg); + } + } + + // Reset all flags for next value. + setting_sets = false; + setting_lines = false; + setting_blocks = false; + setting_file = false; + } + } + printf("\n\nTerminating program.\n"); } diff --git a/run.sh b/run.sh index 2439b652ccf6172dfd82386f7f66abdd71a02046..a1b3aad1dfa91f1ee3fb9f3392b7138a116d9182 100755 --- a/run.sh +++ b/run.sh @@ -72,7 +72,7 @@ function execute_single_file () { echo "" echo -e "${color_blue}Running input file on compiled C code. Runtime args:${color_reset}" echo -e "${color_blue} -v -s 4 -E 1 -b 4 -t $file${color_reset}" - ./a.out -v -s 4 -E 1 -b 4 -t $file + valgrind --leak-check=full ./a.out -v -s 4 -E 1 -b 4 -t $file # Clean up directory. make clean