Changeset - r25044:b22efcf16422
[Not reviewed]
master
0 1 0
Patric Stout - 3 years ago 2021-03-12 08:23:03
truebrain@openttd.org
Fix: calling "exec" from script never returned

Example:

exec other.script
echo hello

The "echo" was never executed.
1 file changed with 11 insertions and 5 deletions:
0 comments (0 inline, 0 general)
src/console_cmds.cpp
Show inline comments
 
@@ -24,49 +24,49 @@
 
#include "fileio_func.h"
 
#include "screenshot.h"
 
#include "genworld.h"
 
#include "strings_func.h"
 
#include "viewport_func.h"
 
#include "window_func.h"
 
#include "date_func.h"
 
#include "company_func.h"
 
#include "gamelog.h"
 
#include "ai/ai.hpp"
 
#include "ai/ai_config.hpp"
 
#include "newgrf.h"
 
#include "newgrf_profiling.h"
 
#include "console_func.h"
 
#include "engine_base.h"
 
#include "road.h"
 
#include "rail.h"
 
#include "game/game.hpp"
 
#include "table/strings.h"
 
#include <time.h>
 

	
 
#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 {
 
public:
 
	ConsoleFileList() : FileList()
 
	{
 
		this->file_list_valid = false;
 
	}
 

	
 
	/** Declare the file storage cache as being invalid, also clears all stored files. */
 
	void InvalidateFileList()
 
	{
 
		this->Clear();
 
		this->file_list_valid = false;
 
	}
 

	
 
	/**
 
	 * (Re-)validate the file storage cache. Only makes a change if the storage was invalid, or if \a force_reload.
 
	 * @param force_reload Always reload the file storage cache.
 
	 */
 
	void ValidateFileList(bool force_reload = false)
 
	{
 
		if (force_reload || !this->file_list_valid) {
 
			this->BuildFileList(FT_SAVEGAME, SLO_LOAD);
 
@@ -939,79 +939,85 @@ DEF_CONSOLE_CMD(ConNetworkConnect)
 

	
 
	return true;
 
}
 

	
 
/*********************************
 
 *  script file console commands
 
 *********************************/
 

	
 
DEF_CONSOLE_CMD(ConExec)
 
{
 
	if (argc == 0) {
 
		IConsoleHelp("Execute a local script file. Usage: 'exec <script> <?>'");
 
		return true;
 
	}
 

	
 
	if (argc < 2) return false;
 

	
 
	FILE *script_file = FioFOpenFile(argv[1], "r", BASE_DIR);
 

	
 
	if (script_file == nullptr) {
 
		if (argc == 2 || atoi(argv[2]) != 0) IConsoleError("script file not found");
 
		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') {
 
				*cmdptr = '\0';
 
				break;
 
			}
 
		}
 
		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;
 
}
 

	
 
DEF_CONSOLE_CMD(ConReturn)
 
{
 
	if (argc == 0) {
 
		IConsoleHelp("Stop executing a running script. Usage: 'return'");
 
		return true;
 
	}
 

	
 
	_script_running = false;
 
	_script_current_depth--;
 
	return true;
 
}
 

	
 
/*****************************
 
 *  default console commands
 
 ******************************/
 
extern bool CloseConsoleLogIfActive();
 

	
 
DEF_CONSOLE_CMD(ConScript)
 
{
 
	extern FILE *_iconsole_output_file;
 

	
 
	if (argc == 0) {
 
		IConsoleHelp("Start or stop logging console output to a file. Usage: 'script <filename>'");
 
		IConsoleHelp("If filename is omitted, a running log is stopped if it is active");
 
		return true;
 
	}
 

	
 
	if (!CloseConsoleLogIfActive()) {
 
		if (argc < 2) return false;
 

	
 
		IConsolePrintF(CC_DEFAULT, "file output started to: %s", argv[1]);
 
		_iconsole_output_file = fopen(argv[1], "ab");
 
		if (_iconsole_output_file == nullptr) IConsoleError("could not open file");
0 comments (0 inline, 0 general)