summaryrefslogtreecommitdiffstats
path: root/RedfishPkg/PrivateLibrary/RedfishLib/edk2libredfish/src/redpath.c
blob: 1fb4346c2b65f297a6be80a59d5edcfb27bc47c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/** @file
  This file is cloned from DMTF libredfish library tag v1.0.0 and maintained
  by EDKII.

//----------------------------------------------------------------------------
// Copyright Notice:
// Copyright 2017 Distributed Management Task Force, Inc. All rights reserved.
// License: BSD 3-Clause License. For full text see link: https://github.com/DMTF/libredfish/LICENSE.md
//----------------------------------------------------------------------------

  Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>
  (C) Copyright 2021 Hewlett Packard Enterprise Development LP<BR>

  SPDX-License-Identifier: BSD-2-Clause-Patent

**/
#include <redpath.h>

static char* getVersion(const char* path, char** end);
static void parseNode(const char* path, redPathNode* node, redPathNode** end);

static char* getStringTill(const char* string, const char* terminator, char** retEnd);

redPathNode* parseRedPath(const char* path)
{
    redPathNode* node;
    redPathNode* endNode;
    char* curPath;
    char* end;

    if(!path || strlen(path) == 0)
    {
        return NULL;
    }

    node = (redPathNode*)calloc(1, sizeof(redPathNode));
    if(!node)
    {
        return NULL;
    }
    if(path[0] == '/')
    {
        node->isRoot = true;
        if(path[1] == 'v')
        {
            node->version = getVersion(path+1, &curPath);
            if(curPath == NULL)
            {
                return node;
            }
            if(curPath[0] == '/')
            {
                curPath++;
            }
            node->next = parseRedPath(curPath);
        }
        else
        {
           node->next = parseRedPath(path+1);
        }
        return node;
    }
    node->isRoot = false;
    curPath = getStringTill(path, "/", &end);
    endNode = node;
    parseNode(curPath, node, &endNode);
    free(curPath);
    if(end != NULL)
    {
        endNode->next = parseRedPath(end+1);
    }
    return node;
}

void cleanupRedPath(redPathNode* node)
{
    if(!node)
    {
        return;
    }
    cleanupRedPath(node->next);
    node->next = NULL;
    if(node->version)
    {
        free(node->version);
    }
    if(node->nodeName)
    {
        free(node->nodeName);
    }
    if(node->op)
    {
        free(node->op);
    }
    if(node->propName)
    {
        free(node->propName);
    }
    if(node->value)
    {
        free(node->value);
    }
    free(node);
}

static char* getVersion(const char* path, char** end)
{
    return getStringTill(path, "/", end);
}

static void parseNode(const char* path, redPathNode* node, redPathNode** end)
{
    char* indexStart;
    char* index;
    char* indexEnd;
    char* nodeName = getStringTill(path, "[", &indexStart);
    size_t tmpIndex;
    char* opChars;

    node->nodeName = nodeName;
    if(indexStart == NULL)
    {
        *end = node;
        return;
    }
    node->next = (redPathNode*)calloc(1, sizeof(redPathNode));
    if(!node->next)
    {
        return;
    }
    //Skip past [
    indexStart++;
    *end = node->next;
    index = getStringTill(indexStart, "]", NULL);
    tmpIndex = (size_t)strtoull(index, &indexEnd, 0);
    if(indexEnd != index)
    {
        free(index);
        node->next->index = tmpIndex;
        node->next->isIndex = true;
        return;
    }
    opChars = strpbrk(index, "<>=~");
    if(opChars == NULL)
    {
        //TODO handle last() and position()
        node->next->op = strdup("exists");
        node->next->propName = index;
        return;
    }
    node->next->propName = (char*)malloc((opChars - index)+1);
    memcpy(node->next->propName, index, (opChars - index));
    node->next->propName[(opChars - index)] = 0;

    tmpIndex = 1;
    while(1)
    {
        if(opChars[tmpIndex] == '=' || opChars[tmpIndex] == '<' || opChars[tmpIndex] == '>' || opChars[tmpIndex] == '~')
        {
            tmpIndex++;
            continue;
        }
        break;
    }

    node->next->op = (char*)malloc(tmpIndex+1);
    memcpy(node->next->op, opChars, tmpIndex);
    node->next->op[tmpIndex] = 0;

    node->next->value = strdup(opChars+tmpIndex);
    free(index);
}

static char* getStringTill(const char* string, const char* terminator, char** retEnd)
{
    char* ret;
    char* end;
    end = strstr((char*)string, terminator);
    if(retEnd)
    {
        *retEnd = end;
    }
    if(end == NULL)
    {
        //No terminator
        return strdup(string);
    }
    ret = (char*)malloc((end-string)+1);
    memcpy(ret, string, (end-string));
    ret[(end-string)] = 0;
    return ret;
}