-
Notifications
You must be signed in to change notification settings - Fork 9
/
myShell.c
257 lines (239 loc) · 4.79 KB
/
myShell.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h> // For strtok() and strcmp()
#include <unistd.h> // For fork(), pid_t
#include <sys/wait.h> // For waitpid() and associated macros
char SHELL_NAME[50] = "myShell";
int QUIT = 0;
// Function to read a line from command into the buffer
char *readLine()
{
char *line = (char *)malloc(sizeof(char) * 1024); // Dynamically Allocate Buffer
char c;
int pos = 0, bufsize = 1024;
if (!line) // Buffer Allocation Failed
{
printf("\nBuffer Allocation Error.");
exit(EXIT_FAILURE);
}
while(1)
{
c=getchar();
if (c == EOF || c == '\n') // If End of File or New line, replace with Null character
{
line[pos] = '\0';
return line;
}
else
{
line[pos] = c;
}
pos ++;
// If we have exceeded the buffer
if (pos >= bufsize)
{
bufsize += 1024;
line = realloc(line, sizeof(char) * bufsize);
if (!line) // Buffer Allocation Failed
{
printf("\nBuffer Allocation Error.");
exit(EXIT_FAILURE);
}
}
}
}
// Function to split a line into constituent commands
char **splitLine(char *line)
{
char **tokens = (char **)malloc(sizeof(char *) * 64);
char *token;
char delim[10] = " \t\n\r\a";
int pos = 0, bufsize = 64;
if (!tokens)
{
printf("\nBuffer Allocation Error.");
exit(EXIT_FAILURE);
}
token = strtok(line, delim);
while (token != NULL)
{
tokens[pos] = token;
pos ++;
if (pos >= bufsize)
{
bufsize += 64;
line = realloc(line, bufsize * sizeof(char *));
if (!line) // Buffer Allocation Failed
{
printf("\nBuffer Allocation Error.");
exit(EXIT_FAILURE);
}
}
token = strtok(NULL, delim);
}
tokens[pos] = NULL;
return tokens;
}
// Section Dealing with Built-in Commands
// Function Declarations
int myShell_cd(char **args);
int myShell_exit();
// Definitions
char *builtin_cmd[] = {"cd", "exit"};
int (*builtin_func[]) (char **) = {&myShell_cd, &myShell_exit}; // Array of function pointers for call from execShell
int numBuiltin() // Function to return number of builtin commands
{
return sizeof(builtin_cmd)/sizeof(char *);
}
// Builtin command definitions
int myShell_cd(char **args)
{
if (args[1] == NULL)
{
printf("myShell: expected argument to \"cd\"\n");
}
else
{
if (chdir(args[1]) != 0)
{
perror("myShell: ");
}
}
return 1;
}
int myShell_exit()
{
QUIT = 1;
return 0;
}
// Function to create child process and run command
int myShellLaunch(char **args)
{
pid_t pid, wpid;
int status;
pid = fork();
if (pid == 0)
{
// The Child Process
if (execvp(args[0], args) == -1)
{
perror("myShell: ");
}
exit(EXIT_FAILURE);
}
else if (pid < 0)
{
//Forking Error
perror("myShell: ");
}
else
{
// The Parent Process
do
{
wpid = waitpid(pid, &status, WUNTRACED);
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
}
return 1;
}
// Function to execute command from terminal
int execShell(char **args)
{
int ret;
if (args[0] == NULL)
{
// Empty command
return 1;
}
// Loop to check for builtin functions
for (int i=0; i< numBuiltin(); i++) // numBuiltin() returns the number of builtin functions
{
if(strcmp(args[0], builtin_cmd[i])==0) // Check if user function matches builtin function name
return (*builtin_func[i])(args); // Call respective builtin function with arguments
}
ret = myShellLaunch(args);
return ret;
}
// Read and Parse from Config File
int readConfig()
{
FILE *fptr;
char line[200];
char **args;
fptr = fopen("config", "r");
if (fptr == NULL)
{
printf("Unable to find config file.\n");
return 1;
}
else
{
while(fgets(line, sizeof(line), fptr) != NULL)
{
printf("\n%s", line);
args=splitLine(line);
if(strcmp(args[0], "export")==0)
strcpy(SHELL_NAME, args[1]);
}
}
free(args);
fclose(fptr);
return 1;
}
// When myShell is called Interactively
int myShellInteract()
{
char *line;
char **args;
while(QUIT == 0)
{
printf("%s> ", SHELL_NAME);
line=readLine();
args=splitLine(line);
execShell(args);
free(line);
free(args);
}
return 1;
}
// When myShell is called with a Script as Argument
int myShellScript(char filename[100])
{
printf("Received Script. Opening %s", filename);
FILE *fptr;
char line[200];
char **args;
fptr = fopen(filename, "r");
if (fptr == NULL)
{
printf("\nUnable to open file.");
return 1;
}
else
{
printf("\nFile Opened. Parsing. Parsed commands displayed first.");
while(fgets(line, sizeof(line), fptr)!= NULL)
{
printf("\n%s", line);
args=splitLine(line);
execShell(args);
}
}
free(args);
fclose(fptr);
return 1;
}
int main(int argc, char **argv)
{
// Read from myShell Configuration Files
readConfig();
// Parsing commands Interactive mode or Script Mode
if (argc == 1)
myShellInteract();
else if (argc == 2)
myShellScript(argv[1]);
else
printf("\nInvalid Number of Arguments");
// Exit the Shell
return EXIT_SUCCESS;
}