2
0

qapi-util.c 2.0 KB

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