-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyShell_2.c
217 lines (196 loc) · 4.87 KB
/
MyShell_2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <ctype.h>
#include <myerr.h>
//文件重定向实现
void Run_ThisProgress(char *argv[],int argc,int IsNeedMF,int TheFile)
{
if(argc != 0)
{
//让子进程去执行shell指令
if(fork() == 0){
//有 '>' 文件重定向
if(IsNeedMF == 1)
{
close(1);
open(argv[TheFile],O_RDWR | O_CREAT,0644);
}
if(-1 == execvp(argv[0],argv))
err_msg("Pid Exec failed!\n");
}
wait(NULL);
}
}
int Judge_Rediect(char *argv[],int argc,int IsNeedMF,int TheFile)
{
//没有标识符'>'
//no '>'
if(TheFile == 0 || IsNeedMF == 0)
argv[argc] = NULL;
//有多个'>'标识符
//have more than one '>'
else if(IsNeedMF > 1){
err_msg("write in error!\n");
return 1;
}
//有'>'却无文件
else if(IsNeedMF == 1 && TheFile == argc)
{
err_msg("The file can not be nameless!\n");
return 1;
}
//不止一个文件
else if(IsNeedMF == 1 && (TheFile + 1) != argc)
{
int j = 0;
for(j = TheFile + 1;j <= argc;j++){
err_msg("The file %s is needless!\n",argv[j]);
}
err_msg("Leave The Procedure!\n");
return 1;
}
return 0;
}
void AlterBuf(char *buf,char *argv[],int* argc,int* IsNeedMF,int* TheFile)//需要改变,所以传指针
{
int i = 0;
int status = 0;//0--空格 1--字符
while(buf[i]!='\0')
{
// is '>'
if(buf[i] == '>' && isspace(buf[i+1]) && status == 0)
{
if (0 == i)
err_quit("write in error!\n");
else{
argv[(*argc)] = NULL;
(*argc)++;
*TheFile = *argc;
(*IsNeedMF) += 1;
}
}
//space->char
else if(!isspace(buf[i])&&status == 0){
argv[(*argc)] = &buf[i];
status = 1;
(*argc)++;
}
//is space
else if(isspace(buf[i]))
{
status = 0;
buf[i] ='\0';
}
i++;
}
}
void do_parse(char* buf)
{
int argc = 0;
char* argv[8];
int IsNeedMoveFile = 0;//表示发现'>'的个数
int TheTransferFile = 0;//内容转移的位置。
//修改命令行。
//暂时只考虑一个>的情况,不考虑 >>
//这里需要改变argc IsNeedMoveFile TheTransferFile的值,所以要传地址
AlterBuf(buf,argv,&argc,&IsNeedMoveFile,&TheTransferFile);
//判断'>'的特殊情况
if(1 == Judge_Rediect(argv,argc,IsNeedMoveFile,TheTransferFile) )
return;
//开始跑程序
Run_ThisProgress(argv ,argc ,IsNeedMoveFile ,TheTransferFile);
int i = 0;
for(i=0;i<argc;i++){
printf("%s ",argv[i]);
}
printf("\n");
}
//形成一个递归,从最后一个管道标识符开始往前
//比如 ls | grep test | wc
//肯定是执行ls 结果给 grep test 再给 wc
//最后一层递归处理ls 递归出来以后ls在父进程,用于输入,此时grep在子进程,用于输出
//然后下一层递归,输出结果给了上一层递归的输入,wc是最后的输出。
void DoJob(char *buf,int tmp[],int tmp_size)
{
if(tmp_size > 0)
{
int fds[2];
if(-1 == pipe(fds))
{
err_msg("pipe failed!\n");
return;
}
pid_t pid = fork();
if(pid == -1)
{
err_msg("fork failed!\n");
return;
}
else if(pid == 0)//father--send
{
close(fds[1]);
close(0);
dup2(fds[0],0);
close(fds[0]);
DoJob(buf,tmp,tmp_size-1);
}
else if(pid > 0)//child---recv
{
close(fds[0]);
close(1);
dup2(fds[1],1);
close(fds[1]);
wait(NULL);
do_parse(buf+tmp[tmp_size-1]);
}
}
else
do_parse(buf);
}
int main()
{
char buf[1024];
while(1){
memset(buf,0,sizeof(buf));
printf("[ root@localhost file ]#");
scanf("%[^\n]",buf);
getchar();
if(strncmp(buf,"exit",4) == 0)
err_quit(NULL);
//read buf
else{
//我需要去识别管道,但是还要保证能处理一个管道后的管道。
int i = 0;
int j = 0;
int tmp[8] = {0};
while(buf[i] != '\0')//将命令根据管道标识符分批次
{
if(buf[i] == '|')
{
if(i == 0)
{
err_msg("| is not right!\n");
break;
}
buf[i] = '\0';//管道前内容为一次指令
if(buf[i+1] == '|') //管道标识符下一个还是管道标识符,错误
{
err_msg("Too many '|'\n");
break;
}
tmp[j] = i + 1;//记录管道所在位置的下一位置
j++;
}
i++;
}
DoJob(buf,tmp,j);//根据管道开始工作。
}
}
return 0;
}