# console.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
    00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
    00008  */
    00009
    00012 #include "stdafx.h"
    00013 #include "console_internal.h"
    00014 #include "network/network.h"
    00015 #include "network/network_func.h"
    00016 #include "debug.h"
    00017
    00018 #include <stdarg.h>
    00019
    00020 #define ICON_BUFFER 79
    00021 #define ICON_HISTORY_SIZE 20
    00022 #define ICON_LINE_HEIGHT 12
    00023 #define ICON_RIGHT_BORDERWIDTH 10
    00024 #define ICON_BOTTOM_BORDERWIDTH 12
    00025 #define ICON_MAX_ALIAS_LINES 40
    00026 #define ICON_TOKEN_COUNT 20
    00027
    00028 /* console parser */
    00029 IConsoleCmd   *_iconsole_cmds;
    00030 IConsoleVar   *_iconsole_vars;
    00031 IConsoleAlias *_iconsole_aliases;
    00032
    00033 /* ** stdlib ** */
    00034 byte _stdlib_developer = 1;
    00035 bool _stdlib_con_developer = false;
    00036 FILE *_iconsole_output_file;
    00037
    00038 void IConsoleInit()
    00039 {
    00040   _iconsole_output_file = NULL;
    00041 #ifdef ENABLE_NETWORK /* Initialize network only variables */
    00042   _redirect_console_to_client = INVALID_CLIENT_ID;
    00043 #endif
    00044
    00045   IConsoleGUIInit();
    00046
    00047   IConsoleStdLibRegister();
    00048 }
    00049
    00050 static void IConsoleWriteToLogFile(const char *string)
    00051 {
    00052   if (_iconsole_output_file != NULL) {
    00053     /* if there is an console output file ... also print it there */
    00054     const char *header = GetLogPrefix();
    00055     if (fwrite(header, strlen(header), 1, _iconsole_output_file) != 1 ||
    00056         fwrite(string, strlen(string), 1, _iconsole_output_file) != 1 ||
    00057         fwrite("\n", 1, 1, _iconsole_output_file) != 1) {
    00058       fclose(_iconsole_output_file);
    00059       _iconsole_output_file = NULL;
    00060       IConsolePrintF(CC_DEFAULT, "cannot write to log file");
    00061     }
    00062   }
    00063 }
    00064
    00065 bool CloseConsoleLogIfActive()
    00066 {
    00067   if (_iconsole_output_file != NULL) {
    00068     IConsolePrintF(CC_DEFAULT, "file output complete");
    00069     fclose(_iconsole_output_file);
    00070     _iconsole_output_file = NULL;
    00071     return true;
    00072   }
    00073
    00074   return false;
    00075 }
    00076
    00077 void IConsoleFree()
    00078 {
    00079   IConsoleGUIFree();
    00080   CloseConsoleLogIfActive();
    00081 }
    00082
    00092 void IConsolePrint(ConsoleColour colour_code, const char *string)
    00093 {
    00094   char *str;
    00095 #ifdef ENABLE_NETWORK
    00096   if (_redirect_console_to_client != INVALID_CLIENT_ID) {
    00097     /* Redirect the string to the client */
    00098     NetworkServerSendRcon(_redirect_console_to_client, colour_code, string);
    00099     return;
    00100   }
    00101 #endif
    00102
    00103   /* Create a copy of the string, strip if of colours and invalid
    00104    * characters and (when applicable) assign it to the console buffer */
    00105   str = strdup(string);
    00106   str_strip_colours(str);
    00107   str_validate(str, str + strlen(str));
    00108
    00109   if (_network_dedicated) {
    00110     fprintf(stdout, "%s%s\n", GetLogPrefix(), str);
    00111     fflush(stdout);
    00112     IConsoleWriteToLogFile(str);
    00113     free(str); // free duplicated string since it's not used anymore
    00114     return;
    00115   }
    00116
    00117   IConsoleWriteToLogFile(str);
    00118   IConsoleGUIPrint(colour_code, str);
    00119 }
    00120
    00126 void CDECL IConsolePrintF(ConsoleColour colour_code, const char *format, ...)
    00127 {
    00128   va_list va;
    00129   char buf[ICON_MAX_STREAMSIZE];
    00130
    00131   va_start(va, format);
    00132   vsnprintf(buf, sizeof(buf), format, va);
    00133   va_end(va);
    00134
    00135   IConsolePrint(colour_code, buf);
    00136 }
    00137
    00146 void IConsoleDebug(const char *dbg, const char *string)
    00147 {
    00148   if (_stdlib_developer > 1)
    00149     IConsolePrintF(CC_DEBUG, "dbg: [%s] %s", dbg, string);
    00150 }
    00151
    00157 void IConsoleWarning(const char *string)
    00158 {
    00159   if (_stdlib_developer > 0)
    00160     IConsolePrintF(CC_WARNING, "WARNING: %s", string);
    00161 }
    00162
    00167 void IConsoleError(const char *string)
    00168 {
    00169   IConsolePrintF(CC_ERROR, "ERROR: %s", string);
    00170 }
    00171
    00179 bool GetArgumentInteger(uint32 *value, const char *arg)
    00180 {
    00181   char *endptr;
    00182
    00183   if (strcmp(arg, "on") == 0 || strcmp(arg, "true") == 0) {
    00184     *value = 1;
    00185     return true;
    00186   }
    00187   if (strcmp(arg, "off") == 0 || strcmp(arg, "false") == 0) {
    00188     *value = 0;
    00189     return true;
    00190   }
    00191
    00192   *value = strtoul(arg, &endptr, 0);
    00193   return arg != endptr;
    00194 }
    00195
    00196 /*  * *************************
    00197     * hooking code            *
    00198     * *************************/
    00199
    00206 static void IConsoleHookAdd(IConsoleHooks *hooks, IConsoleHookTypes type, IConsoleHook *proc)
    00207 {
    00208   if (hooks == NULL || proc == NULL) return;
    00209
    00210   switch (type) {
    00211     case ICONSOLE_HOOK_ACCESS:
    00212       hooks->access = proc;
    00213       break;
    00214     case ICONSOLE_HOOK_PRE_ACTION:
    00215       hooks->pre = proc;
    00216       break;
    00217     case ICONSOLE_HOOK_POST_ACTION:
    00218       hooks->post = proc;
    00219       break;
    00220     default: NOT_REACHED();
    00221   }
    00222 }
    00223
    00232 static bool IConsoleHookHandle(const IConsoleHooks *hooks, IConsoleHookTypes type)
    00233 {
    00234   IConsoleHook *proc = NULL;
    00235   if (hooks == NULL) return false;
    00236
    00237   switch (type) {
    00238     case ICONSOLE_HOOK_ACCESS:
    00239       proc = hooks->access;
    00240       break;
    00241     case ICONSOLE_HOOK_PRE_ACTION:
    00242       proc = hooks->pre;
    00243       break;
    00244     case ICONSOLE_HOOK_POST_ACTION:
    00245       proc = hooks->post;
    00246       break;
    00247     default: NOT_REACHED();
    00248   }
    00249
    00250   return (proc == NULL) ? true : proc();
    00251 }
    00252
    00259 void IConsoleCmdHookAdd(const char *name, IConsoleHookTypes type, IConsoleHook *proc)
    00260 {
    00261   IConsoleCmd *cmd = IConsoleCmdGet(name);
    00262   if (cmd == NULL) return;
    00263   IConsoleHookAdd(&cmd->hook, type, proc);
    00264 }
    00265
    00272 void IConsoleVarHookAdd(const char *name, IConsoleHookTypes type, IConsoleHook *proc)
    00273 {
    00274   IConsoleVar *var = IConsoleVarGet(name);
    00275   if (var == NULL) return;
    00276   IConsoleHookAdd(&var->hook, type, proc);
    00277 }
    00278
    00284 #define IConsoleAddSorted(_base, item_new, IConsoleType, type)                 \
    00285 {                                                                              \
    00286   IConsoleType *item, *item_before;                                            \
    00287   /* first command */                                                          \
    00288   if (_base == NULL) {                                                         \
    00289     _base = item_new;                                                          \
    00290     return;                                                                    \
    00291   }                                                                            \
    00292                                                                                \
    00293   item_before = NULL;                                                          \
    00294   item = _base;                                                                \
    00295                                                                                \
    00296   /* BEGIN - Alphabetically insert the commands into the linked list */        \
    00297   while (item != NULL) {                                                       \
    00298     int i = strcmp(item->name, item_new->name);                                \
    00299     if (i == 0) {                                                              \
    00300       IConsoleError(type " with this name already exists; insertion aborted"); \
    00301       free(item_new);                                                          \
    00302       return;                                                                  \
    00303     }                                                                          \
    00304                                                                                \
    00305     if (i > 0) break; /* insert at this position */                            \
    00306                                                                                \
    00307     item_before = item;                                                        \
    00308     item = item->next;                                                         \
    00309   }                                                                            \
    00310                                                                                \
    00311   if (item_before == NULL) {                                                   \
    00312     _base = item_new;                                                          \
    00313   } else {                                                                     \
    00314     item_before->next = item_new;                                              \
    00315   }                                                                            \
    00316                                                                                \
    00317   item_new->next = item;                                                       \
    00318   /* END - Alphabetical insert */                                              \
    00319 }
    00320
    00326 void IConsoleCmdRegister(const char *name, IConsoleCmdProc *proc)
    00327 {
    00328   char *new_cmd = strdup(name);
    00329   IConsoleCmd *item_new = MallocT<IConsoleCmd>(1);
    00330
    00331   item_new->next = NULL;
    00332   item_new->proc = proc;
    00333   item_new->name = new_cmd;
    00334
    00335   item_new->hook.access = NULL;
    00336   item_new->hook.pre = NULL;
    00337   item_new->hook.post = NULL;
    00338
    00339   IConsoleAddSorted(_iconsole_cmds, item_new, IConsoleCmd, "a command");
    00340 }
    00341
    00347 IConsoleCmd *IConsoleCmdGet(const char *name)
    00348 {
    00349   IConsoleCmd *item;
    00350
    00351   for (item = _iconsole_cmds; item != NULL; item = item->next) {
    00352     if (strcmp(item->name, name) == 0) return item;
    00353   }
    00354   return NULL;
    00355 }
    00356
    00362 void IConsoleAliasRegister(const char *name, const char *cmd)
    00363 {
    00364   char *new_alias = strdup(name);
    00365   char *cmd_aliased = strdup(cmd);
    00366   IConsoleAlias *item_new = MallocT<IConsoleAlias>(1);
    00367
    00368   item_new->next = NULL;
    00369   item_new->cmdline = cmd_aliased;
    00370   item_new->name = new_alias;
    00371
    00372   IConsoleAddSorted(_iconsole_aliases, item_new, IConsoleAlias, "an alias");
    00373 }
    00374
    00380 IConsoleAlias *IConsoleAliasGet(const char *name)
    00381 {
    00382   IConsoleAlias *item;
    00383
    00384   for (item = _iconsole_aliases; item != NULL; item = item->next) {
    00385     if (strcmp(item->name, name) == 0) return item;
    00386   }
    00387
    00388   return NULL;
    00389 }
    00390
    00392 static inline int IConsoleCopyInParams(char *dst, const char *src, uint bufpos)
    00393 {
    00394   /* len is the amount of bytes to add excluding the '\0'-termination */
    00395   int len = min(ICON_MAX_STREAMSIZE - bufpos - 1, (uint)strlen(src));
    00396   strecpy(dst, src, dst + len);
    00397
    00398   return len;
    00399 }
    00400
    00408 static void IConsoleAliasExec(const IConsoleAlias *alias, byte tokencount, char *tokens[ICON_TOKEN_COUNT])
    00409 {
    00410   const char *cmdptr;
    00411   char *aliases[ICON_MAX_ALIAS_LINES], aliasstream[ICON_MAX_STREAMSIZE];
    00412   uint i;
    00413   uint a_index, astream_i;
    00414
    00415   memset(&aliases, 0, sizeof(aliases));
    00416   memset(&aliasstream, 0, sizeof(aliasstream));
    00417
    00418   if (_stdlib_con_developer)
    00419     IConsolePrintF(CC_DEBUG, "condbg: requested command is an alias; parsing...");
    00420
    00421   aliases[0] = aliasstream;
    00422   for (cmdptr = alias->cmdline, a_index = 0, astream_i = 0; *cmdptr != '\0'; cmdptr++) {
    00423     if (a_index >= lengthof(aliases) || astream_i >= lengthof(aliasstream)) break;
    00424
    00425     switch (*cmdptr) {
    00426     case '\'': // ' will double for ""
    00427       aliasstream[astream_i++] = '"';
    00428       break;
    00429     case ';': // Cmd seperator, start new command
    00430       aliasstream[astream_i] = '\0';
    00431       aliases[++a_index] = &aliasstream[++astream_i];
    00432       cmdptr++;
    00433       break;
    00434     case '%': // Some or all parameters
    00435       cmdptr++;
    00436       switch (*cmdptr) {
    00437       case '+': { // All parameters seperated: "[param 1]" "[param 2]"
    00438         for (i = 0; i != tokencount; i++) {
    00439           aliasstream[astream_i++] = '"';
    00440           astream_i += IConsoleCopyInParams(&aliasstream[astream_i], tokens[i], astream_i);
    00441           aliasstream[astream_i++] = '"';
    00442           aliasstream[astream_i++] = ' ';
    00443         }
    00444       } break;
    00445       case '!': { // Merge the parameters to one: "[param 1] [param 2] [param 3...]"
    00446         aliasstream[astream_i++] = '"';
    00447         for (i = 0; i != tokencount; i++) {
    00448           astream_i += IConsoleCopyInParams(&aliasstream[astream_i], tokens[i], astream_i);
    00449           aliasstream[astream_i++] = ' ';
    00450         }
    00451         aliasstream[astream_i++] = '"';
    00452
    00453       } break;
    00454         default: { // One specific parameter: %A = [param 1] %B = [param 2] ...
    00455         int param = *cmdptr - 'A';
    00456
    00457         if (param < 0 || param >= tokencount) {
    00458           IConsoleError("too many or wrong amount of parameters passed to alias, aborting");
    00459           IConsolePrintF(CC_WARNING, "Usage of alias '%s': %s", alias->name, alias->cmdline);
    00460           return;
    00461         }
    00462
    00463         aliasstream[astream_i++] = '"';
    00464         astream_i += IConsoleCopyInParams(&aliasstream[astream_i], tokens[param], astream_i);
    00465         aliasstream[astream_i++] = '"';
    00466       } break;
    00467       } break;
    00468
    00469     default:
    00470       aliasstream[astream_i++] = *cmdptr;
    00471       break;
    00472     }
    00473   }
    00474
    00475   for (i = 0; i <= a_index; i++) IConsoleCmdExec(aliases[i]); // execute each alias in turn
    00476 }
    00477
    00487 void IConsoleVarStringRegister(const char *name, void *addr, uint32 size, const char *help)
    00488 {
    00489   IConsoleVar *var;
    00490   IConsoleVarRegister(name, addr, ICONSOLE_VAR_STRING, help);
    00491   var = IConsoleVarGet(name);
    00492   var->size = size;
    00493 }
    00494
    00502 void IConsoleVarRegister(const char *name, void *addr, IConsoleVarTypes type, const char *help)
    00503 {
    00504   char *new_cmd = strdup(name);
    00505   IConsoleVar *item_new = MallocT<IConsoleVar>(1);
    00506
    00507   item_new->help = (help != NULL) ? strdup(help) : NULL;
    00508
    00509   item_new->next = NULL;
    00510   item_new->name = new_cmd;
    00511   item_new->addr = addr;
    00512   item_new->proc = NULL;
    00513   item_new->type = type;
    00514
    00515   item_new->hook.access = NULL;
    00516   item_new->hook.pre = NULL;
    00517   item_new->hook.post = NULL;
    00518
    00519   IConsoleAddSorted(_iconsole_vars, item_new, IConsoleVar, "a variable");
    00520 }
    00521
    00527 IConsoleVar *IConsoleVarGet(const char *name)
    00528 {
    00529   IConsoleVar *item;
    00530   for (item = _iconsole_vars; item != NULL; item = item->next) {
    00531     if (strcmp(item->name, name) == 0) return item;
    00532   }
    00533
    00534   return NULL;
    00535 }
    00536
    00541 static char *IConsoleVarGetStringValue(const IConsoleVar *var)
    00542 {
    00543   static char tempres[50];
    00544   char *value = tempres;
    00545
    00546   switch (var->type) {
    00547     case ICONSOLE_VAR_BOOLEAN:
    00548       snprintf(tempres, sizeof(tempres), "%s", (*(bool*)var->addr) ? "on" : "off");
    00549       break;
    00550     case ICONSOLE_VAR_BYTE:
    00551       snprintf(tempres, sizeof(tempres), "%u", *(byte*)var->addr);
    00552       break;
    00553     case ICONSOLE_VAR_UINT16:
    00554       snprintf(tempres, sizeof(tempres), "%u", *(uint16*)var->addr);
    00555       break;
    00556     case ICONSOLE_VAR_UINT32:
    00557       snprintf(tempres, sizeof(tempres), "%u",  *(uint32*)var->addr);
    00558       break;
    00559     case ICONSOLE_VAR_INT16:
    00560       snprintf(tempres, sizeof(tempres), "%i", *(int16*)var->addr);
    00561       break;
    00562     case ICONSOLE_VAR_INT32:
    00563       snprintf(tempres, sizeof(tempres), "%i",  *(int32*)var->addr);
    00564       break;
    00565     case ICONSOLE_VAR_STRING:
    00566       value = (char*)var->addr;
    00567       break;
    00568     default: NOT_REACHED();
    00569   }
    00570
    00571   return value;
    00572 }
    00573
    00578 static void IConsoleVarPrintSetValue(const IConsoleVar *var)
    00579 {
    00580   char *value = IConsoleVarGetStringValue(var);
    00581   IConsolePrintF(CC_WARNING, "'%s' changed to:  %s", var->name, value);
    00582 }
    00583
    00589 static void IConsoleVarSetValue(const IConsoleVar *var, uint32 value)
    00590 {
    00591   IConsoleHookHandle(&var->hook, ICONSOLE_HOOK_PRE_ACTION);
    00592   switch (var->type) {
    00593     case ICONSOLE_VAR_BOOLEAN:
    00594       *(bool*)var->addr = (value != 0);
    00595       break;
    00596     case ICONSOLE_VAR_BYTE:
    00597       *(byte*)var->addr = (byte)value;
    00598       break;
    00599     case ICONSOLE_VAR_UINT16:
    00600       *(uint16*)var->addr = (uint16)value;
    00601       break;
    00602     case ICONSOLE_VAR_INT16:
    00603       *(int16*)var->addr = (int16)value;
    00604       break;
    00605     case ICONSOLE_VAR_UINT32:
    00606       *(uint32*)var->addr = (uint32)value;
    00607       break;
    00608     case ICONSOLE_VAR_INT32:
    00609       *(int32*)var->addr = (int32)value;
    00610       break;
    00611     default: NOT_REACHED();
    00612   }
    00613
    00614   IConsoleHookHandle(&var->hook, ICONSOLE_HOOK_POST_ACTION);
    00615   IConsoleVarPrintSetValue(var);
    00616 }
    00617
    00624 static void IConsoleVarSetStringvalue(const IConsoleVar *var, const char *value)
    00625 {
    00626   if (var->type != ICONSOLE_VAR_STRING || var->addr == NULL) return;
    00627
    00628   IConsoleHookHandle(&var->hook, ICONSOLE_HOOK_PRE_ACTION);
    00629   ttd_strlcpy((char*)var->addr, value, var->size);
    00630   IConsoleHookHandle(&var->hook, ICONSOLE_HOOK_POST_ACTION);
    00631   IConsoleVarPrintSetValue(var); // print out the new value, giving feedback
    00632   return;
    00633 }
    00634
    00640 static uint32 IConsoleVarGetValue(const IConsoleVar *var)
    00641 {
    00642   uint32 result = 0;
    00643
    00644   switch (var->type) {
    00645     case ICONSOLE_VAR_BOOLEAN:
    00646       result = *(bool*)var->addr;
    00647       break;
    00648     case ICONSOLE_VAR_BYTE:
    00649       result = *(byte*)var->addr;
    00650       break;
    00651     case ICONSOLE_VAR_UINT16:
    00652       result = *(uint16*)var->addr;
    00653       break;
    00654     case ICONSOLE_VAR_INT16:
    00655       result = *(int16*)var->addr;
    00656       break;
    00657     case ICONSOLE_VAR_UINT32:
    00658       result = *(uint32*)var->addr;
    00659       break;
    00660     case ICONSOLE_VAR_INT32:
    00661       result = *(int32*)var->addr;
    00662       break;
    00663     default: NOT_REACHED();
    00664   }
    00665   return result;
    00666 }
    00667
    00671 void IConsoleVarPrintGetValue(const IConsoleVar *var)
    00672 {
    00673   char *value;
    00674   /* Some variables need really specific handling, handle this in its
    00675    * callback function */
    00676   if (var->proc != NULL) {
    00677     var->proc(0, NULL);
    00678     return;
    00679   }
    00680
    00681   value = IConsoleVarGetStringValue(var);
    00682   IConsolePrintF(CC_WARNING, "Current value for '%s' is:  %s", var->name, value);
    00683 }
    00684
    00692 static void IConsoleVarExec(const IConsoleVar *var, byte tokencount, char *token[ICON_TOKEN_COUNT])
    00693 {
    00694   const char *tokenptr = token[0];
    00695   byte t_index = tokencount;
    00696   uint32 value;
    00697
    00698   if (_stdlib_con_developer)
    00699     IConsolePrintF(CC_DEBUG, "condbg: requested command is a variable");
    00700
    00701   if (tokencount == 0) { // Just print out value
    00702     IConsoleVarPrintGetValue(var);
    00703     return;
    00704   }
    00705
    00706   /* Use of assignment sign is not mandatory but supported, so just 'ignore it appropiately' */
    00707   if (strcmp(tokenptr, "=") == 0) tokencount--;
    00708
    00709   if (tokencount == 1) {
    00710     /* Some variables need really special handling, handle it in their callback procedure */
    00711     if (var->proc != NULL) {
    00712       var->proc(tokencount, &token[t_index - tokencount]); // set the new value
    00713       return;
    00714     }
    00715     /* Strings need special processing. No need to convert the argument to
    00716      * an integer value, just copy over the argument on a one-by-one basis */
    00717     if (var->type == ICONSOLE_VAR_STRING) {
    00718       IConsoleVarSetStringvalue(var, token[t_index - tokencount]);
    00719       return;
    00720     } else if (GetArgumentInteger(&value, token[t_index - tokencount])) {
    00721       IConsoleVarSetValue(var, value);
    00722       return;
    00723     }
    00724
    00725     /* Increase or decrease the value by one. This of course can only happen to 'number' types */
    00726     if (strcmp(tokenptr, "++") == 0 && var->type != ICONSOLE_VAR_STRING) {
    00727       IConsoleVarSetValue(var, IConsoleVarGetValue(var) + 1);
    00728       return;
    00729     }
    00730
    00731     if (strcmp(tokenptr, "--") == 0 && var->type != ICONSOLE_VAR_STRING) {
    00732       IConsoleVarSetValue(var, IConsoleVarGetValue(var) - 1);
    00733       return;
    00734     }
    00735   }
    00736
    00737   IConsoleError("invalid variable assignment");
    00738 }
    00739
    00746 void IConsoleVarProcAdd(const char *name, IConsoleCmdProc *proc)
    00747 {
    00748   IConsoleVar *var = IConsoleVarGet(name);
    00749   if (var == NULL) return;
    00750   var->proc = proc;
    00751 }
    00752
    00758 void IConsoleCmdExec(const char *cmdstr)
    00759 {
    00760   IConsoleCmd   *cmd    = NULL;
    00761   IConsoleAlias *alias  = NULL;
    00762   IConsoleVar   *var    = NULL;
    00763
    00764   const char *cmdptr;
    00765   char *tokens[ICON_TOKEN_COUNT], tokenstream[ICON_MAX_STREAMSIZE];
    00766   uint t_index, tstream_i;
    00767
    00768   bool longtoken = false;
    00769   bool foundtoken = false;
    00770
    00771   if (cmdstr[0] == '#') return; // comments
    00772
    00773   for (cmdptr = cmdstr; *cmdptr != '\0'; cmdptr++) {
    00774     if (!IsValidChar(*cmdptr, CS_ALPHANUMERAL)) {
    00775       IConsoleError("command contains malformed characters, aborting");
    00776       IConsolePrintF(CC_ERROR, "ERROR: command was: '%s'", cmdstr);
    00777       return;
    00778     }
    00779   }
    00780
    00781   if (_stdlib_con_developer)
    00782     IConsolePrintF(CC_DEBUG, "condbg: executing cmdline: '%s'", cmdstr);
    00783
    00784   memset(&tokens, 0, sizeof(tokens));
    00785   memset(&tokenstream, 0, sizeof(tokenstream));
    00786
    00787   /* 1. Split up commandline into tokens, seperated by spaces, commands
    00788    * enclosed in "" are taken as one token. We can only go as far as the amount
    00789    * of characters in our stream or the max amount of tokens we can handle */
    00790   for (cmdptr = cmdstr, t_index = 0, tstream_i = 0; *cmdptr != '\0'; cmdptr++) {
    00791     if (t_index >= lengthof(tokens) || tstream_i >= lengthof(tokenstream)) break;
    00792
    00793     switch (*cmdptr) {
    00794     case ' ': // Token seperator
    00795       if (!foundtoken) break;
    00796
    00797       if (longtoken) {
    00798         tokenstream[tstream_i] = *cmdptr;
    00799       } else {
    00800         tokenstream[tstream_i] = '\0';
    00801         foundtoken = false;
    00802       }
    00803
    00804       tstream_i++;
    00805       break;
    00806     case '"': // Tokens enclosed in "" are one token
    00807       longtoken = !longtoken;
    00808       break;
    00809     case '\\': // Escape character for ""
    00810       if (cmdptr[1] == '"' && tstream_i + 1 < lengthof(tokenstream)) {
    00811         tokenstream[tstream_i++] = *++cmdptr;
    00812         break;
    00813       }
    00814       /* fallthrough */
    00815     default: // Normal character
    00816       tokenstream[tstream_i++] = *cmdptr;
    00817
    00818       if (!foundtoken) {
    00819         tokens[t_index++] = &tokenstream[tstream_i - 1];
    00820         foundtoken = true;
    00821       }
    00822       break;
    00823     }
    00824   }
    00825
    00826   if (_stdlib_con_developer) {
    00827     uint i;
    00828
    00829     for (i = 0; tokens[i] != NULL; i++) {
    00830       IConsolePrintF(CC_DEBUG, "condbg: token %d is: '%s'", i, tokens[i]);
    00831     }
    00832   }
    00833
    00834   if (tokens[0] == '\0') return; // don't execute empty commands
    00835   /* 2. Determine type of command (cmd, alias or variable) and execute
    00836    * First try commands, then aliases, and finally variables. Execute
    00837    * the found action taking into account its hooking code
    00838    */
    00839   cmd = IConsoleCmdGet(tokens[0]);
    00840   if (cmd != NULL) {
    00841     if (IConsoleHookHandle(&cmd->hook, ICONSOLE_HOOK_ACCESS)) {
    00842       IConsoleHookHandle(&cmd->hook, ICONSOLE_HOOK_PRE_ACTION);
    00843       if (cmd->proc(t_index, tokens)) { // index started with 0
    00844         IConsoleHookHandle(&cmd->hook, ICONSOLE_HOOK_POST_ACTION);
    00845       } else {
    00846         cmd->proc(0, NULL); // if command failed, give help
    00847       }
    00848     }
    00849     return;
    00850   }
    00851
    00852   t_index--; // ignore the variable-name for comfort for both aliases and variaables
    00853   alias = IConsoleAliasGet(tokens[0]);
    00854   if (alias != NULL) {
    00855     IConsoleAliasExec(alias, t_index, &tokens[1]);
    00856     return;
    00857   }
    00858
    00859   var = IConsoleVarGet(tokens[0]);
    00860   if (var != NULL) {
    00861     if (IConsoleHookHandle(&var->hook, ICONSOLE_HOOK_ACCESS)) {
    00862       IConsoleVarExec(var, t_index, &tokens[1]);
    00863     }
    00864     return;
    00865   }
    00866
    00867   IConsoleError("command or variable not found");
    00868 }