summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYifan Hong2017-07-07 18:19:52 -0500
committerYifan Hong2017-07-07 18:26:02 -0500
commitc188972e09d81c36c5a2946dc015adec36819583 (patch)
tree24a63a416de881aec3734ecbcb2fb87554757aa6 /KernelConfigParser.cpp
parent6209d174da66ac6d4755e4dc0779379573ea06e9 (diff)
downloadplatform-system-libvintf-c188972e09d81c36c5a2946dc015adec36819583.tar.gz
platform-system-libvintf-c188972e09d81c36c5a2946dc015adec36819583.tar.xz
platform-system-libvintf-c188972e09d81c36c5a2946dc015adec36819583.zip
Allow KernelConfigParser to be less restrictive
Rewrite KernelConfigParser::processRemaining() to allow the following text: # CONFIG_NOT_SET is not set CONFIG_ONE=1 # 'tis a one! CONFIG_TWO=2 #'tis a two! CONFIG_THREE=3#'tis a three! CONFIG_233=233#'tis a three! CONFIG_Y=y CONFIG_YES=y#YES! CONFIG_STR=string CONFIG_HELLO=hello world! #still works CONFIG_WORLD=hello world! CONFIG_GOOD = good morning! #comments here CONFIG_MORNING = good morning! Test: libvintf_test Bug: 38324908 Change-Id: I1e8112b8cbf1f1d8a2b6b34daa9a593eff69cff2
Diffstat (limited to 'KernelConfigParser.cpp')
-rw-r--r--KernelConfigParser.cpp54
1 files changed, 31 insertions, 23 deletions
diff --git a/KernelConfigParser.cpp b/KernelConfigParser.cpp
index 1d08fca..44cbf07 100644
--- a/KernelConfigParser.cpp
+++ b/KernelConfigParser.cpp
@@ -18,6 +18,13 @@
18 18
19#include <regex> 19#include <regex>
20 20
21#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
21namespace android { 28namespace android {
22namespace vintf { 29namespace vintf {
23 30
@@ -39,43 +46,44 @@ const std::map<std::string, std::string>& KernelConfigParser::configs() const {
39 return mConfigs; 46 return mConfigs;
40} 47}
41 48
49// trim spaces between value and #, value and end of line
50std::string trimTrailingSpaces(const std::string& s) {
51 auto r = s.rbegin();
52 for (; r != s.rend() && std::isspace(*r); ++r)
53 ;
54 return std::string{s.begin(), r.base()};
55}
56
42status_t KernelConfigParser::processRemaining() { 57status_t KernelConfigParser::processRemaining() {
43 static std::regex sCommentPattern("^# (CONFIG[\\w_]+) is not set$");
44 58
45 if (mRemaining.empty()) { 59 if (mRemaining.empty()) {
46 return OK; 60 return OK;
47 } 61 }
48 62
49 if (mRemaining[0] == '#') { 63 std::smatch match;
50 if (!mProcessComments) { 64 if (std::regex_match(mRemaining, match, sKeyValuePattern)) {
65 if (mConfigs.emplace(match[1], trimTrailingSpaces(match[2])).second) {
51 return OK; 66 return OK;
52 } 67 }
53 std::smatch sm; 68 mError << "Duplicated key in configs: " << match[1] << "\n";
54 if (!std::regex_match(mRemaining, sm, sCommentPattern)) { 69 return UNKNOWN_ERROR;
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 } 70 }
65 71
66 size_t equalPos = mRemaining.find('='); 72 if (mProcessComments && std::regex_match(mRemaining, match, sNotSetPattern)) {
67 if (equalPos == std::string::npos) { 73 if (mConfigs.emplace(match[1], "n").second) {
68 mError << "Unrecognized line in configs: " << mRemaining << "\n"; 74 return OK;
75 }
76 mError << "Key " << match[1] << " is set but commented as not set"
77 << "\n";
69 return UNKNOWN_ERROR; 78 return UNKNOWN_ERROR;
70 } 79 }
71 std::string key = mRemaining.substr(0, equalPos); 80
72 std::string value = mRemaining.substr(equalPos + 1); 81 if (std::regex_match(mRemaining, match, sCommentPattern)) {
73 if (!mConfigs.emplace(std::move(key), std::move(value)).second) { 82 return OK;
74 mError << "Duplicated key in configs: " << mRemaining.substr(0, equalPos) << "\n";
75 return UNKNOWN_ERROR;
76 } 83 }
77 84
78 return OK; 85 mError << "Unrecognized line in configs: " << mRemaining << "\n";
86 return UNKNOWN_ERROR;
79} 87}
80 88
81status_t KernelConfigParser::process(const char* buf, size_t len) { 89status_t KernelConfigParser::process(const char* buf, size_t len) {