-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell.c
126 lines (110 loc) · 2.7 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
#include <stdbool.h>
#include "keyboard.h"
#include "shell.h"
#include "tty.h"
#include "string.h"
#include "rtc.h"
void shell_init(void)
{
char input_buffer[SHELL_BUFFER_SIZE + 1];
size_t i;
char c;
while (true)
{
terminal_printf("&7dead@&croot&7: /> ");
i = 0;
c = 0;
while (i < SHELL_BUFFER_SIZE)
{
c = keyboard_getchar();
if (c == '\b')
{
if (i > 0)
{
terminal_printf("\b \b");
--i;
}
}
else
{
terminal_putchar(c);
input_buffer[i] = c;
++i;
if (c == '\n')
break;
}
}
input_buffer[i] = '\0';
keyboard_clear_buffer();
shell_parse_input(input_buffer);
}
}
void shell_help_command(void)
{
terminal_printf("List of commands:\n");
terminal_printf("help - Print out the list of commands.\n");
terminal_printf("exit - Exit the shell.\n");
terminal_printf("echo - Print out the message. &eWITH colors!&7\n");
terminal_printf("clear - Clear the terminal.\n");
terminal_printf("time - Print out the current time. (UTC)\n");
}
void shell_exit_command(void)
{
terminal_printf("Exiting shell...\n");
terminal_printf("Goodbye!\n");
while (1)
;
}
void shell_time_command(void)
{
struct Rtc_time current_time = get_rtc_time();
terminal_printf("Current time: %02d:%02d:%02d %02d.%02d.%d\n",
current_time.hour, current_time.min, current_time.sec,
current_time.mday, current_time.mon, current_time.year);
}
void shell_echo_command(char *input)
{
char *message = input;
while (*message == ' ')
{
message++;
}
terminal_printf(message);
terminal_printf("\n");
}
void shell_parse_input(char *input)
{
char *command = strtok(input, " \n");
char *args = input + strlen(command) + 1;
if (command == NULL || *command == '\0')
{
return;
}
if (strcmp(command, "help") == 0)
{
shell_help_command();
}
else if (strcmp(command, "echo") == 0)
{
shell_echo_command(args);
}
else if (strcmp(command, "exit") == 0)
{
shell_exit_command();
}
else if (strcmp(command, "clear") == 0)
{
terminal_clear();
}
else if (strcmp(command, "time") == 0)
{
shell_time_command();
}
else
{
terminal_printf("Unknown command: '");
terminal_print(command);
terminal_printf("'\n");
terminal_printf("Type 'help' to print out the list of commands.\n");
}
}