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 | |
| 21 | namespace android { |
| 22 | namespace vintf { |
| 23 | |
| 24 | KernelConfigParser::KernelConfigParser(bool processComments) : mProcessComments(processComments) {} |
| 25 | |
| 26 | status_t KernelConfigParser::finish() { |
| 27 | return process("\n", 1 /* sizeof "\n" */); |
| 28 | } |
| 29 | |
Yifan Hong | ae53a0e | 2017-07-07 15:19:06 -0700 | [diff] [blame^] | 30 | std::stringbuf* KernelConfigParser::error() const { |
| 31 | return mError.rdbuf(); |
Yifan Hong | 587ca00 | 2017-07-05 16:02:03 -0700 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | std::map<std::string, std::string>& KernelConfigParser::configs() { |
| 35 | return mConfigs; |
| 36 | } |
| 37 | |
| 38 | const std::map<std::string, std::string>& KernelConfigParser::configs() const { |
| 39 | return mConfigs; |
| 40 | } |
| 41 | |
| 42 | status_t KernelConfigParser::processRemaining() { |
| 43 | static std::regex sCommentPattern("^# (CONFIG[\\w_]+) is not set$"); |
| 44 | |
| 45 | if (mRemaining.empty()) { |
| 46 | return OK; |
| 47 | } |
| 48 | |
| 49 | if (mRemaining[0] == '#') { |
| 50 | if (!mProcessComments) { |
| 51 | return OK; |
| 52 | } |
| 53 | std::smatch sm; |
| 54 | if (!std::regex_match(mRemaining, sm, sCommentPattern)) { |
| 55 | return OK; // ignore this comment; |
| 56 | } |
| 57 | if (!mConfigs.emplace(sm[1], "n").second) { |
| 58 | mError << "Key " << sm[1] << " is set but commented as not set" |
| 59 | << "\n"; |
| 60 | return UNKNOWN_ERROR; |
| 61 | } |
| 62 | |
| 63 | return OK; |
| 64 | } |
| 65 | |
| 66 | size_t equalPos = mRemaining.find('='); |
| 67 | if (equalPos == std::string::npos) { |
| 68 | mError << "Unrecognized line in configs: " << mRemaining << "\n"; |
| 69 | return UNKNOWN_ERROR; |
| 70 | } |
| 71 | std::string key = mRemaining.substr(0, equalPos); |
| 72 | std::string value = mRemaining.substr(equalPos + 1); |
| 73 | if (!mConfigs.emplace(std::move(key), std::move(value)).second) { |
| 74 | mError << "Duplicated key in configs: " << mRemaining.substr(0, equalPos) << "\n"; |
| 75 | return UNKNOWN_ERROR; |
| 76 | } |
| 77 | |
| 78 | return OK; |
| 79 | } |
| 80 | |
| 81 | status_t KernelConfigParser::process(const char* buf, size_t len) { |
| 82 | const char* begin = buf; |
| 83 | const char* end = buf; |
| 84 | const char* stop = buf + len; |
| 85 | status_t err = OK; |
| 86 | while (end < stop) { |
| 87 | if (*end == '\n') { |
| 88 | mRemaining.insert(mRemaining.size(), begin, end - begin); |
| 89 | status_t newErr = processRemaining(); |
| 90 | if (newErr != OK && err == OK) { |
| 91 | err = newErr; |
| 92 | // but continue to get more |
| 93 | } |
| 94 | mRemaining.clear(); |
| 95 | begin = end + 1; |
| 96 | } |
| 97 | end++; |
| 98 | } |
| 99 | mRemaining.insert(mRemaining.size(), begin, end - begin); |
| 100 | return err; |
| 101 | } |
| 102 | |
| 103 | } // namespace vintf |
| 104 | } // namespace android |