Commit a51c7ca9 authored by Tomasz Wlostowski's avatar Tomasz Wlostowski

shell: initial environment support (not tested)

parent b9e4a55c
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#define SH_MAX_LINE_LEN 80 #define SH_MAX_LINE_LEN 80
#define SH_MAX_ARGS 8 #define SH_MAX_ARGS 8
#define SH_ENVIRON_SIZE 256
/* interactive shell state definitions */ /* interactive shell state definitions */
...@@ -30,14 +31,22 @@ struct shell_cmd { ...@@ -30,14 +31,22 @@ struct shell_cmd {
extern int uart_read_byte(); 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[] = { static const struct shell_cmd cmds_list[] = {
{ "pll", cmd_pll }, { "pll", cmd_pll },
{ "gui", cmd_gui }, { "gui", cmd_gui },
{ "ver", cmd_version }, { "ver", cmd_version },
{ "stat", cmd_stat }, { "stat", cmd_stat },
{ "ptp", cmd_ptp }, { "ptp", cmd_ptp },
{ "mode", cmd_mode }, { "mode", cmd_mode },
{ "measure_t24p", cmd_measure_t24p }, { "measure_t24p", cmd_measure_t24p },
{ "set", cmd_set },
{ "env", cmd_env },
{ "saveenv", cmd_saveenv },
{ NULL, NULL } { NULL, NULL }
}; };
...@@ -46,6 +55,99 @@ static int cmd_pos = 0, cmd_len = 0; ...@@ -46,6 +55,99 @@ static int cmd_pos = 0, cmd_len = 0;
static int state = SH_PROMPT; static int state = SH_PROMPT;
static int current_key = 0; 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) static int insert(char c)
{ {
if(cmd_len >= SH_MAX_LINE_LEN) if(cmd_len >= SH_MAX_LINE_LEN)
...@@ -124,6 +226,7 @@ int shell_exec(const char *cmd) ...@@ -124,6 +226,7 @@ int shell_exec(const char *cmd)
void shell_init() void shell_init()
{ {
env_buf[0] = 0xff;
cmd_len = cmd_pos = 0; cmd_len = cmd_pos = 0;
state = SH_PROMPT; state = SH_PROMPT;
mprintf("\033[2J\033[1;1H"); mprintf("\033[2J\033[1;1H");
......
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