From 0f6417f232e92aad0d712ab1b131c0829869cddd Mon Sep 17 00:00:00 2001 From: Tom Cherry Date: Tue, 13 Feb 2018 15:25:29 -0800 Subject: [PATCH] Move ActionParser to its own file Bug: 36970783 Test: build Change-Id: Idd5b923e4789760bb9ef67c10982b2642bc6a31a --- init/Android.bp | 1 + init/action.cpp | 39 ------------------------- init/action.h | 16 ---------- init/action_parser.cpp | 66 ++++++++++++++++++++++++++++++++++++++++++ init/action_parser.h | 48 ++++++++++++++++++++++++++++++ init/init.cpp | 1 + init/init_test.cpp | 1 + 7 files changed, 117 insertions(+), 55 deletions(-) create mode 100644 init/action_parser.cpp create mode 100644 init/action_parser.h diff --git a/init/Android.bp b/init/Android.bp index 6c80ee6cf..e218d2aa6 100644 --- a/init/Android.bp +++ b/init/Android.bp @@ -93,6 +93,7 @@ cc_library_static { defaults: ["init_defaults"], srcs: [ "action.cpp", + "action_parser.cpp", "bootchart.cpp", "builtins.cpp", "capabilities.cpp", diff --git a/init/action.cpp b/init/action.cpp index ba03e66fe..3fdff43a6 100644 --- a/init/action.cpp +++ b/init/action.cpp @@ -379,44 +379,5 @@ void ActionManager::ClearQueue() { current_command_ = 0; } -Result ActionParser::ParseSection(std::vector&& args, - const std::string& filename, int line) { - std::vector triggers(args.begin() + 1, args.end()); - if (triggers.size() < 1) { - return Error() << "Actions must have a trigger"; - } - - Subcontext* action_subcontext = nullptr; - if (subcontexts_) { - for (auto& subcontext : *subcontexts_) { - if (StartsWith(filename, subcontext.path_prefix())) { - action_subcontext = &subcontext; - break; - } - } - } - - auto action = std::make_unique(false, action_subcontext, filename, line); - - if (auto result = action->InitTriggers(triggers); !result) { - return Error() << "InitTriggers() failed: " << result.error(); - } - - action_ = std::move(action); - return Success(); -} - -Result ActionParser::ParseLineSection(std::vector&& args, int line) { - return action_ ? action_->AddCommand(std::move(args), line) : Success(); -} - -Result ActionParser::EndSection() { - if (action_ && action_->NumCommands() > 0) { - action_manager_->AddAction(std::move(action_)); - } - - return Success(); -} - } // namespace init } // namespace android diff --git a/init/action.h b/init/action.h index 1bfc6c753..85630c4de 100644 --- a/init/action.h +++ b/init/action.h @@ -25,7 +25,6 @@ #include "builtins.h" #include "keyword_map.h" -#include "parser.h" #include "result.h" #include "subcontext.h" @@ -123,21 +122,6 @@ class ActionManager { std::size_t current_command_; }; -class ActionParser : public SectionParser { - public: - ActionParser(ActionManager* action_manager, std::vector* subcontexts) - : action_manager_(action_manager), subcontexts_(subcontexts), action_(nullptr) {} - Result ParseSection(std::vector&& args, const std::string& filename, - int line) override; - Result ParseLineSection(std::vector&& args, int line) override; - Result EndSection() override; - - private: - ActionManager* action_manager_; - std::vector* subcontexts_; - std::unique_ptr action_; -}; - } // namespace init } // namespace android diff --git a/init/action_parser.cpp b/init/action_parser.cpp new file mode 100644 index 000000000..fd085b82d --- /dev/null +++ b/init/action_parser.cpp @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "action_parser.h" + +#include + +using android::base::StartsWith; + +namespace android { +namespace init { + +Result ActionParser::ParseSection(std::vector&& args, + const std::string& filename, int line) { + std::vector triggers(args.begin() + 1, args.end()); + if (triggers.size() < 1) { + return Error() << "Actions must have a trigger"; + } + + Subcontext* action_subcontext = nullptr; + if (subcontexts_) { + for (auto& subcontext : *subcontexts_) { + if (StartsWith(filename, subcontext.path_prefix())) { + action_subcontext = &subcontext; + break; + } + } + } + + auto action = std::make_unique(false, action_subcontext, filename, line); + + if (auto result = action->InitTriggers(triggers); !result) { + return Error() << "InitTriggers() failed: " << result.error(); + } + + action_ = std::move(action); + return Success(); +} + +Result ActionParser::ParseLineSection(std::vector&& args, int line) { + return action_ ? action_->AddCommand(std::move(args), line) : Success(); +} + +Result ActionParser::EndSection() { + if (action_ && action_->NumCommands() > 0) { + action_manager_->AddAction(std::move(action_)); + } + + return Success(); +} + +} // namespace init +} // namespace android diff --git a/init/action_parser.h b/init/action_parser.h new file mode 100644 index 000000000..32403b6a6 --- /dev/null +++ b/init/action_parser.h @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _INIT_ACTION_PARSER_H +#define _INIT_ACTION_PARSER_H + +#include +#include + +#include "action.h" +#include "parser.h" +#include "subcontext.h" + +namespace android { +namespace init { + +class ActionParser : public SectionParser { + public: + ActionParser(ActionManager* action_manager, std::vector* subcontexts) + : action_manager_(action_manager), subcontexts_(subcontexts), action_(nullptr) {} + Result ParseSection(std::vector&& args, const std::string& filename, + int line) override; + Result ParseLineSection(std::vector&& args, int line) override; + Result EndSection() override; + + private: + ActionManager* action_manager_; + std::vector* subcontexts_; + std::unique_ptr action_; +}; + +} // namespace init +} // namespace android + +#endif diff --git a/init/init.cpp b/init/init.cpp index bd09e4bcc..7e4eaa8c2 100644 --- a/init/init.cpp +++ b/init/init.cpp @@ -45,6 +45,7 @@ #include #include +#include "action_parser.h" #include "import_parser.h" #include "init_first_stage.h" #include "keychords.h" diff --git a/init/init_test.cpp b/init/init_test.cpp index 268873c12..d78aabd16 100644 --- a/init/init_test.cpp +++ b/init/init_test.cpp @@ -21,6 +21,7 @@ #include #include "action.h" +#include "action_parser.h" #include "builtins.h" #include "import_parser.h" #include "keyword_map.h"