blob: 6ddb09f2b730152b2a72b146081e9cb733f2b82e [file] [log] [blame]
Tom Cherry67dee622017-07-27 12:54:48 -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
Elliott Hughesc0e919c2015-02-04 14:46:36 -080017#include "parser.h"
18
Tom Cherry67dee622017-07-27 12:54:48 -070019#include <dirent.h>
20
21#include <android-base/chrono_utils.h>
22#include <android-base/logging.h>
23#include <android-base/stringprintf.h>
24#include <android-base/strings.h>
25
26#include "tokenizer.h"
27#include "util.h"
28
Tom Cherry81f5d3e2017-06-22 12:53:17 -070029namespace android {
30namespace init {
31
Tom Cherry67dee622017-07-27 12:54:48 -070032Parser::Parser() {}
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080033
Tom Cherry67dee622017-07-27 12:54:48 -070034void Parser::AddSectionParser(const std::string& name, std::unique_ptr<SectionParser> parser) {
35 section_parsers_[name] = std::move(parser);
36}
37
38void Parser::AddSingleLineParser(const std::string& prefix, LineCallback callback) {
39 line_callbacks_.emplace_back(prefix, callback);
40}
41
42void Parser::ParseData(const std::string& filename, const std::string& data) {
43 // TODO: Use a parser with const input and remove this copy
44 std::vector<char> data_copy(data.begin(), data.end());
45 data_copy.push_back('\0');
46
47 parse_state state;
48 state.line = 0;
49 state.ptr = &data_copy[0];
50 state.nexttoken = 0;
51
52 SectionParser* section_parser = nullptr;
Steven Moreland7d0a5c32017-11-10 14:20:47 -080053 int section_start_line = -1;
Tom Cherry67dee622017-07-27 12:54:48 -070054 std::vector<std::string> args;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080055
Steven Moreland7d0a5c32017-11-10 14:20:47 -080056 auto end_section = [&] {
57 if (section_parser == nullptr) return;
58
59 if (auto result = section_parser->EndSection(); !result) {
60 LOG(ERROR) << filename << ": " << section_start_line << ": " << result.error();
61 }
62
63 section_parser = nullptr;
64 section_start_line = -1;
65 };
66
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080067 for (;;) {
Tom Cherry67dee622017-07-27 12:54:48 -070068 switch (next_token(&state)) {
69 case T_EOF:
Steven Moreland7d0a5c32017-11-10 14:20:47 -080070 end_section();
Tom Cherry67dee622017-07-27 12:54:48 -070071 return;
72 case T_NEWLINE:
73 state.line++;
74 if (args.empty()) break;
75 // If we have a line matching a prefix we recognize, call its callback and unset any
76 // current section parsers. This is meant for /sys/ and /dev/ line entries for
77 // uevent.
78 for (const auto& [prefix, callback] : line_callbacks_) {
79 if (android::base::StartsWith(args[0], prefix.c_str())) {
Steven Moreland7d0a5c32017-11-10 14:20:47 -080080 end_section();
Tom Cherry67dee622017-07-27 12:54:48 -070081
Tom Cherryb592dd82017-08-02 17:01:36 -070082 if (auto result = callback(std::move(args)); !result) {
83 LOG(ERROR) << filename << ": " << state.line << ": " << result.error();
Tom Cherry67dee622017-07-27 12:54:48 -070084 }
Tom Cherry67dee622017-07-27 12:54:48 -070085 break;
86 }
87 }
88 if (section_parsers_.count(args[0])) {
Steven Moreland7d0a5c32017-11-10 14:20:47 -080089 end_section();
Tom Cherry67dee622017-07-27 12:54:48 -070090 section_parser = section_parsers_[args[0]].get();
Steven Moreland7d0a5c32017-11-10 14:20:47 -080091 section_start_line = state.line;
Tom Cherryb592dd82017-08-02 17:01:36 -070092 if (auto result =
93 section_parser->ParseSection(std::move(args), filename, state.line);
94 !result) {
95 LOG(ERROR) << filename << ": " << state.line << ": " << result.error();
Tom Cherry67dee622017-07-27 12:54:48 -070096 section_parser = nullptr;
97 }
98 } else if (section_parser) {
Tom Cherryb592dd82017-08-02 17:01:36 -070099 if (auto result = section_parser->ParseLineSection(std::move(args), state.line);
100 !result) {
101 LOG(ERROR) << filename << ": " << state.line << ": " << result.error();
Tom Cherry67dee622017-07-27 12:54:48 -0700102 }
103 }
104 args.clear();
105 break;
106 case T_TEXT:
107 args.emplace_back(state.text);
108 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800109 }
110 }
Tom Cherry67dee622017-07-27 12:54:48 -0700111}
112
113bool Parser::ParseConfigFile(const std::string& path) {
114 LOG(INFO) << "Parsing file " << path << "...";
115 android::base::Timer t;
Tom Cherry62ca6632017-08-03 12:54:07 -0700116 auto config_contents = ReadFile(path);
117 if (!config_contents) {
118 LOG(ERROR) << "Unable to read config file '" << path << "': " << config_contents.error();
Tom Cherry67dee622017-07-27 12:54:48 -0700119 return false;
120 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800121
Tom Cherry62ca6632017-08-03 12:54:07 -0700122 config_contents->push_back('\n'); // TODO: fix parse_config.
123 ParseData(path, *config_contents);
Tom Cherry67dee622017-07-27 12:54:48 -0700124 for (const auto& [section_name, section_parser] : section_parsers_) {
125 section_parser->EndFile();
126 }
127
128 LOG(VERBOSE) << "(Parsing " << path << " took " << t << ".)";
129 return true;
130}
131
132bool Parser::ParseConfigDir(const std::string& path) {
133 LOG(INFO) << "Parsing directory " << path << "...";
134 std::unique_ptr<DIR, decltype(&closedir)> config_dir(opendir(path.c_str()), closedir);
135 if (!config_dir) {
136 PLOG(ERROR) << "Could not import directory '" << path << "'";
137 return false;
138 }
139 dirent* current_file;
140 std::vector<std::string> files;
141 while ((current_file = readdir(config_dir.get()))) {
142 // Ignore directories and only process regular files.
143 if (current_file->d_type == DT_REG) {
144 std::string current_path =
145 android::base::StringPrintf("%s/%s", path.c_str(), current_file->d_name);
146 files.emplace_back(current_path);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800147 }
148 }
Tom Cherry67dee622017-07-27 12:54:48 -0700149 // Sort first so we load files in a consistent order (bug 31996208)
150 std::sort(files.begin(), files.end());
151 for (const auto& file : files) {
152 if (!ParseConfigFile(file)) {
153 LOG(ERROR) << "could not import file '" << file << "'";
154 }
155 }
156 return true;
157}
158
159bool Parser::ParseConfig(const std::string& path) {
160 if (is_dir(path.c_str())) {
161 return ParseConfigDir(path);
162 }
163 return ParseConfigFile(path);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800164}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700165
166} // namespace init
167} // namespace android