void cli()
{
char c = uart_getc();
uart_sendc(c);
if (c == '\r' || c == '\n')
{
// Null-terminate the buffer
cli_buffer[buffer_index] = '\0';
if (buffer_index > 0)
{
// Add command to history
if (history_count < MAX_HISTORY_SIZE)
{
strcpy(command_history[history_count], cli_buffer);
history_count++;
}
else
{
// Shift history to accommodate new command
for (int i = 0; i < MAX_HISTORY_SIZE - 1; i++)
{
strcpy(command_history[i], command_history[i + 1]);
}
strcpy(command_history[MAX_HISTORY_SIZE - 1], cli_buffer);
}
execute_command(cli_buffer);
}
// Reset buffer and history index
buffer_index = 0;
current_history_index = -1;
print_os_name();
}
else if (c == '\t')
{
// Tab key (auto-completion)
// Find a matching command
int match_index = -1;
for (int i = 0; i < num_commands; i++)
{
if (strncmp(cli_buffer, commands[i], buffer_index) == 0)
{
if (match_index == -1)
{
match_index = i;
}
else
{
// Ambiguous match, do nothing
match_index = -1;
break;
}
}
}
// Perform auto-completion if a single match found
if (match_index != -1)
{
char *matched_command = commands[match_index];
int matched_len = strlen(matched_command);
for (int i = buffer_index; i < matched_len; i++)
{
uart_sendc(matched_command[i]);
cli_buffer[buffer_index++] = matched_command[i];
}
}
}
else if (c == '_')
{ // '_' key (UP arrow)
if (current_history_index < history_count - 1)
{
current_history_index++;
strcpy(cli_buffer, command_history[current_history_index]);
buffer_index = strlen(cli_buffer);
uart_puts("\033[2K\r"); // Clear current line
print_os_name();
uart_puts(cli_buffer);
}
}
else if (c == '+')
{ // '+' key (DOWN arrow)
if (current_history_index >= 0)
{
current_history_index--;
if (current_history_index >= 0)
{
strcpy(cli_buffer, command_history[current_history_index]);
}
else
{
strcpy(cli_buffer, "");
}
buffer_index = strlen(cli_buffer);
uart_puts("\033[2K\r"); // Clear current line
print_os_name();
uart_puts(cli_buffer);
}
}
else if (c == '\b' || c == '\x7F')
{ // Backspace or delete key
if (buffer_index != 0)
{
uart_sendc(' '); // Clear current character
uart_sendc('\b'); // Move the censor back by 1
uart_sendc('\0');
buffer_index--;
cli_buffer[buffer_index] = '\0';
}
}
else if (buffer_index < MAX_CMD_SIZE - 1)
{
cli_buffer[buffer_index] = c;
buffer_index++;
}
}