Files
integration/luprex/cpp/core/luaconsole.cpp

201 lines
5.3 KiB
C++
Raw Normal View History

#include "wrap-string.hpp"
#include "wrap-vector.hpp"
#include "eng-malloc.hpp"
#include "luastack.hpp"
2021-01-23 16:10:29 -05:00
#include "luaconsole.hpp"
2021-02-10 16:22:24 -05:00
#include "util.hpp"
#include <cstring>
2021-10-07 14:58:20 -04:00
#include <iostream>
2021-02-10 16:22:24 -05:00
2021-01-23 16:10:29 -05:00
LuaConsole::LuaConsole() {
lua_state_ = LuaCoreStack::newstate(eng::l_alloc);
2021-11-04 14:49:25 -04:00
clear_raw_input();
2021-01-23 16:10:29 -05:00
}
LuaConsole::~LuaConsole() {
lua_close(lua_state_);
}
static LuaConsole::StringVec split_words(const eng::string &raw) {
2021-11-04 14:49:25 -04:00
LuaConsole::StringVec result;
eng::string acc;
2021-11-04 14:49:25 -04:00
for (char c : raw) {
2021-10-07 14:58:20 -04:00
if ((c == ' ')||(c == '\n')||(c == '\r')) {
2021-02-02 16:29:07 -05:00
if (!acc.empty()) {
2021-11-04 14:49:25 -04:00
result.push_back(acc);
2021-02-02 16:29:07 -05:00
acc = "";
}
} else {
acc += c;
}
}
if (!acc.empty()) {
2021-11-04 14:49:25 -04:00
result.push_back(acc);
2021-02-02 16:29:07 -05:00
}
2021-11-04 14:49:25 -04:00
return result;
2021-02-02 16:29:07 -05:00
}
2021-11-04 14:49:25 -04:00
LuaConsole::StringVec LuaConsole::get_command() {
StringVec result = words_;
words_.clear();
2021-10-07 14:58:20 -04:00
return result;
}
2021-11-04 14:49:25 -04:00
void LuaConsole::clear_raw_input() {
raw_input_ = "";
lines_ = 0;
prompt_ = "> ";
}
void LuaConsole::add(eng::string line) {
2021-01-23 16:10:29 -05:00
for (int i = 0; i < int(line.size()); i++) {
if (line[i] == '\n') line[i] = ' ';
}
2021-02-02 16:29:07 -05:00
raw_input_ += line;
raw_input_ += '\n';
2021-01-23 16:10:29 -05:00
lines_ += 1;
2021-11-04 14:49:25 -04:00
prompt_ = ">> ";
2021-11-16 12:20:11 -05:00
words_.clear();
2021-01-23 16:10:29 -05:00
// Try to interpret it as a slash-command.
if ((lines_ == 1)&&(raw_input_[0] == '/')) {
words_ = split_words(raw_input_.substr(1));
if ((words_.size() == 1) && (sv::valid_int64(words_[0]))) {
eng::string num = words_[0];
words_.clear();
words_.push_back("choose");
words_.push_back(num);
}
clear_raw_input();
return;
2021-01-23 16:10:29 -05:00
}
2021-02-07 15:35:31 -05:00
// Translate lua expression, deal with initial prefix.
eng::string partial;
eng::string lua_mode;
if (sv::has_prefix(raw_input_, "?=")) {
lua_mode = "luaprobe";
partial = eng::string("return ") + raw_input_.substr(2);
} else if (sv::has_prefix(raw_input_, "?")) {
lua_mode = "luaprobe";
partial = raw_input_.substr(1);
} else if (sv::has_prefix(raw_input_, "=")) {
lua_mode = "luainvoke";
partial = eng::string("return ") + raw_input_.substr(1);
2021-02-02 16:29:07 -05:00
} else {
lua_mode = "luainvoke";
2021-02-07 15:35:31 -05:00
partial = raw_input_;
2021-02-02 16:29:07 -05:00
}
2021-01-23 16:10:29 -05:00
2021-02-10 16:22:24 -05:00
// Try to parse the lua expression
2021-01-23 16:10:29 -05:00
int top = lua_gettop(lua_state_);
2021-02-02 16:29:07 -05:00
int status = luaL_loadbuffer(lua_state_, partial.c_str(), partial.size(), "=stdin");
2021-01-23 16:10:29 -05:00
if (status == LUA_ERRSYNTAX)
{
2021-07-12 00:37:07 -04:00
const char *eof = "<eof>";
int leof = strlen(eof);
2021-01-23 16:10:29 -05:00
size_t lmsg;
const char *msg = lua_tolstring(lua_state_, -1, &lmsg);
2021-07-12 00:37:07 -04:00
const char *tp = msg + lmsg - leof;
if (strstr(msg, eof) != tp) {
words_.push_back("syntax");
words_.push_back(msg);
2021-11-04 14:49:25 -04:00
clear_raw_input();
2021-01-23 16:10:29 -05:00
}
} else {
words_.push_back(lua_mode);
words_.emplace_back(sv::rtrim(partial));
2021-11-04 14:49:25 -04:00
clear_raw_input();
2021-01-23 16:10:29 -05:00
}
lua_settop(lua_state_, top);
}
2021-10-07 14:58:20 -04:00
void CommonCommands::do_command(const StringVec &words) {
if (words.size() == 0) {
return;
}
if (words[0] == "view") {
if (words.size() == 1) {
return do_view_command();
} else {
return do_syntax_error("/view takes no arguments");
}
}
if (words[0] == "moveto") {
if ((words.size() == 3) && sv::valid_int64(words[1]) && sv::valid_int64(words[2])) {
return do_moveto_command(sv::to_int64(words[1]), sv::to_int64(words[2]));
} else {
return do_syntax_error("/moveto [x] [y]");
}
}
if (words[0] == "quit") {
if (words.size() == 1) {
return do_quit_command();
} else {
return do_syntax_error("/quit takes no arguments");
}
}
if (words[0] == "cpl") {
if (words.size() == 1) {
return do_cpl_command();
} else {
return do_syntax_error("/cpl takes no arguments");
}
}
if (words[0] == "work") {
if (words.size() == 1) {
return do_work_command();
} else {
return do_syntax_error("/work takes no arguments");
}
}
if (words[0] == "display") {
if (words.size() == 1) {
return do_display_command();
} else {
return do_syntax_error("/display takes no arguments");
}
}
if (words[0] == "aborthttp") {
if (words.size() == 1) {
return do_aborthttp_command();
} else {
return do_syntax_error("/aborthttp takes no arguments");
}
}
if (words[0] == "connect") {
if ((words.size() == 2)&&(sv::valid_hostname(words[1]))) {
return do_connect_command(words[1]);
} else {
return do_syntax_error("/connect [hostname]");
}
}
if (words[0] == "luainvoke") {
if (words.size() == 2) {
return do_luainvoke_command(words[1]);
} else {
return do_syntax_error("/luainvoke [command]");
}
}
if (words[0] == "luaprobe") {
if (words.size() == 2) {
return do_luaprobe_command(words[1]);
} else {
return do_syntax_error("/luainvoke [command]");
}
}
return do_unknown_command(words[0]);
}