-
-
Notifications
You must be signed in to change notification settings - Fork 367
/
config.c
586 lines (542 loc) · 15.1 KB
/
config.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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
// SPDX-FileCopyrightText: 2006-2021 pancake <[email protected]>
// SPDX-License-Identifier: LGPL-3.0-only
#include "rz_config.h"
RZ_API RZ_OWN RzConfigNode *rz_config_node_new(RZ_NONNULL const char *name, RZ_NONNULL const char *value) {
rz_return_val_if_fail(RZ_STR_ISNOTEMPTY(name) && value, NULL);
RzConfigNode *node = RZ_NEW0(RzConfigNode);
if (!node) {
return NULL;
}
node->name = rz_str_dup(name);
node->value = rz_str_dup(value);
node->flags = CN_RW | CN_STR;
node->i_value = rz_num_get(NULL, value);
node->options = rz_list_new();
return node;
}
RZ_API RZ_OWN RzConfigNode *rz_config_node_clone(RZ_BORROW RzConfigNode *n) {
rz_return_val_if_fail(n, NULL);
RzConfigNode *cn = RZ_NEW0(RzConfigNode);
if (!cn) {
return NULL;
}
cn->name = rz_str_dup(n->name);
cn->desc = n->desc ? rz_str_dup(n->desc) : NULL;
cn->value = rz_str_dup(n->value ? n->value : "");
cn->i_value = n->i_value;
cn->flags = n->flags;
cn->setter = n->setter;
cn->options = rz_list_clone(n->options);
return cn;
}
RZ_API void rz_config_node_free(RZ_NULLABLE void *n) {
RzConfigNode *node = (RzConfigNode *)n;
if (!node) {
return;
}
free(node->name);
free(node->desc);
free(node->value);
rz_list_free(node->options);
free(node);
}
RZ_API RZ_BORROW RzConfigNode *rz_config_node_get(RZ_BORROW RzConfig *cfg, RZ_NONNULL const char *name) {
rz_return_val_if_fail(cfg && RZ_STR_ISNOTEMPTY(name), NULL);
return ht_sp_find(cfg->ht, name, NULL);
}
RZ_API bool rz_config_set_getter(RzConfig *cfg, const char *key, RzConfigCallback cb) {
rz_return_val_if_fail(cfg && key, false);
RzConfigNode *node = rz_config_node_get(cfg, key);
if (node) {
node->getter = cb;
return true;
}
return false;
}
RZ_API bool rz_config_set_setter(RzConfig *cfg, const char *key, RzConfigCallback cb) {
RzConfigNode *node = rz_config_node_get(cfg, key);
if (node) {
node->setter = cb;
return true;
}
return false;
}
/**
* Returns the value of the config variable of \p name as a string
*/
RZ_API RZ_BORROW const char *rz_config_get(RzConfig *cfg, RZ_NONNULL const char *name) {
rz_return_val_if_fail(cfg && RZ_STR_ISNOTEMPTY(name), NULL);
RzConfigNode *node = rz_config_node_get(cfg, name);
if (node) {
if (node->getter) {
node->getter(cfg->user, node);
}
if (rz_config_node_is_bool(node)) {
return rz_str_bool(rz_str_is_true(node->value));
}
return node->value;
} else {
RZ_LOG_DEBUG("rz_config_get: variable '%s' not found\n", name);
}
return NULL;
}
/**
* Reads the value of the config variable of \p name only and only if
* the variable is boolean, then tries to write back the inverted value.
* Returns true in case of success.
*/
RZ_API bool rz_config_toggle(RZ_BORROW RzConfig *cfg, RZ_NONNULL const char *name) {
rz_return_val_if_fail(cfg && RZ_STR_ISNOTEMPTY(name), false);
RzConfigNode *node = rz_config_node_get(cfg, name);
if (!node) {
return false;
}
if (!rz_config_node_is_bool(node)) {
RZ_LOG_DEBUG("(error: '%s' is not a boolean variable)\n", name);
return false;
}
if (rz_config_node_is_ro(node)) {
RZ_LOG_DEBUG("(error: '%s' config key is read only)\n", name);
return false;
}
(void)rz_config_set_i(cfg, name, !node->i_value);
return true;
}
/**
* Reads the value of the config variable of \p name only and only if
* the variable is integer.
*/
RZ_API ut64 rz_config_get_i(RzConfig *cfg, RZ_NONNULL const char *name) {
rz_return_val_if_fail(cfg && RZ_STR_ISNOTEMPTY(name), 0);
RzConfigNode *node = rz_config_node_get(cfg, name);
if (node) {
if (node->getter) {
node->getter(cfg->user, node);
}
if (node->i_value || !strcmp(node->value, "false")) {
return node->i_value;
}
// TODO: Remove it once the switch to `rz_config_get_b()` is complete
if (!strcmp(node->value, "true")) {
return 1;
}
return (ut64)rz_num_math(cfg->num, node->value);
}
return (ut64)0LL;
}
/**
* Reads the value of the config variable of \p name only and only if
* the variable is boolean. Returns false in case of the failure.
*/
RZ_API bool rz_config_get_b(RzConfig *cfg, RZ_NONNULL const char *name) {
rz_return_val_if_fail(cfg && RZ_STR_ISNOTEMPTY(name), false);
RzConfigNode *node = rz_config_node_get(cfg, name);
if (!node) {
return false;
}
if (!rz_config_node_is_bool(node)) {
RZ_LOG_DEBUG("(error: '%s' is not a boolean variable)\n", name);
return false;
}
return rz_str_is_true(node->value);
}
RZ_API const char *rz_config_node_type(RzConfigNode *node) {
rz_return_val_if_fail(node, "");
if (rz_config_node_is_bool(node)) {
return "bool";
}
if (rz_config_node_is_str(node)) {
return "str";
}
if (rz_config_node_is_int(node)) {
return "int";
}
return "";
}
RZ_API RZ_BORROW RzConfigNode *rz_config_set_cb(RZ_BORROW RzConfig *cfg, const char *name, const char *value, RzConfigCallback cb) {
RzConfigNode *node = rz_config_set(cfg, name, value);
if (node && (node->setter = cb)) {
if (!cb(cfg->user, node)) {
return NULL;
}
}
return node;
}
RZ_API RZ_BORROW RzConfigNode *rz_config_set_i_cb(RZ_BORROW RzConfig *cfg, const char *name, st64 ivalue, RzConfigCallback cb) {
RzConfigNode *node = rz_config_set_i(cfg, name, ivalue);
if (node && (node->setter = cb)) {
if (!node->setter(cfg->user, node)) {
return NULL;
}
}
return node;
}
static bool __is_true_or_false(const char *s) {
return s && (!rz_str_casecmp(s, "true") || !rz_str_casecmp(s, "false"));
}
/**
* Writes the boolean \p value in the config variable of \p name only and only if
* the variable is boolean.
*/
RZ_API RZ_BORROW RzConfigNode *rz_config_set_b(RZ_BORROW RzConfig *cfg, RZ_NONNULL const char *name, bool value) {
rz_return_val_if_fail(cfg && cfg->ht, NULL);
rz_return_val_if_fail(RZ_STR_ISNOTEMPTY(name), NULL);
char *ov = NULL;
ut64 oi = 0;
RzConfigNode *node = rz_config_node_get(cfg, name);
if (node) {
if (rz_config_node_is_ro(node)) {
RZ_LOG_DEBUG("(error: '%s' config key is read only)\n", name);
return node;
}
oi = node->i_value;
if (node->value) {
ov = rz_str_dup(node->value);
}
if (rz_config_node_is_bool(node)) {
node->i_value = value ? 1 : 0;
char *svalue = rz_str_dup(rz_str_bool(value));
if (svalue) {
free(node->value);
node->value = svalue;
}
} else {
RZ_LOG_ERROR("(error: '%s' is not a boolean variable)\n", name);
free(ov);
return NULL;
}
} else {
if (!cfg->lock) {
node = rz_config_node_new(name, rz_str_bool(value));
if (!node) {
node = NULL;
goto beach;
}
node->flags = CN_RW | CN_BOOL;
node->i_value = value ? 1 : 0;
ht_sp_insert(cfg->ht, node->name, node);
if (cfg->nodes) {
rz_list_append(cfg->nodes, node);
}
} else {
RZ_LOG_ERROR("(locked: no new keys can be created (%s))\n", name);
}
}
if (node && node->setter) {
if (!node->setter(cfg->user, node)) {
if (oi != UT64_MAX) {
node->i_value = oi;
}
free(node->value);
node->value = rz_str_dup(ov ? ov : "");
}
}
beach:
free(ov);
return node;
}
/* TODO: reduce number of strdups here */
/**
* Writes the string \p value in the config variable of \p name.
*/
RZ_API RZ_BORROW RzConfigNode *rz_config_set(RZ_BORROW RzConfig *cfg, RZ_NONNULL const char *name, const char *value) {
rz_return_val_if_fail(cfg && cfg->ht, NULL);
rz_return_val_if_fail(RZ_STR_ISNOTEMPTY(name), NULL);
char *ov = NULL;
ut64 oi;
RzConfigNode *node = rz_config_node_get(cfg, name);
if (node) {
if (rz_config_node_is_ro(node)) {
eprintf("(error: '%s' config key is read only)\n", name);
return node;
}
oi = node->i_value;
if (node->value) {
ov = rz_str_dup(node->value);
if (!ov) {
goto beach;
}
} else {
free(node->value);
node->value = rz_str_dup("");
}
if (rz_config_node_is_bool(node)) {
bool b = rz_str_is_true(value);
node->i_value = b ? 1 : 0;
char *value = rz_str_dup(rz_str_bool(b));
if (value) {
free(node->value);
node->value = value;
}
} else {
if (!value) {
free(node->value);
node->value = rz_str_dup("");
node->i_value = 0;
} else {
if (node->value == value) {
goto beach;
}
free(node->value);
node->value = rz_str_dup(value);
if (IS_DIGIT(*value) || (value[0] == '-' && IS_DIGIT(value[1]))) {
if (strchr(value, '/')) {
node->i_value = rz_num_get(cfg->num, value);
} else {
node->i_value = rz_num_math(cfg->num, value);
}
} else {
node->i_value = 0;
}
node->flags |= CN_INT;
}
}
} else { // Create a new RzConfigNode
oi = UT64_MAX;
if (!cfg->lock) {
node = rz_config_node_new(name, value);
if (node) {
if (__is_true_or_false(value)) {
node->flags |= CN_BOOL;
node->i_value = rz_str_is_true(value) ? 1 : 0;
}
ht_sp_insert(cfg->ht, node->name, node);
rz_list_append(cfg->nodes, node);
} else {
eprintf("rz_config_set: unable to create a new RzConfigNode\n");
}
} else {
eprintf("rz_config_set: variable '%s' not found\n", name);
}
}
if (node && node->setter) {
if (!node->setter(cfg->user, node)) {
if (oi != UT64_MAX) {
node->i_value = oi;
}
free(node->value);
node->value = rz_str_dup(ov ? ov : "");
free(ov);
return NULL;
}
}
beach:
free(ov);
return node;
}
/**
* \brief Appends the given node to the config \p cfg.
*
* \param cfg The configuration the node is appended.
* \param node The node to append.
* \return bool True if the node was successful added. False otherwise.
*/
RZ_API bool rz_config_add_node(RZ_BORROW RzConfig *cfg, RZ_OWN RzConfigNode *node) {
rz_return_val_if_fail(cfg && node, false);
if (cfg->lock) {
RZ_LOG_WARN("Config locked. Plugin config node not copied.\n");
rz_config_node_free(node);
return false;
}
ht_sp_insert(cfg->ht, node->name, node);
rz_list_append(cfg->nodes, node);
return true;
}
/* rz_config_desc takes a RzConfig and a name,
* rz_config_node_desc takes a RzConfigNode
* Both set and return node->desc */
RZ_API const char *rz_config_desc(RzConfig *cfg, RZ_NONNULL const char *name, RZ_NULLABLE const char *desc) {
rz_return_val_if_fail(RZ_STR_ISNOTEMPTY(name), NULL);
RzConfigNode *node = rz_config_node_get(cfg, name);
return rz_config_node_desc(node, desc);
}
RZ_API const char *rz_config_node_desc(RzConfigNode *node, RZ_NULLABLE const char *desc) {
rz_return_val_if_fail(node, NULL);
if (desc) {
free(node->desc);
node->desc = rz_str_dup(desc);
}
return node->desc;
}
RZ_API bool rz_config_rm(RzConfig *cfg, RZ_NONNULL const char *name) {
rz_return_val_if_fail(RZ_STR_ISNOTEMPTY(name) && cfg, false);
RzConfigNode *node = rz_config_node_get(cfg, name);
if (node) {
ht_sp_delete(cfg->ht, node->name);
rz_list_delete_data(cfg->nodes, node);
return true;
}
return false;
}
RZ_API void rz_config_node_value_format_i(RZ_OUT char *buf, size_t buf_size, const ut64 i, RZ_NULLABLE RzConfigNode *node) {
if (node && rz_config_node_is_bool(node)) {
rz_str_ncpy(buf, rz_str_bool((int)i), buf_size);
return;
}
if (i < 1024) {
snprintf(buf, buf_size, "%" PFMT64d "", i);
} else {
snprintf(buf, buf_size, "0x%08" PFMT64x "", i);
}
}
/**
* Writes the integer \p value in the config variable of \p name only and only if
* the variable is integer.
*/
RZ_API RZ_BORROW RzConfigNode *rz_config_set_i(RZ_BORROW RzConfig *cfg, RZ_NONNULL const char *name, const ut64 i) {
char buf[128], *ov = NULL;
rz_return_val_if_fail(cfg && name, NULL);
RzConfigNode *node = rz_config_node_get(cfg, name);
if (node) {
if (rz_config_node_is_ro(node)) {
node = NULL;
goto beach;
}
if (node->value) {
ov = rz_str_dup(node->value);
}
rz_config_node_value_format_i(buf, sizeof(buf), i, NULL);
free(node->value);
node->value = rz_str_dup(buf);
if (!node->value) {
node = NULL;
goto beach;
}
node->i_value = i;
} else {
if (!cfg->lock) {
rz_config_node_value_format_i(buf, sizeof(buf), i, NULL);
node = rz_config_node_new(name, buf);
if (!node) {
node = NULL;
goto beach;
}
node->flags = CN_RW | CN_INT;
node->i_value = i;
ht_sp_insert(cfg->ht, node->name, node);
if (cfg->nodes) {
rz_list_append(cfg->nodes, node);
}
} else {
RZ_LOG_ERROR("(locked: no new keys can be created (%s))\n", name);
}
}
if (node && node->setter) {
ut64 oi = node->i_value;
int ret = node->setter(cfg->user, node);
if (!ret) {
node->i_value = oi;
free(node->value);
node->value = rz_str_dup(ov ? ov : "");
}
}
beach:
free(ov);
return node;
}
static int cmp(RzConfigNode *a, RzConfigNode *b, void *user) {
return strcmp(a->name, b->name);
}
RZ_API void rz_config_lock(RZ_BORROW RzConfig *cfg, int l) {
rz_list_sort(cfg->nodes, (RzListComparator)cmp, NULL);
cfg->lock = l;
}
RZ_API bool rz_config_readonly(RZ_BORROW RzConfig *cfg, const char *key) {
RzConfigNode *n = rz_config_node_get(cfg, key);
if (n) {
n->flags |= CN_RO;
return true;
}
return false;
}
RZ_API RZ_OWN RzConfig *rz_config_new(RZ_BORROW void *user) {
RzConfig *cfg = RZ_NEW0(RzConfig);
if (!cfg) {
return NULL;
}
cfg->ht = ht_sp_new(HT_STR_DUP, NULL, NULL);
cfg->nodes = rz_list_newf((RzListFree)rz_config_node_free);
if (!cfg->nodes) {
RZ_FREE(cfg);
return NULL;
}
cfg->user = user;
cfg->num = NULL;
cfg->lock = 0;
return cfg;
}
RZ_API RZ_OWN RzConfig *rz_config_clone(RZ_BORROW RzConfig *cfg) {
rz_return_val_if_fail(cfg, NULL);
RzListIter *iter;
RzConfigNode *node;
RzConfig *c = rz_config_new(cfg->user);
if (!c) {
return NULL;
}
rz_list_foreach (cfg->nodes, iter, node) {
RzConfigNode *nn = rz_config_node_clone(node);
ht_sp_insert(c->ht, node->name, nn);
rz_list_append(c->nodes, nn);
}
c->lock = cfg->lock;
return c;
}
RZ_API void rz_config_free(RZ_OWN RzConfig *cfg) {
if (cfg) {
cfg->nodes->free = rz_config_node_free;
rz_list_free(cfg->nodes);
ht_sp_free(cfg->ht);
free(cfg);
}
}
RZ_API void rz_config_visual_hit_i(RzConfig *cfg, const char *name, int delta) {
RzConfigNode *node = rz_config_node_get(cfg, name);
if (node && rz_config_node_is_int(node)) {
(void)rz_config_set_i(cfg, name, rz_config_get_i(cfg, name) + delta);
}
}
RZ_API void rz_config_bump(RZ_BORROW RzConfig *cfg, const char *key) {
char *orig = rz_str_dup(rz_config_get(cfg, key));
if (orig) {
rz_config_set(cfg, key, orig);
free(orig);
}
}
RZ_API void rz_config_serialize(RZ_NONNULL RzConfig *config, RZ_NONNULL Sdb *db) {
RzListIter *iter;
RzConfigNode *node;
rz_list_foreach (config->nodes, iter, node) {
sdb_set(db, node->name, node->value);
}
}
static bool load_config_cb(void *user, const SdbKv *kv) {
RzConfig *config = user;
RzConfigNode *node = rz_config_node_get(config, sdbkv_key(kv));
if (node) {
rz_config_set(config, sdbkv_key(kv), sdbkv_value(kv));
}
return true;
}
RZ_API bool rz_config_unserialize(RZ_NONNULL RzConfig *config, RZ_NONNULL Sdb *db, RZ_NULLABLE char **err) {
sdb_foreach(db, load_config_cb, config);
return true;
}
/**
* \brief Sets the configuration variable and its value passed as argument
*
* \param cfg reference to RzConfig
* \param str reference the configuration variable string (eg, 'asm.arch=x86')
*/
RZ_API bool rz_config_eval(RZ_NONNULL RzConfig *cfg, RZ_NONNULL const char *str) {
rz_return_val_if_fail(str, false);
char *name = rz_str_trim_dup(str);
char *value = strchr(name, '=');
if (!value) {
free(name);
return false;
}
*value++ = 0;
rz_config_set(cfg, name, value);
free(name);
return true;
}