blob: c85171602cebd6b8d208b496b39ee6f26fe6283a [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
Yifan Hong587ca002017-07-05 16:02:03 -070024namespace android {
25namespace vintf {
26
Yifan Hong02e94002017-07-10 15:41:56 -070027KernelConfigParser::KernelConfigParser(bool processComments, bool relaxedFormat)
28 : mProcessComments(processComments), mRelaxedFormat(relaxedFormat) {}
Yifan Hong587ca002017-07-05 16:02:03 -070029
30status_t KernelConfigParser::finish() {
31 return process("\n", 1 /* sizeof "\n" */);
32}
33
Yifan Hongae53a0e2017-07-07 15:19:06 -070034std::stringbuf* KernelConfigParser::error() const {
35 return mError.rdbuf();
Yifan Hong587ca002017-07-05 16:02:03 -070036}
37
38std::map<std::string, std::string>& KernelConfigParser::configs() {
39 return mConfigs;
40}
41
42const std::map<std::string, std::string>& KernelConfigParser::configs() const {
43 return mConfigs;
44}
45
Yifan Hongc1889722017-07-07 16:19:52 -070046// trim spaces between value and #, value and end of line
47std::string trimTrailingSpaces(const std::string& s) {
48 auto r = s.rbegin();
49 for (; r != s.rend() && std::isspace(*r); ++r)
50 ;
51 return std::string{s.begin(), r.base()};
52}
53
Yifan Hong587ca002017-07-05 16:02:03 -070054status_t KernelConfigParser::processRemaining() {
Yifan Hong587ca002017-07-05 16:02:03 -070055 if (mRemaining.empty()) {
56 return OK;
57 }
58
Steven Morelandc16ff2b2020-02-26 17:03:37 -080059 static const std::regex sKeyValuePattern("^\\s*" KEY "\\s*=\\s*([^#]+)" COMMENT "?$");
60 static const std::regex sNotSetPattern("^\\s*#\\s*" KEY " is not set\\s*$");
61 static const std::regex sCommentPattern("^\\s*" COMMENT "$");
62
Yifan Hongc1889722017-07-07 16:19:52 -070063 std::smatch match;
Yifan Hong02e94002017-07-10 15:41:56 -070064
65 if (mRelaxedFormat) {
66 // Allow free format like " CONFIG_FOO = bar #trailing comments"
67 if (std::regex_match(mRemaining, match, sKeyValuePattern)) {
68 if (mConfigs.emplace(match[1], trimTrailingSpaces(match[2])).second) {
69 return OK;
70 }
71 mError << "Duplicated key in configs: " << match[1] << "\n";
72 return UNKNOWN_ERROR;
Yifan Hong587ca002017-07-05 16:02:03 -070073 }
Yifan Hong02e94002017-07-10 15:41:56 -070074 } else {
75 // No spaces. Strictly like "CONFIG_FOO=bar"
76 size_t equalPos = mRemaining.find('=');
77 if (equalPos != std::string::npos) {
78 std::string key = mRemaining.substr(0, equalPos);
79 std::string value = mRemaining.substr(equalPos + 1);
80 if (mConfigs.emplace(std::move(key), std::move(value)).second) {
81 return OK;
82 }
83 mError << "Duplicated key in configs: " << mRemaining.substr(0, equalPos) << "\n";
84 return UNKNOWN_ERROR;
85 }
Yifan Hongc1889722017-07-07 16:19:52 -070086 }
Yifan Hong587ca002017-07-05 16:02:03 -070087
Yifan Hongc1889722017-07-07 16:19:52 -070088 if (mProcessComments && std::regex_match(mRemaining, match, sNotSetPattern)) {
89 if (mConfigs.emplace(match[1], "n").second) {
90 return OK;
91 }
92 mError << "Key " << match[1] << " is set but commented as not set"
93 << "\n";
94 return UNKNOWN_ERROR;
95 }
96
Yifan Hong02e94002017-07-10 15:41:56 -070097 if (mRelaxedFormat) {
98 // Allow free format like " #comments here"
99 if (std::regex_match(mRemaining, match, sCommentPattern)) {
100 return OK;
101 }
102 } else {
103 // No leading spaces before the comment
104 if (mRemaining.at(0) == '#') {
105 return OK;
106 }
Yifan Hong587ca002017-07-05 16:02:03 -0700107 }
108
Yifan Hongc1889722017-07-07 16:19:52 -0700109 mError << "Unrecognized line in configs: " << mRemaining << "\n";
110 return UNKNOWN_ERROR;
Yifan Hong587ca002017-07-05 16:02:03 -0700111}
112
113status_t KernelConfigParser::process(const char* buf, size_t len) {
114 const char* begin = buf;
115 const char* end = buf;
116 const char* stop = buf + len;
117 status_t err = OK;
118 while (end < stop) {
119 if (*end == '\n') {
120 mRemaining.insert(mRemaining.size(), begin, end - begin);
121 status_t newErr = processRemaining();
122 if (newErr != OK && err == OK) {
123 err = newErr;
124 // but continue to get more
125 }
126 mRemaining.clear();
127 begin = end + 1;
128 }
129 end++;
130 }
131 mRemaining.insert(mRemaining.size(), begin, end - begin);
132 return err;
133}
134
Yifan Honga45f77d2018-12-03 16:42:52 -0800135status_t KernelConfigParser::processAndFinish(const char* buf, size_t len) {
136 status_t err = process(buf, len);
137 if (err != OK) {
138 return err;
139 }
140 return finish();
141}
142
143status_t KernelConfigParser::processAndFinish(const std::string& content) {
144 return processAndFinish(content.c_str(), content.size());
145}
146
Yifan Hong587ca002017-07-05 16:02:03 -0700147} // namespace vintf
148} // namespace android