2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
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"
|
2007-12-25 09:48:53 +00:00
|
|
|
#include "core/alloc_func.hpp"
|
2008-01-07 14:23:25 +00:00
|
|
|
#include "string_func.h"
|
2008-05-29 19:58:17 +00:00
|
|
|
#include "strings_type.h"
|
|
|
|
#include "core/math_func.hpp"
|
2008-05-30 18:20:26 +00:00
|
|
|
#include "console_internal.h"
|
|
|
|
#include "network/network.h"
|
|
|
|
#include "network/network_func.h"
|
2008-05-04 21:53:36 +00:00
|
|
|
#include "rev.h"
|
2004-09-17 09:51:44 +00:00
|
|
|
|
2008-05-24 11:55:25 +00:00
|
|
|
#include <stdarg.h>
|
|
|
|
|
2008-01-13 01:21:35 +00:00
|
|
|
#include "table/strings.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 */
|
|
|
|
IConsoleCmd *_iconsole_cmds; ///< list of registred commands
|
|
|
|
IConsoleVar *_iconsole_vars; ///< list of registred vars
|
|
|
|
IConsoleAlias *_iconsole_aliases; ///< list of registred aliases
|
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/* ** stdlib ** */
|
2004-09-19 15:24:45 +00:00
|
|
|
byte _stdlib_developer = 1;
|
|
|
|
bool _stdlib_con_developer = false;
|
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 */
|
|
|
|
_redirect_console_to_client = 0;
|
|
|
|
#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 */
|
2008-05-05 21:54:05 +00:00
|
|
|
if (fwrite(string, strlen(string), 1, _iconsole_output_file) != 1 ||
|
|
|
|
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
|
|
|
|
* @param color_code the colour of the command. Red in case of errors, etc.
|
|
|
|
* @param string the message entered or output on the console (notice, error, etc.)
|
|
|
|
*/
|
2008-05-24 10:35:15 +00:00
|
|
|
void IConsolePrint(ConsoleColour color_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
|
2005-01-15 20:09:16 +00:00
|
|
|
if (_redirect_console_to_client != 0) {
|
|
|
|
/* Redirect the string to the client */
|
2008-05-30 18:20:26 +00:00
|
|
|
NetworkServerSendRcon(_redirect_console_to_client, color_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);
|
|
|
|
str_validate(str);
|
|
|
|
|
2004-12-04 17:54:56 +00:00
|
|
|
if (_network_dedicated) {
|
2007-06-23 14:40:19 +00:00
|
|
|
fprintf(stdout, "%s\n", str);
|
|
|
|
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);
|
|
|
|
IConsoleGUIPrint(color_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
|
|
|
*/
|
2008-05-24 10:35:15 +00:00
|
|
|
void CDECL IConsolePrintF(ConsoleColour color_code, const char *s, ...)
|
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
|
|
|
|
2004-08-24 22:41:42 +00:00
|
|
|
va_start(va, s);
|
2005-05-02 15:52:19 +00:00
|
|
|
vsnprintf(buf, sizeof(buf), s, va);
|
2004-08-24 22:41:42 +00:00
|
|
|
va_end(va);
|
2004-09-14 16:10:20 +00:00
|
|
|
|
2004-09-19 15:24:45 +00:00
|
|
|
IConsolePrint(color_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
|
|
|
{
|
2004-09-19 15:24:45 +00:00
|
|
|
if (_stdlib_developer > 1)
|
2008-05-24 10:35:15 +00:00
|
|
|
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
|
|
|
{
|
2004-09-19 15:24:45 +00:00
|
|
|
if (_stdlib_developer > 0)
|
2008-05-24 10:35:15 +00:00
|
|
|
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
|
|
|
|
2007-02-23 11:50:43 +00:00
|
|
|
/* * *************************
|
|
|
|
* hooking code *
|
|
|
|
* *************************/
|
|
|
|
|
2005-05-17 16:07:48 +00:00
|
|
|
/**
|
|
|
|
* General internal hooking code that is the same for both commands and variables
|
2007-04-03 17:19:06 +00:00
|
|
|
* @param hooks IConsoleHooks structure that will be set according to
|
2005-05-17 16:07:48 +00:00
|
|
|
* @param type type access trigger
|
|
|
|
* @param proc function called when the hook criteria is met
|
|
|
|
*/
|
|
|
|
static void IConsoleHookAdd(IConsoleHooks *hooks, IConsoleHookTypes type, IConsoleHook *proc)
|
|
|
|
{
|
|
|
|
if (hooks == NULL || proc == NULL) return;
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case ICONSOLE_HOOK_ACCESS:
|
|
|
|
hooks->access = proc;
|
|
|
|
break;
|
|
|
|
case ICONSOLE_HOOK_PRE_ACTION:
|
|
|
|
hooks->pre = proc;
|
|
|
|
break;
|
|
|
|
case ICONSOLE_HOOK_POST_ACTION:
|
|
|
|
hooks->post = proc;
|
|
|
|
break;
|
2005-06-02 15:48:28 +00:00
|
|
|
default: NOT_REACHED();
|
2005-05-17 16:07:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle any special hook triggers. If the hook type is met check if
|
|
|
|
* there is a function associated with that and if so, execute it
|
2007-04-03 17:19:06 +00:00
|
|
|
* @param hooks IConsoleHooks structure that will be checked
|
2005-05-17 16:07:48 +00:00
|
|
|
* @param type type of hook, trigger that needs to be activated
|
2007-07-30 13:36:09 +00:00
|
|
|
* @return true on a successful execution of the hook command or if there
|
2005-05-17 16:07:48 +00:00
|
|
|
* is no hook/trigger present at all. False otherwise
|
|
|
|
*/
|
|
|
|
static bool IConsoleHookHandle(const IConsoleHooks *hooks, IConsoleHookTypes type)
|
|
|
|
{
|
|
|
|
IConsoleHook *proc = NULL;
|
|
|
|
if (hooks == NULL) return false;
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case ICONSOLE_HOOK_ACCESS:
|
|
|
|
proc = hooks->access;
|
|
|
|
break;
|
|
|
|
case ICONSOLE_HOOK_PRE_ACTION:
|
|
|
|
proc = hooks->pre;
|
|
|
|
break;
|
|
|
|
case ICONSOLE_HOOK_POST_ACTION:
|
|
|
|
proc = hooks->post;
|
|
|
|
break;
|
2005-06-02 15:48:28 +00:00
|
|
|
default: NOT_REACHED();
|
2005-05-17 16:07:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (proc == NULL) ? true : proc();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a hook to a command that will be triggered at certain points
|
|
|
|
* @param name name of the command that the hook is added to
|
|
|
|
* @param type type of hook that is added (ACCESS, BEFORE and AFTER change)
|
|
|
|
* @param proc function called when the hook criteria is met
|
|
|
|
*/
|
|
|
|
void IConsoleCmdHookAdd(const char *name, IConsoleHookTypes type, IConsoleHook *proc)
|
|
|
|
{
|
|
|
|
IConsoleCmd *cmd = IConsoleCmdGet(name);
|
|
|
|
if (cmd == NULL) return;
|
|
|
|
IConsoleHookAdd(&cmd->hook, type, proc);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a hook to a variable that will be triggered at certain points
|
|
|
|
* @param name name of the variable that the hook is added to
|
|
|
|
* @param type type of hook that is added (ACCESS, BEFORE and AFTER change)
|
|
|
|
* @param proc function called when the hook criteria is met
|
|
|
|
*/
|
|
|
|
void IConsoleVarHookAdd(const char *name, IConsoleHookTypes type, IConsoleHook *proc)
|
|
|
|
{
|
|
|
|
IConsoleVar *var = IConsoleVarGet(name);
|
|
|
|
if (var == NULL) return;
|
|
|
|
IConsoleHookAdd(&var->hook, type, proc);
|
|
|
|
}
|
|
|
|
|
2005-05-02 15:52:19 +00:00
|
|
|
/**
|
|
|
|
* Perhaps ugly macro, but this saves us the trouble of writing the same function
|
|
|
|
* three types, just with different variables. Yes, templates would be handy. It was
|
|
|
|
* either this define or an even more ugly void* magic function
|
|
|
|
*/
|
2006-06-27 21:25:53 +00:00
|
|
|
#define IConsoleAddSorted(_base, item_new, IConsoleType, type) \
|
|
|
|
{ \
|
|
|
|
IConsoleType *item, *item_before; \
|
|
|
|
/* first command */ \
|
|
|
|
if (_base == NULL) { \
|
|
|
|
_base = item_new; \
|
|
|
|
return; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
item_before = NULL; \
|
|
|
|
item = _base; \
|
|
|
|
\
|
|
|
|
/* BEGIN - Alphabetically insert the commands into the linked list */ \
|
|
|
|
while (item != NULL) { \
|
|
|
|
int i = strcmp(item->name, item_new->name); \
|
|
|
|
if (i == 0) { \
|
|
|
|
IConsoleError(type " with this name already exists; insertion aborted"); \
|
|
|
|
free(item_new); \
|
|
|
|
return; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
if (i > 0) break; /* insert at this position */ \
|
|
|
|
\
|
|
|
|
item_before = item; \
|
|
|
|
item = item->next; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
if (item_before == NULL) { \
|
|
|
|
_base = item_new; \
|
|
|
|
} else { \
|
|
|
|
item_before->next = item_new; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
item_new->next = item; \
|
|
|
|
/* END - Alphabetical insert */ \
|
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
|
|
|
|
*/
|
|
|
|
void IConsoleCmdRegister(const char *name, IConsoleCmdProc *proc)
|
|
|
|
{
|
|
|
|
char *new_cmd = strdup(name);
|
2007-01-11 17:29:39 +00:00
|
|
|
IConsoleCmd *item_new = MallocT<IConsoleCmd>(1);
|
2005-05-02 15:52:19 +00:00
|
|
|
|
|
|
|
item_new->next = NULL;
|
|
|
|
item_new->proc = proc;
|
|
|
|
item_new->name = new_cmd;
|
|
|
|
|
|
|
|
item_new->hook.access = NULL;
|
|
|
|
item_new->hook.pre = NULL;
|
|
|
|
item_new->hook.post = NULL;
|
2004-12-05 12:25:25 +00:00
|
|
|
|
2005-05-02 15:52:19 +00:00
|
|
|
IConsoleAddSorted(_iconsole_cmds, item_new, IConsoleCmd, "a command");
|
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
|
|
|
{
|
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;
|
|
|
|
|
|
|
|
IConsoleAddSorted(_iconsole_aliases, item_new, IConsoleAlias, "an alias");
|
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
|
|
|
{
|
2005-05-02 15:52:19 +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
|
|
|
|
2005-05-28 16:59:51 +00:00
|
|
|
if (_stdlib_con_developer)
|
2008-05-24 10:35:15 +00:00
|
|
|
IConsolePrintF(CC_DEBUG, "condbg: 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) {
|
2007-02-23 11:50:43 +00:00
|
|
|
case '\'': // ' will double for ""
|
2005-05-02 15:52:19 +00:00
|
|
|
aliasstream[astream_i++] = '"';
|
|
|
|
break;
|
2007-02-23 11:50:43 +00:00
|
|
|
case ';': // Cmd seperator, start new command
|
2005-05-02 15:52:19 +00:00
|
|
|
aliasstream[astream_i] = '\0';
|
|
|
|
aliases[++a_index] = &aliasstream[++astream_i];
|
2005-05-15 18:43:36 +00:00
|
|
|
cmdptr++;
|
2005-05-02 15:52:19 +00:00
|
|
|
break;
|
2007-02-23 11:50:43 +00:00
|
|
|
case '%': // Some or all parameters
|
2005-05-15 18:43:36 +00:00
|
|
|
cmdptr++;
|
2005-05-02 15:52:19 +00:00
|
|
|
switch (*cmdptr) {
|
2007-02-23 11:50:43 +00:00
|
|
|
case '+': { // All parameters seperated: "[param 1]" "[param 2]"
|
2005-05-02 15:52:19 +00:00
|
|
|
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++] = ' ';
|
2004-12-13 18:51:08 +00:00
|
|
|
}
|
2005-05-02 15:52:19 +00:00
|
|
|
} break;
|
2007-02-23 11:50:43 +00:00
|
|
|
case '!': { // Merge the parameters to one: "[param 1] [param 2] [param 3...]"
|
2005-05-02 15:52:19 +00:00
|
|
|
aliasstream[astream_i++] = '"';
|
|
|
|
for (i = 0; i != tokencount; i++) {
|
|
|
|
astream_i += IConsoleCopyInParams(&aliasstream[astream_i], tokens[i], astream_i);
|
|
|
|
aliasstream[astream_i++] = ' ';
|
2004-12-13 22:13:02 +00:00
|
|
|
}
|
2005-05-02 15:52:19 +00:00
|
|
|
aliasstream[astream_i++] = '"';
|
|
|
|
|
|
|
|
} break;
|
2007-02-23 11:50:43 +00:00
|
|
|
default: { // One specific parameter: %A = [param 1] %B = [param 2] ...
|
2005-05-02 15:52:19 +00:00
|
|
|
int param = *cmdptr - 'A';
|
|
|
|
|
|
|
|
if (param < 0 || param >= tokencount) {
|
|
|
|
IConsoleError("too many or wrong amount of parameters passed to alias, aborting");
|
2008-05-24 10:35:15 +00:00
|
|
|
IConsolePrintF(CC_WARNING, "Usage of alias '%s': %s", alias->name, alias->cmdline);
|
2005-05-02 15:52:19 +00:00
|
|
|
return;
|
2004-12-13 18:51:08 +00:00
|
|
|
}
|
|
|
|
|
2005-05-02 15:52:19 +00:00
|
|
|
aliasstream[astream_i++] = '"';
|
|
|
|
astream_i += IConsoleCopyInParams(&aliasstream[astream_i], tokens[param], astream_i);
|
|
|
|
aliasstream[astream_i++] = '"';
|
|
|
|
} break;
|
|
|
|
} break;
|
2004-12-13 18:51:08 +00:00
|
|
|
|
2005-05-02 15:52:19 +00:00
|
|
|
default:
|
|
|
|
aliasstream[astream_i++] = *cmdptr;
|
|
|
|
break;
|
|
|
|
}
|
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
|
|
|
/**
|
|
|
|
* Special function for adding string-type variables. They in addition
|
|
|
|
* also need a 'size' value saying how long their string buffer is.
|
2007-04-03 17:19:06 +00:00
|
|
|
* @param name name of the variable that will be used
|
|
|
|
* @param addr memory location the variable will point to
|
2005-05-02 15:52:19 +00:00
|
|
|
* @param size the length of the string buffer
|
2007-04-03 17:19:06 +00:00
|
|
|
* @param help the help string shown for the variable
|
|
|
|
* For more information see IConsoleVarRegister()
|
2005-05-02 15:52:19 +00:00
|
|
|
*/
|
|
|
|
void IConsoleVarStringRegister(const char *name, void *addr, uint32 size, const char *help)
|
2004-09-12 20:15:18 +00:00
|
|
|
{
|
2005-05-02 15:52:19 +00:00
|
|
|
IConsoleVar *var;
|
|
|
|
IConsoleVarRegister(name, addr, ICONSOLE_VAR_STRING, help);
|
|
|
|
var = IConsoleVarGet(name);
|
|
|
|
var->size = size;
|
2004-12-05 12:25:25 +00:00
|
|
|
}
|
|
|
|
|
2005-05-02 15:52:19 +00:00
|
|
|
/**
|
|
|
|
* Register a new variable to be used in the console
|
|
|
|
* @param name name of the variable that will be used
|
|
|
|
* @param addr memory location the variable will point to
|
|
|
|
* @param help the help string shown for the variable
|
|
|
|
* @param type the type of the variable (simple atomic) so we know which values it can get
|
|
|
|
*/
|
|
|
|
void IConsoleVarRegister(const char *name, void *addr, IConsoleVarTypes type, const char *help)
|
2004-12-05 12:25:25 +00:00
|
|
|
{
|
2005-05-02 15:52:19 +00:00
|
|
|
char *new_cmd = strdup(name);
|
2007-01-11 17:29:39 +00:00
|
|
|
IConsoleVar *item_new = MallocT<IConsoleVar>(1);
|
2004-12-05 12:25:25 +00:00
|
|
|
|
2005-05-02 15:52:19 +00:00
|
|
|
item_new->help = (help != NULL) ? strdup(help) : NULL;
|
2004-12-05 12:25:25 +00:00
|
|
|
|
2005-05-02 15:52:19 +00:00
|
|
|
item_new->next = NULL;
|
|
|
|
item_new->name = new_cmd;
|
|
|
|
item_new->addr = addr;
|
|
|
|
item_new->proc = NULL;
|
2004-09-19 15:24:45 +00:00
|
|
|
item_new->type = type;
|
2004-09-12 20:15:18 +00:00
|
|
|
|
2005-05-02 15:52:19 +00:00
|
|
|
item_new->hook.access = NULL;
|
|
|
|
item_new->hook.pre = NULL;
|
|
|
|
item_new->hook.post = NULL;
|
2004-12-13 09:01:24 +00:00
|
|
|
|
2005-05-02 15:52:19 +00:00
|
|
|
IConsoleAddSorted(_iconsole_vars, item_new, IConsoleVar, "a variable");
|
2004-09-12 20:15:18 +00:00
|
|
|
}
|
2004-08-24 22:41:42 +00:00
|
|
|
|
2005-05-02 15:52:19 +00:00
|
|
|
/**
|
|
|
|
* Find the variable pointed to by its string
|
|
|
|
* @param name variable to be found
|
|
|
|
* @return return Varstruct of the found variable, or NULL on failure
|
|
|
|
*/
|
|
|
|
IConsoleVar *IConsoleVarGet(const char *name)
|
2004-09-12 20:15:18 +00:00
|
|
|
{
|
2005-05-02 15:52:19 +00:00
|
|
|
IConsoleVar *item;
|
|
|
|
for (item = _iconsole_vars; item != NULL; item = item->next) {
|
2004-09-19 15:24:45 +00:00
|
|
|
if (strcmp(item->name, name) == 0) return item;
|
2005-05-02 15:52:19 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
* Set a new value to a console variable
|
|
|
|
* @param *var the variable being set/changed
|
|
|
|
* @param value the new value given to the variable, cast properly
|
|
|
|
*/
|
|
|
|
static void IConsoleVarSetValue(const IConsoleVar *var, uint32 value)
|
2004-09-12 20:15:18 +00:00
|
|
|
{
|
2005-05-17 16:07:48 +00:00
|
|
|
IConsoleHookHandle(&var->hook, ICONSOLE_HOOK_PRE_ACTION);
|
2005-05-02 15:52:19 +00:00
|
|
|
switch (var->type) {
|
2004-09-19 15:24:45 +00:00
|
|
|
case ICONSOLE_VAR_BOOLEAN:
|
2005-05-02 15:52:19 +00:00
|
|
|
*(bool*)var->addr = (value != 0);
|
2004-09-19 15:24:45 +00:00
|
|
|
break;
|
|
|
|
case ICONSOLE_VAR_BYTE:
|
2005-05-02 15:52:19 +00:00
|
|
|
*(byte*)var->addr = (byte)value;
|
2004-09-19 15:24:45 +00:00
|
|
|
break;
|
|
|
|
case ICONSOLE_VAR_UINT16:
|
2005-07-20 19:12:30 +00:00
|
|
|
*(uint16*)var->addr = (uint16)value;
|
2004-09-19 15:24:45 +00:00
|
|
|
break;
|
|
|
|
case ICONSOLE_VAR_INT16:
|
2005-05-02 15:52:19 +00:00
|
|
|
*(int16*)var->addr = (int16)value;
|
2004-09-19 15:24:45 +00:00
|
|
|
break;
|
2005-05-02 15:52:19 +00:00
|
|
|
case ICONSOLE_VAR_UINT32:
|
|
|
|
*(uint32*)var->addr = (uint32)value;
|
2004-09-19 15:24:45 +00:00
|
|
|
break;
|
2005-05-02 15:52:19 +00:00
|
|
|
case ICONSOLE_VAR_INT32:
|
|
|
|
*(int32*)var->addr = (int32)value;
|
2004-09-19 15:24:45 +00:00
|
|
|
break;
|
2005-05-02 15:52:19 +00:00
|
|
|
default: NOT_REACHED();
|
2004-09-19 15:24:45 +00:00
|
|
|
}
|
2004-08-24 22:41:42 +00:00
|
|
|
|
2005-05-17 16:07:48 +00:00
|
|
|
IConsoleHookHandle(&var->hook, ICONSOLE_HOOK_POST_ACTION);
|
2005-05-02 15:52:19 +00:00
|
|
|
IConsoleVarPrintSetValue(var);
|
2004-08-24 22:41:42 +00:00
|
|
|
}
|
|
|
|
|
2005-05-02 15:52:19 +00:00
|
|
|
/**
|
|
|
|
* Set a new value to a string-type variable. Basically this
|
|
|
|
* means to copy the new value over to the container.
|
|
|
|
* @param *var the variable in question
|
|
|
|
* @param *value the new value
|
|
|
|
*/
|
2006-07-26 03:33:12 +00:00
|
|
|
static void IConsoleVarSetStringvalue(const IConsoleVar *var, const char *value)
|
2004-09-12 20:15:18 +00:00
|
|
|
{
|
2005-05-02 15:52:19 +00:00
|
|
|
if (var->type != ICONSOLE_VAR_STRING || var->addr == NULL) return;
|
|
|
|
|
2005-05-17 16:07:48 +00:00
|
|
|
IConsoleHookHandle(&var->hook, ICONSOLE_HOOK_PRE_ACTION);
|
2007-01-10 18:56:51 +00:00
|
|
|
ttd_strlcpy((char*)var->addr, value, var->size);
|
2005-05-17 16:07:48 +00:00
|
|
|
IConsoleHookHandle(&var->hook, ICONSOLE_HOOK_POST_ACTION);
|
2005-05-02 15:52:19 +00:00
|
|
|
IConsoleVarPrintSetValue(var); // print out the new value, giving feedback
|
|
|
|
return;
|
2004-09-12 20:15:18 +00:00
|
|
|
}
|
2004-08-25 22:11:41 +00:00
|
|
|
|
2005-05-02 15:52:19 +00:00
|
|
|
/**
|
|
|
|
* Query the current value of a variable and return it
|
|
|
|
* @param *var the variable queried
|
|
|
|
* @return current value of the variable
|
|
|
|
*/
|
|
|
|
static uint32 IConsoleVarGetValue(const IConsoleVar *var)
|
2004-09-12 20:15:18 +00:00
|
|
|
{
|
2005-05-02 15:52:19 +00:00
|
|
|
uint32 result = 0;
|
2004-08-24 22:41:42 +00:00
|
|
|
|
2004-09-12 20:15:18 +00:00
|
|
|
switch (var->type) {
|
2004-09-19 15:24:45 +00:00
|
|
|
case ICONSOLE_VAR_BOOLEAN:
|
2005-05-02 15:52:19 +00:00
|
|
|
result = *(bool*)var->addr;
|
2004-09-19 15:24:45 +00:00
|
|
|
break;
|
|
|
|
case ICONSOLE_VAR_BYTE:
|
2005-05-02 15:52:19 +00:00
|
|
|
result = *(byte*)var->addr;
|
2004-09-19 15:24:45 +00:00
|
|
|
break;
|
|
|
|
case ICONSOLE_VAR_UINT16:
|
2005-05-02 15:52:19 +00:00
|
|
|
result = *(uint16*)var->addr;
|
2004-09-19 15:24:45 +00:00
|
|
|
break;
|
|
|
|
case ICONSOLE_VAR_INT16:
|
2005-05-02 15:52:19 +00:00
|
|
|
result = *(int16*)var->addr;
|
2004-09-19 15:24:45 +00:00
|
|
|
break;
|
2005-05-02 15:52:19 +00:00
|
|
|
case ICONSOLE_VAR_UINT32:
|
|
|
|
result = *(uint32*)var->addr;
|
2004-09-19 15:24:45 +00:00
|
|
|
break;
|
2005-05-02 15:52:19 +00:00
|
|
|
case ICONSOLE_VAR_INT32:
|
|
|
|
result = *(int32*)var->addr;
|
2004-09-19 15:24:45 +00:00
|
|
|
break;
|
2005-05-02 15:52:19 +00:00
|
|
|
default: NOT_REACHED();
|
2004-09-12 21:49:38 +00:00
|
|
|
}
|
2005-05-02 15:52:19 +00:00
|
|
|
return result;
|
2004-09-12 20:15:18 +00:00
|
|
|
}
|
|
|
|
|
2005-05-02 15:52:19 +00:00
|
|
|
/**
|
|
|
|
* Get the value of the variable and put it into a printable
|
|
|
|
* string form so we can use it for printing
|
|
|
|
*/
|
|
|
|
static char *IConsoleVarGetStringValue(const IConsoleVar *var)
|
2004-09-12 20:15:18 +00:00
|
|
|
{
|
2005-05-02 15:52:19 +00:00
|
|
|
static char tempres[50];
|
|
|
|
char *value = tempres;
|
2004-08-24 22:41:42 +00:00
|
|
|
|
|
|
|
switch (var->type) {
|
|
|
|
case ICONSOLE_VAR_BOOLEAN:
|
2005-05-02 15:52:19 +00:00
|
|
|
snprintf(tempres, sizeof(tempres), "%s", (*(bool*)var->addr) ? "on" : "off");
|
2004-09-19 15:24:45 +00:00
|
|
|
break;
|
2004-08-24 22:41:42 +00:00
|
|
|
case ICONSOLE_VAR_BYTE:
|
2005-05-02 15:52:19 +00:00
|
|
|
snprintf(tempres, sizeof(tempres), "%u", *(byte*)var->addr);
|
2004-09-19 15:24:45 +00:00
|
|
|
break;
|
2004-08-24 22:41:42 +00:00
|
|
|
case ICONSOLE_VAR_UINT16:
|
2005-05-02 15:52:19 +00:00
|
|
|
snprintf(tempres, sizeof(tempres), "%u", *(uint16*)var->addr);
|
2004-09-19 15:24:45 +00:00
|
|
|
break;
|
2004-08-24 22:41:42 +00:00
|
|
|
case ICONSOLE_VAR_UINT32:
|
2005-05-02 15:52:19 +00:00
|
|
|
snprintf(tempres, sizeof(tempres), "%u", *(uint32*)var->addr);
|
2004-09-19 15:24:45 +00:00
|
|
|
break;
|
2004-08-24 22:41:42 +00:00
|
|
|
case ICONSOLE_VAR_INT16:
|
2005-05-02 15:52:19 +00:00
|
|
|
snprintf(tempres, sizeof(tempres), "%i", *(int16*)var->addr);
|
2004-09-19 15:24:45 +00:00
|
|
|
break;
|
2004-08-24 22:41:42 +00:00
|
|
|
case ICONSOLE_VAR_INT32:
|
2005-05-02 15:52:19 +00:00
|
|
|
snprintf(tempres, sizeof(tempres), "%i", *(int32*)var->addr);
|
2004-09-19 15:24:45 +00:00
|
|
|
break;
|
2004-08-24 22:41:42 +00:00
|
|
|
case ICONSOLE_VAR_STRING:
|
2005-05-02 15:52:19 +00:00
|
|
|
value = (char*)var->addr;
|
2005-06-02 15:48:28 +00:00
|
|
|
break;
|
2005-05-02 15:52:19 +00:00
|
|
|
default: NOT_REACHED();
|
2004-09-19 15:24:45 +00:00
|
|
|
}
|
2004-08-24 22:41:42 +00:00
|
|
|
|
2005-05-02 15:52:19 +00:00
|
|
|
return value;
|
|
|
|
}
|
2004-09-12 20:15:18 +00:00
|
|
|
|
2005-05-02 15:52:19 +00:00
|
|
|
/**
|
|
|
|
* Print out the value of the variable when asked
|
|
|
|
*/
|
|
|
|
void IConsoleVarPrintGetValue(const IConsoleVar *var)
|
2004-09-12 20:15:18 +00:00
|
|
|
{
|
2005-05-02 15:52:19 +00:00
|
|
|
char *value;
|
|
|
|
/* Some variables need really specific handling, handle this in its
|
|
|
|
* callback function */
|
|
|
|
if (var->proc != NULL) {
|
|
|
|
var->proc(0, NULL);
|
|
|
|
return;
|
2004-09-12 20:15:18 +00:00
|
|
|
}
|
2005-05-02 15:52:19 +00:00
|
|
|
|
|
|
|
value = IConsoleVarGetStringValue(var);
|
2008-05-24 10:35:15 +00:00
|
|
|
IConsolePrintF(CC_WARNING, "Current value for '%s' is: %s", var->name, value);
|
2004-08-24 22:41:42 +00:00
|
|
|
}
|
|
|
|
|
2005-05-02 15:52:19 +00:00
|
|
|
/**
|
|
|
|
* Print out the value of the variable after it has been assigned
|
|
|
|
* a new value, thus giving us feedback on the action
|
|
|
|
*/
|
|
|
|
void IConsoleVarPrintSetValue(const IConsoleVar *var)
|
2004-09-12 20:15:18 +00:00
|
|
|
{
|
2005-05-02 15:52:19 +00:00
|
|
|
char *value = IConsoleVarGetStringValue(var);
|
2008-05-24 10:35:15 +00:00
|
|
|
IConsolePrintF(CC_WARNING, "'%s' changed to: %s", var->name, value);
|
2005-05-02 15:52:19 +00:00
|
|
|
}
|
2004-12-04 17:54:56 +00:00
|
|
|
|
2005-05-02 15:52:19 +00:00
|
|
|
/**
|
|
|
|
* Execute a variable command. Without any parameters, print out its value
|
|
|
|
* with parameters it assigns a new value to the variable
|
|
|
|
* @param *var the variable that we will be querying/changing
|
|
|
|
* @param tokencount how many additional parameters have been given to the commandline
|
|
|
|
* @param *token the actual parameters the variable was called with
|
|
|
|
*/
|
|
|
|
void IConsoleVarExec(const IConsoleVar *var, byte tokencount, char *token[ICON_TOKEN_COUNT])
|
|
|
|
{
|
|
|
|
const char *tokenptr = token[0];
|
|
|
|
byte t_index = tokencount;
|
|
|
|
uint32 value;
|
|
|
|
|
2005-05-28 16:59:51 +00:00
|
|
|
if (_stdlib_con_developer)
|
2008-05-24 10:35:15 +00:00
|
|
|
IConsolePrintF(CC_DEBUG, "condbg: requested command is a variable");
|
2005-05-28 16:59:51 +00:00
|
|
|
|
2005-05-02 15:52:19 +00:00
|
|
|
if (tokencount == 0) { /* Just print out value */
|
|
|
|
IConsoleVarPrintGetValue(var);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Use of assignment sign is not mandatory but supported, so just 'ignore it appropiately' */
|
|
|
|
if (strcmp(tokenptr, "=") == 0) tokencount--;
|
|
|
|
|
|
|
|
if (tokencount == 1) {
|
|
|
|
/* Some variables need really special handling, handle it in their callback procedure */
|
|
|
|
if (var->proc != NULL) {
|
|
|
|
var->proc(tokencount, &token[t_index - tokencount]); // set the new value
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* Strings need special processing. No need to convert the argument to
|
|
|
|
* an integer value, just copy over the argument on a one-by-one basis */
|
|
|
|
if (var->type == ICONSOLE_VAR_STRING) {
|
|
|
|
IConsoleVarSetStringvalue(var, token[t_index - tokencount]);
|
|
|
|
return;
|
|
|
|
} else if (GetArgumentInteger(&value, token[t_index - tokencount])) {
|
|
|
|
IConsoleVarSetValue(var, value);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Increase or decrease the value by one. This of course can only happen to 'number' types */
|
|
|
|
if (strcmp(tokenptr, "++") == 0 && var->type != ICONSOLE_VAR_STRING) {
|
|
|
|
IConsoleVarSetValue(var, IConsoleVarGetValue(var) + 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(tokenptr, "--") == 0 && var->type != ICONSOLE_VAR_STRING) {
|
|
|
|
IConsoleVarSetValue(var, IConsoleVarGetValue(var) - 1);
|
|
|
|
return;
|
|
|
|
}
|
2004-09-12 20:15:18 +00:00
|
|
|
}
|
2005-05-02 15:52:19 +00:00
|
|
|
|
|
|
|
IConsoleError("invalid variable assignment");
|
2004-09-12 20:15:18 +00:00
|
|
|
}
|
|
|
|
|
2005-05-02 15:52:19 +00:00
|
|
|
/**
|
|
|
|
* Add a callback function to the variable. Some variables need
|
|
|
|
* very special processing, which can only be done with custom code
|
|
|
|
* @param name name of the variable the callback function is added to
|
|
|
|
* @param proc the function called
|
|
|
|
*/
|
|
|
|
void IConsoleVarProcAdd(const char *name, IConsoleCmdProc *proc)
|
|
|
|
{
|
|
|
|
IConsoleVar *var = IConsoleVarGet(name);
|
|
|
|
if (var == NULL) return;
|
|
|
|
var->proc = proc;
|
|
|
|
}
|
2004-09-12 20:15:18 +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)
|
|
|
|
{
|
|
|
|
IConsoleCmd *cmd = NULL;
|
|
|
|
IConsoleAlias *alias = NULL;
|
|
|
|
IConsoleVar *var = NULL;
|
2004-12-13 20:45:55 +00:00
|
|
|
|
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
|
|
|
|
2005-05-02 15:52:19 +00:00
|
|
|
if (_stdlib_con_developer)
|
2008-05-24 10:35:15 +00:00
|
|
|
IConsolePrintF(CC_DEBUG, "condbg: 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) {
|
|
|
|
case ' ': /* Token seperator */
|
|
|
|
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;
|
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;
|
|
|
|
}
|
|
|
|
/* 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) {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-05-02 15:52:19 +00:00
|
|
|
if (_stdlib_con_developer) {
|
|
|
|
uint i;
|
2006-06-27 21:25:53 +00:00
|
|
|
|
|
|
|
for (i = 0; tokens[i] != NULL; i++) {
|
2008-05-24 10:35:15 +00:00
|
|
|
IConsolePrintF(CC_DEBUG, "condbg: token %d is: '%s'", i, tokens[i]);
|
2006-06-27 21:25:53 +00:00
|
|
|
}
|
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
|
2005-05-02 15:52:19 +00:00
|
|
|
/* 2. Determine type of command (cmd, alias or variable) and execute
|
|
|
|
* First try commands, then aliases, and finally variables. Execute
|
|
|
|
* the found action taking into account its hooking code
|
|
|
|
*/
|
2006-06-27 21:25:53 +00:00
|
|
|
cmd = IConsoleCmdGet(tokens[0]);
|
|
|
|
if (cmd != NULL) {
|
2005-05-02 15:52:19 +00:00
|
|
|
if (IConsoleHookHandle(&cmd->hook, ICONSOLE_HOOK_ACCESS)) {
|
|
|
|
IConsoleHookHandle(&cmd->hook, ICONSOLE_HOOK_PRE_ACTION);
|
|
|
|
if (cmd->proc(t_index, tokens)) { // index started with 0
|
|
|
|
IConsoleHookHandle(&cmd->hook, ICONSOLE_HOOK_POST_ACTION);
|
2006-06-27 21:25:53 +00:00
|
|
|
} else {
|
|
|
|
cmd->proc(0, NULL); // if command failed, give help
|
|
|
|
}
|
2005-05-02 15:52:19 +00:00
|
|
|
}
|
2006-06-27 21:25:53 +00:00
|
|
|
return;
|
|
|
|
}
|
2005-05-02 15:52:19 +00:00
|
|
|
|
2006-06-27 21:25:53 +00:00
|
|
|
t_index--; // ignore the variable-name for comfort for both aliases and variaables
|
|
|
|
alias = IConsoleAliasGet(tokens[0]);
|
|
|
|
if (alias != NULL) {
|
|
|
|
IConsoleAliasExec(alias, t_index, &tokens[1]);
|
|
|
|
return;
|
|
|
|
}
|
2005-05-17 16:07:48 +00:00
|
|
|
|
2006-06-27 21:25:53 +00:00
|
|
|
var = IConsoleVarGet(tokens[0]);
|
|
|
|
if (var != NULL) {
|
|
|
|
if (IConsoleHookHandle(&var->hook, ICONSOLE_HOOK_ACCESS)) {
|
|
|
|
IConsoleVarExec(var, t_index, &tokens[1]);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2005-05-02 15:52:19 +00:00
|
|
|
|
2006-06-27 21:25:53 +00:00
|
|
|
IConsoleError("command or variable not found");
|
2004-08-24 22:41:42 +00:00
|
|
|
}
|