console.c
changeset 4171 3fadda3afe70
parent 4077 d3022f976946
child 4299 b86602eaaff1
equal deleted inserted replaced
4170:1e4a024fd08b 4171:3fadda3afe70
    57 }
    57 }
    58 
    58 
    59 static inline void IConsoleResetHistoryPos(void) {_iconsole_historypos = ICON_HISTORY_SIZE - 1;}
    59 static inline void IConsoleResetHistoryPos(void) {_iconsole_historypos = ICON_HISTORY_SIZE - 1;}
    60 
    60 
    61 
    61 
    62 static void IConsoleHistoryAdd(const char* cmd);
    62 static void IConsoleHistoryAdd(const char *cmd);
    63 static void IConsoleHistoryNavigate(int direction);
    63 static void IConsoleHistoryNavigate(int direction);
    64 
    64 
    65 // ** console window ** //
    65 // ** console window ** //
    66 static void IConsoleWndProc(Window* w, WindowEvent* e)
    66 static void IConsoleWndProc(Window *w, WindowEvent *e)
    67 {
    67 {
    68 	switch (e->event) {
    68 	switch (e->event) {
    69 		case WE_PAINT: {
    69 		case WE_PAINT: {
    70 			int i = _iconsole_scroll;
    70 			int i = _iconsole_scroll;
    71 			int max = (w->height / ICON_LINE_HEIGHT) - 1;
    71 			int max = (w->height / ICON_LINE_HEIGHT) - 1;
   253 {
   253 {
   254 	free(_iconsole_cmdline.buf);
   254 	free(_iconsole_cmdline.buf);
   255 	IConsoleClearBuffer();
   255 	IConsoleClearBuffer();
   256 }
   256 }
   257 
   257 
   258 static void IConsoleWriteToLogFile(const char* string)
   258 static void IConsoleWriteToLogFile(const char *string)
   259 {
   259 {
   260 	if (_iconsole_output_file != NULL) {
   260 	if (_iconsole_output_file != NULL) {
   261 		// if there is an console output file ... also print it there
   261 		// if there is an console output file ... also print it there
   262 		fwrite(string, strlen(string), 1, _iconsole_output_file);
   262 		fwrite(string, strlen(string), 1, _iconsole_output_file);
   263 		fwrite("\n", 1, 1, _iconsole_output_file);
   263 		fwrite("\n", 1, 1, _iconsole_output_file);
   329 /**
   329 /**
   330  * Add the entered line into the history so you can look it back
   330  * Add the entered line into the history so you can look it back
   331  * scroll, etc. Put it to the beginning as it is the latest text
   331  * scroll, etc. Put it to the beginning as it is the latest text
   332  * @param cmd Text to be entered into the 'history'
   332  * @param cmd Text to be entered into the 'history'
   333  */
   333  */
   334 static void IConsoleHistoryAdd(const char* cmd)
   334 static void IConsoleHistoryAdd(const char *cmd)
   335 {
   335 {
   336 	free(_iconsole_history[ICON_HISTORY_SIZE - 1]);
   336 	free(_iconsole_history[ICON_HISTORY_SIZE - 1]);
   337 
   337 
   338 	memmove(&_iconsole_history[1], &_iconsole_history[0], sizeof(_iconsole_history[0]) * (ICON_HISTORY_SIZE - 1));
   338 	memmove(&_iconsole_history[1], &_iconsole_history[0], sizeof(_iconsole_history[0]) * (ICON_HISTORY_SIZE - 1));
   339 	_iconsole_history[0] = strdup(cmd);
   339 	_iconsole_history[0] = strdup(cmd);
   374  * are also logged. All lines to print are added to a temporary buffer which can be
   374  * are also logged. All lines to print are added to a temporary buffer which can be
   375  * used as a history to print them onscreen
   375  * used as a history to print them onscreen
   376  * @param color_code the colour of the command. Red in case of errors, etc.
   376  * @param color_code the colour of the command. Red in case of errors, etc.
   377  * @param string the message entered or output on the console (notice, error, etc.)
   377  * @param string the message entered or output on the console (notice, error, etc.)
   378  */
   378  */
   379 void IConsolePrint(uint16 color_code, const char* string)
   379 void IConsolePrint(uint16 color_code, const char *string)
   380 {
   380 {
   381 #ifdef ENABLE_NETWORK
   381 #ifdef ENABLE_NETWORK
   382 	if (_redirect_console_to_client != 0) {
   382 	if (_redirect_console_to_client != 0) {
   383 		/* Redirect the string to the client */
   383 		/* Redirect the string to the client */
   384 		SEND_COMMAND(PACKET_SERVER_RCON)(NetworkFindClientStateFromIndex(_redirect_console_to_client), color_code, string);
   384 		SEND_COMMAND(PACKET_SERVER_RCON)(NetworkFindClientStateFromIndex(_redirect_console_to_client), color_code, string);
   431  * It is possible to print debugging information to the console,
   431  * It is possible to print debugging information to the console,
   432  * which is achieved by using this function. Can only be used by
   432  * which is achieved by using this function. Can only be used by
   433  * @debug() in debug.c. You need at least a level 2 (developer) for debugging
   433  * @debug() in debug.c. You need at least a level 2 (developer) for debugging
   434  * messages to show up
   434  * messages to show up
   435  */
   435  */
   436 void IConsoleDebug(const char* string)
   436 void IConsoleDebug(const char *string)
   437 {
   437 {
   438 	if (_stdlib_developer > 1)
   438 	if (_stdlib_developer > 1)
   439 		IConsolePrintF(_icolour_dbg, "dbg: %s", string);
   439 		IConsolePrintF(_icolour_dbg, "dbg: %s", string);
   440 }
   440 }
   441 
   441 
   442 /**
   442 /**
   443  * It is possible to print warnings to the console. These are mostly
   443  * It is possible to print warnings to the console. These are mostly
   444  * errors or mishaps, but non-fatal. You need at least a level 1 (developer) for
   444  * errors or mishaps, but non-fatal. You need at least a level 1 (developer) for
   445  * debugging messages to show up
   445  * debugging messages to show up
   446  */
   446  */
   447 void IConsoleWarning(const char* string)
   447 void IConsoleWarning(const char *string)
   448 {
   448 {
   449 	if (_stdlib_developer > 0)
   449 	if (_stdlib_developer > 0)
   450 		IConsolePrintF(_icolour_warn, "WARNING: %s", string);
   450 		IConsolePrintF(_icolour_warn, "WARNING: %s", string);
   451 }
   451 }
   452 
   452 
   453 /**
   453 /**
   454  * It is possible to print error information to the console. This can include
   454  * It is possible to print error information to the console. This can include
   455  * game errors, or errors in general you would want the user to notice
   455  * game errors, or errors in general you would want the user to notice
   456  */
   456  */
   457 void IConsoleError(const char* string)
   457 void IConsoleError(const char *string)
   458 {
   458 {
   459 	IConsolePrintF(_icolour_err, "ERROR: %s", string);
   459 	IConsolePrintF(_icolour_err, "ERROR: %s", string);
   460 }
   460 }
   461 
   461 
   462 /**
   462 /**
   691  * Execute it as well.
   691  * Execute it as well.
   692  * @param *alias is the alias of the command
   692  * @param *alias is the alias of the command
   693  * @param tokencount the number of parameters passed
   693  * @param tokencount the number of parameters passed
   694  * @param *tokens are the parameters given to the original command (0 is the first param)
   694  * @param *tokens are the parameters given to the original command (0 is the first param)
   695  */
   695  */
   696 static void IConsoleAliasExec(const IConsoleAlias* alias, byte tokencount, char* tokens[ICON_TOKEN_COUNT])
   696 static void IConsoleAliasExec(const IConsoleAlias *alias, byte tokencount, char *tokens[ICON_TOKEN_COUNT])
   697 {
   697 {
   698 	const char *cmdptr;
   698 	const char *cmdptr;
   699 	char *aliases[ICON_MAX_ALIAS_LINES], aliasstream[ICON_MAX_STREAMSIZE];
   699 	char *aliases[ICON_MAX_ALIAS_LINES], aliasstream[ICON_MAX_STREAMSIZE];
   700 	uint i;
   700 	uint i;
   701 	uint a_index, astream_i;
   701 	uint a_index, astream_i;
   857  * Set a new value to a string-type variable. Basically this
   857  * Set a new value to a string-type variable. Basically this
   858  * means to copy the new value over to the container.
   858  * means to copy the new value over to the container.
   859  * @param *var the variable in question
   859  * @param *var the variable in question
   860  * @param *value the new value
   860  * @param *value the new value
   861  */
   861  */
   862 static void IConsoleVarSetStringvalue(const IConsoleVar* var, const char* value)
   862 static void IConsoleVarSetStringvalue(const IConsoleVar *var, const char *value)
   863 {
   863 {
   864 	if (var->type != ICONSOLE_VAR_STRING || var->addr == NULL) return;
   864 	if (var->type != ICONSOLE_VAR_STRING || var->addr == NULL) return;
   865 
   865 
   866 	IConsoleHookHandle(&var->hook, ICONSOLE_HOOK_PRE_ACTION);
   866 	IConsoleHookHandle(&var->hook, ICONSOLE_HOOK_PRE_ACTION);
   867 	ttd_strlcpy(var->addr, value, var->size);
   867 	ttd_strlcpy(var->addr, value, var->size);