redpath.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /** @file
  2. This file is cloned from DMTF libredfish library tag v1.0.0 and maintained
  3. by EDKII.
  4. //----------------------------------------------------------------------------
  5. // Copyright Notice:
  6. // Copyright 2017 Distributed Management Task Force, Inc. All rights reserved.
  7. // License: BSD 3-Clause License. For full text see link: https://github.com/DMTF/libredfish/LICENSE.md
  8. //----------------------------------------------------------------------------
  9. Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
  10. (C) Copyright 2021 Hewlett Packard Enterprise Development LP<BR>
  11. SPDX-License-Identifier: BSD-2-Clause-Patent
  12. **/
  13. #include <redpath.h>
  14. static char *
  15. getVersion (
  16. const char *path,
  17. char **end
  18. );
  19. static void
  20. parseNode (
  21. const char *path,
  22. redPathNode *node,
  23. redPathNode **end
  24. );
  25. static char *
  26. getStringTill (
  27. const char *string,
  28. const char *terminator,
  29. char **retEnd
  30. );
  31. redPathNode *
  32. parseRedPath (
  33. const char *path
  34. )
  35. {
  36. redPathNode *node;
  37. redPathNode *endNode;
  38. char *curPath;
  39. char *end;
  40. if (!path || (strlen (path) == 0)) {
  41. return NULL;
  42. }
  43. node = (redPathNode *)calloc (1, sizeof (redPathNode));
  44. if (!node) {
  45. return NULL;
  46. }
  47. if (path[0] == '/') {
  48. node->isRoot = true;
  49. if (path[1] == 'v') {
  50. node->version = getVersion (path+1, &curPath);
  51. if (curPath == NULL) {
  52. return node;
  53. }
  54. if (curPath[0] == '/') {
  55. curPath++;
  56. }
  57. node->next = parseRedPath (curPath);
  58. } else {
  59. node->next = parseRedPath (path+1);
  60. }
  61. return node;
  62. }
  63. node->isRoot = false;
  64. curPath = getStringTill (path, "/", &end);
  65. endNode = node;
  66. parseNode (curPath, node, &endNode);
  67. free (curPath);
  68. if (end != NULL) {
  69. endNode->next = parseRedPath (end+1);
  70. }
  71. return node;
  72. }
  73. void
  74. cleanupRedPath (
  75. redPathNode *node
  76. )
  77. {
  78. if (!node) {
  79. return;
  80. }
  81. cleanupRedPath (node->next);
  82. node->next = NULL;
  83. if (node->version) {
  84. free (node->version);
  85. }
  86. if (node->nodeName) {
  87. free (node->nodeName);
  88. }
  89. if (node->op) {
  90. free (node->op);
  91. }
  92. if (node->propName) {
  93. free (node->propName);
  94. }
  95. if (node->value) {
  96. free (node->value);
  97. }
  98. free (node);
  99. }
  100. static char *
  101. getVersion (
  102. const char *path,
  103. char **end
  104. )
  105. {
  106. return getStringTill (path, "/", end);
  107. }
  108. static void
  109. parseNode (
  110. const char *path,
  111. redPathNode *node,
  112. redPathNode **end
  113. )
  114. {
  115. char *indexStart;
  116. char *index;
  117. char *indexEnd;
  118. char *nodeName = getStringTill (path, "[", &indexStart);
  119. size_t tmpIndex;
  120. char *opChars;
  121. node->nodeName = nodeName;
  122. if (indexStart == NULL) {
  123. *end = node;
  124. return;
  125. }
  126. node->next = (redPathNode *)calloc (1, sizeof (redPathNode));
  127. if (!node->next) {
  128. return;
  129. }
  130. // Skip past [
  131. indexStart++;
  132. *end = node->next;
  133. index = getStringTill (indexStart, "]", NULL);
  134. tmpIndex = (size_t)strtoull (index, &indexEnd, 0);
  135. if (indexEnd != index) {
  136. free (index);
  137. node->next->index = tmpIndex;
  138. node->next->isIndex = true;
  139. return;
  140. }
  141. opChars = strpbrk (index, "<>=~");
  142. if (opChars == NULL) {
  143. // TODO handle last() and position()
  144. node->next->op = strdup ("exists");
  145. node->next->propName = index;
  146. return;
  147. }
  148. node->next->propName = (char *)malloc ((opChars - index)+1);
  149. memcpy (node->next->propName, index, (opChars - index));
  150. node->next->propName[(opChars - index)] = 0;
  151. tmpIndex = 1;
  152. while (1) {
  153. if ((opChars[tmpIndex] == '=') || (opChars[tmpIndex] == '<') || (opChars[tmpIndex] == '>') || (opChars[tmpIndex] == '~')) {
  154. tmpIndex++;
  155. continue;
  156. }
  157. break;
  158. }
  159. node->next->op = (char *)malloc (tmpIndex+1);
  160. memcpy (node->next->op, opChars, tmpIndex);
  161. node->next->op[tmpIndex] = 0;
  162. node->next->value = strdup (opChars+tmpIndex);
  163. free (index);
  164. }
  165. static char *
  166. getStringTill (
  167. const char *string,
  168. const char *terminator,
  169. char **retEnd
  170. )
  171. {
  172. char *ret;
  173. char *end;
  174. end = strstr ((char *)string, terminator);
  175. if (retEnd) {
  176. *retEnd = end;
  177. }
  178. if (end == NULL) {
  179. // No terminator
  180. return strdup (string);
  181. }
  182. ret = (char *)malloc ((end-string)+1);
  183. memcpy (ret, string, (end-string));
  184. ret[(end-string)] = 0;
  185. return ret;
  186. }