📄 shell.c
字号:
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include "44b.h"
//#include "../inc/44blib.h"
#include "platform.h"
#include "console.h"
//#include "../inc/rtc.h"
#include "shell.h"
//#include "../inc/flash.h"
#include "armnet.h"
#define __ROM_SIZE 0x200000
#define BIOS_BASE (__ROM_SIZE-0x10000)
#define BIOS_LOAD (__ROM_SIZE-4)
#define WAIT_TIME 0x600000
extern void NetSever(void);
typedef int (*cmdproc)(int argc, char *argv[]);
typedef struct {
char *cmd;
char *hlp;
cmdproc proc;
}CMD_STRUC;
CMD_STRUC CMD_INNER[] =
{ {"help", "show help", Help},
{"?", "= help", Help},
// {"date", "show or set current date", GetDate},
// {"time", "show or set current time", GetTime},
// {"setweek", "set weekday", SetWeek},
// {"clock", "show system running clock", SysClock},
// {"setmclk", "set system running clock", ChgSysMclk},
// {"setbaud", "set baud rate", ChgBaudRate},
{"ipcfg", "show or set IP address", SetIpAddr},
{"load", "load file to ram", LoadFile2Mem},
{"comload", "load file from serial port", LoadFromUart},
{"run", "run from sdram", RunProgram},
// {"prog", "program flash", ProgFlash},
// {"copy", "copy flash from src to dst address", CopyFlash},
{"boot", "boot from flash", BootLoader},
// {"backup", "move bios to the top of flash", BackupBios},
{"md", "show memory data", MemoryDisplay},
{"move", "move program from flash to sdram", Flash2Mem},
{NULL, NULL, NULL}
};
//extern TIME_STRUC SysTime;
//char *WeekDay[7] = {"SUN","MON", "TUES", "WED", "THURS","FRI", "SAT"};
//extern int MCLK;
extern NODE locnode;
extern unsigned int IP_ADDRESS;
extern unsigned int SERIAL_BAUD;
extern unsigned int download_addr;
extern unsigned int download_begin;
extern unsigned int download_end;
extern unsigned int download_len;
int ip[4];
unsigned int strtoul(unsigned char *s)
{
unsigned long ret;
int i;
ret = 0;
while (*s != '\0') {
if (*s >= '0' && *s <= '9')
i = *s - '0';
else if (*s >= 'a' && *s <= 'f')
i = *s - 'a' + 0xa;
else if (*s >= 'A' && *s <= 'F')
i = *s - 'A' + 0xa;
else
return -1;
ret = (ret << 4) + i;
s++;
}
return ret;
}
void ultostr(unsigned char *s, unsigned int data)
{
int i;
s[8] = 0;
for(i=7; i>=0; i--, data >>=4)
{
if((data&0xf)<=9)
s[i] = (data&0xf)+'0';
else
s[i] = (data&0xf)+'a'-0x0a;
}
}
unsigned int strtobcd(char *s)
{
unsigned long ret;
int i;
ret = 0;
while (*s != '\0') {
if (*s >= '0' && *s <= '9')
i = *s - '0';
else
return -1;
ret = (ret << 4) + i;
s++;
}
return ret;
}
unsigned int strtodec(char *str, int cnt)
{
unsigned long i, data = 0;
for(i=0; i<cnt; i++)
{
data *= 10;
if(str[i]<'0'||str[i]>'9')
return -1;
data += str[i]-'0';
}
return data;
}
/*******************************************************************************************************/
void ShellIn(void)
{
int i, j, key;
int x;
int h_i, h_j, h_jj;
char t_command[MAX_CMD_LEN];
char * command;
char H_command[MAX_CMD_HISTORY][MAX_CMD_LEN];
printf("**************************************************\n");
printf("**************************************************\n");
printf("\\>");
i = 0;
j = 0;
h_i = 0;
h_j = 0;
h_jj = 0;
for(x=0;x<MAX_CMD_HISTORY;x++)
H_command[x][0] = '\0';
command = H_command[0];
for(;;)
{
// key = Uart_GetKey();
key = UartGetch(0);
if(key)
{
if(key == ENTER_KEY)
{
int tmp;
Uart_SendByte('\n');
memcpy(t_command, command, i+1);
if(i)
{
memcpy(H_command[h_i], command, i+1);
h_i++;
h_i %= MAX_CMD_HISTORY;
h_j = h_i;
h_jj = 0;
command = H_command[h_i];
command[0] = '\0';
}
tmp = ParseCmd(t_command, i);
if(tmp<0)
printf("Bad command\n");
printf("\\>");
i = 0;
j = 0;
}
else if(key == BACK_KEY && j>0)
{
if(i==j)
{
i--;
j--;
printf("\b \b");
}
else
{
i--;
j--;
for(x=j;x<i;x++)
command[x]=command[x+1];
command[x] = ' ';
printf("\b \b");
for(x=j;x<i+1;x++)
Uart_SendByte(command[x]);
for(x=0;x<i+1-j;x++)
{
Uart_SendByte(0x1b);
Uart_SendByte(0x5b);
Uart_SendByte(LEFT_KEY);
}
}
command[i] = '\0';
}
else if(key == 0x1b)
{
key = UartGetch(0);
if(key != 0x5b) continue;
key = UartGetch(0);
if(key == RIGHT_KEY)
{
if(j==i) continue;
Uart_SendByte(0x1b);
Uart_SendByte(0x5b);
Uart_SendByte(RIGHT_KEY);
j++;
}
else if(key == LEFT_KEY)
{
if(j==0) continue;
Uart_SendByte(0x1b);
Uart_SendByte(0x5b);
Uart_SendByte(LEFT_KEY);
j--;
}
else if(key == HOME_KEY)
{
for(x=0;x<j;x++)
{
Uart_SendByte(0x1b);
Uart_SendByte(0x5b);
Uart_SendByte(LEFT_KEY);
}
j = 0;
}
else if(key == END_KEY)
{
for(x=j;x<i;x++)
{
Uart_SendByte(0x1b);
Uart_SendByte(0x5b);
Uart_SendByte(RIGHT_KEY);
}
j = i;
}
else if(key == UP_KEY)
{
int cm_len;
int tmp_hj;
if(h_jj == MAX_CMD_HISTORY - 1)
{
Uart_SendByte(BEEP_KEY);
continue;
}
tmp_hj = h_j;
if(h_j==0) h_j = MAX_CMD_HISTORY - 1;
else h_j--;
cm_len = strlen(H_command[h_j]);
if(cm_len == 0)
{
Uart_SendByte(BEEP_KEY);
h_j = tmp_hj;
continue;
}
command = H_command[h_j];
for(x=0;x<j;x++)
{ //回到行首
Uart_SendByte(0x1b);
Uart_SendByte(0x5b);
Uart_SendByte(LEFT_KEY);
}
for(x=0;x<cm_len;x++)
Uart_SendByte(command[x]);
for(;x<i;x++)
Uart_SendByte(' ');
for(;x>cm_len;x--)
{
Uart_SendByte(0x1b);
Uart_SendByte(0x5b);
Uart_SendByte(LEFT_KEY);
}
i = j = cm_len;
h_jj++;
}
else if(key == DOWN_KEY)
{
int cm_len;
if(h_jj == 0)
{
Uart_SendByte(BEEP_KEY);
continue;
}
h_j ++;
h_j %= MAX_CMD_HISTORY;
cm_len = strlen(H_command[h_j]);
command = H_command[h_j];
for(x=0;x<j;x++)
{ //回到行首
Uart_SendByte(0x1b);
Uart_SendByte(0x5b);
Uart_SendByte(LEFT_KEY);
}
for(x=0;x<cm_len;x++)
Uart_SendByte(command[x]);
for(;x<i;x++)
Uart_SendByte(' ');
for(;x>cm_len;x--)
{
Uart_SendByte(0x1b);
Uart_SendByte(0x5b);
Uart_SendByte(LEFT_KEY);
}
i = j = cm_len;
h_jj--;
}
}
else if(key>=0x20 && key<=0x7e && i<(MAX_CMD_LEN-1))
{
if(i==j)
{
command[i++] = key;
Uart_SendByte(key);
j++;
}
else
{
for(x=i;x>j;x--)
command[x] = command[x-1];
command[j] = key;
i++;
for(x=j;x<i;x++)
Uart_SendByte(command[x]);
j++;
for(x=0;x<i-j;x++)
{
Uart_SendByte(0x1b);
Uart_SendByte(0x5b);
Uart_SendByte(LEFT_KEY);
}
}
command[i] = '\0';
}
}
}
}
/*******************************************************************************************************/
int ParseCmd(char *cmdline, int cmd_len)
{
int argc, num_commands;
char *argv[MAX_ARGS];
ParseArgs(cmdline, &argc, argv);
/* only whitespace */
if(argc == 0)
return 0;
num_commands = GetCmdMatche(argv[0]);
if(num_commands<0)
return -1;
if(CMD_INNER[num_commands].proc!=NULL)
CMD_INNER[num_commands].proc(argc, argv);
return 0;
}
/*******************************************************************************************************/
void ParseArgs(char *cmdline, int *argc, char **argv)
{
#define STATE_WHITESPACE 0
#define STATE_WORD 1
char *c;
int state = STATE_WHITESPACE;
int i;
*argc = 0;
if(strlen(cmdline) == 0)
return;
/* convert all tabs into single spaces */
c = cmdline;
while(*c != '\0')
{
if(*c == '\t')
*c = ' ';
c++;
}
c = cmdline;
i = 0;
/* now find all words on the command line */
while(*c != '\0')
{
if(state == STATE_WHITESPACE)
{
if(*c != ' ')
{
argv[i] = c; //将argv[i]指向c
i++;
state = STATE_WORD;
}
}
else
{ /* state == STATE_WORD */
if(*c == ' ')
{
*c = '\0';
state = STATE_WHITESPACE;
}
}
c++;
}
*argc = i;
#undef STATE_WHITESPACE
#undef STATE_WORD
}
/*******************************************************************************************************/
int GetCmdMatche(char *cmdline)
{
int i;
for(i=0; CMD_INNER[i].cmd!=NULL; i++)
{
if(strncmp(CMD_INNER[i].cmd, cmdline, strlen(CMD_INNER[i].cmd))==0)
return i;
}
return -1;
}
/*******************************************************************************************************/
int Help(int argc, char *argv[])
{
int i;
for(i=0; CMD_INNER[i].cmd!=NULL; i++)
{
if(CMD_INNER[i].hlp!=NULL)
{
printf(CMD_INNER[i].cmd);
printf(" ------ ");
printf(CMD_INNER[i].hlp);
Uart_SendByte('\n');
}
}
return 0;
}
/*******************************************************************************************************/
int GetParameter(char *str, int cnt)
{
int i, key;
i = 0;
while(1)
{
key = UartGetch(0);
if(key)
{
if(key == ENTER_KEY)
{
str[i] = 0;
return i;
}
else if(key == BACK_KEY && i>0)
{
i--;
printf("\b \b");
}
else if(key == 0x1b)
{
UartGetch(0);
UartGetch(0);
}
else if(key>=0x20 && key<=0x7e && i<cnt)
{
str[i++] = key;
Uart_SendByte(key);
}
}
}
}
/*******************************************************************************************************/
#if 0
int SysClock(int argc, char *argv[])
{
printf("System is running @ %dHz\n", MCLK);
return 0;
}
int GetDate(int argc, char *argv[])
{
int i, error = 0;
char tmp[8];
char *str;
unsigned int year, month, day;
RtcGetTime(&SysTime);
printf("Current date is %d-%x-%x %s\n", SysTime.year, SysTime.month, SysTime.day, WeekDay[SysTime.weekday]);
if(argc<2)
return 0;
str = argv[1];
for(i=0; i<5; i++)
tmp[i] = str[i];
if(tmp[4]!='-')
error = 1;
year = strtodec(str, 4);
if(year<2000||error)
{
printf("year error!\n");
return 0;
}
str += 5;
i = 0;
if(str[++i]!='-')
if(str[++i]!='-')
error = 1;
str[i] = 0;
month = strtobcd(str);
if((month-1)>0x11||error)
{
printf("month error!\n");
return 0;
}
str += i+1;
i = 0;
if(str[++i]!=0)
if(str[++i]!=0)
error = 1;
str[i] = 0;
day = strtobcd(str);
if((day-1)>0x30||error)
{
printf("day error!\n");
return 0;
}
SysTime.year = year-2000;
SysTime.month = month;
SysTime.day = day;
RtcSetDay(&SysTime);
printf("Set date %d-%2.2x-%2.2x %s\n", year, month, day, WeekDay[SysTime.weekday]);
return 1;
}
#endif
/*******************************************************************************************************/
#if 0
int SetWeek(int argc, char *argv[])
{
int i;
int error = 0;
if(argc<2)
error = 1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -