diff options
author | darkvater <darkvater@openttd.org> | 2004-09-19 15:24:45 +0000 |
---|---|---|
committer | darkvater <darkvater@openttd.org> | 2004-09-19 15:24:45 +0000 |
commit | e2c1370d60501f96713feb441fd386120d1e32b9 (patch) | |
tree | 43e68d106a9b15a2ae77c7dfe20b59411a597776 | |
parent | 089a2a7847a640d2993abb44cd378058154c2df3 (diff) | |
download | openttd-e2c1370d60501f96713feb441fd386120d1e32b9.tar.xz |
(svn r295) -Fix: Rewrite and fix of console stuff, including marking (XXX) of areas that require further investigation (Tron)
-Note: booby trapped some suspicous assignments (Tron)
-rw-r--r-- | console.c | 1622 | ||||
-rw-r--r-- | console.h | 142 | ||||
-rw-r--r-- | console_cmds.c | 309 | ||||
-rw-r--r-- | stdafx.h | 1 |
4 files changed, 989 insertions, 1085 deletions
@@ -7,20 +7,28 @@ #include "variables.h" #include "hal.h" #include <stdarg.h> +#include <string.h> #include "console.h" #ifdef WIN32 #include <windows.h> #endif +#define ICONSOLE_BUFFER_SIZE 80 + +typedef enum { + ICONSOLE_OPENED, + ICONSOLE_CLOSED +} _iconsole_modes; + // ** main console ** // static bool _iconsole_inited; -static byte* _iconsole_buffer[80]; -static byte _iconsole_cbuffer[80]; -static byte _iconsole_cmdline[255]; +static char* _iconsole_buffer[ICONSOLE_BUFFER_SIZE]; +static char _iconsole_cbuffer[ICONSOLE_BUFFER_SIZE]; +static char _iconsole_cmdline[255]; static byte _iconsole_cmdpos; -static byte _iconsole_mode = ICONSOLE_CLOSED; -static Window *_iconsole_win = NULL; +static _iconsole_modes _iconsole_mode = ICONSOLE_CLOSED; +static Window* _iconsole_win = NULL; static byte _iconsole_scroll; // ** console cursor ** // @@ -29,20 +37,22 @@ static byte _icursor_rate; static byte _icursor_counter; // ** stdlib ** // -byte _stdlib_developer=1; -bool _stdlib_con_developer=false; -FILE * _iconsole_output_file; +byte _stdlib_developer = 1; +bool _stdlib_con_developer = false; +FILE* _iconsole_output_file; -// ** main console cmd buffer ** // sign_de: especialy for Celestar :D -static byte* _iconsole_cmdbuffer[20]; +// ** main console cmd buffer ** // sign_de: especially for Celestar :D +static char* _iconsole_cmdbuffer[20]; static byte _iconsole_cmdbufferpos; // ** console window ** // -static void IConsoleWndProc(Window *w, WindowEvent *e); -static const Widget _iconsole_window_widgets[] = {{WIDGETS_END}}; +static void IConsoleWndProc(Window* w, WindowEvent* e); +static const Widget _iconsole_window_widgets[] = { + {WIDGETS_END} +}; static const WindowDesc _iconsole_window_desc = { 0, 0, 2, 2, - WC_CONSOLE,0, + WC_CONSOLE, 0, WDF_STD_TOOLTIPS | WDF_DEF_WIDGET | WDF_UNCLICK_BUTTONS, _iconsole_window_widgets, IConsoleWndProc, @@ -52,24 +62,19 @@ static const WindowDesc _iconsole_window_desc = { /* end of header */ /* *************** */ -static void IConsoleAppendClipboard() +static void IConsoleAppendClipboard(void) { #ifdef WIN32 if (IsClipboardFormatAvailable(CF_TEXT)) { - byte * data; + const char* data; HGLOBAL cbuf; - int i; OpenClipboard(NULL); cbuf = GetClipboardData(CF_TEXT); - data = (byte *) GlobalLock(cbuf); + data = GlobalLock(cbuf); - i=0; - while (IS_INT_INSIDE(data[i], 32, 256)) { - _iconsole_cmdline[_iconsole_cmdpos]=data[i]; - i++; - _iconsole_cmdpos++; - } + for (; IS_INT_INSIDE(*data, 32, 256); ++data) /* XXX magic numbers */ + _iconsole_cmdline[_iconsole_cmdpos++] = *data; /* XXX prone to buffer overflow */ GlobalUnlock(cbuf); CloseClipboard(); @@ -77,147 +82,133 @@ static void IConsoleAppendClipboard() #endif } -static void IConsoleClearCommand() +static void IConsoleClearCommand(void) { -int i; -for (i=0; i<255; i++) _iconsole_cmdline[i]=0; -_iconsole_cmdpos=0; -SetWindowDirty(_iconsole_win); + memset(_iconsole_cmdline, 0, sizeof(_iconsole_cmdline)); + _iconsole_cmdpos = 0; + SetWindowDirty(_iconsole_win); } -static void IConsoleWndProc(Window *w, WindowEvent *e) +static void IConsoleWndProc(Window* w, WindowEvent* e) { // only do window events with the console w = FindWindowById(WC_CONSOLE, 0); switch(e->event) { - - case WE_PAINT: - GfxFillRect(w->left,w->top,w->width,w->height-1,0); + case WE_PAINT: { - int i=_iconsole_scroll; - int max=(w->height/12)-1; - while ((i>_iconsole_scroll-max) && (_iconsole_buffer[i]!=NULL)) { - DoDrawString(_iconsole_buffer[i],5,w->height-(((_iconsole_scroll+2)-i)*12),_iconsole_cbuffer[i]); - i--; + int i = _iconsole_scroll; + int max = w->height / 12 - 1; + GfxFillRect(w->left, w->top, w->width, w->height - 1, 0); + while ((i > _iconsole_scroll - max) && (_iconsole_buffer[i] != NULL)) { + DoDrawString(_iconsole_buffer[i], 5, + w->height - (_iconsole_scroll + 2 - i) * 12, _iconsole_cbuffer[i]); + i--; } - DoDrawString("]",5,w->height-12,_iconsole_color_commands); - DoDrawString((char *)&_iconsole_cmdline,10,w->height-12,_iconsole_color_commands); + DoDrawString("]", 5, w->height - 12, _iconsole_color_commands); + DoDrawString(_iconsole_cmdline, 10, w->height - 12, + _iconsole_color_commands); + break; } - break; - - case WE_TICK: - - _icursor_counter++; - if (_icursor_counter>_icursor_rate) { - _icursor_state=!_icursor_state; - { + case WE_TICK: + _icursor_counter++; + if (_icursor_counter > _icursor_rate) { int posx; int posy; - int color; - _cur_dpi=&_screen; - if (_icursor_state) color=14; else color=0; - posx=10+GetStringWidth((char *)&_iconsole_cmdline); - posy=w->height-3; - GfxFillRect(posx,posy,posx+5,posy+1,color); - _video_driver->make_dirty(posx,posy,5,1); - } - _icursor_counter=0; - } - break; - case WE_DESTROY: - _iconsole_win=NULL; - _iconsole_mode=ICONSOLE_CLOSED; - break; - - case WE_KEYPRESS: - e->keypress.cont=false; - if (e->keypress.keycode == (WKC_CTRL | 'V')) - { - IConsoleAppendClipboard(); - SetWindowDirty(w); - } else - if (e->keypress.keycode == (WKC_UP)) - { - IConsoleCmdBufferNavigate(+1); - SetWindowDirty(w); - } else - if (e->keypress.keycode == (WKC_DOWN)) - { - IConsoleCmdBufferNavigate(-1); - SetWindowDirty(w); - } else - if (e->keypress.keycode == (WKC_SHIFT | WKC_PAGEUP)) - { - if ((_iconsole_scroll - ((w->height/12)-1))<0) { - _iconsole_scroll = 0; - } else { - _iconsole_scroll -= (w->height/12)-1; - } - SetWindowDirty(w); - } else - if (e->keypress.keycode == (WKC_SHIFT | WKC_PAGEDOWN)) - { - if ((_iconsole_scroll + ((w->height/12)-1))>79) { - _iconsole_scroll = 79; - } else { - _iconsole_scroll += (w->height/12)-1; - } - SetWindowDirty(w); - } else - if (e->keypress.keycode == (WKC_SHIFT | WKC_UP)) - { - if ((_iconsole_scroll - 1)<0) { - _iconsole_scroll = 0; - } else { - _iconsole_scroll -= 1; - } - SetWindowDirty(w); - } else - if (e->keypress.keycode == (WKC_SHIFT | WKC_DOWN)) - { - if ((_iconsole_scroll + 1)>79) { - _iconsole_scroll = 79; - } else { - _iconsole_scroll += 1; - } - SetWindowDirty(w); - } else - if (e->keypress.keycode == WKC_BACKQUOTE) - { - IConsoleSwitch(); - } else - if (e->keypress.keycode == WKC_RETURN) - { - IConsolePrintF(_iconsole_color_commands, "] %s", _iconsole_cmdline); - IConsoleCmdBufferAdd(_iconsole_cmdline); - IConsoleCmdExec((byte *) _iconsole_cmdline); - IConsoleClearCommand(); - } else - if (e->keypress.keycode == WKC_BACKSPACE) - { - if (_iconsole_cmdpos!=0) _iconsole_cmdpos--; - _iconsole_cmdline[_iconsole_cmdpos]=0; - SetWindowDirty(w); - _iconsole_cmdbufferpos=19; - } else - if (IS_INT_INSIDE((e->keypress.ascii), 32, 256)) - { - _iconsole_scroll=79; - _iconsole_cmdline[_iconsole_cmdpos]=e->keypress.ascii; - if (_iconsole_cmdpos!=255) _iconsole_cmdpos++; - SetWindowDirty(w); - _iconsole_cmdbufferpos=19; - } else e->keypress.cont=true; - break; + _icursor_state = !_icursor_state; + _cur_dpi = &_screen; + posx = 10 + GetStringWidth(_iconsole_cmdline); + posy = w->height - 3; + GfxFillRect(posx, posy, posx + 5, posy + 1, _icursor_state ? 14 : 0); + _video_driver->make_dirty(posx, posy, 5, 1); + _icursor_counter = 0; + } + break; + case WE_DESTROY: + _iconsole_win = NULL; + _iconsole_mode = ICONSOLE_CLOSED; + break; + case WE_KEYPRESS: + { + e->keypress.cont = false; + switch (e->keypress.keycode) { + case WKC_CTRL | 'V': + IConsoleAppendClipboard(); + SetWindowDirty(w); + break; + case WKC_UP: + IConsoleCmdBufferNavigate(+1); + SetWindowDirty(w); + break; + case WKC_DOWN: + IConsoleCmdBufferNavigate(-1); + SetWindowDirty(w); + break; + case WKC_SHIFT | WKC_PAGEUP: + if (_iconsole_scroll - w->height / 12 - 1 < 0) + _iconsole_scroll = 0; + else + _iconsole_scroll -= w->height / 12 - 1; + SetWindowDirty(w); + break; + case WKC_SHIFT | WKC_PAGEDOWN: + if (_iconsole_scroll + w->height / 12 - 1 > 79) /* XXX magic number */ + _iconsole_scroll = 79; /* XXX magic number */ + else + _iconsole_scroll += w->height / 12 - 1; + SetWindowDirty(w); + break; + case WKC_SHIFT | WKC_UP: + if (_iconsole_scroll <= 0) + _iconsole_scroll = 0; + else + --_iconsole_scroll; + SetWindowDirty(w); + break; + case WKC_SHIFT | WKC_DOWN: + if (_iconsole_scroll >= 79) /* XXX magic number */ + _iconsole_scroll = 79; /* XXX magic number */ + else + ++_iconsole_scroll; + SetWindowDirty(w); + break; + case WKC_BACKQUOTE: + IConsoleSwitch(); + break; + case WKC_RETURN: + IConsolePrintF(_iconsole_color_commands, "] %s", _iconsole_cmdline); + IConsoleCmdBufferAdd(_iconsole_cmdline); + IConsoleCmdExec(_iconsole_cmdline); + IConsoleClearCommand(); + break; + case WKC_BACKSPACE: + if (_iconsole_cmdpos != 0) _iconsole_cmdpos--; + _iconsole_cmdline[_iconsole_cmdpos] = 0; + SetWindowDirty(w); + _iconsole_cmdbufferpos = 19; + break; + default: + if (IS_INT_INSIDE(e->keypress.ascii, 32, 256)) { + _iconsole_scroll = 79; /* XXX magic number */ + _iconsole_cmdline[_iconsole_cmdpos] = e->keypress.ascii; + if (_iconsole_cmdpos != sizeof(_iconsole_cmdline)) + _iconsole_cmdpos++; + SetWindowDirty(w); + _iconsole_cmdbufferpos = 19; + } + else + e->keypress.cont = true; + } + break; + } } } -void IConsoleInit() +void IConsoleInit(void) { - int i; + uint i; #if defined(WITH_REV) extern char _openttd_revision[]; #endif @@ -227,208 +218,202 @@ void IConsoleInit() _iconsole_color_warning = 13; _iconsole_color_debug = 5; _iconsole_color_commands = 2; - _iconsole_scroll=79; - _iconsole_cmdbufferpos=19; - _iconsole_inited=true; - _iconsole_mode=ICONSOLE_CLOSED; - _iconsole_win=NULL; - _icursor_state=false; - _icursor_rate=5; - _icursor_counter=0; - for (i=0;i<20;i++) { - _iconsole_cmdbuffer[i]=NULL; - } - for (i=0;i<80;i++) { - _iconsole_buffer[i]=NULL; - _iconsole_cbuffer[i]=0; - } + _iconsole_scroll = 79; /* XXX magic number */ + _iconsole_cmdbufferpos = 19; /* XXX magic number */ + _iconsole_inited = true; + _iconsole_mode = ICONSOLE_CLOSED; + _iconsole_win = NULL; + _icursor_state = false; + _icursor_rate = 5; + _icursor_counter = 0; + for (i = 0; i < lengthof(_iconsole_cmdbuffer); i++) + _iconsole_cmdbuffer[i] = NULL; + for (i = 0; i < ICONSOLE_BUFFER_SIZE; i++) { + _iconsole_buffer[i] = NULL; + _iconsole_cbuffer[i] = 0; + } IConsoleStdLibRegister(); #if defined(WITH_REV) - IConsolePrintF(13,"OpenTTD Game Console Revision 4 - %s",_openttd_revision); + IConsolePrintF(13, "OpenTTD Game Console Revision 4 - %s", _openttd_revision); #else - IConsolePrint(13,"OpenTTD Game Console Revision 4"); + IConsolePrint(13, "OpenTTD Game Console Revision 4"); #endif - IConsolePrint(12,"---------------------------------"); - IConsolePrint(12,"use \"help\" for more info"); - IConsolePrint(12,""); + IConsolePrint(12, "---------------------------------"); + IConsolePrint(12, "use \"help\" for more info"); + IConsolePrint(12, ""); IConsoleClearCommand(); IConsoleCmdBufferAdd(""); } -void IConsoleClear() +void IConsoleClear(void) { - int i; - for (i=0;i<80;i++) if (_iconsole_buffer[i]!=NULL) { + uint i; + for (i = 0; i < ICONSOLE_BUFFER_SIZE; i++) free(_iconsole_buffer[i]); - } } -void IConsoleFree() +void IConsoleFree(void) { - _iconsole_inited=false; + _iconsole_inited = false; IConsoleClear(); - if (_iconsole_output_file!=NULL) fclose(_iconsole_output_file); + if (_iconsole_output_file != NULL) fclose(_iconsole_output_file); } -void IConsoleResize() +void IConsoleResize(void) { - if (_iconsole_win!=NULL) { + if (_iconsole_win != NULL) { _iconsole_win->height = _screen.height / 3; - _iconsole_win->width= _screen.width; + _iconsole_win->width = _screen.width; } } -void IConsoleSwitch() +void IConsoleSwitch(void) { - if (_iconsole_mode==ICONSOLE_CLOSED) { - _iconsole_win = AllocateWindowDesc(&_iconsole_window_desc); - _iconsole_win->height = _screen.height / 3; - _iconsole_win->width= _screen.width; - _iconsole_mode=ICONSOLE_OPENED; - } else - if (_iconsole_mode==ICONSOLE_OPENED) { - DeleteWindowById(WC_CONSOLE,0); - _iconsole_win=NULL; - _iconsole_mode=ICONSOLE_CLOSED; - } + switch (_iconsole_mode) { + case ICONSOLE_CLOSED: + _iconsole_win = AllocateWindowDesc(&_iconsole_window_desc); + _iconsole_win->height = _screen.height / 3; + _iconsole_win->width = _screen.width; + _iconsole_mode = ICONSOLE_OPENED; + break; + case ICONSOLE_OPENED: + DeleteWindowById(WC_CONSOLE, 0); + _iconsole_win = NULL; + _iconsole_mode = ICONSOLE_CLOSED; + break; + } MarkWholeScreenDirty(); - MarkAllViewportsDirty(0,0,_screen.width,_screen.height); - _video_driver->make_dirty(0,0,_screen.width,_screen.height); + MarkAllViewportsDirty(0, 0, _screen.width, _screen.height); + _video_driver->make_dirty(0, 0, _screen.width, _screen.height); } -void IConsoleClose() +void IConsoleClose(void) { - if (_iconsole_mode==ICONSOLE_OPENED) IConsoleSwitch(); - _iconsole_mode=ICONSOLE_CLOSED; + if (_iconsole_mode == ICONSOLE_OPENED) IConsoleSwitch(); + _iconsole_mode = ICONSOLE_CLOSED; } -void IConsoleOpen() +void IConsoleOpen(void) { - if (_iconsole_mode==ICONSOLE_CLOSED) IConsoleSwitch(); + if (_iconsole_mode == ICONSOLE_CLOSED) IConsoleSwitch(); + /* XXX missing _iconsole_mode ? */ } -void IConsoleCmdBufferAdd(const byte * cmd) +void IConsoleCmdBufferAdd(const char* cmd) { int i; if (_iconsole_cmdbufferpos != 19) return; - if (_iconsole_cmdbuffer[18]!=NULL) free(_iconsole_cmdbuffer[18]); - for (i=18; i>0; i--) _iconsole_cmdbuffer[i]=_iconsole_cmdbuffer[i-1]; - i=strlen(cmd); - _iconsole_cmdbuffer[0]=malloc(i+1); - memset(((void *)_iconsole_cmdbuffer[0]),0,i+1); - memcpy(((void *)_iconsole_cmdbuffer[0]),cmd,i); - _iconsole_cmdbuffer[0][i]=0; - _iconsole_cmdbufferpos = 19; + free(_iconsole_cmdbuffer[18]); + for (i = 18; i > 0; i--) _iconsole_cmdbuffer[i] = _iconsole_cmdbuffer[i - 1]; + _iconsole_cmdbuffer[0] = strdup(cmd); } void IConsoleCmdBufferNavigate(signed char direction) { int i; - i=_iconsole_cmdbufferpos + direction; - if (i<0) i=19; - if (i>19) i=0; - if (direction>0) while (_iconsole_cmdbuffer[i]==NULL) { - i++; - if (i>19) i=0; + i = _iconsole_cmdbufferpos + direction; + if (i < 0) i = 19; + if (i > 19) i = 0; + if (direction > 0) + while (_iconsole_cmdbuffer[i] == NULL) { + ++i; + if (i > 19) i = 0; } - if (direction<0) while (_iconsole_cmdbuffer[i]==NULL) { - i--; - if (i<0) i=19; + if (direction < 0) + while (_iconsole_cmdbuffer[i] == NULL) { + --i; + if (i < 0) i = 19; } _iconsole_cmdbufferpos = i; IConsoleClearCommand(); - memcpy((void *)_iconsole_cmdline,(void *)_iconsole_cmdbuffer[i],strlen(_iconsole_cmdbuffer[i])); - _iconsole_cmdpos =strlen(_iconsole_cmdbuffer[i]); + memcpy(_iconsole_cmdline, _iconsole_cmdbuffer[i], + strlen(_iconsole_cmdbuffer[i])); + _iconsole_cmdpos = strlen(_iconsole_cmdbuffer[i]); } -void IConsolePrint(byte color_code, const byte* string) +void IConsolePrint(byte color_code, const char* string) { - byte * _ex; - byte * _new; - byte _exc; - byte _newc; - int i,j; + char* _ex; + char* _new; + char _exc; + char _newc; + char* i; + int j; if (!_iconsole_inited) return; - _newc=color_code; - i=strlen(string); - _new=malloc(i+1); - memset(_new,0,i+1); - memcpy(_new,string,i); + _newc = color_code; + _new = strdup(string); - for (j=0;j<i;j++) { - if (_new[j]<0x1F) _new[j]=0x20; - } + for (i = _new; *i != '\0'; ++i) + if (*i < 0x1F) *i = ' '; /* XXX 0x1F seems wrong + magic number */ - i=79; - while (i>=0) { - _ex=_iconsole_buffer[i]; - _exc=_iconsole_cbuffer[i]; - _iconsole_buffer[i]=_new; - _iconsole_cbuffer[i]=_newc; - _new=_ex; - _newc=_exc; - i--; - } - if (_ex!=NULL) free(_ex); + for (j = ICONSOLE_BUFFER_SIZE - 1; j >= 0; --j) { + _ex = _iconsole_buffer[j]; + _exc = _iconsole_cbuffer[j]; + _iconsole_buffer[j] = _new; + _iconsole_cbuffer[j] = _newc; + _new = _ex; + _newc = _exc; + } + free(_ex); - if (_iconsole_win!=NULL) SetWindowDirty(_iconsole_win); + if (_iconsole_win != NULL) SetWindowDirty(_iconsole_win); } -void CDECL IConsolePrintF(byte color_code, const char *s, ...) +void CDECL IConsolePrintF(byte color_code, const char* s, ...) { va_list va; char buf[1024]; int len; va_start(va, s); - len = vsprintf(buf, s, va); + len = vsnprintf(buf, sizeof(buf), s, va); va_end(va); - if (_iconsole_output_file!=NULL) { - // if there is an console output file ... also print it there - fwrite((void *) &buf, len, 1, _iconsole_output_file); - buf[1023]='\n'; - fwrite((void *)&buf[1023], 1, 1,_iconsole_output_file); - } + IConsolePrint(color_code, buf); - IConsolePrint(color_code, (byte *) &buf); + if (_iconsole_output_file != NULL) { + // if there is an console output file ... also print it there + fwrite(buf, len, 1, _iconsole_output_file); + /* XXX why newline? */ + buf[1023] = '\n'; + fwrite(&buf[1023], 1, 1, _iconsole_output_file); + } } -void IConsoleDebug(byte* string) +void IConsoleDebug(const char* string) { - if (_stdlib_developer>1) IConsolePrintF(_iconsole_color_debug, "DEBUG: %s", string); + if (_stdlib_developer > 1) + IConsolePrintF(_iconsole_color_debug, "DEBUG: %s", string); } -void IConsoleError(const byte* string) +void IConsoleError(const char* string) { - if (_stdlib_developer>0) IConsolePrintF(_iconsole_color_error, "ERROR: %s", string); + if (_stdlib_developer > 0) + IConsolePrintF(_iconsole_color_error, "ERROR: %s", string); } -void IConsoleWarning(const byte* string) +void IConsoleWarning(const char* string) { - if (_stdlib_developer>0) IConsolePrintF(_iconsole_color_warning, "WARNING: %s", string); + if (_stdlib_developer > 0) + IConsolePrintF(_iconsole_color_warning, "WARNING: %s", string); } -void IConsoleCmdRegister(const byte * name, void * addr) +void IConsoleCmdRegister(const char* name, _iconsole_cmd_addr addr) { - byte * _new; - _iconsole_cmd * item; - _iconsole_cmd * item_new; - int i; + char* _new; + _iconsole_cmd* item; + _iconsole_cmd* item_new; - i=strlen(name); - _new=malloc(i+1); - memset(_new,0,i+1); - memcpy(_new,name,i); + _new = strdup(name); item_new = malloc(sizeof(_iconsole_cmd)); item_new->_next = NULL; - item_new->addr = addr; - item_new->name = _new; + item_new->addr = addr; + item_new->name = _new; item_new->hook_access = NULL; item_new->hook_after_exec = NULL; @@ -437,43 +422,62 @@ void IConsoleCmdRegister(const byte * name, void * addr) item = _iconsole_cmds; if (item == NULL) { _iconsole_cmds = item_new; - } else { - while (item->_next != NULL) { item = item->_next; }; + } else { + while (item->_next != NULL) item = item->_next; item->_next = item_new; - } + } } -_iconsole_cmd * IConsoleCmdGet(const byte * name) +_iconsole_cmd* IConsoleCmdGet(const char* name) { - _iconsole_cmd * item; + _iconsole_cmd* item; item = _iconsole_cmds; while (item != NULL) { - if (strcmp(item->name,name)==0) return item; + if (strcmp(item->name, name) == 0) return item; item = item->_next; - } + } return NULL; } -void IConsoleVarRegister(const byte * name, void * addr, byte type) +void IConsoleVarRegister(const char* name, void* addr, _iconsole_var_types type) { - byte * _new; - _iconsole_var * item; - _iconsole_var * item_new; - int i; + _iconsole_var* item; + _iconsole_var* item_new; - i=strlen(name)+1; - _new=malloc(i+1); - memset(_new,0,i+1); - _new[0]='*'; - memcpy(_new+1,name,i); + item_new = malloc(sizeof(_iconsole_var)); /* XXX unchecked malloc */ - item_new = malloc(sizeof(_iconsole_var)); + item_new->name = malloc(strlen(name) + 2); /* XXX unchecked malloc */ + sprintf(item_new->name, "*%s", name); item_new->_next = NULL; - item_new->addr = addr; - item_new->name = _new; - item_new->type = type; + switch (type) { + case ICONSOLE_VAR_BOOLEAN: + item_new->data.bool_ = addr; + break; + case ICONSOLE_VAR_BYTE: + item_new->data.byte_ = addr; + break; + case ICONSOLE_VAR_UINT16: + item_new->data.uint16_ = addr; + break; + case ICONSOLE_VAR_UINT32: + item_new->data.uint32_ = addr; + break; + case ICONSOLE_VAR_INT16: + item_new->data.int16_ = addr; + break; + case ICONSOLE_VAR_INT32: + item_new->data.int32_ = addr; + break; + case ICONSOLE_VAR_STRING: + item_new->data.string_ = addr; + break; + default: + assert(0); /* XXX */ + break; + } + item_new->type = type; item_new->_malloc = false; item_new->hook_access = NULL; @@ -483,117 +487,96 @@ void IConsoleVarRegister(const byte * name, void * addr, byte type) item = _iconsole_vars; if (item == NULL) { _iconsole_vars = item_new; - } else { - while (item->_next != NULL) { item = item->_next; }; + } else { + while (item->_next != NULL) item = item->_next; item->_next = item_new; - } + } } -void IConsoleVarMemRegister(const byte * name, byte type) +void IConsoleVarMemRegister(const char* name, _iconsole_var_types type) { - _iconsole_var * item; + _iconsole_var* item; item = IConsoleVarAlloc(type); - IConsoleVarInsert(item,name); + IConsoleVarInsert(item, name); } -void IConsoleVarInsert(_iconsole_var * var, const byte * name) +void IConsoleVarInsert(_iconsole_var* var, const char* name) { - byte * _new; - _iconsole_var * item; - _iconsole_var * item_new; - int i; - - item_new = var; - - // dont allow to build variable rings - if (item_new->_next != NULL) return; + _iconsole_var* item; - i=strlen(name)+1; - _new=malloc(i+1); - memset(_new,0,i+1); - _new[0]='*'; - memcpy(_new+1,name,i); + // disallow building variable rings + if (var->_next != NULL) return; - item_new->name = _new; + var->name = malloc(strlen(name) + 2); /* XXX unchecked malloc */ + sprintf(var->name, "*%s", name); item = _iconsole_vars; if (item == NULL) { - _iconsole_vars = item_new; + _iconsole_vars = var; } else { - while (item->_next != NULL) { item = item->_next; }; - item->_next = item_new; + while (item->_next != NULL) item = item->_next; + item->_next = var; } } -_iconsole_var * IConsoleVarGet(const byte * name) +_iconsole_var* IConsoleVarGet(const char* name) { - _iconsole_var * item; - - item = _iconsole_vars; - while (item != NULL) { - if (strcmp(item->name,name)==0) return item; - item = item->_next; - } + _iconsole_var* item; + for (item = _iconsole_vars; item != NULL; item = item->_next) + if (strcmp(item->name, name) == 0) return item; return NULL; } -_iconsole_var * IConsoleVarAlloc(byte type) +_iconsole_var* IConsoleVarAlloc(_iconsole_var_types type) { - _iconsole_var * item; - item=malloc(sizeof(_iconsole_var)); + _iconsole_var* item = malloc(sizeof(_iconsole_var)); /* XXX unchecked malloc */ item->_next = NULL; - item->name = ""; - item->type = type; + item->name = NULL; + item->type = type; switch (item->type) { - case ICONSOLE_VAR_BOOLEAN: - { - item->addr=malloc(sizeof(bool)); - memset(item->addr,0,sizeof(bool)); - item->_malloc=true; - } - break; - case ICONSOLE_VAR_BYTE: - { - item->addr=malloc(sizeof(byte)); - memset(item->addr,0,sizeof(byte)); - item->_malloc=true; - } - break; - case ICONSOLE_VAR_UINT16: - { - item->addr=malloc(sizeof(unsigned short)); - memset(item->addr,0,sizeof(unsigned short)); - item->_malloc=true; - } - break; - case ICONSOLE_VAR_UINT32: - { - item->addr=malloc(sizeof(unsigned int)); - memset(item->addr,0,sizeof(unsigned int)); - item->_malloc=true; - } - break; - case ICONSOLE_VAR_INT16: - { - item->addr=malloc(sizeof(signed short)); - memset(item->addr,0,sizeof(signed short)); - item->_malloc=true; - } - break; - case ICONSOLE_VAR_INT32: - { - item->addr=malloc(sizeof(signed int)); - memset(item->addr,0,sizeof(signed int)); - item->_malloc=true; - } - break; - default: - item->addr = NULL; - item->_malloc = false; - break; - } + case ICONSOLE_VAR_BOOLEAN: + item->data.bool_ = malloc(sizeof(*item->data.bool_)); + *item->data.bool_ = false; + item->_malloc = true; + break; + case ICONSOLE_VAR_BYTE: + item->data.byte_ = malloc(sizeof(*item->data.byte_)); + *item->data.byte_ = 0; + item->_malloc = true; + break; + case ICONSOLE_VAR_UINT16: + item->data.uint16_ = malloc(sizeof(*item->data.uint16_)); + *item->data.uint16_ = 0; + item->_malloc = true; + break; + case ICONSOLE_VAR_UINT32: + item->data.uint32_ = malloc(sizeof(*item->data.uint32_)); + *item->data.uint32_ = 0; + item->_malloc = true; + break; + case ICONSOLE_VAR_INT16: + item->data.int16_ = malloc(sizeof(*item->data.int16_)); + *item->data.int16_ = 0; + item->_malloc = true; + break; + case ICONSOLE_VAR_INT32: + item->data.int32_ = malloc(sizeof(*item->data.int32_)); + *item->data.int32_ = 0; + item->_malloc = true; + break; + case ICONSOLE_VAR_POINTER: + case ICONSOLE_VAR_STRING: /* XXX */ + item->data.addr = NULL; + item->_malloc = false; + break; + default: + assert(0); /* XXX */ + item->data.addr = NULL; + item->_malloc = false; + break; + } item->hook_access = NULL; item->hook_after_change = NULL; @@ -602,217 +585,197 @@ _iconsole_var * IConsoleVarAlloc(byte type) } -void IConsoleVarFree(_iconsole_var * var) +void IConsoleVarFree(_iconsole_var* var) { if (var->_malloc) - free(var->addr); + free(var->data.addr); + free(var->name); free(var); } -void IConsoleVarSetString(_iconsole_var * var, const byte * string) +void IConsoleVarSetString(_iconsole_var* var, const char* string) { - int l; - if (string == NULL) return; - if (var->_malloc) { - free(var->addr); - } + if (var->_malloc) + free(var->data.string_); - l=strlen(string); - var->addr=malloc(l+1); - var->_malloc=true; - memset(var->addr,0,l); - memcpy(var->addr, string, l); - ((byte *)var->addr)[l]=0; + var->data.string_ = strdup(string); + var->_malloc = true; } -void IConsoleVarSetValue(_iconsole_var * var, int value) { +void IConsoleVarSetValue(_iconsole_var* var, int value) { switch (var->type) { - case ICONSOLE_VAR_BOOLEAN: - *(bool *)var->addr = (value != 0); - break; - case ICONSOLE_VAR_BYTE: - *(byte *)var->addr = value; - break; - case ICONSOLE_VAR_UINT16: - *(unsigned short *)var->addr = value; - break; - case ICONSOLE_VAR_UINT32: - *(unsigned int *)var->addr = value; - break; - case ICONSOLE_VAR_INT16: - *(signed short *)var->addr = value; - break; - case ICONSOLE_VAR_INT32: - *(signed int *)var->addr = value; - break; - default: - break; + case ICONSOLE_VAR_BOOLEAN: + *var->data.bool_ = (value != 0); + break; + case ICONSOLE_VAR_BYTE: + *var->data.byte_ = value; + break; + case ICONSOLE_VAR_UINT16: + *var->data.uint16_ = value; + break; + case ICONSOLE_VAR_UINT32: + *var->data.uint32_ = value; + break; + case ICONSOLE_VAR_INT16: + *var->data.int16_ = value; + break; + case ICONSOLE_VAR_INT32: + *var->data.int32_ = value; + break; + default: + assert(0); + break; } } -void IConsoleVarDump(_iconsole_var * var, const byte * dump_desc) +void IConsoleVarDump(const _iconsole_var* var, const char* dump_desc) { - byte var_b; // TYPE BYTE - unsigned short var_ui16; // TYPE UINT16 - unsigned int var_ui32; // TYPE UINT32 - signed short var_i16; // TYPE INT16 - signed int var_i32; // TYPE INT32 - byte * var_s; // TYPE STRING - - if (dump_desc==NULL) dump_desc = var->name; + if (dump_desc == NULL) dump_desc = var->name; switch (var->type) { case ICONSOLE_VAR_BOOLEAN: - { - if (*(bool *)var->addr) { - IConsolePrintF(_iconsole_color_default, "%s = true",dump_desc); - } else { - IConsolePrintF(_iconsole_color_default, "%s = false",dump_desc); - } - } - break; + IConsolePrintF(_iconsole_color_default, "%s = %s", + dump_desc, *var->data.bool_ ? "true" : "false"); + break; + break; case ICONSOLE_VAR_BYTE: - { - var_b=*(byte *)var->addr; - IConsolePrintF(_iconsole_color_default, "%s = %i",dump_desc,var_b); - } - break; + IConsolePrintF(_iconsole_color_default, "%s = %u", + dump_desc, *var->data.byte_); + break; case ICONSOLE_VAR_UINT16: - { - var_ui16=*(unsigned short *)var->addr; - IConsolePrintF(_iconsole_color_default, "%s = %i",dump_desc,var_ui16); - } - break; + IConsolePrintF(_iconsole_color_default, "%s = %u", + dump_desc, *var->data.uint16_); + break; case ICONSOLE_VAR_UINT32: - { - var_ui32=*(unsigned int *)var->addr; - IConsolePrintF(_iconsole_color_default, "%s = %i",dump_desc,var_ui32); - } - break; + IConsolePrintF(_iconsole_color_default, "%s = %u", + dump_desc, *var->data.uint32_); + break; case ICONSOLE_VAR_INT16: - { - var_i16=*(signed short *)var->addr; - IConsolePrintF(_iconsole_color_default, "%s = %i",dump_desc,var_i16); - } - break; + IConsolePrintF(_iconsole_color_default, "%s = %i", + dump_desc, *var->data.int16_); + break; case ICONSOLE_VAR_INT32: - { - var_i32=*(signed int *)var->addr; - IConsolePrintF(_iconsole_color_default, "%s = %i",dump_desc,var_i32); - } - break; + IConsolePrintF(_iconsole_color_default, "%s = %i", + dump_desc, *var->data.int32_); + break; case ICONSOLE_VAR_STRING: - { - var_s=(byte *)var->addr; - IConsolePrintF(_iconsole_color_default, "%s = %s",dump_desc,var_s); - } - break; + IConsolePrintF(_iconsole_color_default, "%s = %s", + dump_desc, var->data.string_); + break; case ICONSOLE_VAR_REFERENCE: - IConsolePrintF(_iconsole_color_default, "%s = @%s",dump_desc,((_iconsole_var *)var->addr)->name); - break; + IConsolePrintF(_iconsole_color_default, "%s = @%s", + dump_desc, var->data.reference_); case ICONSOLE_VAR_UNKNOWN: case ICONSOLE_VAR_POINTER: - { - var_i32=(signed int)((byte *)var->addr); - IConsolePrintF(_iconsole_color_default, "%s = @%i",dump_desc,var_i32); - } - break; - } + IConsolePrintF(_iconsole_color_default, "%s = @%p", + dump_desc, var->data.addr); + break; + case ICONSOLE_VAR_NONE: /* XXX */ + break; + } } // * ************************* * // // * hooking code * // // * ************************* * // -void IConsoleVarHook(const byte * name, byte type, void * proc) +void IConsoleVarHook(const char* name, _iconsole_hook_types type, iconsole_var_hook proc) { - _iconsole_var * hook_var; - hook_var = IConsoleVarGet(name); + _iconsole_var* hook_var = IConsoleVarGet(name); if (hook_var == NULL) return; switch (type) { - case ICONSOLE_HOOK_BEFORE_CHANGE: - hook_var->hook_after_change = proc; - break; - case ICONSOLE_HOOK_AFTER_CHANGE: - hook_var->hook_after_change = proc; - break; - case ICONSOLE_HOOK_ACCESS: - hook_var->hook_access = proc; - break; + case ICONSOLE_HOOK_BEFORE_CHANGE: + hook_var->hook_before_change = proc; + break; + case ICONSOLE_HOOK_AFTER_CHANGE: + hook_var->hook_after_change = proc; + break; + case ICONSOLE_HOOK_ACCESS: + hook_var->hook_access = proc; + break; + case ICONSOLE_HOOK_BEFORE_EXEC: + case ICONSOLE_HOOK_AFTER_EXEC: + assert(0); + break; } } -bool IConsoleVarHookHandle(_iconsole_var * hook_var, byte type) +bool IConsoleVarHookHandle(_iconsole_var* hook_var, _iconsole_hook_types type) { - bool (*proc)(_iconsole_var * hook_var) = NULL; + iconsole_var_hook proc = NULL; switch (type) { - case ICONSOLE_HOOK_BEFORE_CHANGE: - proc = hook_var->hook_before_change; - break; - case ICONSOLE_HOOK_AFTER_CHANGE: - proc = hook_var->hook_after_change; - break; - case ICONSOLE_HOOK_ACCESS: - proc = hook_var->hook_access; - break; + case ICONSOLE_HOOK_BEFORE_CHANGE: + proc = hook_var->hook_before_change; + break; + case ICONSOLE_HOOK_AFTER_CHANGE: + proc = hook_var->hook_after_change; + break; + case ICONSOLE_HOOK_ACCESS: + proc = hook_var->hook_access; + break; + case ICONSOLE_HOOK_BEFORE_EXEC: + case ICONSOLE_HOOK_AFTER_EXEC: + assert(0); + break; } - - if (proc == NULL) { return true;} - - return proc(hook_var); + return proc == NULL ? true : proc(hook_var); } -void IConsoleCmdHook(const byte * name, byte type, void * proc) +void IConsoleCmdHook(const char* name, _iconsole_hook_types type, iconsole_cmd_hook proc) { - _iconsole_cmd * hook_cmd; - hook_cmd = IConsoleCmdGet(name); + _iconsole_cmd* hook_cmd = IConsoleCmdGet(name); if (hook_cmd == NULL) return; switch (type) { - case ICONSOLE_HOOK_AFTER_EXEC: - hook_cmd->hook_after_exec = proc; - break; - case ICONSOLE_HOOK_BEFORE_EXEC: - hook_cmd->hook_before_exec = proc; - break; - case ICONSOLE_HOOK_ACCESS: - hook_cmd->hook_access = proc; - break; + case ICONSOLE_HOOK_AFTER_EXEC: + hook_cmd->hook_after_exec = proc; + break; + case ICONSOLE_HOOK_BEFORE_EXEC: + hook_cmd->hook_before_exec = proc; + break; + case ICONSOLE_HOOK_ACCESS: + hook_cmd->hook_access = proc; + break; + case ICONSOLE_HOOK_BEFORE_CHANGE: + case ICONSOLE_HOOK_AFTER_CHANGE: + assert(0); + break; } } -bool IConsoleCmdHookHandle(_iconsole_cmd * hook_cmd, byte type) +bool IConsoleCmdHookHandle(_iconsole_cmd* hook_cmd, _iconsole_hook_types type) { - bool (*proc)(_iconsole_cmd * hook_cmd) = NULL; + iconsole_cmd_hook proc = NULL; switch (type) { - case ICONSOLE_HOOK_AFTER_EXEC: - proc = hook_cmd->hook_after_exec; - break; - case ICONSOLE_HOOK_BEFORE_EXEC: - proc = hook_cmd->hook_before_exec; - break; - case ICONSOLE_HOOK_ACCESS: - proc = hook_cmd->hook_access; - break; + case ICONSOLE_HOOK_AFTER_EXEC: + proc = hook_cmd->hook_after_exec; + break; + case ICONSOLE_HOOK_BEFORE_EXEC: + proc = hook_cmd->hook_before_exec; + break; + case ICONSOLE_HOOK_ACCESS: + proc = hook_cmd->hook_access; + break; + case ICONSOLE_HOOK_BEFORE_CHANGE: + case ICONSOLE_HOOK_AFTER_CHANGE: + assert(0); + break; } - - if (proc == NULL) { return true;} - - return proc(hook_cmd); + return proc == NULL ? true : proc(hook_cmd); } -void IConsoleCmdExec(const byte* cmdstr) +void IConsoleCmdExec(const char* cmdstr) { - _iconsole_var * (*function)(byte argc, byte* argv[], byte argt[]); - byte * tokens[20]; - byte tokentypes[20]; - byte * tokenstream; - byte * tokenstream_s; - byte execution_mode; - _iconsole_var * var = NULL; - _iconsole_var * result = NULL; - _iconsole_cmd * cmd = NULL; + _iconsole_cmd_addr function; + char* tokens[20]; + byte tokentypes[20]; + char* tokenstream; + char* tokenstream_s; + byte execution_mode; + _iconsole_var* var = NULL; + _iconsole_var* result = NULL; + _iconsole_cmd* cmd = NULL; bool longtoken; bool valid_token; @@ -824,96 +787,102 @@ void IConsoleCmdExec(const byte* cmdstr) //** clearing buffer **// - for (i=0;i<20;i++) { tokens[i]=NULL; tokentypes[i]=ICONSOLE_VAR_NONE; }; - tokenstream_s=tokenstream=malloc(1024); - memset(tokenstream,0,1024); + for (i = 0; i < 20; i++) { + tokens[i] = NULL; + tokentypes[i] = ICONSOLE_VAR_NONE; + } + tokenstream_s = tokenstream = malloc(1024); + memset(tokenstream, 0, 1024); //** parsing **// - longtoken=false; - valid_token=false; - skip_lt_change=false; - l=strlen(cmdstr); - i=0; - c=0; + longtoken = false; + valid_token = false; + skip_lt_change = false; + l = strlen(cmdstr); + i = 0; + c = 0; tokens[c] = tokenstream; - while (i<l) { - if (cmdstr[i]=='"') { + while (i < l) { + if (cmdstr[i] == '"') { if (longtoken) { - if (cmdstr[i+1]=='"') { + if (cmdstr[i + 1] == '"') { i++; *tokenstream = '"'; tokenstream++; - skip_lt_change=true; - } else { - longtoken=!longtoken; - } + skip_lt_change = true; } else { - longtoken=!longtoken; + longtoken = !longtoken; } + } else + longtoken = !longtoken; if (!skip_lt_change) { if (!longtoken) { if (valid_token) { c++; - *tokenstream = 0; + *tokenstream = '\0'; tokenstream++; tokens[c] = tokenstream; valid_token = false; - } } - skip_lt_change=false; } + skip_lt_change=false; } - else if ((!longtoken) && (cmdstr[i]==' ')) { + } else if (!longtoken && cmdstr[i] == ' ') { if (valid_token) { c++; - *tokenstream = 0; + *tokenstream = '\0'; tokenstream++; tokens[c] = tokenstream; valid_token = false; - } } - else { - valid_token=true; + } else { + valid_token = true; *tokenstream = cmdstr[i]; tokenstream++; - } - i++; } + i++; + } tokenstream--; - if (!(*tokenstream==0)) { + if (*tokenstream != '\0') { c++; tokenstream++; - *tokenstream = 0; - } + *tokenstream = '\0'; + } //** interpreting **// - for (i=0; i<c; i++) { - tokentypes[i]=ICONSOLE_VAR_UNKNOWN; - if (tokens[i]!=NULL) if (i>0) if (strlen((char *) tokens[i])>0) { - if (tokens[i][0]=='*') { - if ((i==2) && (tokentypes[1]==ICONSOLE_VAR_UNKNOWN) && (strcmp(tokens[1],"<<")==0)) { - // dont change the variable to an pointer if execution_mode 4 is being prepared - // this is used to assign one variable the value of the other one [token 0 and 2] - } else { + for (i = 0; i < c; i++) { + tokentypes[i] = ICONSOLE_VAR_UNKNOWN; + if (tokens[i] != NULL && i > 0 && strlen(tokens[i]) > 0) { + if (tokens[i][0] == '*') { + if ((i == 2) && (tokentypes[1] == ICONSOLE_VAR_UNKNOWN) && + (strcmp(tokens[1], "<<") == 0)) { + // don't change the variable to an pointer if execution_mode 4 is + // being prepared + // this is used to assign one variable the value of the other one + // [token 0 and 2] + /* XXX empty? */ + } else { var = IConsoleVarGet(tokens[i]); - if (var!=NULL) { - tokens[i]=(byte *)var->addr; - tokentypes[i]=var->type; - } + if (var != NULL) { + assert(0); /* XXX */ + //tokens[i] = var->addr; /* XXX ? */ + tokentypes[i] = var->type; } } - if (tokens[i]!=NULL) if (tokens[i][0]=='@') if (tokens[i][1]=='*') { - var = IConsoleVarGet(tokens[i]+1); - if (var!=NULL) { - tokens[i]=(byte *)var; - tokentypes[i]=ICONSOLE_VAR_REFERENCE; - } + } + if (tokens[i] != NULL && tokens[i][0] == '@' && tokens[i][1] == '*') { + var = IConsoleVarGet(tokens[i] + 1); + if (var != NULL) { + assert(0); /* XXX */ + //tokens[i] = var; /* XXX wtf? incompatible pointer type! */ + tokentypes[i] = ICONSOLE_VAR_REFERENCE; } } } + } execution_mode=0; @@ -922,12 +891,12 @@ void IConsoleCmdExec(const byte* cmdstr) if (cmd != NULL) function = cmd->addr; if (function != NULL) { - execution_mode=1; // this is a command - } else { + execution_mode = 1; // this is a command + } else { var = IConsoleVarGet(tokens[0]); if (var != NULL) { - execution_mode=2; // this is a variable - if (c>2) if (strcmp(tokens[1],"<<")==0) { + execution_mode = 2; // this is a variable + if (c > 2 && strcmp(tokens[1], "<<") == 0) { // this is command to variable mode [normal] function = NULL; @@ -935,335 +904,292 @@ void IConsoleCmdExec(const byte* cmdstr) if (cmd != NULL) function = cmd->addr; if (function != NULL) { - execution_mode=3; - } else { + execution_mode = 3; + } else { result = IConsoleVarGet(tokens[2]); - if (result != NULL) { + if (result != NULL) execution_mode=4; - } - } } } } + } //** executing **// - if (_stdlib_con_developer) IConsolePrintF(_iconsole_color_debug,"CONDEBUG: execution_mode: %i",execution_mode); + if (_stdlib_con_developer) + IConsolePrintF(_iconsole_color_debug, "CONDEBUG: execution_mode: %i", + execution_mode); switch (execution_mode) { - case 0: - { - // not found - IConsoleError("command or variable not found"); - } - break; - case 1: - if (IConsoleCmdHookHandle(cmd,ICONSOLE_HOOK_ACCESS)) { - // execution with command syntax - IConsoleCmdHookHandle(cmd,ICONSOLE_HOOK_BEFORE_EXEC); - result = function(c,tokens,tokentypes); - if (result!=NULL) { - IConsoleVarDump(result,"result"); - IConsoleVarFree(result); + case 0: + // not found + IConsoleError("command or variable not found"); + break; + case 1: + if (IConsoleCmdHookHandle(cmd, ICONSOLE_HOOK_ACCESS)) { + // execution with command syntax + IConsoleCmdHookHandle(cmd, ICONSOLE_HOOK_BEFORE_EXEC); + result = function(c, tokens, tokentypes); + if (result != NULL) { + IConsoleVarDump(result, "result"); + IConsoleVarFree(result); + } + IConsoleCmdHookHandle(cmd, ICONSOLE_HOOK_AFTER_EXEC); + break; } - IConsoleCmdHookHandle(cmd,ICONSOLE_HOOK_AFTER_EXEC); - } - break; - case 2: + case 2: { - // execution with variable syntax - if (IConsoleVarHookHandle(var,ICONSOLE_HOOK_ACCESS)) if ((c==2) || (c==3)) { - // ** variable modifications ** // - IConsoleVarHookHandle(var,ICONSOLE_HOOK_BEFORE_CHANGE); - switch (var->type) { - case ICONSOLE_VAR_BOOLEAN: + // execution with variable syntax + if (IConsoleVarHookHandle(var, ICONSOLE_HOOK_ACCESS) && (c == 2 || c == 3)) { + // ** variable modifications ** // + IConsoleVarHookHandle(var, ICONSOLE_HOOK_BEFORE_CHANGE); + switch (var->type) { + case ICONSOLE_VAR_BOOLEAN: { - if (strcmp(tokens[1],"=")==0) { - if (c==3) { - *(bool *)var->addr=(atoi((char *) tokens[2])!=0); - IConsoleVarDump(var,NULL); + if (strcmp(tokens[1], "=") == 0) { + if (c == 3) { + *var->data.bool_ = (atoi(tokens[2]) != 0); } else { - *(bool *)var->addr=false; - IConsoleVarDump(var,NULL); + *var->data.bool_ = false; } + IConsoleVarDump(var, NULL); + } else if (strcmp(tokens[1], "++") == 0) { + *var->data.bool_ = !*var->data.bool_; /* XXX ++ on bool */ + IConsoleVarDump(var, NULL); + } else if (strcmp(tokens[1], "--") == 0) { + *var->data.bool_ = !*var->data.bool_; /* XXX -- on bool */ + IConsoleVarDump(var, NULL); } - else if (strcmp(tokens[1],"++")==0) { - *(bool *)var->addr=!*(bool *)var->addr; - IConsoleVarDump(var,NULL); - } - else if (strcmp(tokens[1],"--")==0) { - *(bool *)var->addr=!*(bool *)var->addr; - IConsoleVarDump(var,NULL); - } - else { IConsoleError("operation not supported"); } + else + IConsoleError("operation not supported"); + break; } - break; - case ICONSOLE_VAR_BYTE: + case ICONSOLE_VAR_BYTE: { - if (strcmp(tokens[1],"=")==0) { - if (c==3) { - *(byte *)var->addr=atoi((char *) tokens[2]); - IConsoleVarDump(var,NULL); - } else { - *(byte *)var->addr=0; - IConsoleVarDump(var,NULL); - } - } - else if (strcmp(tokens[1],"++")==0) { - (*(byte *)var->addr)++; - IConsoleVarDump(var,NULL); - } - else if (strcmp(tokens[1],"--")==0) { - (*(byte *)var->addr)--; - IConsoleVarDump(var,NULL); + if (strcmp(tokens[1], "=") == 0) { + if (c == 3) + *var->data.byte_ = atoi(tokens[2]); + else + *var->data.byte_ = 0; + IConsoleVarDump(var, NULL); + } else if (strcmp(tokens[1], "++") == 0) { + ++*var->data.byte_; + IConsoleVarDump(var, NULL); + } else if (strcmp(tokens[1], "--")==0) { + --*var->data.byte_; + IConsoleVarDump(var, NULL); } - else { IConsoleError("operation not supported"); } + else + IConsoleError("operation not supported"); + break; } - break; - case ICONSOLE_VAR_UINT16: + case ICONSOLE_VAR_UINT16: { - if (strcmp(tokens[1],"=")==0) { - if (c==3) { - *(unsigned short *)var->addr=atoi((char *) tokens[2]); - IConsoleVarDump(var,NULL); - } else { - *(unsigned short *)var->addr=0; - IConsoleVarDump(var,NULL); - } - } - else if (strcmp(tokens[1],"++")==0) { - (*(unsigned short *)var->addr)++; - IConsoleVarDump(var,NULL); + if (strcmp(tokens[1], "=") == 0) { + if (c == 3) + *var->data.uint16_ = atoi(tokens[2]); + else + *var->data.uint16_ = 0; + IConsoleVarDump(var, NULL); + } else if (strcmp(tokens[1], "++") == 0) { + ++*var->data.uint16_; + IConsoleVarDump(var, NULL); + } else if (strcmp(tokens[1], "--") == 0) { + --*var->data.uint16_; + IConsoleVarDump(var, NULL); } - else if (strcmp(tokens[1],"--")==0) { - (*(unsigned short *)var->addr)--; - IConsoleVarDump(var,NULL); - } - else { IConsoleError("operation not supported"); } + else + IConsoleError("operation not supported"); + break; } - break; - case ICONSOLE_VAR_UINT32: + case ICONSOLE_VAR_UINT32: { - if (strcmp(tokens[1],"=")==0) { - if (c==3) { - *(unsigned int *)var->addr=atoi((char *) tokens[2]); - IConsoleVarDump(var,NULL); - } else { - *(unsigned int *)var->addr=0; - IConsoleVarDump(var,NULL); - } - } - else if (strcmp(tokens[1],"++")==0) { - (*(unsigned int *)var->addr)++; - IConsoleVarDump(var,NULL); + if (strcmp(tokens[1], "=") == 0) { + if (c == 3) + *var->data.uint32_ = atoi(tokens[2]); + else + *var->data.uint32_ = 0; + IConsoleVarDump(var, NULL); + } else if (strcmp(tokens[1], "++") == 0) { + ++*var->data.uint32_; + IConsoleVarDump(var, NULL); + } else if (strcmp(tokens[1], "--") == 0) { + --*var->data.uint32_; + IConsoleVarDump(var, NULL); } - else if (strcmp(tokens[1],"--")==0) { - (*(unsigned int *)var->addr)--; - IConsoleVarDump(var,NULL); - } - else { IConsoleError("operation not supported"); } + else + IConsoleError("operation not supported"); + break; } - break; - case ICONSOLE_VAR_INT16: + case ICONSOLE_VAR_INT16: { - if (strcmp(tokens[1],"=")==0) { - if (c==3) { - *(signed short *)var->addr=atoi((char *) tokens[2]); - IConsoleVarDump(var,NULL); - } else { - *(signed short *)var->addr=0; - IConsoleVarDump(var,NULL); - } + if (strcmp(tokens[1], "=") == 0) { + if (c == 3) + *var->data.int16_ = atoi(tokens[2]); + else + *var->data.int16_ = 0; + IConsoleVarDump(var, NULL); + } else if (strcmp(tokens[1], "++") == 0) { + ++*var->data.int16_; + IConsoleVarDump(var, NULL); + } else if (strcmp(tokens[1], "--") == 0) { + --*var->data.int16_; + IConsoleVarDump(var, NULL); } - else if (strcmp(tokens[1],"++")==0) { - (*(signed short *)var->addr)++; - IConsoleVarDump(var,NULL); - } - else if (strcmp(tokens[1],"--")==0) { - (*(signed short *)var->addr)--; - IConsoleVarDump(var,NULL); - } - else { IConsoleError("operation not supported"); } + else + IConsoleError("operation not supported"); + break; } - break; - case ICONSOLE_VAR_INT32: + case ICONSOLE_VAR_INT32: { - if (strcmp(tokens[1],"=")==0) { - if (c==3) { - *(signed int *)var->addr=atoi((char *) tokens[2]); - IConsoleVarDump(var,NULL); - } else { - *(signed int *)var->addr=0; - IConsoleVarDump(var,NULL); - } + if (strcmp(tokens[1], "=") == 0) { + if (c == 3) + *var->data.int32_ = atoi(tokens[2]); + else + *var->data.int32_ = 0; + IConsoleVarDump(var, NULL); + } else if (strcmp(tokens[1], "++") == 0) { + ++*var->data.int32_; + IConsoleVarDump(var, NULL); + } else if (strcmp(tokens[1], "--") == 0) { + --*var->data.int32_; + IConsoleVarDump(var, NULL); } - else if (strcmp(tokens[1],"++")==0) { - (*(signed int *)var->addr)++; - IConsoleVarDump(var,NULL); - } - else if (strcmp(tokens[1],"--")==0) { - (*(signed int *)var->addr)--; - IConsoleVarDump(var,NULL); - } - else { IConsoleError("operation not supported"); } + else { IConsoleError("operation not supported"); } + break; } - break; - case ICONSOLE_VAR_STRING: + case ICONSOLE_VAR_STRING: { - if (strcmp(tokens[1],"=")==0) { - if (c==3) { - IConsoleVarSetString(var, tokens[2]); - IConsoleVarDump(var,NULL); - } else { - IConsoleVarSetString(var, ""); - IConsoleVarDump(var,NULL); - } + if (strcmp(tokens[1], "=") == 0) { + if (c == 3) + IConsoleVarSetString(var, tokens[2]); + else + IConsoleVarSetString(var, ""); + IConsoleVarDump(var, NULL); } - else { IConsoleError("operation not supported"); } + else + IConsoleError("operation not supported"); + break; } - break; - case ICONSOLE_VAR_POINTER: + case ICONSOLE_VAR_POINTER: { - if (strcmp(tokens[1],"=")==0) { - if (c==3) { - if (tokentypes[2]==ICONSOLE_VAR_UNKNOWN) { - var->addr = (void *)atoi(tokens[2]); - } else { - var->addr = (void *)tokens[2]; - } - IConsoleVarDump(var,NULL); - } else { - var->addr = NULL; - IConsoleVarDump(var,NULL); - } - } - else if (strcmp(tokens[1],"++")==0) { - var->addr = ((char *)var->addr)+1; - IConsoleVarDump(var,NULL); + if (strcmp(tokens[1], "=") == 0) { + if (c == 3) { + if (tokentypes[2] == ICONSOLE_VAR_UNKNOWN) + var->data.addr = (void*)atoi(tokens[2]); /* XXX ? */ + else + var->data.addr = (void*)tokens[2]; /* XXX ? */ + } else + var->data.addr = NULL; + IConsoleVarDump(var, NULL); + } else if (strcmp(tokens[1], "++") == 0) { + ++*(char*)&var->data.addr; /* XXX ++ on an arbitrary pointer? */ + IConsoleVarDump(var, NULL); + } else if (strcmp(tokens[1], "--") == 0) { + --*(char*)&var->data.addr; /* XXX -- on an arbitrary pointer? */ + IConsoleVarDump(var, NULL); } - else if (strcmp(tokens[1],"--")==0) { - var->addr = ((char *)var->addr)-1;; - IConsoleVarDump(var,NULL); - } - else { IConsoleError("operation not supported"); } + else + IConsoleError("operation not supported"); + break; } - break; + case ICONSOLE_VAR_NONE: /* XXX */ + case ICONSOLE_VAR_REFERENCE: /* XXX */ + case ICONSOLE_VAR_UNKNOWN: /* XXX */ + break; } - IConsoleVarHookHandle(var,ICONSOLE_HOOK_AFTER_CHANGE); - } - if (c==1) { - // ** variable output ** // - IConsoleVarDump(var,NULL); + IConsoleVarHookHandle(var, ICONSOLE_HOOK_AFTER_CHANGE); } + if (c == 1) // ** variable output ** // + IConsoleVarDump(var, NULL); + break; } - break; - case 3: - case 4: + case 3: + case 4: { - // execute command with result or assign a variable - if (execution_mode==3) { - if (IConsoleCmdHookHandle(cmd,ICONSOLE_HOOK_ACCESS)) { + // execute command with result or assign a variable + if (execution_mode == 3) { + if (IConsoleCmdHookHandle(cmd, ICONSOLE_HOOK_ACCESS)) { int i; int diff; - void * temp; + void* temp; byte temp2; // tokenshifting - for (diff=0; diff<2; diff++) { - temp=tokens[0]; - temp2=tokentypes[0]; - for (i=1; i<20; i++) { - tokens[i-1]=tokens[i]; - tokentypes[i-1]=tokentypes[i]; + for (diff = 0; diff < 2; diff++) { + temp = tokens[0]; + temp2 = tokentypes[0]; + for (i = 0; i < 19; i++) { + tokens[i] = tokens[i + 1]; + tokentypes[i] = tokentypes[i + 1]; } - tokens[19]=temp; - tokentypes[19]=temp2; + tokens[19] = temp; + tokentypes[19] = temp2; } - IConsoleCmdHookHandle(cmd,ICONSOLE_HOOK_BEFORE_EXEC); - result = function(c,tokens,tokentypes); - IConsoleCmdHookHandle(cmd,ICONSOLE_HOOK_AFTER_EXEC); + IConsoleCmdHookHandle(cmd, ICONSOLE_HOOK_BEFORE_EXEC); + result = function(c, tokens, tokentypes); + IConsoleCmdHookHandle(cmd, ICONSOLE_HOOK_AFTER_EXEC); } else - execution_mode=255; + execution_mode = 255; } - if (IConsoleVarHookHandle(var,ICONSOLE_HOOK_ACCESS)) if (result!=NULL) { - if (result ->type != var -> type) { - IConsoleError("variable type missmatch"); - } else { - IConsoleVarHookHandle(var,ICONSOLE_HOOK_BEFORE_CHANGE); - switch (result->type) { - case ICONSOLE_VAR_BOOLEAN: - { - (*(bool *)var->addr)=(*(bool *)result->addr); - IConsoleVarDump(var,NULL); - } - break; - case ICONSOLE_VAR_BYTE: - { - (*(byte *)var->addr)=(*(byte *)result->addr); - IConsoleVarDump(var,NULL); - } - break; - case ICONSOLE_VAR_UINT16: - { - (*(unsigned short *)var->addr)=(*(unsigned short *)result->addr); - IConsoleVarDump(var,NULL); - } - break; - case ICONSOLE_VAR_UINT32: - { - (*(unsigned int *)var->addr)=(*(unsigned int *)result->addr); - IConsoleVarDump(var,NULL); - } - break; - case ICONSOLE_VAR_INT16: - { - (*(signed short *)var->addr)=(*(signed short *)result->addr); - IConsoleVarDump(var,NULL); - } - break; - case ICONSOLE_VAR_INT32: - { - (*(signed int *)var->addr)=(*(signed int *)result->addr); - IConsoleVarDump(var,NULL); - } - break; - case ICONSOLE_VAR_POINTER: - { - var->addr=result->addr; - IConsoleVarDump(var,NULL); - } - break; - case ICONSOLE_VAR_STRING: - { - IConsoleVarSetString(var,result->addr); - IConsoleVarDump(var,NULL); - } - break; - default: - { + if (IConsoleVarHookHandle(var, ICONSOLE_HOOK_ACCESS) && result != NULL) { + if (result->type != var->type) { IConsoleError("variable type missmatch"); + } else { + IConsoleVarHookHandle(var, ICONSOLE_HOOK_BEFORE_CHANGE); + switch (result->type) { + case ICONSOLE_VAR_BOOLEAN: + *var->data.bool_ = *result->data.bool_; + IConsoleVarDump(var, NULL); + break; + case ICONSOLE_VAR_BYTE: + *var->data.byte_ = *result->data.byte_; + IConsoleVarDump(var, NULL); + break; + case ICONSOLE_VAR_UINT16: + *var->data.uint16_ = *result->data.uint16_; + IConsoleVarDump(var, NULL); + break; + case ICONSOLE_VAR_UINT32: + *var->data.uint32_ = *result->data.uint32_; + IConsoleVarDump(var, NULL); + break; + case ICONSOLE_VAR_INT16: + *var->data.int16_ = *result->data.int16_; + IConsoleVarDump(var, NULL); + break; + case ICONSOLE_VAR_INT32: + *var->data.int32_ = *result->data.int32_; + IConsoleVarDump(var, NULL); + break; + case ICONSOLE_VAR_POINTER: + var->data.addr = result->data.addr; + IConsoleVarDump(var, NULL); + break; + case ICONSOLE_VAR_STRING: + IConsoleVarSetString(var, result->data.string_); + IConsoleVarDump(var, NULL); + break; + default: + IConsoleError("variable type missmatch"); + break; } - break; - } - IConsoleVarHookHandle(var,ICONSOLE_HOOK_AFTER_CHANGE); + IConsoleVarHookHandle(var, ICONSOLE_HOOK_AFTER_CHANGE); } - if (execution_mode==3) { - IConsoleVarFree(result); - result = NULL; + if (execution_mode == 3) { + IConsoleVarFree(result); + result = NULL; } } - - } - break; - default: - { - // execution mode invalid - IConsoleError("invalid execution mode"); + break; } + default: + // execution mode invalid + IConsoleError("invalid execution mode"); + break; } //** freeing the tokens **// - for (i=0;i<20;i++) tokens[i]=NULL; + for (i = 0; i < 20; i++) tokens[i] = NULL; /* XXX wtf? */ free(tokenstream_s); - } @@ -1,17 +1,10 @@ #ifndef CONSOLE_H #define CONSOLE_H -// ** console ** // - -enum { - ICONSOLE_OPENED=0, - ICONSOLE_CLOSED, -} _iconsole_modes; - // ** console parser ** // -enum { - ICONSOLE_VAR_NONE=0, +typedef enum _iconsole_var_types { + ICONSOLE_VAR_NONE, ICONSOLE_VAR_BOOLEAN, ICONSOLE_VAR_BYTE, ICONSOLE_VAR_UINT16, @@ -21,47 +14,65 @@ enum { ICONSOLE_VAR_STRING, ICONSOLE_VAR_POINTER, ICONSOLE_VAR_REFERENCE, - ICONSOLE_VAR_UNKNOWN, + ICONSOLE_VAR_UNKNOWN } _iconsole_var_types; -enum { +typedef enum _iconsole_hook_types { ICONSOLE_HOOK_ACCESS, ICONSOLE_HOOK_BEFORE_CHANGE, ICONSOLE_HOOK_BEFORE_EXEC, ICONSOLE_HOOK_AFTER_CHANGE, - ICONSOLE_HOOK_AFTER_EXEC, + ICONSOLE_HOOK_AFTER_EXEC } _iconsole_hook_types; -typedef struct { - // -------------- // - void * addr; - byte * name; - // -------------- // - void * hook_access; - void * hook_before_exec; - void * hook_after_exec; - // -------------- // - void * _next; - } _iconsole_cmd; +struct _iconsole_var; +typedef bool (*iconsole_var_hook)(struct _iconsole_var* hook_var); -typedef struct { +typedef struct _iconsole_var { // --------------- // - void * addr; - const byte * name; - byte type; + union { + void* addr; + bool* bool_; + byte* byte_; + uint16* uint16_; + uint32* uint32_; + int16* int16_; + int32* int32_; + char* string_; + struct _iconsole_var* reference_; + } data; + char* name; + _iconsole_var_types type; // -------------- // - void * hook_access; - void * hook_before_change; - void * hook_after_change; + iconsole_var_hook hook_access; + iconsole_var_hook hook_before_change; + iconsole_var_hook hook_after_change; // -------------- // - void * _next; + struct _iconsole_var* _next; bool _malloc; - } _iconsole_var; +} _iconsole_var; + +struct _iconsole_cmd; +typedef bool (*iconsole_cmd_hook)(struct _iconsole_cmd* hook_cmd); + +typedef _iconsole_var* (*_iconsole_cmd_addr)(byte argc, char* argv[], byte argt[]); + +typedef struct _iconsole_cmd { + // -------------- // + _iconsole_cmd_addr addr; + char* name; + // -------------- // + iconsole_cmd_hook hook_access; + iconsole_cmd_hook hook_before_exec; + iconsole_cmd_hook hook_after_exec; + // -------------- // + void* _next; +} _iconsole_cmd; // ** console parser ** // -_iconsole_cmd * _iconsole_cmds; // list of registred commands -_iconsole_var * _iconsole_vars; // list of registred vars +_iconsole_cmd* _iconsole_cmds; // list of registred commands +_iconsole_var* _iconsole_vars; // list of registred vars // ** console colors ** // VARDEF byte _iconsole_color_default; @@ -72,58 +83,57 @@ VARDEF byte _iconsole_color_commands; // ** ttd.c functions ** // -void SetDebugString(const char *s); +void SetDebugString(const char* s); // ** console functions ** // -void IConsoleClearCommand(); -void IConsoleInit(); -void IConsoleClear(); -void IConsoleFree(); -void IConsoleResize(); -void IConsoleSwitch(); -void IConsoleClose(); -void IConsoleOpen(); +void IConsoleInit(void); +void IConsoleClear(void); +void IConsoleFree(void); +void IConsoleResize(void); +void IConsoleSwitch(void); +void IConsoleClose(void); +void IConsoleOpen(void); // ** console cmd buffer ** // -void IConsoleCmdBufferAdd(const byte *cmd); +void IConsoleCmdBufferAdd(const char* cmd); void IConsoleCmdBufferNavigate(signed char direction); // ** console output ** // -void IConsolePrint(byte color_code, const byte* string); -void CDECL IConsolePrintF(byte color_code, const char *s, ...); -void IConsoleDebug(byte* string); -void IConsoleError(const byte* string); -void IConsoleWarning(const byte* string); +void IConsolePrint(byte color_code, const char* string); +void CDECL IConsolePrintF(byte color_code, const char* s, ...); +void IConsoleDebug(const char* string); +void IConsoleError(const char* string); +void IConsoleWarning(const char* string); // *** Commands *** // -void IConsoleCmdRegister(const byte * name, void * addr); -_iconsole_cmd * IConsoleCmdGet(const byte * name); +void IConsoleCmdRegister(const char* name, _iconsole_cmd_addr addr); +_iconsole_cmd* IConsoleCmdGet(const char* name); // *** Variables *** // -void IConsoleVarRegister(const byte * name, void * addr, byte type); -void IConsoleVarMemRegister(const byte * name, byte type); -void IConsoleVarInsert(_iconsole_var * var, const byte * name); -_iconsole_var * IConsoleVarGet(const byte * name); -_iconsole_var * IConsoleVarAlloc(byte type); -void IConsoleVarFree(_iconsole_var * var); -void IConsoleVarSetString(_iconsole_var * var, const byte * string); -void IConsoleVarSetValue(_iconsole_var * var, int value); -void IConsoleVarDump(_iconsole_var * var, const byte * dump_desc); +void IConsoleVarRegister(const char* name, void* addr, _iconsole_var_types type); +void IConsoleVarMemRegister(const char* name, _iconsole_var_types type); +void IConsoleVarInsert(_iconsole_var* var, const char* name); +_iconsole_var* IConsoleVarGet(const char* name); +_iconsole_var* IConsoleVarAlloc(_iconsole_var_types type); +void IConsoleVarFree(_iconsole_var* var); +void IConsoleVarSetString(_iconsole_var* var, const char* string); +void IConsoleVarSetValue(_iconsole_var* var, int value); +void IConsoleVarDump(const _iconsole_var* var, const char* dump_desc); // *** Parser *** // -void IConsoleCmdExec(const byte* cmdstr); +void IConsoleCmdExec(const char* cmdstr); // ** console std lib ** // -void IConsoleStdLibRegister(); +void IConsoleStdLibRegister(void); // ** hook code ** // -void IConsoleVarHook(const byte * name, byte type, void * proc); -void IConsoleCmdHook(const byte * name, byte type, void * proc); -bool IConsoleVarHookHandle(_iconsole_var * hook_var, byte type); -bool IConsoleCmdHookHandle(_iconsole_cmd * hook_cmd, byte type); +void IConsoleVarHook(const char* name, _iconsole_hook_types type, iconsole_var_hook proc); +void IConsoleCmdHook(const char* name, _iconsole_hook_types type, iconsole_cmd_hook proc); +bool IConsoleVarHookHandle(_iconsole_var* hook_var, _iconsole_hook_types type); +bool IConsoleCmdHookHandle(_iconsole_cmd* hook_cmd, _iconsole_hook_types type); #endif /* CONSOLE_H */ diff --git a/console_cmds.c b/console_cmds.c index c9fc837fe..548f5982e 100644 --- a/console_cmds.c +++ b/console_cmds.c @@ -7,7 +7,7 @@ #include "variables.h" #if defined(WIN32) -# define ENABLE_NETWORK +# define ENABLE_NETWORK #endif @@ -17,20 +17,20 @@ static bool _script_running; // ** console command / variable defines ** // -#define DEF_CONSOLE_CMD(yyyy) static _iconsole_var * yyyy(byte argc, byte* argv[], byte argt[]) +#define DEF_CONSOLE_CMD(yyyy) static _iconsole_var * yyyy(byte argc, char* argv[], byte argt[]) #define DEF_CONSOLE_CMD_HOOK(yyyy) static bool yyyy(_iconsole_cmd * hookcmd) #define DEF_CONSOLE_VAR_HOOK(yyyy) static bool yyyy(_iconsole_var * hookvar) // ** supporting functions ** // -static int32 GetArgumentInteger(byte *arg) +static uint32 GetArgumentInteger(const char* arg) { - int32 result; - sscanf((char *)arg, "%u", &result); + uint32 result; + sscanf(arg, "%u", &result); if (result == 0 && arg[0] == '0' && arg[1] == 'x') - sscanf((char *)arg, "%x", &result); + sscanf(arg, "%x", &result); return result; } @@ -44,7 +44,7 @@ DEF_CONSOLE_CMD_HOOK(ConCmdHookNoNetwork) if (_networking) { IConsoleError("this command is forbidden in multiplayer"); return false; - } + } return true; } @@ -54,7 +54,7 @@ DEF_CONSOLE_VAR_HOOK(ConVarHookNoNetwork) if (_networking) { IConsoleError("this variable is forbidden in multiplayer"); return false; - } + } return true; } #endif @@ -64,7 +64,7 @@ DEF_CONSOLE_VAR_HOOK(ConVarHookNoNetClient) if (!_networking_server) { IConsoleError("this variable only makes sense for a network server"); return false; - } + } return true; } @@ -78,6 +78,7 @@ DEF_CONSOLE_CMD(ConResetEngines) return 0; } +#ifdef _DEBUG DEF_CONSOLE_CMD(ConResetTile) { if (argc == 2) { @@ -87,6 +88,7 @@ DEF_CONSOLE_CMD(ConResetTile) return 0; } +#endif DEF_CONSOLE_CMD(ConScrollToTile) { @@ -105,9 +107,9 @@ DEF_CONSOLE_CMD(ConScrollToTile) DEF_CONSOLE_CMD(ConNetworkConnect) { - byte * ip; - const byte *port = NULL; - const byte *player = NULL; + char* ip; + const char *port = NULL; + const char *player = NULL; uint16 rport; if (argc<2) return NULL; @@ -117,14 +119,14 @@ DEF_CONSOLE_CMD(ConNetworkConnect) ParseConnectionString(&player, &port, ip); - IConsolePrintF(_iconsole_color_default,"Connecting to %s...", ip); + IConsolePrintF(_iconsole_color_default, "Connecting to %s...", ip); if (player!=NULL) { _network_playas = atoi(player); - IConsolePrintF(_iconsole_color_default," player-no: %s", player); + IConsolePrintF(_iconsole_color_default, " player-no: %s", player); } if (port!=NULL) { rport = atoi(port); - IConsolePrintF(_iconsole_color_default," port: %s", port); + IConsolePrintF(_iconsole_color_default, " port: %s", port); } NetworkCoreConnectGame(ip, rport); @@ -146,7 +148,7 @@ DEF_CONSOLE_CMD(ConExec) if (argc<2) return NULL; doerror = true; - _script_file = fopen(argv[1],"rb"); + _script_file = fopen(argv[1], "rb"); if (_script_file == NULL) { if (argc>2) if (atoi(argv[2])==0) doerror=false; @@ -157,11 +159,8 @@ DEF_CONSOLE_CMD(ConExec) _script_running = true; while (!feof(_script_file) && _script_running) { - - fgets((char *)&cmd, 1024, _script_file); - - IConsoleCmdExec((byte *) &cmd); - + fgets(cmd, sizeof(cmd), _script_file); + IConsoleCmdExec(cmd); } _script_running = false; @@ -182,13 +181,14 @@ DEF_CONSOLE_CMD(ConReturn) DEF_CONSOLE_CMD(ConScript) { extern FILE* _iconsole_output_file; - if (_iconsole_output_file!=NULL) { - IConsolePrintF(_iconsole_color_default,"file output complete"); + if (_iconsole_output_file != NULL) { + IConsolePrintF(_iconsole_color_default, "file output complete"); fclose(_iconsole_output_file); } else { - if (argc<2) return NULL; - IConsolePrintF(_iconsole_color_default,"file output started to: %s",argv[1]); - _iconsole_output_file = fopen(argv[1],"ab"); + if (argc < 2) return NULL; + IConsolePrintF(_iconsole_color_default, "file output started to: %s", + argv[1]); + _iconsole_output_file = fopen(argv[1], "ab"); if (_iconsole_output_file == NULL) IConsoleError("could not open file"); } return NULL; @@ -197,93 +197,94 @@ DEF_CONSOLE_CMD(ConScript) DEF_CONSOLE_CMD(ConEcho) { - if (argc<2) return NULL; + if (argc < 2) return NULL; IConsolePrint(_iconsole_color_default, argv[1]); return NULL; } DEF_CONSOLE_CMD(ConEchoC) { - if (argc<3) return NULL; + if (argc < 3) return NULL; IConsolePrint(atoi(argv[1]), argv[2]); return NULL; } DEF_CONSOLE_CMD(ConPrintF) { - if (argc<3) return NULL; - IConsolePrintF(_iconsole_color_default, argv[1] ,argv[2],argv[3],argv[4],argv[5],argv[6],argv[7],argv[8],argv[9],argv[10],argv[11],argv[12],argv[13],argv[14],argv[15],argv[16],argv[17],argv[18],argv[19]); + if (argc < 3) return NULL; + IConsolePrintF(_iconsole_color_default, argv[1] , argv[2], argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], argv[12], argv[13], argv[14], argv[15], argv[16], argv[17], argv[18], argv[19]); /* XXX ugh... */ return NULL; } DEF_CONSOLE_CMD(ConPrintFC) { - if (argc<3) return NULL; - IConsolePrintF(atoi(argv[1]), argv[2] ,argv[3],argv[4],argv[5],argv[6],argv[7],argv[8],argv[9],argv[10],argv[11],argv[12],argv[13],argv[14],argv[15],argv[16],argv[17],argv[18],argv[19]); + if (argc < 3) return NULL; + IConsolePrintF(atoi(argv[1]), argv[2] , argv[3], argv[4], argv[5], argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], argv[12], argv[13], argv[14], argv[15], argv[16], argv[17], argv[18], argv[19]); /* XXX ugh... */ return NULL; } DEF_CONSOLE_CMD(ConScreenShot) { - if (argc<2) { - _make_screenshot=1; + if (argc < 2) { + _make_screenshot = 1; } else { - if (strcmp(argv[1],"big")==0) { + if (strcmp(argv[1], "big") == 0) _make_screenshot=2; - } - if (strcmp(argv[1],"no_con")==0) { + if (strcmp(argv[1], "no_con") == 0) { IConsoleClose(); - _make_screenshot=1; - } + _make_screenshot = 1; } + } return NULL; } DEF_CONSOLE_CMD(ConInfoVar) { - if (argc<2) return NULL; - if (argt[1]!=ICONSOLE_VAR_REFERENCE) { + if (argc < 2) return NULL; + if (argt[1] != ICONSOLE_VAR_REFERENCE) { IConsoleError("first argument has to be a variable reference"); - } else { - _iconsole_var * item; - item = (_iconsole_var *) argv[1]; - IConsolePrintF(_iconsole_color_default,"var_name: %s",item->name); - IConsolePrintF(_iconsole_color_default,"var_type: %i",item->type); - IConsolePrintF(_iconsole_color_default,"var_addr: %i",item->addr); - if (item->_malloc) IConsolePrintF(_iconsole_color_default,"var_malloc: internal"); - else IConsolePrintF(_iconsole_color_default, "var_malloc: external"); + } else { + _iconsole_var* item; + item = (_iconsole_var*)argv[1]; + IConsolePrintF(_iconsole_color_default, "var_name: %s", item->name); + IConsolePrintF(_iconsole_color_default, "var_type: %i", item->type); + IConsolePrintF(_iconsole_color_default, "var_addr: %i", item->data.addr); + if (item->_malloc) + IConsolePrintF(_iconsole_color_default, "var_malloc: internal"); + else + IConsolePrintF(_iconsole_color_default, "var_malloc: external"); if (item->hook_access) IConsoleWarning("var_access hooked"); if (item->hook_before_change) IConsoleWarning("var_before_change hooked"); if (item->hook_after_change) IConsoleWarning("var_after_change hooked"); - } + } return NULL; } DEF_CONSOLE_CMD(ConInfoCmd) { - if (argc<2) return NULL; - if (argt[1]!=ICONSOLE_VAR_UNKNOWN) { + if (argc < 2) return NULL; + if (argt[1] != ICONSOLE_VAR_UNKNOWN) { IConsoleError("first argument has to be a command name"); - } else { - _iconsole_cmd * item; + } else { + _iconsole_cmd* item; item = IConsoleCmdGet(argv[1]); - if (item==NULL) { + if (item == NULL) { IConsoleError("the given command was not found"); return NULL; } - IConsolePrintF(_iconsole_color_default,"cmd_name: %s",item->name); - IConsolePrintF(_iconsole_color_default,"cmd_addr: %i",item->addr); + IConsolePrintF(_iconsole_color_default, "cmd_name: %s", item->name); + IConsolePrintF(_iconsole_color_default, "cmd_addr: %i", item->addr); if (item->hook_access) IConsoleWarning("cmd_access hooked"); if (item->hook_before_exec) IConsoleWarning("cmd_before_exec hooked"); if (item->hook_after_exec) IConsoleWarning("cmd_after_exec hooked"); - } + } return NULL; } DEF_CONSOLE_CMD(ConDebugLevel) { - if (argc<2) return NULL; + if (argc < 2) return NULL; SetDebugString(argv[1]); return NULL; } @@ -296,100 +297,68 @@ DEF_CONSOLE_CMD(ConExit) DEF_CONSOLE_CMD(ConHelp) { - IConsolePrint(13 ," -- console help -- "); - IConsolePrint(1 ," variables: [command to list them: list_vars]"); - IConsolePrint(1 ," *temp_string = \"my little \""); - IConsolePrint(1 ,""); - IConsolePrint(1 ," commands: [command to list them: list_cmds]"); - IConsolePrint(1 ," [command] [\"string argument with spaces\"] [argument 2] ..."); - IConsolePrint(1 ," printf \"%s world\" *temp_string"); - IConsolePrint(1 ,""); - IConsolePrint(1 ," command/variable returning a value into an variable:"); - IConsolePrint(1 ," *temp_uint16 << random"); - IConsolePrint(1 ," *temp_uint16 << *temp_uint16_2"); - IConsolePrint(1 ,""); + IConsolePrint(13, " -- console help -- "); + IConsolePrint( 1, " variables: [command to list them: list_vars]"); + IConsolePrint( 1, " *temp_string = \"my little \""); + IConsolePrint( 1, ""); + IConsolePrint( 1, " commands: [command to list them: list_cmds]"); + IConsolePrint( 1, " [command] [\"string argument with spaces\"] [argument 2] ..."); + IConsolePrint( 1, " printf \"%s world\" *temp_string"); + IConsolePrint( 1, ""); + IConsolePrint( 1, " command/variable returning a value into an variable:"); + IConsolePrint( 1, " *temp_uint16 << random"); + IConsolePrint( 1, " *temp_uint16 << *temp_uint16_2"); + IConsolePrint( 1, ""); return NULL; } DEF_CONSOLE_CMD(ConRandom) { - _iconsole_var * result; + /* XXX memory leak */ + _iconsole_var* result; result = IConsoleVarAlloc(ICONSOLE_VAR_UINT16); - IConsoleVarSetValue(result,rand()); + IConsoleVarSetValue(result, rand()); return result; } DEF_CONSOLE_CMD(ConListCommands) { - _iconsole_cmd * item; - int l = 0; - - if (argv[1]!=NULL) l = strlen((char *) argv[1]); - - item = _iconsole_cmds; - while (item != NULL) { - if (argv[1]!=NULL) { + const _iconsole_cmd* item; + size_t l = 0; - if (memcmp((void *) item->name, (void *) argv[1],l)==0) - IConsolePrintF(_iconsole_color_default,"%s",item->name); + if (argv[1] != NULL) l = strlen(argv[1]); - } else { - - IConsolePrintF(_iconsole_color_default,"%s",item->name); - - } - item = item->_next; - } + for (item = _iconsole_cmds; item != NULL; item = item->_next) + if (argv[1] == NULL || strncmp(item->name, argv[1], l) == 0) + IConsolePrintF(_iconsole_color_default, "%s", item->name); return NULL; } DEF_CONSOLE_CMD(ConListVariables) { - _iconsole_var * item; - int l = 0; - - if (argv[1]!=NULL) l = strlen((char *) argv[1]); - - item = _iconsole_vars; - while (item != NULL) { - if (argv[1]!=NULL) { + const _iconsole_var* item; + size_t l = 0; - if (memcmp(item->name, argv[1],l)==0) - IConsolePrintF(_iconsole_color_default,"%s",item->name); + if (argv[1] != NULL) l = strlen(argv[1]); - } else { - - IConsolePrintF(_iconsole_color_default,"%s",item->name); - - } - item = item->_next; - } + for (item = _iconsole_vars; item != NULL; item = item->_next) + if (argv[1] == NULL || strncmp(item->name, argv[1], l) == 0) + IConsolePrintF(_iconsole_color_default, "%s", item->name); return NULL; } DEF_CONSOLE_CMD(ConListDumpVariables) { - _iconsole_var * item; - int l = 0; - - if (argv[1]!=NULL) l = strlen((char *) argv[1]); - - item = _iconsole_vars; - while (item != NULL) { - if (argv[1]!=NULL) { + const _iconsole_var* item; + size_t l = 0; - if (memcmp(item->name, argv[1],l)==0) - IConsoleVarDump(item,NULL); + if (argv[1] != NULL) l = strlen(argv[1]); - } else { - - IConsoleVarDump(item,NULL); - - } - item = item->_next; - } + for (item = _iconsole_vars; item != NULL; item = item->_next) + if (argv[1] == NULL || strncmp(item->name, argv[1], l) == 0) + IConsoleVarDump(item, NULL); return NULL; } @@ -402,19 +371,19 @@ DEF_CONSOLE_CMD(ConListDumpVariables) void IConsoleDebugLibRegister() { // stdlib - extern bool _stdlib_con_developer; - - IConsoleVarRegister("con_developer",(void *) &_stdlib_con_developer,ICONSOLE_VAR_BOOLEAN); - IConsoleVarMemRegister("temp_bool",ICONSOLE_VAR_BOOLEAN); - IConsoleVarMemRegister("temp_int16",ICONSOLE_VAR_INT16); - IConsoleVarMemRegister("temp_int32",ICONSOLE_VAR_INT32); - IConsoleVarMemRegister("temp_pointer",ICONSOLE_VAR_POINTER); - IConsoleVarMemRegister("temp_uint16",ICONSOLE_VAR_UINT16); - IConsoleVarMemRegister("temp_uint16_2",ICONSOLE_VAR_UINT16); - IConsoleVarMemRegister("temp_uint32",ICONSOLE_VAR_UINT32); - IConsoleVarMemRegister("temp_string",ICONSOLE_VAR_STRING); - IConsoleVarMemRegister("temp_string2",ICONSOLE_VAR_STRING); - IConsoleCmdRegister("resettile",ConResetTile); + extern bool _stdlib_con_developer; /* XXX extern in .c */ + + IConsoleVarRegister("con_developer", &_stdlib_con_developer, ICONSOLE_VAR_BOOLEAN); + IConsoleVarMemRegister("temp_bool", ICONSOLE_VAR_BOOLEAN); + IConsoleVarMemRegister("temp_int16", ICONSOLE_VAR_INT16); + IConsoleVarMemRegister("temp_int32", ICONSOLE_VAR_INT32); + IConsoleVarMemRegister("temp_pointer", ICONSOLE_VAR_POINTER); + IConsoleVarMemRegister("temp_uint16", ICONSOLE_VAR_UINT16); + IConsoleVarMemRegister("temp_uint16_2", ICONSOLE_VAR_UINT16); + IConsoleVarMemRegister("temp_uint32", ICONSOLE_VAR_UINT32); + IConsoleVarMemRegister("temp_string", ICONSOLE_VAR_STRING); + IConsoleVarMemRegister("temp_string2", ICONSOLE_VAR_STRING); + IConsoleCmdRegister("resettile", ConResetTile); } #endif @@ -425,51 +394,49 @@ void IConsoleDebugLibRegister() void IConsoleStdLibRegister() { // stdlib - extern byte _stdlib_developer; + extern byte _stdlib_developer; /* XXX extern in .c */ #ifdef _DEBUG IConsoleDebugLibRegister(); -#else - (void)ConResetTile; // Silence warning, this is only used in _DEBUG #endif - // functions [please add them alphabeticaly] + // functions [please add them alphabetically] #ifdef ENABLE_NETWORK - IConsoleCmdRegister("connect",ConNetworkConnect); - IConsoleCmdHook("connect",ICONSOLE_HOOK_ACCESS,ConCmdHookNoNetwork); + IConsoleCmdRegister("connect", ConNetworkConnect); + IConsoleCmdHook("connect", ICONSOLE_HOOK_ACCESS, ConCmdHookNoNetwork); #endif - IConsoleCmdRegister("debug_level",ConDebugLevel); - IConsoleCmdRegister("dump_vars",ConListDumpVariables); - IConsoleCmdRegister("echo",ConEcho); - IConsoleCmdRegister("echoc",ConEchoC); - IConsoleCmdRegister("exec",ConExec); - IConsoleCmdRegister("exit",ConExit); - IConsoleCmdRegister("help",ConHelp); - IConsoleCmdRegister("info_cmd",ConInfoCmd); - IConsoleCmdRegister("info_var",ConInfoVar); - IConsoleCmdRegister("list_cmds",ConListCommands); - IConsoleCmdRegister("list_vars",ConListVariables); - IConsoleCmdRegister("printf",ConPrintF); - IConsoleCmdRegister("printfc",ConPrintFC); - IConsoleCmdRegister("quit",ConExit); - IConsoleCmdRegister("random",ConRandom); - IConsoleCmdRegister("resetengines",ConResetEngines); - IConsoleCmdHook("resetengines",ICONSOLE_HOOK_ACCESS,ConCmdHookNoNetwork); - IConsoleCmdRegister("return",ConReturn); - IConsoleCmdRegister("screenshot",ConScreenShot); - IConsoleCmdRegister("script",ConScript); - IConsoleCmdRegister("scrollto",ConScrollToTile); + IConsoleCmdRegister("debug_level", ConDebugLevel); + IConsoleCmdRegister("dump_vars", ConListDumpVariables); + IConsoleCmdRegister("echo", ConEcho); + IConsoleCmdRegister("echoc", ConEchoC); + IConsoleCmdRegister("exec", ConExec); + IConsoleCmdRegister("exit", ConExit); + IConsoleCmdRegister("help", ConHelp); + IConsoleCmdRegister("info_cmd", ConInfoCmd); + IConsoleCmdRegister("info_var", ConInfoVar); + IConsoleCmdRegister("list_cmds", ConListCommands); + IConsoleCmdRegister("list_vars", ConListVariables); + IConsoleCmdRegister("printf", ConPrintF); + IConsoleCmdRegister("printfc", ConPrintFC); + IConsoleCmdRegister("quit", ConExit); + IConsoleCmdRegister("random", ConRandom); + IConsoleCmdRegister("resetengines", ConResetEngines); + IConsoleCmdHook("resetengines", ICONSOLE_HOOK_ACCESS, ConCmdHookNoNetwork); + IConsoleCmdRegister("return", ConReturn); + IConsoleCmdRegister("screenshot", ConScreenShot); + IConsoleCmdRegister("script", ConScript); + IConsoleCmdRegister("scrollto", ConScrollToTile); // variables [please add them alphabeticaly] - IConsoleVarRegister("developer",(void *) &_stdlib_developer,ICONSOLE_VAR_BYTE); + IConsoleVarRegister("developer", &_stdlib_developer, ICONSOLE_VAR_BYTE); #ifdef ENABLE_NETWORK - IConsoleVarRegister("net_client_timeout",&_network_client_timeout,ICONSOLE_VAR_UINT16); - IConsoleVarHook("*net_client_timeout",ICONSOLE_HOOK_ACCESS,ConVarHookNoNetClient); - IConsoleVarRegister("net_ready_ahead",&_network_ready_ahead,ICONSOLE_VAR_UINT16); - IConsoleVarRegister("net_sync_freq",&_network_sync_freq,ICONSOLE_VAR_UINT16); - IConsoleVarHook("*net_sync_freq",ICONSOLE_HOOK_ACCESS,ConVarHookNoNetClient); + IConsoleVarRegister("net_client_timeout", &_network_client_timeout, ICONSOLE_VAR_UINT16); + IConsoleVarHook("*net_client_timeout", ICONSOLE_HOOK_ACCESS, ConVarHookNoNetClient); + IConsoleVarRegister("net_ready_ahead", &_network_ready_ahead, ICONSOLE_VAR_UINT16); + IConsoleVarRegister("net_sync_freq", &_network_sync_freq, ICONSOLE_VAR_UINT16); + IConsoleVarHook("*net_sync_freq", ICONSOLE_HOOK_ACCESS, ConVarHookNoNetClient); #endif } -/* -------------------- dont cross this line --------------------- */ +/* -------------------- don't cross this line --------------------- */ @@ -62,6 +62,7 @@ # define CDECL _cdecl # define NOT_REACHED() _assume(0) # define snprintf _snprintf +# define vsnprintf _vsnprintf # undef TTD_ALIGNMENT_4 # undef TTD_ALIGNMENT_2 # define GCC_PACK |