/
main.c
349 lines (293 loc) · 9.24 KB
/
main.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
339
340
341
342
343
344
345
346
347
348
349
/*
Nome: Rodrigo Fernandes da Costa
n. USP: 6793073
Nome: Lucas de Moraes Franco
n. USP: 6793239
*/
#include "main.h"
int main(void){
struct token token = {0};
struct command command = {0};
boolean end = false;
pid_t shell_pgid;
int terminal;
void *handle;
terminal = STDIN_FILENO;
while(tcgetpgrp(terminal) != (shell_pgid = getpgrp())){
kill(- shell_pgid,SIGTTIN);
}
shell_pgid = getpid();
if(setpgid(shell_pgid,shell_pgid) < 0){
printf("Erro");
exit(1);
}
tcsetpgrp(terminal,shell_pgid);
terminateCommand ( & command );
signal( SIGCHLD , terminateHandler );
stopCommand( & command );
signal ( SIGTSTP , stopHandler );
handle = dlopen("./libparser.so", RTLD_LAZY);
do{
((void(*)(struct token*))(dlsym(handle,"getTokens")))(&token);
((void(*)(struct token*))(dlsym(handle,"identifyTokens")))(&token);
if(!((boolean(*)(struct token*))(dlsym(handle,"checkSyntax")))(&token)) continue;
if(ECHO_TOKENS) ((void(*)(struct token*))(dlsym(handle,"echoTokens")))(&token);
if(ECHO_INPUT) ((void(*)(struct token*))(dlsym(handle,"echoInput")))(&token);
while(((boolean(*)(struct token*,struct command*))(dlsym(handle,"getCommand")))(&token, &command)){
if(ECHO_COMMAND) ((void(*)(struct command))(dlsym(handle,"echoCommand")))(*(command.next));
end = executeCommand(command.next);
}
}
while(!end);
((void(*)(struct command*))(dlsym(handle,"clearCommand")))(&command);
((void(*)(struct token*))(dlsym(handle,"clearToken")))(&token);
dlclose(handle);
return 0;
}
boolean executeCommand(struct command * command){
boolean end = false;
static int nextPipe[2];
static int previousPipe[2];
int pipeInput;
int pipeOutput;
pid_t pipeGroup;
boolean pipes;
if(command->pipeOutput){
pipe(nextPipe);
pipeOutput = nextPipe[1];
}
if(command->pipeInput){
pipeInput = previousPipe[0];
}
switch(command->type){
case commandExit:
case commandQuit:
case commandCd:
case commandFg:
end = executeAsParent(command, pipeInput, pipeOutput);
break;
default:
executeAsChild(command, pipeInput, pipeOutput);
}
if(command->pipeOutput){
close ( nextPipe[1] );
}
if(command->pipeInput){
close ( previousPipe[0] );
}
if(command->pipeInput || command->pipeOutput){
if(pipes){
setpgid(command->id,pipeGroup);
} else {
setpgid(command->id,command->id);
pipeGroup = command->id;
pipes = true;
}
} else {
pipes = false;
}
previousPipe[0] = nextPipe[0] ;
previousPipe[1] = nextPipe[1] ;
return end;
}
boolean executeAsParent(struct command * command, int pipeInput, int pipeOutput){
void *handle;
int fdInput = 0;
int fdOutput = 0;
int fdAppend = 0;
int stdoutSave;
int stdinSave;
boolean end = false;
if((command->input)[0]){
stdinSave = dup(0);
fdInput = open(command->input, O_RDONLY, 0666);
dup2(fdInput, 0);
}
if((command->output)[0]){
stdoutSave = dup(1);
fdOutput = open(command->output, O_RDWR | O_CREAT | O_TRUNC, 0666);
dup2(fdOutput, 1);
}
if((command->append)[0]){
stdoutSave = dup(1);
fdAppend = open(command->append, O_RDWR | O_CREAT | O_APPEND, 0666);
dup2(fdAppend, 1);
}
if(command->pipeInput){
stdinSave = dup(0);
dup2(pipeInput, 0);
}
if(command->pipeOutput){
stdoutSave = dup(1);
dup2(pipeOutput, 1);
}
switch(command->type){
case commandExit:
handle = dlopen("./libexit.so", RTLD_LAZY);
end = ((boolean(*)(struct command))(dlsym(handle,"shellExit")))(*command);
dlclose(handle);
break;
case commandQuit:
handle = dlopen("./libquit.so", RTLD_LAZY);
end = ((boolean(*)(struct command))(dlsym(handle,"shellQuit")))(*command);
dlclose(handle);
break;
case commandCd:
handle = dlopen("./libcd.so", RTLD_LAZY);
((void(*)(struct command))(dlsym(handle,"cd")))(*command);
dlclose(handle);
break;
case commandFg:
handle = dlopen("./libfg.so", RTLD_LAZY);
((void(*)(struct command))(dlsym(handle,"fg")))(*command);
dlclose(handle);
break;
}
if((command->input)[0]){
dup2(stdinSave,0);
close(fdInput);
}
if((command->output)[0]){
dup2(stdoutSave,1);
close(fdOutput);
}
if((command->append)[0]){;
dup2(stdoutSave,1);
close(fdAppend);
}
if(command->pipeInput){
dup2(stdinSave,0);
}
if(command->pipeOutput){
dup2(stdoutSave,1);
}
return end;
}
void executeAsChild(struct command * command, int pipeInput, int pipeOutput){
/* lib */
void * handle;
/* I/O */
int fdInput = 0;
int fdOutput = 0;
int fdAppend = 0;
/* process */
pid_t pid;
command->id = pid = fork();
if(pid < 0){ /* erro */
printf("error> in %s at line %d\n", __FILE__, __LINE__);
exit(1);
}
if(pid == 0){ /* filho */
if((command->input)[0]){
fdInput = open(command->input, O_RDONLY, 0666);
dup2(fdInput,0);
}
if((command->output)[0]){
fdOutput = open(command->output, O_RDWR | O_CREAT | O_TRUNC, 0666);
dup2(fdOutput,1);
}
if((command->append)[0]){
fdAppend = open(command->append, O_RDWR | O_CREAT | O_APPEND, 0666);
dup2(fdAppend,1);
}
if(command->pipeInput){
dup2(pipeInput,0);
}
if(command->pipeOutput){
dup2(pipeOutput,1);
}
switch(command->type){
case commandSystem:
handle = dlopen("./libsystem.so", RTLD_LAZY);
((void(*)(struct command))(dlsym(handle,"shellSystem")))(*command);
dlclose(handle);
break;
case commandPwd:
handle = dlopen("./libpwd.so", RTLD_LAZY);
((void(*)(struct command))(dlsym(handle,"pwd")))(*command);
dlclose(handle);
break;
case commandJobs:
handle = dlopen("./libjobs.so", RTLD_LAZY);
((void(*)(struct command))(dlsym(handle,"jobs")))(*command);
dlclose(handle);
break;
case commandBg:
handle = dlopen("./libbg.so", RTLD_LAZY);
((void(*)(struct command))(dlsym(handle,"bg")))(*command);
dlclose(handle);
break;
case commandHistory:
handle = dlopen("./libhistory.so", RTLD_LAZY);
((void(*)(struct command))(dlsym(handle,"history")))(*command);
dlclose(handle);
break;
case commandKill:
handle = dlopen("./libkill.so", RTLD_LAZY);
((void(*)(struct command))(dlsym(handle,"shellKill")))(*command);
dlclose(handle);
break;
}
if(fdInput) close(fdInput);
if(fdOutput) close(fdOutput);
if(fdAppend) close(fdAppend);
exit(0);
}
if(pid > 0){ /* pai */
if(command->background){
printf("jobs> %s running as %d \n" , command -> argument . string , command -> id);
waitpid(-1,NULL,WNOHANG|WUNTRACED);
}else{
command -> status = foreground;
waitpid(command->id,NULL,WUNTRACED);
if(command -> status != suspended){
command -> status = finish;
}
fflush(stdin);
}
}
}
void stopCommand(struct command * argCommand){
static boolean initialized = false;
static struct command * headCommand;
struct command * command;
int status;
if(!initialized){
headCommand = argCommand;
initialized = true;
return;
}
command = headCommand ;
while(command = command -> next){
if(command->status == foreground){
command->status = suspended;
printf("jobs> %s suspended as %d \n" , command -> argument . string , command -> id);
kill(command->id, SIGSTOP);
}
}
}
void stopHandler(int signalNumber){
stopCommand(NULL);
}
void terminateCommand(struct command * argCommand){
static boolean initialized = false;
static struct command * headCommand;
struct command * command;
int status;
if(!initialized){
headCommand = argCommand;
initialized = true;
return;
}
command = headCommand ;
while(command = command -> next){
if(command->status == running || command->status == suspended){
if(waitpid(command->id,&status,WNOHANG|WUNTRACED) && ( WIFEXITED(status) || WIFSIGNALED(status))){
command->status = finish;
}
}
}
}
void terminateHandler(int signalNumber){
terminateCommand(NULL);
}