2
0

qapi-util.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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/error.h"
  14. #include "qemu-common.h"
  15. #include "qapi/util.h"
  16. int qapi_enum_parse(const char * const lookup[], const char *buf,
  17. int max, int def, Error **errp)
  18. {
  19. int i;
  20. if (!buf) {
  21. return def;
  22. }
  23. for (i = 0; i < max; i++) {
  24. if (!strcmp(buf, lookup[i])) {
  25. return i;
  26. }
  27. }
  28. error_setg(errp, "invalid parameter value: %s", buf);
  29. return def;
  30. }
  31. /*
  32. * Parse a valid QAPI name from @str.
  33. * A valid name consists of letters, digits, hyphen and underscore.
  34. * It may be prefixed by __RFQDN_ (downstream extension), where RFQDN
  35. * may contain only letters, digits, hyphen and period.
  36. * The special exception for enumeration names is not implemented.
  37. * See docs/qapi-code-gen.txt for more on QAPI naming rules.
  38. * Keep this consistent with scripts/qapi.py!
  39. * If @complete, the parse fails unless it consumes @str completely.
  40. * Return its length on success, -1 on failure.
  41. */
  42. int parse_qapi_name(const char *str, bool complete)
  43. {
  44. const char *p = str;
  45. if (*p == '_') { /* Downstream __RFQDN_ */
  46. p++;
  47. if (*p != '_') {
  48. return -1;
  49. }
  50. while (*++p) {
  51. if (!qemu_isalnum(*p) && *p != '-' && *p != '.') {
  52. break;
  53. }
  54. }
  55. if (*p != '_') {
  56. return -1;
  57. }
  58. p++;
  59. }
  60. if (!qemu_isalpha(*p)) {
  61. return -1;
  62. }
  63. while (*++p) {
  64. if (!qemu_isalnum(*p) && *p != '-' && *p != '_') {
  65. break;
  66. }
  67. }
  68. if (complete && *p) {
  69. return -1;
  70. }
  71. return p - str;
  72. }