OpenTTD Source  1.11.2
console.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * 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.
4  * 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.
5  * 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/>.
6  */
7 
10 #include "stdafx.h"
11 #include "console_internal.h"
12 #include "network/network.h"
13 #include "network/network_func.h"
14 #include "network/network_admin.h"
15 #include "debug.h"
16 #include "console_func.h"
17 #include "settings_type.h"
18 
19 #include <stdarg.h>
20 
21 #include "safeguards.h"
22 
23 static const uint ICON_TOKEN_COUNT = 20;
24 static const uint ICON_MAX_RECURSE = 10;
25 
26 /* console parser */
29 
30 FILE *_iconsole_output_file;
31 
32 void IConsoleInit()
33 {
34  _iconsole_output_file = nullptr;
37 
38  IConsoleGUIInit();
39 
40  IConsoleStdLibRegister();
41 }
42 
43 static void IConsoleWriteToLogFile(const char *string)
44 {
45  if (_iconsole_output_file != nullptr) {
46  /* if there is an console output file ... also print it there */
47  const char *header = GetLogPrefix();
48  if ((strlen(header) != 0 && fwrite(header, strlen(header), 1, _iconsole_output_file) != 1) ||
49  fwrite(string, strlen(string), 1, _iconsole_output_file) != 1 ||
50  fwrite("\n", 1, 1, _iconsole_output_file) != 1) {
51  fclose(_iconsole_output_file);
52  _iconsole_output_file = nullptr;
53  IConsolePrintF(CC_DEFAULT, "cannot write to log file");
54  }
55  }
56 }
57 
58 bool CloseConsoleLogIfActive()
59 {
60  if (_iconsole_output_file != nullptr) {
61  IConsolePrintF(CC_DEFAULT, "file output complete");
62  fclose(_iconsole_output_file);
63  _iconsole_output_file = nullptr;
64  return true;
65  }
66 
67  return false;
68 }
69 
70 void IConsoleFree()
71 {
72  IConsoleGUIFree();
73  CloseConsoleLogIfActive();
74 }
75 
85 void IConsolePrint(TextColour colour_code, const char *string)
86 {
87  assert(IsValidConsoleColour(colour_code));
88 
89  char *str;
91  /* Redirect the string to the client */
93  return;
94  }
95 
98  return;
99  }
100 
101  /* Create a copy of the string, strip if of colours and invalid
102  * characters and (when applicable) assign it to the console buffer */
103  str = stredup(string);
104  str_strip_colours(str);
105  str_validate(str, str + strlen(str));
106 
107  if (_network_dedicated) {
108  NetworkAdminConsole("console", str);
109  fprintf(stdout, "%s%s\n", GetLogPrefix(), str);
110  fflush(stdout);
111  IConsoleWriteToLogFile(str);
112  free(str); // free duplicated string since it's not used anymore
113  return;
114  }
115 
116  IConsoleWriteToLogFile(str);
117  IConsoleGUIPrint(colour_code, str);
118 }
119 
125 void CDECL IConsolePrintF(TextColour colour_code, const char *format, ...)
126 {
127  assert(IsValidConsoleColour(colour_code));
128 
129  va_list va;
130  char buf[ICON_MAX_STREAMSIZE];
131 
132  va_start(va, format);
133  vseprintf(buf, lastof(buf), format, va);
134  va_end(va);
135 
136  IConsolePrint(colour_code, buf);
137 }
138 
147 void IConsoleDebug(const char *dbg, const char *string)
148 {
149  if (_settings_client.gui.developer <= 1) return;
150  IConsolePrintF(CC_DEBUG, "dbg: [%s] %s", dbg, string);
151 }
152 
158 void IConsoleWarning(const char *string)
159 {
160  if (_settings_client.gui.developer == 0) return;
161  IConsolePrintF(CC_WARNING, "WARNING: %s", string);
162 }
163 
168 void IConsoleError(const char *string)
169 {
170  IConsolePrintF(CC_ERROR, "ERROR: %s", string);
171 }
172 
180 bool GetArgumentInteger(uint32 *value, const char *arg)
181 {
182  char *endptr;
183 
184  if (strcmp(arg, "on") == 0 || strcmp(arg, "true") == 0) {
185  *value = 1;
186  return true;
187  }
188  if (strcmp(arg, "off") == 0 || strcmp(arg, "false") == 0) {
189  *value = 0;
190  return true;
191  }
192 
193  *value = strtoul(arg, &endptr, 0);
194  return arg != endptr;
195 }
196 
202 template<class T>
203 void IConsoleAddSorted(T **base, T *item_new)
204 {
205  if (*base == nullptr) {
206  *base = item_new;
207  return;
208  }
209 
210  T *item_before = nullptr;
211  T *item = *base;
212  /* The list is alphabetically sorted, insert the new item at the correct location */
213  while (item != nullptr) {
214  if (strcmp(item->name, item_new->name) > 0) break; // insert here
215 
216  item_before = item;
217  item = item->next;
218  }
219 
220  if (item_before == nullptr) {
221  *base = item_new;
222  } else {
223  item_before->next = item_new;
224  }
225 
226  item_new->next = item;
227 }
228 
235 {
236  char *q = name;
237  for (const char *p = name; *p != '\0'; p++) {
238  if (*p != '_') *q++ = *p;
239  }
240  *q = '\0';
241  return name;
242 }
243 
249 void IConsoleCmdRegister(const char *name, IConsoleCmdProc *proc, IConsoleHook *hook)
250 {
251  IConsoleCmd *item_new = MallocT<IConsoleCmd>(1);
252  item_new->name = RemoveUnderscores(stredup(name));
253  item_new->next = nullptr;
254  item_new->proc = proc;
255  item_new->hook = hook;
256 
257  IConsoleAddSorted(&_iconsole_cmds, item_new);
258 }
259 
266 {
267  IConsoleCmd *item;
268 
269  for (item = _iconsole_cmds; item != nullptr; item = item->next) {
270  if (strcmp(item->name, name) == 0) return item;
271  }
272  return nullptr;
273 }
274 
280 void IConsoleAliasRegister(const char *name, const char *cmd)
281 {
282  if (IConsoleAliasGet(name) != nullptr) {
283  IConsoleError("an alias with this name already exists; insertion aborted");
284  return;
285  }
286 
287  char *new_alias = RemoveUnderscores(stredup(name));
288  char *cmd_aliased = stredup(cmd);
289  IConsoleAlias *item_new = MallocT<IConsoleAlias>(1);
290 
291  item_new->next = nullptr;
292  item_new->cmdline = cmd_aliased;
293  item_new->name = new_alias;
294 
296 }
297 
304 {
305  IConsoleAlias *item;
306 
307  for (item = _iconsole_aliases; item != nullptr; item = item->next) {
308  if (strcmp(item->name, name) == 0) return item;
309  }
310 
311  return nullptr;
312 }
320 static void IConsoleAliasExec(const IConsoleAlias *alias, byte tokencount, char *tokens[ICON_TOKEN_COUNT], const uint recurse_count)
321 {
322  char alias_buffer[ICON_MAX_STREAMSIZE] = { '\0' };
323  char *alias_stream = alias_buffer;
324 
325  DEBUG(console, 6, "Requested command is an alias; parsing...");
326 
327  if (recurse_count > ICON_MAX_RECURSE) {
328  IConsoleError("Too many alias expansions, recursion limit reached. Aborting");
329  return;
330  }
331 
332  for (const char *cmdptr = alias->cmdline; *cmdptr != '\0'; cmdptr++) {
333  switch (*cmdptr) {
334  case '\'': // ' will double for ""
335  alias_stream = strecpy(alias_stream, "\"", lastof(alias_buffer));
336  break;
337 
338  case ';': // Cmd separator; execute previous and start new command
339  IConsoleCmdExec(alias_buffer, recurse_count);
340 
341  alias_stream = alias_buffer;
342  *alias_stream = '\0'; // Make sure the new command is terminated.
343 
344  cmdptr++;
345  break;
346 
347  case '%': // Some or all parameters
348  cmdptr++;
349  switch (*cmdptr) {
350  case '+': { // All parameters separated: "[param 1]" "[param 2]"
351  for (uint i = 0; i != tokencount; i++) {
352  if (i != 0) alias_stream = strecpy(alias_stream, " ", lastof(alias_buffer));
353  alias_stream = strecpy(alias_stream, "\"", lastof(alias_buffer));
354  alias_stream = strecpy(alias_stream, tokens[i], lastof(alias_buffer));
355  alias_stream = strecpy(alias_stream, "\"", lastof(alias_buffer));
356  }
357  break;
358  }
359 
360  case '!': { // Merge the parameters to one: "[param 1] [param 2] [param 3...]"
361  alias_stream = strecpy(alias_stream, "\"", lastof(alias_buffer));
362  for (uint i = 0; i != tokencount; i++) {
363  if (i != 0) alias_stream = strecpy(alias_stream, " ", lastof(alias_buffer));
364  alias_stream = strecpy(alias_stream, tokens[i], lastof(alias_buffer));
365  }
366  alias_stream = strecpy(alias_stream, "\"", lastof(alias_buffer));
367  break;
368  }
369 
370  default: { // One specific parameter: %A = [param 1] %B = [param 2] ...
371  int param = *cmdptr - 'A';
372 
373  if (param < 0 || param >= tokencount) {
374  IConsoleError("too many or wrong amount of parameters passed to alias, aborting");
375  IConsolePrintF(CC_WARNING, "Usage of alias '%s': %s", alias->name, alias->cmdline);
376  return;
377  }
378 
379  alias_stream = strecpy(alias_stream, "\"", lastof(alias_buffer));
380  alias_stream = strecpy(alias_stream, tokens[param], lastof(alias_buffer));
381  alias_stream = strecpy(alias_stream, "\"", lastof(alias_buffer));
382  break;
383  }
384  }
385  break;
386 
387  default:
388  *alias_stream++ = *cmdptr;
389  *alias_stream = '\0';
390  break;
391  }
392 
393  if (alias_stream >= lastof(alias_buffer) - 1) {
394  IConsoleError("Requested alias execution would overflow execution buffer");
395  return;
396  }
397  }
398 
399  IConsoleCmdExec(alias_buffer, recurse_count);
400 }
401 
407 void IConsoleCmdExec(const char *cmdstr, const uint recurse_count)
408 {
409  const char *cmdptr;
410  char *tokens[ICON_TOKEN_COUNT], tokenstream[ICON_MAX_STREAMSIZE];
411  uint t_index, tstream_i;
412 
413  bool longtoken = false;
414  bool foundtoken = false;
415 
416  if (cmdstr[0] == '#') return; // comments
417 
418  for (cmdptr = cmdstr; *cmdptr != '\0'; cmdptr++) {
419  if (!IsValidChar(*cmdptr, CS_ALPHANUMERAL)) {
420  IConsoleError("command contains malformed characters, aborting");
421  IConsolePrintF(CC_ERROR, "ERROR: command was: '%s'", cmdstr);
422  return;
423  }
424  }
425 
426  DEBUG(console, 4, "Executing cmdline: '%s'", cmdstr);
427 
428  memset(&tokens, 0, sizeof(tokens));
429  memset(&tokenstream, 0, sizeof(tokenstream));
430 
431  /* 1. Split up commandline into tokens, separated by spaces, commands
432  * enclosed in "" are taken as one token. We can only go as far as the amount
433  * of characters in our stream or the max amount of tokens we can handle */
434  for (cmdptr = cmdstr, t_index = 0, tstream_i = 0; *cmdptr != '\0'; cmdptr++) {
435  if (tstream_i >= lengthof(tokenstream)) {
436  IConsoleError("command line too long");
437  return;
438  }
439 
440  switch (*cmdptr) {
441  case ' ': // Token separator
442  if (!foundtoken) break;
443 
444  if (longtoken) {
445  tokenstream[tstream_i] = *cmdptr;
446  } else {
447  tokenstream[tstream_i] = '\0';
448  foundtoken = false;
449  }
450 
451  tstream_i++;
452  break;
453  case '"': // Tokens enclosed in "" are one token
454  longtoken = !longtoken;
455  if (!foundtoken) {
456  if (t_index >= lengthof(tokens)) {
457  IConsoleError("command line too long");
458  return;
459  }
460  tokens[t_index++] = &tokenstream[tstream_i];
461  foundtoken = true;
462  }
463  break;
464  case '\\': // Escape character for ""
465  if (cmdptr[1] == '"' && tstream_i + 1 < lengthof(tokenstream)) {
466  tokenstream[tstream_i++] = *++cmdptr;
467  break;
468  }
469  FALLTHROUGH;
470  default: // Normal character
471  tokenstream[tstream_i++] = *cmdptr;
472 
473  if (!foundtoken) {
474  if (t_index >= lengthof(tokens)) {
475  IConsoleError("command line too long");
476  return;
477  }
478  tokens[t_index++] = &tokenstream[tstream_i - 1];
479  foundtoken = true;
480  }
481  break;
482  }
483  }
484 
485  for (uint i = 0; i < lengthof(tokens) && tokens[i] != nullptr; i++) {
486  DEBUG(console, 8, "Token %d is: '%s'", i, tokens[i]);
487  }
488 
489  if (StrEmpty(tokens[0])) return; // don't execute empty commands
490  /* 2. Determine type of command (cmd or alias) and execute
491  * First try commands, then aliases. Execute
492  * the found action taking into account its hooking code
493  */
494  RemoveUnderscores(tokens[0]);
495  IConsoleCmd *cmd = IConsoleCmdGet(tokens[0]);
496  if (cmd != nullptr) {
497  ConsoleHookResult chr = (cmd->hook == nullptr ? CHR_ALLOW : cmd->hook(true));
498  switch (chr) {
499  case CHR_ALLOW:
500  if (!cmd->proc(t_index, tokens)) { // index started with 0
501  cmd->proc(0, nullptr); // if command failed, give help
502  }
503  return;
504 
505  case CHR_DISALLOW: return;
506  case CHR_HIDE: break;
507  }
508  }
509 
510  t_index--;
511  IConsoleAlias *alias = IConsoleAliasGet(tokens[0]);
512  if (alias != nullptr) {
513  IConsoleAliasExec(alias, t_index, &tokens[1], recurse_count + 1);
514  return;
515  }
516 
517  IConsoleError("command not found");
518 }
IConsoleCmd::proc
IConsoleCmdProc * proc
process executed when command is typed
Definition: console_internal.h:39
INVALID_CLIENT_ID
@ INVALID_CLIENT_ID
Client is not part of anything.
Definition: network_type.h:40
str_validate
void str_validate(char *str, const char *last, StringValidationSettings settings)
Scans the string for valid characters and if it finds invalid ones, replaces them with a question mar...
Definition: string.cpp:255
IConsoleAlias::cmdline
char * cmdline
command(s) that is/are being aliased
Definition: console_internal.h:59
_iconsole_cmds
IConsoleCmd * _iconsole_cmds
list of registered commands
Definition: console.cpp:27
TextColour
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition: gfx_type.h:250
CompanyProperties::name
std::string name
Name of the company if the user changed it.
Definition: company_base.h:58
_settings_client
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:79
_redirect_console_to_client
ClientID _redirect_console_to_client
If not invalid, redirect the console output to a client.
Definition: network.cpp:59
IConsoleGUIPrint
void IConsoleGUIPrint(TextColour colour_code, char *str)
Handle the printing of text entered into the console or redirected there by any other means.
Definition: console_gui.cpp:511
INVALID_ADMIN_ID
static const AdminIndex INVALID_ADMIN_ID
An invalid admin marker.
Definition: network_type.h:54
NetworkAdminConsole
void NetworkAdminConsole(const char *origin, const char *string)
Send console to the admin network (if they did opt in for the respective update).
Definition: network_admin.cpp:949
_redirect_console_to_admin
AdminIndex _redirect_console_to_admin
Redirection of the (remote) console to the admin.
Definition: network_admin.cpp:31
CC_DEFAULT
static const TextColour CC_DEFAULT
Default colour of the console.
Definition: console_type.h:23
IConsoleAliasGet
IConsoleAlias * IConsoleAliasGet(const char *name)
Find the alias pointed to by its string.
Definition: console.cpp:303
IConsoleCmd::hook
IConsoleHook * hook
any special trigger action that needs executing
Definition: console_internal.h:40
IConsoleCmdGet
IConsoleCmd * IConsoleCmdGet(const char *name)
Find the command pointed to by its string.
Definition: console.cpp:265
ICON_TOKEN_COUNT
static const uint ICON_TOKEN_COUNT
Maximum number of tokens in one command.
Definition: console.cpp:23
IConsoleAddSorted
void IConsoleAddSorted(T **base, T *item_new)
Add an item to an alphabetically sorted list.
Definition: console.cpp:203
ICON_MAX_STREAMSIZE
static const uint ICON_MAX_STREAMSIZE
maximum length of a totally expanded command
Definition: console_internal.h:16
GetLogPrefix
const char * GetLogPrefix()
Get the prefix for logs; if show_date_in_logs is enabled it returns the date, otherwise it returns no...
Definition: debug.cpp:247
DEBUG
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:35
console_internal.h
IConsoleError
void IConsoleError(const char *string)
It is possible to print error information to the console.
Definition: console.cpp:168
IConsoleCmd
Definition: console_internal.h:35
str_strip_colours
void str_strip_colours(char *str)
Scans the string for colour codes and strips them.
Definition: string.cpp:324
CHR_ALLOW
@ CHR_ALLOW
Allow command execution.
Definition: console_internal.h:20
GetArgumentInteger
bool GetArgumentInteger(uint32 *value, const char *arg)
Change a string into its number representation.
Definition: console.cpp:180
safeguards.h
IsValidConsoleColour
bool IsValidConsoleColour(TextColour c)
Check whether the given TextColour is valid for console usage.
Definition: console_gui.cpp:523
RemoveUnderscores
char * RemoveUnderscores(char *name)
Remove underscores from a string; the string will be modified!
Definition: console.cpp:234
IsValidChar
bool IsValidChar(WChar key, CharSetFilter afilter)
Only allow certain keys.
Definition: string.cpp:401
StrEmpty
static bool StrEmpty(const char *s)
Check if a string buffer is empty.
Definition: string_func.h:60
CC_DEBUG
static const TextColour CC_DEBUG
Colour for debug output.
Definition: console_type.h:27
GUISettings::developer
uint8 developer
print non-fatal warnings in console (>= 1), copy debug output to console (== 2)
Definition: settings_type.h:173
NetworkServerSendRcon
void NetworkServerSendRcon(ClientID client_id, TextColour colour_code, const char *string)
Send an rcon reply to the client.
Definition: network_server.cpp:2098
settings_type.h
vseprintf
int CDECL vseprintf(char *str, const char *last, const char *format, va_list ap)
Safer implementation of vsnprintf; same as vsnprintf except:
Definition: string.cpp:61
ICON_MAX_RECURSE
static const uint ICON_MAX_RECURSE
Maximum number of recursion.
Definition: console.cpp:24
_network_dedicated
bool _network_dedicated
are we a dedicated server?
Definition: network.cpp:55
_iconsole_aliases
IConsoleAlias * _iconsole_aliases
list of registered aliases
Definition: console.cpp:28
stdafx.h
IConsolePrint
void IConsolePrint(TextColour colour_code, const char *string)
Handle the printing of text entered into the console or redirected there by any other means.
Definition: console.cpp:85
CS_ALPHANUMERAL
@ CS_ALPHANUMERAL
Both numeric and alphabetic and spaces and stuff.
Definition: string_type.h:27
IConsoleAlias::name
char * name
name of the alias
Definition: console_internal.h:56
IConsoleAlias::next
IConsoleAlias * next
next alias in list
Definition: console_internal.h:57
IConsoleAliasExec
static void IConsoleAliasExec(const IConsoleAlias *alias, byte tokencount, char *tokens[ICON_TOKEN_COUNT], const uint recurse_count)
An alias is just another name for a command, or for more commands Execute it as well.
Definition: console.cpp:320
IConsoleAlias
–Aliases– Aliases are like shortcuts for complex functions, variable assignments, etc.
Definition: console_internal.h:55
IConsoleCmd::name
char * name
name of command
Definition: console_internal.h:36
IConsoleCmd::next
IConsoleCmd * next
next command in list
Definition: console_internal.h:37
CC_ERROR
static const TextColour CC_ERROR
Colour for error lines.
Definition: console_type.h:24
stredup
char * stredup(const char *s, const char *last)
Create a duplicate of the given string.
Definition: string.cpp:137
CHR_DISALLOW
@ CHR_DISALLOW
Disallow command execution.
Definition: console_internal.h:21
network.h
IConsoleDebug
void IConsoleDebug(const char *dbg, const char *string)
It is possible to print debugging information to the console, which is achieved by using this functio...
Definition: console.cpp:147
lengthof
#define lengthof(x)
Return the length of an fixed size array.
Definition: stdafx.h:369
CHR_HIDE
@ CHR_HIDE
Hide the existence of the command.
Definition: console_internal.h:22
IConsoleCmdExec
void IConsoleCmdExec(const char *cmdstr, const uint recurse_count)
Execute a given command passed to us.
Definition: console.cpp:407
IConsoleCmdRegister
void IConsoleCmdRegister(const char *name, IConsoleCmdProc *proc, IConsoleHook *hook)
Register a new command to be used in the console.
Definition: console.cpp:249
IConsoleAliasRegister
void IConsoleAliasRegister(const char *name, const char *cmd)
Register a an alias for an already existing command in the console.
Definition: console.cpp:280
IConsoleCmdProc
bool IConsoleCmdProc(byte argc, char *argv[])
–Commands– Commands are commands, or functions.
Definition: console_internal.h:33
network_admin.h
console_func.h
strecpy
char * strecpy(char *dst, const char *src, const char *last)
Copies characters from one buffer to another.
Definition: string.cpp:112
CC_WARNING
static const TextColour CC_WARNING
Colour for warning lines.
Definition: console_type.h:25
free
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: stdafx.h:456
IConsoleWarning
void IConsoleWarning(const char *string)
It is possible to print warnings to the console.
Definition: console.cpp:158
lastof
#define lastof(x)
Get the last element of an fixed size array.
Definition: stdafx.h:385
network_func.h
ConsoleHookResult
ConsoleHookResult
Return values of console hooks (#IConsoleHook).
Definition: console_internal.h:19
IConsolePrintF
void CDECL IConsolePrintF(TextColour colour_code, const char *format,...)
Handle the printing of text entered into the console or redirected there by any other means.
Definition: console.cpp:125
ClientSettings::gui
GUISettings gui
settings related to the GUI
Definition: settings_type.h:581
debug.h
NetworkServerSendAdminRcon
void NetworkServerSendAdminRcon(AdminIndex admin_index, TextColour colour_code, const char *string)
Pass the rcon reply to the admin.
Definition: network_admin.cpp:939