2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
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"
|
|
|
|
#include "network/network.h"
|
|
|
|
#include "network/network_func.h"
|
2009-09-09 15:11:46 +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>
|
|
|
|
|
2004-09-21 21:40:59 +00:00
|
|
|
#define ICON_BUFFER 79
|
2005-05-02 15:52:19 +00:00
|
|
|
#define ICON_HISTORY_SIZE 20
|
2004-09-21 21:40:59 +00:00
|
|
|
#define ICON_LINE_HEIGHT 12
|
2004-12-04 17:54:56 +00:00
|
|
|
#define ICON_RIGHT_BORDERWIDTH 10
|
|
|
|
#define ICON_BOTTOM_BORDERWIDTH 12
|
2004-12-13 18:51:08 +00:00
|
|
|
#define ICON_MAX_ALIAS_LINES 40
|
2005-05-02 15:52:19 +00:00
|
|
|
#define ICON_TOKEN_COUNT 20
|
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
|
|
|
{
|
2004-09-14 16:10:20 +00:00
|
|
|
_iconsole_output_file = NULL;
|
2005-01-31 00:12:44 +00:00
|
|
|
#ifdef ENABLE_NETWORK /* Initialize network only variables */
|
2008-12-22 12:59:31 +00:00
|
|
|
_redirect_console_to_client = INVALID_CLIENT_ID;
|
2005-01-31 00:12:44 +00:00
|
|
|
#endif
|
|
|
|
|
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
|
|
|
{
|
|
|
|
if (_iconsole_output_file != NULL) {
|
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);
|
|
|
|
_iconsole_output_file = NULL;
|
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
|
|
|
{
|
|
|
|
if (_iconsole_output_file != NULL) {
|
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);
|
2005-01-23 21:42:29 +00:00
|
|
|
_iconsole_output_file = NULL;
|
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.)
|
|
|
|
*/
|
2009-02-09 02:57:15 +00:00
|
|
|
void IConsolePrint(ConsoleColour colour_code, const char *string)
|
2004-08-24 22:41:42 +00:00
|
|
|
{
|
2006-11-29 21:38:19 +00:00
|
|
|
char *str;
|
2005-01-31 00:12:44 +00:00
|
|
|
#ifdef ENABLE_NETWORK
|
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;
|
|
|
|
}
|
2005-01-31 00:12:44 +00:00
|
|
|
#endif
|
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 */
|
|
|
|
str = strdup(string);
|
|
|
|
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) {
|
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
|
|
|
*/
|
2009-05-10 17:27:25 +00:00
|
|
|
void CDECL IConsolePrintF(ConsoleColour colour_code, const char *format, ...)
|
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);
|
|
|
|
vsnprintf(buf, sizeof(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)
|
|
|
|
{
|
|
|
|
if (*base == NULL) {
|
|
|
|
*base = item_new;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
T *item_before = NULL;
|
|
|
|
T *item = *base;
|
|
|
|
/* The list is alphabetically sorted, insert the new item at the correct location */
|
|
|
|
while (item != NULL) {
|
|
|
|
if (strcmp(item->name, item_new->name) > 0) break; // insert here
|
|
|
|
|
|
|
|
item_before = item;
|
|
|
|
item = item->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (item_before == NULL) {
|
|
|
|
*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
|
|
|
|
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);
|
2010-02-10 18:18:08 +00:00
|
|
|
item_new->name = strdup(name);
|
2005-05-02 15:52:19 +00:00
|
|
|
item_new->next = NULL;
|
|
|
|
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
|
|
|
|
* @return return Cmdstruct of the found command, or NULL on failure
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
2005-05-02 15:52:19 +00:00
|
|
|
for (item = _iconsole_cmds; item != NULL; item = item->next) {
|
2004-09-19 15:24:45 +00:00
|
|
|
if (strcmp(item->name, name) == 0) return item;
|
|
|
|
}
|
2004-09-12 20:15:18 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
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
|
|
|
{
|
2010-03-24 11:20:30 +00:00
|
|
|
if (IConsoleAliasGet(name) != NULL) {
|
|
|
|
IConsoleError("an alias with this name already exists; insertion aborted");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-05-02 15:52:19 +00:00
|
|
|
char *new_alias = strdup(name);
|
|
|
|
char *cmd_aliased = strdup(cmd);
|
2007-01-11 17:29:39 +00:00
|
|
|
IConsoleAlias *item_new = MallocT<IConsoleAlias>(1);
|
2004-12-13 18:51:08 +00:00
|
|
|
|
2005-05-02 15:52:19 +00:00
|
|
|
item_new->next = NULL;
|
|
|
|
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
|
|
|
|
* @return return Aliasstruct of the found alias, or NULL on failure
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
2005-05-02 15:52:19 +00:00
|
|
|
for (item = _iconsole_aliases; item != NULL; 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
|
|
|
|
2004-12-13 18:51:08 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-05-02 15:52:19 +00:00
|
|
|
/** copy in an argument into the aliasstream */
|
|
|
|
static inline int IConsoleCopyInParams(char *dst, const char *src, uint bufpos)
|
2005-01-22 22:47:58 +00:00
|
|
|
{
|
2008-10-02 00:08:45 +00:00
|
|
|
/* len is the amount of bytes to add excluding the '\0'-termination */
|
|
|
|
int len = min(ICON_MAX_STREAMSIZE - bufpos - 1, (uint)strlen(src));
|
|
|
|
strecpy(dst, src, dst + len);
|
2004-12-13 18:51:08 +00:00
|
|
|
|
2005-05-02 15:52:19 +00:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
*/
|
2006-07-26 03:33:12 +00:00
|
|
|
static void IConsoleAliasExec(const IConsoleAlias *alias, byte tokencount, char *tokens[ICON_TOKEN_COUNT])
|
2005-05-02 15:52:19 +00:00
|
|
|
{
|
|
|
|
const char *cmdptr;
|
|
|
|
char *aliases[ICON_MAX_ALIAS_LINES], aliasstream[ICON_MAX_STREAMSIZE];
|
2006-02-13 21:15:00 +00:00
|
|
|
uint i;
|
2005-05-02 15:52:19 +00:00
|
|
|
uint a_index, astream_i;
|
2004-12-13 18:51:08 +00:00
|
|
|
|
2005-05-02 15:52:19 +00:00
|
|
|
memset(&aliases, 0, sizeof(aliases));
|
|
|
|
memset(&aliasstream, 0, sizeof(aliasstream));
|
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
|
|
|
|
2005-05-02 15:52:19 +00:00
|
|
|
aliases[0] = aliasstream;
|
2005-05-15 18:43:36 +00:00
|
|
|
for (cmdptr = alias->cmdline, a_index = 0, astream_i = 0; *cmdptr != '\0'; cmdptr++) {
|
2005-05-02 15:52:19 +00:00
|
|
|
if (a_index >= lengthof(aliases) || astream_i >= lengthof(aliasstream)) break;
|
|
|
|
|
|
|
|
switch (*cmdptr) {
|
2010-08-01 18:48:00 +00:00
|
|
|
case '\'': // ' will double for ""
|
2005-05-02 15:52:19 +00:00
|
|
|
aliasstream[astream_i++] = '"';
|
2010-08-01 18:48:00 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ';': // Cmd seperator, start new command
|
|
|
|
aliasstream[astream_i] = '\0';
|
|
|
|
aliases[++a_index] = &aliasstream[++astream_i];
|
|
|
|
cmdptr++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '%': // Some or all parameters
|
|
|
|
cmdptr++;
|
|
|
|
switch (*cmdptr) {
|
|
|
|
case '+': { // All parameters seperated: "[param 1]" "[param 2]"
|
|
|
|
for (i = 0; i != tokencount; i++) {
|
|
|
|
aliasstream[astream_i++] = '"';
|
|
|
|
astream_i += IConsoleCopyInParams(&aliasstream[astream_i], tokens[i], astream_i);
|
|
|
|
aliasstream[astream_i++] = '"';
|
|
|
|
aliasstream[astream_i++] = ' ';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case '!': { // Merge the parameters to one: "[param 1] [param 2] [param 3...]"
|
|
|
|
aliasstream[astream_i++] = '"';
|
|
|
|
for (i = 0; i != tokencount; i++) {
|
|
|
|
astream_i += IConsoleCopyInParams(&aliasstream[astream_i], tokens[i], astream_i);
|
|
|
|
aliasstream[astream_i++] = ' ';
|
|
|
|
}
|
|
|
|
aliasstream[astream_i++] = '"';
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
aliasstream[astream_i++] = '"';
|
|
|
|
astream_i += IConsoleCopyInParams(&aliasstream[astream_i], tokens[param], astream_i);
|
|
|
|
aliasstream[astream_i++] = '"';
|
|
|
|
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:
|
|
|
|
aliasstream[astream_i++] = *cmdptr;
|
|
|
|
break;
|
2005-05-02 15:52:19 +00:00
|
|
|
}
|
2005-04-24 21:56:46 +00:00
|
|
|
}
|
2004-12-13 18:51:08 +00:00
|
|
|
|
2006-02-13 21:15:00 +00:00
|
|
|
for (i = 0; i <= a_index; i++) IConsoleCmdExec(aliases[i]); // execute each alias in turn
|
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
|
|
|
|
* individual tokens (seperated by spaces), then execute it if possible
|
|
|
|
* @param cmdstr string to be parsed and executed
|
|
|
|
*/
|
|
|
|
void IConsoleCmdExec(const char *cmdstr)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
2005-05-02 15:52:19 +00:00
|
|
|
/* 1. Split up commandline into tokens, seperated by spaces, commands
|
|
|
|
* 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++) {
|
2005-05-02 15:52:19 +00:00
|
|
|
if (t_index >= lengthof(tokens) || tstream_i >= lengthof(tokenstream)) break;
|
2004-08-24 22:41:42 +00:00
|
|
|
|
2005-05-02 15:52:19 +00:00
|
|
|
switch (*cmdptr) {
|
2009-03-15 00:32:18 +00:00
|
|
|
case ' ': // Token seperator
|
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) {
|
|
|
|
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;
|
|
|
|
}
|
2010-07-29 14:26:28 +00:00
|
|
|
/* FALL THROUGH */
|
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) {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-10 16:10:05 +00:00
|
|
|
for (uint i = 0; tokens[i] != NULL; 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
|
|
|
}
|
|
|
|
|
2005-05-16 15:05:49 +00:00
|
|
|
if (tokens[0] == '\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-06-26 15:12:51 +00:00
|
|
|
IConsoleCmd *cmd = IConsoleCmdGet(tokens[0]);
|
2006-06-27 21:25:53 +00:00
|
|
|
if (cmd != NULL) {
|
2010-03-24 20:43:31 +00:00
|
|
|
ConsoleHookResult chr = (cmd->hook == NULL ? CHR_ALLOW : cmd->hook(true));
|
|
|
|
switch (chr) {
|
|
|
|
case CHR_ALLOW:
|
|
|
|
if (!cmd->proc(t_index, tokens)) { // index started with 0
|
|
|
|
cmd->proc(0, NULL); // if command failed, give help
|
|
|
|
}
|
|
|
|
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]);
|
2006-06-27 21:25:53 +00:00
|
|
|
if (alias != NULL) {
|
|
|
|
IConsoleAliasExec(alias, t_index, &tokens[1]);
|
|
|
|
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
|
|
|
}
|