2 * Copyright 2020 Toomas Soome <tsoome@me.com>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * Big Theory Statement.
29 * nvstore is abstraction layer to implement data read/write to different
30 * types of non-volatile storage.
32 * Provides cli command 'nvostre'
37 #include "bootstrap.h"
39 COMMAND_SET(nvstore, "nvstore", "manage non-volatile data", command_nvstore);
42 nvstore_usage(const char *me)
44 printf("Usage:\t%s -l\n", me);
45 printf("\t%s store -l\n", me);
46 printf("\t%s store [-t type] key value\n", me);
47 printf("\t%s store -g key\n", me);
48 printf("\t%s store -d key\n", me);
52 * Usage: nvstore -l # list stores
53 * nvstore store -l # list data in store
54 * nvstore store [-t type] key value
55 * nvstore store -g key # get value
56 * nvstore store -d key # delete key
59 command_nvstore(int argc, char *argv[])
62 bool list, get, delete;
64 char *me, *name, *type;
71 while ((c = getopt(argc, argv, "l")) != -1) {
87 if (STAILQ_EMPTY(&stores)) {
88 printf("No configured nvstores\n");
91 printf("List of configured nvstores:\n");
92 STAILQ_FOREACH(st, &stores, nvs_next) {
93 printf("\t%s\n", st->nvs_name);
101 if (argc == 0 || (argc != 0 && list)) {
106 st = nvstore_get_store(argv[0]);
116 get = delete = false;
118 while ((c = getopt(argc, argv, "d:g:lt:")) != -1) {
129 if (delete || list) {
156 (void) nvstore_print(st);
160 if (delete && name != NULL) {
161 (void) nvstore_unset_var(st, name);
165 if (get && name != NULL) {
168 if (nvstore_get_var(st, name, (void **)&ptr) == 0)
169 printf("%s = %s\n", name, ptr);
174 c = nvstore_set_var_from_string(st, type, argv[0], argv[1]);
176 printf("error: %s\n", strerror(c));