-
Notifications
You must be signed in to change notification settings - Fork 1
/
uci_api.c
490 lines (443 loc) · 10.6 KB
/
uci_api.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/***************************************************************************
* Copyright (C) 2017 - 2020, Lanka Hsu, <lankahsu@gmail.com>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
***************************************************************************/
#include "utilx9.h"
#include <ctype.h>
int uci_list_free(UCIX_t *uci_req, const char *u_name, const char *u_key)
{
return uci_option_del_ex(uci_req, u_name, u_key);
}
int uci_list_del_ex(UCIX_t *uci_req, const char *u_name, const char *u_key, const char *u_val)
{
int ret = -1;
if ((uci_req->uci_req) && (u_name) && (u_key))
{
struct uci_section *u_section = uci_section_get(uci_req, u_name);
if (u_section)
{
struct uci_ptr ptr =
{
.p = uci_req->uci_pkg,
.s = u_section,
.option = u_key,
.value =u_val,
};
if (UCI_OK != uci_del_list(uci_req->uci_req, &ptr))
{
//uci_perror(uci_req->uci_req, NULL);
DBG_ER_LN("uci_del_list error !!!");
}
else if (UCI_OK != uci_commit(uci_req->uci_req, &uci_req->uci_pkg, false))
{
//uci_perror(uci_req->uci_req, NULL);
DBG_ER_LN("uci_commit error !!!");
}
else
{
ret = 0;
}
}
else
{
DBG_ER_LN("uci_section_get error !!! (u_name: %s)", u_name);
}
}
return ret;
}
int uci_list_add_ex(UCIX_t *uci_req, const char *u_name, const char *u_key, const char *u_val)
{
int ret = -1;
if ((uci_req->uci_req) && (u_name) && (u_key) && (u_val))
{
struct uci_section *u_section = uci_section_get(uci_req, u_name);
if (u_section)
{
struct uci_option *o_key = uci_lookup_option(uci_req->uci_req, u_section, u_key);
if ((NULL != o_key) && (UCI_TYPE_LIST == o_key->type))
{
struct uci_element *u_item;
UCI_FOREACH_ITEM(&o_key->v.list, u_item)
{
//DBG_WN_LN("(key: %s, val: %s)", key, u_item->name);
if (SAFE_STRCMP(u_item->name, (char*)u_val) == 0)
{
// Found !!!
DBG_TR_LN("%s (key: %s, val: %s)", DBG_TXT_FOUND, u_key, u_item->name);
return 0;
}
}
}
struct uci_ptr ptr =
{
.p = uci_req->uci_pkg,
.s = u_section,
.option = u_key,
.value = u_val,
};
if (UCI_OK != uci_add_list(uci_req->uci_req, &ptr))
{
//uci_perror(uci_req->uci_req, NULL);
DBG_ER_LN("uci_set error !!!");
}
else if (UCI_OK != uci_commit(uci_req->uci_req, &uci_req->uci_pkg, false))
{
//uci_perror(uci_req->uci_req, NULL);
DBG_ER_LN("uci_commit error !!!");
}
else
{
ret = 0;
}
}
else
{
DBG_ER_LN("uci_section_get error !!! (u_name: %s)", u_name);
}
}
return ret;
}
int uci_option_del_ex(UCIX_t *uci_req, const char *u_name, const char *u_key)
{
int ret = -1;
if ((uci_req->uci_req) && (u_name) && (u_key))
{
struct uci_section *u_section = uci_section_get(uci_req, u_name);
if (u_section)
{
struct uci_ptr ptr =
{
.p = uci_req->uci_pkg,
.s = u_section,
.option = u_key,
};
if (UCI_OK != uci_delete(uci_req->uci_req, &ptr))
{
//uci_perror(uci_req->uci_req, NULL);
DBG_ER_LN("uci_delete error !!!");
}
else if (UCI_OK != uci_commit(uci_req->uci_req, &uci_req->uci_pkg, false))
{
//uci_perror(uci_req->uci_req, NULL);
DBG_ER_LN("uci_commit error !!!");
}
else
{
ret = 0;
}
}
else
{
DBG_ER_LN("uci_section_get error !!! (u_name: %s)", u_name);
}
}
return ret;
}
int uci_option_set_str(UCIX_t *uci_req, const char *u_name, const char *u_key, const char *u_val)
{
int ret = -1;
if ((uci_req->uci_req) && (u_name) && (u_key) && (u_val))
{
struct uci_section *u_section = uci_section_get(uci_req, u_name);
if (u_section)
{
struct uci_ptr ptr =
{
.p = uci_req->uci_pkg,
.o = NULL,
.s = u_section,
.option = u_key,
.value = u_val,
};
if (UCI_OK != uci_set(uci_req->uci_req, &ptr))
{
//uci_perror(uci_req->uci_req, NULL);
DBG_ER_LN("uci_set error !!!");
}
else if (UCI_OK != uci_commit(uci_req->uci_req, &uci_req->uci_pkg, false))
{
//uci_perror(uci_req->uci_req, NULL);
DBG_ER_LN("uci_commit error !!!");
}
else
{
ret = 0;
}
}
else
{
DBG_ER_LN("uci_section_get error !!! (u_name: %s)", u_name);
}
}
return ret;
}
const char *uci_option_get_str(UCIX_t *uci_req, const char *u_name, const char *u_key)
{
if ((uci_req->uci_req) && (u_name) && (u_key))
{
struct uci_section *u_section = uci_section_get(uci_req, u_name);
if (u_section)
{
return uci_lookup_option_string(uci_req->uci_req, u_section, u_key);
}
}
return NULL;
}
struct uci_option *uci_option_get_obj(UCIX_t *uci_req, const char *u_name, const char *u_key)
{
if ((uci_req->uci_req) && (u_name) && (u_key))
{
struct uci_section *u_section = uci_section_get(uci_req, u_name);
if (u_section)
{
return uci_lookup_option(uci_req->uci_req, u_section, u_key);
}
}
return NULL;
}
static bool uci_validate_str_ex(const char *str, bool is_name, bool is_package)
{
if (!*str)
{
return false;
}
for (; *str; str++)
{
unsigned char c = *str;
if (isalnum(c) || c == '_')
{
continue;
}
if (c == '-' && is_package)
{
continue;
}
if (is_name || (c < 33) || (c > 126))
{
return false;
}
}
return true;
}
struct uci_section *uci_section_get(UCIX_t *uci_req, const char *u_name)
{
if ((uci_req->uci_req) && (uci_req->uci_pkg))
{
return uci_lookup_section(uci_req->uci_req, uci_req->uci_pkg, u_name);
}
return NULL;
}
int uci_section_del(UCIX_t *uci_req, const char *u_name)
{
int ret = -1;
if ((uci_req->uci_req) && (u_name))
{
struct uci_section *u_section = uci_section_get(uci_req, u_name);
if (u_section)
{
struct uci_ptr ptr =
{
.p = uci_req->uci_pkg,
.s = u_section,
};
if (UCI_OK != uci_delete(uci_req->uci_req, &ptr))
{
//uci_perror(uci_req->uci_req, NULL);
DBG_ER_LN("uci_delete error !!!");
}
else if (UCI_OK != uci_commit(uci_req->uci_req, &uci_req->uci_pkg, false))
{
//uci_perror(uci_req->uci_req, NULL);
DBG_ER_LN("uci_commit error !!!");
}
else
{
ret = 0;
}
}
else
{
DBG_ER_LN("uci_section_get error !!! (u_name: %s)", u_name);
}
}
return ret;
}
struct uci_section *uci_section_set(UCIX_t *uci_req, const char *u_type, const char *u_name)
{
struct uci_section *u_section = NULL;
if ((u_name) && (false == uci_validate_str_ex(u_name, true, false)))
{
return u_section;
}
else if ((uci_req) && (uci_req->uci_req) && (uci_req->uci_pkg) && (u_type) && (u_name))
{
u_section = uci_section_get(uci_req, u_name);
if (u_section)
{
DBG_TR_LN("uci_section_get foind !!! (u_name: %s)", u_name);
}
else if (UCI_OK == uci_add_section(uci_req->uci_req, uci_req->uci_pkg, u_type, &u_section))
{
//DBG_ER_LN("(name: %p)", section->e.name);
if ((u_name) && (u_section->e.name))
{
SAFE_FREE(u_section->e.name);
u_section->e.name = strdup(u_name);
u_section->anonymous = false;
}
if (UCI_OK == uci_save(uci_req->uci_req, uci_req->uci_pkg))
{
uci_commit(uci_req->uci_req, &uci_req->uci_pkg, false);
}
else
{
DBG_ER_LN("uci_save error !!! (u_type: %s)", u_type);
}
}
}
return u_section;
}
void uci_show_value_ex(struct uci_option *u_option)
{
char *u_cname = u_option->section->package->e.name;
char *u_name = u_option->section->e.name;
char *u_key = u_option->e.name;
switch (u_option->type)
{
case UCI_TYPE_STRING:
{
char *u_val = u_option->v.string;
DBG_IF_LN("option (%s/%s, %s=%s)", u_cname, u_name, u_key, u_val);
}
break;
case UCI_TYPE_LIST:
{
struct uci_element *u_item;
UCI_FOREACH_ITEM(&u_option->v.list, u_item)
{
char *u_val = u_item->name;
DBG_IF_LN("list (%s/%s, %s=%s)", u_cname, u_name, u_key, u_val);
}
}
break;
default:
DBG_WN_LN("%s (type: %d)", DBG_TXT_NO_SUPPORT, u_option->type);
break;
}
}
void uci_show_option_ex(struct uci_option *u_option)
{
#if (1)
char *u_cname = u_option->section->package->e.name;
char *u_name = u_option->section->e.name;
char *u_key = u_option->e.name;
DBG_TR_LN("(u_cname: %s, u_name: %s, u_key: %s)", u_cname, u_name, u_key);
#endif
uci_show_value_ex(u_option);
}
void uci_show_section_ex(struct uci_section *u_section)
{
#if (1)
char *u_cname = u_section->package->e.name;
char *u_name = u_section->e.name;
char *u_type = u_section->type;
DBG_TR_LN("(u_cname: %s, u_type: %s, u_name: %s)", u_cname, u_type, u_name);
#endif
struct uci_element *u_item;
UCI_FOREACH_ITEM(&u_section->options, u_item)
{
uci_show_option_ex(uci_to_option(u_item));
}
}
int uci_show_package_ex(struct uci_package *u_ptr)
{
int ret = -1;
struct uci_element *u_item;
UCI_FOREACH_ITEM(&u_ptr->sections, u_item)
{
struct uci_section *u_section = uci_to_section(u_item);
uci_show_section_ex(u_section);
}
ret = 0;
goto exit_package;
exit_package:
return ret;
}
int uci_show_what_ex(UCIX_t *uci_req, char *u_what)
{
int ret = -1;
struct uci_ptr u_ptr;
if (uci_lookup_ptr(uci_req->uci_req, &u_ptr, u_what, true) != UCI_OK)
{
goto exit_what;
}
struct uci_element *u_item = u_ptr.last;
DBG_IF_LN_0("_________________________________________________________________________________\n");
DBG_TR_LN("(u_what: %s, u_item->type: %d)", u_what, u_item->type);
switch (u_item->type)
{
case UCI_TYPE_PACKAGE:
uci_show_package_ex(u_ptr.p);
break;
case UCI_TYPE_SECTION:
//uci_show_section(ptr.s);
break;
case UCI_TYPE_OPTION:
//uci_show_option(ptr.o, true);
break;
default:
/* should not happen */
goto exit_what;
}
ret = 0;
exit_what:
return ret;
}
int uci_show_configs_ex(UCIX_t *uci_req)
{
int ret = -1;
char **u_configs = NULL;
char **u_config;
if ((uci_list_configs(uci_req->uci_req, &u_configs) != UCI_OK) || !u_configs)
{
goto exit_config;
}
DBG_IF_LN_0("_________________________________________________________________________________\n");
for (u_config = u_configs; *u_config; u_config++)
{
DBG_IF_LN("(u_config: %s)", *u_config);
}
ret = 0;
exit_config:
SAFE_FREE(u_configs);
return ret;
}
void uci_close(UCIX_t *uci_req)
{
UCI_PKG_FREE(uci_req->uci_req, uci_req->uci_pkg);
uci_req_FREE(uci_req->uci_req);
}
int uci_open(UCIX_t *uci_req)
{
int ret = 0;
uci_req->uci_req = UCI_LOAD(uci_req->uci_filename, uci_req->uci_pkg);
if (uci_req->uci_req == NULL)
{
ret = -1;
goto cleanup;
}
return ret;
cleanup:
uci_close(uci_req);
return ret;
}