summaryrefslogtreecommitdiff
path: root/src/console_cmds.cpp
diff options
context:
space:
mode:
authorPatric Stout <truebrain@openttd.org>2021-03-12 09:23:03 +0100
committerPatric Stout <github@truebrain.nl>2021-03-13 10:01:05 +0100
commit8230cd009d1c8aeaa7b8ea353db2f94027b97eb7 (patch)
tree7ae3c7a33e027eebe03aa0156b13a5653702af9a /src/console_cmds.cpp
parentbcb3313e134e0e8cf12366ec9d1340e9986ccc57 (diff)
downloadopenttd-8230cd009d1c8aeaa7b8ea353db2f94027b97eb7.tar.xz
Fix: calling "exec" from script never returned
Example: exec other.script echo hello The "echo" was never executed.
Diffstat (limited to 'src/console_cmds.cpp')
-rw-r--r--src/console_cmds.cpp16
1 files changed, 11 insertions, 5 deletions
diff --git a/src/console_cmds.cpp b/src/console_cmds.cpp
index 18c4263f5..f66cfa6b6 100644
--- a/src/console_cmds.cpp
+++ b/src/console_cmds.cpp
@@ -45,7 +45,7 @@
#include "safeguards.h"
/* scriptfile handling */
-static bool _script_running; ///< Script is running (used to abort execution when #ConReturn is encountered).
+static uint _script_current_depth; ///< Depth of scripts running (used to abort execution when #ConReturn is encountered).
/** File list storage for the console, for caching the last 'ls' command. */
class ConsoleFileList : public FileList {
@@ -960,10 +960,11 @@ DEF_CONSOLE_CMD(ConExec)
return true;
}
- _script_running = true;
+ _script_current_depth++;
+ uint script_depth = _script_current_depth;
char cmdline[ICON_CMDLN_SIZE];
- while (_script_running && fgets(cmdline, sizeof(cmdline), script_file) != nullptr) {
+ while (fgets(cmdline, sizeof(cmdline), script_file) != nullptr) {
/* Remove newline characters from the executing script */
for (char *cmdptr = cmdline; *cmdptr != '\0'; cmdptr++) {
if (*cmdptr == '\n' || *cmdptr == '\r') {
@@ -972,13 +973,18 @@ DEF_CONSOLE_CMD(ConExec)
}
}
IConsoleCmdExec(cmdline);
+ /* Ensure that we are still on the same depth or that we returned via 'return'. */
+ assert(_script_current_depth == script_depth || _script_current_depth == script_depth - 1);
+
+ /* The 'return' command was executed. */
+ if (_script_current_depth == script_depth - 1) break;
}
if (ferror(script_file)) {
IConsoleError("Encountered error while trying to read from script file");
}
- _script_running = false;
+ if (_script_current_depth == script_depth) _script_current_depth--;
FioFCloseFile(script_file);
return true;
}
@@ -990,7 +996,7 @@ DEF_CONSOLE_CMD(ConReturn)
return true;
}
- _script_running = false;
+ _script_current_depth--;
return true;
}