blob: acceed4f7570a3648440a43b064d982bf3628fe2 [file] [log] [blame]
Colin Cross6310a822010-04-20 14:29:05 -07001/*
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 Cherryb7349902015-08-26 11:43:36 -070020#include <map>
Tom Cherry3f5eaae52017-04-06 16:30:22 -070021#include <memory>
Yabin Cui00ede7d2015-07-24 13:26:04 -070022#include <string>
Tom Cherrybac32992015-07-31 12:45:25 -070023#include <vector>
Yabin Cui00ede7d2015-07-24 13:26:04 -070024
Tom Cherry30a6f272017-04-19 15:31:58 -070025// 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 Cherryb7349902015-08-26 11:43:36 -070048class SectionParser {
Tom Cherry30a6f272017-04-19 15:31:58 -070049 public:
50 virtual ~SectionParser() {}
51 virtual bool ParseSection(std::vector<std::string>&& args, const std::string& filename,
Tom Cherry012c5732017-04-18 13:21:54 -070052 int line, std::string* err) = 0;
Tom Cherry30a6f272017-04-19 15:31:58 -070053 virtual bool ParseLineSection(std::vector<std::string>&&, int, std::string*) { return true; };
54 virtual void EndSection(){};
55 virtual void EndFile(){};
Tom Cherryb7349902015-08-26 11:43:36 -070056};
Colin Cross6310a822010-04-20 14:29:05 -070057
Tom Cherryb7349902015-08-26 11:43:36 -070058class Parser {
59public:
60 static Parser& GetInstance();
Tom Cherryb7349902015-08-26 11:43:36 -070061 bool ParseConfig(const std::string& path);
62 void AddSectionParser(const std::string& name,
63 std::unique_ptr<SectionParser> parser);
Jaekyun Seok4ec72cc2017-02-22 20:37:57 +090064 void set_is_system_etc_init_loaded(bool loaded) {
65 is_system_etc_init_loaded_ = loaded;
66 }
67 void set_is_vendor_etc_init_loaded(bool loaded) {
68 is_vendor_etc_init_loaded_ = loaded;
69 }
70 void set_is_odm_etc_init_loaded(bool loaded) {
71 is_odm_etc_init_loaded_ = loaded;
72 }
73 bool is_system_etc_init_loaded() { return is_system_etc_init_loaded_; }
74 bool is_vendor_etc_init_loaded() { return is_vendor_etc_init_loaded_; }
75 bool is_odm_etc_init_loaded() { return is_odm_etc_init_loaded_; }
Colin Cross6310a822010-04-20 14:29:05 -070076
Tom Cherryb7349902015-08-26 11:43:36 -070077private:
78 Parser();
79
80 void ParseData(const std::string& filename, const std::string& data);
81 bool ParseConfigFile(const std::string& path);
82 bool ParseConfigDir(const std::string& path);
83
84 std::map<std::string, std::unique_ptr<SectionParser>> section_parsers_;
Jaekyun Seok4ec72cc2017-02-22 20:37:57 +090085 bool is_system_etc_init_loaded_ = false;
86 bool is_vendor_etc_init_loaded_ = false;
87 bool is_odm_etc_init_loaded_ = false;
Tom Cherryb7349902015-08-26 11:43:36 -070088};
Elliott Hughes8d82ea02015-02-06 20:15:18 -080089
Colin Cross6310a822010-04-20 14:29:05 -070090#endif