pmt: Improve, etc.

- Added custom file descriptors for better print status handling.
 - Added macro to be used for adding functions to make things easier.
 - Duplicate function protection added.
This commit is contained in:
2025-08-25 13:12:46 +03:00
parent 853d2c97b3
commit 80bcc0268d
3 changed files with 54 additions and 32 deletions

View File

@@ -51,7 +51,7 @@ private:
public: public:
void registerFunction(std::unique_ptr<basic_function> _func, CLI::App &_app); void registerFunction(std::unique_ptr<basic_function> _func, CLI::App &_app);
[[nodiscard]] bool isUsed(std::string name) const; [[nodiscard]] bool isUsed(const std::string &name) const;
[[nodiscard]] bool handleAll() const; [[nodiscard]] bool handleAll() const;
}; };
@@ -75,6 +75,7 @@ using VariableTable = basic_variables;
using Error = Helper::Error; using Error = Helper::Error;
extern std::unique_ptr<VariableTable> Variables; extern std::unique_ptr<VariableTable> Variables;
extern FILE *pstdout, *pstderr;
int Main(int argc, char **argv); int Main(int argc, char **argv);

View File

@@ -70,6 +70,12 @@ void processCommandLine(std::vector<std::string> &vec1,
void basic_function_manager::registerFunction( void basic_function_manager::registerFunction(
std::unique_ptr<basic_function> _func, CLI::App &_app) { std::unique_ptr<basic_function> _func, CLI::App &_app) {
LOGN(PMTF, INFO) << "registering function: " << _func->name() << std::endl; LOGN(PMTF, INFO) << "registering function: " << _func->name() << std::endl;
for (const auto& f : _functions) {
if (strcmp(f->name(), _func->name()) != 0) {
LOGN(PMTF, INFO) << "Function is already registered: " << _func->name() << ". Skipping." << std::endl;
return;
}
}
if (!_func->init(_app)) if (!_func->init(_app))
throw Error("Cannot init function: %s\n", _func->name()); throw Error("Cannot init function: %s\n", _func->name());
_functions.push_back(std::move(_func)); _functions.push_back(std::move(_func));
@@ -77,7 +83,7 @@ void basic_function_manager::registerFunction(
<< std::endl; << std::endl;
} }
bool basic_function_manager::isUsed(const std::string name) const { bool basic_function_manager::isUsed(const std::string &name) const {
if (_functions.empty()) return false; if (_functions.empty()) return false;
for (const auto &func : _functions) { for (const auto &func : _functions) {
if (func->name() == name) return func->isUsed(); if (func->name() == name) return func->isUsed();

View File

@@ -26,18 +26,9 @@
namespace PartitionManager { namespace PartitionManager {
__attribute__((constructor)) // Usage: REGISTER_FUNCTION(FUNCTION_CLASS);
void init() { #define REGISTER_FUNCTION(cls) \
Helper::LoggingProperties::setLogFile("/sdcard/Documents/last_pmt_logs.log"); FuncManager.registerFunction(std::make_unique<cls>(), AppMain)
}
static void sigHandler(const int sig) {
// Even if only SIGINT is to be captured for now, this is still a more appropriate code
if (sig == SIGINT) println("\n%sInterrupted.%s", YELLOW, STYLE_RESET);
exit(sig);
}
auto Variables = std::make_unique<VariableTable>();
basic_variables::basic_variables() basic_variables::basic_variables()
: logFile(Helper::LoggingProperties::FILE), onLogical(false), : logFile(Helper::LoggingProperties::FILE), onLogical(false),
@@ -49,9 +40,38 @@ basic_variables::basic_variables()
} }
} }
__attribute__((constructor))
void init() {
Helper::LoggingProperties::setLogFile("/sdcard/Documents/last_pmt_logs.log");
}
static void sigHandler(const int sig) {
// Even if only SIGINT is to be captured for now, this is still a more appropriate code
if (sig == SIGINT) println("\n%sInterrupted.%s", YELLOW, STYLE_RESET);
exit(sig);
}
static int write(void *cookie, const char *buf, const int size) {
auto *real = static_cast<FILE*>(cookie);
if (!Variables->quietProcess) return fwrite(buf, 1, static_cast<size_t>(size), real);
else return size;
}
static FILE* make_fp(FILE* real) {
return funopen(real, nullptr, write, nullptr, nullptr);
}
auto Variables = std::make_unique<VariableTable>();
FILE* pstdout = make_fp(stdout);
FILE* pstderr = make_fp(stderr);
int Main(int argc, char **argv) { int Main(int argc, char **argv) {
try { try {
// try-catch start // try-catch start
Helper::garbageCollector collector;
collector.closeAfterProgress(pstdout);
collector.closeAfterProgress(pstderr);
if (argc < 2) { if (argc < 2) {
println( println(
"Usage: %s [OPTIONS] [SUBCOMMAND]\nUse --help for more information.", "Usage: %s [OPTIONS] [SUBCOMMAND]\nUse --help for more information.",
@@ -93,17 +113,15 @@ int Main(int argc, char **argv) {
AppMain.add_flag("-v,--version", Variables->viewVersion, AppMain.add_flag("-v,--version", Variables->viewVersion,
"Print version and exit"); "Print version and exit");
FuncManager.registerFunction(std::make_unique<backupFunction>(), AppMain); REGISTER_FUNCTION(backupFunction);
FuncManager.registerFunction(std::make_unique<flashFunction>(), AppMain); REGISTER_FUNCTION(flashFunction);
FuncManager.registerFunction(std::make_unique<eraseFunction>(), AppMain); REGISTER_FUNCTION(eraseFunction);
FuncManager.registerFunction(std::make_unique<partitionSizeFunction>(), REGISTER_FUNCTION(partitionSizeFunction);
AppMain); REGISTER_FUNCTION(infoFunction);
FuncManager.registerFunction(std::make_unique<infoFunction>(), AppMain); REGISTER_FUNCTION(realPathFunction);
FuncManager.registerFunction(std::make_unique<realPathFunction>(), AppMain); REGISTER_FUNCTION(typeFunction);
FuncManager.registerFunction(std::make_unique<typeFunction>(), AppMain); REGISTER_FUNCTION(rebootFunction);
FuncManager.registerFunction(std::make_unique<rebootFunction>(), AppMain); REGISTER_FUNCTION(memoryTestFunction);
FuncManager.registerFunction(std::make_unique<memoryTestFunction>(),
AppMain);
CLI11_PARSE(AppMain, argc, argv); CLI11_PARSE(AppMain, argc, argv);
@@ -132,8 +150,7 @@ int Main(int argc, char **argv) {
} catch (Helper::Error &error) { } catch (Helper::Error &error) {
// catch Helper::Error // catch Helper::Error
if (!Variables->quietProcess) fprintf(pstderr, "%s%sERROR(S) OCCURRED:%s\n%s", RED, BOLD, STYLE_RESET,
fprintf(stderr, "%s%sERROR(S) OCCURRED:%s\n%s", RED, BOLD, STYLE_RESET,
error.what()); error.what());
return EXIT_FAILURE; return EXIT_FAILURE;
} catch (CLI::Error &error) { } catch (CLI::Error &error) {
@@ -148,17 +165,15 @@ int Main(int argc, char **argv) {
void print(const char *format, ...) { void print(const char *format, ...) {
va_list args; va_list args;
va_start(args, format); va_start(args, format);
if (!Variables->quietProcess) vfprintf(stdout, format, args); vfprintf(pstdout, format, args);
va_end(args); va_end(args);
} }
void println(const char *format, ...) { void println(const char *format, ...) {
va_list args; va_list args;
va_start(args, format); va_start(args, format);
if (!Variables->quietProcess) { vfprintf(pstdout, format, args);
vfprintf(stdout, format, args); print("\n");
print("\n");
}
va_end(args); va_end(args);
} }