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 */
|
2010-02-10 17:32:39 +00:00
|
|
|
IConsoleCmd *_iconsole_cmds; ///< list of registered commands
|
|
|
|
IConsoleAlias *_iconsole_aliases; ///< list of registered 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;
|
2008-05-24 10:35:15 +00:00
|
|
|
IConsolePrintF(CC_DEFAULT, "cannot write to 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) {
|
2008-05-24 10:35:15 +00:00
|
|
|
IConsolePrintF(CC_DEFAULT, "file output complete");
|
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
|
2009-02-09 02:57:15 +00:00
|
|
|
* @param colour_code the colour of the command. Red in case of errors, etc.
|
2005-03-27 17:20:27 +00:00
|
|
|
* @param string the message entered or output on the console (notice, error, etc.)
|
|
|
|
*/
|
2011-01-03 12:01:41 +00:00
|
|
|
void IConsolePrint(TextColour colour_code, const char *string)
|
2004-08-24 22:41:42 +00:00
|
|
|
{
|
2011-01-01 17:06:59 +00:00
|
|
|
assert(IsValidConsoleColour(colour_code));
|
|
|
|
|
2006-11-29 21:38:19 +00:00
|
|
|
char *str;
|
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 */
|
2014-04-25 15:40:32 +00:00
|
|
|
str = stredup(string);
|
2006-11-29 21:38:19 +00:00
|
|
|
str_strip_colours(str);
|
2009-03-06 01:23:25 +00:00
|
|
|
str_validate(str, str + strlen(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);
|
2004-08-24 22:41:42 +00:00
|
|
|
}
|
|
|
|
|
2005-05-02 15:52:19 +00:00
|
|
|
/**
|
|
|
|
* Handle the printing of text entered into the console or redirected there
|
|
|
|
* by any other means. Uses printf() style format, for more information look
|
2007-04-04 03:21:14 +00:00
|
|
|
* at IConsolePrint()
|
2005-05-02 15:52:19 +00:00
|
|
|
*/
|
2011-01-03 12:01:41 +00:00
|
|
|
void CDECL IConsolePrintF(TextColour colour_code, const char *format, ...)
|
2004-08-24 22:41:42 +00:00
|
|
|
{
|
2011-01-01 17:06:59 +00:00
|
|
|
assert(IsValidConsoleColour(colour_code));
|
|
|
|
|
2004-08-24 22:41:42 +00:00
|
|
|
va_list va;
|
2005-05-02 15:52:19 +00:00
|
|
|
char buf[ICON_MAX_STREAMSIZE];
|
2004-09-14 16:10:20 +00:00
|
|
|
|
2009-05-10 17:27:25 +00:00
|
|
|
va_start(va, format);
|
2014-04-24 19:51:45 +00:00
|
|
|
vseprintf(buf, lastof(buf), format, va);
|
2004-08-24 22:41:42 +00:00
|
|
|
va_end(va);
|
2004-09-14 16:10:20 +00:00
|
|
|
|
2009-02-09 02:57:15 +00:00
|
|
|
IConsolePrint(colour_code, buf);
|
2004-08-24 22:41:42 +00:00
|
|
|
}
|
|
|
|
|
2005-05-02 15:52:19 +00:00
|
|
|
/**
|
|
|
|
* It is possible to print debugging information to the console,
|
|
|
|
* which is achieved by using this function. Can only be used by
|
2007-04-03 17:19:06 +00:00
|
|
|
* debug() in debug.cpp. You need at least a level 2 (developer) for debugging
|
2005-05-02 15:52:19 +00:00
|
|
|
* messages to show up
|
2006-12-26 17:36:18 +00:00
|
|
|
* @param dbg debugging category
|
|
|
|
* @param string debugging message
|
2005-05-02 15:52:19 +00:00
|
|
|
*/
|
2006-12-26 17:36:18 +00:00
|
|
|
void IConsoleDebug(const char *dbg, const char *string)
|
2004-09-12 20:15:18 +00:00
|
|
|
{
|
2010-02-10 17:01:03 +00:00
|
|
|
if (_settings_client.gui.developer <= 1) return;
|
|
|
|
IConsolePrintF(CC_DEBUG, "dbg: [%s] %s", dbg, string);
|
2004-08-24 22:41:42 +00:00
|
|
|
}
|
|
|
|
|
2005-05-02 15:52:19 +00:00
|
|
|
/**
|
|
|
|
* It is possible to print warnings to the console. These are mostly
|
|
|
|
* errors or mishaps, but non-fatal. You need at least a level 1 (developer) for
|
|
|
|
* debugging messages to show up
|
|
|
|
*/
|
2006-07-26 03:33:12 +00:00
|
|
|
void IConsoleWarning(const char *string)
|
2004-09-14 16:10:20 +00:00
|
|
|
{
|
2010-02-10 17:01:03 +00:00
|
|
|
if (_settings_client.gui.developer == 0) return;
|
|
|
|
IConsolePrintF(CC_WARNING, "WARNING: %s", string);
|
2004-09-14 16:10:20 +00:00
|
|
|
}
|
2004-08-24 22:41:42 +00:00
|
|
|
|
2005-05-02 15:52:19 +00:00
|
|
|
/**
|
|
|
|
* It is possible to print error information to the console. This can include
|
|
|
|
* game errors, or errors in general you would want the user to notice
|
|
|
|
*/
|
2006-07-26 03:33:12 +00:00
|
|
|
void IConsoleError(const char *string)
|
2004-09-12 20:15:18 +00:00
|
|
|
{
|
2008-05-24 10:35:15 +00:00
|
|
|
IConsolePrintF(CC_ERROR, "ERROR: %s", string);
|
2005-04-24 21:44:45 +00:00
|
|
|
}
|
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
|
|
|
/**
|
2010-03-24 11:11:38 +00:00
|
|
|
* Add an item to an alphabetically sorted list.
|
|
|
|
* @param base first item of the list
|
|
|
|
* @param item_new the item to add
|
2005-05-02 15:52:19 +00:00
|
|
|
*/
|
2010-03-24 11:11:38 +00:00
|
|
|
template<class T>
|
|
|
|
void IConsoleAddSorted(T **base, T *item_new)
|
|
|
|
{
|
2019-04-10 21:07:06 +00:00
|
|
|
if (*base == nullptr) {
|
2010-03-24 11:11:38 +00:00
|
|
|
*base = item_new;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
T *item_before = nullptr;
|
2010-03-24 11:11:38 +00:00
|
|
|
T *item = *base;
|
|
|
|
/* The list is alphabetically sorted, insert the new item at the correct location */
|
2019-04-10 21:07:06 +00:00
|
|
|
while (item != nullptr) {
|
2010-03-24 11:11:38 +00:00
|
|
|
if (strcmp(item->name, item_new->name) > 0) break; // insert here
|
|
|
|
|
|
|
|
item_before = item;
|
|
|
|
item = item->next;
|
|
|
|
}
|
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
if (item_before == nullptr) {
|
2010-03-24 11:11:38 +00:00
|
|
|
*base = item_new;
|
|
|
|
} else {
|
|
|
|
item_before->next = item_new;
|
|
|
|
}
|
|
|
|
|
|
|
|
item_new->next = item;
|
2005-05-02 15:52:19 +00:00
|
|
|
}
|
2004-12-05 12:25:25 +00:00
|
|
|
|
2010-08-16 15:18:21 +00:00
|
|
|
/**
|
|
|
|
* Remove underscores from a string; the string will be modified!
|
2018-10-28 02:17:36 +00:00
|
|
|
* @param[in,out] name String to remove the underscores from.
|
|
|
|
* @return \a name, with its contents modified.
|
2010-08-16 15:18:21 +00:00
|
|
|
*/
|
|
|
|
char *RemoveUnderscores(char *name)
|
|
|
|
{
|
|
|
|
char *q = name;
|
|
|
|
for (const char *p = name; *p != '\0'; p++) {
|
|
|
|
if (*p != '_') *q++ = *p;
|
|
|
|
}
|
|
|
|
*q = '\0';
|
|
|
|
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
|
|
|
|
*/
|
2010-02-10 18:18:08 +00:00
|
|
|
void IConsoleCmdRegister(const char *name, IConsoleCmdProc *proc, IConsoleHook *hook)
|
2005-05-02 15:52:19 +00:00
|
|
|
{
|
2007-01-11 17:29:39 +00:00
|
|
|
IConsoleCmd *item_new = MallocT<IConsoleCmd>(1);
|
2014-04-25 15:40:32 +00:00
|
|
|
item_new->name = RemoveUnderscores(stredup(name));
|
2019-04-10 21:07:06 +00:00
|
|
|
item_new->next = nullptr;
|
2005-05-02 15:52:19 +00:00
|
|
|
item_new->proc = proc;
|
2010-02-10 18:18:08 +00:00
|
|
|
item_new->hook = hook;
|
2004-12-05 12:25:25 +00:00
|
|
|
|
2010-03-24 11:11:38 +00:00
|
|
|
IConsoleAddSorted(&_iconsole_cmds, item_new);
|
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
|
|
|
*/
|
|
|
|
IConsoleCmd *IConsoleCmdGet(const char *name)
|
2004-09-12 20:15:18 +00:00
|
|
|
{
|
2005-05-02 15:52:19 +00:00
|
|
|
IConsoleCmd *item;
|
2004-08-24 22:41:42 +00:00
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
for (item = _iconsole_cmds; item != nullptr; item = item->next) {
|
2004-09-19 15:24:45 +00:00
|
|
|
if (strcmp(item->name, name) == 0) return item;
|
|
|
|
}
|
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
|
|
|
|
*/
|
|
|
|
void IConsoleAliasRegister(const char *name, const char *cmd)
|
2004-12-13 18:51:08 +00:00
|
|
|
{
|
2019-04-10 21:07:06 +00:00
|
|
|
if (IConsoleAliasGet(name) != nullptr) {
|
2010-03-24 11:20:30 +00:00
|
|
|
IConsoleError("an alias with this name already exists; insertion aborted");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-25 15:40:32 +00:00
|
|
|
char *new_alias = RemoveUnderscores(stredup(name));
|
|
|
|
char *cmd_aliased = stredup(cmd);
|
2007-01-11 17:29:39 +00:00
|
|
|
IConsoleAlias *item_new = MallocT<IConsoleAlias>(1);
|
2004-12-13 18:51:08 +00:00
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
item_new->next = nullptr;
|
2005-05-02 15:52:19 +00:00
|
|
|
item_new->cmdline = cmd_aliased;
|
|
|
|
item_new->name = new_alias;
|
|
|
|
|
2010-03-24 11:11:38 +00:00
|
|
|
IConsoleAddSorted(&_iconsole_aliases, item_new);
|
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
|
|
|
*/
|
|
|
|
IConsoleAlias *IConsoleAliasGet(const char *name)
|
2004-12-13 18:51:08 +00:00
|
|
|
{
|
2009-01-10 00:31:47 +00:00
|
|
|
IConsoleAlias *item;
|
2004-12-13 18:51:08 +00:00
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
for (item = _iconsole_aliases; item != nullptr; item = item->next) {
|
2004-12-13 18:51:08 +00:00
|
|
|
if (strcmp(item->name, name) == 0) return item;
|
|
|
|
}
|
2005-05-02 15:52:19 +00:00
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
return nullptr;
|
2004-12-13 18:51:08 +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
|
|
|
|
2010-02-10 23:38:43 +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) {
|
|
|
|
IConsoleError("Too many alias expansions, recursion limit reached. Aborting");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-25 10:13:59 +00:00
|
|
|
for (const char *cmdptr = alias->cmdline; *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) {
|
|
|
|
IConsoleError("too many or wrong amount of parameters passed to alias, aborting");
|
|
|
|
IConsolePrintF(CC_WARNING, "Usage of alias '%s': %s", alias->name, alias->cmdline);
|
|
|
|
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) {
|
|
|
|
IConsoleError("Requested alias execution would overflow execution buffer");
|
|
|
|
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)) {
|
2005-05-02 15:52:19 +00:00
|
|
|
IConsoleError("command contains malformed characters, aborting");
|
2008-05-24 10:35:15 +00:00
|
|
|
IConsolePrintF(CC_ERROR, "ERROR: command was: '%s'", 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
|
|
|
|
2010-02-10 16:10:05 +00:00
|
|
|
DEBUG(console, 4, "Executing cmdline: '%s'", 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)) {
|
|
|
|
IConsoleError("command line too long");
|
|
|
|
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)) {
|
|
|
|
IConsoleError("command line too long");
|
|
|
|
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)) {
|
|
|
|
IConsoleError("command line too long");
|
|
|
|
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++) {
|
2010-02-10 23:38:43 +00:00
|
|
|
DEBUG(console, 8, "Token %d is: '%s'", 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
|
|
|
|
*/
|
2010-08-16 15:18:21 +00:00
|
|
|
RemoveUnderscores(tokens[0]);
|
2010-06-26 15:12:51 +00:00
|
|
|
IConsoleCmd *cmd = IConsoleCmdGet(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--;
|
2010-06-26 15:12:51 +00:00
|
|
|
IConsoleAlias *alias = IConsoleAliasGet(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
|
|
|
|
2010-02-10 17:32:39 +00:00
|
|
|
IConsoleError("command not found");
|
2004-08-24 22:41:42 +00:00
|
|
|
}
|