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