-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShell Code
288 lines (250 loc) · 7.52 KB
/
Shell Code
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
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#define BUFFER_LENGTH 1024
#define TOKEN_LENGTH 255
#define TOKEN_DELIMETERS " \t\n\a\r"
#define MAX_QUEUE 10
int pid_queue[MAX_QUEUE];
int front = -1;
int rear = -1;
int skip_input = 0;
/*Function: read_string
*Parameters: void - It accpets no parameter as this method is used to read input string.
*Returns: String of characters enter by user.
*Description: It read the the command entered by msh shell user
*and copy it in variable name "command" and retrun it to its calling function.
*/
char *readString(void)
{
int buffer_size = BUFFER_LENGTH;
//parse and store
char *command = malloc(sizeof(char) * buffer_size);
memset(command,0,BUFFER_LENGTH );
if(!command)
{
exit(EXIT_FAILURE);
}
while(1)
{
//takes user input
if( fgets(command,BUFFER_LENGTH,stdin) == NULL ) return NULL;
size_t length = strlen(command);
// replace the end of the file with the null character
if(command[length-1] == '\n')
{
command[length-1] = '\0';
return command;
}
return command;
}
}
/*Function: split_command
*Parameter(s):command- A pointer to string of character contains user command
*Returns: Pointer to Array of String contains tokens of string of user command
*Description: Tokenizes the user command once " \t\n\r" delimeters are found
*and put different token into different index of string array.
*/
char **split_command(char *command)
{
int buffer_size = TOKEN_LENGTH;
char **tokens = malloc(buffer_size * sizeof(char*));
char *token;
token = strtok(command,TOKEN_DELIMETERS);
int i = 0;
// tokenize the user command to treat it like separate strings.
while(token!=NULL)
{
tokens[i]=token;
i++;
token = strtok(NULL,TOKEN_DELIMETERS);
}
tokens[i] = NULL;
for (i =0; i<sizeof(tokens); i++){
printf("%s",tokens[i]);
}
return tokens;
}
void insertPid (int pid_child)
{
//condition to check if queue is full.
if(rear >= MAX_QUEUE - 1)
{
front = front + 1;
}
if(front == -1)
front = 0;
rear = rear+1;
pid_queue[rear] = pid_child;
}
/*Function: showpid
*Parameters: void
*Returns: void
*Description: Display all the process id present in the pid_queue
*/
void showpid()
{//checking the error condition for showpid().
if (front == -1 || front == rear+1)
{
printf("No process spwaned by your shell\n");
return;
}
int i;
for(i=front;i<=rear;i++)
{
printf("%d\n",pid_queue[i]);
}
}
/*Function: read_msh
*Parameters: cmd - A pointer to string array. Contains token of the command
*entered by the user
*Returns: An integer value of 0 for success and -1 for error
*Description: It process the user input command and fork a child process and execute the command
*It aslo search for user program in different directory.
*/
int run_msh(char **cmd)
{
pid_t pid;
int status;
char cmd_str[20];
int *shw_pid[MAX_QUEUE];
int front = -1;
int rear = -1;
while(1){
if( cmd[0] == '\0' ) return 1;
// check for the user input to exit the program
if((strcmp(cmd[0],"exit")== 0) || (strcmp(cmd[0],"quit") == 0))
{
exit(1);
}
//Change Directory commamnd.
else if(!strcmp(cmd[0],"cd"))
{
if(cmd[1] != NULL){
chdir(cmd[1]);
}
return 1;
}
//when user enters showpid.
else if(!strcmp(cmd[0],"showpid"))
{
if(cmd[1] == NULL)
{
showpid();
return 1;
}
else
{
printf("Wrong Command: Try only showpid\n");
return 1;
}
}
else
{
pid = fork();
if(pid == 0)
{
char cmd_str[20];
memset(cmd_str,0,20);
strcpy(cmd_str,"./");
strcat(cmd_str,cmd[0]);
int i;
//for(i=0;i<strlen(cmd_str);i++)
//{
// if(cmd_str[i]=='\n'){
// cmd_str[i] ='\0';
//}
//}
//check inside every given directory and handle ls.
if(execl(cmd_str,cmd[0],cmd[1],cmd[2],cmd[3],cmd[4],cmd[5]) == -1)
{
memset(cmd_str,0,20);
strcpy(cmd_str,"/usr/local/bin/");
strcat(cmd_str,cmd[0]);
if(execl(cmd_str,cmd[0],cmd[1],cmd[2],cmd[3],cmd[4],cmd[5]) == -1)
{
memset(cmd_str,0,20);
strcpy(cmd_str,"/usr/bin/");
strcat(cmd_str,cmd[0]);
if(execl(cmd_str,cmd[0],cmd[1],cmd[2],cmd[3],cmd[4],cmd[5]) == -1)
{
memset(cmd_str,0,20);
strcpy(cmd_str,"/bin/");
strcat(cmd_str,cmd[0]);
if(execl(cmd_str,cmd[0],cmd[1],cmd[2],cmd[3],cmd[4],cmd[5])== -1)
{
printf("%s: Command not found\n",cmd[0]);
return 0;
}
}
}
}
}
else
{
insertPid(pid);
wait(&status);
return 0;
}
}
}
}
/*Function: initialize_shell
*Parameters: void
*Returns: An integer 0 on success and -1 on failure
*Description: It initialize all the methods available in shell and process the user command
* and return ineger value.
*/
int initialize_shell(void)
{
char *input_string;
char **command;
int status;
while(1)
{
printf("msh>");
input_string = readString(); // call readString method to read all string
if( input_string == NULL )
continue;
command = split_command(input_string); // call for split_command function which tokenize user command
status = run_msh(command); // call for run_msh function
free(input_string); // releasing memory
free(command); // releasing memory
}
return 0;
}
/*Function: handle_signal
*Parameters: int
*Returns: void
*Description: This will handle signal interupt
*/
static void handle_signal(int sig)
{
skip_input = 1;
}
/*Function: main
*Parameters: void
*Returns: int
*Description: this function is responsible to handle signal and to intialize all other
*function of this codw
*/
int main()
{
struct sigaction act; // Created structure
memset(&act,0,sizeof(act));
act.sa_handler = & handle_signal;
if(sigaction(SIGINT, &act, NULL)< 0)
{
perror("Installing handler failed");
return 1;
}
if(sigaction(SIGTSTP, &act, NULL)< 0)
{
perror("Installing handler failed");
return 1;
}
return initialize_shell();
}