2010-04-20 23:29:05 +02: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 01:57:14 +02:00
|
|
|
#include <dirent.h>
|
2015-02-07 05:15:18 +01:00
|
|
|
#include <errno.h>
|
2010-04-20 23:29:05 +02:00
|
|
|
#include <fcntl.h>
|
|
|
|
|
2015-07-24 02:53:11 +02:00
|
|
|
#include "action.h"
|
2010-04-20 23:29:05 +02:00
|
|
|
#include "init_parser.h"
|
|
|
|
#include "log.h"
|
2015-07-31 21:45:25 +02:00
|
|
|
#include "parser.h"
|
|
|
|
#include "service.h"
|
2010-04-20 23:29:05 +02:00
|
|
|
#include "util.h"
|
|
|
|
|
2015-07-25 01:57:14 +02:00
|
|
|
#include <base/stringprintf.h>
|
2010-04-20 23:29:05 +02:00
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
Parser::Parser() {
|
2015-02-04 23:46:36 +01:00
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
Parser& Parser::GetInstance() {
|
|
|
|
static Parser instance;
|
|
|
|
return instance;
|
2011-12-16 23:20:12 +01:00
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
void Parser::AddSectionParser(const std::string& name,
|
|
|
|
std::unique_ptr<SectionParser> parser) {
|
|
|
|
section_parsers_[name] = std::move(parser);
|
2011-12-20 21:53:48 +01:00
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
void Parser::ParseData(const std::string& filename, const std::string& data) {
|
2015-08-06 19:46:28 +02: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-12 22:54:41 +02:00
|
|
|
parse_state state;
|
2015-08-26 20:43:36 +02:00
|
|
|
state.filename = filename.c_str();
|
2010-12-26 18:55:10 +01:00
|
|
|
state.line = 0;
|
2015-08-06 19:46:28 +02:00
|
|
|
state.ptr = &data_copy[0];
|
2010-04-20 23:29:05 +02:00
|
|
|
state.nexttoken = 0;
|
2011-12-20 21:53:48 +01:00
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
SectionParser* section_parser = nullptr;
|
|
|
|
std::vector<std::string> args;
|
2011-12-20 21:53:48 +01:00
|
|
|
|
2010-04-20 23:29:05 +02:00
|
|
|
for (;;) {
|
|
|
|
switch (next_token(&state)) {
|
|
|
|
case T_EOF:
|
2015-08-26 20:43:36 +02:00
|
|
|
if (section_parser) {
|
|
|
|
section_parser->EndSection();
|
|
|
|
}
|
|
|
|
return;
|
2010-04-20 23:29:05 +02:00
|
|
|
case T_NEWLINE:
|
2010-12-26 18:55:10 +01:00
|
|
|
state.line++;
|
2015-08-26 20:43:36 +02: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-20 23:29:05 +02:00
|
|
|
}
|
|
|
|
}
|
2015-08-26 20:43:36 +02:00
|
|
|
args.clear();
|
2010-04-20 23:29:05 +02:00
|
|
|
break;
|
|
|
|
case T_TEXT:
|
2015-08-26 20:43:36 +02:00
|
|
|
args.emplace_back(state.text);
|
2010-04-20 23:29:05 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
bool Parser::ParseConfigFile(const std::string& path) {
|
|
|
|
INFO("Parsing file %s...\n", path.c_str());
|
2015-03-28 07:20:44 +01:00
|
|
|
Timer t;
|
2015-02-06 21:19:48 +01:00
|
|
|
std::string data;
|
2015-08-26 20:43:36 +02:00
|
|
|
if (!read_file(path.c_str(), &data)) {
|
2015-05-07 04:19:24 +02:00
|
|
|
return false;
|
2015-02-06 21:19:48 +01:00
|
|
|
}
|
2010-04-20 23:29:05 +02:00
|
|
|
|
2015-05-12 22:54:41 +02:00
|
|
|
data.push_back('\n'); // TODO: fix parse_config.
|
2015-08-26 20:43:36 +02:00
|
|
|
ParseData(path, data);
|
|
|
|
for (const auto& sp : section_parsers_) {
|
|
|
|
sp.second->EndFile(path);
|
|
|
|
}
|
|
|
|
DumpState();
|
2015-03-28 07:20:44 +01:00
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
NOTICE("(Parsing %s took %.2fs.)\n", path.c_str(), t.duration());
|
2015-05-07 04:19:24 +02:00
|
|
|
return true;
|
2010-04-20 23:29:05 +02:00
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02: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 01:57:14 +02:00
|
|
|
if (!config_dir) {
|
2015-08-26 20:43:36 +02:00
|
|
|
ERROR("Could not import directory '%s'\n", path.c_str());
|
2015-07-25 01:57:14 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
dirent* current_file;
|
|
|
|
while ((current_file = readdir(config_dir.get()))) {
|
|
|
|
std::string current_path =
|
2015-08-26 20:43:36 +02:00
|
|
|
android::base::StringPrintf("%s/%s", path.c_str(), current_file->d_name);
|
2015-07-25 01:57:14 +02:00
|
|
|
// Ignore directories and only process regular files.
|
|
|
|
if (current_file->d_type == DT_REG) {
|
2015-08-26 20:43:36 +02:00
|
|
|
if (!ParseConfigFile(current_path)) {
|
2015-07-25 01:57:14 +02:00
|
|
|
ERROR("could not import file '%s'\n", current_path.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
bool Parser::ParseConfig(const std::string& path) {
|
|
|
|
if (is_dir(path.c_str())) {
|
|
|
|
return ParseConfigDir(path);
|
2015-07-25 01:57:14 +02:00
|
|
|
}
|
2015-08-26 20:43:36 +02:00
|
|
|
return ParseConfigFile(path);
|
2015-07-25 01:57:14 +02:00
|
|
|
}
|
|
|
|
|
2015-08-26 20:43:36 +02:00
|
|
|
void Parser::DumpState() const {
|
|
|
|
ServiceManager::GetInstance().DumpState();
|
|
|
|
ActionManager::GetInstance().DumpState();
|
2010-04-20 23:29:05 +02:00
|
|
|
}
|