📄 myshell.c
字号:
#define _U_D_#include "myshell.h"
int main(int argc, char* argv[])
{
char buf[MaxCommandLine];
int n, paramNumber;
int commandType;
char* params[MaxParamNumber]; Init();
printPrompt();
while ((n=read(STDIN_FILENO, buf, MaxCommandLine))>0)
{
buf[n]='\0';// printf("===========%s\n",buf); char * buf2 = buf; int j = 0; for (; 1; j++)
{ char * buf1;
if ((buf1 = strtok(buf2, deline))!=NULL)
{ buf2 = buf2 + strlen(buf1) + 1; // printf("===========--%s\n",buf1); //It needs to operate &. if( strchr(buf1,'&') ) { ampersandFunc(buf1);// printf("++& ampersand.\n"); continue; }
commandType=parseCommand(buf1, params, ¶mNumber);
switch (commandType)
{ case -2:
printf("illegal command params.\n%s"); break; case -1: printf("The command \'%s\' is executed by /bin/shell\n",buf1); execute(buf1); break; case -3: break; case -4: break;
default: commandArray[commandType](params, paramNumber); break;
} } else { break; } } printPrompt();
}
return 0;
}
//Returned value is -1 which means command beyond the area of myshell.//-2 means too many parameters.//-3 means no any inputs from users.int parseCommand(char* buf, char* params[], int* paramNumber)
{
int i;
*paramNumber=0; // for( i = 0 ; i < strlen(buf) ;i ++)// {// printf("===========-%X,%c\n",*(buf+i),*(buf+i));// }
if ((params[*paramNumber]=strtok(buf, delim))!=NULL)
{
for (i=CommandNumber-1; i>=0; i--)
{
if (strcmp(params[*paramNumber], commandStr[i])==0)
{
break;
}
}
//when not found, i==-1
if (i==-1)
{
return i;
}
}
else
{
return -3;
}
(*paramNumber)++;
//the maximum param number is only $(MaxParamNumber), so I test for MaxParamNumber+1 to see if strtok return NULL
while (1)
{ //a continus revoke of strtok() follows the previous revoke's result.
if ((params[*paramNumber]=strtok(NULL, delim))==NULL)
{
break;
}
(*paramNumber)++;
if (*paramNumber==MaxParamNumber)
{
//this means the param number is more than MaxParamNumber and it is wrong
return -2;
}
}
return i;
}
void Init(){ delim=" \n"; deline="\n";
helpStr="Syntax: quit\nSyntax: cd directory\nSyntax: dir [directory]\nSyntax: clr \nSyntax: environ\nSyntax: echo \nSyntax: help \nSyntax: pause\n";
commandStr[0]="quit"; commandStr[1]="cd"; commandStr[2]="dir"; commandStr[3]="clr"; commandStr[4]="environ"; commandStr[5]="echo"; commandStr[6]="help"; commandStr[7]="pause"; commandStr[8]="myshell"; //A function pointer Array which points the different functions. commandArray[0] = quitShell; commandArray[1] = changeDir; commandArray[2] = listDir; commandArray[3] = clrFile; commandArray[4] = environFile; commandArray[5] = echoFile; commandArray[6] = helpFile; commandArray[7] = pauseFile; commandArray[8] = inputDirect; //initialize environment params. InitEnvironParams(); createbatch();
}//To create batch file when the program is executed.int createbatch(){ FILE * fp= NULL; fp = fopen("batch.file","wb"); char *str = "cd .. \ndir \ncd myshell\necho 1234 567 8956\nhelp > ning.yu\nenviron > yu.ning\n"; fwrite(str,strlen(str),1,fp); fclose(fp); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -