📄 shell.c
字号:
/*
;************************************************************************************************************
;* 北京精仪达盛科技有限公司
;* 研 发 部
;*
;* http://www.techshine.com
;*
;*--------------------------------------------- 文件信息 ----------------------------------------------------
;*
;* 文件名称 : Shell.c
;* 文件功能 : 控制台源程序
;* 补充说明 :
;*-------------------------------------------- 最新版本信息 -------------------------------------------------
;* 修改作者 : ARM7开发小组
;* 修改日期 : 2004/04/25
;* 版本声明 : V1.0.1
;*-------------------------------------------- 历史版本信息 -------------------------------------------------
;* 文件作者 : XXXX
;* 创建日期 : XXXX
;* 版本声明 : XXXX
;*-----------------------------------------------------------------------------------------------------------
;*-----------------------------------------------------------------------------------------------------------
;************************************************************************************************************
;*/
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include "../inc/44b.h"
#include "../inc/44blib.h"
#include "../inc/rtc.h"
#include "../inc/shell.h"
#include "../inc/flash.h"
#include "../net/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];
/*
*************************************************************************************************************
- 函数名称 : void RtcSetDay(TIME_STRUC *time)
- 函数说明 : 设定日
- 输入参数 : TIME_STRUC *time
- 输出参数 : 无
*************************************************************************************************************
*/
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 RtcSetDay(TIME_STRUC *time)
- 函数说明 : 设定日
- 输入参数 : TIME_STRUC *time
- 输出参数 : 无
*************************************************************************************************************
*/
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;
}
}
/*
*************************************************************************************************************
- 函数名称 : void RtcSetDay(TIME_STRUC *time)
- 函数说明 : 设定日
- 输入参数 : TIME_STRUC *time
- 输出参数 : 无
*************************************************************************************************************
*/
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;
}
/*
*************************************************************************************************************
- 函数名称 : void RtcSetDay(TIME_STRUC *time)
- 函数说明 : 设定日
- 输入参数 : TIME_STRUC *time
- 输出参数 : 无
*************************************************************************************************************
*/
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 RtcSetDay(TIME_STRUC *time)
- 函数说明 : 设定日
- 输入参数 : TIME_STRUC *time
- 输出参数 : 无
*************************************************************************************************************
*/
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];
Uart_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();
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)
Uart_Printf("Bad command\n");
Uart_Printf("\\>");
i = 0;
j = 0;
}
else if(key == BACK_KEY && j>0)
{
if(i==j)
{
i--;
j--;
Uart_Printf("\b \b");
}
else
{
i--;
j--;
for(x=j;x<i;x++)
command[x]=command[x+1];
command[x] = ' ';
Uart_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 = Uart_Getch();
if(key != 0x5b) continue;
key = Uart_Getch();
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';
}
}
}
}
/*
*************************************************************************************************************
- 函数名称 : void RtcSetDay(TIME_STRUC *time)
- 函数说明 : 设定日
- 输入参数 : TIME_STRUC *time
- 输出参数 : 无
*************************************************************************************************************
*/
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 RtcSetDay(TIME_STRUC *time)
- 函数说明 : 设定日
- 输入参数 : TIME_STRUC *time
- 输出参数 : 无
*************************************************************************************************************
*/
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)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -