Tom Cherry | 0f6417f | 2018-02-13 15:25:29 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "action_parser.h" |
| 18 | |
| 19 | #include <android-base/strings.h> |
| 20 | |
| 21 | using android::base::StartsWith; |
| 22 | |
| 23 | namespace android { |
| 24 | namespace init { |
| 25 | |
| 26 | Result<Success> ActionParser::ParseSection(std::vector<std::string>&& args, |
| 27 | const std::string& filename, int line) { |
| 28 | std::vector<std::string> triggers(args.begin() + 1, args.end()); |
| 29 | if (triggers.size() < 1) { |
| 30 | return Error() << "Actions must have a trigger"; |
| 31 | } |
| 32 | |
| 33 | Subcontext* action_subcontext = nullptr; |
| 34 | if (subcontexts_) { |
| 35 | for (auto& subcontext : *subcontexts_) { |
| 36 | if (StartsWith(filename, subcontext.path_prefix())) { |
| 37 | action_subcontext = &subcontext; |
| 38 | break; |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | auto action = std::make_unique<Action>(false, action_subcontext, filename, line); |
| 44 | |
| 45 | if (auto result = action->InitTriggers(triggers); !result) { |
| 46 | return Error() << "InitTriggers() failed: " << result.error(); |
| 47 | } |
| 48 | |
| 49 | action_ = std::move(action); |
| 50 | return Success(); |
| 51 | } |
| 52 | |
| 53 | Result<Success> ActionParser::ParseLineSection(std::vector<std::string>&& args, int line) { |
| 54 | return action_ ? action_->AddCommand(std::move(args), line) : Success(); |
| 55 | } |
| 56 | |
| 57 | Result<Success> ActionParser::EndSection() { |
| 58 | if (action_ && action_->NumCommands() > 0) { |
| 59 | action_manager_->AddAction(std::move(action_)); |
| 60 | } |
| 61 | |
| 62 | return Success(); |
| 63 | } |
| 64 | |
| 65 | } // namespace init |
| 66 | } // namespace android |