blob: 4ab24a4ee11c8c8809f9ed309ca5266acc735c16 [file] [log] [blame]
Colin Crossca7648d2010-04-13 19:29:51 -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
Tom Cherry67dee622017-07-27 12:54:48 -070017#ifndef _INIT_PARSER_H_
18#define _INIT_PARSER_H_
Colin Crossca7648d2010-04-13 19:29:51 -070019
Tom Cherry67dee622017-07-27 12:54:48 -070020#include <map>
21#include <memory>
22#include <string>
23#include <vector>
24
Tom Cherryb592dd82017-08-02 17:01:36 -070025#include "result.h"
26
Tom Cherry67dee622017-07-27 12:54:48 -070027// SectionParser is an interface that can parse a given 'section' in init.
28//
29// You can implement up to 4 functions below, with ParseSection() being mandatory.
30// The first two function return bool with false indicating a failure and has a std::string* err
31// parameter into which an error string can be written. It will be reported along with the
32// filename and line number of where the error occurred.
33//
34// 1) bool ParseSection(std::vector<std::string>&& args, const std::string& filename,
35// int line, std::string* err)
36// This function is called when a section is first encountered.
37//
38// 2) bool ParseLineSection(std::vector<std::string>&& args, int line, std::string* err)
39// This function is called on each subsequent line until the next section is encountered.
40//
41// 3) bool EndSection()
42// This function is called either when a new section is found or at the end of the file.
43// It indicates that parsing of the current section is complete and any relevant objects should
44// be committed.
45//
46// 4) bool EndFile()
47// This function is called at the end of the file.
48// It indicates that the parsing has completed and any relevant objects should be committed.
Colin Crossca7648d2010-04-13 19:29:51 -070049
Tom Cherry81f5d3e2017-06-22 12:53:17 -070050namespace android {
51namespace init {
52
Tom Cherry67dee622017-07-27 12:54:48 -070053class SectionParser {
54 public:
55 virtual ~SectionParser() {}
Tom Cherryb592dd82017-08-02 17:01:36 -070056 virtual Result<Success> ParseSection(std::vector<std::string>&& args,
57 const std::string& filename, int line) = 0;
58 virtual Result<Success> ParseLineSection(std::vector<std::string>&&, int) { return Success(); };
Tom Cherry67dee622017-07-27 12:54:48 -070059 virtual void EndSection(){};
60 virtual void EndFile(){};
Colin Cross6310a822010-04-20 14:29:05 -070061};
62
Tom Cherry67dee622017-07-27 12:54:48 -070063class Parser {
64 public:
65 // LineCallback is the type for callbacks that can parse a line starting with a given prefix.
66 //
67 // They take the form of bool Callback(std::vector<std::string>&& args, std::string* err)
68 //
69 // Similar to ParseSection() and ParseLineSection(), this function returns bool with false
70 // indicating a failure and has an std::string* err parameter into which an error string can
71 // be written.
Tom Cherryb592dd82017-08-02 17:01:36 -070072 using LineCallback = std::function<Result<Success>(std::vector<std::string>&&)>;
Tom Cherry67dee622017-07-27 12:54:48 -070073
74 Parser();
75
76 bool ParseConfig(const std::string& path);
77 void AddSectionParser(const std::string& name, std::unique_ptr<SectionParser> parser);
78 void AddSingleLineParser(const std::string& prefix, LineCallback callback);
79
80 private:
81 void ParseData(const std::string& filename, const std::string& data);
82 bool ParseConfigFile(const std::string& path);
83 bool ParseConfigDir(const std::string& path);
84
85 std::map<std::string, std::unique_ptr<SectionParser>> section_parsers_;
86 std::vector<std::pair<std::string, LineCallback>> line_callbacks_;
87};
Colin Crossca7648d2010-04-13 19:29:51 -070088
Tom Cherry81f5d3e2017-06-22 12:53:17 -070089} // namespace init
90} // namespace android
91
Tom Cherry67dee622017-07-27 12:54:48 -070092#endif