preset.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #include "arg.h"
  2. #include "preset.h"
  3. #include "peg-parser.h"
  4. #include "log.h"
  5. #include <fstream>
  6. #include <sstream>
  7. #include <filesystem>
  8. static std::string rm_leading_dashes(const std::string & str) {
  9. size_t pos = 0;
  10. while (pos < str.size() && str[pos] == '-') {
  11. ++pos;
  12. }
  13. return str.substr(pos);
  14. }
  15. std::vector<std::string> common_preset::to_args() const {
  16. std::vector<std::string> args;
  17. for (const auto & [opt, value] : options) {
  18. args.push_back(opt.args.back()); // use the last arg as the main arg
  19. if (opt.value_hint == nullptr && opt.value_hint_2 == nullptr) {
  20. // flag option, no value
  21. if (common_arg_utils::is_falsey(value)) {
  22. // skip the flag
  23. args.pop_back();
  24. }
  25. }
  26. if (opt.value_hint != nullptr) {
  27. // single value
  28. args.push_back(value);
  29. }
  30. if (opt.value_hint != nullptr && opt.value_hint_2 != nullptr) {
  31. throw std::runtime_error(string_format(
  32. "common_preset::to_args(): option '%s' has two values, which is not supported yet",
  33. opt.args.back()
  34. ));
  35. }
  36. }
  37. return args;
  38. }
  39. std::string common_preset::to_ini() const {
  40. std::ostringstream ss;
  41. ss << "[" << name << "]\n";
  42. for (const auto & [opt, value] : options) {
  43. auto espaced_value = value;
  44. string_replace_all(espaced_value, "\n", "\\\n");
  45. ss << rm_leading_dashes(opt.args.back()) << " = ";
  46. ss << espaced_value << "\n";
  47. }
  48. ss << "\n";
  49. return ss.str();
  50. }
  51. static std::map<std::string, std::map<std::string, std::string>> parse_ini_from_file(const std::string & path) {
  52. std::map<std::string, std::map<std::string, std::string>> parsed;
  53. if (!std::filesystem::exists(path)) {
  54. throw std::runtime_error("preset file does not exist: " + path);
  55. }
  56. std::ifstream file(path);
  57. if (!file.good()) {
  58. throw std::runtime_error("failed to open server preset file: " + path);
  59. }
  60. std::string contents((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
  61. static const auto parser = build_peg_parser([](auto & p) {
  62. // newline ::= "\r\n" / "\n" / "\r"
  63. auto newline = p.rule("newline", p.literal("\r\n") | p.literal("\n") | p.literal("\r"));
  64. // ws ::= [ \t]*
  65. auto ws = p.rule("ws", p.chars("[ \t]", 0, -1));
  66. // comment ::= [;#] (!newline .)*
  67. auto comment = p.rule("comment", p.chars("[;#]", 1, 1) + p.zero_or_more(p.negate(newline) + p.any()));
  68. // eol ::= ws comment? (newline / EOF)
  69. auto eol = p.rule("eol", ws + p.optional(comment) + (newline | p.end()));
  70. // ident ::= [a-zA-Z_] [a-zA-Z0-9_.-]*
  71. auto ident = p.rule("ident", p.chars("[a-zA-Z_]", 1, 1) + p.chars("[a-zA-Z0-9_.-]", 0, -1));
  72. // value ::= (!eol-start .)*
  73. auto eol_start = p.rule("eol-start", ws + (p.chars("[;#]", 1, 1) | newline | p.end()));
  74. auto value = p.rule("value", p.zero_or_more(p.negate(eol_start) + p.any()));
  75. // header-line ::= "[" ws ident ws "]" eol
  76. auto header_line = p.rule("header-line", "[" + ws + p.tag("section-name", p.chars("[^]]")) + ws + "]" + eol);
  77. // kv-line ::= ident ws "=" ws value eol
  78. auto kv_line = p.rule("kv-line", p.tag("key", ident) + ws + "=" + ws + p.tag("value", value) + eol);
  79. // comment-line ::= ws comment (newline / EOF)
  80. auto comment_line = p.rule("comment-line", ws + comment + (newline | p.end()));
  81. // blank-line ::= ws (newline / EOF)
  82. auto blank_line = p.rule("blank-line", ws + (newline | p.end()));
  83. // line ::= header-line / kv-line / comment-line / blank-line
  84. auto line = p.rule("line", header_line | kv_line | comment_line | blank_line);
  85. // ini ::= line* EOF
  86. auto ini = p.rule("ini", p.zero_or_more(line) + p.end());
  87. return ini;
  88. });
  89. common_peg_parse_context ctx(contents);
  90. const auto result = parser.parse(ctx);
  91. if (!result.success()) {
  92. throw std::runtime_error("failed to parse server config file: " + path);
  93. }
  94. std::string current_section = COMMON_PRESET_DEFAULT_NAME;
  95. std::string current_key;
  96. ctx.ast.visit(result, [&](const auto & node) {
  97. if (node.tag == "section-name") {
  98. const std::string section = std::string(node.text);
  99. current_section = section;
  100. parsed[current_section] = {};
  101. } else if (node.tag == "key") {
  102. const std::string key = std::string(node.text);
  103. current_key = key;
  104. } else if (node.tag == "value" && !current_key.empty() && !current_section.empty()) {
  105. parsed[current_section][current_key] = std::string(node.text);
  106. current_key.clear();
  107. }
  108. });
  109. return parsed;
  110. }
  111. static std::map<std::string, common_arg> get_map_key_opt(common_params_context & ctx_params) {
  112. std::map<std::string, common_arg> mapping;
  113. for (const auto & opt : ctx_params.options) {
  114. if (opt.env != nullptr) {
  115. mapping[opt.env] = opt;
  116. }
  117. for (const auto & arg : opt.args) {
  118. mapping[rm_leading_dashes(arg)] = opt;
  119. }
  120. }
  121. return mapping;
  122. }
  123. common_presets common_presets_load(const std::string & path, common_params_context & ctx_params) {
  124. common_presets out;
  125. auto key_to_opt = get_map_key_opt(ctx_params);
  126. auto ini_data = parse_ini_from_file(path);
  127. for (auto section : ini_data) {
  128. common_preset preset;
  129. if (section.first.empty()) {
  130. preset.name = COMMON_PRESET_DEFAULT_NAME;
  131. } else {
  132. preset.name = section.first;
  133. }
  134. LOG_DBG("loading preset: %s\n", preset.name.c_str());
  135. for (const auto & [key, value] : section.second) {
  136. LOG_DBG("option: %s = %s\n", key.c_str(), value.c_str());
  137. if (key_to_opt.find(key) != key_to_opt.end()) {
  138. preset.options[key_to_opt[key]] = value;
  139. LOG_DBG("accepted option: %s = %s\n", key.c_str(), value.c_str());
  140. } else {
  141. // TODO: maybe warn about unknown key?
  142. }
  143. }
  144. out[preset.name] = preset;
  145. }
  146. return out;
  147. }