| Colin Cross | 6310a82 | 2010-04-20 14:29:05 -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 |  | 
|  | 17 | #ifndef _INIT_INIT_PARSER_H_ | 
|  | 18 | #define _INIT_INIT_PARSER_H_ | 
|  | 19 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 20 | #include <map> | 
| Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 21 | #include <memory> | 
| Yabin Cui | 00ede7d | 2015-07-24 13:26:04 -0700 | [diff] [blame] | 22 | #include <string> | 
| Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 23 | #include <vector> | 
| Yabin Cui | 00ede7d | 2015-07-24 13:26:04 -0700 | [diff] [blame] | 24 |  | 
| Tom Cherry | 30a6f27 | 2017-04-19 15:31:58 -0700 | [diff] [blame] | 25 | //  SectionParser is an interface that can parse a given 'section' in init. | 
|  | 26 | // | 
|  | 27 | //  You can implement up to 4 functions below, with ParseSection() being mandatory. | 
|  | 28 | //  The first two function return bool with false indicating a failure and has a std::string* err | 
|  | 29 | //  parameter into which an error string can be written.  It will be reported along with the | 
|  | 30 | //  filename and line number of where the error occurred. | 
|  | 31 | // | 
|  | 32 | //  1) bool ParseSection(std::vector<std::string>&& args, const std::string& filename, | 
|  | 33 | //                       int line, std::string* err) | 
|  | 34 | //    This function is called when a section is first encountered. | 
|  | 35 | // | 
|  | 36 | //  2) bool ParseLineSection(std::vector<std::string>&& args, int line, std::string* err) | 
|  | 37 | //    This function is called on each subsequent line until the next section is encountered. | 
|  | 38 | // | 
|  | 39 | //  3) bool EndSection() | 
|  | 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 | // | 
|  | 44 | //  4) bool EndFile() | 
|  | 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. | 
|  | 47 |  | 
| Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 48 | namespace android { | 
|  | 49 | namespace init { | 
|  | 50 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 51 | class SectionParser { | 
| Tom Cherry | 30a6f27 | 2017-04-19 15:31:58 -0700 | [diff] [blame] | 52 | public: | 
|  | 53 | virtual ~SectionParser() {} | 
|  | 54 | virtual bool ParseSection(std::vector<std::string>&& args, const std::string& filename, | 
| Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame] | 55 | int line, std::string* err) = 0; | 
| Tom Cherry | 30a6f27 | 2017-04-19 15:31:58 -0700 | [diff] [blame] | 56 | virtual bool ParseLineSection(std::vector<std::string>&&, int, std::string*) { return true; }; | 
|  | 57 | virtual void EndSection(){}; | 
|  | 58 | virtual void EndFile(){}; | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 59 | }; | 
| Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 60 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 61 | class Parser { | 
| Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 62 | public: | 
| Tom Cherry | 35c5bcc | 2017-04-24 16:55:30 -0700 | [diff] [blame] | 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. | 
|  | 70 | using LineCallback = std::function<bool(std::vector<std::string>&&, std::string*)>; | 
|  | 71 |  | 
| Tom Cherry | ed506f7 | 2017-05-25 15:58:59 -0700 | [diff] [blame] | 72 | // TODO: init is the only user of this as a singleton; remove it. | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 73 | static Parser& GetInstance(); | 
| Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 74 |  | 
| Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 75 | Parser(); | 
|  | 76 |  | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 77 | bool ParseConfig(const std::string& path); | 
| Tom Cherry | 35c5bcc | 2017-04-24 16:55:30 -0700 | [diff] [blame] | 78 | void AddSectionParser(const std::string& name, std::unique_ptr<SectionParser> parser); | 
|  | 79 | void AddSingleLineParser(const std::string& prefix, LineCallback callback); | 
|  | 80 | void set_is_system_etc_init_loaded(bool loaded) { is_system_etc_init_loaded_ = loaded; } | 
|  | 81 | void set_is_vendor_etc_init_loaded(bool loaded) { is_vendor_etc_init_loaded_ = loaded; } | 
|  | 82 | void set_is_odm_etc_init_loaded(bool loaded) { is_odm_etc_init_loaded_ = loaded; } | 
| Jaekyun Seok | 4ec72cc | 2017-02-22 20:37:57 +0900 | [diff] [blame] | 83 | bool is_system_etc_init_loaded() { return is_system_etc_init_loaded_; } | 
|  | 84 | bool is_vendor_etc_init_loaded() { return is_vendor_etc_init_loaded_; } | 
|  | 85 | bool is_odm_etc_init_loaded() { return is_odm_etc_init_loaded_; } | 
| Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 86 |  | 
| Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 87 | private: | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 88 | void ParseData(const std::string& filename, const std::string& data); | 
|  | 89 | bool ParseConfigFile(const std::string& path); | 
|  | 90 | bool ParseConfigDir(const std::string& path); | 
|  | 91 |  | 
|  | 92 | std::map<std::string, std::unique_ptr<SectionParser>> section_parsers_; | 
| Tom Cherry | 35c5bcc | 2017-04-24 16:55:30 -0700 | [diff] [blame] | 93 | std::vector<std::pair<std::string, LineCallback>> line_callbacks_; | 
| Jaekyun Seok | 4ec72cc | 2017-02-22 20:37:57 +0900 | [diff] [blame] | 94 | bool is_system_etc_init_loaded_ = false; | 
|  | 95 | bool is_vendor_etc_init_loaded_ = false; | 
|  | 96 | bool is_odm_etc_init_loaded_ = false; | 
| Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 97 | }; | 
| Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 98 |  | 
| Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 99 | }  // namespace init | 
|  | 100 | }  // namespace android | 
|  | 101 |  | 
| Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 102 | #endif |