-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemmerss_assignment4.c
More file actions
261 lines (227 loc) · 8.2 KB
/
demmerss_assignment4.c
File metadata and controls
261 lines (227 loc) · 8.2 KB
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
/*
Stephan Demmers
Assignment 4 SmallSH
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <signal.h>
// Predefined constants per the specification
#define INPUT_LENGTH 2048
#define MAX_ARGS 512
// Global variables
int last_status = 0; // For storing exit status or last foreground cmd
bool foreground_only_mode = false; // Helps toggle background only mode
// Function declarations
void handle_SIGTSTP(int signo);
void run_shell();
/*
Main entry of the shell and helps set up the signal handles for INT
and TSTP and then enters the shell loop and will eventually return
the exit status of the program.
*/
int main() {
struct sigaction SIGTSTP_action = {0}, SIGINT_action = {0};
SIGTSTP_action.sa_handler = handle_SIGTSTP;
sigfillset(&SIGTSTP_action.sa_mask);
SIGTSTP_action.sa_flags = SA_RESTART;
sigaction(SIGTSTP, &SIGTSTP_action, NULL);
SIGINT_action.sa_handler = SIG_IGN;
sigaction(SIGINT, &SIGINT_action, NULL);
run_shell();
return 0;
}
// Struct definition reused from sample parser
struct command_line {
char *argv[MAX_ARGS + 1];
int argc;
char *input_file;
char *output_file;
bool is_bg;
};
/*
Function to free memory from a parsed command that was entered.
Takes in a pointer to the command_line struct.
*/
void free_command(struct command_line *cmd) {
for (int i = 0; i < cmd->argc; i++) {
free(cmd->argv[i]);
}
free(cmd->input_file);
free(cmd->output_file);
free(cmd);
}
/*
Parser adapted from the sample given. Puts user input into a
command_line struct and handles I/O redirection signals as well
as backgrounding processes and tokenizes arguments. Returns a pointer
to a dynamically allocated command_line struct. Returns NULL for
all blank and comment lines.
*/
struct command_line *parse_input() {
char input[INPUT_LENGTH];
struct command_line *curr_command = calloc(1, sizeof(struct command_line));
printf(": ");
fflush(stdout);
// Used to handle input errors
if (fgets(input, INPUT_LENGTH, stdin) == NULL) {
clearerr(stdin);
return NULL;
}
// Ignores blank lines and comments
if (input[0] == '\n' || input[0] == '#') {
return NULL;
}
char *token = strtok(input, " \n");
while (token) {
if (!strcmp(token, "<")) {
curr_command->input_file = strdup(strtok(NULL, " \n"));
} else if (!strcmp(token, ">")) {
curr_command->output_file = strdup(strtok(NULL, " \n"));
} else if (!strcmp(token, "&")) {
curr_command->is_bg = true;
} else {
curr_command->argv[curr_command->argc++] = strdup(token);
}
token = strtok(NULL, " \n");
}
return curr_command;
}
/*
Signal Handler for SIGTSTP that helps to toggle foreground-only
mode. Prints a message letting the user know what mode they're in
and then reprompt them for a command. Takes in a signal number.
*/
void handle_SIGTSTP(int signo) {
if (!foreground_only_mode) {
char *msg = "\nEntering foreground-only mode (& is now ignored)\n";
write(STDOUT_FILENO, msg, 50);
foreground_only_mode = true;
} else {
char *msg = "\nExiting foreground-only mode\n";
write(STDOUT_FILENO, msg, 30);
foreground_only_mode = false;
}
// Reprompt the user
char *prompt = ": ";
write(STDOUT_FILENO, prompt, 2);
}
/*
Main shell loop that handles command line input from a user. It
also reaps background processes, parses input, handles the built-in
exit, cd, and status commands, and forks a new child process for any
non built-in commands. Manages foreground and background execution and
I/O redirections.
*/
void run_shell() {
while (1) {
// Periodically checks for any completed background processes
pid_t bg_pid;
int bg_status;
while ((bg_pid = waitpid(-1, &bg_status, WNOHANG)) > 0) {
if (WIFEXITED(bg_status)) {
printf("background pid %d is done: exit value %d\n", bg_pid, WEXITSTATUS(bg_status));
} else if (WIFSIGNALED(bg_status)) {
printf("background pid %d is done: terminated by signal %d\n", bg_pid, WTERMSIG(bg_status));
}
fflush(stdout);
}
struct command_line *cmd = parse_input();
// Used to skip blank lines and comments
if (cmd == NULL) {
continue;
}
// Built-in exit command
if (strcmp(cmd->argv[0], "exit") == 0) {
free_command(cmd);
exit(0);
}
// Built-in cd command
else if (strcmp(cmd->argv[0], "cd") == 0) {
char *target_dir = NULL;
if (cmd->argc == 1) {
target_dir = getenv("HOME"); // Go to home dir if no args
} else {
target_dir = cmd->argv[1];
}
if (chdir(target_dir) != 0) {
perror("cd");
}
}
// Built-in status command
else if (strcmp(cmd->argv[0], "status") == 0) {
if (WIFEXITED(last_status)) {
printf("exit value %d\n", WEXITSTATUS(last_status));
} else if (WIFSIGNALED(last_status)) {
printf("terminated by signal %d\n", WTERMSIG(last_status));
}
}
else {
pid_t spawnpid = fork();
int child_status;
switch (spawnpid) {
case -1:
perror("fork");
break;
case 0:
// Restore SIGINT default if this is a foreground child
if (!cmd->is_bg || foreground_only_mode) {
struct sigaction SIGINT_default = {0};
SIGINT_default.sa_handler = SIG_DFL;
sigaction(SIGINT, &SIGINT_default, NULL);
}
// Handles the input redirection
if (cmd->input_file) {
int input_fd = open(cmd->input_file, O_RDONLY);
if (input_fd == -1) {
fprintf(stderr, "cannot open %s for input\n", cmd->input_file);
exit(1);
}
dup2(input_fd, 0);
close(input_fd);
} else if (cmd->is_bg && !foreground_only_mode) {
int devnull_fd = open("/dev/null", O_RDONLY);
dup2(devnull_fd, 0);
close(devnull_fd);
}
// Handles the output redirection
if (cmd->output_file) {
int output_fd = open(cmd->output_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (output_fd == -1) {
fprintf(stderr, "cannot open %s for output\n", cmd->output_file);
exit(1);
}
dup2(output_fd, 1);
close(output_fd);
} else if (cmd->is_bg && !foreground_only_mode) {
int devnull_fd = open("/dev/null", O_WRONLY);
dup2(devnull_fd, 1);
close(devnull_fd);
}
execvp(cmd->argv[0], cmd->argv);
fprintf(stderr, "%s: no such file or directory\n", cmd->argv[0]);
exit(1);
break;
default:
if (cmd->is_bg && !foreground_only_mode) {
printf("background pid is %d\n", spawnpid);
fflush(stdout);
} else {
spawnpid = waitpid(spawnpid, &child_status, 0);
last_status = child_status;
if (WIFSIGNALED(child_status)) {
printf("terminated by signal %d\n", WTERMSIG(child_status));
fflush(stdout);
}
}
break;
}
}
free_command(cmd);
}
}