-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
225 lines (186 loc) · 5.3 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdbool.h>
#define BUFFER_SIZE 64
void main_loop();
char* read_line();
char** parse_line(char*);
int execute_command(char**);
void launch_shell(char** , bool);
int number_of_builtins();
//builtin shell commands
int cd (char**);
int Exit (char**);
char* builtin_str [] = { // Have to be builtins to manipulate the current (parent) process.
"cd",
"exit"
};
int(*builtin_function[]) (char**) = { // An array of function pointers that take an array of strings,
&cd, // and return an int
&Exit
};
int main() {
main_loop(); //Read , Parse , Execute.
return 0;
}
void main_loop (){
char* line;
char** arguments;
int status = 1;
while (status){
printf("shell> ");
//Read
line = read_line();
//Parse
arguments = parse_line(line);
//Execute
status = execute_command(arguments);
}
}
char* read_line(){
int buffer_size = BUFFER_SIZE;
int position = 0;
char* buffer = malloc(sizeof(char) * buffer_size);
int c;
if (!buffer) {
fprintf(stderr, "Buffer allocation error\n");
exit(EXIT_FAILURE);
}
while (1) {
// Read a character
c = getchar();
// If we hit EOF, replace it with a null character and return.
if (c == EOF || c == '\n') {
buffer[position] = '\0';
return buffer;
} else {
buffer[position] = c;
}
position++;
//If command line exceeded 512 characters print error message.
if(position >= 512){
fprintf(stderr , "A very long command line (over 512 characters\n");
buffer[0] = '\0';
while ( getchar() != '\n' ); // Get to the end of the stream to skip the long command.
return buffer;
}
// If we have exceeded the buffer, reallocate.
if (position >= buffer_size) {
buffer_size += BUFFER_SIZE;
buffer = realloc(buffer, buffer_size);
if (!buffer) {
fprintf(stderr, "Buffer allocation error\n");
exit(EXIT_FAILURE);
}
}
}
}
char** parse_line(char* line){
int buffer_size = BUFFER_SIZE;
int position = 0;
char** tokens = malloc(sizeof(char*) * buffer_size);
char* token;
if(!tokens){
fprintf(stderr , "Tokens allocation error\n");
exit(EXIT_FAILURE);
}
//TODO edit the delimiters.
token = strtok(line , " ");
while(token != NULL){
tokens[position] = token;
position++;
if(position >= buffer_size){
buffer_size += BUFFER_SIZE;
tokens = realloc(tokens, sizeof(char*) * buffer_size);
if(!tokens){
fprintf(stderr , "Tokens allocation error\n");
exit(EXIT_FAILURE);
}
}
token = strtok(NULL , " ");
}
if(strcmp(tokens[position-1] , "&") ==0) {
tokens[position] = NULL;
}
else if( tokens[position-1][strlen(tokens[position-1])-1] == '&' && strlen(tokens[position-1]) > strlen(strtok(tokens[position-1] , "&"))){
tokens[position-1] = strtok(tokens[position-1] , "&");
tokens[position] = "&";
position++;
if(position >= buffer_size){
buffer_size += BUFFER_SIZE;
tokens = realloc(tokens, sizeof(char*) * buffer_size);
if(!tokens){
fprintf(stderr , "Tokens allocation error\n");
exit(EXIT_FAILURE);
}
}
tokens[position] = NULL;
}
return tokens;
}
int execute_command(char** arguments){
bool wait = true;
if(arguments[0] == NULL){ // Empty command line.
return 1;
}
for( int i=0 ; i<number_of_builtins() ; i++){
if(strcmp(arguments[0] , builtin_str[i]) == 0){
return (*builtin_function[i])(arguments);
}
}
int i =0;
while(arguments[i]){
if(strcmp(arguments[i] , "&") == 0){
arguments[i] = NULL;
wait = false;
//printf("wait = false\n");
break;
}
i++;
}
launch_shell(arguments , wait);
return 1; // To keep the main loop from breaking.
}
void launch_shell(char** arguments , bool wait){
pid_t pid ;
int status;
pid = fork();
if(pid == 0){ // Child process
if( execvp( arguments[0] , arguments) == -1){
perror("execvp FAILED, A command doesn't exist or can't be executed");
}
exit(EXIT_FAILURE);
}
else if( pid < 0){
perror("Fork FAILED");
}
else{ // Parent process
if(wait){
//printf("you are waiting \n");
do{
waitpid( pid , &status , WUNTRACED);
}
while( !WIFEXITED(status) && !WIFSIGNALED(status));
}
}
}
int cd (char** arguments){
if(arguments [1] == NULL){
fprintf(stderr , "Expected argument to \"cd\"\n");
}
else{
if(chdir(arguments[1]) != 0){
perror("chdir FAILED");
}
}
return 1;
}
int Exit(char** arguments){
return 0;
}
int number_of_builtins(){
return sizeof(builtin_str) / sizeof(char*);
}