⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shell.c

📁 基于RTX51系统,C语言写的程序
💻 C
字号:
#include<rtx51tny.h>
#include<reg52.h>

#define MaxLenWord 20 /*20   单词最大长度*/                 
#define MaxLenWordTable 10 /*10   词表最多字数*/
#define MaxLenComNumBuf 256 /*256   命令缓冲区最大长度*/
#define MaxComNum 30         //最大命令种类数
#define MaxLenCom 10         //命令最大长度
#define MAIN 0
int i;
unsigned char ch;
typedef struct{
                int Num;     //有效单词数
                int LeftCurveNum;  //左括号数
                int RightCurveNum; //右括号数
                struct{
                       int Length;
                       unsigned char Str[MaxLenWord+1]; //单词内容+'\0' 
                      }wt[MaxLenWordTable];               
}stWORDTABLE;


unsigned  char ComTable[MaxComNum][MaxLenCom+1]= {"com1","com2","com3","com4","com5","com6","com7","out","close","help","debug"};
  
  //os_create_task(MAIN);

void TaskMain(void)_task_ MAIN  //主任务定义
{
  //串口人机界面的初始界面
  PrintStr("\t\t****************************************\n");
  PrintStr("\t\t*    Welcome to use SimpleShell        *\n");
  PrintStr("\t\t****************************************\n\n\n");
  
  PrintStr(">");                    //串口人机界面的命令提示符
  while(!ShellEnd)
  {
    switch(State)
     {
        case StatInputCom:
            {
             if(ch=_getkey())
             {
                if(ch==13)
                {
                 PrintStr("\n");
                  ComBuf[i+1]='\0';
                  if(i+1==0)
                    PrintStr(">");
                  else
                    State=StatExeCom;
                 } 
                else
                   {
                    i=i+1;
                    if((i>=MaxLenComBuf)&&(ch!=8))
                      {
                       putchar(7);
                       i=MaxLenComBuf-1;
                      }
                     else
                       {
                        if(ch==8)
                          {
                           i=i-2;
                           if(i<-1)
                            {i=-1;putchar(7);}
                           else
                            {
                             putchar(8);
                             putchar(' ');
                             putchar(8);    
                             }
                          }
                         else
                          {
                           putchar(ch);
                           ComBuf[i]=ch;
                          }
                        }
                    }
                  break;
             }
              else
              {
               os_wait(K_IVL,10,0);
               break;
            }     
      }
      case StatExeCom:
           {
            PrintStr("\n");
            if(InterpretCommand(ComBuf,&WordTable)==1&& WordTable.Num !=0)
              {
               strlwr(WordTable.wt[0].Str);
                for(tem=0;tem<MaxComNum&&!ComMatchFlag;tem++)
                 if(strcmp(WordTable.wt[0].Str,ComTale[tem])==0)
                   ComMatchFlag=1;
                  if(ComMatchFlag)
                    {
                     tem--;
                     switch(tem)
                      {
                       case 0:{Command1(&WordTable);break;}
                       case 1:{Command2(&WordTable);break;}
                       case 2:{Command3(&WordTable);break;}
                       case 3:{Command4(&WordTable);break;}
                       case 4:{Command5(&WordTable);break;}
                       case 5:{Command6(&WordTable);break;}
                       case 6:{Command7(&WordTable);break;}
                       case 7:{DebugOut(&WordTable);break;}
                       case 8:
                              {
                               //ShellEnd=1;
                               PrintStr("\n\t This Command is limited!\n\n");
                               break;    
                               }
                       case 9:{clrscr();break;}
                       case 10:{DisplayHelpMenu(&WordTable);break;}
                       case 11:{DebugChange(&WordTable);break;}    
                       }
                    }
                    else
                     PrintStr("  Bad command!\n\n");
                   }
                   else
                      {
                        if(WordTable.Num)
                          PrintStr("  Bad command!\n\n");
                       }
                 ComMatchFlag=0;
                 State=StatInputCom;
                  if(ShellEnd)
                  {
                   PrintStr("\n\n");
                  }
                 else PrintStr(">");
                 i=-1;
                 break;
                 }
                 default:
                        {
                          //ShellEnd=1;
                           PrintStr("System fatal error!\n");
                           putchar(7);putchar(7);putchar(7);
                         }
           }                             
       }  
    }



/////////////////////////////////////////////////////////

