-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell.c
404 lines (368 loc) · 11.2 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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/* Name: Daan Rosendal
* Student number: 15229394
* Study: Bachelor HBO-ICT (Software Engineering) at Windesheim in Zwolle. I follow Operating
* Systems as a "bijvak".
*
* This file contains the implementation of the shell. The shell is an interactive command-line
* interpreter that can execute commands. The shell supports the following functionalities:
* - External commands
* - Built-in commands: exit, cd
* - Sequences
* - Pipes
* - Redirects
* - Detached commands
* - Subshells
* - Environment variables (using set and unset)
*/
#define _POSIX_C_SOURCE 200112L
#include "shell.h"
#include "arena.h"
#include "front.h"
#include "parser/ast.h"
#include <fcntl.h>
#include <pwd.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
/* Signal handler for SIGINT.
*
* This function is called when the SIGINT signal is received (e.g., when Ctrl+C is pressed). It
* sets up the signal handler to call itself again, ensuring that SIGINT is consistently handled.
*/
void sigint_handler() { signal(SIGINT, sigint_handler); }
/* Initialize the shell.
*
* This function sets up the signal handler for SIGINT, ensuring that the shell consistently handles
* the interrupt signal.
*/
void initialize(void) { signal(SIGINT, sigint_handler); }
/* Clean up the shell.
*
* This function is called when the shell is about to exit. It performs any necessary cleanup.
*/
void shell_exit(void) {}
/* Execute a sequence of commands.
*
* This function runs the first command in the sequence, followed by the second command.
*
* node: the AST node representing the sequence of commands
*/
void execute_sequence_command(node_t *node) {
run_command(node->sequence.first);
run_command(node->sequence.second);
}
/* Execute a subshell command.
*
* This function creates a child process to execute the command within a subshell.
*
* node: the AST node representing the subshell command
*/
void execute_subshell_command(node_t *node) {
pid_t pid = fork();
if (pid < 0) {
perror("fork");
exit(EXIT_FAILURE);
} else if (pid == 0) { // Child process
run_command(node->subshell.child);
exit(EXIT_SUCCESS);
} else { // Parent process
int status;
waitpid(pid, &status, 0);
}
}
// These constants are used to index the read and write ends of a pipe.
const int PIPE_INPUT = 0;
const int PIPE_OUTPUT = 1;
/* Fork processes for each command in a pipeline.
*
* This function creates a child process for each command in a pipeline, setting up the necessary
* pipes for communication between the processes.
*
* node: the AST node representing the pipeline command
* pipe_fds: an array of pipe file descriptors
* pipe_count: the number of pipes in the pipeline
* pipeline_commands: an array of AST nodes representing the commands in the pipeline
*/
void fork_processes(node_t *node, int pipe_fds[][2], size_t pipe_count,
node_t **pipeline_commands) {
for (size_t i = 0; i < node->pipe.n_parts; i++) {
pid_t pid = fork();
if (pid == -1) {
perror("fork");
exit(EXIT_FAILURE);
} else if (pid == 0) { // Child process
// Close unused write ends of previous pipes
for (size_t j = 0; j < i; j++) {
close(pipe_fds[j][PIPE_OUTPUT]);
}
// Close unused read ends of subsequent pipes
for (size_t j = i + 1; j < pipe_count; j++) {
close(pipe_fds[j][PIPE_INPUT]);
close(pipe_fds[j][PIPE_OUTPUT]);
}
// Redirect input/output
if (i != 0) {
dup2(pipe_fds[i - 1][PIPE_INPUT], STDIN_FILENO);
close(pipe_fds[i - 1][PIPE_INPUT]);
}
if (i != pipe_count) {
dup2(pipe_fds[i][PIPE_OUTPUT], STDOUT_FILENO);
close(pipe_fds[i][PIPE_OUTPUT]);
}
run_command(pipeline_commands[i]);
exit(EXIT_SUCCESS);
}
}
}
/* Execute a pipeline command.
*
* This function creates a pipeline of commands, with the output of each command connected to the
* input of the next command.
*
* node: the AST node representing the pipeline command
*/
void execute_pipe_command(node_t *node) {
size_t pipe_count = node->pipe.n_parts - 1;
int pipe_fds[pipe_count][2];
// Create pipes
for (size_t i = 0; i < pipe_count; i++) {
if (pipe(pipe_fds[i]) == -1) {
perror("pipe");
exit(EXIT_FAILURE);
}
}
// Fork processes for each command in the pipeline
fork_processes(node, pipe_fds, pipe_count, node->pipe.parts);
// Close all pipe ends in the parent process
for (size_t i = 0; i < pipe_count; i++) {
close(pipe_fds[i][PIPE_INPUT]);
close(pipe_fds[i][PIPE_OUTPUT]);
}
// Wait for all child processes to finish
for (size_t i = 0; i < node->pipe.n_parts; i++) {
int status;
wait(&status);
}
}
/* Execute a detached command.
*
* This function creates a child process to execute the command in the background, without waiting
* for its completion.
*
* node: the AST node representing the detached command
*/
void execute_detach_command(node_t *node) {
pid_t pid = fork();
if (pid == -1) {
perror("fork");
exit(EXIT_FAILURE);
} else if (pid == 0) { // Child process
run_command(node->detach.child);
exit(EXIT_SUCCESS);
}
// Parent process continues without waiting for the child
}
/* Open a file for redirection.
*
* This function opens a file for redirection based on the specified mode.
*
* node: the AST node representing the redirect command
*
* Returns:
* the file descriptor of the opened file
*/
int open_file_for_redirect(node_t *node) {
int fd;
if (node->redirect.mode == REDIRECT_APPEND) {
fd = open(node->redirect.target, O_WRONLY | O_CREAT | O_APPEND, 0644);
} else if (node->redirect.mode == REDIRECT_OUTPUT) {
fd = open(node->redirect.target, O_WRONLY | O_CREAT | O_TRUNC, 0644);
} else if (node->redirect.mode == REDIRECT_INPUT) {
fd = open(node->redirect.target, O_RDONLY);
} else if (node->redirect.mode == REDIRECT_DUP) {
fd = node->redirect.fd2;
} else {
perror("Invalid redirect mode");
exit(EXIT_FAILURE);
}
return fd;
}
/* Execute a redirect command.
*
* This function creates a child process to execute the command with input/output redirected as
* specified.
*
* node: the AST node representing the redirect command
*/
void execute_redirect_command(node_t *node) {
pid_t pid = fork();
if (pid == -1) {
perror("fork");
exit(EXIT_FAILURE);
} else if (pid == 0) { // Child process
int fd = open_file_for_redirect(node);
if (fd == -1) {
perror("open");
exit(EXIT_FAILURE);
}
if (dup2(fd, node->redirect.fd) == -1) {
perror("dup2");
close(fd);
exit(EXIT_FAILURE);
}
if (node->redirect.fd != fd) {
close(fd);
}
run_command(node->redirect.child);
exit(EXIT_FAILURE);
} else { // Parent process
int status;
waitpid(pid, &status, 0);
}
}
/* Execute an exit command.
*
* This function exits the shell with the specified exit code.
*
* node: the AST node representing the exit command
*/
void execute_exit_command(node_t *node) {
if (node->command.argc == 1) {
exit(42);
} else {
exit(atoi(node->command.argv[1]));
}
}
/* Execute a cd command.
*
* This function changes the current working directory as specified by the command arguments.
*
* node: the AST node representing the cd command
*/
void execute_cd_command(node_t *node) {
if (node->command.argc == 1) {
const char *home = getenv("HOME");
if (home != NULL) {
chdir(home);
}
} else {
chdir(node->command.argv[1]);
}
}
/* Execute a set command.
*
* This function sets an environment variable with the specified name and value.
*
* node: the AST node representing the set command
*/
void execute_set_command(node_t *node) {
if (node->command.argc != 2) {
perror("Usage: set <env_var=value>");
exit(EXIT_FAILURE);
} else {
char *env_var = strtok(node->command.argv[1], "=");
char *value = strtok(NULL, "=");
if (env_var == NULL || value == NULL) {
perror("Invalid format. Usage: set <env_var=value>");
exit(EXIT_FAILURE);
}
if (setenv(env_var, value, 1) != 0) {
perror("setenv");
exit(EXIT_FAILURE);
}
}
}
/* Execute an unset command.
*
* This function unsets (removes) the specified environment variable.
*
* node: the AST node representing the unset command
*/
void execute_unset_command(node_t *node) {
if (node->command.argc < 2) {
perror("Usage: unset <variable>");
exit(EXIT_FAILURE);
} else {
if (unsetenv(node->command.argv[1]) != 0) {
perror("unsetenv");
exit(EXIT_FAILURE);
}
}
}
/* Execute an external command.
*
* This function executes an external command using execvp in a child process.
*
* node: the AST node representing the external command
*/
void execute_external_command(node_t *node) {
pid_t pid = fork();
if (pid < 0) {
perror("fork");
exit(EXIT_FAILURE);
} else if (pid == 0) { // Child process
execvp(node->command.program, node->command.argv);
perror("execvp");
exit(EXIT_FAILURE);
} else { // Parent process
int status;
signal(SIGINT, SIG_IGN);
waitpid(pid, &status, 0);
}
}
/* Execute a simple command.
*
* This function determines the type of simple command (e.g., exit, cd, set, unset, external) and
* executes it accordingly.
*
* node: the AST node representing the simple command
*/
void execute_simple_command(node_t *node) {
if (strcmp(node->command.program, "exit") == 0) {
execute_exit_command(node);
} else if (strcmp(node->command.program, "cd") == 0) {
execute_cd_command(node);
} else if (strcmp(node->command.program, "set") == 0) {
execute_set_command(node);
} else if (strcmp(node->command.program, "unset") == 0) {
execute_unset_command(node);
} else {
execute_external_command(node);
}
}
/* Run a command.
*
* This function dispatches the execution of different types of commands based on the type of the
* AST node.
*
* node: the AST node representing the command to execute
*/
void run_command(node_t *node) {
arena_push();
switch (node->type) {
case NODE_SEQUENCE:
execute_sequence_command(node);
break;
case NODE_SUBSHELL:
execute_subshell_command(node);
break;
case NODE_PIPE:
execute_pipe_command(node);
break;
case NODE_DETACH:
execute_detach_command(node);
break;
case NODE_REDIRECT:
execute_redirect_command(node);
break;
case NODE_COMMAND:
execute_simple_command(node);
break;
default:
perror("Invalid command type");
exit(EXIT_FAILURE);
}
arena_pop();
}