📄 shell.c
字号:
/****************************************Copyright (c)**************************************************
** Unversity of electronic technology and science of China
** school of electronic engineer
**
**--------------File Info-------------------------------------------------------------------------------
** File name: shell.c
** Last modified Date: 2006.11.02
** Last Version:
** Descriptions: command shell base on 51
**
**------------------------------------------------------------------------------------------------------
** Created by: wang zheng
** Created date: 2006.10.29
** Version:
** Descriptions:
**
**------------------------------------------------------------------------------------------------------
** Modified by: wang zheng
** Modified date:
** Version:
** Descriptions:
**
**------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
** Version:
** Descriptions:
**
********************************************************************************************************/
#define __MASTER_FILE__
#include "command_shell.h"
/************************************************************************************************
* add your function handler at here
************************************************************************************************/
cmd_tabel code TABLE [ CMD_NUM] =
{
{ 4, "help", help },
{ 8 , "ipconfig", ipconfig},
{ 5 , "login", login},
{ 4 , "exit" , exit },
{ 4 , "time" , gettime},
{ 6 , "sucess" , sucess }
};
/************************************************************************************************
*
************************************************************************************************/
char code menue[]=
" mp3 mp3 mp3mp3 mp3mp3\r\n"
" mp3 mp3 mp3 mp3 mp3 mp3\r\n"
"mp3 mp3 mp3 mp3 mp3 mp3\r\n"
"mp3 mp3 mp3 mp3mp3 mp3\r\n"
"mp3 mp3 mp3 mp3 mp3\r\n"
"mp3 mp3 mp3 mp3 mp3\r\n"
"mp3 mp3 mp3 mp3 mp3mp3\r\n"
"\r\n"
">";
char code bad_command[]=" *******bad command***********\r\n";
/************************************************************************************************
* internal function of command shell
************************************************************************************************/
void Normal_Normal(void);
void Normal_Control(void);
void Normal_Cr(void);
void Control_Control_bs(void);
void Control_Cr(void );
void Func_Execute(void); /* these are state process functions */
/*********************************************************************************************************
** 函数名称: Cmd_Shell_Init()
** 功能描述:
** 输 入:
** 输 出:
** 全局变量: curr_stat ,sig_renew
** 调用模块: Uart0_Write()
**
** 作 者: wangzheng
** 日 期: 2006.11.02
**-------------------------------------------------------------------------------------------------------
** 修改人: wangzheng
** 日 期:
**-------------------------------------------------------------------------------------------------------
** 修改人: wangzheng
** 日 期:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
void Cmd_Shell_Init( )
{
curr_stat = normal_char;
sig_renew = 0;
Uart_Write(menue); /* print the wellcome information*/
}
/*********************************************************************************************************
** 函数名称: State_Tran( )
** 功能描述: tranfer to next state of FSM
** 输 入: uint8 next_state
** 输 出:
** 全局变量: curr_stat,sig_renew
** 调用模块:
**
** 作 者: wangzheng
** 日 期: 2006.11.02
**-------------------------------------------------------------------------------------------------------
** 修改人: wangzheng
** 日 期:
**-------------------------------------------------------------------------------------------------------
** 修改人: wangzheng
** 日 期:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
void State_Tran(uint8 next_stat )
{
curr_stat = next_stat;
sig_renew = 1;
}
/*********************************************************************************************************
** 函数名称: Init_cmd_buf()
** 功能描述:
** 输 入:
** 输 出:
** 全局变量: cmd_buf
** 调用模块:
**
** 作 者: wangzheng
** 日 期: 2006.11.02
**-------------------------------------------------------------------------------------------------------
** 修改人: wangzheng
** 日 期:
**-------------------------------------------------------------------------------------------------------
** 修改人: wangzheng
** 日 期:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
void Init_Cmd_Buf(void )
{
uint8 cnt;
cmd_buf.length = 0;
for(cnt=0;cnt<MAX_CMD_LENGTH;cnt++)
cmd_buf.cmd_line[cnt]=0;
}
/*********************************************************************************************************
** 函数名称: Search_cmd();
** 功能描述: search for the matched command in TABLE
** 输 入:
** 输 出: the index of matched command in TABLE
** 全局变量:
** 调用模块:
**
** 作 者: wangzheng
** 日 期: 2006.11.02
**-------------------------------------------------------------------------------------------------------
** 修改人: wangzheng
** 日 期:
**-------------------------------------------------------------------------------------------------------
** 修改人: wangzheng
** 日 期:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
int8 Search_Cmd( )
{
uint8 temp_length=0; /* cmd length ,exclude the parametes*/
uint8 cnt=0; /* search count*/
uint8 search_buf[10]; /* buf for save the length matched cmd's index*/
uint8 search_index=0;
uint8 temp=0;
uint8 i=0;
while( (cmd_buf.cmd_line[temp_length] != '-') && (cmd_buf.cmd_line[temp_length] !=0 )
&& (temp_length<=MAX_CMD_LENGTH) )
temp_length++; /*temp_length then is the length of cmd ,
dont contanin parametes */
for(cnt=0; cnt<= CMD_NUM;cnt++ )
{
if( TABLE[cnt].length == temp_length )
{
search_buf[search_index] = cnt; /* save the index of length matched cmd,
prepare for next search*/
search_index++;
}
} /* search for the cmd_length matched commmand in TABLE ,
and save the index of matched command*/
if( search_index== 0 )
return notfind; /* all the cmd'S length in TABLE is not match*/
for(cnt=0; cnt<search_index; cnt++ )
{
temp = search_buf[cnt];
while( ( TABLE[temp].cmd_line[i] == cmd_buf.cmd_line[i] )&& (i<temp_length) )
i++;
if(i==temp_length)
return temp ; /* the index of cmd in TABLE[]*/
} /* search for the cmd_line contant matched cmd in TABLE ,and save the index */
return notfind;
}
/*********************************************************************************************************
** 函数名称: Normal_Normal();
** 功能描述: normal_normal state process function
** 输 入:
** 输 出:
** 全局变量: rec ,cmd_buf
** 调用模块: Uart0_Put_Char()
**
** 作 者: wangzheng
** 日 期: 2006.11.02
**-------------------------------------------------------------------------------------------------------
** 修改人: wangzheng
** 日 期:
**-------------------------------------------------------------------------------------------------------
** 修改人: wangzheng
** 日 期:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
void Normal_Normal(void)
{
if( (rec!=32) && (cmd_buf.length <= MAX_CMD_LENGTH) ) /* ignore the 'space' */
{
if( (rec>=65) && (rec<=90) )
rec = (rec + 32);
cmd_buf.cmd_line[ cmd_buf.length]= rec ; /* save the revieved char */
cmd_buf.length++;
Uart_Put_Char(rec); /* echo to the terminal */
}
else ;
}
/*********************************************************************************************************
** 函数名称: Control_Control_bs()
** 功能描述: control_control state process function
** 输 入:
** 输 出:
** 全局变量: cmd_buf
** 调用模块: Uart0_Put_Char( )
**
** 作 者: wangzheng
** 日 期:
**-------------------------------------------------------------------------------------------------------
** 修改人: wangzheng
** 日 期:
**-------------------------------------------------------------------------------------------------------
** 修改人: wangzheng
** 日 期:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
void Control_Control_bs(void )
{
if(cmd_buf.length >0)
{
cmd_buf.cmd_line[cmd_buf.length] = 0;
cmd_buf.length--;
Uart_Put_Char(8); // print backspace
Uart_Put_Char(32); // print space
Uart_Put_Char(8); //print backspace
}
}
/*********************************************************************************************************
** 函数名称: Func_execute ()
** 功能描述: execute the user function
** 输 入:
** 输 出:
** 全局变量:
** 调用模块: Init_cmd_buf( )
**
** 作 者: wangzheng
** 日 期: 2006.11.02
**-------------------------------------------------------------------------------------------------------
** 修改人: wangzheng
** 日 期:
**-------------------------------------------------------------------------------------------------------
** 修改人: wangzheng
** 日 期:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
void Func_Execute(void)
{
int8 index;
index = Search_Cmd( ); // search for the matched command in TABLE
if( index!= -1 )
TABLE[index].pfun();
else
Uart_Write(bad_command);
Init_Cmd_Buf( ); // prepare for next command
}
/*********************************************************************************************************
** 函数名称: Cmd_Shell_FSM( )
** 功能描述: core of command shell, a finite state machine
** 输 入:
** 输 出:
** 全局变量: ..........
** 调用模块:
**
** 作 者: wangzheng
** 日 期: 2006.11.02
**-------------------------------------------------------------------------------------------------------
** 修改人: wangzheng
** 日 期:
**-------------------------------------------------------------------------------------------------------
** 修改人: wangzheng
** 日 期:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
void Cmd_Shell_FSM(void)
{
while(sig_renew)
{
sig_renew = 0; // waitting for next uart interrupt change sig_new state
switch ( curr_stat ) // in normal char state
{
case normal_char :
switch(curr_sig)
{
case normal_char :
Normal_Normal( ); break;
case backspace :
State_Tran(control_char); break;
case cr :
State_Tran(func_exe); break;
default: break;
}break;
case control_char : // in control char state
switch ( curr_sig )
{
case normal_char :
State_Tran( normal_char ); break;
case backspace :
Control_Control_bs( ); break;
case cr :
State_Tran( func_exe ); break;
default: break;
}break;
case func_exe : // in function execute state
switch (curr_sig )
{
case cr :
Uart_Put_Char('\r');
Uart_Put_Char('\n');
Func_Execute( ); //execute the user defined function
Uart_Put_Char('>'); break;
case backspace :
Control_Control_bs( ); break;
case normal_char :
State_Tran (normal_char); break;
default: break;
}
default : break;
}
}
}
/************************************************************************************************
* end of file
************************************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -