diff options
-rw-r--r-- | hscript/script.cc | 23 | ||||
-rw-r--r-- | hscript/user.cc | 3 | ||||
-rw-r--r-- | tools/hscript-validate/validator.cc | 14 | ||||
-rw-r--r-- | util/output.hh | 42 |
4 files changed, 37 insertions, 45 deletions
diff --git a/hscript/script.cc b/hscript/script.cc index 4d5a730..d8510b6 100644 --- a/hscript/script.cc +++ b/hscript/script.cc @@ -93,8 +93,7 @@ struct Script::ScriptPrivate { err_str += " at installfile:" + std::to_string(OBJ->lineno());\ if(errors) *errors += 1;\ output_error("installfile:" + std::to_string(lineno),\ - "duplicate value for key '" + KEY + "'", err_str,\ - opts.test(Pretty)); + "duplicate value for key '" + KEY + "'", err_str); if(key_name == "network") { if(this->network) { @@ -125,7 +124,7 @@ struct Script::ScriptPrivate { if(warnings) *warnings += 1; output_warning("installfile:" + std::to_string(lineno), "package '" + pkg + "' has already been specified", - "", opts.test(Pretty)); + ""); continue; } packages.insert(pkg); @@ -161,8 +160,7 @@ Script::Script() { const Script *Script::load(const std::string path, const ScriptOptions opts) { std::ifstream file(path); if(!file) { - output_error(path, "Cannot open installfile", "", - (opts.test(Pretty))); + output_error(path, "Cannot open installfile", ""); return nullptr; } @@ -172,7 +170,7 @@ const Script *Script::load(const std::string path, const ScriptOptions opts) { #define PARSER_ERROR(err_str) \ errors++;\ output_error("installfile:" + std::to_string(lineno),\ - err_str, "", (opts.test(Pretty)));\ + err_str, "");\ if(!opts.test(ScriptOptionFlags::KeepGoing)) {\ break;\ } @@ -180,7 +178,7 @@ const Script *Script::load(const std::string path, const ScriptOptions opts) { #define PARSER_WARNING(warn_str) \ warnings++;\ output_warning("installfile:" + std::to_string(lineno),\ - warn_str, "", (opts.test(Pretty))); + warn_str, ""); const Script *Script::load(std::istream &sstream, const ScriptOptions opts) { using namespace Horizon::Keys; @@ -246,15 +244,13 @@ const Script *Script::load(std::istream &sstream, const ScriptOptions opts) { if(sstream.fail() && !sstream.eof()) { output_error("installfile:" + std::to_string(lineno + 1), "line exceeds maximum length", - "Maximum line length is " + std::to_string(LINE_MAX), - (opts.test(Pretty))); + "Maximum line length is " + std::to_string(LINE_MAX)); errors++; } if(sstream.bad() && !sstream.eof()) { output_error("installfile:" + std::to_string(lineno), - "I/O error while reading installfile", "", - (opts.test(Pretty))); + "I/O error while reading installfile", ""); errors++; } @@ -262,7 +258,7 @@ const Script *Script::load(std::istream &sstream, const ScriptOptions opts) { #define MISSING_ERROR(key) \ output_error("installfile:" + std::to_string(lineno),\ "expected value for key '" + std::string(key) + "'",\ - "this key is required", (opts.test(Pretty)));\ + "this key is required");\ errors++; if(!the_script->internal->network) { @@ -284,8 +280,7 @@ const Script *Script::load(std::istream &sstream, const ScriptOptions opts) { output_message("parser", "0", "installfile", std::to_string(errors) + " error(s), " + - std::to_string(warnings) + " warning(s).", - "", opts.test(Pretty)); + std::to_string(warnings) + " warning(s).", ""); if(errors > 0) { delete the_script; diff --git a/hscript/user.cc b/hscript/user.cc index 12d01fc..ec4a213 100644 --- a/hscript/user.cc +++ b/hscript/user.cc @@ -17,7 +17,8 @@ using namespace Horizon::Keys; Key *RootPassphrase::parseFromData(const std::string data, int lineno, int *errors, int *warnings) { - if(data.size() < 5 || data[0] != '$' || data[2] != '$') { + if(data.size() < 5 || data[0] != '$' || (data[1] != '2' && data[1] != '6') + || data[2] != '$') { if(errors) *errors += 1; output_error("installfile:" + std::to_string(lineno), "rootpw: value is not a crypt-style encrypted passphrase"); diff --git a/tools/hscript-validate/validator.cc b/tools/hscript-validate/validator.cc index cdcb43d..cfaf38d 100644 --- a/tools/hscript-validate/validator.cc +++ b/tools/hscript-validate/validator.cc @@ -14,6 +14,8 @@ #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; @@ -23,7 +25,7 @@ int main(int argc, char *argv[]) { /* Default to pretty if we are using a TTY, unless -n specified. */ if(isatty(1) && isatty(2)) { - opts.set(Horizon::ScriptOptionFlags::Pretty); + pretty = true; } auto cli = ( @@ -32,7 +34,7 @@ int main(int argc, char *argv[]) { [&opts] { opts.set(ScriptOptionFlags::KeepGoing); } ), clipp::option("-n", "--no-colour").doc("Do not 'prettify' output")( - [&opts] { opts.reset(ScriptOptionFlags::Pretty); } + [] { pretty = false; } ), clipp::option("-s", "--strict").doc("Strict parsing")( [&opts] { opts.set(ScriptOptionFlags::StrictMode); } @@ -44,9 +46,9 @@ int main(int argc, char *argv[]) { return EXIT_FAILURE; } - bold_if_pretty(opts.test(ScriptOptionFlags::Pretty), std::cout); + bold_if_pretty(std::cout); std::cout << "HorizonScript Validation Utility version 0.1.0"; - reset_if_pretty(opts.test(ScriptOptionFlags::Pretty), std::cout); + 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; @@ -58,10 +60,10 @@ int main(int argc, char *argv[]) { } if(!my_script->validate()) { - output_error("installfile", "Script failed validation step. Stop.", "", true); + output_error("installfile", "Script failed validation step. Stop.", ""); result_code = EXIT_FAILURE; } else { - output_info("installfile", "Script passed validation.", "", true); + output_info("installfile", "Script passed validation.", ""); } delete my_script; diff --git a/util/output.hh b/util/output.hh index fa81127..cbb1d05 100644 --- a/util/output.hh +++ b/util/output.hh @@ -13,32 +13,30 @@ #ifndef __HORIZON_OUTPUT_HH_ #define __HORIZON_OUTPUT_HH_ +extern bool pretty; + #include <string> #include <iostream> -/*! Bolds output on +stream+ if it's a TTY. - * @param pretty Whether to act. +/*! Bolds output on +stream+ if pretty output is desired. * @param stream The stream to turn bold. */ -inline void bold_if_pretty(bool pretty, std::ostream &stream) { +inline void bold_if_pretty(std::ostream &stream) { if(pretty) stream << "\033[0;1m"; } -/*! ANSI colour code on +stream+ if +pretty+. - * @param pretty Whether to act. +/*! ANSI colour code on +stream+ if pretty output is desired. * @param stream The stream on which to colourise output. * @param what The colour code. */ -inline void colour_if_pretty(bool pretty, std::ostream &stream, - std::string what) { +inline void colour_if_pretty(std::ostream &stream, std::string what) { if(pretty) stream << "\033[" + what + ";1m"; } -/*! Reset all formatting on +stream+ if +pretty+. - * @param pretty Whether to act. +/*! Reset all formatting on +stream+ if pretty output is desired. * @param stream The stream on which to reset formatting. */ -inline void reset_if_pretty(bool pretty, std::ostream &stream) { +inline void reset_if_pretty(std::ostream &stream) { if(pretty) stream << "\033[0m"; } @@ -48,17 +46,16 @@ inline void reset_if_pretty(bool pretty, std::ostream &stream) { * @param where The location that triggered the message. * @param message The message. * @param detail Additional detail for the message, if available. - * @param pretty Whether or not to colourise (interactive output). */ inline void output_message(std::string type, std::string colour, std::string where, std::string message, - std::string detail = "", bool pretty = false) { + std::string detail = "") { std::cerr << where << ": "; - colour_if_pretty(pretty, std::cerr, colour); + colour_if_pretty(std::cerr, colour); std::cerr << type << ": "; - bold_if_pretty(pretty, std::cerr); + bold_if_pretty(std::cerr); std::cerr << message; - reset_if_pretty(pretty, std::cerr); + reset_if_pretty(std::cerr); if(!detail.empty()) { std::cerr << ": " << detail; } @@ -69,33 +66,30 @@ inline void output_message(std::string type, std::string colour, * @param where The location where the error occurred. * @param message The error that occurred. * @param detail Additional detail for the error, if available. - * @param pretty Whether or not to colourise (interactive output). */ inline void output_error(std::string where, std::string message, - std::string detail = "", bool pretty = false) { - output_message("error", "31", where, message, detail, pretty); + std::string detail = "") { + output_message("error", "31", where, message, detail); } /*! Outputs a warning message. * @param where The location where the warning occurred. * @param message The warning to display. * @param detail Additional detail for the warning, if available. - * @param pretty Whether or not to colourise (interactive output). */ inline void output_warning(std::string where, std::string message, - std::string detail = "", bool pretty = false) { - output_message("warning", "33", where, message, detail, pretty); + std::string detail = "") { + output_message("warning", "33", where, message, detail); } /*! Outputs an informational message. * @param where The location relevant to the information. * @param message The information to display. * @param detail Additional detail for the information, if available. - * @param pretty Whether or not to colourise (interactive output). */ inline void output_info(std::string where, std::string message, - std::string detail = "", bool pretty = false) { - output_message("info", "36", where, message, detail, pretty); + std::string detail = "") { + output_message("info", "36", where, message, detail); } #endif /* !__HORIZON_OUTPUT_HH_ */ |