xref: /aosp_15_r20/system/core/init/parser.h (revision 00c7fec1bb09f3284aad6a6f96d2f63dfc3650ad)
1*00c7fec1SAndroid Build Coastguard Worker /*
2*00c7fec1SAndroid Build Coastguard Worker  * Copyright (C) 2010 The Android Open Source Project
3*00c7fec1SAndroid Build Coastguard Worker  *
4*00c7fec1SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*00c7fec1SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*00c7fec1SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*00c7fec1SAndroid Build Coastguard Worker  *
8*00c7fec1SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*00c7fec1SAndroid Build Coastguard Worker  *
10*00c7fec1SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*00c7fec1SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*00c7fec1SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*00c7fec1SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*00c7fec1SAndroid Build Coastguard Worker  * limitations under the License.
15*00c7fec1SAndroid Build Coastguard Worker  */
16*00c7fec1SAndroid Build Coastguard Worker 
17*00c7fec1SAndroid Build Coastguard Worker #ifndef _INIT_PARSER_H_
18*00c7fec1SAndroid Build Coastguard Worker #define _INIT_PARSER_H_
19*00c7fec1SAndroid Build Coastguard Worker 
20*00c7fec1SAndroid Build Coastguard Worker #include <map>
21*00c7fec1SAndroid Build Coastguard Worker #include <memory>
22*00c7fec1SAndroid Build Coastguard Worker #include <string>
23*00c7fec1SAndroid Build Coastguard Worker #include <vector>
24*00c7fec1SAndroid Build Coastguard Worker 
25*00c7fec1SAndroid Build Coastguard Worker #include "result.h"
26*00c7fec1SAndroid Build Coastguard Worker 
27*00c7fec1SAndroid Build Coastguard Worker //  SectionParser is an interface that can parse a given 'section' in init.
28*00c7fec1SAndroid Build Coastguard Worker //
29*00c7fec1SAndroid Build Coastguard Worker //  You can implement up to 4 functions below, with ParseSection being mandatory. The first two
30*00c7fec1SAndroid Build Coastguard Worker //  functions return Result<void> indicating if they have an error. It will be reported along
31*00c7fec1SAndroid Build Coastguard Worker //  with the filename and line number of where the error occurred.
32*00c7fec1SAndroid Build Coastguard Worker //
33*00c7fec1SAndroid Build Coastguard Worker //  1) ParseSection
34*00c7fec1SAndroid Build Coastguard Worker //    This function is called when a section is first encountered.
35*00c7fec1SAndroid Build Coastguard Worker //
36*00c7fec1SAndroid Build Coastguard Worker //  2) ParseLineSection
37*00c7fec1SAndroid Build Coastguard Worker //    This function is called on each subsequent line until the next section is encountered.
38*00c7fec1SAndroid Build Coastguard Worker //
39*00c7fec1SAndroid Build Coastguard Worker //  3) EndSection
40*00c7fec1SAndroid Build Coastguard Worker //    This function is called either when a new section is found or at the end of the file.
41*00c7fec1SAndroid Build Coastguard Worker //    It indicates that parsing of the current section is complete and any relevant objects should
42*00c7fec1SAndroid Build Coastguard Worker //    be committed.
43*00c7fec1SAndroid Build Coastguard Worker //
44*00c7fec1SAndroid Build Coastguard Worker //  4) EndFile
45*00c7fec1SAndroid Build Coastguard Worker //    This function is called at the end of the file.
46*00c7fec1SAndroid Build Coastguard Worker //    It indicates that the parsing has completed and any relevant objects should be committed.
47*00c7fec1SAndroid Build Coastguard Worker 
48*00c7fec1SAndroid Build Coastguard Worker namespace android {
49*00c7fec1SAndroid Build Coastguard Worker namespace init {
50*00c7fec1SAndroid Build Coastguard Worker 
51*00c7fec1SAndroid Build Coastguard Worker class SectionParser {
52*00c7fec1SAndroid Build Coastguard Worker   public:
~SectionParser()53*00c7fec1SAndroid Build Coastguard Worker     virtual ~SectionParser() {}
54*00c7fec1SAndroid Build Coastguard Worker     virtual Result<void> ParseSection(std::vector<std::string>&& args, const std::string& filename,
55*00c7fec1SAndroid Build Coastguard Worker                                       int line) = 0;
ParseLineSection(std::vector<std::string> &&,int)56*00c7fec1SAndroid Build Coastguard Worker     virtual Result<void> ParseLineSection(std::vector<std::string>&&, int) { return {}; };
EndSection()57*00c7fec1SAndroid Build Coastguard Worker     virtual Result<void> EndSection() { return {}; };
EndFile()58*00c7fec1SAndroid Build Coastguard Worker     virtual void EndFile(){};
59*00c7fec1SAndroid Build Coastguard Worker };
60*00c7fec1SAndroid Build Coastguard Worker 
61*00c7fec1SAndroid Build Coastguard Worker class Parser {
62*00c7fec1SAndroid Build Coastguard Worker   public:
63*00c7fec1SAndroid Build Coastguard Worker     //  LineCallback is the type for callbacks that can parse a line starting with a given prefix.
64*00c7fec1SAndroid Build Coastguard Worker     //
65*00c7fec1SAndroid Build Coastguard Worker     //  They take the form of bool Callback(std::vector<std::string>&& args, std::string* err)
66*00c7fec1SAndroid Build Coastguard Worker     //
67*00c7fec1SAndroid Build Coastguard Worker     //  Similar to ParseSection() and ParseLineSection(), this function returns bool with false
68*00c7fec1SAndroid Build Coastguard Worker     //  indicating a failure and has an std::string* err parameter into which an error string can
69*00c7fec1SAndroid Build Coastguard Worker     //  be written.
70*00c7fec1SAndroid Build Coastguard Worker     using LineCallback = std::function<Result<void>(std::vector<std::string>&&)>;
71*00c7fec1SAndroid Build Coastguard Worker 
72*00c7fec1SAndroid Build Coastguard Worker     Parser();
73*00c7fec1SAndroid Build Coastguard Worker 
74*00c7fec1SAndroid Build Coastguard Worker     bool ParseConfig(const std::string& path);
75*00c7fec1SAndroid Build Coastguard Worker     Result<void> ParseConfigFile(const std::string& path);
76*00c7fec1SAndroid Build Coastguard Worker     void AddSectionParser(const std::string& name, std::unique_ptr<SectionParser> parser);
77*00c7fec1SAndroid Build Coastguard Worker     void AddSingleLineParser(const std::string& prefix, LineCallback callback);
78*00c7fec1SAndroid Build Coastguard Worker 
79*00c7fec1SAndroid Build Coastguard Worker     // Host init verifier check file permissions.
80*00c7fec1SAndroid Build Coastguard Worker     bool ParseConfigFileInsecure(const std::string& path, bool follow_symlinks);
81*00c7fec1SAndroid Build Coastguard Worker 
parse_error_count()82*00c7fec1SAndroid Build Coastguard Worker     size_t parse_error_count() const { return parse_error_count_; }
83*00c7fec1SAndroid Build Coastguard Worker 
84*00c7fec1SAndroid Build Coastguard Worker   private:
85*00c7fec1SAndroid Build Coastguard Worker     void ParseData(const std::string& filename, std::string* data);
86*00c7fec1SAndroid Build Coastguard Worker     bool ParseConfigDir(const std::string& path);
87*00c7fec1SAndroid Build Coastguard Worker 
88*00c7fec1SAndroid Build Coastguard Worker     std::map<std::string, std::unique_ptr<SectionParser>> section_parsers_;
89*00c7fec1SAndroid Build Coastguard Worker     std::vector<std::pair<std::string, LineCallback>> line_callbacks_;
90*00c7fec1SAndroid Build Coastguard Worker     size_t parse_error_count_ = 0;
91*00c7fec1SAndroid Build Coastguard Worker };
92*00c7fec1SAndroid Build Coastguard Worker 
93*00c7fec1SAndroid Build Coastguard Worker }  // namespace init
94*00c7fec1SAndroid Build Coastguard Worker }  // namespace android
95*00c7fec1SAndroid Build Coastguard Worker 
96*00c7fec1SAndroid Build Coastguard Worker #endif
97