/
mshell.c
312 lines (273 loc) · 5.58 KB
/
mshell.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <pwd.h>
#include <fcntl.h>
#include "mshell.h"
#include "user.h"
int mshell_init(struct mshell *mshell, int argc, char **argv, char **env)
{
memset(mshell, 0, sizeof(struct mshell));
mshell->user = getpwuid(getuid());
mshell->main_loop = 1;
mshell->is_show_cmdline = 1;
getcwd(mshell->cur_dir, PATH_MAX);
mshell->fd = 0;
mshell->pipe = -1;
mshell->wait_for_childs = 0;
/*需要执行脚本*/
if(argc >= 2) {
mshell->fd = open(argv[1], O_RDONLY);
if(mshell->fd < 0) {
mshell->main_loop = 0;
perror("open");
return 0;
}
mshell->is_show_cmdline = 0;
}
signal_init();
load_env(mshell, env);
return 0;
}
int mshell_read_oneline(struct mshell *mshell)
{
unsigned char *oneline;
int offset = 0;
int oneline_len = 20;
int ret = 0;
if(mshell->oneline) {
free(mshell->oneline);
}
oneline = (void *)malloc(oneline_len);
if(!oneline) {
perror("malloc");
return -1;
}
while(1) {
ret = read(mshell->fd, &oneline[offset], 1);
if(ret < 0) {
perror("read");
return -1;
} else if(!ret) {
mshell->main_loop = 0;
oneline[offset] = '\0';
goto exit;
}
if(oneline[offset] == '\n') {
oneline[offset] = '\0';
break;
}
if(oneline[offset] == '\t') {
printf("/b");
}
offset++;
if(offset >= (oneline_len - 1)) {
oneline = (void *)realloc(oneline, oneline_len + 20);
if(oneline == NULL) {
perror("realloc");
return -1;
}
oneline_len += 20;
}
}
exit:
mshell->oneline = oneline;
return 0;
}
int parser_one_block(unsigned char *oneline, int *oneline_offset, unsigned char **out)
{
unsigned char *block = NULL;
int has_quotation_mark = 0;
int last_is_escape = 0;
int offset = *oneline_offset;
int skip_offset = 0;
int out_offset = 0, out_len = 0;
/*filter space*/
while(oneline[offset] == ' ' || oneline[offset] == '\n')
offset++;
skip_offset = offset;
if(oneline[offset] == '\"' || oneline[offset] == '\'') {
has_quotation_mark = 1;
offset++;
}
if(oneline[offset] == ';' || oneline[offset] == '|' ||
oneline[offset] == '#' || oneline[offset] == '&') {
block = (void *)malloc(2);
block[0] = oneline[offset++];
block[1] = '\0';
out_offset++;
goto out;
}
while(oneline[offset] != '\0') {
if(out_offset >= (out_len - 1)) {
out_len += 20;
if(block == NULL) {
block = (void *)malloc(out_len);
} else {
block = (void *)realloc((void *)block, out_len);
}
}
if(last_is_escape) {
/*TODO: save char*/
block[out_offset++] = oneline[offset++];
last_is_escape = 0;
continue;
}
switch(oneline[offset]) {
case '\\':
last_is_escape = 1;
offset++;
continue;
case '\"':
case '\'':
if(has_quotation_mark) {
has_quotation_mark = 0;
} else {
has_quotation_mark = 1;
}
offset++;
continue;
case ' ':
case '\n':
case ';':
case '|':
case '&':
case '#':
if(!has_quotation_mark) {
goto out;
}
break;
}
block[out_offset++] = oneline[offset++];
}
out:
if(has_quotation_mark || last_is_escape) {
free(block);
*out = NULL;
return -1;
}
if(skip_offset == offset) {
free(block);
block = NULL;
} else {
block[out_offset] = '\0';
}
*oneline_offset = offset;
*out = block;
return 0;
}
int mshell_parser_oneline(const struct mshell *mshell, struct cmd *cmd)
{
int offset = 0;
unsigned char *out, *tmp;
int i = 0;
for(i=0; i<ARRAY_SIZE(cmd->cmd); i++) {
if(cmd->cmd[i] != NULL) {
free(cmd->cmd[i]);
cmd->cmd[i] = NULL;
} else {
i = 0;
break;
}
}
cmd->offset = 0; /*init offset*/
/*parser command*/
do {
parser_one_block(mshell->oneline, &offset, &out);
//printf("out: offset = %d, %s\n", offset, out);
//fflush(stdout);
//if(out != NULL) {
// free(out);
//}
if(out != NULL) {
tmp = out;
str_replace(&out, tmp, "~", mshell->user->pw_dir);
free(tmp);
//printf("%s\n", out);
}
cmd->cmd[i++] = out;
cmd->max = i;
} while(out != NULL);
/*TODO: parser arg*/
return 0;
}
int cmd_is_null(struct mshell *mshell, const struct cmd *cmd)
{
if(cmd->cmd[cmd->offset] == NULL) {
return 1;
}
return 0;
}
int mshell_handle_external_cmd(struct mshell *mshell, struct cmd *cmd)
{
int ret = 0;
int pid = 0;
int offset = cmd->offset, i;
unsigned char *tmp;
int pipefd[2] = {-1, -1};
if(cmd->cmd[offset] != NULL) {
/*TODO:准备参数, offset指向下一个*/
for(i=offset; cmd->cmd[i] != NULL; i++) {
switch(cmd->cmd[i][0]) {
case ';':
case '#':
case '$':
goto out;
case '|':
ret = pipe(pipefd);
if(ret < 0) {
perror("pipe");
return -1;
}
goto out;
}
}
out:
tmp = cmd->cmd[i];
cmd->cmd[i] = NULL;
pid = fork();
if(pid == 0) {
if(mshell->pipe != -1) {
close(0); //close input
dup2(mshell->pipe, 0);
}
if(pipefd[1] >= 0) {
close(pipefd[0]);
close(1); // close output
dup2(pipefd[1], 1); // ouput > pipe out
}
ret = execvp(cmd->cmd[offset], (void *)&cmd->cmd[cmd->offset]);
if(ret < 0) {
perror(cmd->cmd[offset]);
}
exit(ret);
} else if(pid > 0) {
if(tmp == NULL || (tmp != NULL && tmp[0] != '|')) {
mshell->wait_for_childs++;
while(mshell->wait_for_childs) {
wait(&ret);
mshell->wait_for_childs--;
}
} else {
mshell->wait_for_childs++;
}
//wait(&ret);
cmd->cmd[i] = tmp;
mshell->pipe = pipefd[0];
if(pipefd[1] >= 0) {
close(pipefd[1]);
}
if(tmp != NULL && tmp[0] == '#') {
cmd->offset = cmd->max;
} else {
cmd->offset = i + 1;
}
} else {
perror("fork");
}
}
exit:
return ret;
}