Yifan Hong | 587ca00 | 2017-07-05 16:02:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
| 17 | #include "KernelConfigParser.h" |
| 18 | |
| 19 | #include <regex> |
| 20 | |
Yifan Hong | c188972 | 2017-07-07 16:19:52 -0700 | [diff] [blame] | 21 | #define KEY "(CONFIG[\\w_]+)" |
| 22 | #define COMMENT "(?:#.*)" |
| 23 | |
| 24 | static const std::regex sKeyValuePattern("^\\s*" KEY "\\s*=\\s*([^#]+)" COMMENT "?$"); |
| 25 | static const std::regex sNotSetPattern("^\\s*#\\s*" KEY " is not set\\s*$"); |
| 26 | static const std::regex sCommentPattern("^\\s*" COMMENT "$"); |
| 27 | |
Yifan Hong | 587ca00 | 2017-07-05 16:02:03 -0700 | [diff] [blame] | 28 | namespace android { |
| 29 | namespace vintf { |
| 30 | |
Yifan Hong | 02e9400 | 2017-07-10 15:41:56 -0700 | [diff] [blame] | 31 | KernelConfigParser::KernelConfigParser(bool processComments, bool relaxedFormat) |
| 32 | : mProcessComments(processComments), mRelaxedFormat(relaxedFormat) {} |
Yifan Hong | 587ca00 | 2017-07-05 16:02:03 -0700 | [diff] [blame] | 33 | |
| 34 | status_t KernelConfigParser::finish() { |
| 35 | return process("\n", 1 /* sizeof "\n" */); |
| 36 | } |
| 37 | |
Yifan Hong | ae53a0e | 2017-07-07 15:19:06 -0700 | [diff] [blame] | 38 | std::stringbuf* KernelConfigParser::error() const { |
| 39 | return mError.rdbuf(); |
Yifan Hong | 587ca00 | 2017-07-05 16:02:03 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | std::map<std::string, std::string>& KernelConfigParser::configs() { |
| 43 | return mConfigs; |
| 44 | } |
| 45 | |
| 46 | const std::map<std::string, std::string>& KernelConfigParser::configs() const { |
| 47 | return mConfigs; |
| 48 | } |
| 49 | |
Yifan Hong | c188972 | 2017-07-07 16:19:52 -0700 | [diff] [blame] | 50 | // trim spaces between value and #, value and end of line |
| 51 | std::string trimTrailingSpaces(const std::string& s) { |
| 52 | auto r = s.rbegin(); |
| 53 | for (; r != s.rend() && std::isspace(*r); ++r) |
| 54 | ; |
| 55 | return std::string{s.begin(), r.base()}; |
| 56 | } |
| 57 | |
Yifan Hong | 587ca00 | 2017-07-05 16:02:03 -0700 | [diff] [blame] | 58 | status_t KernelConfigParser::processRemaining() { |
Yifan Hong | 587ca00 | 2017-07-05 16:02:03 -0700 | [diff] [blame] | 59 | |
| 60 | if (mRemaining.empty()) { |
| 61 | return OK; |
| 62 | } |
| 63 | |
Yifan Hong | c188972 | 2017-07-07 16:19:52 -0700 | [diff] [blame] | 64 | std::smatch match; |
Yifan Hong | 02e9400 | 2017-07-10 15:41:56 -0700 | [diff] [blame] | 65 | |
| 66 | if (mRelaxedFormat) { |
| 67 | // Allow free format like " CONFIG_FOO = bar #trailing comments" |
| 68 | if (std::regex_match(mRemaining, match, sKeyValuePattern)) { |
| 69 | if (mConfigs.emplace(match[1], trimTrailingSpaces(match[2])).second) { |
| 70 | return OK; |
| 71 | } |
| 72 | mError << "Duplicated key in configs: " << match[1] << "\n"; |
| 73 | return UNKNOWN_ERROR; |
Yifan Hong | 587ca00 | 2017-07-05 16:02:03 -0700 | [diff] [blame] | 74 | } |
Yifan Hong | 02e9400 | 2017-07-10 15:41:56 -0700 | [diff] [blame] | 75 | } else { |
| 76 | // No spaces. Strictly like "CONFIG_FOO=bar" |
| 77 | size_t equalPos = mRemaining.find('='); |
| 78 | if (equalPos != std::string::npos) { |
| 79 | std::string key = mRemaining.substr(0, equalPos); |
| 80 | std::string value = mRemaining.substr(equalPos + 1); |
| 81 | if (mConfigs.emplace(std::move(key), std::move(value)).second) { |
| 82 | return OK; |
| 83 | } |
| 84 | mError << "Duplicated key in configs: " << mRemaining.substr(0, equalPos) << "\n"; |
| 85 | return UNKNOWN_ERROR; |
| 86 | } |
Yifan Hong | c188972 | 2017-07-07 16:19:52 -0700 | [diff] [blame] | 87 | } |
Yifan Hong | 587ca00 | 2017-07-05 16:02:03 -0700 | [diff] [blame] | 88 | |
Yifan Hong | c188972 | 2017-07-07 16:19:52 -0700 | [diff] [blame] | 89 | if (mProcessComments && std::regex_match(mRemaining, match, sNotSetPattern)) { |
| 90 | if (mConfigs.emplace(match[1], "n").second) { |
| 91 | return OK; |
| 92 | } |
| 93 | mError << "Key " << match[1] << " is set but commented as not set" |
| 94 | << "\n"; |
| 95 | return UNKNOWN_ERROR; |
| 96 | } |
| 97 | |
Yifan Hong | 02e9400 | 2017-07-10 15:41:56 -0700 | [diff] [blame] | 98 | if (mRelaxedFormat) { |
| 99 | // Allow free format like " #comments here" |
| 100 | if (std::regex_match(mRemaining, match, sCommentPattern)) { |
| 101 | return OK; |
| 102 | } |
| 103 | } else { |
| 104 | // No leading spaces before the comment |
| 105 | if (mRemaining.at(0) == '#') { |
| 106 | return OK; |
| 107 | } |
Yifan Hong | 587ca00 | 2017-07-05 16:02:03 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Yifan Hong | c188972 | 2017-07-07 16:19:52 -0700 | [diff] [blame] | 110 | mError << "Unrecognized line in configs: " << mRemaining << "\n"; |
| 111 | return UNKNOWN_ERROR; |
Yifan Hong | 587ca00 | 2017-07-05 16:02:03 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | status_t KernelConfigParser::process(const char* buf, size_t len) { |
| 115 | const char* begin = buf; |
| 116 | const char* end = buf; |
| 117 | const char* stop = buf + len; |
| 118 | status_t err = OK; |
| 119 | while (end < stop) { |
| 120 | if (*end == '\n') { |
| 121 | mRemaining.insert(mRemaining.size(), begin, end - begin); |
| 122 | status_t newErr = processRemaining(); |
| 123 | if (newErr != OK && err == OK) { |
| 124 | err = newErr; |
| 125 | // but continue to get more |
| 126 | } |
| 127 | mRemaining.clear(); |
| 128 | begin = end + 1; |
| 129 | } |
| 130 | end++; |
| 131 | } |
| 132 | mRemaining.insert(mRemaining.size(), begin, end - begin); |
| 133 | return err; |
| 134 | } |
| 135 | |
Yifan Hong | a45f77d | 2018-12-03 16:42:52 -0800 | [diff] [blame^] | 136 | status_t KernelConfigParser::processAndFinish(const char* buf, size_t len) { |
| 137 | status_t err = process(buf, len); |
| 138 | if (err != OK) { |
| 139 | return err; |
| 140 | } |
| 141 | return finish(); |
| 142 | } |
| 143 | |
| 144 | status_t KernelConfigParser::processAndFinish(const std::string& content) { |
| 145 | return processAndFinish(content.c_str(), content.size()); |
| 146 | } |
| 147 | |
Yifan Hong | 587ca00 | 2017-07-05 16:02:03 -0700 | [diff] [blame] | 148 | } // namespace vintf |
| 149 | } // namespace android |