diff --git a/src/packages/game/IZ_app.c b/src/packages/game/IZ_app.c index 0dcb4b2..34fc76c 100644 --- a/src/packages/game/IZ_app.c +++ b/src/packages/game/IZ_app.c @@ -81,7 +81,7 @@ IZ_ProcedureResult IZ_AppInitialize(IZ_App* app, u8 argc, const char* argv[]) { const char* cmdline_buffer; char config_path[128]; // TODO abstract command line args parsing - if ((cmdline_buffer = lws_cmdline_option(argc, argv, "-c"))) { + if ((cmdline_buffer = IZ_ConfigGetCommandlineOption(argc, argv, "-c"))) { memcpy_s(config_path, 128, cmdline_buffer, 128); } else { IZ_ConfigGetDefaultPath(config_path, 128); diff --git a/src/packages/game/IZ_config.c b/src/packages/game/IZ_config.c index 0fa3f4a..1c804e0 100644 --- a/src/packages/game/IZ_config.c +++ b/src/packages/game/IZ_config.c @@ -6,3 +6,26 @@ void IZ_ConfigGetDefaultPath(const char* config_path, size_t string_size) { memcpy_s(config_path, string_size, config_path_dir, 128); strcat_s(config_path, string_size, "config-game.ini"); } + +const char* IZ_ConfigGetCommandlineOption(u8 argc, const char* argv[], const char* val) { + size_t n = strlen(val); + int c = argc; + + while (--c > 0) { + + if (!strncmp(argv[c], val, n)) { + if (!*(argv[c] + n) && c < argc - 1) { + /* coverity treats unchecked argv as "tainted" */ + if (!argv[c + 1] || strlen(argv[c + 1]) > 1024) + return NULL; + return argv[c + 1]; + } + + if (argv[c][n] == '=') + return &argv[c][n + 1]; + return argv[c] + n; + } + } + + return NULL; +} diff --git a/src/packages/game/IZ_config.h b/src/packages/game/IZ_config.h index 168daf4..605c1f1 100644 --- a/src/packages/game/IZ_config.h +++ b/src/packages/game/IZ_config.h @@ -3,8 +3,11 @@ #include #include +#include "IZ_common.h" // TODO unify loading of config from cmdline and config file void IZ_ConfigGetDefaultPath(const char*, size_t); +const char* IZ_ConfigGetCommandlineOption(u8, const char**, const char*); + #endif diff --git a/src/packages/game/output/IZ_video.c b/src/packages/game/output/IZ_video.c index d789936..82feefe 100644 --- a/src/packages/game/output/IZ_video.c +++ b/src/packages/game/output/IZ_video.c @@ -20,6 +20,13 @@ void IZ_VideoLoadConfig(IZ_VideoState* state, const char* config_path) { state->config.max_fps = ini_getl("Video", "MaxFps", IZ_DEFAULT_VIDEO_STATE.config.max_fps, config_path); } +void IZ_VideoOverrideConfig(IZ_VideoState* state, u8 argc, const char* argv[]) { + const char* cmdline_buffer; + if ((cmdline_buffer = IZ_ConfigGetCommandlineOption(argc, argv, "-f"))) { + state->config.max_fps = atoi(cmdline_buffer); + } +} + IZ_ProcedureResult IZ_VideoInitialize(IZ_VideoState* state, const char* config_path, u8 argc, const char* argv[]) { SDL_memcpy(state, &IZ_DEFAULT_VIDEO_STATE, sizeof(IZ_VideoState)); @@ -27,6 +34,7 @@ IZ_ProcedureResult IZ_VideoInitialize(IZ_VideoState* state, const char* config_p if (IZ_VideoSaveConfig(state, config_path)) { // fprintf_s(stderr, "Error committing video config.\n"); } + IZ_VideoOverrideConfig(state, argc, argv); state->last_update_at = 0u; SDL_Window* window = SDL_CreateWindow( diff --git a/src/packages/server/IZ_app.c b/src/packages/server/IZ_app.c index 0f9baee..c014937 100644 --- a/src/packages/server/IZ_app.c +++ b/src/packages/server/IZ_app.c @@ -18,7 +18,7 @@ IZ_ProcedureResult IZ_AppInitialize(IZ_App *app, u8 argc, const char **argv) { const char* cmdline_buffer; char config_path[128]; // TODO abstract command line args parsing - if ((cmdline_buffer = lws_cmdline_option(argc, argv, "-c"))) { + if ((cmdline_buffer = IZ_ConfigGetCommandlineOption(argc, argv, "-c"))) { memcpy_s(config_path, 128, cmdline_buffer, 128); } else { IZ_ConfigGetDefaultPath(config_path, 128); diff --git a/src/packages/server/IZ_config.c b/src/packages/server/IZ_config.c index 5b79fba..d744eb4 100644 --- a/src/packages/server/IZ_config.c +++ b/src/packages/server/IZ_config.c @@ -6,3 +6,26 @@ void IZ_ConfigGetDefaultPath(const char* config_path, size_t string_size) { memcpy_s(config_path, string_size, config_path_dir, 128); strcat_s(config_path, string_size, "config-server.ini"); } + +const char* IZ_ConfigGetCommandlineOption(u8 argc, const char* argv[], const char* val) { + size_t n = strlen(val); + int c = argc; + + while (--c > 0) { + + if (!strncmp(argv[c], val, n)) { + if (!*(argv[c] + n) && c < argc - 1) { + /* coverity treats unchecked argv as "tainted" */ + if (!argv[c + 1] || strlen(argv[c + 1]) > 1024) + return NULL; + return argv[c + 1]; + } + + if (argv[c][n] == '=') + return &argv[c][n + 1]; + return argv[c] + n; + } + } + + return NULL; +} diff --git a/src/packages/server/IZ_config.h b/src/packages/server/IZ_config.h index 168daf4..605c1f1 100644 --- a/src/packages/server/IZ_config.h +++ b/src/packages/server/IZ_config.h @@ -3,8 +3,11 @@ #include #include +#include "IZ_common.h" // TODO unify loading of config from cmdline and config file void IZ_ConfigGetDefaultPath(const char*, size_t); +const char* IZ_ConfigGetCommandlineOption(u8, const char**, const char*); + #endif diff --git a/src/packages/server/main.c b/src/packages/server/main.c index 243bde6..07ed178 100644 --- a/src/packages/server/main.c +++ b/src/packages/server/main.c @@ -1,6 +1,6 @@ #include "IZ_app.h" -IZ_ProcedureResult main(i32 argc, const char *argv[]) { +IZ_ProcedureResult main(i32 argc, char* argv[]) { IZ_App app; - return IZ_AppRun(&app, argc, argv); + return IZ_AppRun(&app, argc, (const char**) argv); } diff --git a/src/packages/server/network/IZ_wsserver.c b/src/packages/server/network/IZ_wsserver.c index 134f272..d050ced 100644 --- a/src/packages/server/network/IZ_wsserver.c +++ b/src/packages/server/network/IZ_wsserver.c @@ -39,7 +39,7 @@ void IZ_WSServerLoadConfig(IZ_WSServerState* state, const char* config_path, u8 // state->config.log_level = atoi(cmdline_buffer); // } - if ((cmdline_buffer = lws_cmdline_option(argc, argv, "-p"))) { + if ((cmdline_buffer = IZ_ConfigGetCommandlineOption(argc, argv, "-p"))) { state->config.port = atoi(cmdline_buffer); } } diff --git a/src/packages/server/network/IZ_wsserver.h b/src/packages/server/network/IZ_wsserver.h index 1cc757f..d7a6833 100644 --- a/src/packages/server/network/IZ_wsserver.h +++ b/src/packages/server/network/IZ_wsserver.h @@ -4,6 +4,7 @@ #include "libwebsockets.h" #include #include "../IZ_common.h" +#include "../IZ_config.h" #include "IZ_websocket.h" /* one of these is created for each client connecting to us */