From 62d5f0526f61957eef660710120723686c68493c Mon Sep 17 00:00:00 2001 From: Ritchie Cunningham Date: Sun, 21 Sep 2025 20:42:25 +0100 Subject: [PATCH] [Add] Implemented Lua command execution engine. Replacing hardcoded c++ 'ls' command with a generic system that finds and runs Lua scripts from the virual file system. --- common/src/command_processor.cpp | 38 ++++++++++++++------------------ 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/common/src/command_processor.cpp b/common/src/command_processor.cpp index f1695d1..0110856 100644 --- a/common/src/command_processor.cpp +++ b/common/src/command_processor.cpp @@ -21,6 +21,23 @@ std::string CommandProcessor::process_command(const std::string& command) { size_t end = cmd.find_last_not_of(" \t\n\r"); cmd = (end == std::string::npos) ? "" : cmd.substr(0, end+1); + /* === Generic script executer. === */ + std::string command_name = cmd.substr(0, cmd.find(" ")); + + /* Search for script in the /bin directory. */ + std::string script_filename = command_name + ".lua"; + vfs_node* root = _current_dir; + while(root->parent != nullptr) { + root = root->parent; + } + if(root->children.count("bin") && root->children["bin"]->children.count(script_filename)) { + vfs_node* script_node = root->children["bin"]->children[script_filename]; + /* TODO: Pass arguments to the Lua script. */ + return _lua->execute(script_node->content, _current_dir); + } + + + /* === Tmp fallback for built-in C++ commands. */ if(command.rfind("cd ", 0) == 0) { std::string target_dir_name = command.substr(3); if(target_dir_name == "..") { @@ -38,27 +55,6 @@ std::string CommandProcessor::process_command(const std::string& command) { return "cd: no such file or directory\n"; } return get_full_path(_current_dir); - } else if(cmd == "ls") { - /* Find the root of the VFS to look for the /bin directory. */ - vfs_node* root = _current_dir; - while(root->parent != nullptr) { - root = root->parent; - } - - fprintf(stderr, "DEBUG: VFS root found, name: '%s'\n", root->name.c_str()); - - /* Find and execute the ls.lua script. */ - if(root->children.count("bin")) { - fprintf(stderr, "DEBUG: Found '/bin' directory.\n"); - vfs_node* bin_dir = root->children["bin"]; - if(bin_dir->children.count("ls.lua")) { - fprintf(stderr, "DEBUG: Found '/bin/ls.lua'. Executing.\n"); - vfs_node* ls_script_node = bin_dir->children["ls.lua"]; - return _lua->execute(ls_script_node->content, _current_dir); - } - } - fprintf(stderr, "DEBUG: 'ls' command failed to find '/bin/ls.lua'.\n"); - return "ls: command not found\n"; } return "Unknown command: " + command + "\n";