qapi-util.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * QAPI util functions
  3. *
  4. * Authors:
  5. * Hu Tao <hutao@cn.fujitsu.com>
  6. * Peter Lieven <pl@kamp.de>
  7. *
  8. * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
  9. * See the COPYING.LIB file in the top-level directory.
  10. *
  11. */
  12. #include "qemu/osdep.h"
  13. #include "qapi/compat-policy.h"
  14. #include "qapi/error.h"
  15. #include "qemu/ctype.h"
  16. #include "qapi/qmp/qerror.h"
  17. CompatPolicy compat_policy;
  18. static bool compat_policy_input_ok1(const char *adjective,
  19. CompatPolicyInput policy,
  20. ErrorClass error_class,
  21. const char *kind, const char *name,
  22. Error **errp)
  23. {
  24. switch (policy) {
  25. case COMPAT_POLICY_INPUT_ACCEPT:
  26. return true;
  27. case COMPAT_POLICY_INPUT_REJECT:
  28. error_set(errp, error_class, "%s %s %s disabled by policy",
  29. adjective, kind, name);
  30. return false;
  31. case COMPAT_POLICY_INPUT_CRASH:
  32. default:
  33. abort();
  34. }
  35. }
  36. bool compat_policy_input_ok(unsigned special_features,
  37. const CompatPolicy *policy,
  38. ErrorClass error_class,
  39. const char *kind, const char *name,
  40. Error **errp)
  41. {
  42. if ((special_features & 1u << QAPI_DEPRECATED)
  43. && !compat_policy_input_ok1("Deprecated",
  44. policy->deprecated_input,
  45. error_class, kind, name, errp)) {
  46. return false;
  47. }
  48. if ((special_features & (1u << QAPI_UNSTABLE))
  49. && !compat_policy_input_ok1("Unstable",
  50. policy->unstable_input,
  51. error_class, kind, name, errp)) {
  52. return false;
  53. }
  54. return true;
  55. }
  56. const char *qapi_enum_lookup(const QEnumLookup *lookup, int val)
  57. {
  58. assert(val >= 0 && val < lookup->size);
  59. return lookup->array[val];
  60. }
  61. int qapi_enum_parse(const QEnumLookup *lookup, const char *buf,
  62. int def, Error **errp)
  63. {
  64. int i;
  65. if (!buf) {
  66. return def;
  67. }
  68. for (i = 0; i < lookup->size; i++) {
  69. if (!strcmp(buf, lookup->array[i])) {
  70. return i;
  71. }
  72. }
  73. error_setg(errp, "invalid parameter value: %s", buf);
  74. return def;
  75. }
  76. bool qapi_bool_parse(const char *name, const char *value, bool *obj, Error **errp)
  77. {
  78. if (g_str_equal(value, "on") ||
  79. g_str_equal(value, "yes") ||
  80. g_str_equal(value, "true") ||
  81. g_str_equal(value, "y")) {
  82. *obj = true;
  83. return true;
  84. }
  85. if (g_str_equal(value, "off") ||
  86. g_str_equal(value, "no") ||
  87. g_str_equal(value, "false") ||
  88. g_str_equal(value, "n")) {
  89. *obj = false;
  90. return true;
  91. }
  92. error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name,
  93. "'on' or 'off'");
  94. return false;
  95. }
  96. /*
  97. * Parse a valid QAPI name from @str.
  98. * A valid name consists of letters, digits, hyphen and underscore.
  99. * It may be prefixed by __RFQDN_ (downstream extension), where RFQDN
  100. * may contain only letters, digits, hyphen and period.
  101. * The special exception for enumeration names is not implemented.
  102. * See docs/devel/qapi-code-gen.rst for more on QAPI naming rules.
  103. * Keep this consistent with scripts/qapi-gen.py!
  104. * If @complete, the parse fails unless it consumes @str completely.
  105. * Return its length on success, -1 on failure.
  106. */
  107. int parse_qapi_name(const char *str, bool complete)
  108. {
  109. const char *p = str;
  110. if (*p == '_') { /* Downstream __RFQDN_ */
  111. p++;
  112. if (*p != '_') {
  113. return -1;
  114. }
  115. while (*++p) {
  116. if (!qemu_isalnum(*p) && *p != '-' && *p != '.') {
  117. break;
  118. }
  119. }
  120. if (*p != '_') {
  121. return -1;
  122. }
  123. p++;
  124. }
  125. if (!qemu_isalpha(*p)) {
  126. return -1;
  127. }
  128. while (*++p) {
  129. if (!qemu_isalnum(*p) && *p != '-' && *p != '_') {
  130. break;
  131. }
  132. }
  133. if (complete && *p) {
  134. return -1;
  135. }
  136. return p - str;
  137. }