Commit de90af81 authored by Tomasz Wlostowski's avatar Tomasz Wlostowski

shell: moved environment functions to separate file

parent 07c2ccca
PLATFORM = lm32
OBJS_WRC = wrc_main.o wrc_ptp.o dev/uart.o dev/endpoint.o dev/minic.o dev/pps_gen.o dev/syscon.o dev/softpll_ng.o lib/mprintf.o dev/ep_pfilter.o dev/dna.o dev/i2c.o monitor/monitor.o dev/onewire.o dev/eeprom.o
OBJS_WRC = wrc_main.o wrc_ptp.o dev/uart.o dev/endpoint.o dev/minic.o dev/pps_gen.o dev/syscon.o dev/softpll_ng.o dev/ep_pfilter.o dev/dna.o dev/i2c.o monitor/monitor.o dev/onewire.o dev/eeprom.o
D = ptp-noposix
PTPD_CFLAGS = -ffreestanding -DPTPD_FREESTANDING -DWRPC_EXTRA_SLIM -DPTPD_MSBF -DPTPD_DBG
......@@ -34,6 +34,7 @@ OBJS_PLATFORM+=target/lm32/debug.o
include shell/shell.mk
include tests/tests.mk
include lib/lib.mk
CC=$(CROSS_COMPILE)gcc
OBJCOPY=$(CROSS_COMPILE)objcopy
......@@ -41,7 +42,7 @@ OBJDUMP=$(CROSS_COMPILE)objdump
CFLAGS= $(CFLAGS_PLATFORM) -ffunction-sections -fdata-sections -Os -Iinclude -include include/trace.h $(PTPD_CFLAGS) -Iptp-noposix/PTPWRd -I.
LDFLAGS= $(LDFLAGS_PLATFORM) -ffunction-sections -fdata-sections -Os -Iinclude
SIZE = $(CROSS_COMPILE)size
OBJS=$(OBJS_PLATFORM) $(OBJS_WRC) $(OBJS_PTPD) $(OBJS_PTPD_FREE) $(OBJS_SHELL) $(OBJS_TESTS)
OBJS=$(OBJS_PLATFORM) $(OBJS_WRC) $(OBJS_PTPD) $(OBJS_PTPD_FREE) $(OBJS_SHELL) $(OBJS_TESTS) $(OBJS_LIB)
OUTPUT=wrc
REVISION=$(shell git rev-parse HEAD)
......
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "shell.h"
#define SH_ENVIRON_SIZE 256
/* Environment-related functions */
static unsigned char env_buf[SH_ENVIRON_SIZE];
void env_init()
{
env_buf[0] = 0xff;
}
static char *_env_get(const char *var)
{
int i = 0;
while(i < SH_ENVIRON_SIZE && env_buf[i] != 0xff)
{
if(env_buf[i] == 0xaa && !strcasecmp((char*)env_buf + i + 1, var))
return (char*)env_buf + i;
i++;
}
return NULL;
}
char *env_get(const char *var)
{
char *p = _env_get(var);
if(!p) return NULL;
return p+2+strlen(p+1);
}
static int _env_end()
{
int i;
for(i=0;i<SH_ENVIRON_SIZE;i++)
if(env_buf[i] == 0xff)
return i;
}
int env_set(const char *var, const char *value)
{
unsigned char *vstart = _env_get(var), *p;
int end;
if(vstart) /* entry already present? remove current and append at the end of environment */
{
p=vstart+1;
while(*p != 0xaa && *p != 0xff) p++;
memmove(vstart, p, SH_ENVIRON_SIZE - (p - env_buf));
}
end = _env_end();
if ((end + strlen(var) + strlen(value) + 3) >= SH_ENVIRON_SIZE)
return -ENOMEM;
p = &env_buf[end];
*p++ = 0xaa;
memcpy(p, var, strlen(var) + 1); p += strlen(var) + 1;
memcpy(p, value, strlen(value) + 1); p += strlen(value) + 1;
*p++ = 0xff;
p = env_buf;
return 0;
}
int cmd_env(const char *args[])
{
unsigned char *p = env_buf;
while(*p != 0xff)
{
if(*p==0xaa)
mprintf("%s=%s\n", p+1, p+strlen(p+1)+2);
p++;
}
return 0;
}
int cmd_saveenv(const char *args[])
{
return -ENOTSUP;
}
int cmd_set(const char *args[])
{
if(!args[1])
return -EINVAL;
return env_set(args[0], args[1]);
}
......@@ -2,6 +2,8 @@
#include <string.h>
#include <errno.h>
#include "util.h"
#include "uart.h"
#include "shell.h"
#define SH_MAX_LINE_LEN 80
......@@ -29,11 +31,6 @@ struct shell_cmd {
int (*exec)(const char *args[]);
};
extern int uart_read_byte();
static int cmd_env(const char *args[]);
static int cmd_saveenv(const char *args[]);
static int cmd_set(const char *args[]);
static const struct shell_cmd cmds_list[] = {
{ "pll", cmd_pll },
......@@ -46,7 +43,8 @@ static const struct shell_cmd cmds_list[] = {
{ "set", cmd_set },
{ "env", cmd_env },
{ "saveenv", cmd_saveenv },
{ "time", cmd_time },
{ NULL, NULL }
};
......@@ -55,99 +53,6 @@ static int cmd_pos = 0, cmd_len = 0;
static int state = SH_PROMPT;
static int current_key = 0;
/* Environment-related functions */
static unsigned char env_buf[SH_ENVIRON_SIZE];
static char *_env_get(const char *var)
{
int i = 0;
while(i < SH_ENVIRON_SIZE && env_buf[i] != 0xff)
{
if(env_buf[i] == 0xaa && !strcasecmp(env_buf + i + 1, var))
return env_buf + i;
i++;
}
return NULL;
}
static char *env_get(const char *var)
{
char *p = _env_get(var);
if(!p) return NULL;
return p+2+strlen(p+1);
}
static int _env_end()
{
int i;
for(i=0;i<SH_ENVIRON_SIZE;i++)
if(env_buf[i] == 0xff)
return i;
}
static int env_set(const char *var, const char *value)
{
unsigned char *vstart = _env_get(var), *p;
int end;
if(vstart) /* entry already present? remove current and append at the end of environment */
{
p=vstart+1;
while(*p != 0xaa && *p != 0xff) p++;
memmove(vstart, p, SH_ENVIRON_SIZE - (p - env_buf));
}
end = _env_end();
if ((end + strlen(var) + strlen(value) + 3) >= SH_ENVIRON_SIZE)
return -ENOMEM;
p = &env_buf[end];
*p++ = 0xaa;
memcpy(p, var, strlen(var) + 1); p += strlen(var) + 1;
memcpy(p, value, strlen(value) + 1); p += strlen(value) + 1;
*p++ = 0xff;
p = env_buf;
return 0;
}
static int cmd_env(const char *args[])
{
unsigned char *p = env_buf;
while(*p != 0xff)
{
if(*p==0xaa)
mprintf("%s=%s\n", p+1, p+strlen(p+1)+2);
p++;
}
return 0;
}
static int cmd_saveenv(const char *args[])
{
return -ENOTSUP;
}
static int cmd_set(const char *args[])
{
if(!args[1])
return -EINVAL;
return env_set(args[0], args[1]);
}
static int insert(char c)
{
if(cmd_len >= SH_MAX_LINE_LEN)
......@@ -226,7 +131,7 @@ int shell_exec(const char *cmd)
void shell_init()
{
env_buf[0] = 0xff;
env_init();
cmd_len = cmd_pos = 0;
state = SH_PROMPT;
mprintf("\033[2J\033[1;1H");
......
......@@ -9,8 +9,18 @@ int cmd_ptp(const char *args[]);
int cmd_sfp(const char *args[]);
int cmd_mode(const char *args[]);
int cmd_measure_t24p(const char *args[]);
int cmd_time(const char *args[]);
int cmd_env(const char *args[]);
int cmd_saveenv(const char *args[]);
int cmd_set(const char *args[]);
char *env_get(const char *var);
int env_set(const char *var, const char *value);
void env_init();
int shell_exec(const char *buf);
void shell_interactive();
#endif
OBJS_SHELL = shell/shell.o \
shell/environ.o \
shell/cmd_version.o \
shell/cmd_pll.o \
shell/cmd_stat.o \
shell/cmd_ptp.o \
shell/cmd_mode.o \
shell/cmd_measure_t24p.o \
shell/cmd_time.o \
shell/cmd_gui.o
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment