blob: c6f4f459b846fa39db19b09359f092394391734a [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;
53 std::vector<std::string> args;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080054
55 for (;;) {
Tom Cherry67dee622017-07-27 12:54:48 -070056 switch (next_token(&state)) {
57 case T_EOF:
58 if (section_parser) section_parser->EndSection();
59 return;
60 case T_NEWLINE:
61 state.line++;
62 if (args.empty()) break;
63 // If we have a line matching a prefix we recognize, call its callback and unset any
64 // current section parsers. This is meant for /sys/ and /dev/ line entries for
65 // uevent.
66 for (const auto& [prefix, callback] : line_callbacks_) {
67 if (android::base::StartsWith(args[0], prefix.c_str())) {
68 if (section_parser) section_parser->EndSection();
69
70 std::string ret_err;
71 if (!callback(std::move(args), &ret_err)) {
72 LOG(ERROR) << filename << ": " << state.line << ": " << ret_err;
73 }
74 section_parser = nullptr;
75 break;
76 }
77 }
78 if (section_parsers_.count(args[0])) {
79 if (section_parser) section_parser->EndSection();
80 section_parser = section_parsers_[args[0]].get();
81 std::string ret_err;
82 if (!section_parser->ParseSection(std::move(args), filename, state.line,
83 &ret_err)) {
84 LOG(ERROR) << filename << ": " << state.line << ": " << ret_err;
85 section_parser = nullptr;
86 }
87 } else if (section_parser) {
88 std::string ret_err;
89 if (!section_parser->ParseLineSection(std::move(args), state.line, &ret_err)) {
90 LOG(ERROR) << filename << ": " << state.line << ": " << ret_err;
91 }
92 }
93 args.clear();
94 break;
95 case T_TEXT:
96 args.emplace_back(state.text);
97 break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080098 }
99 }
Tom Cherry67dee622017-07-27 12:54:48 -0700100}
101
102bool Parser::ParseConfigFile(const std::string& path) {
103 LOG(INFO) << "Parsing file " << path << "...";
104 android::base::Timer t;
105 std::string data;
106 std::string err;
107 if (!ReadFile(path, &data, &err)) {
108 LOG(ERROR) << err;
109 return false;
110 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800111
Tom Cherry67dee622017-07-27 12:54:48 -0700112 data.push_back('\n'); // TODO: fix parse_config.
113 ParseData(path, data);
114 for (const auto& [section_name, section_parser] : section_parsers_) {
115 section_parser->EndFile();
116 }
117
118 LOG(VERBOSE) << "(Parsing " << path << " took " << t << ".)";
119 return true;
120}
121
122bool Parser::ParseConfigDir(const std::string& path) {
123 LOG(INFO) << "Parsing directory " << path << "...";
124 std::unique_ptr<DIR, decltype(&closedir)> config_dir(opendir(path.c_str()), closedir);
125 if (!config_dir) {
126 PLOG(ERROR) << "Could not import directory '" << path << "'";
127 return false;
128 }
129 dirent* current_file;
130 std::vector<std::string> files;
131 while ((current_file = readdir(config_dir.get()))) {
132 // Ignore directories and only process regular files.
133 if (current_file->d_type == DT_REG) {
134 std::string current_path =
135 android::base::StringPrintf("%s/%s", path.c_str(), current_file->d_name);
136 files.emplace_back(current_path);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800137 }
138 }
Tom Cherry67dee622017-07-27 12:54:48 -0700139 // Sort first so we load files in a consistent order (bug 31996208)
140 std::sort(files.begin(), files.end());
141 for (const auto& file : files) {
142 if (!ParseConfigFile(file)) {
143 LOG(ERROR) << "could not import file '" << file << "'";
144 }
145 }
146 return true;
147}
148
149bool Parser::ParseConfig(const std::string& path) {
150 if (is_dir(path.c_str())) {
151 return ParseConfigDir(path);
152 }
153 return ParseConfigFile(path);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800154}
Tom Cherry81f5d3e2017-06-22 12:53:17 -0700155
156} // namespace init
157} // namespace android