Skip to content

Commit

Permalink
RetroShell command processing is done in the RetroShell class (again)
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkwhoffmann committed Aug 19, 2024
1 parent 0da40fa commit 3e48e9f
Show file tree
Hide file tree
Showing 11 changed files with 231 additions and 185 deletions.
2 changes: 1 addition & 1 deletion Emulator/Headless.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ Headless::execScript()
// Launch the emulator thread
vamiga.launch(this, vamiga::process);

// Execute scripts
// Execute script
barrier.lock();
vamiga.retroShell.execScript(script);
barrier.lock();
Expand Down
8 changes: 3 additions & 5 deletions Emulator/Headless.h
Original file line number Diff line number Diff line change
Expand Up @@ -417,11 +417,10 @@ static const char *script[] = {
"server gdb",
"server gdb set PORT 8000",
"server gdb set VERBOSE true",
"server gdb set VERBOSE false"
};
"server gdb set VERBOSE false",

/*
static const char *debugScript[] = {
// Enter debugger
".",

"",
"break",
Expand Down Expand Up @@ -531,6 +530,5 @@ static const char *debugScript[] = {
"i host",
"i server"
};
*/

}
2 changes: 1 addition & 1 deletion Emulator/Misc/RetroShell/CommandConsole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ Console::initCommands(Command &root)

auto stream = std::ifstream(argv.front());
if (!stream.is_open()) throw Error(ERROR_FILE_NOT_FOUND, argv.front());
asyncExecScript(stream);
retroShell.asyncExecScript(stream);
});

root.add({"wait"}, {Arg::value, Arg::seconds},
Expand Down
139 changes: 17 additions & 122 deletions Emulator/Misc/RetroShell/Console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Console::_initialize()
history.push_back( { "", 0 } );

// Print the startup message and the input prompt
asyncExec("welcome");
// retroShell.asyncExec("welcome");
}

Console&
Expand Down Expand Up @@ -160,6 +160,18 @@ Console::clear()
needsDisplay();
}

bool
Console::isEmpty()
{
return storage.isCleared();
}

bool
Console::lastLineIsEmpty()
{
return storage.lastLineIsEmpty();
}

void
Console::printState()
{
Expand Down Expand Up @@ -254,7 +266,7 @@ Console::press(RetroShellKey key, bool shift)
if (tabPressed) {

// TAB was pressed twice
asyncExec("help \"" + input + "\"");
retroShell.asyncExec("help \"" + input + "\"");

} else {

Expand Down Expand Up @@ -339,7 +351,8 @@ Console::pressReturn(bool shift)
if (shift) {

// Switch the interpreter
retroShell.switchConsole();
// retroShell.switchConsole();
retroShell.asyncExec(".");

} else {

Expand All @@ -352,7 +365,7 @@ Console::pressReturn(bool shift)
ipos = (isize)history.size() - 1;

// Feed the command into the command queue
asyncExec(input);
retroShell.asyncExec(input);

// Clear the input line
input = "";
Expand Down Expand Up @@ -523,124 +536,6 @@ Console::getRoot()
return root;
}

void
Console::asyncExec(const string &command)
{
// Feed the command into the command queue
commands.push_back({ 0, command});
emulator.put(Cmd(CMD_RSH_EXECUTE));
}

void
Console::exec()
{
SYNCHRONIZED

// Only proceed if there is anything to process
if (commands.empty()) return;

std::pair<isize, string> cmd;

try {

while (!commands.empty()) {

cmd = commands.front();
commands.erase(commands.begin());
exec(cmd);
}
msgQueue.put(MSG_RSH_EXEC);

} catch (ScriptInterruption &) {

msgQueue.put(MSG_RSH_WAIT);

} catch (...) {

// Remove all remaining commands
commands = { };

msgQueue.put(MSG_RSH_ERROR);
}

// Print prompt
*this << getPrompt();
}

void
Console::exec(QueuedCmd cmd)
{
auto line = cmd.first;
auto command = cmd.second;

try {

// Print the command if it comes from a script
if (line) *this << command << '\n';

// Call the interpreter
exec(command);

} catch (ScriptInterruption &) {

// Rethrow the exception
throw;

} catch (std::exception &err) {

// Print error message
describe(err, line, command);

// Rethrow the exception if the command is not prefixed with 'try'
if (command.rfind("try", 0)) throw;
}
}

void
Console::asyncExecScript(std::stringstream &ss)
{
SYNCHRONIZED

std::string line;
isize nr = 1;

while (std::getline(ss, line)) {

commands.push_back({ nr++, line });
}

emulator.put(Cmd(CMD_RSH_EXECUTE));
}

void
Console::asyncExecScript(const std::ifstream &fs)
{
std::stringstream ss;
ss << fs.rdbuf();
asyncExecScript(ss);
}

void
Console::asyncExecScript(const string &contents)
{
std::stringstream ss;
ss << contents;
asyncExecScript(ss);
}

void
Console::abortScript()
{
{ SYNCHRONIZED

if (!commands.empty()) {

commands.clear();
agnus.cancel<SLOT_RSH>();
}
}
}

void
Console::exec(const string& userInput, bool verbose)
{
Expand Down
35 changes: 7 additions & 28 deletions Emulator/Misc/RetroShell/Console.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ struct ScriptInterruption: util::Exception {

class Console : public SubComponent {

friend class RetroShell;
friend class RshServer;
friend class Interpreter;

Expand Down Expand Up @@ -88,9 +89,6 @@ class Console : public SubComponent {
// Input line
string input;

// Command queue (stores all pending commands)
std::vector<QueuedCmd> commands;

// Cursor position
isize cursor = 0;

Expand Down Expand Up @@ -184,6 +182,12 @@ class Console : public SubComponent {
// Clears the console window
void clear();

// Returns true if the console is cleared
bool isEmpty();

// Returns true if the last line contains no text
bool lastLineIsEmpty();

// Prints the welcome message
virtual void welcome() = 0;

Expand Down Expand Up @@ -282,31 +286,6 @@ class Console : public SubComponent {
// Returns the root node of the currently active instruction tree
Command &getRoot();


//
// Executing commands
//

public:

// Adds a command to the list of pending commands
void asyncExec(const string &command);

// Adds the commands of a shell script to the list of pending commands
void asyncExecScript(std::stringstream &ss) throws;
void asyncExecScript(const std::ifstream &fs) throws;
void asyncExecScript(const string &contents) throws;
// void asyncExecScript(const class MediaFile &script) throws;

// Aborts the execution of a script
void abortScript();

// Executes all pending commands
void exec() throws;

// Executes a single pending command
void exec(QueuedCmd cmd) throws;

protected:

// Executes a single command
Expand Down
2 changes: 1 addition & 1 deletion Emulator/Misc/RetroShell/DebugConsole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace vamiga {
void
DebugConsole::_pause()
{
asyncExec("state");
retroShell.asyncExec("state");
}

string
Expand Down
Loading

0 comments on commit 3e48e9f

Please sign in to comment.