Colin Cross | ca7648d | 2010-04-13 19:29:51 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | |
Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 17 | #ifndef _INIT_PARSER_H_ |
| 18 | #define _INIT_PARSER_H_ |
Colin Cross | ca7648d | 2010-04-13 19:29:51 -0700 | [diff] [blame] | 19 | |
Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 20 | #include <map> |
| 21 | #include <memory> |
| 22 | #include <string> |
| 23 | #include <vector> |
| 24 | |
Tom Cherry | b592dd8 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 25 | #include "result.h" |
| 26 | |
Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 27 | // SectionParser is an interface that can parse a given 'section' in init. |
| 28 | // |
Steven Moreland | b480d44 | 2017-11-10 12:50:58 -0800 | [diff] [blame] | 29 | // You can implement up to 4 functions below, with ParseSection being mandatory. The first two |
| 30 | // functions return Result<Success> indicating if they have an error. It will be reported along |
| 31 | // with the filename and line number of where the error occurred. |
Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 32 | // |
Steven Moreland | b480d44 | 2017-11-10 12:50:58 -0800 | [diff] [blame] | 33 | // 1) ParseSection |
Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 34 | // This function is called when a section is first encountered. |
| 35 | // |
Steven Moreland | b480d44 | 2017-11-10 12:50:58 -0800 | [diff] [blame] | 36 | // 2) ParseLineSection |
Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 37 | // This function is called on each subsequent line until the next section is encountered. |
| 38 | // |
Steven Moreland | b480d44 | 2017-11-10 12:50:58 -0800 | [diff] [blame] | 39 | // 3) EndSection |
Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 40 | // This function is called either when a new section is found or at the end of the file. |
| 41 | // It indicates that parsing of the current section is complete and any relevant objects should |
| 42 | // be committed. |
| 43 | // |
Steven Moreland | b480d44 | 2017-11-10 12:50:58 -0800 | [diff] [blame] | 44 | // 4) EndFile |
Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 45 | // This function is called at the end of the file. |
| 46 | // It indicates that the parsing has completed and any relevant objects should be committed. |
Colin Cross | ca7648d | 2010-04-13 19:29:51 -0700 | [diff] [blame] | 47 | |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 48 | namespace android { |
| 49 | namespace init { |
| 50 | |
Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 51 | class SectionParser { |
| 52 | public: |
| 53 | virtual ~SectionParser() {} |
Tom Cherry | b592dd8 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 54 | virtual Result<Success> ParseSection(std::vector<std::string>&& args, |
| 55 | const std::string& filename, int line) = 0; |
| 56 | virtual Result<Success> ParseLineSection(std::vector<std::string>&&, int) { return Success(); }; |
Steven Moreland | 7d0a5c3 | 2017-11-10 14:20:47 -0800 | [diff] [blame] | 57 | virtual Result<Success> EndSection() { return Success(); }; |
Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 58 | virtual void EndFile(){}; |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 59 | }; |
| 60 | |
Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 61 | class Parser { |
| 62 | public: |
| 63 | // LineCallback is the type for callbacks that can parse a line starting with a given prefix. |
| 64 | // |
| 65 | // They take the form of bool Callback(std::vector<std::string>&& args, std::string* err) |
| 66 | // |
| 67 | // Similar to ParseSection() and ParseLineSection(), this function returns bool with false |
| 68 | // indicating a failure and has an std::string* err parameter into which an error string can |
| 69 | // be written. |
Tom Cherry | b592dd8 | 2017-08-02 17:01:36 -0700 | [diff] [blame] | 70 | using LineCallback = std::function<Result<Success>(std::vector<std::string>&&)>; |
Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 71 | |
| 72 | Parser(); |
| 73 | |
| 74 | bool ParseConfig(const std::string& path); |
| 75 | void AddSectionParser(const std::string& name, std::unique_ptr<SectionParser> parser); |
| 76 | void AddSingleLineParser(const std::string& prefix, LineCallback callback); |
| 77 | |
Tom Cherry | d72432d | 2018-06-19 15:18:40 -0700 | [diff] [blame] | 78 | // Host init verifier check file permissions. |
| 79 | bool ParseConfigFileInsecure(const std::string& path); |
| 80 | |
Tom Cherry | 194b5d1 | 2018-05-09 17:38:30 -0700 | [diff] [blame] | 81 | size_t parse_error_count() const { return parse_error_count_; } |
| 82 | |
Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 83 | private: |
Tom Cherry | d72432d | 2018-06-19 15:18:40 -0700 | [diff] [blame] | 84 | void ParseData(const std::string& filename, std::string* data); |
Tom Cherry | 194b5d1 | 2018-05-09 17:38:30 -0700 | [diff] [blame] | 85 | bool ParseConfigFile(const std::string& path); |
| 86 | bool ParseConfigDir(const std::string& path); |
Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 87 | |
| 88 | std::map<std::string, std::unique_ptr<SectionParser>> section_parsers_; |
| 89 | std::vector<std::pair<std::string, LineCallback>> line_callbacks_; |
Tom Cherry | 194b5d1 | 2018-05-09 17:38:30 -0700 | [diff] [blame] | 90 | size_t parse_error_count_ = 0; |
Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 91 | }; |
Colin Cross | ca7648d | 2010-04-13 19:29:51 -0700 | [diff] [blame] | 92 | |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 93 | } // namespace init |
| 94 | } // namespace android |
| 95 | |
Tom Cherry | 67dee62 | 2017-07-27 12:54:48 -0700 | [diff] [blame] | 96 | #endif |