2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
|
|
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file console.cpp Handling of the in-game console. */
|
2007-02-23 11:50:43 +00:00
|
|
|
|
2004-08-24 22:41:42 +00:00
|
|
|
#include "stdafx.h"
|
2008-05-30 18:20:26 +00:00
|
|
|
#include "console_internal.h"
|
2012-01-03 21:47:01 +00:00
|
|
|
#include "network/network.h"
|
|
|
|
#include "network/network_func.h"
|
2010-10-17 17:41:52 +00:00
|
|
|
#include "network/network_admin.h"
|
2012-01-03 21:47:01 +00:00
|
|
|
#include "debug.h"
|
2010-01-15 16:41:15 +00:00
|
|
|
#include "console_func.h"
|
2010-02-10 17:01:03 +00:00
|
|
|
#include "settings_type.h"
|
2004-09-17 09:51:44 +00:00
|
|
|
|
2008-05-24 11:55:25 +00:00
|
|
|
#include <stdarg.h>
|
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "safeguards.h"
|
|
|
|
|
2011-01-01 16:35:32 +00:00
|
|
|
static const uint ICON_TOKEN_COUNT = 20; ///< Maximum number of tokens in one command
|
2020-02-01 21:08:05 +00:00
|
|
|
static const uint ICON_MAX_RECURSE = 10; ///< Maximum number of recursion
|
2004-09-19 15:24:45 +00:00
|
|
|
|
2008-01-13 13:36:01 +00:00
|
|
|
/* console parser */
|
2021-04-24 13:19:57 +00:00
|
|
|
/* static */ IConsole::CommandList &IConsole::Commands()
|
|
|
|
{
|
|
|
|
static IConsole::CommandList cmds;
|
|
|
|
return cmds;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ IConsole::AliasList &IConsole::Aliases()
|
|
|
|
{
|
|
|
|
static IConsole::AliasList aliases;
|
|
|
|
return aliases;
|
|
|
|
}
|
2008-01-13 13:36:01 +00:00
|
|
|
|
2005-05-02 15:52:19 +00:00
|
|
|
FILE *_iconsole_output_file;
|
2004-09-12 20:15:18 +00:00
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
void IConsoleInit()
|
2004-08-24 22:41:42 +00:00
|
|
|
{
|
2019-04-10 21:07:06 +00:00
|
|
|
_iconsole_output_file = nullptr;
|
2008-12-22 12:59:31 +00:00
|
|
|
_redirect_console_to_client = INVALID_CLIENT_ID;
|
2010-10-17 17:41:52 +00:00
|
|
|
_redirect_console_to_admin = INVALID_ADMIN_ID;
|
2005-01-31 00:12:44 +00:00
|
|
|
|
2008-05-24 11:55:25 +00:00
|
|
|
IConsoleGUIInit();
|
2005-01-31 00:12:44 +00:00
|
|
|
|
2004-09-12 20:15:18 +00:00
|
|
|
IConsoleStdLibRegister();
|
2004-08-24 22:41:42 +00:00
|
|
|
}
|
|
|
|
|
2006-07-26 03:33:12 +00:00
|
|
|
static void IConsoleWriteToLogFile(const char *string)
|
2005-01-16 18:19:33 +00:00
|
|
|
{
|
2019-04-10 21:07:06 +00:00
|
|
|
if (_iconsole_output_file != nullptr) {
|
2007-02-23 11:50:43 +00:00
|
|
|
/* if there is an console output file ... also print it there */
|
2009-09-09 15:11:46 +00:00
|
|
|
const char *header = GetLogPrefix();
|
2010-02-25 18:26:55 +00:00
|
|
|
if ((strlen(header) != 0 && fwrite(header, strlen(header), 1, _iconsole_output_file) != 1) ||
|
2009-09-09 15:11:46 +00:00
|
|
|
fwrite(string, strlen(string), 1, _iconsole_output_file) != 1 ||
|
2008-05-05 21:54:05 +00:00
|
|
|
fwrite("\n", 1, 1, _iconsole_output_file) != 1) {
|
|
|
|
fclose(_iconsole_output_file);
|
2019-04-10 21:07:06 +00:00
|
|
|
_iconsole_output_file = nullptr;
|
2021-06-12 19:33:01 +00:00
|
|
|
IConsolePrint(CC_ERROR, "Cannot write to console log file; closing the log file.");
|
2008-05-05 21:54:05 +00:00
|
|
|
}
|
2005-01-16 18:19:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
bool CloseConsoleLogIfActive()
|
2005-01-16 18:19:33 +00:00
|
|
|
{
|
2019-04-10 21:07:06 +00:00
|
|
|
if (_iconsole_output_file != nullptr) {
|
2021-06-12 19:33:01 +00:00
|
|
|
IConsolePrint(CC_INFO, "Console log file closed.");
|
2005-01-16 18:19:33 +00:00
|
|
|
fclose(_iconsole_output_file);
|
2019-04-10 21:07:06 +00:00
|
|
|
_iconsole_output_file = nullptr;
|
2005-01-16 18:19:33 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
void IConsoleFree()
|
2004-08-24 22:41:42 +00:00
|
|
|
{
|
2008-05-24 11:55:25 +00:00
|
|
|
IConsoleGUIFree();
|
2005-01-16 18:19:33 +00:00
|
|
|
CloseConsoleLogIfActive();
|
2004-08-24 22:41:42 +00:00
|
|
|
}
|
|
|
|
|
2005-03-27 17:20:27 +00:00
|
|
|
/**
|
|
|
|
* Handle the printing of text entered into the console or redirected there
|
2008-09-30 20:39:50 +00:00
|
|
|
* by any other means. Text can be redirected to other clients in a network game
|
2005-03-27 17:20:27 +00:00
|
|
|
* as well as to a logfile. If the network server is a dedicated server, all activities
|
|
|
|
* are also logged. All lines to print are added to a temporary buffer which can be
|
|
|
|
* used as a history to print them onscreen
|
2021-06-12 18:43:37 +00:00
|
|
|
* @param colour_code The colour of the command.
|
|
|
|
* @param string The message to output on the console (notice, error, etc.)
|
2005-03-27 17:20:27 +00:00
|
|
|
*/
|
2021-06-12 18:43:37 +00:00
|
|
|
void IConsolePrint(TextColour colour_code, const std::string &string)
|
2004-08-24 22:41:42 +00:00
|
|
|
{
|
2011-01-01 17:06:59 +00:00
|
|
|
assert(IsValidConsoleColour(colour_code));
|
|
|
|
|
2008-12-22 12:59:31 +00:00
|
|
|
if (_redirect_console_to_client != INVALID_CLIENT_ID) {
|
2005-01-15 20:09:16 +00:00
|
|
|
/* Redirect the string to the client */
|
2009-02-09 02:57:15 +00:00
|
|
|
NetworkServerSendRcon(_redirect_console_to_client, colour_code, string);
|
2005-01-15 20:09:16 +00:00
|
|
|
return;
|
|
|
|
}
|
2010-10-17 17:41:52 +00:00
|
|
|
|
|
|
|
if (_redirect_console_to_admin != INVALID_ADMIN_ID) {
|
|
|
|
NetworkServerSendAdminRcon(_redirect_console_to_admin, colour_code, string);
|
|
|
|
return;
|
|
|
|
}
|
2005-01-15 20:09:16 +00:00
|
|
|
|
2006-11-29 21:38:19 +00:00
|
|
|
/* Create a copy of the string, strip if of colours and invalid
|
|
|
|
* characters and (when applicable) assign it to the console buffer */
|
2021-06-12 18:43:37 +00:00
|
|
|
char *str = stredup(string.c_str());
|
2006-11-29 21:38:19 +00:00
|
|
|
str_strip_colours(str);
|
2021-05-29 09:21:38 +00:00
|
|
|
StrMakeValidInPlace(str);
|
2006-11-29 21:38:19 +00:00
|
|
|
|
2004-12-04 17:54:56 +00:00
|
|
|
if (_network_dedicated) {
|
2010-10-17 17:43:01 +00:00
|
|
|
NetworkAdminConsole("console", str);
|
2009-09-09 15:11:46 +00:00
|
|
|
fprintf(stdout, "%s%s\n", GetLogPrefix(), str);
|
2007-06-23 14:40:19 +00:00
|
|
|
fflush(stdout);
|
2006-11-29 21:38:19 +00:00
|
|
|
IConsoleWriteToLogFile(str);
|
|
|
|
free(str); // free duplicated string since it's not used anymore
|
2004-12-04 17:54:56 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-05-24 11:55:25 +00:00
|
|
|
IConsoleWriteToLogFile(str);
|
2009-02-09 02:57:15 +00:00
|
|
|
IConsoleGUIPrint(colour_code, str);
|
2022-09-09 16:40:13 +00:00
|
|
|
free(str);
|
2004-08-24 22:41:42 +00:00
|
|
|
}
|
|
|
|
|
2005-05-02 15:52:19 +00:00
|
|
|
/**
|
|
|
|
* Change a string into its number representation. Supports
|
|
|
|
* decimal and hexadecimal numbers as well as 'on'/'off' 'true'/'false'
|
2007-07-30 13:36:09 +00:00
|
|
|
* @param *value the variable a successful conversion will be put in
|
2005-05-02 15:52:19 +00:00
|
|
|
* @param *arg the string to be converted
|
|
|
|
* @return Return true on success or false on failure
|
|
|
|
*/
|
|
|
|
bool GetArgumentInteger(uint32 *value, const char *arg)
|
2005-04-24 21:44:45 +00:00
|
|
|
{
|
2005-05-06 22:06:40 +00:00
|
|
|
char *endptr;
|
2005-04-24 21:56:46 +00:00
|
|
|
|
2005-05-06 22:06:40 +00:00
|
|
|
if (strcmp(arg, "on") == 0 || strcmp(arg, "true") == 0) {
|
|
|
|
*value = 1;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (strcmp(arg, "off") == 0 || strcmp(arg, "false") == 0) {
|
|
|
|
*value = 0;
|
|
|
|
return true;
|
|
|
|
}
|
2004-08-24 22:41:42 +00:00
|
|
|
|
2005-05-06 22:06:40 +00:00
|
|
|
*value = strtoul(arg, &endptr, 0);
|
2005-06-04 07:35:12 +00:00
|
|
|
return arg != endptr;
|
2005-05-02 15:52:19 +00:00
|
|
|
}
|
2004-08-24 22:41:42 +00:00
|
|
|
|
2005-05-02 15:52:19 +00:00
|
|
|
/**
|
2021-04-24 13:19:57 +00:00
|
|
|
* Creates a copy of a string with underscores removed from it
|
|
|
|
* @param name String to remove the underscores from.
|
|
|
|
* @return A copy of \a name, without underscores.
|
2005-05-02 15:52:19 +00:00
|
|
|
*/
|
2021-04-24 13:19:57 +00:00
|
|
|
static std::string RemoveUnderscores(std::string name)
|
2010-03-24 11:11:38 +00:00
|
|
|
{
|
2021-04-24 13:19:57 +00:00
|
|
|
name.erase(std::remove(name.begin(), name.end(), '_'), name.end());
|
2010-08-16 15:18:21 +00:00
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2005-05-02 15:52:19 +00:00
|
|
|
/**
|
|
|
|
* Register a new command to be used in the console
|
|
|
|
* @param name name of the command that will be used
|
|
|
|
* @param proc function that will be called upon execution of command
|
|
|
|
*/
|
2021-04-24 13:19:57 +00:00
|
|
|
/* static */ void IConsole::CmdRegister(const std::string &name, IConsoleCmdProc *proc, IConsoleHook *hook)
|
2005-05-02 15:52:19 +00:00
|
|
|
{
|
2021-04-24 13:19:57 +00:00
|
|
|
IConsole::Commands().try_emplace(RemoveUnderscores(name), name, proc, hook);
|
2004-08-24 22:41:42 +00:00
|
|
|
}
|
|
|
|
|
2005-05-02 15:52:19 +00:00
|
|
|
/**
|
|
|
|
* Find the command pointed to by its string
|
|
|
|
* @param name command to be found
|
2019-04-10 21:07:06 +00:00
|
|
|
* @return return Cmdstruct of the found command, or nullptr on failure
|
2005-05-02 15:52:19 +00:00
|
|
|
*/
|
2021-04-24 13:19:57 +00:00
|
|
|
/* static */ IConsoleCmd *IConsole::CmdGet(const std::string &name)
|
2004-09-12 20:15:18 +00:00
|
|
|
{
|
2021-04-24 13:19:57 +00:00
|
|
|
auto item = IConsole::Commands().find(RemoveUnderscores(name));
|
|
|
|
if (item != IConsole::Commands().end()) return &item->second;
|
2019-04-10 21:07:06 +00:00
|
|
|
return nullptr;
|
2004-09-12 20:15:18 +00:00
|
|
|
}
|
2004-08-24 22:41:42 +00:00
|
|
|
|
2005-05-02 15:52:19 +00:00
|
|
|
/**
|
|
|
|
* Register a an alias for an already existing command in the console
|
|
|
|
* @param name name of the alias that will be used
|
|
|
|
* @param cmd name of the command that 'name' will be alias of
|
|
|
|
*/
|
2021-04-24 13:19:57 +00:00
|
|
|
/* static */ void IConsole::AliasRegister(const std::string &name, const std::string &cmd)
|
2004-12-13 18:51:08 +00:00
|
|
|
{
|
2021-04-24 13:19:57 +00:00
|
|
|
auto result = IConsole::Aliases().try_emplace(RemoveUnderscores(name), name, cmd);
|
2021-06-12 19:07:04 +00:00
|
|
|
if (!result.second) IConsolePrint(CC_ERROR, "An alias with the name '{}' already exists.", name);
|
2004-12-13 18:51:08 +00:00
|
|
|
}
|
|
|
|
|
2005-05-02 15:52:19 +00:00
|
|
|
/**
|
|
|
|
* Find the alias pointed to by its string
|
|
|
|
* @param name alias to be found
|
2019-04-10 21:07:06 +00:00
|
|
|
* @return return Aliasstruct of the found alias, or nullptr on failure
|
2005-05-02 15:52:19 +00:00
|
|
|
*/
|
2021-04-24 13:19:57 +00:00
|
|
|
/* static */ IConsoleAlias *IConsole::AliasGet(const std::string &name)
|
2004-12-13 18:51:08 +00:00
|
|
|
{
|
2021-04-24 13:19:57 +00:00
|
|
|
auto item = IConsole::Aliases().find(RemoveUnderscores(name));
|
|
|
|
if (item != IConsole::Aliases().end()) return &item->second;
|
2019-04-10 21:07:06 +00:00
|
|
|
return nullptr;
|
2004-12-13 18:51:08 +00:00
|
|
|
}
|
2021-04-24 13:19:57 +00:00
|
|
|
|
2005-05-02 15:52:19 +00:00
|
|
|
/**
|
|
|
|
* An alias is just another name for a command, or for more commands
|
|
|
|
* Execute it as well.
|
|
|
|
* @param *alias is the alias of the command
|
|
|
|
* @param tokencount the number of parameters passed
|
|
|
|
* @param *tokens are the parameters given to the original command (0 is the first param)
|
|
|
|
*/
|
2020-02-01 21:08:05 +00:00
|
|
|
static void IConsoleAliasExec(const IConsoleAlias *alias, byte tokencount, char *tokens[ICON_TOKEN_COUNT], const uint recurse_count)
|
2005-05-02 15:52:19 +00:00
|
|
|
{
|
2013-11-25 10:13:59 +00:00
|
|
|
char alias_buffer[ICON_MAX_STREAMSIZE] = { '\0' };
|
|
|
|
char *alias_stream = alias_buffer;
|
2004-12-13 18:51:08 +00:00
|
|
|
|
2021-06-12 07:10:17 +00:00
|
|
|
Debug(console, 6, "Requested command is an alias; parsing...");
|
2005-05-28 16:59:51 +00:00
|
|
|
|
2020-02-01 21:08:05 +00:00
|
|
|
if (recurse_count > ICON_MAX_RECURSE) {
|
2021-06-12 19:07:04 +00:00
|
|
|
IConsolePrint(CC_ERROR, "Too many alias expansions, recursion limit reached.");
|
2020-02-01 21:08:05 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-04-24 13:19:57 +00:00
|
|
|
for (const char *cmdptr = alias->cmdline.c_str(); *cmdptr != '\0'; cmdptr++) {
|
2005-05-02 15:52:19 +00:00
|
|
|
switch (*cmdptr) {
|
2010-08-01 18:48:00 +00:00
|
|
|
case '\'': // ' will double for ""
|
2013-11-25 10:13:59 +00:00
|
|
|
alias_stream = strecpy(alias_stream, "\"", lastof(alias_buffer));
|
2010-08-01 18:48:00 +00:00
|
|
|
break;
|
|
|
|
|
2013-11-25 10:13:59 +00:00
|
|
|
case ';': // Cmd separator; execute previous and start new command
|
2020-02-01 21:08:05 +00:00
|
|
|
IConsoleCmdExec(alias_buffer, recurse_count);
|
2013-11-25 10:13:59 +00:00
|
|
|
|
|
|
|
alias_stream = alias_buffer;
|
|
|
|
*alias_stream = '\0'; // Make sure the new command is terminated.
|
|
|
|
|
2010-08-01 18:48:00 +00:00
|
|
|
cmdptr++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '%': // Some or all parameters
|
|
|
|
cmdptr++;
|
|
|
|
switch (*cmdptr) {
|
2013-01-08 22:46:42 +00:00
|
|
|
case '+': { // All parameters separated: "[param 1]" "[param 2]"
|
2013-11-25 10:13:59 +00:00
|
|
|
for (uint i = 0; i != tokencount; i++) {
|
|
|
|
if (i != 0) alias_stream = strecpy(alias_stream, " ", lastof(alias_buffer));
|
|
|
|
alias_stream = strecpy(alias_stream, "\"", lastof(alias_buffer));
|
|
|
|
alias_stream = strecpy(alias_stream, tokens[i], lastof(alias_buffer));
|
|
|
|
alias_stream = strecpy(alias_stream, "\"", lastof(alias_buffer));
|
2010-08-01 18:48:00 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case '!': { // Merge the parameters to one: "[param 1] [param 2] [param 3...]"
|
2013-11-25 10:13:59 +00:00
|
|
|
alias_stream = strecpy(alias_stream, "\"", lastof(alias_buffer));
|
|
|
|
for (uint i = 0; i != tokencount; i++) {
|
|
|
|
if (i != 0) alias_stream = strecpy(alias_stream, " ", lastof(alias_buffer));
|
|
|
|
alias_stream = strecpy(alias_stream, tokens[i], lastof(alias_buffer));
|
2010-08-01 18:48:00 +00:00
|
|
|
}
|
2013-11-25 10:13:59 +00:00
|
|
|
alias_stream = strecpy(alias_stream, "\"", lastof(alias_buffer));
|
2010-08-01 18:48:00 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default: { // One specific parameter: %A = [param 1] %B = [param 2] ...
|
|
|
|
int param = *cmdptr - 'A';
|
|
|
|
|
|
|
|
if (param < 0 || param >= tokencount) {
|
2021-06-12 19:07:04 +00:00
|
|
|
IConsolePrint(CC_ERROR, "Too many or wrong amount of parameters passed to alias.");
|
2021-06-12 19:37:19 +00:00
|
|
|
IConsolePrint(CC_HELP, "Usage of alias '{}': '{}'.", alias->name, alias->cmdline);
|
2010-08-01 18:48:00 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-25 10:13:59 +00:00
|
|
|
alias_stream = strecpy(alias_stream, "\"", lastof(alias_buffer));
|
|
|
|
alias_stream = strecpy(alias_stream, tokens[param], lastof(alias_buffer));
|
|
|
|
alias_stream = strecpy(alias_stream, "\"", lastof(alias_buffer));
|
2010-08-01 18:48:00 +00:00
|
|
|
break;
|
|
|
|
}
|
2004-12-13 18:51:08 +00:00
|
|
|
}
|
2010-08-01 18:48:00 +00:00
|
|
|
break;
|
2004-12-13 18:51:08 +00:00
|
|
|
|
2010-08-01 18:48:00 +00:00
|
|
|
default:
|
2013-11-25 10:13:59 +00:00
|
|
|
*alias_stream++ = *cmdptr;
|
|
|
|
*alias_stream = '\0';
|
2010-08-01 18:48:00 +00:00
|
|
|
break;
|
2005-05-02 15:52:19 +00:00
|
|
|
}
|
2013-11-25 10:13:59 +00:00
|
|
|
|
|
|
|
if (alias_stream >= lastof(alias_buffer) - 1) {
|
2021-06-12 19:07:04 +00:00
|
|
|
IConsolePrint(CC_ERROR, "Requested alias execution would overflow execution buffer.");
|
2013-11-25 10:13:59 +00:00
|
|
|
return;
|
|
|
|
}
|
2005-04-24 21:56:46 +00:00
|
|
|
}
|
2004-12-13 18:51:08 +00:00
|
|
|
|
2020-02-01 21:08:05 +00:00
|
|
|
IConsoleCmdExec(alias_buffer, recurse_count);
|
2004-12-13 18:51:08 +00:00
|
|
|
}
|
|
|
|
|
2005-05-02 15:52:19 +00:00
|
|
|
/**
|
|
|
|
* Execute a given command passed to us. First chop it up into
|
2013-01-08 22:46:42 +00:00
|
|
|
* individual tokens (separated by spaces), then execute it if possible
|
2005-05-02 15:52:19 +00:00
|
|
|
* @param cmdstr string to be parsed and executed
|
|
|
|
*/
|
2020-02-01 21:08:05 +00:00
|
|
|
void IConsoleCmdExec(const char *cmdstr, const uint recurse_count)
|
2005-05-02 15:52:19 +00:00
|
|
|
{
|
|
|
|
const char *cmdptr;
|
|
|
|
char *tokens[ICON_TOKEN_COUNT], tokenstream[ICON_MAX_STREAMSIZE];
|
|
|
|
uint t_index, tstream_i;
|
2004-09-12 20:15:18 +00:00
|
|
|
|
2005-05-02 15:52:19 +00:00
|
|
|
bool longtoken = false;
|
|
|
|
bool foundtoken = false;
|
|
|
|
|
2005-05-28 16:59:51 +00:00
|
|
|
if (cmdstr[0] == '#') return; // comments
|
|
|
|
|
2005-05-15 18:43:36 +00:00
|
|
|
for (cmdptr = cmdstr; *cmdptr != '\0'; cmdptr++) {
|
2006-11-16 22:05:33 +00:00
|
|
|
if (!IsValidChar(*cmdptr, CS_ALPHANUMERAL)) {
|
2021-06-12 19:07:04 +00:00
|
|
|
IConsolePrint(CC_ERROR, "Command '{}' contains malformed characters.", cmdstr);
|
2005-05-02 15:52:19 +00:00
|
|
|
return;
|
2004-08-24 22:41:42 +00:00
|
|
|
}
|
2004-09-19 15:24:45 +00:00
|
|
|
}
|
2004-09-12 20:15:18 +00:00
|
|
|
|
2021-06-12 07:10:17 +00:00
|
|
|
Debug(console, 4, "Executing cmdline: '{}'", cmdstr);
|
2004-08-24 22:41:42 +00:00
|
|
|
|
2005-05-02 15:52:19 +00:00
|
|
|
memset(&tokens, 0, sizeof(tokens));
|
|
|
|
memset(&tokenstream, 0, sizeof(tokenstream));
|
2004-08-24 22:41:42 +00:00
|
|
|
|
2013-01-08 22:46:42 +00:00
|
|
|
/* 1. Split up commandline into tokens, separated by spaces, commands
|
2005-05-02 15:52:19 +00:00
|
|
|
* enclosed in "" are taken as one token. We can only go as far as the amount
|
|
|
|
* of characters in our stream or the max amount of tokens we can handle */
|
2005-05-15 18:43:36 +00:00
|
|
|
for (cmdptr = cmdstr, t_index = 0, tstream_i = 0; *cmdptr != '\0'; cmdptr++) {
|
2017-06-22 16:32:50 +00:00
|
|
|
if (tstream_i >= lengthof(tokenstream)) {
|
2021-06-12 19:07:04 +00:00
|
|
|
IConsolePrint(CC_ERROR, "Command line too long.");
|
2017-06-22 16:32:50 +00:00
|
|
|
return;
|
|
|
|
}
|
2004-08-24 22:41:42 +00:00
|
|
|
|
2005-05-02 15:52:19 +00:00
|
|
|
switch (*cmdptr) {
|
2013-01-08 22:46:42 +00:00
|
|
|
case ' ': // Token separator
|
2005-05-02 15:52:19 +00:00
|
|
|
if (!foundtoken) break;
|
2004-09-12 20:15:18 +00:00
|
|
|
|
2005-05-02 17:58:11 +00:00
|
|
|
if (longtoken) {
|
|
|
|
tokenstream[tstream_i] = *cmdptr;
|
|
|
|
} else {
|
|
|
|
tokenstream[tstream_i] = '\0';
|
|
|
|
foundtoken = false;
|
|
|
|
}
|
2004-08-24 22:41:42 +00:00
|
|
|
|
2005-05-02 15:52:19 +00:00
|
|
|
tstream_i++;
|
2004-09-19 15:24:45 +00:00
|
|
|
break;
|
2007-02-23 11:50:43 +00:00
|
|
|
case '"': // Tokens enclosed in "" are one token
|
2005-05-02 15:52:19 +00:00
|
|
|
longtoken = !longtoken;
|
2010-05-20 15:10:33 +00:00
|
|
|
if (!foundtoken) {
|
2017-06-22 16:32:50 +00:00
|
|
|
if (t_index >= lengthof(tokens)) {
|
2021-06-12 19:07:04 +00:00
|
|
|
IConsolePrint(CC_ERROR, "Command line too long.");
|
2017-06-22 16:32:50 +00:00
|
|
|
return;
|
|
|
|
}
|
2010-05-20 15:10:33 +00:00
|
|
|
tokens[t_index++] = &tokenstream[tstream_i];
|
|
|
|
foundtoken = true;
|
|
|
|
}
|
2004-09-19 15:24:45 +00:00
|
|
|
break;
|
2007-02-23 11:50:43 +00:00
|
|
|
case '\\': // Escape character for ""
|
2006-10-21 22:24:28 +00:00
|
|
|
if (cmdptr[1] == '"' && tstream_i + 1 < lengthof(tokenstream)) {
|
|
|
|
tokenstream[tstream_i++] = *++cmdptr;
|
|
|
|
break;
|
|
|
|
}
|
2017-08-13 18:38:42 +00:00
|
|
|
FALLTHROUGH;
|
2007-02-23 11:50:43 +00:00
|
|
|
default: // Normal character
|
2005-05-02 15:52:19 +00:00
|
|
|
tokenstream[tstream_i++] = *cmdptr;
|
2004-08-24 22:41:42 +00:00
|
|
|
|
2005-05-02 15:52:19 +00:00
|
|
|
if (!foundtoken) {
|
2017-06-22 16:32:50 +00:00
|
|
|
if (t_index >= lengthof(tokens)) {
|
2021-06-12 19:07:04 +00:00
|
|
|
IConsolePrint(CC_ERROR, "Command line too long.");
|
2017-06-22 16:32:50 +00:00
|
|
|
return;
|
|
|
|
}
|
2005-05-02 15:52:19 +00:00
|
|
|
tokens[t_index++] = &tokenstream[tstream_i - 1];
|
|
|
|
foundtoken = true;
|
2004-08-25 22:11:41 +00:00
|
|
|
}
|
2004-09-19 15:24:45 +00:00
|
|
|
break;
|
2004-08-24 22:41:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
for (uint i = 0; i < lengthof(tokens) && tokens[i] != nullptr; i++) {
|
2021-06-12 07:10:17 +00:00
|
|
|
Debug(console, 8, "Token {} is: '{}'", i, tokens[i]);
|
2005-05-02 15:52:19 +00:00
|
|
|
}
|
|
|
|
|
2014-01-28 20:03:12 +00:00
|
|
|
if (StrEmpty(tokens[0])) return; // don't execute empty commands
|
2010-02-10 17:32:39 +00:00
|
|
|
/* 2. Determine type of command (cmd or alias) and execute
|
|
|
|
* First try commands, then aliases. Execute
|
2005-05-02 15:52:19 +00:00
|
|
|
* the found action taking into account its hooking code
|
|
|
|
*/
|
2021-04-24 13:19:57 +00:00
|
|
|
IConsoleCmd *cmd = IConsole::CmdGet(tokens[0]);
|
2019-04-10 21:07:06 +00:00
|
|
|
if (cmd != nullptr) {
|
|
|
|
ConsoleHookResult chr = (cmd->hook == nullptr ? CHR_ALLOW : cmd->hook(true));
|
2010-03-24 20:43:31 +00:00
|
|
|
switch (chr) {
|
|
|
|
case CHR_ALLOW:
|
|
|
|
if (!cmd->proc(t_index, tokens)) { // index started with 0
|
2019-04-10 21:07:06 +00:00
|
|
|
cmd->proc(0, nullptr); // if command failed, give help
|
2010-03-24 20:43:31 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
|
|
|
|
case CHR_DISALLOW: return;
|
|
|
|
case CHR_HIDE: break;
|
2005-05-02 15:52:19 +00:00
|
|
|
}
|
2006-06-27 21:25:53 +00:00
|
|
|
}
|
2005-05-02 15:52:19 +00:00
|
|
|
|
2010-02-10 17:32:39 +00:00
|
|
|
t_index--;
|
2021-04-24 13:19:57 +00:00
|
|
|
IConsoleAlias *alias = IConsole::AliasGet(tokens[0]);
|
2019-04-10 21:07:06 +00:00
|
|
|
if (alias != nullptr) {
|
2020-02-01 21:08:05 +00:00
|
|
|
IConsoleAliasExec(alias, t_index, &tokens[1], recurse_count + 1);
|
2006-06-27 21:25:53 +00:00
|
|
|
return;
|
|
|
|
}
|
2005-05-17 16:07:48 +00:00
|
|
|
|
2021-06-12 19:07:04 +00:00
|
|
|
IConsolePrint(CC_ERROR, "Command '{}' not found.", tokens[0]);
|
2004-08-24 22:41:42 +00:00
|
|
|
}
|