123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- #include "cmd.h"
- #include <fcntl.h>
- #include <libKeyboard/keyboard.h>
- #include <printf.h>
- #include <stddef.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/stat.h>
- #include <unistd.h>
- #define pause_cpu() asm volatile("pause\n\t");
- #define MEM_HISTORY 1024
- int shell_readline(int fd, char *buf);
- void print_ascii_logo();
- extern char *shell_current_path;
- char history_commands[MEM_HISTORY][INPUT_BUFFER_SIZE];
- char real_history_commands[MEM_HISTORY][INPUT_BUFFER_SIZE];
- int count_history;
- int current_command_index;
- void main_loop(int kb_fd)
- {
- count_history = 0;
- current_command_index = 0;
- unsigned char input_buffer[INPUT_BUFFER_SIZE] = {0};
-
- shell_current_path = (char *)malloc(3);
- memset(shell_current_path, 0, 3);
- shell_current_path[0] = '/';
- shell_current_path[1] = '\0';
-
- while (true)
- {
- int argc = 0;
- char **argv;
- printf("[DragonOS] %s # ", shell_current_path);
- memset(input_buffer, 0, INPUT_BUFFER_SIZE);
-
- put_string(" ", COLOR_BLACK, COLOR_WHITE);
-
- count_history++;
- int count = shell_readline(kb_fd, input_buffer);
- if (!count || current_command_index < count_history - 1)
- count_history--;
- if (count)
- {
- strcpy(real_history_commands[count_history - 1], input_buffer);
- count_history++;
- memset(history_commands, 0, sizeof(history_commands));
- for (int i = 0; i <= count_history - 2; i++)
- strcpy(history_commands[i], real_history_commands[i]);
- current_command_index = count_history - 1;
- }
- if (count)
- {
- char command_origin[strlen(input_buffer)];
- strcpy(command_origin, input_buffer);
- int cmd_num = parse_command(input_buffer, &argc, &argv);
- printf("\n");
- if (cmd_num >= 0)
- shell_run_built_in_command(cmd_num, argc, argv);
- }
- else
- printf("\n");
- }
- }
- int main()
- {
-
-
-
- print_ascii_logo();
-
-
-
- main_loop(0);
- while (1)
- ;
- }
- void clear_command(int count, char *buf)
- {
- for (int i = 0; i < count; i++)
- printf("%c", '\b');
- memset(buf, 0, sizeof(buf));
- }
- void change_command(char *buf, int type)
- {
- current_command_index -= type;
-
- if (current_command_index < 0)
- current_command_index++;
- if (current_command_index >= count_history - 1)
- {
-
- if (count_history == 1)
- {
-
- if (current_command_index > 1)
- current_command_index = 1;
- }
- else
- current_command_index = count_history - 2;
- }
- strcpy(buf, history_commands[current_command_index]);
- printf("%s", buf);
- put_string(" ", COLOR_BLACK, COLOR_WHITE);
- }
- int shell_readline(int fd, char *buf)
- {
- int key = 0;
- int count = 0;
- while (1)
- {
-
- key = getchar();
-
- if (key == 224)
- {
- key = getchar();
-
- switch (key)
- {
- case 72:
-
- if (count_history != 0)
- {
-
- printf("%c", '\b');
- clear_command(count, buf);
- count = 0;
-
- change_command(buf, 1);
- count = strlen(buf);
- }
- key = 0xc8;
- break;
- case 80:
-
- if (count_history != 0)
- {
-
- printf("%c", '\b');
- clear_command(count, buf);
- count = 0;
-
- change_command(buf, -1);
- count = strlen(buf);
- }
- key = 0x50;
- break;
- default:
- break;
- }
- }
- if (key == '\n')
- {
- if (count > 0 && current_command_index >= count_history)
- {
- memset(history_commands[current_command_index - 1], 0,
- sizeof(history_commands[current_command_index - 1]));
- count_history--;
- }
- printf("%c", '\b');
- return count;
- }
- if (key && key != 0x50 && key != 0xc8)
- {
- if (key == '\b')
- {
- if (count > 0)
- {
-
- printf("%c", '\b');
-
- printf("%c", '\b');
- buf[--count] = 0;
-
- put_string(" ", COLOR_BLACK, COLOR_WHITE);
- }
- }
- else
- {
- printf("%c", '\b');
- buf[count++] = key;
- printf("%c", key);
-
- put_string(" ", COLOR_BLACK, COLOR_WHITE);
- }
- if (count > 0 && current_command_index >= count_history)
- {
- memset(history_commands[count_history], 0, sizeof(history_commands[count_history]));
- strcpy(history_commands[count_history], buf);
- }
- else if (count > 0)
- {
- memset(history_commands[current_command_index], 0, sizeof(history_commands[current_command_index]));
- strcpy(history_commands[current_command_index], buf);
- }
- }
-
- if (count >= INPUT_BUFFER_SIZE - 1)
- {
- printf("%c", '\b');
- return count;
- }
- pause_cpu();
- }
- }
- void print_ascii_logo()
- {
- printf("\n\n");
- printf(" ____ ___ ____ \n");
- printf("| _ \\ _ __ __ _ __ _ ___ _ __ / _ \\ / ___| \n");
- printf("| | | || '__| / _` | / _` | / _ \\ | '_ \\ | | | |\\___ \\ \n");
- printf("| |_| || | | (_| || (_| || (_) || | | || |_| | ___) |\n");
- printf("|____/ |_| \\__,_| \\__, | \\___/ |_| |_| \\___/ |____/ \n");
- printf(" |___/ \n");
- printf("\n\n");
- }
|