-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell.c
270 lines (247 loc) · 7.51 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
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <sys/stat.h>
#include <fcntl.h>
#define BUFFER_SIZE 100
#define MAX_NO_OF_ARGUMENTS 10
#define MAX_LEN_OF_AN_ARGUMENT 20
//General Purpose Buffer
char buf[BUFFER_SIZE];
//A struct to store background process information
typedef struct bg_process{
int pid;
char* name;
}bg_process;
//Function Definitions
void showPrompt();
char* getCmd();
void storeHistory();
void printHistory();
char** parseCmd(char* cmd,int* nargs);
int execCmd(char** args,int l);
int runOtherCmd(char** args,int nargs);
void updateBgProcessList();
void printBgProcess();
int addBgProcess(int pid, char* name);
int removeBgProcess(int pid);
//List and counters to maintain the list of background processes
bg_process** bglist=NULL;
int bgcounter=0;
int nbgprocess=0;
int main(){
using_history();
stifle_history(10);
while(1){
showPrompt();
char* cmd=getCmd();
if(strlen(cmd)==0){printf("\n");continue;}
int nargs;
char** args=parseCmd(cmd,&nargs);
int x=execCmd(args,nargs);
if(x!=2)storeHistory(cmd); //History commands should not be stored
//freeMemory(cmd,args,nargs);
//fflush(stdin);
}
exit(EXIT_SUCCESS);
}
void showPrompt(){
getcwd(buf,BUFFER_SIZE);
printf("$~%s $$",buf);
}
char* getCmd(){
char* x=readline(""); //Allocated with Malloc, so no need to take care of size or \0
/*scanf("%[^\n]", x);
int len=strlen(x);
printf("len: %d\n",len);
x=(char*)realloc(x,sizeof(char)*(len-1));*/
return x;
}
void storeHistory(char* cmd){
if (cmd && *cmd) add_history (cmd);
}
void printHistory(){
printf("\n\nThe previous commands are (a maximum of 10 are shown):\n");
struct _hist_entry** list=history_list();
int count=0;
while(list[count]!=NULL){
printf("$ %d: %s\n",count+1,list[count]->line);
count++;
}
}
char** parseCmd(char* cmd, int* nargs){
int len=strlen(cmd);
int ptr=0;
int wptr=0;
int c=0;
char** args=(char**)malloc(MAX_NO_OF_ARGUMENTS*sizeof(char*));
while(ptr<len){
char* word=(char*)malloc(MAX_NO_OF_ARGUMENTS*sizeof(char));
while(cmd[ptr]!=' ' && ptr<len)word[wptr++]=cmd[ptr++];
word[wptr]='\0';
args[c++]=word;
wptr=0;
ptr++;
}
*nargs=c;
return args;
}
int execCmd(char** args, int nargs){
if(!strcmp(args[0],"cd")){ //if cd is entered
if(chdir(args[1]))printf("cd error: Directory not found\n");
}else if(!strcmp(args[0],"history")){ //if history is entered
printHistory();
}else if(!strcmp(args[0],"exit")){ //if exit is entered
updateBgProcessList();
if(nbgprocess==0){
exit(EXIT_SUCCESS);
}
printf("The following background processes are running: \n");
printBgProcess();
printf("Please kill these processes before exiting\n");
return 1;
}else if(!strcmp(args[0],"kill")){ //if kill is entered
int pid=0;
int i=0;
while(args[1][i]!='\0')pid=pid*10+(args[1][i++]-'0');
//printf("\n%d\n",pid);
if(!(kill(pid,SIGTERM)==0)){
printf("\nkill error: Either pid is invalid or you do not have permission to kill the process\n");
return 0;
}else{
updateBgProcessList();
printf("\nKilling Successful\n");
}
}else if(!strcmp(args[0],"job")){ //if job is entered
updateBgProcessList();
printBgProcess();
}else if(args[0][0]=='!'){ //if ! is entered
struct _hist_entry** list=history_list();
char* cmd=NULL;
if(args[0][1]!='-'){
char c=args[0][1]-'0'-1;
if(c>9 || c<0){printf("'!' error: Invalid Number given"); return 1;}
cmd=list[c]->line;
}else if(args[0][1]=='-'){
HISTORY_STATE* state=history_get_history_state();
int l=state->length;
char c=args[0][2]-'0'-1;
if(c>9 || c<0){printf("'!' error: Invalid Number given"); return 1;}
cmd=list[l-c-1]->line;
}else{
printf("'!' error: Invalid Number given");
return 1;
}
int l;
printf("Executing: %s\n",cmd);
char** args=parseCmd(cmd,&l);
int t=execCmd(args,nargs);
storeHistory(cmd);
return 2;
}else{ //if another cmd is entered
runOtherCmd(args,nargs);
}
}
void updateBgProcessList(){
if(nbgprocess==0)return;
for(int i=0;i<bgcounter;i++){
if(bglist[i]!=NULL){
int status;
int t=waitpid(bglist[i]->pid, &status,WNOHANG);
if(t==-1){
printf("Error occured in updating background process list");
}else if(t){
removeBgProcess(bglist[i]->pid);
}
}
}
}
int addBgProcess(int pid, char* name){
nbgprocess++;
bg_process* newproc=(bg_process*)malloc(sizeof(bg_process));
newproc->pid=pid;
newproc->name=name;
int i=0;
while(bgcounter!=0 && i<bgcounter){
if(bglist[i]==NULL)break;
i++;
}
if(i<bgcounter){
bglist[i]=newproc;
return 1;
}
bgcounter++;
bglist=(bg_process**)realloc(bglist,sizeof(bg_process*)*bgcounter);
bglist[bgcounter-1]=newproc;
return 1;
}
int removeBgProcess(int pid){
for(int i=0;i<bgcounter;i++){
if(bglist[i]->pid==pid){
bg_process* temp=bglist[i];
bglist[i]=NULL;
free(temp);
nbgprocess--;
return 1;
}
}
return 0;
}
void printBgProcess(){
if(nbgprocess==0){
printf("No Background Processes running\n");
return;
}
printf("PID NAME\n");
for(int i=0;i<bgcounter;i++){
if(bglist[i]!=NULL){
printf("%d %s\n",bglist[i]->pid,bglist[i]->name);
}
}
}
int runOtherCmd(char** args, int nargs){
int status;
char* path=args[0];
int pid=fork();
if(pid>0){
if(strcmp(args[nargs-1],"&")) {
int p=wait(&status);
}else{
addBgProcess(pid,path);
printBgProcess();
return 1;
}
}else if(pid==0){
int fdI=0;
for(int i=0;i<nargs;i++){
if(!strcmp(args[i],"<")){
fdI=open(args[++i],O_RDONLY);
if(fdI==-1){
printf("Unable to open file\n");
return 0;
}
dup2(fdI,STDIN_FILENO);
close(fdI);
}else if(!strcmp(args[i],">")){
int fdO=creat(args[++i],0644);
if(fdO==-1){
printf("Unable to Create output file, perhaps it already exists\n");
return 0;
}
dup2(fdO,STDOUT_FILENO);
close(fdO);
}
}
int e=execvp(path,args);
if(e==-1){printf("%s Command not found! Please check...\n",path);return 0;}
}else{
printf("Process Creation failed :(\n");
return 0;
}
}