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