/*-1* Copyright 2020 Toomas Soome <[email protected]>2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6* 1. Redistributions of source code must retain the above copyright7* notice, this list of conditions and the following disclaimer.8* 2. Redistributions in binary form must reproduce the above copyright9* notice, this list of conditions and the following disclaimer in the10* documentation and/or other materials provided with the distribution.11*12* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND13* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE14* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE15* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE16* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL17* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS18* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)19* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT20* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY21* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF22* SUCH DAMAGE.23*/2425/*26* Big Theory Statement.27*28* nvstore is abstraction layer to implement data read/write to different29* types of non-volatile storage.30*31* Provides cli command 'nvostre'32*/3334#include "stand.h"35#include "nvstore.h"36#include "bootstrap.h"3738COMMAND_SET(nvstore, "nvstore", "manage non-volatile data", command_nvstore);3940static void41nvstore_usage(const char *me)42{43printf("Usage:\t%s -l\n", me);44printf("\t%s store -l\n", me);45printf("\t%s store [-t type] key value\n", me);46printf("\t%s store -g key\n", me);47printf("\t%s store -d key\n", me);48}4950/*51* Usage: nvstore -l # list stores52* nvstore store -l # list data in store53* nvstore store [-t type] key value54* nvstore store -g key # get value55* nvstore store -d key # delete key56*/57static int58command_nvstore(int argc, char *argv[])59{60int c;61bool list, get, delete;62nvstore_t *st;63char *me, *name, *type;6465me = argv[0];66optind = 1;67optreset = 1;6869list = false;70while ((c = getopt(argc, argv, "l")) != -1) {71switch (c) {72case 'l':73list = true;74break;75case '?':76default:77return (CMD_ERROR);78}79}8081argc -= optind;82argv += optind;8384if (argc == 0) {85if (list) {86if (STAILQ_EMPTY(&stores)) {87printf("No configured nvstores\n");88return (CMD_OK);89}90printf("List of configured nvstores:\n");91STAILQ_FOREACH(st, &stores, nvs_next) {92printf("\t%s\n", st->nvs_name);93}94return (CMD_OK);95}96nvstore_usage(me);97return (CMD_ERROR);98}99100if (argc == 0 || (argc != 0 && list)) {101nvstore_usage(me);102return (CMD_ERROR);103}104105st = nvstore_get_store(argv[0]);106if (st == NULL) {107nvstore_usage(me);108return (CMD_ERROR);109}110111optind = 1;112optreset = 1;113name = NULL;114type = NULL;115get = delete = false;116117while ((c = getopt(argc, argv, "d:g:lt:")) != -1) {118switch (c) {119case 'd':120if (list || get) {121nvstore_usage(me);122return (CMD_ERROR);123}124name = optarg;125delete = true;126break;127case 'g':128if (delete || list) {129nvstore_usage(me);130return (CMD_ERROR);131}132name = optarg;133get = true;134break;135case 'l':136if (delete || get) {137nvstore_usage(me);138return (CMD_ERROR);139}140list = true;141break;142case 't':143type = optarg;144break;145case '?':146default:147return (CMD_ERROR);148}149}150151argc -= optind;152argv += optind;153154if (list) {155(void) nvstore_print(st);156return (CMD_OK);157}158159if (delete && name != NULL) {160(void) nvstore_unset_var(st, name);161return (CMD_OK);162}163164if (get && name != NULL) {165char *ptr = NULL;166167if (nvstore_get_var(st, name, (void **)&ptr) == 0)168printf("%s = %s\n", name, ptr);169return (CMD_OK);170}171172if (argc == 2) {173c = nvstore_set_var_from_string(st, type, argv[0], argv[1]);174if (c != 0) {175printf("error: %s\n", strerror(c));176return (CMD_ERROR);177}178return (CMD_OK);179}180181nvstore_usage(me);182return (CMD_OK);183}184185186