diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index db19cbe81..07372230e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -3,6 +3,7 @@ # Core sources set(SRB2_CORE_SOURCES am_map.c + args.c b_bot.c command.c comptime.c @@ -50,6 +51,7 @@ set(SRB2_CORE_SOURCES set(SRB2_CORE_HEADERS am_map.h + args.h b_bot.h byteptr.h command.h diff --git a/src/Makefile b/src/Makefile index 4fae86e1c..1ac5ab6d1 100644 --- a/src/Makefile +++ b/src/Makefile @@ -607,6 +607,7 @@ OBJS:=$(i_main_o) \ $(OBJDIR)/lzf.o \ $(OBJDIR)/vid_copy.o \ $(OBJDIR)/b_bot.o \ + $(OBJDIR)/args.o \ $(i_cdmus_o) \ $(i_net_o) \ $(i_system_o) \ diff --git a/src/args.c b/src/args.c new file mode 100644 index 000000000..465208e47 --- /dev/null +++ b/src/args.c @@ -0,0 +1,1309 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "args.h" +#include "command.h" +#include "m_misc.h" + + +/* ------------------ */ +/* Utility functions. */ +/* ------------------ */ + + +// Prints a message to the console marking it as an error. +static void err(const char* msg) { + CONS_Alert(CONS_ERROR, "%s\n", msg); +} + +// Duplicates a string, automatically allocating memory for the copy. +// Returns NULL if memory cannot be allocated for the copy. +static char* str_dup(const char* string) { + size_t len = strlen(string) + 1; + char *copy = malloc(len); + return copy ? memcpy(copy, string, len) : NULL; +} + + +// Hashes a string using the FNV-1a algorithm. +static uint32_t str_hash(const char* string) { + uint32_t hash = 2166136261u; + size_t length = strlen(string); + for (size_t i = 0; i < length; i++) { + hash ^= (uint8_t)string[i]; + hash *= 16777619; + } + return hash; +} + + +// Attempts to parse a string as an integer value, exiting on failure. +static int try_str_to_int(const char* string) { + char *endptr; + errno = 0; + long result = strtol(string, &endptr, 0); + if (errno == ERANGE || result > INT_MAX || result < INT_MIN) { + err(va("'%s' is out of range", string)); + } + if (*endptr != '\0') { + err(va("cannot parse '%s' as an integer", string)); + } + return (int) result; +} + + +// Attempts to parse a string as a double value, exiting on failure. +static double try_str_to_double(const char* string) { + char *endptr; + errno = 0; + double result = strtod(string, &endptr); + if (errno == ERANGE) { + err(va("'%s' is out of range", string)); + } + if (*endptr != '\0') { + err(va("cannot parse '%s' as a floating-point value", string)); + } + return result; +} + + +/* --------------------------------- */ +/* Vec: a dynamic array of pointers. */ +/* --------------------------------- */ + + +typedef struct { + int count; + int capacity; + void** entries; +} Vec; + + +static Vec* vec_new() { + Vec* vec = malloc(sizeof(Vec)); + if (!vec) { + return NULL; + } + vec->count = 0; + vec->capacity = 0; + vec->entries = NULL; + return vec; +} + + +static void vec_free(Vec* vec) { + if (vec) { + free(vec->entries); + free(vec); + } +} + + +static bool vec_add(Vec* vec, void* entry) { + if (vec->count + 1 > vec->capacity) { + int new_capacity = vec->capacity < 8 ? 8 : vec->capacity * 2; + void** new_array = realloc(vec->entries, sizeof(void*) * new_capacity); + if (!new_array) { + return false; + } + vec->entries = new_array; + vec->capacity = new_capacity; + } + vec->entries[vec->count] = entry; + vec->count++; + return true; +} + + +/* ------------------------------------------------------------------- */ +/* Map: a linear-probing hash map with string-keys and pointer-values. */ +/* ------------------------------------------------------------------- */ + + +// The map automatically grows to keep count/capacity < MAP_MAX_LOAD. +// This data structure performs optimally when it's less than half-full. +#define MAP_MAX_LOAD 0.5 + + +typedef struct { + char* key; + void* value; + uint32_t key_hash; +} MapEntry; + + +typedef struct { + int count; + int capacity; + int max_load_threshold; + MapEntry* entries; +} Map; + + +static Map* map_new() { + Map* map = malloc(sizeof(Map)); + if (!map) { + return NULL; + } + map->count = 0; + map->capacity = 0; + map->max_load_threshold = 0; + map->entries = NULL; + return map; +} + + +static void map_free(Map* map) { + if (map) { + for (int i = 0; i < map->capacity; i++) { + MapEntry* entry = &map->entries[i]; + if (entry->key != NULL) { + free(entry->key); + } + } + free(map->entries); + free(map); + } +} + + +static MapEntry* map_find(Map* map, const char* key, uint32_t key_hash) { + // Capacity is always a power of 2 so we can use bitwise-AND as a fast + // modulo operator, i.e. this is equivalent to: index = key_hash % capacity. + size_t index = key_hash & (map->capacity - 1); + + for (;;) { + MapEntry* entry = &map->entries[index]; + if (entry->key == NULL) { + return entry; + } else if (key_hash == entry->key_hash && strcmp(key, entry->key) == 0) { + return entry; + } + index = (index + 1) & (map->capacity - 1); + } +} + + +static bool map_grow(Map* map) { + MapEntry* old_entries = map->entries; + int old_capacity = map->capacity; + int new_capacity = old_capacity < 8 ? 8 : old_capacity * 2; + + MapEntry* new_entries = malloc(sizeof(MapEntry) * new_capacity); + if (!new_entries) { + return false; + } + + for (int i = 0; i < new_capacity; i++) { + new_entries[i].key = NULL; + } + + map->count = 0; + map->capacity = new_capacity; + map->max_load_threshold = new_capacity * MAP_MAX_LOAD; + map->entries = new_entries; + + for (int i = 0; i < old_capacity; i++) { + MapEntry* src = &old_entries[i]; + if (src->key == NULL) continue; + + MapEntry* dst = map_find(map, src->key, src->key_hash); + dst->key = src->key; + dst->value = src->value; + dst->key_hash = src->key_hash; + map->count++; + } + + free(old_entries); + return true; +} + + +// Returns true if the key was found. +static bool map_get(Map* map, const char* key, void** value) { + if (map->count == 0) return false; + + uint32_t key_hash = str_hash(key); + MapEntry* entry = map_find(map, key, key_hash); + if (entry->key == NULL) return false; + + *value = entry->value; + return true; +} + + +// Adds a new entry to the map or updates the value of an existing entry. +// (Note that the map stores its own internal copy of the key string.) +static bool map_set(Map* map, const char* key, void* value) { + if (map->count == map->max_load_threshold) { + if (!map_grow(map)) { + return false; + } + } + + uint32_t key_hash = str_hash(key); + MapEntry* entry = map_find(map, key, key_hash); + if (entry->key == NULL) { + char* key_copy = str_dup(key); + if (!key_copy) { + return false; + } + map->count++; + entry->key = key_copy; + entry->value = value; + entry->key_hash = key_hash; + } else { + entry->value = value; + } + + return true; +} + + +// Convenience wrapper for map_set(). This splits the key string into space- +// separated words and adds a separate entry to the map for each word. +static bool map_set_splitkey(Map* map, const char* key_string, void* value) { + char *copy_of_key_string = str_dup(key_string); + if (!copy_of_key_string) { + return false; + } + + char* word_start = copy_of_key_string; + char* string_end = word_start + strlen(copy_of_key_string); + + while (word_start < string_end) { + if (*word_start == ' ') { + word_start++; + continue; + } + + char* word_end = word_start; + while (true) { + if (*word_end == ' ' || *word_end == '\0') { + break; + } + word_end++; + } + *word_end = '\0'; + + if (!map_set(map, word_start, value)) { + free(copy_of_key_string); + return false; + } + + word_start = word_end + 1; + } + + free(copy_of_key_string); + return true; +} + + +/* -------- */ +/* Options. */ +/* -------- */ + + +typedef enum { + OPT_FLAG, + OPT_STR, + OPT_INT, + OPT_DBL, +} OptionType; + + +typedef union { + char* str_val; + int int_val; + double dbl_val; +} OptionValue; + + +typedef struct { + OptionType type; + int count; + int capacity; + OptionValue* values; + OptionValue fallback; +} Option; + + +static void option_free(Option* opt) { + if (opt) { + free(opt->values); + free(opt); + } +} + + +static bool option_append_value(Option* opt, OptionValue value) { + if (opt->count + 1 > opt->capacity) { + int new_capacity = opt->capacity < 4 ? 4 : opt->capacity * 2; + OptionValue* new_array = realloc(opt->values, sizeof(OptionValue) * new_capacity); + if (!new_array) { + return false; + } + opt->capacity = new_capacity; + opt->values = new_array; + } + opt->values[opt->count] = value; + opt->count++; + return true; +} + + +static bool option_try_set(Option* opt, char* arg) { + if (opt->type == OPT_STR) { + return option_append_value(opt, (OptionValue){.str_val = arg}); + } + else if (opt->type == OPT_INT) { + int value = try_str_to_int(arg); + return option_append_value(opt, (OptionValue){.int_val = value}); + } + else if (opt->type == OPT_DBL) { + double value = try_str_to_double(arg); + return option_append_value(opt, (OptionValue){.dbl_val = value}); + } + assert(false); + return false; +} + + +static Option* option_new() { + Option *option = malloc(sizeof(Option)); + if (!option) { + return NULL; + } + option->count = 0; + option->capacity = 0; + option->values = NULL; + return option; +} + + +static Option* option_new_flag() { + Option *opt = option_new(); + if (!opt) { + return NULL; + } + opt->type = OPT_FLAG; + return opt; +} + + +static Option* option_new_str(char* fallback) { + Option *opt = option_new(); + if (!opt) { + return NULL; + } + opt->type = OPT_STR; + opt->fallback = (OptionValue){.str_val = fallback}; + return opt; +} + + +static Option* option_new_int(int fallback) { + Option *opt = option_new(); + if (!opt) { + return NULL; + } + opt->type = OPT_INT; + opt->fallback = (OptionValue){.int_val = fallback}; + return opt; +} + + +static Option* option_new_double(double fallback) { + Option *opt = option_new(); + if (!opt) { + return NULL; + } + opt->type = OPT_DBL; + opt->fallback = (OptionValue){.dbl_val = fallback}; + return opt; +} + + +static char* option_get_str(Option* opt) { + if (opt->count > 0) { + return opt->values[opt->count - 1].str_val; + } + return opt->fallback.str_val; +} + + +static int option_get_int(Option* opt) { + if (opt->count > 0) { + return opt->values[opt->count - 1].int_val; + } + return opt->fallback.int_val; +} + + +static double option_get_double(Option* opt) { + if (opt->count > 0) { + return opt->values[opt->count - 1].dbl_val; + } + return opt->fallback.dbl_val; +} + + +// Returns the option's values as a freshly-allocated array of string pointers. +static char** option_get_str_list(Option* opt) { + if (opt->count == 0) { + return NULL; + } + char** list = malloc(sizeof(char*) * opt->count); + if (!list) { + return NULL; + } + for (int i = 0; i < opt->count; i++) { + list[i] = opt->values[i].str_val; + } + return list; +} + + +// Returns the option's values as a freshly-allocated array of integers. +static int* option_get_int_list(Option* opt) { + if (opt->count == 0) { + return NULL; + } + int* list = malloc(sizeof(int) * opt->count ); + if (!list) { + return NULL; + } + for (int i = 0; i < opt->count ; i++) { + list[i] = opt->values[i].int_val; + } + return list; +} + + +// Returns the option's values as a freshly-allocated array of doubles. +static double* option_get_double_list(Option* opt) { + if (opt->count == 0) { + return NULL; + } + double* list = malloc(sizeof(double) * opt->count ); + if (!list) { + return NULL; + } + for (int i = 0; i < opt->count ; i++) { + list[i] = opt->values[i].dbl_val; + } + return list; +} + + +// Returns a freshly-allocated state-string for debugging. +static char* option_to_str(Option* opt) { + if (opt->type == OPT_FLAG) { + return va("%i", opt->count); + } + + char *fallback = NULL; + if (opt->type == OPT_STR) { + fallback = str_dup(opt->fallback.str_val); + } else if (opt->type == OPT_INT) { + fallback = va("%i", opt->fallback.int_val); + } else if (opt->type == OPT_DBL) { + fallback = va("%f", opt->fallback.dbl_val); + } + + char *values = str_dup(""); + for (int i = 0; i < opt->count; i++) { + char *value = NULL; + if (opt->type == OPT_STR) { + value = str_dup(opt->values[i].str_val); + } else if (opt->type == OPT_INT) { + value = va("%i", opt->values[i].int_val); + } else if (opt->type == OPT_DBL) { + value = va("%f", opt->values[i].dbl_val); + } + char *old_values = values; + if (i == 0) { + values = str_dup(value); + } else { + values = va("%s, %s", old_values, value); + } + free(old_values); + free(value); + } + + char *output = va("(%s) [%s]", fallback, values); + free(fallback); + free(values); + return output; +} + + +/* ----------------------------------------------------- */ +/* ArgStream: a wrapper for an array of string pointers. */ +/* ----------------------------------------------------- */ + + +typedef struct ArgStream { + int count; + int index; + char** args; +} ArgStream; + + +static ArgStream* argstream_new(int count, char** args) { + ArgStream* stream = malloc(sizeof(ArgStream)); + if (!stream) { + return NULL; + } + stream->count = count; + stream->index = 0; + stream->args = args; + return stream; +} + + +static void argstream_free(ArgStream* stream) { + free(stream); +} + + +static char* argstream_next(ArgStream* stream) { + return stream->args[stream->index++]; +} + + +static bool argstream_has_next(ArgStream* stream) { + return stream->index < stream->count; +} + + +/* ----------------- */ +/* ArgParser: setup. */ +/* ----------------- */ + + +struct ArgParser { + char* helptext; + char* version; + Vec* option_vec; + Map* option_map; + Vec* command_vec; + Map* command_map; + Vec* positional_args; + ap_callback_t callback; + char* cmd_name; + struct ArgParser* cmd_parser; + bool enable_help_command; + bool had_memory_error; + struct ArgParser* parent; + bool first_positional_arg_ends_options; +}; + + +ArgParser* ap_new() { + ArgParser *parser = malloc(sizeof(ArgParser)); + if (!parser) { + return NULL; + } + + parser->helptext = NULL; + parser->version = NULL; + parser->callback = NULL; + parser->cmd_name = NULL; + parser->cmd_parser = NULL; + parser->enable_help_command = false; + parser->had_memory_error = false; + parser->parent = NULL; + parser->first_positional_arg_ends_options = false; + + parser->option_vec = NULL; + parser->option_map = NULL; + parser->command_vec = NULL; + parser->command_map = NULL; + parser->positional_args = NULL; + + parser->option_vec = vec_new(); + if (!parser->option_vec) { + ap_free(parser); + return NULL; + } + + parser->option_map = map_new(); + if (!parser->option_map) { + ap_free(parser); + return NULL; + } + + parser->command_vec = vec_new(); + if (!parser->command_vec) { + ap_free(parser); + return NULL; + } + + parser->command_map = map_new(); + if (!parser->command_map) { + ap_free(parser); + return NULL; + } + + parser->positional_args = vec_new(); + if (!parser->positional_args) { + ap_free(parser); + return NULL; + } + + return parser; +} + + +void ap_free(ArgParser* parser) { + if (!parser) { + return; + } + + free(parser->helptext); + free(parser->version); + + if (parser->option_map) { + map_free(parser->option_map); + } + + if (parser->option_vec) { + for (int i = 0; i < parser->option_vec->count; i++) { + option_free(parser->option_vec->entries[i]); + } + vec_free(parser->option_vec); + } + + if (parser->command_map) { + map_free(parser->command_map); + } + + if (parser->command_vec) { + for (int i = 0; i < parser->command_vec->count; i++) { + ap_free(parser->command_vec->entries[i]); + } + vec_free(parser->command_vec); + } + + if (parser->positional_args) { + vec_free(parser->positional_args); + } + + free(parser); +} + + +static void ap_set_memory_error_flag(ArgParser* parser) { + parser->had_memory_error = true; + + ArgParser* parent = parser->parent; + while (parent) { + parent->had_memory_error = true; + parent = parent->parent; + } +} + + +void ap_set_helptext(ArgParser* parser, const char* helptext) { + free(parser->helptext); + parser->helptext = NULL; + + if (helptext) { + parser->helptext = str_dup(helptext); + if (!parser->helptext) { + ap_set_memory_error_flag(parser); + } + } +} + + +char* ap_get_helptext(ArgParser* parser) { + return parser->helptext; +} + + +void ap_set_version(ArgParser* parser, const char* version) { + free(parser->version); + parser->version = NULL; + + if (version) { + parser->version = str_dup(version); + if (!parser->version) { + ap_set_memory_error_flag(parser); + } + } +} + + +char* ap_get_version(ArgParser* parser) { + return parser->version; +} + + +void ap_first_pos_arg_ends_options(ArgParser* parser, bool enable) { + parser->first_positional_arg_ends_options = enable; +} + + +/* -------------------------------------- */ +/* ArgParser: register flags and options. */ +/* -------------------------------------- */ + + +static void ap_register_option(ArgParser* parser, const char* name, Option* opt) { + if (!opt) { + ap_set_memory_error_flag(parser); + return; + } + + if (vec_add(parser->option_vec, opt)) { + if (map_set_splitkey(parser->option_map, name, opt)) { + return; + } else { + ap_set_memory_error_flag(parser); + parser->option_vec->count--; + option_free(opt); + return; + } + } else { + ap_set_memory_error_flag(parser); + option_free(opt); + return; + } +} + + +// Register a new flag. +void ap_flag(ArgParser *parser, const char* name) { + Option* opt = option_new_flag(); + ap_register_option(parser, name, opt); +} + + +// Register a new string-valued option. +void ap_str_opt(ArgParser* parser, const char* name, const char* fallback) { + Option* opt = option_new_str((char*)fallback); + ap_register_option(parser, name, opt); +} + + +// Register a new integer-valued option. +void ap_int_opt(ArgParser* parser, const char* name, int fallback) { + Option* opt = option_new_int(fallback); + ap_register_option(parser, name, opt); +} + + +// Register a new double-valued option. +void ap_dbl_opt(ArgParser* parser, const char* name, double fallback) { + Option* opt = option_new_double(fallback); + ap_register_option(parser, name, opt); +} + + +/* ---------------------------------- */ +/* ArgParser: flag and option values. */ +/* ---------------------------------- */ + + +// Retrieve an Option instance by name. +static Option* ap_get_opt(ArgParser* parser, const char* name) { + void* opt; + if (!map_get(parser->option_map, name, &opt)) { + err(va("'%s' is not a registered flag or option name", name)); + } + return (Option*)opt; +} + + +// Returns the number of times the specified flag or option was found. +int ap_count(ArgParser* parser, const char* name) { + Option* opt = ap_get_opt(parser, name); + return opt->count; +} + + +// Returns true if the specified flag or option was found. +bool ap_found(ArgParser* parser, const char* name) { + Option* opt = ap_get_opt(parser, name); + return opt->count > 0; +} + + +// Returns the value of the specified string option. +char* ap_str_value(ArgParser* parser, const char* name) { + Option* opt = ap_get_opt(parser, name); + return (char*)option_get_str(opt); +} + + +// Returns the string value at the specified index. +char* ap_str_value_at_index(ArgParser* parser, const char* name, int index) { + Option* opt = ap_get_opt(parser, name); + return opt->values[index].str_val; +} + + +// Returns the value of the specified integer option. +int ap_int_value(ArgParser* parser, const char* name) { + Option* opt = ap_get_opt(parser, name); + return option_get_int(opt); +} + + +// Returns the integer value at the specified index. +int ap_int_value_at_index(ArgParser* parser, const char* name, int index) { + Option* opt = ap_get_opt(parser, name); + return opt->values[index].int_val; +} + + +// Returns the value of the specified floating-point option. +double ap_dbl_value(ArgParser* parser, const char* name) { + Option* opt = ap_get_opt(parser, name); + return option_get_double(opt); +} + + +// Returns the floating-point value at the specified index. +double ap_dbl_value_at_index(ArgParser* parser, const char* name, int index) { + Option* opt = ap_get_opt(parser, name); + return opt->values[index].dbl_val; +} + + +// Returns an option's values as a freshly-allocated array of string pointers. +// The array's memory is not affected by calls to ap_free(). +// Returns NULL if memory cannot be allocated for the array. +char** ap_str_values(ArgParser* parser, const char* name) { + Option* opt = ap_get_opt(parser, name); + return (char**)option_get_str_list(opt); +} + + +// Returns an option's values as a freshly-allocated array of integers. The +// array's memory is not affected by calls to ap_free(). +// Returns NULL if memory cannot be allocated for the array. +int* ap_int_values(ArgParser* parser, const char* name) { + Option* opt = ap_get_opt(parser, name); + return option_get_int_list(opt); +} + + +// Returns an option's values as a freshly-allocated array of doubles. The +// array's memory is not affected by calls to ap_free(). +// Returns NULL if memory cannot be allocated for the array. +double* ap_dbl_values(ArgParser* parser, const char* name) { + Option* opt = ap_get_opt(parser, name); + return option_get_double_list(opt); +} + + +/* -------------------------------- */ +/* ArgParser: positional arguments. */ +/* -------------------------------- */ + + +// Returns true if the parser has found one or more positional arguments. +bool ap_has_args(ArgParser* parser) { + return parser->positional_args->count > 0; +} + + +// Returns the number of positional arguments. +int ap_count_args(ArgParser* parser) { + return parser->positional_args->count; +} + + +// Returns the positional argument at the specified index. +char* ap_arg(ArgParser* parser, int index) { + return (char*)parser->positional_args->entries[index]; +} + + +// Returns the positional arguments as a freshly-allocated array of string +// pointers. The memory occupied by the returned array is not affected by +// calls to ap_free(). +// Returns NULL if memory cannot be allocated for the array. +char** ap_args(ArgParser* parser) { + int count = ap_count_args(parser); + char** args = malloc(sizeof(char*) * count); + if (!args) { + return NULL; + } + memcpy(args, parser->positional_args->entries, sizeof(char*) * count); + return args; +} + + +// Attempts to parse and return the positional arguments as a freshly +// allocated array of integers. Exits with an error message on failure. The +// memory occupied by the returned array is not affected by calls to +// ap_free(). +// Returns NULL if memory cannot be allocated for the array. +int* ap_args_as_ints(ArgParser* parser) { + int count = ap_count_args(parser); + int* args = malloc(sizeof(int) * count); + if (!args) { + return NULL; + } + for (int i = 0; i < count; i++) { + *(args + i) = try_str_to_int(parser->positional_args->entries[i]); + } + return args; +} + + +// Attempts to parse and return the positional arguments as a freshly +// allocated array of doubles. Exits with an error message on failure. The +// memory occupied by the returned array is not affected by calls to +// ap_free(). +// Returns NULL if memory cannot be allocated for the array. +double* ap_args_as_doubles(ArgParser* parser) { + int count = ap_count_args(parser); + double *args = malloc(sizeof(double) * count); + if (!args) { + return NULL; + } + for (int i = 0; i < count; i++) { + *(args + i) = try_str_to_double(parser->positional_args->entries[i]); + } + return args; +} + + +/* -------------------- */ +/* ArgParser: commands. */ +/* -------------------- */ + + +// Register a new command. +ArgParser* ap_cmd(ArgParser* parser, const char* name) { + ArgParser* cmd_parser = ap_new(); + if (!cmd_parser) { + return NULL; + } + + if (vec_add(parser->command_vec, cmd_parser)) { + if (map_set_splitkey(parser->command_map, name, cmd_parser)) { + parser->enable_help_command = true; + return cmd_parser; + } else { + parser->command_vec--; + ap_free(cmd_parser); + return NULL; + } + } else { + ap_free(cmd_parser); + return NULL; + } +} + + +// Register a callback function for a command. +void ap_callback(ArgParser* parser, ap_callback_t function) { + parser->callback = function; +} + + +// Returns true if the parser has found a command. +bool ap_has_cmd(ArgParser* parser) { + return parser->cmd_name != NULL; +} + + +// Returns the command name, if the parser has found a command. +char* ap_cmd_name(ArgParser* parser) { + return parser->cmd_name; +} + + +// Returns the command's parser instance, if the parser has found a command. +ArgParser* ap_cmd_parser(ArgParser* parser) { + return parser->cmd_parser; +} + + +// Toggles support for the automatic 'help' command. +void ap_enable_help_command(ArgParser* parser, bool enable) { + parser->enable_help_command = enable; +} + + +/* --------------------------- */ +/* ArgParser: parse arguments. */ +/* --------------------------- */ + + +// Parse an option of the form --name=value or -n=value. +static void ap_handle_equals_opt(ArgParser* parser, const char* prefix, const char* arg) { + char* name = str_dup(arg); + if (!name) { + ap_set_memory_error_flag(parser); + return; + } + + *strchr(name, '=') = '\0'; + char *value = strchr(arg, '=') + 1; + + Option* option; + bool found = map_get(parser->option_map, name, (void**)&option); + + if (!found || option->type == OPT_FLAG) { + err(va("%s%s is not a recognised option name", prefix, name)); + } + + if (strlen(value) == 0) { + err(va("missing argument for %s%s", prefix, name)); + } + + if (!option_try_set(option, value)) { + ap_set_memory_error_flag(parser); + } + + free(name); +} + +// Parse a long-form option, i.e. an option beginning with a double dash. +static void ap_handle_long_opt(ArgParser* parser, const char* arg, ArgStream* stream) { + Option* option; + if (map_get(parser->option_map, arg, (void**)&option)) { + if (option->type == OPT_FLAG) { + option->count++; + } else if (argstream_has_next(stream)) { + if (!option_try_set(option, argstream_next(stream))) { + ap_set_memory_error_flag(parser); + } + } else { + err(va("missing argument for --%s", arg)); + } + } else if (strcmp(arg, "help") == 0 && parser->helptext != NULL) { + CONS_Printf("%s\n", parser->helptext); + } else if (strcmp(arg, "version") == 0 && parser->version != NULL) { + CONS_Printf("%s\n", parser->version); + } else { + err(va("--%s is not a recognised flag or option name", arg)); + } +} + + +// Parse a short-form option, i.e. an option beginning with a single dash. +static void ap_handle_short_opt(ArgParser* parser, const char* arg, ArgStream* stream) { + for (size_t i = 0; i < strlen(arg); i++) { + char keystr[] = {arg[i], 0}; + Option* option; + bool found = map_get(parser->option_map, keystr, (void**)&option); + if (!found) { + if (arg[i] == 'h' && parser->helptext != NULL) { + CONS_Printf("%s\n", parser->helptext); + } else if (arg[i] == 'v' && parser->version != NULL) { + CONS_Printf("%s\n", parser->version); + } else if (strlen(arg) > 1) { + err(va("'%c' in -%s is not a recognised flag or option name", arg[i], arg)); + } else { + err(va("-%s is not a recognised flag or option name", arg)); + } + } else if (option->type == OPT_FLAG) { + option->count++; + } else if (argstream_has_next(stream)) { + if (!option_try_set(option, argstream_next(stream))) { + ap_set_memory_error_flag(parser); + } + } else if (strlen(arg) > 1) { + err(va("missing argument for '%c' in -%s", arg[i], arg)); + } else { + err(va("missing argument for -%s", arg)); + } + } +} + + +// Parse a stream of string arguments. +static void ap_parse_stream(ArgParser* parser, ArgStream* stream) { + if (parser->had_memory_error) { + return; + } + + ArgParser* cmd_parser; + bool is_first_arg = true; + + while (argstream_has_next(stream)) { + char* arg = argstream_next(stream); + + // If we encounter a '--' argument, turn off option-parsing. + if (strcmp(arg, "--") == 0) { + while (argstream_has_next(stream)) { + if (!vec_add(parser->positional_args, argstream_next(stream))) { + ap_set_memory_error_flag(parser); + } + } + } + + // Is the argument a long-form option or flag? + else if (strncmp(arg, "--", 2) == 0) { + if (strstr(arg, "=") != NULL) { + ap_handle_equals_opt(parser, "--", arg + 2); + } else { + ap_handle_long_opt(parser, arg + 2, stream); + } + } + + // Is the argument a short-form option or flag? + else if (arg[0] == '-') { + if (strlen(arg) == 1 || isdigit(arg[1])) { + if (!vec_add(parser->positional_args, arg)) { + ap_set_memory_error_flag(parser); + } + } else if (strstr(arg, "=") != NULL) { + ap_handle_equals_opt(parser, "-", arg + 1); + } else { + ap_handle_short_opt(parser, arg + 1, stream); + } + } + + // Is the argument a registered command? + else if (is_first_arg && map_get(parser->command_map, arg, (void**)&cmd_parser)) { + parser->cmd_name = arg; + parser->cmd_parser = cmd_parser; + ap_parse_stream(cmd_parser, stream); + if (cmd_parser->callback && !parser->had_memory_error) { + cmd_parser->callback(arg, cmd_parser); + } + } + + // Is the argument the automatic 'help' command? + else if (is_first_arg && parser->enable_help_command && strcmp(arg, "help") == 0) { + if (argstream_has_next(stream)) { + char* name = argstream_next(stream); + if (map_get(parser->command_map, name, (void**)&cmd_parser)) { + if (cmd_parser->helptext) { + CONS_Printf("%s\n", cmd_parser->helptext); + } + } else { + err(va("'%s' is not a recognised command", name)); + } + } else { + err(va("the help command requires an argument")); + } + } + + // Otherwise add the argument to our list of positionals. + else { + if (!vec_add(parser->positional_args, arg)) { + ap_set_memory_error_flag(parser); + } + if (parser->first_positional_arg_ends_options) { + while (argstream_has_next(stream)) { + if (!vec_add(parser->positional_args, argstream_next(stream))) { + ap_set_memory_error_flag(parser); + } + } + } + } + + is_first_arg = false; + } +} + + +// Parse an array of string arguments. +static bool ap_parse_array(ArgParser* parser, int count, char* args[]) { + if (parser->had_memory_error) { + return false; + } + + ArgStream *stream = argstream_new(count, args); + if (!stream) { + return false; + } + + ap_parse_stream(parser, stream); + argstream_free(stream); + + return !parser->had_memory_error; +} + + +// This function parses the application's command line arguments. It assumes that [argc] and [argv] +// are the arguments passed to main(), i.e. that the first element in [argv] is the program's name +// and should be ignored when parsing the actual *arguments*. In some situations [argv] can be +// empty, i.e. [argc == 0], which can lead to security vulnerabilities if not explicitly handled. +bool ap_parse(ArgParser* parser, int argc, char* argv[]) { + if (argc > 1) { + return ap_parse_array(parser, argc - 1, argv + 1); + } + return !parser->had_memory_error; +} + + +/* --------------------- */ +/* ArgParser: utilities. */ +/* --------------------- */ + + +bool ap_had_memory_error(ArgParser* parser) { + return parser->had_memory_error; +} + + +void ap_print(ArgParser* parser) { + CONS_Printf("%s\n", "Flags/Options:"); + if (parser->option_map->count > 0) { + for (int i = 0; i < parser->option_map->capacity; i++) { + MapEntry* entry = &parser->option_map->entries[i]; + if (entry->key != NULL) { + Option* opt = entry->value; + char* opt_str = option_to_str(opt); + CONS_Printf("%s\n", entry->key, opt_str); + free(opt_str); + } + } + } else { + CONS_Printf("%s\n", " [none]"); + } + + CONS_Printf("%s\n", "\nArguments:"); + if (parser->positional_args->count > 0) { + for (int i = 0; i < parser->positional_args->count; i++) { + CONS_Printf("%s\n", ap_arg(parser, i)); + } + } else { + CONS_Printf("%s\n", " [none]"); + } + + CONS_Printf("%s\n", "\nCommand:"); + if (ap_has_cmd(parser)) { + CONS_Printf("%s\n", ap_cmd_name(parser)); + } else { + CONS_Printf("%s\n", " [none]"); + } +} + + +/* --------------------- */ +/* Deprecated functions. */ +/* --------------------- */ + + +void ap_cmd_help(ArgParser* parser, bool enable) { + ap_enable_help_command(parser, enable); +} + + +void ap_helptext(ArgParser* parser, const char* helptext) { + ap_set_helptext(parser, helptext); +} + + +void ap_version(ArgParser* parser, const char* version) { + ap_set_version(parser, version); +} diff --git a/src/args.h b/src/args.h new file mode 100644 index 000000000..39cd57b5c --- /dev/null +++ b/src/args.h @@ -0,0 +1,206 @@ +// ----------------------------------------------------------------------------- +// Args: a C99 library for parsing command line arguments. +// Version: 2.8.0 +// Notes: This has been edited to work properly within SRB2. +// Licensed under the 0BSD license. +// ----------------------------------------------------------------------------- + +#ifndef args_h +#define args_h + +#include + +// ----------------------------------------------------------------------------- +// Types. +// ----------------------------------------------------------------------------- + +// An ArgParser instance stores registered flags, options and commands. +typedef struct ArgParser ArgParser; + +// A command callback function should accept two arguments: the command's name +// and the command's ArgParser instance. +typedef void (*ap_callback_t)(char* cmd_name, ArgParser* cmd_parser); + +// ----------------------------------------------------------------------------- +// Initialization, parsing, teardown. +// ----------------------------------------------------------------------------- + +// Initializes a new ArgParser instance. Returns NULL if sufficient memory +// cannot be allocated for the new parser. +ArgParser* ap_new(); + +// Specifies a helptext string for the parser. If [helptext] is not NULL, this +// activates an automatic --help/-h flag. (Either --help or -h can be overridden +// by explicitly registered flags.) The parser stores and manages its own copy +// of the [helptext] string so [helptext] can be freed immediately after this +// call if it was dynamically constructed. +void ap_set_helptext(ArgParser* parser, const char* helptext); + +// Returns a pointer to the parser's helptext string. +char* ap_get_helptext(ArgParser* parser); + +// Specifies a version string for the parser. If [version] is not NULL, this +// activates an automatic --version/-v flag. (Either --version or -v can be +// overridden by explicitly registered flags.) The parser stores and manages its +// own copy of the [version] string so [version] can be freed immediately after +// this call if it was dynamically constructed. +void ap_set_version(ArgParser* parser, const char* version); + +// Returns a pointer to the parser's version string. +char* ap_get_version(ArgParser* parser); + +// If toggled to true, the first positional argument ends option parsing; all +// subsequent arguments will be treated as positionals. Defaults to false. +void ap_first_pos_arg_ends_options(ArgParser* parser, bool enable); + +// Parses the application's command line arguments. The parameters are assumed +// to be [argc] and [argv] as supplied to main(), i.e. the first element of the +// array is assumed to be the binary name and is therefore ignored. Returns true +// if the arguments were successfully parsed. Returns false if parsing failed +// due to a memory allocation error. (This memory allocation error may have +// occured during set-up rather than during parsing.) +bool ap_parse(ArgParser* parser, int argc, char** argv); + +// Frees the memory associated with the parser and any subparsers. +void ap_free(ArgParser* parser); + +// ----------------------------------------------------------------------------- +// Register flags and options. +// ----------------------------------------------------------------------------- + +// Registers a new flag. +void ap_flag(ArgParser* parser, const char* name); + +// Registers a new string-valued option. +void ap_str_opt(ArgParser* parser, const char* name, const char* fallback); + +// Registers a new integer-valued option. +void ap_int_opt(ArgParser* parser, const char* name, int fallback); + +// Registers a new double-valued option. +void ap_dbl_opt(ArgParser* parser, const char* name, double fallback); + +// ----------------------------------------------------------------------------- +// Inspect flags and options. +// ----------------------------------------------------------------------------- + +// Returns the number of times the specified flag or option was found. +int ap_count(ArgParser* parser, const char* name); + +// Returns true if the specified flag or option was found. +bool ap_found(ArgParser* parser, const char* name); + +// Returns the value of a string option. +char* ap_str_value(ArgParser* parser, const char* name); + +// Returns the string value at the specified index. +char* ap_str_value_at_index(ArgParser* parser, const char* name, int index); + +// Returns the value of an integer option. +int ap_int_value(ArgParser* parser, const char* name); + +// Returns the integer value at the specified index. +int ap_int_value_at_index(ArgParser* parser, const char* name, int index); + +// Returns the value of a floating-point option. +double ap_dbl_value(ArgParser* parser, const char* name); + +// Returns the floating-point value at the specified index. +double ap_dbl_value_at_index(ArgParser* parser, const char* name, int index); + +// Returns an option's values as a freshly-allocated array of string +// pointers. The array's memory is not affected by calls to ap_free(). +// Returns NULL if memory allocation fails. +char** ap_str_values(ArgParser* parser, const char* name); + +// Returns an option's values as a freshly-allocated array of integers. +// The array's memory is not affected by calls to ap_free(). +// Returns NULL if memory allocation fails. +int* ap_int_values(ArgParser* parser, const char* name); + +// Returns an option's values as a freshly-allocated array of doubles. +// The array's memory is not affected by calls to ap_free(). +// Returns NULL if memory allocation fails. +double* ap_dbl_values(ArgParser* parser, const char* name); + +// ----------------------------------------------------------------------------- +// Positional arguments. +// ----------------------------------------------------------------------------- + +// Returns true if the parser has found one or more positional arguments. +bool ap_has_args(ArgParser* parser); + +// Returns the number of positional arguments. +int ap_count_args(ArgParser* parser); + +// Returns the positional argument at the specified index. +char* ap_arg(ArgParser* parser, int index); + +// Returns the positional arguments as a freshly-allocated array of string +// pointers. The memory occupied by the returned array is not affected by +// calls to ap_free(). Returns NULL if memory allocation fails. +char** ap_args(ArgParser* parser); + +// Attempts to parse and return the positional arguments as a freshly allocated +// array of integers. Exits with an error message on failure. The memory +// occupied by the returned array is not affected by calls to ap_free(). +// Returns NULL if memory allocation fails. +int* ap_args_as_ints(ArgParser* parser); + +// Attempts to parse and return the positional arguments as a freshly allocated +// array of doubles. Exits with an error message on failure. The memory +// occupied by the returned array is not affected by calls to ap_free(). +// Returns NULL if memory allocation fails. +double* ap_args_as_doubles(ArgParser* parser); + +// ----------------------------------------------------------------------------- +// Commands. +// ----------------------------------------------------------------------------- + +// Registers a new command. Returns the ArgParser instance for the command. +// Returns NULL if sufficient memory cannot be allocated for the new parser. +ArgParser* ap_cmd(ArgParser* parser, const char* name); + +// Registers a callback function on a command parser. The function will be +// called if the command is found. +void ap_callback(ArgParser* parser, ap_callback_t function); + +// Returns true if the parser has found a command. +bool ap_has_cmd(ArgParser* parser); + +// Returns the command name, if the parser has found a command. +char* ap_cmd_name(ArgParser* parser); + +// Returns the command's parser instance, if the parser has found a command. +ArgParser* ap_cmd_parser(ArgParser* parser); + +// This boolean switch toggles support for an automatic 'help' command that +// prints subcommand helptext. The value defaults to false but gets toggled +// automatically to true whenever a command is registered. You can use this +// function to disable the feature if required. +void ap_enable_help_command(ArgParser* parser, bool enable); + +// ----------------------------------------------------------------------------- +// Debugging utilities. +// ----------------------------------------------------------------------------- + +// Dump a parser instance for debugging. +void ap_print(ArgParser* parser); + +// True if an attempt to allocate memory failed. +bool ap_had_memory_error(ArgParser* parser); + +// ----------------------------------------------------------------------------- +// Deprecated. +// ----------------------------------------------------------------------------- + +// Replaced by ap_enable_help_command(). +void ap_cmd_help(ArgParser* parser, bool enable); + +// Replaced by ap_set_helptext(). +void ap_helptext(ArgParser* parser, const char* helptext); + +// Replaced by ap_set_version(). +void ap_version(ArgParser* parser, const char* version); + +#endif diff --git a/src/command.c b/src/command.c index ff2ff7b13..a7b8dee4f 100644 --- a/src/command.c +++ b/src/command.c @@ -278,7 +278,7 @@ static xcommand_t *com_commands = NULL; // current commands #define MAX_ARGS 80 static size_t com_argc; -static char *com_argv[MAX_ARGS]; +char *com_argv[MAX_ARGS]; static const char *com_null_string = ""; static char *com_args = NULL; // current command args or NULL @@ -328,6 +328,16 @@ const char *COM_Argv(size_t arg) return com_argv[arg]; } +/** Returns all of the console command arguments + * + * \return String pointer to the argv table. + * \sa COM_ArgvList + */ +char **COM_ArgvList(void) +{ + return com_argv; +} + /** Gets all console command arguments. * * \return String pointer to all arguments for the last command. diff --git a/src/command.h b/src/command.h index 55b31217d..73fb145a6 100644 --- a/src/command.h +++ b/src/command.h @@ -33,8 +33,12 @@ typedef void (*com_func_t)(void); void COM_AddCommand(const char *name, com_func_t func); int COM_AddLuaCommand(const char *name); +#define MAX_ARGS 80 +extern char *com_argv[MAX_ARGS]; + size_t COM_Argc(void); const char *COM_Argv(size_t arg); // if argv > argc, returns empty string +char **COM_ArgvList(void); char *COM_Args(void); size_t COM_CheckParm(const char *check); // like M_CheckParm :) size_t COM_CheckPartialParm(const char *check); diff --git a/src/d_netcmd.c b/src/d_netcmd.c index 538d7612a..d554c344b 100644 --- a/src/d_netcmd.c +++ b/src/d_netcmd.c @@ -141,11 +141,7 @@ static void Command_SetViews_f(void); static void Command_Addfile(void); static void Command_Addskins(void); -static void Command_Localskin1(void); -static void Command_Localskin2(void); -static void Command_Localskin3(void); -static void Command_Localskin4(void); -static void Command_LocalskinAll(void); +static void Command_GLocalSkin(void); static void Command_ListWADS_f(void); #ifdef DELFILE static void Command_Delfile(void); @@ -628,11 +624,7 @@ void D_RegisterServerCommands(void) COM_AddCommand("addfile", Command_Addfile); COM_AddCommand("addskins", Command_Addskins); - COM_AddCommand("localskin", Command_Localskin1); - COM_AddCommand("localskin2", Command_Localskin2); - COM_AddCommand("localskin3", Command_Localskin3); - COM_AddCommand("localskin4", Command_Localskin4); - COM_AddCommand("localskinall", Command_LocalskinAll); + COM_AddCommand("localskin", Command_GLocalSkin); COM_AddCommand("listwad", Command_ListWADS_f); #ifdef DELFILE @@ -4534,81 +4526,56 @@ Command_Addskins (void) } */ } +// args n stuff +#include "args.h" -static void Command_Localskin1 (void) { - if (!Playing() && !demo.playback) - { - CONS_Printf("You can only use this command in gameplay.\n"); - return; - } - if (COM_Argc() > 3) - { - CONS_Printf("localskin : Set a localskin.\n"); - return; - } - if (fasticmp(COM_Argv(2), "-display") || fasticmp(COM_Argv(2), "-d")) - SetLocalPlayerSkin(displayplayers[0], COM_Argv(1), &cv_localskin); - else - SetLocalPlayerSkin(consoleplayer, COM_Argv(1), &cv_localskin); -} +static void Command_GLocalSkin (void) { + ArgParser* parser = ap_new(); + ap_set_helptext(parser, "Usage: localskin [options] \n\nOptions: \n\ + -d, --display: Use the first displaying player instead of yourself.\n\ + Has no effect on replays. Omits any player related\n\ + options.\n\ + -a, --all: Set a localskin to all players. Omits any player related\n\ + options.\n\ + -p, --player: Set a localskin to a specific player name.\n\ + If omitted, defaults to the client player, unless -d \n\ + or --display is added."); + ap_set_version(parser, ""); + ap_str_opt(parser, "player p", player_names[consoleplayer]); + ap_flag(parser, "display d"); + ap_flag(parser, "all a"); -static void Command_Localskin2 (void) { - if (!Playing() && !demo.playback) - { - CONS_Printf("You can only use this command in gameplay.\n"); - return; - } - if (COM_Argc() != 2) - { - CONS_Printf("localskin2 : Set a localskin.\n"); - return; - } - SetLocalPlayerSkin(displayplayers[1], COM_Argv(1), &cv_localskin2); -} + ap_parse(parser, COM_Argc(), COM_ArgvList()); -static void Command_Localskin3 (void) { - if (!Playing() && !demo.playback) - { - CONS_Printf("You can only use this command in gameplay.\n"); - return; - } - if (COM_Argc() != 2) - { - CONS_Printf("localskin3 : Set a localskin.\n"); - return; - } - SetLocalPlayerSkin(displayplayers[2], COM_Argv(1), &cv_localskin3); -} + boolean disply; + boolean allp; -static void Command_Localskin4 (void) { - if (!Playing() && !demo.playback) - { - CONS_Printf("You can only use this command in gameplay.\n"); - return; - } - if (COM_Argc() != 2) - { - CONS_Printf("localskin4 : Set a localskin.\n"); - return; - } - SetLocalPlayerSkin(displayplayers[3], COM_Argv(1), &cv_localskin4); -} + if (ap_found(parser, "display")) disply = true; else disply = false; + if (ap_found(parser, "all")) allp = true; else allp = false; -static void Command_LocalskinAll (void) { - if (!Playing() && !demo.playback) - { - CONS_Printf("You can only use this command in gameplay.\n"); - return; - } - if (COM_Argc() != 2) - { - CONS_Printf("localskinall : Set a localskin to all players.\n"); - return; - } - INT32 i; - for (i; i < MAXPLAYERS; i++) { - SetLocalPlayerSkin(i, COM_Argv(1), &cv_localskinall); + INT32 i, idx; + i = 0; + idx = 0; + + for (idx = 0; idx < ap_count_args(parser); idx++) { + if (disply) { + SetLocalPlayerSkin(displayplayers[0], ap_arg(parser, idx), &cv_localskin); + return; + } else { + for (i = 0; i < MAXPLAYERS; i++) { + if (allp) { + SetLocalPlayerSkin(i, ap_arg(parser, idx), &cv_localskinall); + } else { + if (fasticmp(player_names[i], ap_str_value(parser, "player"))) { + SetLocalPlayerSkin(i, ap_arg(parser, idx), &cv_localskin); + break; + } + } + } + } } + + ap_free(parser); } #ifdef DELFILE diff --git a/src/p_enemy.c b/src/p_enemy.c index 42b4006e3..daf94e935 100644 --- a/src/p_enemy.c +++ b/src/p_enemy.c @@ -4169,6 +4169,12 @@ void A_SignPlayer(mobj_t *actor) // same thing happens on p_mobj.c ov->localskin = &localskins[actor->target->player->localskin - 1]; ov->skinlocal = actor->target->skinlocal; + } else { + // needs - 1 or else it pukes an error out + // same thing happens on p_mobj.c + if (actor->target->player->localskin) + ov->localskin = &skins[actor->target->player->localskin - 1]; + ov->skinlocal = actor->target->skinlocal; } P_SetMobjState(ov, actor->info->seestate); // S_PLAY_SIGN } diff --git a/src/r_things.c b/src/r_things.c index 51e559cce..0a22f1322 100644 --- a/src/r_things.c +++ b/src/r_things.c @@ -2727,7 +2727,9 @@ void R_InitSkins(void) strncpy(skin->facerank, "PLAYRANK", 9); strncpy(skin->facewant, "PLAYWANT", 9); strncpy(skin->facemmap, "PLAYMMAP", 9); + skin->wadnum = 0; // god what have you brought to this world skin->prefcolor = SKINCOLOR_BLUE; + skin->localskin = false; // SRB2kart skin->kartspeed = 8; @@ -2877,10 +2879,12 @@ void SetLocalPlayerSkin(INT32 playernum, const char *skinname, consvar_t *cvar) } } + /* if (player->localskin > 0) CV_StealthSet(cvar, ( (player->skinlocal) ? localskins : skins )[player->localskin - 1].name); else CV_StealthSet(cvar, "none"); + */ } // Same as SetPlayerSkin, but uses the skin #. @@ -3337,6 +3341,8 @@ next_token: Forceskin_cons_t[numskins+1].strvalue = skins[numskins].name; } + skin->localskin = local; + // add face graphics if (local) { ST_LoadLocalFaceGraphics(skin->facerank, skin->facewant, skin->facemmap, numlocalskins); diff --git a/src/r_things.h b/src/r_things.h index 2b2283115..9b71464a2 100644 --- a/src/r_things.h +++ b/src/r_things.h @@ -94,6 +94,8 @@ typedef struct // specific sounds per skin sfxenum_t soundsid[NUMSKINSOUNDS]; // sound # in S_sfx table + + boolean localskin; } skin_t; extern CV_PossibleValue_t Forceskin_cons_t[];