2010-04-21 05:29:05 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-07-25 07:57:14 +08:00
|
|
|
#include <dirent.h>
|
2015-02-07 12:15:18 +08:00
|
|
|
#include <errno.h>
|
2010-04-21 05:29:05 +08:00
|
|
|
#include <fcntl.h>
|
|
|
|
|
2015-07-24 08:53:11 +08:00
|
|
|
#include "action.h"
|
2010-04-21 05:29:05 +08:00
|
|
|
#include "init_parser.h"
|
|
|
|
#include "log.h"
|
2015-08-01 03:45:25 +08:00
|
|
|
#include "parser.h"
|
|
|
|
#include "service.h"
|
2010-04-21 05:29:05 +08:00
|
|
|
#include "util.h"
|
|
|
|
|
2015-12-05 14:00:26 +08:00
|
|
|
#include <android-base/stringprintf.h>
|
2010-04-21 05:29:05 +08:00
|
|
|
|
2015-08-27 02:43:36 +08:00
|
|
|
Parser::Parser() {
|
2015-02-05 06:46:36 +08:00
|
|
|
}
|
|
|
|
|
2015-08-27 02:43:36 +08:00
|
|
|
Parser& Parser::GetInstance() {
|
|
|
|
static Parser instance;
|
|
|
|
return instance;
|
2011-12-17 06:20:12 +08:00
|
|
|
}
|
|
|
|
|
2015-08-27 02:43:36 +08:00
|
|
|
void Parser::AddSectionParser(const std::string& name,
|
|
|
|
std::unique_ptr<SectionParser> parser) {
|
|
|
|
section_parsers_[name] = std::move(parser);
|
2011-12-21 04:53:48 +08:00
|
|
|
}
|
|
|
|
|
2015-08-27 02:43:36 +08:00
|
|
|
void Parser::ParseData(const std::string& filename, const std::string& data) {
|
2015-08-07 01:46:28 +08:00
|
|
|
//TODO: Use a parser with const input and remove this copy
|
|
|
|
std::vector<char> data_copy(data.begin(), data.end());
|
|
|
|
data_copy.push_back('\0');
|
|
|
|
|
2015-05-13 04:54:41 +08:00
|
|
|
parse_state state;
|
2015-08-27 02:43:36 +08:00
|
|
|
state.filename = filename.c_str();
|
2010-12-27 01:55:10 +08:00
|
|
|
state.line = 0;
|
2015-08-07 01:46:28 +08:00
|
|
|
state.ptr = &data_copy[0];
|
2010-04-21 05:29:05 +08:00
|
|
|
state.nexttoken = 0;
|
2011-12-21 04:53:48 +08:00
|
|
|
|
2015-08-27 02:43:36 +08:00
|
|
|
SectionParser* section_parser = nullptr;
|
|
|
|
std::vector<std::string> args;
|
2011-12-21 04:53:48 +08:00
|
|
|
|
2010-04-21 05:29:05 +08:00
|
|
|
for (;;) {
|
|
|
|
switch (next_token(&state)) {
|
|
|
|
case T_EOF:
|
2015-08-27 02:43:36 +08:00
|
|
|
if (section_parser) {
|
|
|
|
section_parser->EndSection();
|
|
|
|
}
|
|
|
|
return;
|
2010-04-21 05:29:05 +08:00
|
|
|
case T_NEWLINE:
|
2010-12-27 01:55:10 +08:00
|
|
|
state.line++;
|
2015-08-27 02:43:36 +08:00
|
|
|
if (args.empty()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (section_parsers_.count(args[0])) {
|
|
|
|
if (section_parser) {
|
|
|
|
section_parser->EndSection();
|
|
|
|
}
|
|
|
|
section_parser = section_parsers_[args[0]].get();
|
|
|
|
std::string ret_err;
|
|
|
|
if (!section_parser->ParseSection(args, &ret_err)) {
|
|
|
|
parse_error(&state, "%s\n", ret_err.c_str());
|
|
|
|
section_parser = nullptr;
|
|
|
|
}
|
|
|
|
} else if (section_parser) {
|
|
|
|
std::string ret_err;
|
|
|
|
if (!section_parser->ParseLineSection(args, state.filename,
|
|
|
|
state.line, &ret_err)) {
|
|
|
|
parse_error(&state, "%s\n", ret_err.c_str());
|
2010-04-21 05:29:05 +08:00
|
|
|
}
|
|
|
|
}
|
2015-08-27 02:43:36 +08:00
|
|
|
args.clear();
|
2010-04-21 05:29:05 +08:00
|
|
|
break;
|
|
|
|
case T_TEXT:
|
2015-08-27 02:43:36 +08:00
|
|
|
args.emplace_back(state.text);
|
2010-04-21 05:29:05 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-27 02:43:36 +08:00
|
|
|
bool Parser::ParseConfigFile(const std::string& path) {
|
|
|
|
INFO("Parsing file %s...\n", path.c_str());
|
2015-03-28 14:20:44 +08:00
|
|
|
Timer t;
|
2015-02-07 04:19:48 +08:00
|
|
|
std::string data;
|
2015-08-27 02:43:36 +08:00
|
|
|
if (!read_file(path.c_str(), &data)) {
|
2015-05-07 10:19:24 +08:00
|
|
|
return false;
|
2015-02-07 04:19:48 +08:00
|
|
|
}
|
2010-04-21 05:29:05 +08:00
|
|
|
|
2015-05-13 04:54:41 +08:00
|
|
|
data.push_back('\n'); // TODO: fix parse_config.
|
2015-08-27 02:43:36 +08:00
|
|
|
ParseData(path, data);
|
|
|
|
for (const auto& sp : section_parsers_) {
|
|
|
|
sp.second->EndFile(path);
|
|
|
|
}
|
2015-10-10 05:03:14 +08:00
|
|
|
|
|
|
|
// Turning this on and letting the INFO logging be discarded adds 0.2s to
|
|
|
|
// Nexus 9 boot time, so it's disabled by default.
|
|
|
|
if (false) DumpState();
|
2015-03-28 14:20:44 +08:00
|
|
|
|
2015-08-27 02:43:36 +08:00
|
|
|
NOTICE("(Parsing %s took %.2fs.)\n", path.c_str(), t.duration());
|
2015-05-07 10:19:24 +08:00
|
|
|
return true;
|
2010-04-21 05:29:05 +08:00
|
|
|
}
|
|
|
|
|
2015-08-27 02:43:36 +08:00
|
|
|
bool Parser::ParseConfigDir(const std::string& path) {
|
|
|
|
INFO("Parsing directory %s...\n", path.c_str());
|
|
|
|
std::unique_ptr<DIR, int(*)(DIR*)> config_dir(opendir(path.c_str()), closedir);
|
2015-07-25 07:57:14 +08:00
|
|
|
if (!config_dir) {
|
2015-08-27 02:43:36 +08:00
|
|
|
ERROR("Could not import directory '%s'\n", path.c_str());
|
2015-07-25 07:57:14 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
dirent* current_file;
|
|
|
|
while ((current_file = readdir(config_dir.get()))) {
|
|
|
|
std::string current_path =
|
2015-08-27 02:43:36 +08:00
|
|
|
android::base::StringPrintf("%s/%s", path.c_str(), current_file->d_name);
|
2015-07-25 07:57:14 +08:00
|
|
|
// Ignore directories and only process regular files.
|
|
|
|
if (current_file->d_type == DT_REG) {
|
2015-08-27 02:43:36 +08:00
|
|
|
if (!ParseConfigFile(current_path)) {
|
2015-07-25 07:57:14 +08:00
|
|
|
ERROR("could not import file '%s'\n", current_path.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-08-27 02:43:36 +08:00
|
|
|
bool Parser::ParseConfig(const std::string& path) {
|
|
|
|
if (is_dir(path.c_str())) {
|
|
|
|
return ParseConfigDir(path);
|
2015-07-25 07:57:14 +08:00
|
|
|
}
|
2015-08-27 02:43:36 +08:00
|
|
|
return ParseConfigFile(path);
|
2015-07-25 07:57:14 +08:00
|
|
|
}
|
|
|
|
|
2015-08-27 02:43:36 +08:00
|
|
|
void Parser::DumpState() const {
|
|
|
|
ServiceManager::GetInstance().DumpState();
|
|
|
|
ActionManager::GetInstance().DumpState();
|
2010-04-21 05:29:05 +08:00
|
|
|
}
|