Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 30a6f276 authored by Tom Cherry's avatar Tom Cherry
Browse files

init: clean up the SectionParser interface and Parser class

Remove the dependency on Action and Service from what should be a
generic Parser class.

Make ActionParser, ImportParser, and ServiceParser take a pointer to
their associated classes instead of accessing them through a
singleton.

Misc fixes to SectionParser Interface:
1) Make SectionParser::ParseLineSection() non-const as it always should
have been.
2) Use Rvalue references where appropriate
3) Remove extra std::string& filename in SectionParser::EndFile()
4) Only have SectionParser::ParseSection() as pure virtual

Document SectionParser.

Make ImportParser report the filename and line number of failed imports.

Make ServiceParser report the filename and line number of duplicated services.

Test: Boot bullhead

Change-Id: I86568a5b375fb4f27f4cb235ed1e37635f01d630
parent a0bf415c
Loading
Loading
Loading
Loading
+4 −5
Original line number Diff line number Diff line
@@ -363,7 +363,7 @@ void ActionManager::DumpState() const {
    }
}

bool ActionParser::ParseSection(const std::vector<std::string>& args, const std::string& filename,
bool ActionParser::ParseSection(std::vector<std::string>&& args, const std::string& filename,
                                int line, std::string* err) {
    std::vector<std::string> triggers(args.begin() + 1, args.end());
    if (triggers.size() < 1) {
@@ -380,13 +380,12 @@ bool ActionParser::ParseSection(const std::vector<std::string>& args, const std:
    return true;
}

bool ActionParser::ParseLineSection(const std::vector<std::string>& args, int line,
                                    std::string* err) {
    return action_ ? action_->AddCommand(args, line, err) : false;
bool ActionParser::ParseLineSection(std::vector<std::string>&& args, int line, std::string* err) {
    return action_ ? action_->AddCommand(std::move(args), line, err) : false;
}

void ActionParser::EndSection() {
    if (action_ && action_->NumCommands() > 0) {
        ActionManager::GetInstance().AddAction(std::move(action_));
        action_manager_->AddAction(std::move(action_));
    }
}
+5 −6
Original line number Diff line number Diff line
@@ -114,16 +114,15 @@ private:

class ActionParser : public SectionParser {
  public:
    ActionParser() : action_(nullptr) {
    }
    bool ParseSection(const std::vector<std::string>& args, const std::string& filename, int line,
    ActionParser(ActionManager* action_manager)
        : action_manager_(action_manager), action_(nullptr) {}
    bool ParseSection(std::vector<std::string>&& args, const std::string& filename, int line,
                      std::string* err) override;
    bool ParseLineSection(const std::vector<std::string>& args, int line, std::string* err) override;
    bool ParseLineSection(std::vector<std::string>&& args, int line, std::string* err) override;
    void EndSection() override;
    void EndFile(const std::string&) override {
    }

  private:
    ActionManager* action_manager_;
    std::unique_ptr<Action> action_;
};

+1 −1
Original line number Diff line number Diff line
@@ -390,7 +390,7 @@ static void import_late(const std::vector<std::string>& args, size_t start_index

    // Turning this on and letting the INFO logging be discarded adds 0.2s to
    // Nexus 9 boot time, so it's disabled by default.
    if (false) parser.DumpState();
    if (false) DumpState();
}

/* mount_fstab
+8 −6
Original line number Diff line number Diff line
@@ -20,7 +20,7 @@

#include "util.h"

bool ImportParser::ParseSection(const std::vector<std::string>& args, const std::string& filename,
bool ImportParser::ParseSection(std::vector<std::string>&& args, const std::string& filename,
                                int line, std::string* err) {
    if (args.size() != 2) {
        *err = "single argument needed for import\n";
@@ -35,16 +35,18 @@ bool ImportParser::ParseSection(const std::vector<std::string>& args, const std:
    }

    LOG(INFO) << "Added '" << conf_file << "' to import list";
    imports_.emplace_back(std::move(conf_file));
    if (filename_.empty()) filename_ = filename;
    imports_.emplace_back(std::move(conf_file), line);
    return true;
}

void ImportParser::EndFile(const std::string& filename) {
void ImportParser::EndFile() {
    auto current_imports = std::move(imports_);
    imports_.clear();
    for (const auto& s : current_imports) {
        if (!Parser::GetInstance().ParseConfig(s)) {
            PLOG(ERROR) << "could not import file '" << s << "' from '" << filename << "'";
    for (const auto& [import, line_num] : current_imports) {
        if (!parser_->ParseConfig(import)) {
            PLOG(ERROR) << filename_ << ": " << line_num << ": Could not import file '" << import
                        << "'";
        }
    }
}
+8 −11
Original line number Diff line number Diff line
@@ -24,20 +24,17 @@

class ImportParser : public SectionParser {
  public:
    ImportParser()  {
    }
    bool ParseSection(const std::vector<std::string>& args, const std::string& filename, int line,
    ImportParser(Parser* parser) : parser_(parser) {}
    bool ParseSection(std::vector<std::string>&& args, const std::string& filename, int line,
                      std::string* err) override;
    bool ParseLineSection(const std::vector<std::string>& args, int line,
                          std::string* err) override {
        return true;
    }
    void EndSection() override {
    }
    void EndFile(const std::string& filename) override;
    void EndFile() override;

  private:
    std::vector<std::string> imports_;
    Parser* parser_;
    // Store filename for later error reporting.
    std::string filename_;
    // Vector of imports and their line numbers for later error reporting.
    std::vector<std::pair<std::string, int>> imports_;
};

#endif
Loading