blob: 5713f2288d1c0f0e1d69de694318a8c665d695f2 [file] [log] [blame]
Yifan Hong587ca002017-07-05 16:02:03 -07001/*
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 Hongc1889722017-07-07 16:19:52 -070021#define KEY "(CONFIG[\\w_]+)"
22#define COMMENT "(?:#.*)"
23
24static const std::regex sKeyValuePattern("^\\s*" KEY "\\s*=\\s*([^#]+)" COMMENT "?$");
25static const std::regex sNotSetPattern("^\\s*#\\s*" KEY " is not set\\s*$");
26static const std::regex sCommentPattern("^\\s*" COMMENT "$");
27
Yifan Hong587ca002017-07-05 16:02:03 -070028namespace android {
29namespace vintf {
30
Yifan Hong02e94002017-07-10 15:41:56 -070031KernelConfigParser::KernelConfigParser(bool processComments, bool relaxedFormat)
32 : mProcessComments(processComments), mRelaxedFormat(relaxedFormat) {}
Yifan Hong587ca002017-07-05 16:02:03 -070033
34status_t KernelConfigParser::finish() {
35 return process("\n", 1 /* sizeof "\n" */);
36}
37
Yifan Hongae53a0e2017-07-07 15:19:06 -070038std::stringbuf* KernelConfigParser::error() const {
39 return mError.rdbuf();
Yifan Hong587ca002017-07-05 16:02:03 -070040}
41
42std::map<std::string, std::string>& KernelConfigParser::configs() {
43 return mConfigs;
44}
45
46const std::map<std::string, std::string>& KernelConfigParser::configs() const {
47 return mConfigs;
48}
49
Yifan Hongc1889722017-07-07 16:19:52 -070050// trim spaces between value and #, value and end of line
51std::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 Hong587ca002017-07-05 16:02:03 -070058status_t KernelConfigParser::processRemaining() {
Yifan Hong587ca002017-07-05 16:02:03 -070059
60 if (mRemaining.empty()) {
61 return OK;
62 }
63
Yifan Hongc1889722017-07-07 16:19:52 -070064 std::smatch match;
Yifan Hong02e94002017-07-10 15:41:56 -070065
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 Hong587ca002017-07-05 16:02:03 -070074 }
Yifan Hong02e94002017-07-10 15:41:56 -070075 } 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 Hongc1889722017-07-07 16:19:52 -070087 }
Yifan Hong587ca002017-07-05 16:02:03 -070088
Yifan Hongc1889722017-07-07 16:19:52 -070089 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 Hong02e94002017-07-10 15:41:56 -070098 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 Hong587ca002017-07-05 16:02:03 -0700108 }
109
Yifan Hongc1889722017-07-07 16:19:52 -0700110 mError << "Unrecognized line in configs: " << mRemaining << "\n";
111 return UNKNOWN_ERROR;
Yifan Hong587ca002017-07-05 16:02:03 -0700112}
113
114status_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 Honga45f77d2018-12-03 16:42:52 -0800136status_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
144status_t KernelConfigParser::processAndFinish(const std::string& content) {
145 return processAndFinish(content.c_str(), content.size());
146}
147
Yifan Hong587ca002017-07-05 16:02:03 -0700148} // namespace vintf
149} // namespace android