blob: 5c7af79ee08113cda52e2c6f335f8de9d80368c1 [file] [log] [blame]
Colin Cross6310a822010-04-20 14:29:05 -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 Cherry3f5eaae52017-04-06 16:30:22 -070017#include "init_parser.h"
18
Lee Campbellf13b1b32015-07-24 16:57:14 -070019#include <dirent.h>
Colin Cross6310a822010-04-20 14:29:05 -070020
Tom Cherry3f5eaae52017-04-06 16:30:22 -070021#include <android-base/logging.h>
22#include <android-base/stringprintf.h>
23
Tom Cherrybac32992015-07-31 12:45:25 -070024#include "parser.h"
Tom Cherry30a6f272017-04-19 15:31:58 -070025#include "util.h"
Colin Cross6310a822010-04-20 14:29:05 -070026
Tom Cherryb7349902015-08-26 11:43:36 -070027Parser::Parser() {
Elliott Hughesc0e919c2015-02-04 14:46:36 -080028}
29
Tom Cherryb7349902015-08-26 11:43:36 -070030Parser& Parser::GetInstance() {
31 static Parser instance;
32 return instance;
Colin Cross6310a822010-04-20 14:29:05 -070033}
34
Tom Cherryb7349902015-08-26 11:43:36 -070035void Parser::AddSectionParser(const std::string& name,
36 std::unique_ptr<SectionParser> parser) {
37 section_parsers_[name] = std::move(parser);
Colin Cross6310a822010-04-20 14:29:05 -070038}
39
Tom Cherryb7349902015-08-26 11:43:36 -070040void Parser::ParseData(const std::string& filename, const std::string& data) {
Tom Cherry4ad60fb2015-08-06 10:46:28 -070041 //TODO: Use a parser with const input and remove this copy
42 std::vector<char> data_copy(data.begin(), data.end());
43 data_copy.push_back('\0');
44
Tom Cherryeaa3b4e2015-05-12 13:54:41 -070045 parse_state state;
Tom Cherryb7349902015-08-26 11:43:36 -070046 state.filename = filename.c_str();
Bruce Beare1be69682010-12-26 09:55:10 -080047 state.line = 0;
Tom Cherry4ad60fb2015-08-06 10:46:28 -070048 state.ptr = &data_copy[0];
Colin Cross6310a822010-04-20 14:29:05 -070049 state.nexttoken = 0;
Dima Zavin78a1b1f2011-12-20 12:53:48 -080050
Tom Cherryb7349902015-08-26 11:43:36 -070051 SectionParser* section_parser = nullptr;
52 std::vector<std::string> args;
Dima Zavin78a1b1f2011-12-20 12:53:48 -080053
Colin Cross6310a822010-04-20 14:29:05 -070054 for (;;) {
55 switch (next_token(&state)) {
56 case T_EOF:
Tom Cherryb7349902015-08-26 11:43:36 -070057 if (section_parser) {
58 section_parser->EndSection();
59 }
60 return;
Colin Cross6310a822010-04-20 14:29:05 -070061 case T_NEWLINE:
Bruce Beare1be69682010-12-26 09:55:10 -080062 state.line++;
Tom Cherryb7349902015-08-26 11:43:36 -070063 if (args.empty()) {
64 break;
Colin Cross6310a822010-04-20 14:29:05 -070065 }
Tom Cherryb7349902015-08-26 11:43:36 -070066 if (section_parsers_.count(args[0])) {
67 if (section_parser) {
68 section_parser->EndSection();
69 }
70 section_parser = section_parsers_[args[0]].get();
71 std::string ret_err;
Tom Cherry30a6f272017-04-19 15:31:58 -070072 if (!section_parser->ParseSection(std::move(args), state.filename, state.line,
73 &ret_err)) {
Tom Cherryb7349902015-08-26 11:43:36 -070074 parse_error(&state, "%s\n", ret_err.c_str());
75 section_parser = nullptr;
76 }
77 } else if (section_parser) {
78 std::string ret_err;
Tom Cherry30a6f272017-04-19 15:31:58 -070079 if (!section_parser->ParseLineSection(std::move(args), state.line, &ret_err)) {
Tom Cherryb7349902015-08-26 11:43:36 -070080 parse_error(&state, "%s\n", ret_err.c_str());
81 }
82 }
83 args.clear();
Colin Cross6310a822010-04-20 14:29:05 -070084 break;
85 case T_TEXT:
Tom Cherryb7349902015-08-26 11:43:36 -070086 args.emplace_back(state.text);
Colin Cross6310a822010-04-20 14:29:05 -070087 break;
88 }
89 }
90}
91
Tom Cherryb7349902015-08-26 11:43:36 -070092bool Parser::ParseConfigFile(const std::string& path) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -070093 LOG(INFO) << "Parsing file " << path << "...";
Elliott Hughesda40c002015-03-27 23:20:44 -070094 Timer t;
Elliott Hughesf682b472015-02-06 12:19:48 -080095 std::string data;
Tom Cherry53089aa2017-03-31 15:47:33 -070096 if (!read_file(path, &data)) {
Elliott Hughese5ce30f2015-05-06 19:19:24 -070097 return false;
Elliott Hughesf682b472015-02-06 12:19:48 -080098 }
Colin Cross6310a822010-04-20 14:29:05 -070099
Tom Cherryeaa3b4e2015-05-12 13:54:41 -0700100 data.push_back('\n'); // TODO: fix parse_config.
Tom Cherryb7349902015-08-26 11:43:36 -0700101 ParseData(path, data);
Tom Cherry30a6f272017-04-19 15:31:58 -0700102 for (const auto& [section_name, section_parser] : section_parsers_) {
103 section_parser->EndFile();
Tom Cherryb7349902015-08-26 11:43:36 -0700104 }
Elliott Hughes1946d3b2015-10-09 14:03:14 -0700105
Elliott Hughes331cf2f2016-11-29 19:20:58 +0000106 LOG(VERBOSE) << "(Parsing " << path << " took " << t << ".)";
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700107 return true;
Colin Cross6310a822010-04-20 14:29:05 -0700108}
109
Tom Cherryb7349902015-08-26 11:43:36 -0700110bool Parser::ParseConfigDir(const std::string& path) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700111 LOG(INFO) << "Parsing directory " << path << "...";
Tom Cherryb7349902015-08-26 11:43:36 -0700112 std::unique_ptr<DIR, int(*)(DIR*)> config_dir(opendir(path.c_str()), closedir);
Lee Campbellf13b1b32015-07-24 16:57:14 -0700113 if (!config_dir) {
Elliott Hughesf86b5a62016-06-24 15:12:21 -0700114 PLOG(ERROR) << "Could not import directory '" << path << "'";
Lee Campbellf13b1b32015-07-24 16:57:14 -0700115 return false;
116 }
117 dirent* current_file;
Glenn Kasten2de79642016-10-06 12:58:46 -0700118 std::vector<std::string> files;
Lee Campbellf13b1b32015-07-24 16:57:14 -0700119 while ((current_file = readdir(config_dir.get()))) {
Lee Campbellf13b1b32015-07-24 16:57:14 -0700120 // Ignore directories and only process regular files.
121 if (current_file->d_type == DT_REG) {
Glenn Kasten2de79642016-10-06 12:58:46 -0700122 std::string current_path =
123 android::base::StringPrintf("%s/%s", path.c_str(), current_file->d_name);
124 files.emplace_back(current_path);
125 }
126 }
127 // Sort first so we load files in a consistent order (bug 31996208)
128 std::sort(files.begin(), files.end());
129 for (const auto& file : files) {
130 if (!ParseConfigFile(file)) {
131 LOG(ERROR) << "could not import file '" << file << "'";
Lee Campbellf13b1b32015-07-24 16:57:14 -0700132 }
133 }
134 return true;
135}
136
Tom Cherryb7349902015-08-26 11:43:36 -0700137bool Parser::ParseConfig(const std::string& path) {
138 if (is_dir(path.c_str())) {
139 return ParseConfigDir(path);
Lee Campbellf13b1b32015-07-24 16:57:14 -0700140 }
Tom Cherryb7349902015-08-26 11:43:36 -0700141 return ParseConfigFile(path);
Lee Campbellf13b1b32015-07-24 16:57:14 -0700142}