//==========================================================================
//模块名称: shell命令模块
//功能描述:判断输入的命令信息
//
//==========================================================================
#include "command.h"
#include "strsplit.h"
char cd_path_his[50];
char outbuf[2048];
//========================================================================================================
//function name : getcmd_info
//description: split input command and get it's info
//parameter:
//return value: int,the command's type
//========================================================================================================
int get_cmd_type(char *inputbuf,char *cmd_option[])
{
int cmd_type;
if(inputbuf==NULL)
{
perror("in function get_cmd_tpye() error!\n");
exit(-1);
}
if((strstr(inputbuf,"cd")!=NULL) || (strstr(inputbuf,"exit")!=NULL)||(strstr(inputbuf,"jobs")!=NULL)||(strstr(inputbuf,"bg")!=NULL)||(strstr(inputbuf,"fg")!=NULL))
{
cmd_type=0; //内部命令
strsplit(inputbuf," ",cmd_option);
return cmd_type;
}
else if( strstr(inputbuf,">") )//rewrite方式重定向
{
cmd_type=1;
strsplit(inputbuf,">",cmd_option);
return cmd_type;
}
else if( strstr(inputbuf,">>"))//append方式重定向
{
cmd_type=2;
strsplit(inputbuf,">>",cmd_option);
return cmd_type;
}
else if( strstr(inputbuf,"|") )//pipe 管道命令
{
cmd_type=3;
strsplit(inputbuf,"|",cmd_option);
return cmd_type;
}
else //普通命令
{
cmd_type=4;
strsplit(inputbuf," ",cmd_option);
return cmd_type;
}
return -1;
}
//==============================================================================================================
//function name: iner_cmd_addr
//description: execute iner command address
//parameter:
//return value: null
//==============================================================================================================
void iner_cmd_addr(char *cmd_option[])
{
char temp[50];
char path[50];
char cmdbin[20];
int fd[2];
int i=0;
pid_t pid;
printf("in 66 line");
strcpy(cmdbin,cmd_option[0]);
printf("in 68 line");
if(pipe(fd)<0)
{
perror("pipe error!\n");
exit(1);
}
if((pid=fork())<0)
{
perror("fork error in comm_cmd addr\n");
exit(-1);
}
if(pid==0)
{
close(fd[0]);
dup2(fd[1],1);
if(strcmp(cmdbin,"cd")==0)
{
memset(temp,0,50);
memset(path,0,50);
if(cmd_option[1]!=NULL)
strcpy(path,cmd_option[1]);
// strcpy(cd_cmd_his,cd_cur_path);
getcwd(temp,sizeof(temp));
if(strcmp(path,"~")==0)
{
strcpy(path,"/home");
}
else if(strcmp(path,"-")==0)
{
strcpy(path,cd_path_his);
}
//printf("path=%s",path);
chdir(path);
strcpy(cd_path_his,temp);
}
else if(strcmp(cmdbin,"jobs")==0)
{
;
}
else if(strcmp(cmdbin,"bg")==0)
{
;
}
else if(strcmp(cmdbin,"fg")==0)
{
;
}
else if(strcmp(cmdbin,"exit")==0)
{
printf("Bye bye!\n");
exit(1);
}
}
else
{
close(fd[1]);
waitpid(pid,NULL,0);
while(read(fd[0],outbuf+i,1)!=0)
{
if(outbuf[i]=='\n')
{
outbuf[i]='\r';
i++;
outbuf[i]='\n';
}
i++;
}
}
}
//==============================================================================================================
//function name: comm_cmd_addr
//description:
//parameter:
//return value:
//=========================================================================================================
void comm_cmd_addr(char *cmd_option[])
{
pid_t pid;
char cmdbin[20];
int fd[2];
int i=0;
strcpy(cmdbin,cmd_option[0]);
if(pipe(fd)<0)
{
perror("pipe error!\n");
exit(1);
}
if((pid=fork())<0)
{
perror("fork error in comm_cmd addr\n");
exit(-1);
}
if(0==pid)
{
close(fd[0]);
dup2(fd[1],1);
execvp(cmdbin,cmd_option);
}
else
{
close(fd[1]);
//dup2(fd[0],0);
waitpid(pid,NULL,0);
while(read(fd[0],outbuf+i,1)!=0)
{
if(outbuf[i]=='\n')
{
outbuf[i]='\r';
i++;
outbuf[i]='\n';
}
i++;
}
}
}