Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 1 | /* |
| 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 Campbell | f13b1b3 | 2015-07-24 16:57:14 -0700 | [diff] [blame] | 17 | #include <dirent.h> |
Elliott Hughes | 8d82ea0 | 2015-02-06 20:15:18 -0800 | [diff] [blame] | 18 | #include <errno.h> |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 19 | #include <fcntl.h> |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 20 | |
Tom Cherry | fa0c21c | 2015-07-23 17:53:11 -0700 | [diff] [blame] | 21 | #include "action.h" |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 22 | #include "init_parser.h" |
| 23 | #include "log.h" |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 24 | #include "parser.h" |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 25 | #include "service.h" |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 26 | #include "util.h" |
| 27 | |
Lee Campbell | f13b1b3 | 2015-07-24 16:57:14 -0700 | [diff] [blame] | 28 | #include <base/stringprintf.h> |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 29 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame^] | 30 | Parser::Parser() { |
Elliott Hughes | c0e919c | 2015-02-04 14:46:36 -0800 | [diff] [blame] | 31 | } |
| 32 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame^] | 33 | Parser& Parser::GetInstance() { |
| 34 | static Parser instance; |
| 35 | return instance; |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 36 | } |
| 37 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame^] | 38 | void Parser::AddSectionParser(const std::string& name, |
| 39 | std::unique_ptr<SectionParser> parser) { |
| 40 | section_parsers_[name] = std::move(parser); |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame^] | 43 | void Parser::ParseData(const std::string& filename, const std::string& data) { |
Tom Cherry | 4ad60fb | 2015-08-06 10:46:28 -0700 | [diff] [blame] | 44 | //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 Cherry | eaa3b4e | 2015-05-12 13:54:41 -0700 | [diff] [blame] | 48 | parse_state state; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame^] | 49 | state.filename = filename.c_str(); |
Bruce Beare | 1be6968 | 2010-12-26 09:55:10 -0800 | [diff] [blame] | 50 | state.line = 0; |
Tom Cherry | 4ad60fb | 2015-08-06 10:46:28 -0700 | [diff] [blame] | 51 | state.ptr = &data_copy[0]; |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 52 | state.nexttoken = 0; |
Dima Zavin | 78a1b1f | 2011-12-20 12:53:48 -0800 | [diff] [blame] | 53 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame^] | 54 | SectionParser* section_parser = nullptr; |
| 55 | std::vector<std::string> args; |
Dima Zavin | 78a1b1f | 2011-12-20 12:53:48 -0800 | [diff] [blame] | 56 | |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 57 | for (;;) { |
| 58 | switch (next_token(&state)) { |
| 59 | case T_EOF: |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame^] | 60 | if (section_parser) { |
| 61 | section_parser->EndSection(); |
| 62 | } |
| 63 | return; |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 64 | case T_NEWLINE: |
Bruce Beare | 1be6968 | 2010-12-26 09:55:10 -0800 | [diff] [blame] | 65 | state.line++; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame^] | 66 | if (args.empty()) { |
| 67 | break; |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 68 | } |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame^] | 69 | 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 Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 87 | break; |
| 88 | case T_TEXT: |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame^] | 89 | args.emplace_back(state.text); |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 90 | break; |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame^] | 95 | bool Parser::ParseConfigFile(const std::string& path) { |
| 96 | INFO("Parsing file %s...\n", path.c_str()); |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 97 | Timer t; |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 98 | std::string data; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame^] | 99 | if (!read_file(path.c_str(), &data)) { |
Elliott Hughes | e5ce30f | 2015-05-06 19:19:24 -0700 | [diff] [blame] | 100 | return false; |
Elliott Hughes | f682b47 | 2015-02-06 12:19:48 -0800 | [diff] [blame] | 101 | } |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 102 | |
Tom Cherry | eaa3b4e | 2015-05-12 13:54:41 -0700 | [diff] [blame] | 103 | data.push_back('\n'); // TODO: fix parse_config. |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame^] | 104 | ParseData(path, data); |
| 105 | for (const auto& sp : section_parsers_) { |
| 106 | sp.second->EndFile(path); |
| 107 | } |
| 108 | DumpState(); |
Elliott Hughes | da40c00 | 2015-03-27 23:20:44 -0700 | [diff] [blame] | 109 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame^] | 110 | NOTICE("(Parsing %s took %.2fs.)\n", path.c_str(), t.duration()); |
Elliott Hughes | e5ce30f | 2015-05-06 19:19:24 -0700 | [diff] [blame] | 111 | return true; |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame^] | 114 | bool Parser::ParseConfigDir(const std::string& path) { |
| 115 | INFO("Parsing directory %s...\n", path.c_str()); |
| 116 | std::unique_ptr<DIR, int(*)(DIR*)> config_dir(opendir(path.c_str()), closedir); |
Lee Campbell | f13b1b3 | 2015-07-24 16:57:14 -0700 | [diff] [blame] | 117 | if (!config_dir) { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame^] | 118 | ERROR("Could not import directory '%s'\n", path.c_str()); |
Lee Campbell | f13b1b3 | 2015-07-24 16:57:14 -0700 | [diff] [blame] | 119 | return false; |
| 120 | } |
| 121 | dirent* current_file; |
| 122 | while ((current_file = readdir(config_dir.get()))) { |
| 123 | std::string current_path = |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame^] | 124 | android::base::StringPrintf("%s/%s", path.c_str(), current_file->d_name); |
Lee Campbell | f13b1b3 | 2015-07-24 16:57:14 -0700 | [diff] [blame] | 125 | // Ignore directories and only process regular files. |
| 126 | if (current_file->d_type == DT_REG) { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame^] | 127 | if (!ParseConfigFile(current_path)) { |
Lee Campbell | f13b1b3 | 2015-07-24 16:57:14 -0700 | [diff] [blame] | 128 | ERROR("could not import file '%s'\n", current_path.c_str()); |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | return true; |
| 133 | } |
| 134 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame^] | 135 | bool Parser::ParseConfig(const std::string& path) { |
| 136 | if (is_dir(path.c_str())) { |
| 137 | return ParseConfigDir(path); |
Lee Campbell | f13b1b3 | 2015-07-24 16:57:14 -0700 | [diff] [blame] | 138 | } |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame^] | 139 | return ParseConfigFile(path); |
Lee Campbell | f13b1b3 | 2015-07-24 16:57:14 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame^] | 142 | void Parser::DumpState() const { |
| 143 | ServiceManager::GetInstance().DumpState(); |
| 144 | ActionManager::GetInstance().DumpState(); |
Colin Cross | 6310a82 | 2010-04-20 14:29:05 -0700 | [diff] [blame] | 145 | } |