-
Notifications
You must be signed in to change notification settings - Fork 0
/
lsh.c
333 lines (270 loc) · 6.75 KB
/
lsh.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wait.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define LSH_BUFSIZE 512
#define LSH_TOKEN_DELIMITERS " \t\r\n"
char ** lsh_split(char *, char *);
void ctrl_c_handler(int dummy)
{
printf("\n");
printf("> ");
fflush(STDIN_FILENO);
}
int lsh_inbuilt_cd(char ** args) {
if(args[1] == NULL) {
fprintf(stderr, "lsh: cd expected a parameter\n");
} else {
if(chdir(args[1]) != 0) {
perror("lsh");
}
}
return EXIT_SUCCESS;
}
int lsh_inbuilt_exit(char ** args) {
exit(EXIT_SUCCESS);
}
int (*lsh_inbuilt_functions[])(char **) = {
&lsh_inbuilt_cd,
&lsh_inbuilt_exit
};
char * lsh_inbuilt_names[] = {
"cd",
"exit"
};
int lsh_inbuilt_functions_size = (sizeof(lsh_inbuilt_names) / sizeof(char*));
pid_t spawn_process(char ** args, int in, int out, int err)
{
pid_t pid;
int status;
pid = fork();
if(pid == 0) {
// fprintf(stdout, "%s: IN[%i] --> OUT[%i]\n", args[0], in, out);
if(in != STDIN_FILENO) {
dup2(in, STDIN_FILENO);
close(in);
}
if(out != STDOUT_FILENO) {
dup2(out, STDOUT_FILENO);
close(out);
}
if(err != STDERR_FILENO) {
dup2(err, STDERR_FILENO);
close(err);
}
if(execvp(args[0], args) == -1) {
fprintf(stderr, "lsh:spawn:execvp = %s\n", args[0]);
}
exit(EXIT_FAILURE);
}
return pid;
}
int lsh_launch(char ** commands, int n, int run_in_bg, int redirect_type, char * redirect_file)
{
char ** com_args;
pid_t pid;
pid_t wpid;
int status = EXIT_SUCCESS;
int i = 0;
// com_args = lsh_split(commands[0], LSH_TOKEN_DELIMITERS);
int fd[2];
int in = STDIN_FILENO;
int out = STDOUT_FILENO;
int err = STDERR_FILENO;
for (i = 0; i < n - 1; ++i)
{
com_args = lsh_split(commands[i], LSH_TOKEN_DELIMITERS);
if(pipe(fd)) {
perror("lsh:launch:pipe");
return 1;
}
// printf("%i zapisuje do %i\n", fd[STDOUT_FILENO], fd[STDIN_FILENO]);
pid = spawn_process(com_args, in, fd[STDOUT_FILENO], err);
close(fd[STDOUT_FILENO]);
in = fd[STDIN_FILENO];
}
// Kod ktory zabral mi 8h mojego zycia
// dup2 byl wykonywany rowniez w funkcji spawn_process
// co prowadzilo do... nie wiem czego, ale chyba zamykalo pipe'a
// z ktorego potem szly same EOF'y
// if (in != STDIN_FILENO) {
// dup2(in, STDIN_FILENO);
// }
com_args = lsh_split(commands[i], LSH_TOKEN_DELIMITERS);
// Check if not empty
if (com_args[0] == NULL) {
// An empty command was entered.
return EXIT_SUCCESS;
}
for (i = 0; i < lsh_inbuilt_functions_size; i++) {
if (strcmp(com_args[0], lsh_inbuilt_names[i]) == 0) {
return (*lsh_inbuilt_functions[i])(com_args);
}
}
if(redirect_type >= 0) {
switch(redirect_type) {
case STDOUT_FILENO:
out = creat(redirect_file, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if(out < 0) {
perror("lsh:launch:fileopen:out");
return 1;
}
break;
case STDIN_FILENO:
in = open(redirect_file, O_RDONLY);
if(in < 0) {
perror("lsh:launch:fileopen:in");
return 1;
}
break;
case STDERR_FILENO:
printf("STDERR to %s\n", redirect_file);
err = creat(redirect_file, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if(out < 0) {
perror("lsh:launch:fileopen:err");
return 1;
}
break;
}
}
pid = spawn_process(com_args, in, out, err);
// wait (or dont) for process
if(!run_in_bg) {
do {
wpid = waitpid(pid, &status, WUNTRACED);
} while(!WIFEXITED(status) && !WIFSIGNALED(status));
}
return 1;
}
char * lsh_readline()
{
int bufsize = LSH_BUFSIZE;
char * buffer = malloc(sizeof(char) * bufsize);
int c;
int position = 0;
if(!buffer) {
perror("lsh:readline:malloc");
exit(EXIT_FAILURE);
}
while(1) {
c = getchar();
if(position == 0 && c == EOF) {
printf("\nBye :)\n");
exit(EXIT_SUCCESS);
} else
if(c == '\n' || c == EOF) {
buffer[position] = '\0';
return buffer;
} else {
buffer[position] = c;
}
position++;
}
}
char ** lsh_split(char * line, char * delims)
{
int bufsize = LSH_BUFSIZE;
char * token;
char ** tokens = malloc(sizeof(char*) * bufsize);
char ** tokens_temp;
if(!tokens) {
perror("lsh:split:malloc");
exit(EXIT_FAILURE);
}
token = strtok(line, delims);
int pos = 0;
while(token != NULL) {
tokens[pos] = token;
pos++;
if(pos >= bufsize) {
tokens_temp = tokens;
bufsize += LSH_BUFSIZE;
tokens = realloc(tokens, sizeof(char*) * bufsize);
if(!tokens) {
perror("lsh:split:realloc");
free(tokens_temp);
exit(EXIT_FAILURE);
}
}
token = strtok(NULL, delims);
}
// add NULL at the end to signal last element
tokens[pos] = NULL;
return tokens;
}
void lsh_loop()
{
char * line;
char ** commands;
int status;
int comnum;
int run_in_bg;
char * redirect_file;
int redirect_type = -1;
char * ampersand_pos = NULL;
char * redirect_pos = NULL;
do {
redirect_type = -1; // -1 means no redirect
run_in_bg = 0;
ampersand_pos = NULL;
redirect_pos = NULL;
printf("> ");
line = lsh_readline();
if(ampersand_pos = strchr(line, '&')) {
// remove ampersand from args and replace it with null byte
*ampersand_pos = '\0';
run_in_bg = 1;
}
if(redirect_pos = strchr(line, '<')) {
redirect_type = STDIN_FILENO;
*redirect_pos = '\0';
redirect_file = redirect_pos+1;
while(*redirect_file == ' ') {
redirect_file++;
}
}
if(redirect_pos = strchr(line, '2')) {
if(*(redirect_pos+1) == '>') {
redirect_type = STDERR_FILENO;
*redirect_pos = '\0';
*(redirect_pos+1) = '\0';
redirect_file = redirect_pos+2;
while(*redirect_file == ' ') {
redirect_file++;
}
}
} else if(redirect_pos = strchr(line, '>')) {
redirect_type = STDOUT_FILENO;
*redirect_pos = '\0';
redirect_file = redirect_pos+1;
while(*redirect_file == ' ') {
redirect_file++;
}
}
commands = lsh_split(line, "|");
for(comnum = 0; commands[comnum] != NULL; comnum++);
status = lsh_launch(commands, comnum, run_in_bg, redirect_type, redirect_file);
free(line);
// for(int i = 0; commands[i] != NULL; i++)
// free(commands[i]);
} while(1);
}
int main()
{
printf("LSH Alpha\n");
// handle CTRL+C
signal(SIGINT, ctrl_c_handler);
// remove zombies
struct sigaction sigchld_action = {
.sa_handler = SIG_DFL,
.sa_flags = SA_NOCLDWAIT
};
sigaction(SIGCHLD, &sigchld_action, NULL);
lsh_loop();
return 0;
}