//========================================================================
// The information contained herein is the exclusive property of
// sunplusedu. And shall not be distributed, reproduced,
// or disclosed in whole in part without prior written permission.
// (C) COPYRIGHT 2003 sunplusedu.
// ALL RIGHTS RESERVED
// The entire notice above must be reproduced on all authorized copies.
//=========================================================================
//==============================================================================
// 工程名称: myshell
// 功能描述: 执行简单的shell内部和外部命令,重定向和管道命令,前后台执行
// 命令,jobs功能,记录历史命令等功能
// 涉及的库: stdio.h stdlib.h fcntl.h unistd.h string.h signal.h
// sys/types.h sys/wait.h termios.h
// 组成文件: main.c addBlank.c check.c history.c init_environ.c isincmd.c
// keypress.c my_pipe.c my_strstr.c myjobs.c redirect.c save_cmd.c
// myinclude.h addBlank.h check.h history.h init_environ.h isincmd.h
// keypress.h my_pipe.h my_strstr.h myjobs.h redirect.h save_cmd.h
// 硬件连接: 无
// 维护记录: 2009-4-12 v1.0 by sxz
//==============================================================================
#include "myinclude.h"
//==============================================================================
// 文件名: main.c
// 功能描述: 执行简单的shell内部和外部命令,重定向和管道命令,前后台执行
// 命令,jobs功能,记录历史命令等功能
// 维护记录: 2009-4-12 v1.0 by sxz
//==============================================================================
char cmd[40]=""; //输入的命令行
char *arg[10]={NULL}; //以空格划分命令行后的命令段
char addr[40]=""; //刚进入shell时的工作目录
int cn=0; //以空格划分命令行后的命令段的个数
NODE *head=NULL; //jobs链表的首地址
pid_t PID; //子进程的pid
int main()
{
int i=0;
int j=0;
int key=0; //输入字符对应的键值
int flag=0;
NODE *pi=NULL;
pid_t pid;
signal(SIGTSTP,ctrl_z); //安装crtl+z信号
signal(SIGINT,ctrl_c); //安装ctrl+c信号
signal(SIGQUIT,ctrl_q); //安装ctrl+\信号
getcwd(addr,40); //取得刚进入shell时的工作目录
while(1)
{
j=0;
printf("[root@localhost %s] ysh>",get_current_dir_name());
memset(cmd,0,40);
set_keypress(); //设置输入的模式
while(1) //输入命令
{
key=getchar();
if(key == 27) //输入的是光标,打印历史命令
{
history();
}
else if(key == 127) //输入的是Backspace,删除命令字符
{
cmd[--j] = '\0';
printf("\r ");
printf("\r[root@localhost %s] ysh>%s",get_current_dir_name(),cmd);
}
else if(key == 10) //键入Enter时,保存命令
{
save_cmd();
break;
}
else //保存命令到数组并显示在屏幕
{
printf("%c",key);
cmd[j++]=(char)key;
}
}
printf("\n");
reset_keypress(); //恢复标准模式
strcpy(cmd,addBlank(cmd)); //为命令行中的“<” “>>” “>”的前后加上空格
cn=my_strstr(cmd,arg); //以空格分割命令行
if(arg[0] != NULL)
{
if(isincmd(arg) == 1) //执行内部命令
{
continue;
}
else if(strcmp(arg[0],"jobs") == 0) //执行jobs命令
{
output_jobs();
}
else if(strcmp(arg[0],"fg") == 0) //执行fg命令
{
fg_jobs();
}
else if(strcmp(arg[0],"bg") == 0) //执行bg命令
{
bg_jobs();
}
else
{
if(init_environ(arg) == 1) //判断指定的可执行程序中是否对应有此命令
{
continue;
}
if(arg[1] != NULL)
{
if(strcmp(arg[1],"&") == 0) //在后台执行命令
{
flag=1;
pi=(NODE *)malloc(sizeof(NODE));
pi->num=NUM;
pi->pid=getpid();
strcpy(pi->state,"stopped");
strcpy(pi->comand,arg[0]);
head=jobs_link(pi);
}
}
if((pid=fork()) < 0) //创建子进程
{
perror("fork error!\n");
exit (1);
}
if(pid == 0)
{
i=check();
switch(i)
{
case 1:
redirect(); //执行类似"cat>text"的重定向命令
break;
case 2: //执行类似"cat>>text"的重定向命令
redirect_cat();
break;
case 3: //执行类似"cat>test<text"的重定向命令
redirect_a();
break;
case 4: //执行类似"cat>>test<text"的重定向命令
redirect_cat_a();
break;
case 5: //执行管道命令
my_pipe();
break;
default:
execvp(arg[0],arg); //执行普通的外部命令
break;
}
}
else
{
PID=pid;
if(flag == 1) //执行后台命令,父进程不等待子进程
{
;
}
else //执行前台命令,父进程等待子进程
{
waitpid(pid,NULL,WUNTRACED);
}
}
}
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
shell解释器的程序设计代码
共32个文件
c:13个
h:12个
mysh_profile:1个
4星 · 超过85%的资源 需积分: 9 18 下载量 137 浏览量
2010-01-24
13:41:00
上传
评论
收藏 28KB RAR 举报
温馨提示
实现shell的内部与外部命令 实现前台与后台的作业功能 实现管道与重定向的功能
资源推荐
资源详情
资源评论
收起资源包目录
linuxshell.rar (32个子文件)
shell
shell
code
text 427B
myjobs.h 342B
save_cmd.h 138B
addBlank.h 139B
my_strstr.c 844B
main.c 5KB
redirect.h 282B
keypress.h 165B
test 11KB
test.c 354B
isincmd.h 176B
my_strstr.h 153B
check.h 173B
save_cmd.c 834B
init_environ.c 2KB
redirect.c 2KB
myjobs.c 4KB
my_pipe.h 177B
history.c 1KB
isincmd.c 1KB
main.c.bak 5KB
mysh_profile 39B
Makefile 245B
my_pipe.c 866B
init_environ.h 195B
test1 427B
keypress.c 1KB
myinclude.h 663B
history.h 121B
check.c 1KB
addBlank.c 2KB
myshell 21KB
共 32 条
- 1
资源评论
- sjtuzjt2013-06-11很不错的例子。赞
xiangsilei
- 粉丝: 1
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功