From 76db26ee0be6ab74d468d11bc9de9dfde6f5ed28 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Sat, 11 Apr 2020 13:50:24 +0200 Subject: [PATCH] - split off the console's command line class into its own file. --- src/CMakeLists.txt | 1 + src/console/c_commandline.cpp | 204 ++++++++++++++++++++++++++++++++++ src/console/c_commandline.h | 58 ++++++++++ src/console/c_cvars.cpp | 1 - src/console/c_cvars.h | 3 +- src/console/c_dispatch.cpp | 160 -------------------------- src/console/c_dispatch.h | 19 +--- 7 files changed, 265 insertions(+), 181 deletions(-) create mode 100644 src/console/c_commandline.cpp create mode 100644 src/console/c_commandline.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 3dba43ff7d..9ab0ae58d2 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -828,6 +828,7 @@ set (PCH_SOURCES playsim/bots/b_move.cpp playsim/bots/b_think.cpp bbannouncer.cpp + console/c_commandline.cpp console/c_bind.cpp console/c_cmds.cpp console/c_console.cpp diff --git a/src/console/c_commandline.cpp b/src/console/c_commandline.cpp new file mode 100644 index 0000000000..4516af4ed5 --- /dev/null +++ b/src/console/c_commandline.cpp @@ -0,0 +1,204 @@ +/* +** c_dispatch.cpp +** Functions for executing console commands and aliases +** +**--------------------------------------------------------------------------- +** Copyright 1998-2007 Randy Heit +** All rights reserved. +** +** Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions +** are met: +** +** 1. Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** 2. Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in the +** documentation and/or other materials provided with the distribution. +** 3. The name of the author may not be used to endorse or promote products +** derived from this software without specific prior written permission. +** +** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR +** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +**--------------------------------------------------------------------------- +** +*/ + +// HEADER FILES ------------------------------------------------------------ + +#include +#include +#include +#include + +#include "c_commandline.h" +#include "c_cvars.h" +#include "v_text.h" + +// ParseCommandLine +// +// Parse a command line (passed in args). If argc is non-NULL, it will +// be set to the number of arguments. If argv is non-NULL, it will be +// filled with pointers to each argument; argv[0] should be initialized +// to point to a buffer large enough to hold all the arguments. The +// return value is the necessary size of this buffer. +// +// Special processing: +// Inside quoted strings, \" becomes just " +// \\ becomes just a single backslash +// \c becomes just TEXTCOLOR_ESCAPE +// $ is replaced by the contents of + +static long ParseCommandLine(const char* args, int* argc, char** argv, bool no_escapes) +{ + int count; + char* buffplace; + + count = 0; + buffplace = NULL; + if (argv != NULL) + { + buffplace = argv[0]; + } + + for (;;) + { + while (*args <= ' ' && *args) + { // skip white space + args++; + } + if (*args == 0) + { + break; + } + else if (*args == '\"') + { // read quoted string + char stuff; + if (argv != NULL) + { + argv[count] = buffplace; + } + count++; + args++; + do + { + stuff = *args++; + if (!no_escapes && stuff == '\\' && *args == '\"') + { + stuff = '\"', args++; + } + else if (!no_escapes && stuff == '\\' && *args == '\\') + { + args++; + } + else if (!no_escapes && stuff == '\\' && *args == 'c') + { + stuff = TEXTCOLOR_ESCAPE, args++; + } + else if (stuff == '\"') + { + stuff = 0; + } + else if (stuff == 0) + { + args--; + } + if (argv != NULL) + { + *buffplace = stuff; + } + buffplace++; + } while (stuff); + } + else + { // read unquoted string + const char* start = args++, * end; + FBaseCVar* var; + UCVarValue val; + + while (*args && *args > ' ' && *args != '\"') + args++; + if (*start == '$' && (var = FindCVarSub(start + 1, int(args - start - 1)))) + { + val = var->GetGenericRep(CVAR_String); + start = val.String; + end = start + strlen(start); + } + else + { + end = args; + } + if (argv != NULL) + { + argv[count] = buffplace; + while (start < end) + *buffplace++ = *start++; + *buffplace++ = 0; + } + else + { + buffplace += end - start + 1; + } + count++; + } + } + if (argc != NULL) + { + *argc = count; + } + return (long)(buffplace - (char*)0); +} + +FCommandLine::FCommandLine (const char *commandline, bool no_escapes) +{ + cmd = commandline; + _argc = -1; + _argv = NULL; + noescapes = no_escapes; +} + +FCommandLine::~FCommandLine () +{ + if (_argv != NULL) + { + delete[] _argv; + } +} + +void FCommandLine::Shift() +{ + // Only valid after _argv has been filled. + for (int i = 1; i < _argc; ++i) + { + _argv[i - 1] = _argv[i]; + } +} + +int FCommandLine::argc () +{ + if (_argc == -1) + { + argsize = ParseCommandLine (cmd, &_argc, NULL, noescapes); + } + return _argc; +} + +char *FCommandLine::operator[] (int i) +{ + if (_argv == NULL) + { + int count = argc(); + _argv = new char *[count + (argsize+sizeof(char*)-1)/sizeof(char*)]; + _argv[0] = (char *)_argv + count*sizeof(char *); + ParseCommandLine (cmd, NULL, _argv, noescapes); + } + return _argv[i]; +} diff --git a/src/console/c_commandline.h b/src/console/c_commandline.h new file mode 100644 index 0000000000..54d3ad0a6b --- /dev/null +++ b/src/console/c_commandline.h @@ -0,0 +1,58 @@ +#pragma once +/* +** c_dispatch.h +** +**--------------------------------------------------------------------------- +** Copyright 1998-2006 Randy Heit +** All rights reserved. +** +** Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions +** are met: +** +** 1. Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** 2. Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in the +** documentation and/or other materials provided with the distribution. +** 3. The name of the author may not be used to endorse or promote products +** derived from this software without specific prior written permission. +** +** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR +** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +**--------------------------------------------------------------------------- +** +*/ + +class FConfigFile; +struct osdfuncparm_t; + + +// Class that can parse command lines +class FCommandLine +{ + friend int OSD_RegisterFunction(const char* pszName, const char* pszDesc, int (*func)(osdfuncparm_t const* const)); +public: + FCommandLine (const char *commandline, bool no_escapes = false); + ~FCommandLine (); + int argc (); + char *operator[] (int i); + const char *args () { return cmd; } + void Shift(); + +private: + const char *cmd; + int _argc; + char **_argv; + long argsize; + bool noescapes; +}; + diff --git a/src/console/c_cvars.cpp b/src/console/c_cvars.cpp index a8f5ab2b5c..20a0119f72 100644 --- a/src/console/c_cvars.cpp +++ b/src/console/c_cvars.cpp @@ -44,7 +44,6 @@ #include "printf.h" #include "palutil.h" -//#include "version.h" struct FLatchedValue { diff --git a/src/console/c_cvars.h b/src/console/c_cvars.h index 0e6594cd0e..37ae5903df 100644 --- a/src/console/c_cvars.h +++ b/src/console/c_cvars.h @@ -36,7 +36,7 @@ #include "zstring.h" #include "tarray.h" -class FSerializer; +class FSerializer; // this needs to go away. /* ========================================================== @@ -227,7 +227,6 @@ void C_WriteCVars (uint8_t **demo_p, uint32_t filter, bool compact=false); // Read all cvars from *demo_p and set them appropriately. void C_ReadCVars (uint8_t **demo_p); -void C_SerializeCVars(FSerializer& arc, const char* label, uint32_t filter); void C_InstallHandlers(ConsoleCallbacks* cb); // Backup demo cvars. Called before a demo starts playing to save all diff --git a/src/console/c_dispatch.cpp b/src/console/c_dispatch.cpp index 6c9d1399e4..f07c254c47 100644 --- a/src/console/c_dispatch.cpp +++ b/src/console/c_dispatch.cpp @@ -637,166 +637,6 @@ void AddCommandString (const char *text, int keynum) } } -// ParseCommandLine -// -// Parse a command line (passed in args). If argc is non-NULL, it will -// be set to the number of arguments. If argv is non-NULL, it will be -// filled with pointers to each argument; argv[0] should be initialized -// to point to a buffer large enough to hold all the arguments. The -// return value is the necessary size of this buffer. -// -// Special processing: -// Inside quoted strings, \" becomes just " -// \\ becomes just a single backslash -// \c becomes just TEXTCOLOR_ESCAPE -// $ is replaced by the contents of - -static long ParseCommandLine (const char *args, int *argc, char **argv, bool no_escapes) -{ - int count; - char *buffplace; - - count = 0; - buffplace = NULL; - if (argv != NULL) - { - buffplace = argv[0]; - } - - for (;;) - { - while (*args <= ' ' && *args) - { // skip white space - args++; - } - if (*args == 0) - { - break; - } - else if (*args == '\"') - { // read quoted string - char stuff; - if (argv != NULL) - { - argv[count] = buffplace; - } - count++; - args++; - do - { - stuff = *args++; - if (!no_escapes && stuff == '\\' && *args == '\"') - { - stuff = '\"', args++; - } - else if (!no_escapes && stuff == '\\' && *args == '\\') - { - args++; - } - else if (!no_escapes && stuff == '\\' && *args == 'c') - { - stuff = TEXTCOLOR_ESCAPE, args++; - } - else if (stuff == '\"') - { - stuff = 0; - } - else if (stuff == 0) - { - args--; - } - if (argv != NULL) - { - *buffplace = stuff; - } - buffplace++; - } while (stuff); - } - else - { // read unquoted string - const char *start = args++, *end; - FBaseCVar *var; - UCVarValue val; - - while (*args && *args > ' ' && *args != '\"') - args++; - if (*start == '$' && (var = FindCVarSub (start+1, int(args-start-1)))) - { - val = var->GetGenericRep (CVAR_String); - start = val.String; - end = start + strlen (start); - } - else - { - end = args; - } - if (argv != NULL) - { - argv[count] = buffplace; - while (start < end) - *buffplace++ = *start++; - *buffplace++ = 0; - } - else - { - buffplace += end - start + 1; - } - count++; - } - } - if (argc != NULL) - { - *argc = count; - } - return (long)(buffplace - (char *)0); -} - -FCommandLine::FCommandLine (const char *commandline, bool no_escapes) -{ - cmd = commandline; - _argc = -1; - _argv = NULL; - noescapes = no_escapes; -} - -FCommandLine::~FCommandLine () -{ - if (_argv != NULL) - { - delete[] _argv; - } -} - -void FCommandLine::Shift() -{ - // Only valid after _argv has been filled. - for (int i = 1; i < _argc; ++i) - { - _argv[i - 1] = _argv[i]; - } -} - -int FCommandLine::argc () -{ - if (_argc == -1) - { - argsize = ParseCommandLine (cmd, &_argc, NULL, noescapes); - } - return _argc; -} - -char *FCommandLine::operator[] (int i) -{ - if (_argv == NULL) - { - int count = argc(); - _argv = new char *[count + (argsize+sizeof(char*)-1)/sizeof(char*)]; - _argv[0] = (char *)_argv + count*sizeof(char *); - ParseCommandLine (cmd, NULL, _argv, noescapes); - } - return _argv[i]; -} - static FConsoleCommand *ScanChainForName (FConsoleCommand *start, const char *name, size_t namelen, FConsoleCommand **prev) { int comp; diff --git a/src/console/c_dispatch.h b/src/console/c_dispatch.h index 88976d95f5..8e01977fe9 100644 --- a/src/console/c_dispatch.h +++ b/src/console/c_dispatch.h @@ -36,28 +36,11 @@ #include #include "tarray.h" +#include "c_commandline.h" #include "zstring.h" class FConfigFile; -// Class that can parse command lines -class FCommandLine -{ -public: - FCommandLine (const char *commandline, bool no_escapes = false); - ~FCommandLine (); - int argc (); - char *operator[] (int i); - const char *args () { return cmd; } - void Shift(); - -private: - const char *cmd; - int _argc; - char **_argv; - long argsize; - bool noescapes; -}; // Contains the contents of an exec'ed file struct FExecList