diff options
author | A. Wilcox <AWilcox@Wilcox-Tech.com> | 2019-10-12 09:28:24 -0500 |
---|---|---|
committer | A. Wilcox <AWilcox@Wilcox-Tech.com> | 2019-10-12 09:28:24 -0500 |
commit | 90397f6a91e3e960609952486707ec76b332465e (patch) | |
tree | 228802ee51b5c3580e9c486499444ac0c6894c7d /tools/hscript-simulate | |
parent | 6006326ea9fa14c7865ca255241cb9ec9788b9cd (diff) | |
download | horizon-90397f6a91e3e960609952486707ec76b332465e.tar.gz horizon-90397f6a91e3e960609952486707ec76b332465e.tar.bz2 horizon-90397f6a91e3e960609952486707ec76b332465e.tar.xz horizon-90397f6a91e3e960609952486707ec76b332465e.zip |
tools: Add new tool, hscript-simulate, for simulation of script execution
Diffstat (limited to 'tools/hscript-simulate')
-rw-r--r-- | tools/hscript-simulate/CMakeLists.txt | 7 | ||||
-rw-r--r-- | tools/hscript-simulate/simulator.cc | 70 |
2 files changed, 77 insertions, 0 deletions
diff --git a/tools/hscript-simulate/CMakeLists.txt b/tools/hscript-simulate/CMakeLists.txt new file mode 100644 index 0000000..a8dfda8 --- /dev/null +++ b/tools/hscript-simulate/CMakeLists.txt @@ -0,0 +1,7 @@ +set(SIMULATOR_SRCS + simulator.cc +) +add_executable(hscript-simulate ${SIMULATOR_SRCS}) +target_link_libraries(hscript-simulate hscript) + +install(TARGETS hscript-simulate DESTINATION bin) diff --git a/tools/hscript-simulate/simulator.cc b/tools/hscript-simulate/simulator.cc new file mode 100644 index 0000000..0342894 --- /dev/null +++ b/tools/hscript-simulate/simulator.cc @@ -0,0 +1,70 @@ +/* + * simulator.cc - Implementation of the HorizonScript simulation utility + * Project Horizon + * + * Copyright (c) 2019 Adélie Linux and contributors. All rights reserved. + * This code is licensed under the AGPL 3.0 license, as noted in the + * LICENSE-code file in the root directory of this repository. + * + * SPDX-License-Identifier: AGPL-3.0-only + */ + +#include <unistd.h> +#include "hscript/script.hh" +#include "util/output.hh" +#include "3rdparty/clipp.h" + +bool pretty = false; + +int main(int argc, char *argv[]) { + const Horizon::Script *my_script; + Horizon::ScriptOptions opts; + int result_code = EXIT_SUCCESS; + std::string installfile; + using Horizon::ScriptOptionFlags; + + /* Default to pretty if we are using a TTY, unless -n specified. */ + if(isatty(1) && isatty(2)) { + pretty = true; + } + + opts.set(ScriptOptionFlags::Simulate); + + auto cli = ( + clipp::value("installfile", installfile), + clipp::option("-n", "--no-colour").doc("Do not 'prettify' output")( + [] { pretty = false; } + ), + clipp::option("-s", "--strict").doc("Strict parsing")( + [&opts] { opts.set(ScriptOptionFlags::StrictMode); } + ) + ); + if(!clipp::parse(argc, argv, cli)) { + std::cout << "usage:" << std::endl; + std::cout << clipp::usage_lines(cli, "hscript-simulate") << std::endl; + return EXIT_FAILURE; + } + + bold_if_pretty(std::cout); + std::cout << "HorizonScript Simulation Utility version 0.1.0"; + reset_if_pretty(std::cout); + std::cout << std::endl; + std::cout << "Copyright (c) 2019 Adélie Linux and contributors. AGPL-3.0 license." << std::endl; + std::cout << std::endl; + + my_script = Horizon::Script::load(installfile, opts); + if(my_script == nullptr) { + std::cout << "Could not load the specified script." << std::endl; + return EXIT_FAILURE; + } + + if(!my_script->execute()) { + output_error("installfile", "Script failed. Stop.", ""); + result_code = EXIT_FAILURE; + } else { + output_info("installfile", "Script completed.", ""); + } + + delete my_script; + return result_code; +} |