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