include详解 shell_socket实现基于tcp/ip的网络远程shell命令解析器(完善中...)

//==========================================================================

//模块名称: 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++;

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值