From 7c1efb0e68e7decd2d5276e7ff02f65bde9bbf9a Mon Sep 17 00:00:00 2001 From: "A. Wilcox" Date: Tue, 8 Oct 2019 21:19:13 -0500 Subject: hscript: Initial attempt at handling 'pkginstall' key --- hscript/meta.cc | 31 +++++++++++++++++++++++++++++-- hscript/meta.hh | 12 ++++++++++++ hscript/script.cc | 23 +++++++++++++++++------ 3 files changed, 58 insertions(+), 8 deletions(-) (limited to 'hscript') diff --git a/hscript/meta.cc b/hscript/meta.cc index 83c6269..f333f0b 100644 --- a/hscript/meta.cc +++ b/hscript/meta.cc @@ -19,8 +19,7 @@ using namespace Horizon::Keys; Key *Hostname::parseFromData(const std::string data, int lineno, int *errors, int *warnings) { std::regex valid_re("[A-Za-z0-9.]*"); - bool valid = std::regex_match(data, valid_re); - if(!valid) { + if(!std::regex_match(data, valid_re)) { if(errors) *errors += 1; output_error("installfile:" + std::to_string(lineno), "hostname: expected machine or DNS name", @@ -39,3 +38,31 @@ bool Hostname::execute() const { /* Write the hostname to /etc/hostname in the target environment. */ return false; } + + +Key *PkgInstall::parseFromData(const std::string data, int lineno, int *errors, + int *warnings) { + std::regex valid_pkg("[0-9A-Za-z_-]*((>?<|[<>]?=|[~>])[0-9A-Za-z-_.]+)?"); + std::string next_pkg; + std::istringstream stream(data); + std::set all_pkgs; + + while(stream >> next_pkg) { + if(!std::regex_match(next_pkg, valid_pkg)) { + if(errors) *errors += 1; + output_error("installfile:" + std::to_string(lineno), + "pkginstall: expected package name", + "'" + next_pkg + "' is not a valid package or atom"); + return nullptr; + } + if(all_pkgs.find(next_pkg) != all_pkgs.end()) { + if(warnings) *warnings += 1; + output_warning("installfile:" + std::to_string(lineno), + "pkginstall: package '" + next_pkg + + "' is already in the target package set"); + continue; + } + all_pkgs.insert(next_pkg); + } + return new PkgInstall(lineno, all_pkgs); +} diff --git a/hscript/meta.hh b/hscript/meta.hh index 626ce91..6515259 100644 --- a/hscript/meta.hh +++ b/hscript/meta.hh @@ -14,6 +14,7 @@ #define __HSCRIPT_META_HH_ #include +#include #include "key.hh" namespace Horizon { @@ -33,6 +34,17 @@ public: }; class PkgInstall : public Key { +private: + const std::set _pkgs; + PkgInstall(int _line, const std::set my_pkgs) : Key(_line), + _pkgs(my_pkgs) {} +public: + static Key *parseFromData(const std::string data, int lineno, int *errors, + int *warnings); + const std::set packages() const { return _pkgs; } + bool validate() const override { return true; } + bool execute() const override { return true; } + }; class Language : public Key { diff --git a/hscript/script.cc b/hscript/script.cc index 5ff7cb2..709fbaf 100644 --- a/hscript/script.cc +++ b/hscript/script.cc @@ -14,6 +14,7 @@ #include #include #include +#include #include "script.hh" #include "disk.hh" @@ -71,7 +72,7 @@ struct Script::ScriptPrivate { /*! The target system's hostname. */ std::unique_ptr hostname; /*! The packages to install to the target system. */ - std::vector packages; + std::set packages; /*! The root shadow line. */ std::unique_ptr rootpw; /*! Target system's mountpoints. */ @@ -82,16 +83,18 @@ struct Script::ScriptPrivate { * @param key_obj The Key object associated with the key. * @param errors Output parameter: if given, incremented on error. * @param warnings Output parameter: if given, incremented on warning. + * @param opts Script parsing options. */ bool store_key(const std::string key_name, Keys::Key *key_obj, int lineno, - int *errors, int *warnings) { + int *errors, int *warnings, ScriptOptions opts) { #define DUPLICATE_ERROR(OBJ, KEY, OLD_VAL) \ std::string err_str("previous value was ");\ err_str += OLD_VAL;\ 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); + "duplicate value for key '" + KEY + "'", err_str,\ + opts.test(Pretty)); if(key_name == "network") { if(this->network) { @@ -116,8 +119,16 @@ struct Script::ScriptPrivate { this->hostname = std::move(name); return true; } else if(key_name == "pkginstall") { - /*! TODO: implement */ - return false; + Keys::PkgInstall *install = dynamic_cast(key_obj); + for(auto &pkg : install->packages()) { + if(opts.test(StrictMode) && packages.find(pkg) != packages.end()) { + output_warning("installfile:" + std::to_string(lineno), + "package '" + pkg + "' has already been specified", + "", opts.test(Pretty)); + } + packages.insert(pkg); + } + return true; } else if(key_name == "rootpw") { /*! TODO: implement */ return false; @@ -215,7 +226,7 @@ const Script *Script::load(std::istream &sstream, const ScriptOptions opts) { } if(!the_script->internal->store_key(key, key_obj, lineno, &errors, - &warnings)) { + &warnings, opts)) { PARSER_ERROR("stopping due to prior errors") continue; } -- cgit v1.2.3-70-g09d2