KernelConfigParser.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. #include "KernelConfigParser.h"
  17. #include <regex>
  18. #define KEY "(CONFIG[\\w_]+)"
  19. #define COMMENT "(?:#.*)"
  20. static const std::regex sKeyValuePattern("^\\s*" KEY "\\s*=\\s*([^#]+)" COMMENT "?$");
  21. static const std::regex sNotSetPattern("^\\s*#\\s*" KEY " is not set\\s*$");
  22. static const std::regex sCommentPattern("^\\s*" COMMENT "$");
  23. namespace android {
  24. namespace vintf {
  25. KernelConfigParser::KernelConfigParser(bool processComments, bool relaxedFormat)
  26. : mProcessComments(processComments), mRelaxedFormat(relaxedFormat) {}
  27. status_t KernelConfigParser::finish() {
  28. return process("\n", 1 /* sizeof "\n" */);
  29. }
  30. std::stringbuf* KernelConfigParser::error() const {
  31. return mError.rdbuf();
  32. }
  33. std::map<std::string, std::string>& KernelConfigParser::configs() {
  34. return mConfigs;
  35. }
  36. const std::map<std::string, std::string>& KernelConfigParser::configs() const {
  37. return mConfigs;
  38. }
  39. // trim spaces between value and #, value and end of line
  40. std::string trimTrailingSpaces(const std::string& s) {
  41. auto r = s.rbegin();
  42. for (; r != s.rend() && std::isspace(*r); ++r)
  43. ;
  44. return std::string{s.begin(), r.base()};
  45. }
  46. status_t KernelConfigParser::processRemaining() {
  47. if (mRemaining.empty()) {
  48. return OK;
  49. }
  50. std::smatch match;
  51. if (mRelaxedFormat) {
  52. // Allow free format like " CONFIG_FOO = bar #trailing comments"
  53. if (std::regex_match(mRemaining, match, sKeyValuePattern)) {
  54. if (mConfigs.emplace(match[1], trimTrailingSpaces(match[2])).second) {
  55. return OK;
  56. }
  57. mError << "Duplicated key in configs: " << match[1] << "\n";
  58. return UNKNOWN_ERROR;
  59. }
  60. } else {
  61. // No spaces. Strictly like "CONFIG_FOO=bar"
  62. size_t equalPos = mRemaining.find('=');
  63. if (equalPos != std::string::npos) {
  64. std::string key = mRemaining.substr(0, equalPos);
  65. std::string value = mRemaining.substr(equalPos + 1);
  66. if (mConfigs.emplace(std::move(key), std::move(value)).second) {
  67. return OK;
  68. }
  69. mError << "Duplicated key in configs: " << mRemaining.substr(0, equalPos) << "\n";
  70. return UNKNOWN_ERROR;
  71. }
  72. }
  73. if (mProcessComments && std::regex_match(mRemaining, match, sNotSetPattern)) {
  74. if (mConfigs.emplace(match[1], "n").second) {
  75. return OK;
  76. }
  77. mError << "Key " << match[1] << " is set but commented as not set"
  78. << "\n";
  79. return UNKNOWN_ERROR;
  80. }
  81. if (mRelaxedFormat) {
  82. // Allow free format like " #comments here"
  83. if (std::regex_match(mRemaining, match, sCommentPattern)) {
  84. return OK;
  85. }
  86. } else {
  87. // No leading spaces before the comment
  88. if (mRemaining.at(0) == '#') {
  89. return OK;
  90. }
  91. }
  92. mError << "Unrecognized line in configs: " << mRemaining << "\n";
  93. return UNKNOWN_ERROR;
  94. }
  95. status_t KernelConfigParser::process(const char* buf, size_t len) {
  96. const char* begin = buf;
  97. const char* end = buf;
  98. const char* stop = buf + len;
  99. status_t err = OK;
  100. while (end < stop) {
  101. if (*end == '\n') {
  102. mRemaining.insert(mRemaining.size(), begin, end - begin);
  103. status_t newErr = processRemaining();
  104. if (newErr != OK && err == OK) {
  105. err = newErr;
  106. // but continue to get more
  107. }
  108. mRemaining.clear();
  109. begin = end + 1;
  110. }
  111. end++;
  112. }
  113. mRemaining.insert(mRemaining.size(), begin, end - begin);
  114. return err;
  115. }
  116. status_t KernelConfigParser::processAndFinish(const char* buf, size_t len) {
  117. status_t err = process(buf, len);
  118. if (err != OK) {
  119. return err;
  120. }
  121. return finish();
  122. }
  123. status_t KernelConfigParser::processAndFinish(const std::string& content) {
  124. return processAndFinish(content.c_str(), content.size());
  125. }
  126. } // namespace vintf
  127. } // namespace android