diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 3e992f0a3..1f47bbdb6 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -3,7 +3,6 @@ # Core sources set(SRB2_CORE_SOURCES am_map.c - args.c b_bot.c command.c comptime.c diff --git a/src/Makefile b/src/Makefile index 717f881a0..06567753a 100644 --- a/src/Makefile +++ b/src/Makefile @@ -538,7 +538,6 @@ OBJS:=$(i_main_o) \ $(OBJDIR)/i_time.o \ $(OBJDIR)/lzf.o \ $(OBJDIR)/b_bot.o \ - $(OBJDIR)/args.o \ $(i_net_o) \ $(i_system_o) \ $(i_sound_o) \ diff --git a/src/args.c b/src/args.c deleted file mode 100644 index 88eec9092..000000000 --- a/src/args.c +++ /dev/null @@ -1,1309 +0,0 @@ -#include "doomtype.h" -#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 boolean 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 boolean 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 boolean 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 boolean 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 boolean 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 boolean 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 boolean 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 boolean 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; - boolean enable_help_command; - boolean had_memory_error; - struct ArgParser* parent; - boolean 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, boolean 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. -boolean 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. -boolean 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. -boolean 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, boolean 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; - boolean 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; - boolean 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; - boolean 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 boolean 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. -boolean 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. */ -/* --------------------- */ - - -boolean 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, boolean 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 deleted file mode 100644 index e2db1e501..000000000 --- a/src/args.h +++ /dev/null @@ -1,206 +0,0 @@ -// ----------------------------------------------------------------------------- -// 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 "doomtype.h" - -// ----------------------------------------------------------------------------- -// 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, boolean 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.) -boolean 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. -boolean 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. -boolean 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. -boolean 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 booleanean 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, boolean enable); - -// ----------------------------------------------------------------------------- -// Debugging utilities. -// ----------------------------------------------------------------------------- - -// Dump a parser instance for debugging. -void ap_print(ArgParser* parser); - -// True if an attempt to allocate memory failed. -boolean ap_had_memory_error(ArgParser* parser); - -// ----------------------------------------------------------------------------- -// Deprecated. -// ----------------------------------------------------------------------------- - -// Replaced by ap_enable_help_command(). -void ap_cmd_help(ArgParser* parser, boolean 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/d_netcmd.c b/src/d_netcmd.c index 9753bf11a..43d7377a7 100644 --- a/src/d_netcmd.c +++ b/src/d_netcmd.c @@ -4605,14 +4605,6 @@ static void Command_Addfile(void) SendNetXCmd(XD_ADDFILE, buf, buf_p - buf); } -// i hate myself -static boolean DumbStartsWith(const char *pre, const char *str) -{ - size_t lenpre = strlen(pre), - lenstr = strlen(str); - return lenstr < lenpre ? false : memcmp(pre, str, lenpre) == 0; -} - /** Adds something at runtime. */ static void @@ -4649,16 +4641,13 @@ Command_Addskins (void) */ } -// args n stuff -#include "args.h" - -static void Command_GLocalSkin (void) +static void Command_GLocalSkin (void) { size_t first_option; size_t option_display; size_t option_all; size_t option_player; - + option_player = COM_CheckPartialParm("-p"); option_display = COM_CheckPartialParm("-d"); option_all = COM_CheckPartialParm("-a"); @@ -4686,31 +4675,27 @@ static void Command_GLocalSkin (void) fuck = ConcatCommandArgv(1, first_option); - char* player_name = player_names[consoleplayer]; - char* display_num = "0"; + const char* player_name = player_names[consoleplayer]; + const char* display_num = "0"; size_t dnum = 0; if (option_display) // -display { // handle default: 0 - if (COM_Argv(option_display + 1)[0] == "" || COM_Argv(option_display + 1)[0] != "-") + if (COM_Argc() >= option_display) display_num = COM_Argv(option_display + 1); - if (isdigit(display_num[0]) || display_num == "0") - { - dnum = atoi(display_num); - if (dnum > 3 || dnum < 0) // nuh uh - dnum = 0; - SetLocalPlayerSkin(displayplayers[dnum], fuck, NULL); + dnum = atoi(display_num); + if (dnum > 3) // nuh uh + dnum = 0; - CONS_Printf("Successfully applied localskin to displayed player.\n"); - return; - } - - CONS_Printf("Could not apply localskin.\n"); - + SetLocalPlayerSkin(displayplayers[dnum], fuck, NULL); + + CONS_Printf("Successfully applied localskin to displayed player.\n"); + + Z_Free(fuck); return; - } + } else if (option_all) // -all { int i; @@ -4723,34 +4708,38 @@ static void Command_GLocalSkin (void) } CONS_Printf("Successfully applied localskin to all players.\n"); + Z_Free(fuck); return; - } + } else // -player or no other arguments { - if (option_player) + if (option_player) { int i; player_name = COM_Argv(option_player + 1); - for (i = 0; i < MAXPLAYERS; ++i) + for (i = 0; i < MAXPLAYERS; ++i) { if (fasticmp(player_names[i], player_name)) SetLocalPlayerSkin(i, fuck, NULL); } CONS_Printf("Successfully applied localskin to specified player.\n"); + Z_Free(fuck); return; - } - else + } + else { SetLocalPlayerSkin(consoleplayer, fuck, &cv_localskin); CONS_Printf("Successfully applied localskin.\n"); + Z_Free(fuck); return; } CONS_Printf("Could not apply localskin.\n"); + Z_Free(fuck); return; } }