Repository navigation
Expand file tree
/
Copy pathsimple_sh.c
More file actions
198 lines (187 loc) · 4.33 KB
/
Copy pathsimple_sh.c
File metadata and controls
198 lines (187 loc) · 4.33 KB
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
#include "simple_sh.h"
#define TRUE 1
int pipe_flag;
void generate_pipe(char *line, int count_loop, char **history, int *h_counter)
{
int fd[2];
pipe(fd);
char *items[COMMAND_ARGV_MAX];
char *item[COMMAND_ARGV_MAX];
int i = 0;
for (i=0; i < COMMAND_ARGV_MAX; i++) {
items[i] = NULL;
item[i] = NULL;
}
count_loop++;
pid_t child_id = fork();
if (child_id == -1) {
write(1, "error:fork failed!\n", 19);
return;
}
if (child_id == 0) { /*child process*/
dup2(fd[1], 1);
close(fd[0]);
interpret_line(line, "|", items);
interpret_line(items[count_loop-1], " ", item);
if (builtin_pipe(history, item, h_counter) == 0) {
if (execve(item[0], item, 0) == -1) {
fprintf(stderr, "error:%s\n", strerror(errno));
}
exit(1);
}
}
else { /*father process*/
dup2(fd[0], 0);
close(fd[1]);
if (pipe_flag > count_loop) {
//printf("1\n");
generate_pipe(line,count_loop,history, h_counter);
}
else{
interpret_line(line, "|", items);
interpret_line(items[count_loop], " ", item);
if (builtin_pipe(history, item, h_counter) == 0) {
if (execve(item[0], item, 0) == -1) {
fprintf(stderr, "error:%s\n", strerror(errno));
}
}
waitpid(-1,0,0);
}
}
}
void run()
{
int status, i;
/*indicate that whether need to read from stdin*/
int flag = 0;
int built_result;
char *path;
char **parameters;
char **history_list;
char **temp;
int history_counter = 0;
int length;
char buffer[BUFFER_MAX];
char buf[BUFFER_MAX];
int offset;
parameters = malloc(sizeof(char *)*COMMAND_ARGV_MAX);
history_list = malloc(sizeof(char *)*HISTORY_LIST_MAX);
temp = malloc(sizeof(char *)*COMMAND_ARGV_MAX);
for(i=0; i < COMMAND_ARGV_MAX; i++){
parameters[i] = NULL;
temp[i] = NULL;
}
for (i=0; i < HISTORY_LIST_MAX; i++) {
history_list[i] = NULL;
}
while (TRUE) {
if(flag == 0){
prompt();
length = read(0, buffer, BUFFER_MAX);
if(length == 1){
write(1, "error:no commands!\n", 19);
continue;
}
memset(buf, '\0', sizeof(buf));
strncpy(buf, buffer, length-1);
if (interpret_input(buf, parameters) == -1) {
write(1, "error:Too much parameters\n", 26);
continue;
}
add_history(&history_counter, history_list, buf);
}
else {
if (pipe_flag > 0) {
memset(buffer, '\0', sizeof(buffer));
strcpy(buffer, strstr(buf, "|"));
memset(buf, '\0', sizeof(buf));
strcpy(buf, history_list[offset]);
strcat(buf, buffer);
}
else {
memset(buf, '\0', sizeof(buf));
strcpy(buf, history_list[0]);
}
if (interpret_input(buf, parameters) == -1) {
write(1, "error:Too much parameters\n", 26);
continue;
}
flag =0;
}
/*calculate pipe_flag*/
i = 0;
pipe_flag = 0;
while (parameters[i] != NULL) {
if (strcmp(parameters[i], "|") == 0) {
pipe_flag++;
}
/* write(1, parameters[i], strlen(parameters[i]));
* write(1, "\n", 1);
*/
i++;
}
/*printf("pipe_flag = %d\n", pipe_flag);*/
if (parameters[1] != NULL) {
offset = s2i(parameters[1]);
}
if (pipe_flag == 0) {
built_result=built_in(&history_counter, history_list, parameters, pipe_flag, temp);
}
else {
i = 0;
while (strcmp(parameters[i], "|") != 0) {
temp[i] = malloc(sizeof(char)*(strlen(parameters[i])+1));
strcpy(temp[i], parameters[i]);
i++;
}
temp[i] = NULL;
built_result = built_in(&history_counter, history_list, temp, pipe_flag, temp);
}
switch (built_result) {
case 0:
/* printf("history [] command\n");*/
flag = 1;
continue;
case 1:
/* printf("built-in command!\n");*/
continue;
case 2:
/* printf("arguements error\n");*/
write(1, "error:Wrong arguements for builtin commands!\n", 45);
continue;
case 3:
/* printf("normal command\n");*/
break;
default:
break;
}
path = malloc(sizeof(char)*(strlen(parameters[0])+1));
strcpy(path, parameters[0]);
/*fork a child process to execute the instructions*/
pid_t child_id = fork();
if (child_id < 0) {
write(1, "error:fork failed\n", 18);
continue;
}
if(child_id != 0){
waitpid(-1, &status, 0);
}
else{
if (pipe_flag > 0) {
generate_pipe(buf, 0, history_list, &history_counter);
exit(0);
}
else{
if (execve(parameters[0], parameters, 0) == -1) {
fprintf(stderr, "error:%s\n", strerror(errno));
exit(1);
}
}
}
}
}
int main(int argc, char *argv[])
{
run();
return 0;
}