2 * This file is part of buxton.
4 * Copyright (C) 2013 Intel Corporation
6 * buxton is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1
9 * of the License, or (at your option) any later version.
22 #include "buxtonarray.h"
23 #include "buxtonresponse.h"
30 static char *nv(char *s)
38 bool cli_create_db(BuxtonControl *control,
39 __attribute__((unused)) BuxtonDataType type,
41 __attribute__((unused)) char *two,
42 __attribute__((unused)) char *three,
43 __attribute__((unused)) char *four)
45 BuxtonString layer_name;
48 if (!control->client.direct) {
49 printf("Unable to create db in non direct mode\n");
52 layer_name = buxton_string_pack(one);
54 ret = buxton_direct_init_db(control, &layer_name);
59 bool cli_set_label(BuxtonControl *control, BuxtonDataType type,
60 char *one, char *two, char *three, char *four)
67 key = buxton_key_create(two, three, one, type);
69 key = buxton_key_create(two, NULL, one, type);
77 label = buxton_string_pack(four);
79 label = buxton_string_pack(three);
82 if (control->client.direct) {
83 ret = buxton_direct_set_label(control, (_BuxtonKey *)key, &label);
85 ret = !buxton_set_label(&control->client, key, label.value,
90 char *name = get_name(key);
91 printf("Failed to update key \'%s:%s\' label in layer '%s'\n",
99 bool cli_create_group(BuxtonControl *control, BuxtonDataType type,
100 char *one, char *two, char *three, char *four)
105 key = buxton_key_create(two, NULL, one, type);
110 if (control->client.direct) {
111 ret = buxton_direct_create_group(control, (_BuxtonKey *)key, NULL);
113 ret = !buxton_create_group(&control->client, key, NULL, NULL, true);
117 char *group = get_group(key);
118 printf("Failed to create group \'%s\' in layer '%s'\n",
122 buxton_key_free(key);
126 bool cli_remove_group(BuxtonControl *control, BuxtonDataType type,
127 char *one, char *two, char *three, char *four)
132 key = buxton_key_create(two, NULL, one, type);
137 if (control->client.direct) {
138 ret = buxton_direct_remove_group(control, (_BuxtonKey *)key, NULL);
140 ret = !buxton_remove_group(&control->client, key, NULL, NULL, true);
144 char *group = get_group(key);
145 printf("Failed to remove group \'%s\' in layer '%s'\n",
149 buxton_key_free(key);
153 bool cli_get_label(BuxtonControl *control, BuxtonDataType type,
154 char *one, char *two, char *three,
155 __attribute__((unused)) char *four)
157 /* Not yet implemented */
161 bool cli_set_value(BuxtonControl *control, BuxtonDataType type,
162 char *one, char *two, char *three, char *four)
169 memzero((void*)&set, sizeof(BuxtonData));
170 key = buxton_key_create(two, three, one, type);
176 value.length = (uint32_t)strlen(four) + 1;
181 set.store.d_string.value = value.value;
182 set.store.d_string.length = value.length;
183 if (control->client.direct) {
184 ret = buxton_direct_set_value(control,
188 ret = !buxton_set_value(&control->client, key,
189 four, NULL, NULL, true);
193 set.store.d_int32 = (int32_t)strtol(four, NULL, 10);
195 printf("Invalid int32_t value\n");
198 if (control->client.direct) {
199 ret = buxton_direct_set_value(control,
203 ret = !buxton_set_value(&control->client, key,
204 &set.store.d_int32, NULL,
209 set.store.d_uint32 = (uint32_t)strtol(value.value, NULL, 10);
211 printf("Invalid uint32_t value\n");
214 if (control->client.direct) {
215 ret = buxton_direct_set_value(control,
219 ret = !buxton_set_value(&control->client, key,
220 &set.store.d_uint32, NULL,
225 set.store.d_int64 = strtoll(value.value, NULL, 10);
227 printf("Invalid int64_t value\n");
230 if (control->client.direct) {
231 ret = buxton_direct_set_value(control,
235 ret = !buxton_set_value(&control->client, key,
236 &set.store.d_int64, NULL,
241 set.store.d_uint64 = strtoull(value.value, NULL, 10);
243 printf("Invalid uint64_t value\n");
246 if (control->client.direct) {
247 ret = buxton_direct_set_value(control,
251 ret = !buxton_set_value(&control->client, key,
252 &set.store.d_uint64, NULL,
257 set.store.d_float = strtof(value.value, NULL);
259 printf("Invalid float value\n");
262 if (control->client.direct) {
263 ret = buxton_direct_set_value(control,
267 ret = !buxton_set_value(&control->client, key,
268 &set.store.d_float, NULL,
273 set.store.d_double = strtod(value.value, NULL);
275 printf("Invalid double value\n");
278 if (control->client.direct) {
279 ret = buxton_direct_set_value(control,
283 ret = !buxton_set_value(&control->client, key,
284 &set.store.d_double, NULL,
289 if (strcaseeq(value.value, "true") ||
290 strcaseeq(value.value, "on") ||
291 strcaseeq(value.value, "enable") ||
292 strcaseeq(value.value, "yes") ||
293 strcaseeq(value.value, "y") ||
294 strcaseeq(value.value, "t") ||
295 strcaseeq(value.value, "1")) {
296 set.store.d_boolean = true;
297 } else if (strcaseeq(value.value, "false") ||
298 strcaseeq(value.value, "off") ||
299 strcaseeq(value.value, "disable") ||
300 strcaseeq(value.value, "no") ||
301 strcaseeq(value.value, "n") ||
302 strcaseeq(value.value, "f") ||
303 strcaseeq(value.value, "0")) {
304 set.store.d_boolean = false;
306 printf("Invalid bool value\n");
309 if (control->client.direct) {
310 ret = buxton_direct_set_value(control,
314 ret = !buxton_set_value(&control->client, key,
315 &set.store.d_boolean,
324 char *group = get_group(key);
325 char *name = get_name(key);
326 char *layer = get_layer(key);
328 printf("Failed to update key \'%s:%s\' in layer '%s'\n",
329 nv(group), nv(name), nv(layer));
338 void get_value_callback(BuxtonResponse response, void *data)
341 BuxtonData *r = (BuxtonData *)data;
344 if (buxton_response_status(response) != 0) {
348 p = buxton_response_value(response);
352 key = buxton_response_key(response);
358 switch (buxton_key_get_type(key)) {
360 r->store.d_string.value = (char *)p;
361 r->store.d_string.length = (uint32_t)strlen(r->store.d_string.value) + 1;
365 r->store.d_int32 = *(int32_t *)p;
369 r->store.d_uint32 = *(uint32_t *)p;
373 r->store.d_int64 = *(int64_t *)p;
377 r->store.d_uint64 = *(uint64_t *)p;
381 r->store.d_float = *(float *)p;
385 memcpy(&r->store.d_double, p, sizeof(double));
389 r->store.d_boolean = *(bool *)p;
396 if (buxton_key_get_type(key) != STRING) {
402 bool cli_get_value(BuxtonControl *control, BuxtonDataType type,
403 char *one, char *two, char *three, __attribute__((unused)) char * four)
407 _cleanup_free_ char *prefix = NULL;
408 _cleanup_free_ char *group = NULL;
409 _cleanup_free_ char *name = NULL;
415 memzero((void*)&get, sizeof(BuxtonData));
417 key = buxton_key_create(two, three, one, type);
418 r = asprintf(&prefix, "[%s] ", one);
423 key = buxton_key_create(one, two, NULL, type);
424 r = asprintf(&prefix, " ");
435 if (control->client.direct) {
436 ret = buxton_direct_get_value_for_layer(control, key,
440 ret = buxton_get_value(&control->client,
446 group = get_group(key);
447 name = get_name(key);
448 printf("Requested key was not found in layer \'%s\': %s:%s\n",
449 one, nv(group), nv(name));
453 if (control->client.direct) {
454 ret_val = buxton_direct_get_value(control, key, &get, &dlabel, NULL);
459 ret = buxton_get_value(&control->client, key,
460 get_value_callback, &get,
464 group = get_group(key);
465 name = get_name(key);
466 printf("Requested key was not found: %s:%s\n", nv(group),
472 group = get_group(key);
473 name = get_name(key);
476 printf("%s%s:%s = %s\n", prefix, nv(group), nv(name),
477 get.store.d_string.value ? get.store.d_string.value : "");
480 printf("%s%s:%s = %" PRId32 "\n", prefix, nv(group),
481 nv(name), get.store.d_int32);
484 printf("%s%s:%s = %" PRIu32 "\n", prefix, nv(group),
485 nv(name), get.store.d_uint32);
488 printf("%s%s:%s = %" PRId64 "\n", prefix, nv(group),
489 nv(name), get.store.d_int64);
492 printf("%s%s:%s = %" PRIu64 "\n", prefix, nv(group),
493 nv(name), get.store.d_uint64);
496 printf("%s%s:%s = %f\n", prefix, nv(group),
497 nv(name), get.store.d_float);
500 printf("%s%s:%s = %f\n", prefix, nv(group),
501 nv(name), get.store.d_double);
504 if (get.store.d_boolean == true) {
505 printf("%s%s:%s = true\n", prefix, nv(group),
508 printf("%s%s:%s = false\n", prefix, nv(group),
512 case BUXTON_TYPE_MIN:
513 printf("Requested key was not found: %s:%s\n", nv(group),
517 printf("unknown type\n");
521 if (get.type == STRING) {
522 free(get.store.d_string.value);
527 bool cli_list_keys(BuxtonControl *control,
528 __attribute__((unused))BuxtonDataType type,
529 char *one, char *two, char *three,
530 __attribute__((unused)) char *four)
532 /* not yet implemented */
536 void unset_value_callback(BuxtonResponse response, void *data)
538 BuxtonKey key = buxton_response_key(response);
545 group = buxton_key_get_group(key);
546 name = buxton_key_get_name(key);
547 printf("unset key %s:%s\n", nv(group), nv(name));
551 buxton_key_free(key);
554 bool cli_unset_value(BuxtonControl *control,
556 char *one, char *two, char *three,
557 __attribute__((unused)) char *four)
561 key = buxton_key_create(two, three, one, type);
567 if (control->client.direct) {
568 return buxton_direct_unset_value(control, key, NULL);
570 return !buxton_unset_value(&control->client,
571 key, unset_value_callback,
576 * Editor modelines - http://www.wireshark.org/tools/modelines.html
581 * indent-tabs-mode: t
584 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
585 * :indentSize=8:tabSize=8:noTabs=false: