-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell.c
340 lines (264 loc) · 6.93 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
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <stdbool.h>
#include <fcntl.h>
#define INPUT_STRING_SIZE 80
#define MAX_DIR_STRING_SIZE 255
#define PATH_SPLITTER ":"
#include "io.h"
#include "parse.h"
#include "process.h"
#include "shell.h"
char *cdir=NULL;
int cmd_help(tok_t arg[]);
int cmd_cd(tok_t arg[]);
process * create_process(tok_t* arg);
void add_process(process* p);
void desc_process(process* p);
int cmd_wait(tok_t arg[]);
int cmd_fg(tok_t arg[]);
int cmd_bg(tok_t arg[]);
void terminal_cmd(tok_t arg[]){
int status;
process *p=create_process(arg);
add_process(p);
latest_process=p;
pid_t id=fork();
if(id==0){
latest_process->pid=getpid();
launch_process(latest_process);
}else if(id>0){
latest_process->pid=id;
//desc_process(latest_process);
if(latest_process->background){
fprintf(stdout,"ID: %d running in background\n",latest_process->pid);
waitpid(id,&(latest_process->status),WNOHANG | WUNTRACED);
}else{
waitpid(id,&(latest_process->status),WUNTRACED);
tcsetattr(shell_terminal,TCSADRAIN,&shell_tmodes);
tcsetpgrp(shell_terminal,shell_pgid);
}
if(WEXITSTATUS(latest_process->status)==EXIT_FAILURE){
if(latest_process->prev){
latest_process=p->prev;
free(p);
}else{
first_process=latest_process=NULL;
free(p);
}
}
}else if(id<0){
fprintf(stderr,"failed to fork\n");
}
}
void set_dir(){
free(cdir);
char *temp=malloc(MAX_DIR_STRING_SIZE);
if((temp=getcwd(temp,MAX_DIR_STRING_SIZE))==NULL ){
int i=2;
while(errno==ERANGE && temp==NULL){
temp=realloc(temp,MAX_DIR_STRING_SIZE*i);
temp=getcwd(temp,MAX_DIR_STRING_SIZE*i);
i++;
//printf("err: %d , i: %d\n, %s\n",errno,i,temp);
}
}
int len=strlen(temp)+1;
cdir=malloc(len);
strncpy(cdir,temp,len);
free(temp);
}
int cmd_quit(tok_t arg[]) {
printf("Bye\n");
exit(0);
return 1;
}
/* Command Lookup table */
typedef int cmd_fun_t (tok_t args[]); /* cmd functions take token array and return int */
typedef struct fun_desc {
cmd_fun_t *fun;
char *cmd;
char *doc;
} fun_desc_t;
fun_desc_t cmd_table[] = {
{cmd_help, "?", "show this help menu"},
{cmd_quit, "quit", "quit the command shell"},
{cmd_cd,"cd","change current working directory"},
{cmd_wait,"wait","Wait for all background jobs to terminate before returning to prompt"},
{cmd_fg,"fg","Move process with specific id or most recent process into foreground"},
{cmd_bg,"bg","Move process with specific id or most recent process into background"}
};
int cmd_help(tok_t arg[]) {
int i;
for (i=0; i < (sizeof(cmd_table)/sizeof(fun_desc_t)); i++) {
printf("%s - %s\n",cmd_table[i].cmd, cmd_table[i].doc);
}
return 1;
}
int cmd_cd(tok_t arg[]){
char temp[MAX_DIR_STRING_SIZE];
if(arg[0]){
strcpy(temp,arg[0]);
char *spacer="\ ";
int i;
for(i=1;arg[i];i++){
strcat(temp,spacer);
strcat(temp,arg[i]);
}
}
//printf("TEST: %s\n",temp);
if(chdir(temp)==0){
set_dir();
return 1;
}else{
fprintf(stdout,"no such file or directory :\\\n");
return 0;
}
}
int cmd_wait(tok_t arg[]){
process *p;
for(p=first_process ; p ; p=p->next){
waitpid(p->pid,&(p->status),WUNTRACED);
}
return 1;
}
int cmd_fg(tok_t arg[]){
if(arg[0]){
pid_t id=atoi(arg[0]);
process *p;
for(p=first_process; p ; p=p->next){
if(p->pid==id){
put_process_in_foreground(p,1);
break;
}
}
}
return 1;
}
int cmd_bg(tok_t arg[]){
system("cowsay I dont really know what to do in this function. moo.");
return 1;
}
int lookup(char cmd[]) {
int i;
for (i=0; i < (sizeof(cmd_table)/sizeof(fun_desc_t)); i++) {
if (cmd && (strcmp(cmd_table[i].cmd, cmd) == 0)) return i;
}
return -1;
}
void init_shell(){
/* Check if we are running interactively */
shell_terminal = STDIN_FILENO;
/** Note that we cannot take control of the terminal if the shell
is not interactive */
shell_is_interactive = isatty(shell_terminal);
if(shell_is_interactive){
/* force into foreground */
while(tcgetpgrp (shell_terminal) != (shell_pgid = getpgrp())){
kill( - shell_pgid, SIGTTIN);
}
signal (SIGINT, SIG_IGN);//interrupt : C-c
signal (SIGQUIT, SIG_IGN);/*C-\, same as SIGINT, makes core dump, (like user detects error) */
signal (SIGTSTP, SIG_IGN);//C-z
signal (SIGTTIN, SIG_IGN);//sent if tries to read from terminal when in background
signal (SIGTTOU, SIG_IGN);//same as previous but writing to terminal
shell_pgid = getpid();
/* Put shell in its own process group */
if(setpgid(shell_pgid, shell_pgid) < 0){
perror("Couldn't put the shell in its own process group");
exit(1);
}
/* Take control of the terminal */
tcsetpgrp(shell_terminal, shell_pgid);
tcgetattr(shell_terminal, &shell_tmodes);
}
/** YOUR CODE HERE */
}
/**
* Add a process to our process list
*/
void add_process(process* p){
if(!first_process){
first_process=p;
first_process->prev=NULL;
}else{
latest_process->next=p;
p->prev=latest_process;
}
}
/**
* Creates a process given the inputString from stdin
*/
process* create_process(tok_t * arg){
process * P=malloc(sizeof(process));
P->argv=arg;
P->tmodes=shell_tmodes;
P->stdin=STDIN_FILENO;
P->stdout=STDOUT_FILENO;
P->stderr=STDERR_FILENO;
P->completed=0;
P->stopped=0;
P->background=0;
redirectOut(P);
redirectIn(P);
int tokPos;
if((tokPos=isDirectTok(arg,"&"))){
if(strlen(arg[tokPos])==1){
P->background=1;
int moveUp=tokPos;
arg[moveUp]=NULL;
while(arg[moveUp+1]){
arg[moveUp]=arg[moveUp+1];
arg[moveUp+1]=NULL;
moveUp++;
}
}
}
int arg_count=0;
while(P->argv[arg_count]){
arg_count++;
}
P->argc=arg_count;
return P;
}
void desc_process(process *p){
fprintf(stdout,"pid: %d,proc ptr: %p,prev: %p\n",p->pid,p,p->prev);
}
int shell (int argc, char *argv[]) {
char *s=NULL; //= malloc(INPUT_STRING_SIZE+1); /* user input string */
tok_t *t; /* tokens parsed from input */
int lineNum = 0;
int fundex = -1;
pid_t pid = getpid(); /* get current processes PID */
pid_t ppid = getppid(); /* get parents PID */
pid_t cpid, tcpid, cpgid;
init_shell();
printf("%s running as PID %d under %d\n",argv[0],pid,ppid);
lineNum=0;
set_dir();
fprintf(stdout, "%d:%s $ ", lineNum,cdir);
while ((s = freadln(stdin))){
t = getToks(s); /* break the line into tokens */
//printf("args s: %s\n",s);
fundex = lookup(t[0]); /* Is first token a shell literal */
if(fundex >= 0) cmd_table[fundex].fun(&t[1]);
else {
if(t[0]){
terminal_cmd(t);
}
//fprintf(stdout, "This shell only supports built-ins. Replace this to run programs as commands.\n");
}
free(s);
freeToks(t);
fprintf(stdout, "%d:%s $ ", lineNum,cdir);
}
return 0;
}