-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell.c
338 lines (319 loc) · 6.72 KB
/
shell.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdbool.h>
#define ERROR -1
/************************************************************************************
CHECKLIST
[commandline]
o semicolons
o redirection
o pipe
o background
o mix
[interactive]
o EOF
o semicolons
o redirection
o pipe
o background
o mix
************************************************************************************/
typedef enum {start, middle, end, nopipe} pipekind;
typedef int pipe_io[2];
char **fullcommand, **nextcommand, **command;
char input_filename[512], output_filename[512], error_filename[512];
size_t i, semicolons, next, pipes;
bool interactive, commandline, background, alternate;
int infile, outfile, errfile;
pipe_io file, _file;
pid_t pid;
void run();
void separate(char *commands);
void prompt();
void interact();
void connectpipe();
void _run(pipekind kind);
void redirect();
void redirect_in(size_t index);
void redirect_out(size_t index);
void redirect_err(size_t index);
void execute(char **command, pipekind kind);
void erase(char **command);
bool eof(int c);
int main(int count, char *rawinput[])
{
interactive = count == 1;
commandline = count == 3 && !strcmp("-c", rawinput[1]);
if (interactive) interact();
else if (commandline)
{
separate(rawinput[2]);
run();
}
else printf("myshell: invalid input\n");
}
void interact()
{
while (true)
{
prompt();
run();
}
}
#define moresemicolon (semicolons > 0)
#define morepipe (pipes > 0)
//check the inputline if there have pipes run connectpipe, else run _run with argument nopipe
void run()
{
size_t i;
while(true)
{
pipes = 0;
for(i = 0; command[i] != NULL; i++) if (!strcmp(command[i], "|")) pipes++;;
if (morepipe) connectpipe();
else _run(nopipe);
if (moresemicolon)
{
command = &command[1];
semicolons--;
}
else break;
}
free(fullcommand);
}
#define currentpipe (alternate ? _file : file)
#define nextpipe (alternate ? file : _file)
//check the amount of pipe and separate it to three kind of argument and run _run
void connectpipe()
{
size_t i, j;
pipekind kind = start;
alternate = false;
for (i = 0; command[i] != NULL; i++) if (!strcmp(command[i], "|"))
{
erase(&command[i]);
while (true)
{
if (pipe(currentpipe) != ERROR)
{
_run(kind);
pipes--;
if (*command == NULL) return;
for (j = 0; command[j] != NULL; j++)
if (!strcmp(command[j], "|")) erase(&command[j]);;
if (!morepipe)
{
_run(end);
return;
}
alternate = alternate ? false : true;
}
else perror("pipe()");
kind = middle;
}
};
}
//throw the pipekind and command to execute, after execute replace the command to next command
void _run(pipekind kind)
{
while (*command != NULL)
{
execute(command, kind);
for (i = 0; i < next; i++) if (command != NULL) erase(&command[i]);;
command = &command[next];
if (morepipe) break;
}
}
#define READ 0
#define WRITE 1
#define CHILD 0
#define PARENT default
#define previouspipe nextpipe
// use two pipe to get previous output and pass to current input and run execvp after using pipe close it to end the process
void execute(char **command, pipekind kind)
{
void (*getnextcommand)() = redirect;
switch (pid = fork())
{
case ERROR: perror("fork()"); break;
case CHILD:
if (*command == NULL) exit(EXIT_SUCCESS);
switch (kind)
{
case start:
dup2(currentpipe[WRITE], STDOUT_FILENO);
break;
case middle:
dup2(previouspipe[READ], STDIN_FILENO);
dup2(currentpipe[WRITE], STDOUT_FILENO);
break;
case end:
dup2(currentpipe[READ], STDIN_FILENO);
default: break;
}
redirect();
execvp(*command, command);
perror(*command);
_exit(EXIT_FAILURE);
PARENT:
switch (kind)
{
case start:
close(currentpipe[WRITE]);
break;
case middle:
close(previouspipe[READ]);
close(currentpipe[WRITE]);
break;
case end:
close(currentpipe[READ]);
default: break;
}
if (!background) while(wait(NULL) > 0);;
getnextcommand();
}
}
// check which sign it is and run the correspond function
void redirect()
{
size_t i;
for (i = 0; command[i] != NULL; i++)
{
if (!strcmp(command[i], "<")) redirect_in(i++);
else if (!strcmp(command[i], ">")) redirect_out(i++);
else if (!strcmp(command[i], "2>")) redirect_err(i++);
}
if (morepipe) i++;
next = i;
}
// redirect in
void redirect_in(size_t i)
{
if (pid == CHILD)
{
erase(&command[i]);
strcpy(input_filename, command[++i]);
erase(&command[i]);
infile = open(input_filename, O_RDONLY);
if (infile != ERROR)
{
dup2(infile, STDIN_FILENO);
close(infile);
}
else
{
perror(input_filename);
exit(EXIT_SUCCESS);
}
}
}
// redirect out
void redirect_out(size_t i)
{
if (pid == CHILD)
{
erase(&command[i]);
strcpy(output_filename, command[++i]);
erase(&command[i]);
outfile = open(output_filename, O_RDWR | O_CREAT, 0777);
if (outfile != ERROR)
{
dup2(outfile, STDOUT_FILENO);
close(outfile);
}
else
{
perror(output_filename);
exit(EXIT_SUCCESS);
}
}
}
// redirect error
void redirect_err(size_t i)
{
if (pid == CHILD)
{
erase(&command[i]);
strcpy(error_filename, command[++i]);
erase(&command[i]);
errfile = open(error_filename, O_RDWR | O_CREAT, 0777);
if (errfile != ERROR)
{
dup2(errfile, STDERR_FILENO);
close(errfile);
}
else
{
perror(error_filename);
exit(EXIT_SUCCESS);
}
}
}
//seperate the inputline into each string and store in command
void separate(char *commands)
{
size_t max = 1024;
char *token;
i = 0;
semicolons = 0;
fullcommand = malloc(sizeof(char *) * max);
token = strtok(commands, " ");
while (token != NULL)
{
if (!strcmp(token, ";"))
{
fullcommand[i] = NULL;
semicolons++;
}
else
{
fullcommand[i] = malloc(sizeof(char) * (strlen(token) + 1));
strcpy(fullcommand[i], token);
}
token = strtok(NULL, " ");
if (i == max) fullcommand = realloc(fullcommand, max *= 2);
i++;
}
fullcommand[i] = NULL;
if (!strcmp(fullcommand[--i], "&"))
{
background = true;
erase(&fullcommand[i]);
}
else background = false;
command = fullcommand;
}
// free
void erase(char **command)
{
free(*command);
*command = NULL;
}
// prompt
void prompt()
{
size_t i = 0, max = 4096;
char letter;
char *input = malloc(sizeof(char) * max);
printf("$ ");
for (i = 0; (letter = getchar()) != '\n'; i++)
{
if (!eof(letter)) input[i] = letter;
if (i == max) input = realloc(input, max *= 2);
}
input[i] = 0;
separate(input);
free(input);
}
bool eof(int c)
{
if (c == EOF)
{
putchar('\n');
exit(EXIT_SUCCESS);
}
else return false;
}