bit InterpretCommand(unsigned char *Combuf,stWORDTABLE *WordTable)
{
  int i=0;
  int j=0;
  int k=-1;
  int StrFlag=0;

  int SentenceEndFlag=0;
  char ch;
  WordTable->Num=0;
  WordTable->LeftCurveNum=0;
  WordTable->RightCurveNum=0;

  ch=ComBuf[0];
  while(!SentenceEndFlag&&i<MaxLenComBuf)
     {
      if((ch>='0'&&ch<='9')||(ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')||(ch=='.'))
        {
          if(StrFlag==0)
           {  StrFlag=1;
              k=k+1;
              j=0;
              if(k>MaxLenWordTable)
                return 0;
              WordTable->wt[k].Str[j]=ch;
              WordTable->Num=k+1;
             
           }
          else
           {
             j=j+1;
             if(j>=MaxLenWord) return 0;
             WordTable->wt[k].Str[j]=ch;
           }   
         }
       else if(ch==' '||ch==','||ch=='('||ch==')'||ch=='\0')
             {
               if(ch=='(')
                 WordTable->LeftCurveNum++;
               if(ch==')')
                 WordTable->RightCurveNum++;
               if(StrFlag==1)
                 {
                  StrFlag=0;j=j+1;
                  WordTable->wt[k].Str[j]='\0';
                  WordTable->wt[k].Length=j;
                 }
               if(ch=='\0')
                  SentenceEndFlag=1; 
              }
         else
            {return 0;}
         i=i+1;
         ch=ComBuf[i];     
     }
     if(i<MaxLenComBuf||ComBuf[MaxLenComBuf]=='\0')
       {if(WordTable->LeftCurveNum==WordTable->RightCurveNum)
           return 1;
        else
           return 0;
        }
      else
        {return 0;}               
}
///////////////////////////help&debug///////////////////////////////
void DisplayHelpMenu(stWORDTABLE WordTable)
{
 WordTable=WordTable;
 PrintStr("\n");
 PrintStr("\tcom1    Command1.\n");
 PrintStr("\tcom2    Command2.\n");
 PrintStr("\tcom3    Command3.\n");
 PrintStr("\tcom4    Command4.\n");
 PrintStr("\tcom5    Command5.\n");
 PrintStr("\tcom6    Command6.\n");
 PrintStr("\tcom7    Command7.\n");
 PrintStr("\tout     Set P0 out data.\n");
 PrintStr("\tclose   Exit.(this command is limited here.)\n");
 PrintStr("\tclr     Clear sreen.\n");
 PrintStr("\thelp    Display this menu.\n\n");
 PrintStr("\tdebug   Set debug parameter.\n\n");
}


void Debugchange(stWORDTABLE *WordTable)
{
 if(WordTable->Num==1)
    PrintStr("\n\tPlease input New Value!\n\n");
 else
   {
    if((WordTable->wt[1].Str[0]>='0')&&(WordTable->wt[1].Str[0]<='9'))
       mDebug=WordTable->wt[1].Str[0]-'0';
    else
      {PrintStr("\n\tValue Error!\n\n");return;}
   }
 PrintStr("mDebug=");
 PrintByte(mDebug);
 PrintStr("\n");
}
//////////////////////////////////////////// out/////////////////////////
void Debugout(stWORDTABLE *WordTable)
{
 unsigned char i;
 outDebug=0;
 if(WordTable->Num==1)
  {
   printStr("\n\tPlease input New Value!\n\n");
   return;
  }
 else
  {
   if(WordTable->wt[1].Length>3)
    {
     PrintStr("\n\tPlease input Corrret Value!\n\n");
     return;
    }
    else
     {
       for(i=0;i<WordTable->wt[1].Length;i++)
        {

         if(WordTable->wt[1].Str[i]>='0'&&WordTable->wt[1].Str[i]<='9')
           outDebug=outDebug+WordTable->wt[1].Str[i]-'0';
         else
           {
            PrintStr("\n\tPlease input Corrret Value!\n\n");
            return;
           }
         outDebug=outDebug*10; 
       }
       outDebug=outDebug/10;
       if(outDebug>255)
         PrintStr("\n\tPlease input Corrret Value!\n\n");
       else
         {
          i=(unsigned char)outDebug;
          P0=i;
         } 
       } 
   }
 PrintStr("outDebug=");
 PrintByte(unsigned char)outDebug;
 PrintStr("\n");
}
////////////////////////////////////////debug_add//////////////
/*void TaskUser1 (void)_task_ USER1
{
 for(;;)
 {
  if(mDebug&0x1)
   PrintStr("\tUserTask1 is active.\n");
   os_wait(K_TMO,255,0);
 }
}

void TaskUser2(void)_task_ USER2
{
  int i;
  i=0;
  for(;;)
  {
   if(mDebug&0x2)
   {
    i=1-i;
    if(i==0)
      P0=0;
    else
      P0=1;
   }
  os_wait(K_TMO,255,0);   
 }
}

 */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -