libcmo21/Unvirt/CmdHelper.cpp

449 lines
11 KiB
C++
Raw Normal View History

2023-02-13 10:57:10 +08:00
#include "CmdHelper.hpp"
2023-02-13 16:52:00 +08:00
#include "TerminalHelper.hpp"
2023-03-03 16:05:32 +08:00
#include "StructFormatter.hpp"
#include "AccessibleValue.hpp"
2023-02-14 16:28:37 +08:00
2023-03-03 11:06:26 +08:00
#include <CKMinContext.hpp>
#include <CKFile.hpp>
#include <iostream>
#include <cstdio>
#include <cstdarg>
2023-03-04 11:11:36 +08:00
#include <functional>
2023-03-03 16:05:32 +08:00
2023-03-03 11:06:26 +08:00
namespace Unvirt::CmdHelper {
2023-02-13 10:57:10 +08:00
2023-03-04 11:11:36 +08:00
static FILE* fout = stdout;
2023-02-13 22:13:30 +08:00
#pragma region CmdSplitter
2023-02-13 10:57:10 +08:00
2023-03-03 11:06:26 +08:00
CmdSplitter::CmdSplitter() :
mCmdChar(0), mBuffer(nullptr), mResult(nullptr),
mState(StateType::NORMAL), mPreState(StateType::NORMAL) {
;
}
CmdSplitter::~CmdSplitter() {
;
}
2023-02-13 10:57:10 +08:00
2023-03-03 11:06:26 +08:00
const std::deque<std::string> CmdSplitter::Convert(const std::string& u8cmd) {
// set up variables
std::deque<std::string> result;
std::string buffer;
mBuffer = &buffer;
mResult = &result;
mState = mPreState = StateType::SPACE;
2023-02-13 10:57:10 +08:00
2023-03-03 11:06:26 +08:00
// split
for (auto it = u8cmd.begin(); it != u8cmd.end(); ++it) {
mCmdChar = (*it);
2023-03-04 11:11:36 +08:00
// skip all invalid characters, \0 and etc.
// mCmdChar >= 0 to ensure all non-ASCII UTF8 char can be accepted directly.
if (mCmdChar >= 0 && (!std::isprint(mCmdChar)))
continue;
2023-02-13 10:57:10 +08:00
switch (mState) {
case StateType::SPACE:
2023-03-03 11:06:26 +08:00
ProcSpace();
2023-02-13 10:57:10 +08:00
break;
case StateType::SINGLE:
2023-03-03 11:06:26 +08:00
ProcSingle();
break;
2023-02-13 10:57:10 +08:00
case StateType::DOUBLE:
2023-03-03 11:06:26 +08:00
ProcDouble();
break;
2023-02-13 10:57:10 +08:00
case StateType::ESCAPE:
2023-03-03 11:06:26 +08:00
ProcEscape();
break;
case StateType::NORMAL:
ProcNormal();
2023-02-13 10:57:10 +08:00
break;
}
2023-03-03 11:06:26 +08:00
}
// final proc
switch (mState) {
case StateType::SPACE:
break;
case StateType::NORMAL:
// push the last one
mResult->push_back(*mBuffer);
break;
case StateType::SINGLE:
case StateType::DOUBLE:
case StateType::ESCAPE:
// error
result.clear();
break;
}
// return value
return result;
}
2023-02-13 10:57:10 +08:00
2023-03-03 11:06:26 +08:00
#pragma endregion
#pragma region ArgParser
2023-03-03 16:05:32 +08:00
bool ArgParser::ParseInt(const std::deque<std::string>& cmd, const size_t expected_index, int32_t& result) {
2023-03-03 11:06:26 +08:00
if (expected_index >= cmd.size()) {
result = 0;
return false;
2023-02-13 10:57:10 +08:00
}
2023-03-03 11:06:26 +08:00
char* pend = nullptr;
errno = 0;
int64_t v = std::strtoll(cmd[expected_index].c_str(), &pend, 10);
if (pend == cmd[expected_index].c_str() || errno == ERANGE) {
result = 0;
return false;
}
result = static_cast<int>(v);
return true;
}
2023-03-03 16:05:32 +08:00
bool ArgParser::ParseString(const std::deque<std::string>& cmd, const size_t expected_index, std::string& result) {
2023-03-03 11:06:26 +08:00
if (expected_index >= cmd.size()) {
result.clear();
return false;
} else {
result = cmd[expected_index];
return true;
}
}
2023-03-03 16:05:32 +08:00
bool ArgParser::ParseSwitch(const std::deque<std::string>& cmd, const size_t expected_index, const std::vector<std::string>& switches, std::string& gotten) {
2023-03-03 11:06:26 +08:00
if (expected_index >= cmd.size()) {
gotten.clear();
return false;
}
for (const auto& sw : switches) {
if (cmd[expected_index] == sw) {
gotten = cmd[expected_index];
return true;
}
}
gotten.clear();
return false;
}
2023-02-13 10:57:10 +08:00
#pragma endregion
2023-03-04 11:11:36 +08:00
#pragma region InteractiveCmd Misc
2023-03-03 11:06:26 +08:00
InteractiveCmd::InteractiveCmd() :
2023-03-03 16:05:32 +08:00
m_CmdSplitter(), m_PageLen(10),
m_Ctx(nullptr), m_File(nullptr), m_Doc(nullptr) {
2023-03-03 11:06:26 +08:00
2023-03-04 11:11:36 +08:00
// create context and file
m_Ctx = new LibCmo::CK2::CKMinContext();
m_File = new LibCmo::CK2::CKFile(m_Ctx);
// bind callback
m_Ctx->SetPrintCallback(std::bind(&InteractiveCmd::PrintMinContextMsg, this, std::placeholders::_1));
2023-03-03 11:06:26 +08:00
}
InteractiveCmd::~InteractiveCmd() {
2023-03-04 11:11:36 +08:00
// delete doc if necessary
2023-03-03 11:06:26 +08:00
if (m_Doc != nullptr) delete m_Doc;
2023-03-04 11:11:36 +08:00
// delete file and ctx
delete m_File;
delete m_Ctx;
}
bool InteractiveCmd::HasOpenedFile(void) {
return m_Doc != nullptr;
}
void InteractiveCmd::ClearDocument(void) {
if (m_Doc == nullptr) return;
// clear doc
delete m_Doc;
m_Doc = nullptr;
// clear all loaded objects
m_Ctx->ClearCKObject();
2023-03-03 11:06:26 +08:00
}
2023-03-04 11:11:36 +08:00
void InteractiveCmd::PrintMinContextMsg(const std::string& msg) {
fputs(UNVIRT_TERMCOL_LIGHT_YELLOW(("[CKMinContext] ")), fout);
fputs(msg.c_str(), fout);
fputc('\n', fout);
}
#pragma endregion
#pragma region InteractiveCmd Dispatch
2023-03-03 11:06:26 +08:00
void InteractiveCmd::Run(void) {
std::string u8cmd;
2023-03-03 16:05:32 +08:00
while (true) {
2023-03-03 11:06:26 +08:00
// get command
GetCmdLine(u8cmd);
// split cmd and parse it
auto cmds = m_CmdSplitter.Convert(u8cmd);
// get sub command
if (cmds.size() < 1u) {
this->PrintCommonError("No command specified!");
this->PrintHelp();
continue;
}
std::string subcmd(cmds.front());
cmds.pop_front();
// dispatch command
2023-03-03 16:05:32 +08:00
if (subcmd == "load") this->ProcLoad(cmds);
else if (subcmd == "unload") this->ProcUnLoad(cmds);
else if (subcmd == "info") this->ProcInfo(cmds);
else if (subcmd == "ls") this->ProcLs(cmds);
2023-03-04 00:13:03 +08:00
else if (subcmd == "items") this->ProcItems(cmds);
2023-03-04 11:11:36 +08:00
else if (subcmd == "encoding") this->ProcEncoding(cmds);
else if (subcmd == "temp") this->ProcTemp(cmds);
2023-03-03 16:05:32 +08:00
else if (subcmd == "help") this->PrintHelp();
else if (subcmd == "exit") break;
2023-03-03 11:06:26 +08:00
else {
2023-03-04 00:13:03 +08:00
this->PrintCommonError("No such command \"%s\".", subcmd.c_str());
2023-03-03 11:06:26 +08:00
this->PrintHelp();
}
2023-03-04 00:13:03 +08:00
2023-03-03 11:06:26 +08:00
}
}
void InteractiveCmd::GetCmdLine(std::string& u8cmd) {
2023-03-04 11:11:36 +08:00
fputs("Unvirt> ", fout);
2023-03-03 11:06:26 +08:00
#if defined(LIBCMO_OS_WIN32)
std::wstring wcmd;
std::getline(std::wcin, wcmd);
LibCmo::EncodingHelper::WcharToChar(wcmd, u8cmd, CP_UTF8);
#else
std::getline(std::cin, u8cmd);
#endif
}
void InteractiveCmd::PrintHelp(void) {
2023-03-04 11:11:36 +08:00
fputs(UNVIRT_TERMCOL_LIGHT_YELLOW(("Allowed Subcommands: \n")), fout);
2023-03-03 16:05:32 +08:00
2023-03-04 11:11:36 +08:00
fputs("load\n", fout);
fputs("\tDescription: Load a Virtools composition.\n", fout);
fputs("\tSyntax: load <file path>\n", fout);
2023-03-03 16:05:32 +08:00
2023-03-04 11:11:36 +08:00
fputs("unload\n", fout);
fputs("\tDescription: Release loaded Virtools composition.\n", fout);
fputs("\tSyntax: unload\n", fout);
2023-03-03 16:05:32 +08:00
2023-03-04 11:11:36 +08:00
fputs("info\n", fout);
fputs("\tDescription: Show the header info of loaded Virtools composition.\n", fout);
fputs("\tSyntax: info\n", fout);
2023-03-03 16:05:32 +08:00
2023-03-04 11:11:36 +08:00
fputs("ls\n", fout);
fputs("\tDescription: List something about loaded Virtools composition.\n", fout);
fputs("\tSyntax: ls <obj | mgr> [page]\n", fout);
2023-03-03 16:05:32 +08:00
2023-03-04 11:11:36 +08:00
fputs("items\n", fout);
fputs("\tDescription: Set up how many items should be listed in one page when using \"ls\" command.\n", fout);
fputs("\tSyntax: items <num>\n", fout);
2023-03-03 16:05:32 +08:00
2023-03-04 11:11:36 +08:00
fputs("encoding\n", fout);
fputs("\tDescription: Set the encoding series for CKMinContext.\n", fout);
fputs("\tSyntax: encoding [encoding name1] [encoding name2] [encoding name3] ...\n", fout);
fputs("temp\n", fout);
fputs("\tDescription: Set the Temp path for CKMinContext.\n", fout);
fputs("\tSyntax: temp <temp path>\n", fout);
fputs("exit\n", fout);
fputs("\tDescription: Exit program\n", fout);
fputs("\tSyntax: exit\n", fout);
2023-03-03 16:05:32 +08:00
2023-03-03 11:06:26 +08:00
}
void InteractiveCmd::PrintArgParseError(const std::deque<std::string>& cmd, size_t pos) {
if (pos >= cmd.size()) {
2023-03-04 11:11:36 +08:00
fprintf(fout, UNVIRT_TERMCOL_LIGHT_RED(("Lost argument at position %zu.\n")), pos);
2023-03-03 11:06:26 +08:00
} else {
2023-03-04 11:11:36 +08:00
fprintf(fout, UNVIRT_TERMCOL_LIGHT_RED(("Unexpected argument \"%s\".\n")), cmd[pos].c_str());
2023-03-03 11:06:26 +08:00
}
// arg error always print help
this->PrintHelp();
}
void InteractiveCmd::PrintCommonError(const char* u8_fmt, ...) {
va_list argptr;
va_start(argptr, u8_fmt);
2023-03-04 11:11:36 +08:00
std::fputs(UNVIRT_TERMCOLHDR_LIGHT_RED, fout);
std::vfprintf(fout, u8_fmt, argptr);
std::fputs(UNVIRT_TERMCOLTAIL, fout);
2023-03-03 11:06:26 +08:00
va_end(argptr);
2023-03-04 11:11:36 +08:00
std::fputc('\n', fout);
2023-03-03 11:06:26 +08:00
}
#pragma endregion
2023-03-04 11:11:36 +08:00
#pragma region InteractiveCmd Processors
2023-03-03 11:06:26 +08:00
2023-03-03 16:05:32 +08:00
void InteractiveCmd::ProcLoad(const std::deque<std::string>& cmd) {
// check pre-requirement
if (HasOpenedFile()) {
this->PrintCommonError("Already have a opened file. Close it before calling \"load\".");
return;
}
// check requirement
size_t pos = 0u;
std::string filepath;
if (!ArgParser::ParseString(cmd, pos, filepath)) {
this->PrintArgParseError(cmd, pos);
return;
}
// proc
m_Doc = new LibCmo::CK2::CKFileDocument();
LibCmo::CK2::CKERROR err = m_File->DeepLoad(filepath.c_str(), &m_Doc);
if (err != LibCmo::CK2::CKERROR::CKERR_OK) {
// fail to load. release all.
this->PrintCommonError("Fail to open file. Function return: %s\n%s",
2023-03-04 00:13:03 +08:00
Unvirt::AccessibleValue::GetCkErrorName(err).c_str(),
Unvirt::AccessibleValue::GetCkErrorDescription(err).c_str()
2023-03-03 16:05:32 +08:00
);
2023-03-04 11:11:36 +08:00
this->ClearDocument();
2023-03-03 16:05:32 +08:00
}
2023-03-03 11:06:26 +08:00
}
2023-03-03 16:05:32 +08:00
void InteractiveCmd::ProcUnLoad(const std::deque<std::string>& cmd) {
// check pre-requirement
if (!HasOpenedFile()) {
this->PrintCommonError("No loaded file.");
return;
}
// free all
2023-03-04 11:11:36 +08:00
this->ClearDocument();
2023-03-03 11:06:26 +08:00
}
2023-03-03 16:05:32 +08:00
void InteractiveCmd::ProcInfo(const std::deque<std::string>& cmd) {
// check pre-requirement
if (!HasOpenedFile()) {
this->PrintCommonError("No loaded file.");
return;
}
// print
Unvirt::StructFormatter::PrintCKFileInfo(m_Doc->m_FileInfo);
2023-03-03 11:06:26 +08:00
}
2023-03-03 16:05:32 +08:00
void InteractiveCmd::ProcLs(const std::deque<std::string>& cmd) {
2023-03-04 00:13:03 +08:00
// static values of switches
2023-03-03 16:05:32 +08:00
static const std::vector<std::string> c_AllowedSwitches{
2023-03-03 11:06:26 +08:00
"obj", "mgr"
};
2023-03-04 00:13:03 +08:00
// check pre-requirement
if (!HasOpenedFile()) {
this->PrintCommonError("No loaded file.");
return;
}
// check requirement
size_t pos = 0u;
std::string sw;
if (!ArgParser::ParseSwitch(cmd, pos, c_AllowedSwitches, sw)) {
this->PrintArgParseError(cmd, pos);
return;
}
++pos;
int32_t gotten_page;
2023-03-04 11:11:36 +08:00
if (!ArgParser::ParseInt(cmd, pos, gotten_page)) {
gotten_page = 1; // asssume as 1
}
if (gotten_page <= 0) {
this->PrintCommonError("Page out of range.");
return;
2023-03-04 00:13:03 +08:00
}
2023-03-04 11:11:36 +08:00
size_t page = static_cast<size_t>(gotten_page) - 1;
2023-03-04 00:13:03 +08:00
// show list
if (sw == c_AllowedSwitches[0]) {
// obj list
if (page * this->m_PageLen >= m_Doc->m_FileObjects.size()) {
this->PrintCommonError("Page out of range.");
return;
}
Unvirt::StructFormatter::PrintObjectList(this->m_Doc->m_FileObjects, page, this->m_PageLen);
} else {
// mgr list
if (page * this->m_PageLen >= m_Doc->m_FileManagersData.size()) {
this->PrintCommonError("Page out of range.");
return;
}
Unvirt::StructFormatter::PrintManagerList(this->m_Doc->m_FileManagersData, page, this->m_PageLen);
}
2023-03-03 11:06:26 +08:00
}
2023-03-04 00:13:03 +08:00
void InteractiveCmd::ProcItems(const std::deque<std::string>& cmd) {
2023-03-03 16:05:32 +08:00
// check requirement
size_t pos = 0u;
int32_t count;
if (!ArgParser::ParseInt(cmd, pos, count) || count <= 0) {
this->PrintArgParseError(cmd, pos);
return;
}
// assign
m_PageLen = static_cast<size_t>(count);
}
2023-03-04 11:11:36 +08:00
void InteractiveCmd::ProcEncoding(const std::deque<std::string>& cmd) {
// create list first
std::vector<std::string> encoding_list;
2023-02-13 10:57:10 +08:00
2023-03-04 11:11:36 +08:00
// get list item
size_t pos = 0u;
while (true) {
std::string pending;
if (!ArgParser::ParseString(cmd, pos, pending)) {
break; // no more encoding, break
2023-02-14 16:28:37 +08:00
}
2023-02-13 10:57:10 +08:00
2023-03-04 11:11:36 +08:00
// add and move to next
++pos;
encoding_list.push_back(std::move(pending));
2023-02-13 10:57:10 +08:00
}
2023-03-04 11:11:36 +08:00
// apply list
this->m_Ctx->SetEncoding(encoding_list);
}
2023-02-13 22:13:30 +08:00
2023-03-04 11:11:36 +08:00
void InteractiveCmd::ProcTemp(const std::deque<std::string>& cmd) {
// check requirement
size_t pos = 0u;
std::string temppath;
if (!ArgParser::ParseString(cmd, pos, temppath)) {
this->PrintArgParseError(cmd, pos);
return;
2023-02-13 22:13:30 +08:00
}
2023-03-04 11:11:36 +08:00
// assign
m_Ctx->SetTempPath(temppath.c_str());
}
2023-02-13 10:57:10 +08:00
2023-02-14 16:28:37 +08:00
#pragma endregion
2023-02-13 10:57:10 +08:00
2023-03-03 11:06:26 +08:00
}