-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.c
More file actions
74 lines (63 loc) · 2.36 KB
/
shell.c
File metadata and controls
74 lines (63 loc) · 2.36 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "shell.h"
#include "command.h"
#include "tokenizer.h"
#include "builtins.h"
#include "utils.h"
#define HISTORY_SIZE 10
char* command_history[HISTORY_SIZE];
int history_count = 0;
int main() {
while (1) {
printf("ByteShell> ");
char input[100];
fgets(input, sizeof(input), stdin);
input[strcspn(input, "\n")] = '\0'; // Remove trailing newline
// Add command to history
if (history_count < HISTORY_SIZE) {
command_history[history_count] = strdup(input);
history_count++;
} else {
free(command_history[0]);
for (int i = 0; i < HISTORY_SIZE - 1; i++) {
command_history[i] = command_history[i + 1];
}
command_history[HISTORY_SIZE - 1] = strdup(input);
}
char** tokens = tokenize_input(input);
if (tokens[0] != NULL) {
execute_system_command(tokens[0], tokens);
// if (strcmp(tokens[0], "ls") == 0) {
// execute_system_command("ls", tokens);
// } else if (strcmp(tokens[0], "checkpid") == 0) {
// execute_system_command("checkpid", tokens);
// } else if (strcmp(tokens[0], "kill") == 0) {
// execute_system_command("kill", tokens);
// } else if (strcmp(tokens[0], "echo") == 0) {
// execute_system_command("echo", tokens);
// } else if (strcmp(tokens[0], "help") == 0) {
// execute_help_command();
// } else if (strcmp(tokens[0], "exit") == 0) {
// execute_exit_command();
// break;
// } else if (strcmp(tokens[0], "cd") == 0) {
// execute_cd_command(tokens);
// } else if (strcmp(tokens[0], "history") == 0) {
// execute_history_command();
// } else if (strcmp(tokens[0], "alias") == 0) {
// execute_alias_command(tokens);
// } else {
// // printf("Command not found: %s\n", tokens[0]);
// execute_system_command(tokens[0], tokens);
// }
}
free_tokens(tokens);
}
// Cleanup history
for (int i = 0; i < history_count; i++) {
free(command_history[i]);
}
return 0;
}