blob: b44ca59d58adcc632eb5f40211c1958f775df7fb [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
Lee Campbellf13b1b32015-07-24 16:57:14 -070017#include <dirent.h>
Elliott Hughes8d82ea02015-02-06 20:15:18 -080018#include <errno.h>
Elliott Hughesda40c002015-03-27 23:20:44 -070019#include <fcntl.h>
Colin Cross6310a822010-04-20 14:29:05 -070020
Tom Cherryfa0c21c2015-07-23 17:53:11 -070021#include "action.h"
Colin Cross6310a822010-04-20 14:29:05 -070022#include "init_parser.h"
23#include "log.h"
Tom Cherrybac32992015-07-31 12:45:25 -070024#include "parser.h"
Tom Cherrybac32992015-07-31 12:45:25 -070025#include "service.h"
Colin Cross6310a822010-04-20 14:29:05 -070026#include "util.h"
27
Elliott Hughes4f713192015-12-04 22:00:26 -080028#include <android-base/stringprintf.h>
Colin Cross6310a822010-04-20 14:29:05 -070029
Tom Cherryb7349902015-08-26 11:43:36 -070030Parser::Parser() {
Elliott Hughesc0e919c2015-02-04 14:46:36 -080031}
32
Tom Cherryb7349902015-08-26 11:43:36 -070033Parser& Parser::GetInstance() {
34 static Parser instance;
35 return instance;
Colin Cross6310a822010-04-20 14:29:05 -070036}
37
Tom Cherryb7349902015-08-26 11:43:36 -070038void Parser::AddSectionParser(const std::string& name,
39 std::unique_ptr<SectionParser> parser) {
40 section_parsers_[name] = std::move(parser);
Colin Cross6310a822010-04-20 14:29:05 -070041}
42
Tom Cherryb7349902015-08-26 11:43:36 -070043void Parser::ParseData(const std::string& filename, const std::string& data) {
Tom Cherry4ad60fb2015-08-06 10:46:28 -070044 //TODO: Use a parser with const input and remove this copy
45 std::vector<char> data_copy(data.begin(), data.end());
46 data_copy.push_back('\0');
47
Tom Cherryeaa3b4e2015-05-12 13:54:41 -070048 parse_state state;
Tom Cherryb7349902015-08-26 11:43:36 -070049 state.filename = filename.c_str();
Bruce Beare1be69682010-12-26 09:55:10 -080050 state.line = 0;
Tom Cherry4ad60fb2015-08-06 10:46:28 -070051 state.ptr = &data_copy[0];
Colin Cross6310a822010-04-20 14:29:05 -070052 state.nexttoken = 0;
Dima Zavin78a1b1f2011-12-20 12:53:48 -080053
Tom Cherryb7349902015-08-26 11:43:36 -070054 SectionParser* section_parser = nullptr;
55 std::vector<std::string> args;
Dima Zavin78a1b1f2011-12-20 12:53:48 -080056
Colin Cross6310a822010-04-20 14:29:05 -070057 for (;;) {
58 switch (next_token(&state)) {
59 case T_EOF:
Tom Cherryb7349902015-08-26 11:43:36 -070060 if (section_parser) {
61 section_parser->EndSection();
62 }
63 return;
Colin Cross6310a822010-04-20 14:29:05 -070064 case T_NEWLINE:
Bruce Beare1be69682010-12-26 09:55:10 -080065 state.line++;
Tom Cherryb7349902015-08-26 11:43:36 -070066 if (args.empty()) {
67 break;
Colin Cross6310a822010-04-20 14:29:05 -070068 }
Tom Cherryb7349902015-08-26 11:43:36 -070069 if (section_parsers_.count(args[0])) {
70 if (section_parser) {
71 section_parser->EndSection();
72 }
73 section_parser = section_parsers_[args[0]].get();
74 std::string ret_err;
75 if (!section_parser->ParseSection(args, &ret_err)) {
76 parse_error(&state, "%s\n", ret_err.c_str());
77 section_parser = nullptr;
78 }
79 } else if (section_parser) {
80 std::string ret_err;
81 if (!section_parser->ParseLineSection(args, state.filename,
82 state.line, &ret_err)) {
83 parse_error(&state, "%s\n", ret_err.c_str());
84 }
85 }
86 args.clear();
Colin Cross6310a822010-04-20 14:29:05 -070087 break;
88 case T_TEXT:
Tom Cherryb7349902015-08-26 11:43:36 -070089 args.emplace_back(state.text);
Colin Cross6310a822010-04-20 14:29:05 -070090 break;
91 }
92 }
93}
94
Tom Cherryb7349902015-08-26 11:43:36 -070095bool Parser::ParseConfigFile(const std::string& path) {
96 INFO("Parsing file %s...\n", path.c_str());
Elliott Hughesda40c002015-03-27 23:20:44 -070097 Timer t;
Elliott Hughesf682b472015-02-06 12:19:48 -080098 std::string data;
Tom Cherryb7349902015-08-26 11:43:36 -070099 if (!read_file(path.c_str(), &data)) {
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700100 return false;
Elliott Hughesf682b472015-02-06 12:19:48 -0800101 }
Colin Cross6310a822010-04-20 14:29:05 -0700102
Tom Cherryeaa3b4e2015-05-12 13:54:41 -0700103 data.push_back('\n'); // TODO: fix parse_config.
Tom Cherryb7349902015-08-26 11:43:36 -0700104 ParseData(path, data);
105 for (const auto& sp : section_parsers_) {
106 sp.second->EndFile(path);
107 }
Elliott Hughes1946d3b2015-10-09 14:03:14 -0700108
109 // Turning this on and letting the INFO logging be discarded adds 0.2s to
110 // Nexus 9 boot time, so it's disabled by default.
111 if (false) DumpState();
Elliott Hughesda40c002015-03-27 23:20:44 -0700112
Tom Cherryb7349902015-08-26 11:43:36 -0700113 NOTICE("(Parsing %s took %.2fs.)\n", path.c_str(), t.duration());
Elliott Hughese5ce30f2015-05-06 19:19:24 -0700114 return true;
Colin Cross6310a822010-04-20 14:29:05 -0700115}
116
Tom Cherryb7349902015-08-26 11:43:36 -0700117bool Parser::ParseConfigDir(const std::string& path) {
118 INFO("Parsing directory %s...\n", path.c_str());
119 std::unique_ptr<DIR, int(*)(DIR*)> config_dir(opendir(path.c_str()), closedir);
Lee Campbellf13b1b32015-07-24 16:57:14 -0700120 if (!config_dir) {
Tom Cherryb7349902015-08-26 11:43:36 -0700121 ERROR("Could not import directory '%s'\n", path.c_str());
Lee Campbellf13b1b32015-07-24 16:57:14 -0700122 return false;
123 }
124 dirent* current_file;
125 while ((current_file = readdir(config_dir.get()))) {
126 std::string current_path =
Tom Cherryb7349902015-08-26 11:43:36 -0700127 android::base::StringPrintf("%s/%s", path.c_str(), current_file->d_name);
Lee Campbellf13b1b32015-07-24 16:57:14 -0700128 // Ignore directories and only process regular files.
129 if (current_file->d_type == DT_REG) {
Tom Cherryb7349902015-08-26 11:43:36 -0700130 if (!ParseConfigFile(current_path)) {
Lee Campbellf13b1b32015-07-24 16:57:14 -0700131 ERROR("could not import file '%s'\n", current_path.c_str());
132 }
133 }
134 }
135 return true;
136}
137
Tom Cherryb7349902015-08-26 11:43:36 -0700138bool Parser::ParseConfig(const std::string& path) {
139 if (is_dir(path.c_str())) {
140 return ParseConfigDir(path);
Lee Campbellf13b1b32015-07-24 16:57:14 -0700141 }
Tom Cherryb7349902015-08-26 11:43:36 -0700142 return ParseConfigFile(path);
Lee Campbellf13b1b32015-07-24 16:57:14 -0700143}
144
Tom Cherryb7349902015-08-26 11:43:36 -0700145void Parser::DumpState() const {
146 ServiceManager::GetInstance().DumpState();
147 ActionManager::GetInstance().DumpState();
Colin Cross6310a822010-04-20 14:29:05 -0700148}