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 #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
28 namespace android {
29 namespace vintf {
30
KernelConfigParser(bool processComments,bool relaxedFormat)31 KernelConfigParser::KernelConfigParser(bool processComments, bool relaxedFormat)
32 : mProcessComments(processComments), mRelaxedFormat(relaxedFormat) {}
33
finish()34 status_t KernelConfigParser::finish() {
35 return process("\n", 1 /* sizeof "\n" */);
36 }
37
error() const38 std::stringbuf* KernelConfigParser::error() const {
39 return mError.rdbuf();
40 }
41
configs()42 std::map<std::string, std::string>& KernelConfigParser::configs() {
43 return mConfigs;
44 }
45
configs() const46 const std::map<std::string, std::string>& KernelConfigParser::configs() const {
47 return mConfigs;
48 }
49
50 // trim spaces between value and #, value and end of line
trimTrailingSpaces(const std::string & s)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
processRemaining()58 status_t KernelConfigParser::processRemaining() {
59
60 if (mRemaining.empty()) {
61 return OK;
62 }
63
64 std::smatch match;
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;
74 }
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 }
87 }
88
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
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 }
108 }
109
110 mError << "Unrecognized line in configs: " << mRemaining << "\n";
111 return UNKNOWN_ERROR;
112 }
113
process(const char * buf,size_t len)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
136 } // namespace vintf
137 } // namespace android
138