?? shell.c
字號(hào):
#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"
#include "../inc/option.h"
//#define __ROM_SIZE 0x200000
//#define BIOS_BASE (__ROM_SIZE-0x10000)
//#define BIOS_LOAD (__ROM_SIZE-2*SECTOR_SIZE-4)
//#define ENV_ADD (SECTOR_SIZE-0x100)
#define WAIT_TIME 0xf00000
extern void NetSever(void);
typedef int (*cmdproc)(int argc, char *argv[]);
typedef struct {
const char *cmd;
const 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},
{"comload", "load file from serial port", LoadFromUart},
{"load", "load file to ram", LoadFile2Mem},
{"run", "run from sdram", RunProgram},
{"comrun", "load codes from serial port and run the codes", Uart_Load_Run},
{"netrun", "load codes from NET port and run the codes", Net_Load_Run},
{"prog", "program flash", ProgFlash},
{"appprog", "load APP file to ram and AutoProgram flash", APP_Auto_Prog_Flash},
{"copy", "copy flash from src to dst address", CopyFlash},
{"move", "move program from flash to sdram", Flash2Mem},
// {"boot", "boot from flash", BootLoader},
// {"backup", "move bios to the top of flash", BackupBios},
{"biosprg", "load BIOS file to ram and AutoProgram flash", Bios_Auto_Prog_Flash},
{"md", "show memory data", MemoryDisplay},
// {"senv", "save enviroment value to flash", SaveEnv},
// {"lenv", "load enviroment value from flash", LoadEnv},
{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 GATE_ADDRESS;
extern unsigned int MASK_ADDRESS;
extern unsigned int SERIAL_BAUD;
extern unsigned char MCLK_M;
extern unsigned char MCLK_P;
extern unsigned char MCLK_S;
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 GetBoardKey(void)
//{
// unsigned int key;
// void (*fp)(void) =(void (*)(void))BIOS_LOAD;
//
// if((rPDATG&0xf0) != 0xf0)
// {
// key = rPDATG&0xf0;
// Delay(500); //延時(shí)若干個(gè)100us,消除抖動(dòng)
// if(key == (rPDATG&0xf0))
// {
// while((rPDATG&0xf0) != 0xf0);
// Uart_Printf("boot from flash...\n");
// (*fp)();
// }
// }
//}
/*******************************************************************************************************/
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(;;)
{
// GetBoardKey();
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';
}
}
}
}
/*******************************************************************************************************/
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[])
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -