-
Notifications
You must be signed in to change notification settings - Fork 11
/
shell.c
314 lines (272 loc) · 9.89 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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "shell.h"
#include <dirent.h>
char current_dir[1024] = "";
builtin_command builtins[NUM_BUILTINS] = {
{"cd", shell_cd, "Change the current directory. Usage: cd <directory>"},
{"help", shell_help, "Display this help message. Usage: help"},
{"pwd", shell_pwd, "Print the current working directory. Usage: pwd"},
{"echo", shell_echo, "Print arguments to the console. Usage: echo <text>"},
{"bye", shell_exit_command, "Exit the shell. Usage: bye"},
{"exit", shell_exit_command, "Exit the shell. Usage: exit"},
{"ls", shell_list, "List the contents in directory. Usage: ls <directory>"},
{"roll", shell_roll_wrapper, "Rolls the terminal. Usage: roll"}
};
int main(int argc, char** argv)
{
printf("\nHello, this is Ameya's basic shell (apksh).\n\n");
shell_loop();
return 0;
}
void shell_loop(void)
{
char *line = NULL;
char **args = NULL;
int status = 1;
do
{
getcwd(current_dir, sizeof(current_dir)); // Update current directory
printf("%s :) ", current_dir);
line = shell_read_line();
args = shell_line_parse(line);
status = shell_execute(args);
free(line);
free(args);
} while (status);
};
char *shell_read_line(void)
{
int buffer_size = 1024;
int i = 0;
char *buffer = malloc(sizeof(char) * buffer_size);
if (!buffer)
{
shell_handle_error("allocation error");
}
while (1)
{
int c = getchar();
if (c == EOF || c == '\n')
{
buffer[i] = '\0';
return buffer;
}
else
{
buffer[i] = c;
}
i++;
if (i >= buffer_size)
{
buffer_size += 1024;
buffer = realloc(buffer, buffer_size);
if (!buffer)
{
shell_handle_error("allocation error");
}
}
}
}
char **shell_line_parse(char* line)
{
int buffer_size = 64;
int i = 0;
char **tokens = malloc(buffer_size * sizeof(char *));
char *token;
if (!tokens)
{
shell_handle_error("allocation error");
}
token = strtok(line, " ");
while (token != NULL)
{
tokens[i] = strdup(token);
if (!tokens[i])
{
shell_handle_error("allocation error");
}
i++;
if (i >= buffer_size)
{
buffer_size += 64;
tokens = realloc(tokens, buffer_size * sizeof(char *));
if (!tokens)
{
shell_handle_error("allocation error");
}
}
token = strtok(NULL, " ");
}
tokens[i] = NULL;
return tokens;
}
void shell_handle_error(const char *message)
{
fprintf(stderr, "apksh: %s\n", message);
exit(EXIT_FAILURE);
}
int shell_cd(char** args)
{
if (args[1] == NULL)
{
fprintf(stderr, "apksh: expected argument to \"cd\"\n");
}
else
{
if (chdir(args[1]) != 0)
{
perror("apksh");
}
}
return 1;
}
int shell_help(char** args)
{
printf("Ameya's Shell (apksh)\n");
printf("The following are built-in:\n");
for (int i = 0; i < NUM_BUILTINS; i++)
{
printf(" %s - %s\n", builtins[i].name, builtins[i].description);
}
return 1;
}
int shell_list(char** args)
{
char* directory = args[1] ? args[1] : ".";
DIR *dir = opendir(directory);
if (dir == NULL)
{
perror("apksh: ");
return 1;
}
struct dirent *entry;
while ((entry = readdir(dir)) != NULL)
{
printf("%s\n", entry->d_name);
}
closedir(dir);
return 1;
}
int shell_make_dir(char** args)
{
if (args[1] == NULL)
{
fprintf(stderr, "apksh: No arguement given. Usage: mkdir <directory>");
return 1;
}
if (mkdir(args[1], 0755) != 0)
{
perror("apksh");
}
return 1;
}
int shell_pwd(char** args)
{
char cwd[1024];
if (getcwd(cwd, sizeof(cwd)) != NULL)
{
printf("%s\n", cwd);
}
else
{
perror("apksh");
}
return 1;
}
int shell_echo(char** args)
{
for (int i = 1; args[i] != NULL; i++)
{
printf("%s ", args[i]);
}
printf("\n");
return 1;
}
int shell_exit_command(char** args)
{
return 0;
}
int shell_man(char** args)
{
if (args[1] == NULL)
{
printf("Usage: man <command>\n");
printf("Available commands:\n");
for (int i = 0; i < NUM_BUILTINS; i++)
{
printf(" %s\n", builtins[i].name);
}
return 1;
}
for (int i = 0; i < NUM_BUILTINS; i++)
{
if (strcmp(args[1], builtins[i].name) == 0)
{
printf("%s - %s\n", builtins[i].name, builtins[i].description);
return 1;
}
}
printf("apksh: no manual entry for %s\n", args[1]);
return 1;
}
int shell_execute(char** args)
{
if (args[0] == NULL)
{
return 1; // An empty command was entered.
}
if (strcmp(args[0], "man") == 0)
{
return shell_man(args);
}
for (int i = 0; i < NUM_BUILTINS; i++)
{
if (strcmp(args[0], builtins[i].name) == 0)
{
return builtins[i].func(args);
}
}
printf("apksh: '%s' is not a recognized built-in command.\n", args[0]);
return 1;
}
int shell_launch(char** args)
{
pid_t pid, wpid;
int status;
pid = fork();
if (pid == 0)
{
if (execvp(args[0], args) == -1) //to make it such that external commands work if they dont exist in the builtin commands of apksh
{
fprintf(stderr, "apksh: Such a command doesn't exist.\n");
}
exit(EXIT_FAILURE);
}
else if (pid < 0)
{
perror("apksh");
}
else
{
do
{
wpid = waitpid(pid, &status, WUNTRACED);
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
}
return 1;
}
int shell_roll(void)
{
printf("⡀⠛⠉⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣴⣿⣿⣿⣿⣿⣿⣿⣿⣶⣤⣄⣀⠀⠀⠀⠀⠀⠀⠀⠉⠁⠀⣀⣀⠀⣀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣄⡀⠀⠀⠀\n" "⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣾⣿⣿⣿⣿⣿⣛⣿⣿⣷⣿⣿⣯⠉⠉⠀⠀⠀⠀⠀⠀⠀⠀⠀⠸⣏⠉⣿⠉⢹⠟⢉⠉⢻⣿⠉⢻⠋⠙⡏⣿⠋⢻⡏⠉⣿⠉⣉⣻⠀\n" "⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣧⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣻⡀⠈⢀⣿⠀⢸⠀⠀⣿⠀⢸⠀⠰⣿⣿⠀⢸⠁⢀⡟⠀⢹⣿⠀\n" "⠇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⣿⣿⣿⣿⡿⠿⠿⢿⣿⣿⣿⣿⣿⡿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠘⣧⠀⣼⣿⠀⢸⡀⠀⣏⠀⢸⠀⠀⣿⣿⡄⠘⠀⢸⡇⠀⢰⣾⠀\n" "⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⣿⣿⡿⠋⠀⠀⠀⠀⠀⠈⠉⠉⠁⠀⠀⠈⢻⡆⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⣀⣿⣿⣆⡈⢁⣰⣿⣄⠘⢀⣼⣿⣿⣇⣀⣀⣼⣧⣀⣈⣹⡇\n" "⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⣿⣿⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⣿⡄⠀⠀⠀⠀⠀⠀⠀⠀⠈⠉⣿⣿⣿⣿⣿⣿⠟⠿⢿⣿⠿⠛⠛⠻⠿⠿⠻⠛⠉⠉⠉⠀\n" "⠇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⣿⣿⠀⠀⢀⣀⣀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠹⣧⣄⠀⠀⠀⠀⠀⠀⣴⠶⡶⠿⣿⣿⠿⠿⢿⡿⠿⠿⣿⠿⢿⡿⢿⡿⠀⠀⠀⠀⠀⠀⠀\n" "⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠰⣿⣿⠀⠀⢨⣭⣽⣿⡇⠀⢠⣾⣿⣿⣷⣆⠀⠀⢸⣿⠀⠀⠀⠀⠀⠀⣿⠀⢱⡆⠈⣿⠀⢴⣾⡇⠀⣶⣿⠀⠘⡇⠀⡇⠀⠀⠀⠀⠀⠀⠀\n" "⠇⠀⠀⠀⠀⠀⠀⠀⠀⠀⢰⣿⠉⠛⠀⠀⠀⠀⠉⠁⠀⠀⠘⡏⠉⠉⠛⠋⠀⣠⣼⣿⠀⠀⠀⠀⠀⠀⣿⠀⢨⡁⠺⣿⠀⣈⣹⡇⠀⣉⣿⠀⡀⠁⠀⡇⠀⠀⠀⠀⠀⠀⠀\n" "⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⡀⠀⠀⠀⠀⠀⣀⠀⠀⠀⠀⢹⡄⠀⠀⠀⠀⣿⣿⡿⠀⠀⠀⠀⠀⠀⣿⠀⠸⠇⠀⣿⠀⠹⢿⡇⠀⠿⢿⠀⢸⡀⠀⡇⠀⠀⠀⠀⠀⠀⠀\n" "⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠸⢷⣄⡀⠀⢠⡾⠋⠀⠛⢶⣶⣾⡇⠀⣠⠄⢰⣿⠟⠀⠀⠀⠀⠀⠀⠀⠻⢶⣶⡶⠚⠓⠶⠶⠾⠷⠶⠶⠾⠶⠾⠳⠾⠟⠀⠀⠀⠀⠀⠀⠀\n" "⡆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⣹⡷⣠⠏⠙⢷⣶⠲⠶⠶⣷⣶⡿⠋⢀⣾⠃⠀⠀⠀⠀⠀⠀⠀⠀⢀⣀⣹⣧⡀⢀⠀⠀⣀⣀⣀⡀⢀⣀⣀⣀⣀⣀⠀⠀⠀⠀⠀⠀⠀\n" "⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣴⠟⣫⣽⠃⠀⠀⠀⠉⠉⠙⠛⠋⠀⠀⢀⣾⡃⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⠉⢉⡉⠻⡏⠉⣿⠟⢉⡉⠙⣿⠉⢹⡏⢉⡿⠀⠀⠀⠀⠀⠀⠀\n" "⠀⠀⠀⠀⠀⠀⠀⠀⢀⡴⠛⠁⠀⣼⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣼⡏⢳⡄⠀⠀⠀⠀⠀⠀⠀⠀⣿⠀⠸⠇⣰⡇⠀⣿⠀⢸⣧⣀⣿⠀⠈⠀⣼⠁⠀⠀⠀⠀⠀⠀⠀\n" "⠀⠀⠀⠀⠀⠀⢀⣴⠏⠀⠀⠀⢸⣿⣿⡀⠀⠀⠰⣦⣄⡀⣀⣤⡾⣿⣿⣧⠀⠻⢦⣄⡀⠀⠀⠀⠀⠀⣿⠀⢸⠀⠈⡇⠀⣿⠀⢸⡟⠛⣿⠀⢠⠀⢹⣆⠀⠀⠀⠀⠀⠀⠀\n" "⠀⠀⠀⠀⠀⠀⠘⠁⠀⠀⠀⠀⣾⣿⣿⣷⣄⡀⠀⠙⠿⣿⣏⣽⣿⣿⣿⣿⠄⢸⣧⠈⠙⠶⣤⣀⠀⠀⣿⣀⣸⣄⣠⣷⣀⣿⣦⣀⣁⣠⣿⣀⣸⣧⣀⣿⠀⠀⠀⠀⠀⠀⠀\n" "⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣿⣿⣿⣿⣿⣿⣶⣶⣶⣿⣿⣿⣿⣿⣿⣿⠀⠀⠹⣆⠀⠀⠀⠉⠳⣦⡀⠉⠉⠙⠻⣿⠉⠁⠀⠉⠉⠀⠀⠈⠉⠀⠉⠹⠇⠀⠀⠀⠀⠀⠀\n" "⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠛⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠀⠀⠀⢿⡆⠀⠀⠀⠀⠻⣿⠓⠒⠲⢦⣹⠷⠒⠲⣶⡖⠒⣶⣶⠒⣶⠒⠶⠒⠛⠀⠀⠀⠀⠀\n""⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠘⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠇⠀⠀⠀⢸⣧⠀⠀⠀⠀⠀⢻⡀⠀⠀⠈⠙⠀⠀⠀⠻⠇⠀⠻⠿⠐⠻⠀⠀⠀⠀⠀⠀⠀⠀⠀\n");
return 1;
}
int shell_roll_wrapper(char** args)
{
return shell_roll();
}