-
Notifications
You must be signed in to change notification settings - Fork 3
/
shell.c
274 lines (254 loc) · 5.21 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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <sys/types.h>
/*
Compilar con: gcc shell.c -o shell
Ejecutar con: ./shell
*/
#define MAX_LINE 80
#define MAX_ARGS 40
#define HISTORY_PATH ".history"
int p_wait;
int in_file, out_file;
int saved_in, saved_out;
int in, out;
int pipe_ind;
int save_c;
void parseInput(char *command, char **args)
{
int args_count = 0;
int command_len = strlen(command);
int arg_start = -1;
for(int i = 0; i < command_len; i++)
{
if(command[i] == ' ' || command[i] == '\t' || command[i] == '\n')
{
if(arg_start != -1)
{
args[args_count++] = &command[arg_start];
arg_start = -1;
}
command[i] = '\0';
}
else
{
if(command[i] == '&')
{
p_wait = 0;
i = command_len;
}
if(arg_start == -1) arg_start = i;
}
}
args[args_count] = NULL;
}
void checkFlags(char **args)
{
for(int i = 0; args[i] != NULL; i++)
{
if(!strcmp(args[i], ">"))
{
if(args[i+1] == NULL)
printf("Invalid command format\n");
else
out_file = i + 1;
}
if(!strcmp(args[i], "<"))
{
if(args[i+1] == NULL)
printf("Invalid command format\n");
else
in_file = i + 1;
}
if(!strcmp(args[i], "|"))
{
if(args[i+1] == NULL)
printf("Invalid command after |\n");
else
pipe_ind = i;
}
}
}
void manageHistory(char **args)
{
FILE* h = fopen(HISTORY_PATH, "r");
if(h == NULL)
{
printf("The history is empty\n");
}
else
{
if(args[1] == NULL)
{
char c = fgetc(h);
while(c != EOF)
{
printf ("%c", c);
c = fgetc(h);
}
}
else if(!strcmp(args[1], "-c"))
{
save_c = 0;
remove(HISTORY_PATH);
}
else
{
printf("[!] Invalid syntax");
}
fclose(h);
}
}
void execute(char **args)
{
if(execvp(args[0], args) < 0)
{
printf("[!] Command not found\n");
exit(1);
}
}
void saveCommand(char *command)
{
FILE* h = fopen(HISTORY_PATH, "a+");
fprintf(h, "%s", command);
rewind(h);
}
int main(void)
{
char command[MAX_LINE];
char last_command[MAX_LINE];
char parse_command[MAX_LINE];
char *args[MAX_ARGS];
char *argsp1[MAX_ARGS], *argsp2[MAX_ARGS];
int should_run = 1, history = 0;
int alert;
int pipech[2];
while(should_run)
{
printf("Jshell$ ");
fflush(stdout);
fgets(command, MAX_LINE, stdin);
p_wait = 1;
alert = 0;
out_file = in_file = -1;
pipe_ind = -1;
save_c = 1;
strcpy(parse_command, command);
parseInput(parse_command, args);
if(args[0] == NULL || !strcmp(args[0], "\0") || !strcmp(args[0], "\n")) continue;
if(!strcmp(args[0], "exit"))
{
should_run = 0;
continue;
}
if(!strcmp(args[0], "!!"))
{
if(history)
{
printf("%s", last_command);
strcpy(command, last_command);
strcpy(parse_command, command);
parseInput(parse_command, args);
}
else
{
printf("No commands in history \n");
continue;
}
}
checkFlags(args);
if(in_file != -1)
{
in = open(args[in_file], O_RDONLY);
if(in < 0)
{
printf("Failed to open file \'%s\'\n", args[in_file]);
alert = 1;
}
else
{
saved_in = dup(0);
dup2(in, 0);
close(in);
args[in_file - 1] = NULL;
}
}
if(out_file != -1)
{
out = open(args[out_file], O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IRGRP | S_IWGRP | S_IWUSR);
if(out < 0)
{
printf("Failed to open file \'%s\'\n", args[out_file]);
alert = 1;
}
else
{
saved_out = dup(1);
dup2(out, 1);
close(out);
args[out_file - 1] = NULL;
}
}
if(pipe_ind != -1)
{
int i = 0;
for(; i < pipe_ind; i++) argsp1[i] = args[i];
argsp1[i] = NULL;
i++;
for(; args[i] != NULL; i++) argsp2[i-pipe_ind-1] = args[i];
argsp2[i] = NULL;
}
if(!alert && should_run)
{
if(!strcmp(args[0], "history")) manageHistory(args);
else
{
if(!strcmp(args[0], "stop") || !strcmp(args[0], "continue"))
{
args[2] = args[1];
args[1] = strcmp(args[0], "stop") ? "-SIGCONT" : "-SIGSTOP";
args[0] = "kill";
args[3] = NULL;
}
if(fork() == 0)
{
if(pipe_ind != -1)
{
pipe(pipech);
if(fork() == 0)
{
saved_out = dup(1);
dup2(pipech[1], 1);
close(pipech[0]);
execute(argsp1);
}
else
{
wait(NULL);
saved_in = dup(0);
dup2(pipech[0], 0);
close(pipech[1]);
execute(argsp2);
}
}
else
execute(args);
}
else
{
if(p_wait) wait(NULL);
}
}
strcpy(last_command, command);
if(save_c) saveCommand(command);
history = 1;
}
dup2(saved_out, 1);
dup2(saved_in, 0);
}
return 0;
}