mirror of https://github.com/OpenTTD/OpenTTD
Codechange: make IConsoleCmdExec use C++ strings internally
parent
28d0e6dfc0
commit
f04cf54939
191
src/console.cpp
191
src/console.cpp
|
@ -8,6 +8,8 @@
|
||||||
/** @file console.cpp Handling of the in-game console. */
|
/** @file console.cpp Handling of the in-game console. */
|
||||||
|
|
||||||
#include "stdafx.h"
|
#include "stdafx.h"
|
||||||
|
#include "core/string_builder.hpp"
|
||||||
|
#include "core/string_consumer.hpp"
|
||||||
#include "console_internal.h"
|
#include "console_internal.h"
|
||||||
#include "network/network.h"
|
#include "network/network.h"
|
||||||
#include "network/network_func.h"
|
#include "network/network_func.h"
|
||||||
|
@ -18,7 +20,6 @@
|
||||||
|
|
||||||
#include "safeguards.h"
|
#include "safeguards.h"
|
||||||
|
|
||||||
static const uint ICON_TOKEN_COUNT = 20; ///< Maximum number of tokens in one command
|
|
||||||
static const uint ICON_MAX_RECURSE = 10; ///< Maximum number of recursion
|
static const uint ICON_MAX_RECURSE = 10; ///< Maximum number of recursion
|
||||||
|
|
||||||
/* console parser */
|
/* console parser */
|
||||||
|
@ -176,14 +177,12 @@ static std::string RemoveUnderscores(std::string name)
|
||||||
/**
|
/**
|
||||||
* An alias is just another name for a command, or for more commands
|
* An alias is just another name for a command, or for more commands
|
||||||
* Execute it as well.
|
* Execute it as well.
|
||||||
* @param *alias is the alias of the command
|
* @param alias is the alias of the command
|
||||||
* @param tokencount the number of parameters passed
|
* @param tokens are the parameters given to the original command (0 is the first param)
|
||||||
* @param *tokens are the parameters given to the original command (0 is the first param)
|
* @param recurse_count the number of re-entrant calls to this function
|
||||||
*/
|
*/
|
||||||
static void IConsoleAliasExec(const IConsoleAlias *alias, uint8_t tokencount, char *tokens[ICON_TOKEN_COUNT], const uint recurse_count)
|
static void IConsoleAliasExec(const IConsoleAlias *alias, std::span<std::string> tokens, uint recurse_count)
|
||||||
{
|
{
|
||||||
std::string alias_buffer;
|
|
||||||
|
|
||||||
Debug(console, 6, "Requested command is an alias; parsing...");
|
Debug(console, 6, "Requested command is an alias; parsing...");
|
||||||
|
|
||||||
if (recurse_count > ICON_MAX_RECURSE) {
|
if (recurse_count > ICON_MAX_RECURSE) {
|
||||||
|
@ -191,72 +190,75 @@ static void IConsoleAliasExec(const IConsoleAlias *alias, uint8_t tokencount, ch
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const char *cmdptr = alias->cmdline.c_str(); *cmdptr != '\0'; cmdptr++) {
|
std::string buffer;
|
||||||
switch (*cmdptr) {
|
StringBuilder builder{buffer};
|
||||||
|
|
||||||
|
StringConsumer consumer{alias->cmdline};
|
||||||
|
while (consumer.AnyBytesLeft()) {
|
||||||
|
auto c = consumer.TryReadUtf8();
|
||||||
|
if (!c.has_value()) {
|
||||||
|
IConsolePrint(CC_ERROR, "Alias '{}' ('{}') contains malformed characters.", alias->name, alias->cmdline);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (*c) {
|
||||||
case '\'': // ' will double for ""
|
case '\'': // ' will double for ""
|
||||||
alias_buffer += '\"';
|
builder.PutChar('\"');
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ';': // Cmd separator; execute previous and start new command
|
case ';': // Cmd separator; execute previous and start new command
|
||||||
IConsoleCmdExec(alias_buffer, recurse_count);
|
IConsoleCmdExec(builder.GetString(), recurse_count);
|
||||||
|
|
||||||
alias_buffer.clear();
|
buffer.clear();
|
||||||
|
|
||||||
cmdptr++;
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case '%': // Some or all parameters
|
case '%': // Some or all parameters
|
||||||
cmdptr++;
|
c = consumer.ReadUtf8();
|
||||||
switch (*cmdptr) {
|
switch (*c) {
|
||||||
case '+': { // All parameters separated: "[param 1]" "[param 2]"
|
case '+': { // All parameters separated: "[param 1]" "[param 2]"
|
||||||
for (uint i = 0; i != tokencount; i++) {
|
for (size_t i = 0; i < tokens.size(); ++i) {
|
||||||
if (i != 0) alias_buffer += ' ';
|
if (i != 0) builder.PutChar(' ');
|
||||||
alias_buffer += '\"';
|
builder.PutChar('\"');
|
||||||
alias_buffer += tokens[i];
|
builder += tokens[i];
|
||||||
alias_buffer += '\"';
|
builder.PutChar('\"');
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
case '!': { // Merge the parameters to one: "[param 1] [param 2] [param 3...]"
|
case '!': { // Merge the parameters to one: "[param 1] [param 2] [param 3...]"
|
||||||
alias_buffer += '\"';
|
builder.PutChar('\"');
|
||||||
for (uint i = 0; i != tokencount; i++) {
|
for (size_t i = 0; i < tokens.size(); ++i) {
|
||||||
if (i != 0) alias_buffer += " ";
|
if (i != 0) builder.PutChar(' ');
|
||||||
alias_buffer += tokens[i];
|
builder += tokens[i];
|
||||||
}
|
}
|
||||||
alias_buffer += '\"';
|
builder.PutChar('\"');
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
default: { // One specific parameter: %A = [param 1] %B = [param 2] ...
|
default: { // One specific parameter: %A = [param 1] %B = [param 2] ...
|
||||||
int param = *cmdptr - 'A';
|
size_t param = *c - 'A';
|
||||||
|
|
||||||
if (param < 0 || param >= tokencount) {
|
if (param >= tokens.size()) {
|
||||||
IConsolePrint(CC_ERROR, "Too many or wrong amount of parameters passed to alias.");
|
IConsolePrint(CC_ERROR, "Too many or wrong amount of parameters passed to alias.");
|
||||||
IConsolePrint(CC_HELP, "Usage of alias '{}': '{}'.", alias->name, alias->cmdline);
|
IConsolePrint(CC_HELP, "Usage of alias '{}': '{}'.", alias->name, alias->cmdline);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
alias_buffer += '\"';
|
builder.PutChar('\"');
|
||||||
alias_buffer += tokens[param];
|
builder += tokens[param];
|
||||||
alias_buffer += '\"';
|
builder.PutChar('\"');
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
alias_buffer += *cmdptr;
|
builder.PutUtf8(*c);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (alias_buffer.size() >= ICON_MAX_STREAMSIZE - 1) {
|
|
||||||
IConsolePrint(CC_ERROR, "Requested alias execution would overflow execution buffer.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
IConsoleCmdExec(alias_buffer, recurse_count);
|
IConsoleCmdExec(builder.GetString(), recurse_count);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -264,88 +266,73 @@ static void IConsoleAliasExec(const IConsoleAlias *alias, uint8_t tokencount, ch
|
||||||
* individual tokens (separated by spaces), then execute it if possible
|
* individual tokens (separated by spaces), then execute it if possible
|
||||||
* @param command_string string to be parsed and executed
|
* @param command_string string to be parsed and executed
|
||||||
*/
|
*/
|
||||||
void IConsoleCmdExec(const std::string &command_string, const uint recurse_count)
|
void IConsoleCmdExec(std::string_view command_string, const uint recurse_count)
|
||||||
{
|
{
|
||||||
const char *cmdptr;
|
|
||||||
char *tokens[ICON_TOKEN_COUNT], tokenstream[ICON_MAX_STREAMSIZE];
|
|
||||||
uint t_index, tstream_i;
|
|
||||||
|
|
||||||
bool longtoken = false;
|
|
||||||
bool foundtoken = false;
|
|
||||||
|
|
||||||
if (command_string[0] == '#') return; // comments
|
if (command_string[0] == '#') return; // comments
|
||||||
|
|
||||||
for (cmdptr = command_string.c_str(); *cmdptr != '\0'; cmdptr++) {
|
|
||||||
if (!IsValidChar(*cmdptr, CS_ALPHANUMERAL)) {
|
|
||||||
IConsolePrint(CC_ERROR, "Command '{}' contains malformed characters.", command_string);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Debug(console, 4, "Executing cmdline: '{}'", command_string);
|
Debug(console, 4, "Executing cmdline: '{}'", command_string);
|
||||||
|
|
||||||
memset(&tokens, 0, sizeof(tokens));
|
std::string buffer;
|
||||||
memset(&tokenstream, 0, sizeof(tokenstream));
|
StringBuilder builder{buffer};
|
||||||
|
StringConsumer consumer{command_string};
|
||||||
|
|
||||||
|
std::vector<std::string> tokens;
|
||||||
|
bool found_token = false;
|
||||||
|
bool in_quotes = false;
|
||||||
|
|
||||||
/* 1. Split up commandline into tokens, separated by spaces, commands
|
/* 1. Split up commandline into tokens, separated by spaces, commands
|
||||||
* enclosed in "" are taken as one token. We can only go as far as the amount
|
* enclosed in "" are taken as one token. We can only go as far as the amount
|
||||||
* of characters in our stream or the max amount of tokens we can handle */
|
* of characters in our stream or the max amount of tokens we can handle */
|
||||||
for (cmdptr = command_string.c_str(), t_index = 0, tstream_i = 0; *cmdptr != '\0'; cmdptr++) {
|
while (consumer.AnyBytesLeft()) {
|
||||||
if (tstream_i >= lengthof(tokenstream)) {
|
auto c = consumer.TryReadUtf8();
|
||||||
IConsolePrint(CC_ERROR, "Command line too long.");
|
if (!c.has_value()) {
|
||||||
|
IConsolePrint(CC_ERROR, "Command '{}' contains malformed characters.", command_string);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (*cmdptr) {
|
switch (*c) {
|
||||||
case ' ': // Token separator
|
case ' ': // Token separator
|
||||||
if (!foundtoken) break;
|
if (!found_token) break;
|
||||||
|
|
||||||
if (longtoken) {
|
if (in_quotes) {
|
||||||
tokenstream[tstream_i] = *cmdptr;
|
builder.PutUtf8(*c);
|
||||||
} else {
|
break;
|
||||||
tokenstream[tstream_i] = '\0';
|
|
||||||
foundtoken = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
tstream_i++;
|
|
||||||
break;
|
|
||||||
case '"': // Tokens enclosed in "" are one token
|
|
||||||
longtoken = !longtoken;
|
|
||||||
if (!foundtoken) {
|
|
||||||
if (t_index >= lengthof(tokens)) {
|
|
||||||
IConsolePrint(CC_ERROR, "Command line too long.");
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
tokens[t_index++] = &tokenstream[tstream_i];
|
|
||||||
foundtoken = true;
|
tokens.emplace_back(std::move(buffer));
|
||||||
}
|
buffer.clear();
|
||||||
break;
|
found_token = false;
|
||||||
case '\\': // Escape character for ""
|
|
||||||
if (cmdptr[1] == '"' && tstream_i + 1 < lengthof(tokenstream)) {
|
|
||||||
tokenstream[tstream_i++] = *++cmdptr;
|
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
[[fallthrough]];
|
|
||||||
default: // Normal character
|
|
||||||
tokenstream[tstream_i++] = *cmdptr;
|
|
||||||
|
|
||||||
if (!foundtoken) {
|
case '"': // Tokens enclosed in "" are one token
|
||||||
if (t_index >= lengthof(tokens)) {
|
in_quotes = !in_quotes;
|
||||||
IConsolePrint(CC_ERROR, "Command line too long.");
|
found_token = true;
|
||||||
return;
|
break;
|
||||||
|
|
||||||
|
case '\\': // Escape character for ""
|
||||||
|
if (consumer.ReadUtf8If('"')) {
|
||||||
|
builder.PutUtf8('"');
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
tokens[t_index++] = &tokenstream[tstream_i - 1];
|
[[fallthrough]];
|
||||||
foundtoken = true;
|
|
||||||
}
|
default: // Normal character
|
||||||
break;
|
builder.PutUtf8(*c);
|
||||||
|
found_token = true;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (uint i = 0; i < lengthof(tokens) && tokens[i] != nullptr; i++) {
|
if (found_token) {
|
||||||
|
tokens.emplace_back(std::move(buffer));
|
||||||
|
buffer.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
for (size_t i = 0; i < tokens.size(); i++) {
|
||||||
Debug(console, 8, "Token {} is: '{}'", i, tokens[i]);
|
Debug(console, 8, "Token {} is: '{}'", i, tokens[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (StrEmpty(tokens[0])) return; // don't execute empty commands
|
if (tokens.empty() || tokens[0].empty()) return; // don't execute empty commands
|
||||||
/* 2. Determine type of command (cmd or alias) and execute
|
/* 2. Determine type of command (cmd or alias) and execute
|
||||||
* First try commands, then aliases. Execute
|
* First try commands, then aliases. Execute
|
||||||
* the found action taking into account its hooking code
|
* the found action taking into account its hooking code
|
||||||
|
@ -354,21 +341,23 @@ void IConsoleCmdExec(const std::string &command_string, const uint recurse_count
|
||||||
if (cmd != nullptr) {
|
if (cmd != nullptr) {
|
||||||
ConsoleHookResult chr = (cmd->hook == nullptr ? CHR_ALLOW : cmd->hook(true));
|
ConsoleHookResult chr = (cmd->hook == nullptr ? CHR_ALLOW : cmd->hook(true));
|
||||||
switch (chr) {
|
switch (chr) {
|
||||||
case CHR_ALLOW:
|
case CHR_ALLOW: {
|
||||||
if (!cmd->proc(t_index, tokens)) { // index started with 0
|
std::vector<char *> c_strings;
|
||||||
|
for (auto &token : tokens) c_strings.emplace_back(token.data());
|
||||||
|
if (!cmd->proc(static_cast<uint8_t>(tokens.size()), c_strings.data())) { // index started with 0
|
||||||
cmd->proc(0, nullptr); // if command failed, give help
|
cmd->proc(0, nullptr); // if command failed, give help
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
case CHR_DISALLOW: return;
|
case CHR_DISALLOW: return;
|
||||||
case CHR_HIDE: break;
|
case CHR_HIDE: break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
t_index--;
|
|
||||||
IConsoleAlias *alias = IConsole::AliasGet(tokens[0]);
|
IConsoleAlias *alias = IConsole::AliasGet(tokens[0]);
|
||||||
if (alias != nullptr) {
|
if (alias != nullptr) {
|
||||||
IConsoleAliasExec(alias, t_index, &tokens[1], recurse_count + 1);
|
IConsoleAliasExec(alias, std::span(tokens).subspan(1), recurse_count + 1);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -46,7 +46,7 @@ inline void IConsolePrint(TextColour colour_code, fmt::format_string<A, Args...>
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Parser */
|
/* Parser */
|
||||||
void IConsoleCmdExec(const std::string &command_string, const uint recurse_count = 0);
|
void IConsoleCmdExec(std::string_view command_string, const uint recurse_count = 0);
|
||||||
|
|
||||||
bool IsValidConsoleColour(TextColour c);
|
bool IsValidConsoleColour(TextColour c);
|
||||||
|
|
||||||
|
|
|
@ -13,7 +13,6 @@
|
||||||
#include "gfx_type.h"
|
#include "gfx_type.h"
|
||||||
|
|
||||||
static const uint ICON_CMDLN_SIZE = 1024; ///< maximum length of a typed in command
|
static const uint ICON_CMDLN_SIZE = 1024; ///< maximum length of a typed in command
|
||||||
static const uint ICON_MAX_STREAMSIZE = 2048; ///< maximum length of a totally expanded command
|
|
||||||
|
|
||||||
/** Return values of console hooks (#IConsoleHook). */
|
/** Return values of console hooks (#IConsoleHook). */
|
||||||
enum ConsoleHookResult : uint8_t {
|
enum ConsoleHookResult : uint8_t {
|
||||||
|
|
Loading…
Reference in New Issue