-
Notifications
You must be signed in to change notification settings - Fork 11
/
uclcmd_common.c
270 lines (240 loc) · 6.83 KB
/
uclcmd_common.c
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*-
* Copyright (c) 2014-2015 Allan Jude <allanjude@freebsd.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
*/
#include <errno.h>
#include "uclcmd.h"
/*
* This method is called with the arguments of asprintf() as parameter.
* It handles error checking all instances of asprintf()
* asprintf returns -1 when it cannot allocate memory.
*/
void
uclcmd_asprintf(char ** __restrict s, char const * __restrict fmt, ...)
{
int retcode;
va_list ap;
va_start(ap, fmt);
retcode = vasprintf(s, fmt, ap);
va_end(ap);
if (retcode != -1) {
return;
}
fprintf(stderr, "ENOMEM(%d): Could not allocate memory.\n", ENOMEM);
abort();
}
char*
expand_subkeys(const ucl_object_t *obj, char *nodepath)
{
char *result = NULL;
int count = 0;
ucl_object_iter_t it = NULL;
const ucl_object_t *cur;
result = malloc(1024); /* XXX: use sbuf instead */
result[0] = '\0';
if (obj != NULL) {
/* Compile a list of the keys in the current object */
while ((cur = ucl_iterate_object(obj, &it, true))) {
if (!ucl_object_key(cur))
continue;
if (strlen(result) > (1024 - 5 - strlen(ucl_object_key(cur)))) {
result = strcat(result, " ...");
break;
}
if (count)
result = strcat(result, " ");
result = strcat(result, ucl_object_key(cur));
count++;
}
}
return result;
}
ucl_object_t*
get_object(char *selected_node)
{
char *dst_key = selected_node;
char *dst_prefix = NULL;
char *dst_frag = NULL;
ucl_object_t *parent_obj = NULL;
ucl_object_t *selected_obj = NULL;
if (strlen(dst_key) == 1 && dst_key[0] == input_sepchar) {
dst_key++;
}
dst_prefix = strdup(dst_key);
dst_frag = strrchr(dst_prefix, input_sepchar);
if (dst_frag == NULL || strlen(dst_frag) == 0) {
dst_frag = dst_key;
parent_obj = root_obj;
} else {
/*
* dst_frag is a pointer to the last period in dst_prefix, write a
* null byte to split the two strings, and then advance the pointer
* to the first byte after the period
*/
dst_frag[0] = '\0';
dst_frag++;
parent_obj = __DECONST(ucl_object_t *,
ucl_lookup_path_char(root_obj, dst_prefix, input_sepchar));
if (parent_obj == NULL) {
free(dst_prefix);
return NULL;
}
}
if (ucl_object_type(parent_obj) == UCL_ARRAY) {
selected_obj = __DECONST(ucl_object_t *,
ucl_array_find_index(parent_obj, strtoul(dst_frag, NULL, 10)));
} else {
selected_obj = __DECONST(ucl_object_t *,
ucl_object_find_key(parent_obj, dst_frag));
}
if (selected_obj == NULL) {
selected_obj = parent_obj;
}
if (debug > 0) {
fprintf(stderr, "selecting key: %s\n", dst_key);
fprintf(stderr, "intended sub-key: %s\n", dst_frag);
fprintf(stderr, "selected sub-key: %s\n", ucl_object_key(selected_obj));
}
free(dst_prefix);
return selected_obj;
}
ucl_object_t*
get_parent(char *selected_node)
{
char *dst_key = selected_node;
char *dst_prefix = NULL;
char *dst_frag = NULL;
ucl_object_t *parent_obj = NULL;
if (strlen(dst_key) == 1 && dst_key[0] == input_sepchar) {
dst_key++;
}
dst_prefix = strdup(dst_key);
dst_frag = strrchr(dst_prefix, input_sepchar);
if (dst_frag == NULL || strlen(dst_frag) == 0) {
dst_frag = dst_key;
parent_obj = root_obj;
} else {
/*
* dst_frag is a pointer to the last period in dst_prefix, write a
* null byte to split the two strings, and then advance the pointer
* to the first byte after the period
*/
dst_frag[0] = '\0';
dst_frag++;
parent_obj = __DECONST(ucl_object_t *,
ucl_lookup_path_char(root_obj, dst_prefix, input_sepchar));
}
free(dst_prefix);
if (parent_obj == NULL) {
return NULL;
}
return parent_obj;
}
void
replace_sep(char *key, int oldsep, int newsep)
{
char *ptr;
if (oldsep == newsep) return;
ptr = key;
while ((ptr = strchr(ptr, oldsep)) != NULL) {
*ptr = newsep;
ptr++;
}
}
ucl_type_t
string_to_type (const char *strtype)
{
if (strtype == NULL) {
return UCL_NULL;
}
if (strcasecmp(strtype, "object") == 0) {
return UCL_OBJECT;
} else if (strcasecmp(strtype, "array") == 0) {
return UCL_ARRAY;
} else if (strncasecmp(strtype, "int", 3) == 0 ||
strcasecmp(strtype, "number") == 0) {
return UCL_INT;
} else if (strcasecmp(strtype, "float") == 0 ||
strcasecmp(strtype, "double") == 0) {
return UCL_FLOAT;
} else if (strcasecmp(strtype, "string") == 0) {
return UCL_STRING;
} else if (strncasecmp(strtype, "bool", 4) == 0) {
return UCL_BOOLEAN;
} else if (strcasecmp(strtype, "time") == 0 ||
strncasecmp(strtype, "date", 4) == 0) {
return UCL_TIME;
} else if (strcasecmp(strtype, "userdata") == 0) {
return UCL_USERDATA;
} else if (strcasecmp(strtype, "null") == 0) {
return UCL_NULL;
}
return UCL_NULL;
}
char *
type_as_string (ucl_type_t type)
{
char *ret = NULL;
switch (type) {
case UCL_OBJECT:
uclcmd_asprintf(&ret, "UCL_OBJECT");
break;
case UCL_ARRAY:
uclcmd_asprintf(&ret, "UCL_ARRAY");
break;
case UCL_INT:
uclcmd_asprintf(&ret, "UCL_INT");
break;
case UCL_FLOAT:
uclcmd_asprintf(&ret, "UCL_FLOAT");
break;
case UCL_STRING:
uclcmd_asprintf(&ret, "UCL_STRING");
break;
case UCL_BOOLEAN:
uclcmd_asprintf(&ret, "UCL_BOOLEAN");
break;
case UCL_TIME:
uclcmd_asprintf(&ret, "UCL_TIME");
break;
case UCL_USERDATA:
uclcmd_asprintf(&ret, "UCL_USERDATA");
break;
case UCL_NULL:
default:
uclcmd_asprintf(&ret, "UCL_NULL");
break;
}
return ret;
}
char *
objtype_as_string (const ucl_object_t *obj)
{
if (obj == NULL) {
return NULL;
}
return type_as_string(ucl_object_type(obj));
}