-
Notifications
You must be signed in to change notification settings - Fork 0
/
lsh.c
411 lines (360 loc) · 9.52 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
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
405
406
407
408
409
410
411
/*
* Main source code file for lsh shell program
*/
/* Defining that we are running on a POSIX system */
#define _POSIX_C_SOURCE 1
/* Includes */
#include <stdio.h>
#include <stdlib.h>
#include <readline/readline.h>
#include <readline/history.h>
#include "parse.h"
#include <unistd.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <string.h>
#include <signal.h>
#include <sys/types.h>
/* Function declarations */
void PrintCommand(int, Command *);
void PrintPgm(Pgm *);
void stripwhite(char *);
void childhandler(int signal_nr);
void interrupthandler(int signal_nr);
void sigtstp_handler(int signal_nr);
char* concat(char *s1, char *s2);
void runPgm(Pgm *nextPgm);
/* When non-zero, this global means the user is done using this program. */
int done = 0;
/* Variables for handling processes
* Needs to be global since we use them
* in many functions. Could maybe
* pass them to the functions instead?
*/
pid_t pid;
int status;
/*
* Name: main
*
* Description: Gets the ball rolling...
*
*/
int main(void)
{
Command cmd;
int n;
/*
* Struct for background processes, listen for SIGCHILD signals
* then call childhandler() in order to terminate them.
* This is done in order to avoid zombies.
*/
struct sigaction sigchld_action;
memset (&sigchld_action, 0, sizeof (sigchld_action));
sigchld_action.sa_handler = &childhandler;
/* sigaction(SIGCHLD, &sigchld_action, NULL); */
/*
* Struct for interrupting processes without
* exiting our lsh shell.
* Handles SIGINT signals.
*/
struct sigaction sigint_action;
memset(&sigint_action, 0, sizeof (sigint_action));
sigint_action.sa_handler = &interrupthandler;
sigaction(SIGINT, &sigint_action, NULL);
/*
* Saving STDOUT and STDIN in case we need to restore
* them later in the program.
*/
int saved_stdout;
saved_stdout = dup(STDOUT_FILENO);
int saved_stdin;
saved_stdin = dup(STDIN_FILENO);
/* Main loop of the program */
while(!done){
char *line;
line = readline("> ");
if(!line){
/* Encountered EOF at top level */
done = 1;
}
else{
/*
* Remove leading and trailing whitespace from the line
* Then, if there is anything left, add it to the history list
* and execute it.
*/
stripwhite(line);
if(*line){
add_history(line);
n = parse(line, &cmd);
/* Print the command for debugging
* purposes. Not used in "release".
*/
//PrintCommand(n, &cmd);
/* Does the user want to exit shell? */
if(strcmp(cmd.pgm->pgmlist[0],"exit") == 0){
return 0;
}
/* Does the user want to change directory? */
else if(strcmp(cmd.pgm->pgmlist[0],"cd") == 0){
if(cmd.pgm->pgmlist[1] != NULL){
if((chdir(cmd.pgm->pgmlist[1])) == -1){
perror("cd failed");
}
}
else{
/* If the user does not specify a path, go to HOME */
chdir(getenv("HOME"));
}
}
/* Spawning a new process in order to handle commands */
else{
/*
* Check if the rstdin or rstdout is set and then
* redirect the input/output to that file.
* Should maybe be moved to the child process instead
* since we always need to restore them at the moment?
*/
if(cmd.rstdin != NULL){
FILE * input;
input = fopen(cmd.rstdin,"r");
dup2(fileno(input), STDIN_FILENO);
fclose(input);
}
if(cmd.rstdout != NULL){
FILE * output;
output = fopen(cmd.rstdout,"w+");
dup2(fileno(output), STDOUT_FILENO);
fclose(output);
}
/* Forking child process to handle commands */
if((pid = fork()) < 0){
perror("Failed to fork()");
exit(EXIT_FAILURE);
}
else if(pid == 0){
if(cmd.background){
setpgid(pid, pid);
}
/* Initiate the recursive function */
Pgm *nextPgm;
nextPgm = cmd.pgm;
runPgm(nextPgm);
}
/* This is the parent process. */
else{
if(cmd.background){
printf("Spawned process %d in background\n", pid);
/* signal(SIGCHLD, childhandler) */
sigaction(SIGCHLD, &sigchld_action, NULL);
/* setpgid(pid, pid); */
}
else{
/* Wait for the process to complete */
if (waitpid (pid, &status, 0) != pid){
status = -1;
}
}
/* Restore terminal in case we piped stdout or stdin somewhere */
dup2(saved_stdin, STDIN_FILENO);
dup2(saved_stdout, STDOUT_FILENO);
}
}
}
}
if(line){
free(line);
}
}
return 0;
}
/*
* Name: runPgm
* Arguments: nextPgm in order
* Description: Executes a chain of Pgms
* recursively.
*/
void runPgm(Pgm *nextPgm){
/*
* Base case of function
* This happens when we reached the end
* of the chain since there is no more
* commands to run after this.
*/
if(nextPgm->next == NULL){
if(execvp(nextPgm->pgmlist[0], nextPgm->pgmlist) < 0){
char * err_msg = concat("Execution of command failed: ", nextPgm->pgmlist[0]);
perror(err_msg);
free(err_msg);
exit(EXIT_FAILURE);
}
}
/* Rest of the cases where we
* will spawn a child that
* executes command for us
* before we execute our
* command.
*/
else{
/* Create variables for piping input/output */
int fds[2];
if(pipe(fds) != 0){
perror("Pipe error");
}
/* Should maybe create a define for these?
* instead of creating ints and referencing
* them to the fds.
*/
int read_fd = fds[0];
int write_fd = fds[1];
/* Fork a new process */
if ((pid = fork()) < 0){
perror("Failed to fork()");
exit(EXIT_FAILURE);
}
/* We are a child, duplicate FDs
* and run the function again
*/
else if(pid == 0){
dup2(write_fd, STDOUT_FILENO);
close(write_fd);
nextPgm = nextPgm->next;
runPgm(nextPgm);
}
/*
* We are a parent, wait
* for the child before
* executing.
*/
else{
dup2(read_fd, STDIN_FILENO);
close(read_fd);
close(write_fd);
/*
* We need to wait for our child to execute
* first since we want it's output
*/
if(waitpid(pid, &status, 0) != pid) {
status = -1; // error
}
if(execvp(nextPgm->pgmlist[0], nextPgm->pgmlist) < 0){
char * err_msg = concat("Execution of command failed: ", nextPgm->pgmlist[0]);
perror(err_msg);
free(err_msg);
exit(EXIT_FAILURE);
}
}
}
}
/*
* Name: childhandler
* Arguments: Number of signal (ID)
* Description: Handle termination
* of children in order to avoid zombies.
*/
void childhandler(int signal_nr){
int childstatus;
pid_t childpid;
/* childpid = wait(&childstatus); */
while(waitpid((pid_t)-1, 0, WNOHANG) > 0){
} /*
if (childpid == -1){
char pidstring[6]; // Assuming no longer PIDs than 6
sprintf(pidstring, "%d", childpid);
char * err_msg = concat("Could not shutdown child:", pidstring);
perror(err_msg);
free(err_msg);
exit(EXIT_FAILURE);
}
else{
/* This is only written for debugging purposes */
//printf("Child with pid: %d has terminated with status: %d\n", childpid, childstatus );
}
/*
* Name: interrupthandler
* Arguments: Number of signal (ID)
* Description: Handles CTRL+C
* commands issued in Shell.
*/
void interrupthandler(int signal_nr){
//printf("Do nothing...");
signal(SIGINT, SIG_IGN);
}
void sigtstp_handler(int signal_nr){
//printf("Do nothing...");
}
/*
* Name: concat
* Arguments: Pointer to string s1 and s2
* Returns: Result
* Description: Concatenetes two strings
* and returns them.
*/
char* concat(char *s1, char *s2){
char *result = malloc(strlen(s1)+strlen(s2)+1);
if (result == NULL){
perror("Function concat failed!");
exit(EXIT_FAILURE);
}
else{
strcpy(result, s1);
strcat(result, s2);
return result;
}
}
/*
* Name: PrintCommand
* Arguments:
* Description: Prints a Command structure as returned by parse on stdout.
*/
void PrintCommand(int n, Command *cmd){
printf("Parse returned %d:\n", n);
printf(" stdin : %s\n", cmd->rstdin ? cmd->rstdin : "<none>" );
printf(" stdout: %s\n", cmd->rstdout ? cmd->rstdout : "<none>" );
printf(" bg : %s\n", cmd->background ? "yes" : "no");
PrintPgm(cmd->pgm);
}
/*
* Name: PrintPgm
* Arguments: Pgm pointer
* Description: Prints a list of Pgm:s
*/
void PrintPgm (Pgm *p)
{
if (p == NULL) {
return;
}
else {
char **pl = p->pgmlist;
/*
* The list is in reversed order so print
* it reversed to get right
*/
PrintPgm(p->next);
printf(" [ ");
while (*pl) {
printf("%s ", *pl++);
}
printf("]\n");
}
}
/*
* Name: stripwhite
* Arguments: A string in which to remove whitespace
* Description: Strip whitespace from the start and end of STRING.
*/
void stripwhite(char *string){
register int i = 0;
/* Replaced whitespace with isspace, since whitespace is not supported. */
while(isspace(string[i])){
i++;
}
if(i){
strcpy (string, string + i);
}
i = strlen(string) - 1;
while(i> 0 && isspace (string[i])){
i--;
}
string[++i] = '\0';
}