📄 commands.cpp
字号:
#include <string>#include <iostream>#include <stdlib.h>#include <dirent.h>#include <unistd.h>using namespace std;#include "shell.h"#include "commands.h"extern char chr_Prompt0[MAX_ENVIRONMENT_LENGTH]; //chr_Promt0 is original Promtextern char chr_PathName0[MAX_ENVIRONMENT_LENGTH]; //Original path nameextern char *chr_PathName; //Path name
char *chr_SysemReturn=(char*)malloc(sizeof(char)*MID_OUTPRINT); //Used to get information returned by system()char *func_cd(char *chrtmp_Argument){ //This argument has not been processed by 'char* getArgument(char*)'
//char *advan_chrtmp_Argument is an advanced argument for chrtmp_Argument char *advan_chrtmp_Argument=(char*)malloc(sizeof(char)*(strlen(chrtmp_Argument)+strlen(chr_PathName))); //char *tmpchr0 stores the whole command line that will run in system() char *tmpchr0=(char*)malloc(sizeof(char)*(strlen(chrtmp_Argument)+strlen(chr_PathName))); //char *tmpchr1 sotres the relative directory char *tmpchr1; //char *tmpchr2 stores the absolute directory char *tmpchr2=(char*)malloc(sizeof(char)*(strlen(chrtmp_Argument)+strlen(chr_PathName))); //char *tmpchr3 is used as a temporary variable for chr_PathName char *tmpchr3=(char*)malloc(sizeof(char)*MAX_COMMANDLINE_LENGTH); strcpy(advan_chrtmp_Argument,trim(chrtmp_Argument)); tmpchr1=trim(strmid(advan_chrtmp_Argument,4,strlen(advan_chrtmp_Argument)-3));
strcpy(tmpchr2,chr_PathName); strcat(tmpchr2,"/"); strcat(tmpchr2,tmpchr1);
if(!strcmp(strmid(trim(tmpchr1),1,1),"")){
//"cd" //system("cd");
initial(); //chr_PathName=chr_PathName0;
}
else if(!strcmp(strmid(tmpchr1,1,1),".") && (!strcmp(strmid(tmpchr1,2,1),"") || !strcmp(strmid(tmpchr1,2,1)," "))){
system("cd .");
} else if(!strcmp(strmid(tmpchr1,1,2),"..") && (!strcmp(strmid(tmpchr1,3,1),"") || !strcmp(strmid(tmpchr1,3,1)," "))){ internal_func_cd1(); cout<<"cd .."<<endl;
}
else if(!strcmp(strmid(tmpchr1,1,1),"/") && (!strcmp(strmid(tmpchr1,2,1),"") || !strcmp(strmid(tmpchr1,2,1)," "))){ strcpy(tmpchr0,"cd \""); strcat(tmpchr0,tmpchr1); strcat(tmpchr0,"\""); cout<<tmpchr0<<endl; if(system(tmpchr0)==0){ //Runs well strcpy(chr_PathName,"/"); } } else if(!strcmp(strmid(trim(tmpchr1),1,1),"/")){ strcpy(tmpchr0,"cd \""); strcat(tmpchr0,tmpchr1); strcat(tmpchr0,"\""); if(system(tmpchr0)==0){ //Runs well strcpy(chr_PathName,trim(tmpchr1,'/')); } } else { //Directory is a sub-directory at current working directory strcpy(tmpchr0,"cd \""); strcat(tmpchr0,tmpchr2); strcat(tmpchr0,"\""); if(system(tmpchr0)==0){ //Runs well strcat(chr_PathName,"/"); strcat(chr_PathName,trim(tmpchr1,'/')); } } cout<<"chr_PathName: "<<chr_PathName<<endl; strcpy(tmpchr3,"/"); strcpy(chr_PathName,trim(chr_PathName,'/')); strcat(tmpchr3,chr_PathName); strcpy(chr_PathName,tmpchr3); chdir(chr_PathName); return "";} char *func_clr(char *chrtmp_Argument){ char* returnInfo0=argumentIsNull("clr",chrtmp_Argument); char *returnInfo1=""; if(strlen(returnInfo0)<2){ system("cls || clear"); return returnInfo1;
}
else
return returnInfo0;
} char *func_dir(char *chrtmp_Argument){ char* tmpchr0=(char*)malloc(sizeof(char)*MAX_COMMANDLINE_LENGTH); char* tmpchr1; char* returnInfo0; chdir(chr_PathName); //Synchronize current working directory strcpy(tmpchr0,"dir "); tmpchr1=strmid(chrtmp_Argument,5,strlen(chrtmp_Argument)-4); returnInfo0=argumentIsNull("quit",tmpchr1); if(strlen(returnInfo0)<2){
strcat(tmpchr0,"\""); strcat(tmpchr0,chr_PathName);
strcat(tmpchr0,"\""); system(tmpchr0);
} else { strcat(tmpchr0,tmpchr1); system(tmpchr0); } return "";
}char *func_environ(char *chrtmp_Argument){ char* returnInfo0=argumentIsNull("environ",chrtmp_Argument); char* returnInfo1=(char*)malloc(sizeof(char)*MAX_ENVIRONMENT_LENGTH); if(strlen(returnInfo0)<2){ strcpy(returnInfo1,SHELL_NAME); strcat(returnInfo1,"_Path = "); strcat(returnInfo1,chr_Prompt0); strcat(returnInfo1,"\n"); return returnInfo1;
}
else
return returnInfo0;
}char *func_echo(char *chrtmp_Argument){ char* tmpchr0=(char*)malloc(sizeof(char)*strlen(chrtmp_Argument));
char* tmpchr1;
tmpchr1=ltrim(chrtmp_Argument);
//char tmpchr0[MAX_COMMANDLINE_LENGTH]; if(strlen(chrtmp_Argument)>=6){ strcpy(tmpchr0,strmid(tmpchr1,6,strlen(tmpchr1)-5)); strcat(tmpchr0,"\n"); return tmpchr0;
}
else
return "\n";
}char *func_help(char *chrtmp_Argument){ char* returnInfo0=argumentIsNull("help",chrtmp_Argument); char* returnInfo1="help information...\n"; return (strlen(returnInfo0)<2 ? returnInfo1 : returnInfo0);
}char *func_pause(char *chrtmp_Argument){ char* returnInfo0=argumentIsNull("pause",chrtmp_Argument); char* returnInfo1=""; if(strlen(returnInfo0)<2){ char *tmpchr0=(char*)malloc(sizeof(char)*MAX_COMMANDLINE_LENGTH); cin.getline(tmpchr0,MAX_COMMANDLINE_LENGTH); cin.clear();
}
else
return returnInfo0;
return returnInfo1;
}char *func_else(char* chrtmp_CommandLine){ char *tmpchr0=(char*)malloc(sizeof(char)*MAX_COMMANDLINE_LENGTH); strcpy(tmpchr0,"No such command: '"); strcat(tmpchr0,chrtmp_CommandLine); strcat(tmpchr0,"' in "); strcat(tmpchr0,SHELL_NAME); strcat(tmpchr0," !\nLoaded commands in system shell instead.\n"); system(chrtmp_CommandLine); return tmpchr0;
} char *func_quit(char *chrtmp_Argument){ char *returnInfo0=argumentIsNull("quit",chrtmp_Argument); char *returnInfo1=(char*)malloc(sizeof(char)*256); if(strlen(returnInfo0)<2){ strcpy(returnInfo1,SHELL_NAME); strcat(returnInfo1," exited!\n"); chdir(chr_PathName0); //Return to the original directory return returnInfo1;
}
else
return returnInfo0;
}char *argumentIsNull(char *chrtmp_Command, char *chrtmp_Argument){ //if str(chrtmp_Argument)>1,it returns an error report char *tmpchr0=(char*)malloc(sizeof(char)*256); if(strcmp(chrtmp_Argument,"")==0)
return " "; else { strcpy(tmpchr0,"Command '"); strcat(tmpchr0,chrtmp_Command); strcat(tmpchr0,"' doesn't support arguments!\n"); return tmpchr0;
}
}void internal_func_cd1(void){ //Used as a temporary variable for chr_PathName char *tmpchr_PathName=(char*)malloc(sizeof(char)*strlen(chr_PathName)); //Stores characters separated by strtok(tmpchr_PathName,"/") char *tmpchrArray[strlen(chr_PathName)]; char *tokenPtr; int inti1; //Used in while() below //Initialize char *tmpchrArray[] for(int inti0=0;inti0<=(signed int)strlen(chr_PathName);inti0++){ tmpchrArray[inti0]=(char*)malloc(sizeof(char)*strlen(chr_PathName)); } //Separate tmpchr_PathName by "/" inti1=0; strcpy(tmpchr_PathName,chr_PathName); tokenPtr=strtok(tmpchr_PathName,"/"); while(tokenPtr!=NULL){ strcpy(tmpchrArray[inti1],tokenPtr); inti1++; tokenPtr=strtok(NULL,"/"); } strcpy(chr_PathName,""); for(int inti2=0;inti2<inti1-1;inti2++){ strcat(chr_PathName,"/"); strcat(chr_PathName,tmpchrArray[inti2]); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -