diff --git a/userprog/argument_parsing.h b/userprog/argument_parsing.h index d5cef69f63d1e20c7ab9ee15d643ee9390064401..585efe1b65621145e6316efb3b7ecfcc35c470d7 100644 --- a/userprog/argument_parsing.h +++ b/userprog/argument_parsing.h @@ -1,6 +1,19 @@ +/* + * argument_parsing.h --prototypes functions required for parsing arguments from + * command line and placing these into the program stack structure. + * + * Authored by Joshua Saxby + */ #ifndef USERPROG_ARGUMENT_PARSING_H #define USERPROG_ARGUMENT_PARSING_H + +// some macros for argument length and count limits +// TODO: decide how many arguments we want --is 255 the standard number? +#define USERPROG_ARGV_COUNT 255 +// TODO: clarify what a sensible value for this is +#define USERPROG_RAW_ARGV_LENGTH 255 + /* * Given a string containing the command invoking the program `command_line` * and an array of C strings (pointer to `char* argv[]`), parse `command_line` diff --git a/userprog/parse_arguments.c b/userprog/parse_arguments.c index c86364fdd2408b978f019f9df3d7c09a48b0ec31..9de6f294fa5d77fc3319b6a36cb7c52458ed1241 100644 --- a/userprog/parse_arguments.c +++ b/userprog/parse_arguments.c @@ -1,11 +1,29 @@ +/* + * parse_arguments.c --implements function for parsing a command-line string + * into the program arguments contained within it. + * + * Authored by Joshua Saxby + */ +#include <stddef.h> +#include <string.h> + #include "userprog/argument_parsing.h" int parse_arguments(const char* command_line, char**argv) { - /* - * XXX: Dummy implementation which sets argv[0] to "echo" so at least there's - * a filename to load. - */ - argv[0] = "echo"; - return 1; + int argc = 0; + // the state pointer required by strtok_r() for use between calls + char* state_pointer; + // first call to strtok_r() is the only one where string to parse is passed + argv[0] = strtok_r(command_line, " ", &state_pointer); + argc++; + // now, parse token-by-token for any additional arguments + char* token = strtok_r(NULL, " ", &state_pointer); + // continue while still tokens to read and maximum arg count is not reached + while (token != NULL && argc < USERPROG_ARGV_COUNT) { + argv[argc] = token; + argc++; + token = strtok_r(NULL, " ", &state_pointer); + } + return argc; } diff --git a/userprog/process.c b/userprog/process.c index 87fd42a738663bdcfd7c5d2e1da8f92bc950bee4..5fbfb38e05f7d429a994a2e87d71221068c56531 100644 --- a/userprog/process.c +++ b/userprog/process.c @@ -232,9 +232,11 @@ load (const char *command, void (**eip) (void), void **esp) int i; // extract arguments (including file name) - // NOTE: decide how many arguments we want --is 255 the standard number? - char* argv[255]; - int argc = parse_arguments(command, argv); + char* argv[USERPROG_ARGV_COUNT]; + // copy the command string so we have a separate copy to manipulate + char command_copy[USERPROG_RAW_ARGV_LENGTH]; + strlcpy(command_copy, command, USERPROG_RAW_ARGV_LENGTH); + int argc = parse_arguments(command_copy, argv); char* file_name = argv[0]; // file name is always argument 0 /* Allocate and activate page directory. */