* SPDX-License-Identifier: GPL-2.0
*/
-#include "common.h"
-#include <string.h>
-#include <stdlib.h>
-#include <stdbool.h>
+#include "env.h"
#include <avr/eeprom.h>
#include "config.h"
#include "debug.h"
-#include "xmalloc.h"
#include "crc.h"
-#include "command.h"
-#include "env.h"
+#include "getopt-min.h"
#define ENV_SIZE (CONFIG_ENV_SIZE - sizeof(uint16_t) -1)
return -1;
}
- np = (char *) xmalloc(len+1);
+ np = (char *) malloc(len+1);
if (np == NULL) {
printf_P(PSTR("## Can't malloc %d bytes\n"), len+1);
return 1;
static
-int env_item_print(env_item_t *ep)
+int env_item_print(env_item_t *ep, bool mode)
{
int len;
char *ev = ep->envvar;
+ if (mode) {
+ len = printf_P(PSTR("setenv %s "), ev) - 7;
+ len += printf_P(PSTR("'%s'\n"), ev + len) - 2;
+ } else {
len = printf_P(PSTR("%s="), ev);
len += printf_P(PSTR("%s\n"), ev + len);
-
+ }
return len;
}
* Returns -1 in case of error, or length of printed string
*/
static
-int env_print(const MEMX char *name)
+int env_print(const MEMX char *name, bool mode)
{
int len = -1;
env_item_t *ep = envlist_search(name);
if (ep != NULL)
- len = env_item_print(ep);
+ len = env_item_print(ep, mode);
} else { /* print whole list */
len = 0;
for (int i = 0 ; i < entrycount; i++) {
- len += env_item_print(&env_list[i]);
+ len += env_item_print(&env_list[i], mode);
}
}
return len;
* @return 0 if ok, 1 on error
*/
static
-command_ret_t _do_env_set(int flag, int argc, char * const argv[])
+command_ret_t _do_env_set(uint_fast8_t flag, int argc, char * const argv[])
{
int i, len;
char *name, *value, *valp, *p;
for (i = 2, len += 1; i < argc; ++i)
len += strlen(argv[i]) + 1;
- value = xmalloc(len);
+ value = malloc(len);
if (value == NULL) {
printf_P(PSTR("## Can't malloc %d bytes\n"), len);
return CMD_RET_FAILURE;
*/
}
-command_ret_t do_env_print(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+command_ret_t do_env_print(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[])
{
+ (void) cmdtp; (void) flag;
+
+ bool mode = 0;
command_ret_t rc = CMD_RET_SUCCESS;
- (void) cmdtp; (void) flag;
+ /* reset getopt() */
+ optind = 0;
+
+ int opt;
+ while ((opt = getopt(argc, argv, PSTR("s"))) != -1) {
+ switch (opt) {
+ case 's':
+ mode = 1;
+ break;
+ default: /* '?' */
+ return CMD_RET_USAGE;
+ }
+ }
- if (argc == 1) {
+ if (optind == argc) {
/* print all env vars */
- int size = env_print(NULL);
+ int size = env_print(NULL, mode);
if (size < 0)
return CMD_RET_FAILURE;
- printf_P(PSTR("\nEnvironment size: %d/%d bytes\n"),
- size, ENV_SIZE);
+ if (mode == 0)
+ printf_P(PSTR("\nEnvironment size: %d/%d bytes\n"),
+ size, ENV_SIZE);
return CMD_RET_SUCCESS;
}
/* print selected env vars */
- for (int i = 1; i < argc; ++i) {
- int rc = env_print(argv[i]);
+ while (optind < argc) {
+ int rc = env_print(argv[optind], mode);
if (rc < 0) {
- printf_P(PSTR("## Error: \"%s\" not defined\n"), argv[i]);
+ printf_P(PSTR("## Error: \"%s\" not defined\n"), argv[optind]);
rc = CMD_RET_FAILURE;
}
+ optind++;
}
return rc;
}
-command_ret_t do_env_set(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+command_ret_t do_env_set(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[])
{
(void) cmdtp;
}
-command_ret_t do_env_default(cmd_tbl_t *cmdtp, int flag,
+command_ret_t do_env_default(cmd_tbl_t *cmdtp, uint_fast8_t flag,
int argc, char * const argv[])
{
(void) cmdtp; (void) flag; (void) argc; (void) argv;
}
-command_ret_t do_env_save(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+command_ret_t do_env_save(cmd_tbl_t *cmdtp, uint_fast8_t flag, int argc, char * const argv[])
{
(void) cmdtp; (void) flag; (void) argc; (void) argv;