CoolPotOS/sysapp/shell.c

369 lines
11 KiB
C
Raw Normal View History

2024-04-09 23:46:56 +08:00
#include "../include/shell.h"
#include "../include/queue.h"
2024-04-11 22:53:15 +08:00
#include "../include/graphics.h"
2024-04-09 23:46:56 +08:00
#include "../include/common.h"
#include "../include/task.h"
#include "../include/cmos.h"
2024-05-13 20:52:43 +08:00
#include "../include/vdisk.h"
2024-05-18 23:44:25 +08:00
#include "../include/vfs.h"
2024-04-09 23:46:56 +08:00
extern Queue *key_char_queue;
2024-05-13 20:52:43 +08:00
extern vdisk vdisk_ctl[10];
2024-04-09 23:46:56 +08:00
char getc() {
while (key_char_queue->size == 0x00) {
printf("");
2024-04-09 23:46:56 +08:00
}
return queue_pop(key_char_queue);
}
int gets(char *buf, int buf_size) {
int index = 0;
char c;
while ((c = getc()) != '\n') {
if (c == '\b') {
if (index > 0) {
index--;
2024-05-21 21:09:04 +08:00
print("\b \b");
2024-04-09 23:46:56 +08:00
}
} else {
buf[index++] = c;
2024-05-21 21:09:04 +08:00
putchar(c);
2024-04-09 23:46:56 +08:00
}
}
buf[index] = '\0';
2024-05-21 21:09:04 +08:00
putchar(c);
2024-04-09 23:46:56 +08:00
return index;
}
int cmd_parse(char *cmd_str, char **argv, char token) {
int arg_idx = 0;
while (arg_idx < MAX_ARG_NR) {
argv[arg_idx] = NULL;
arg_idx++;
}
char *next = cmd_str;
int argc = 0;
while (*next) {
while (*next == token) *next++;
if (*next == 0) break;
argv[argc] = next;
while (*next && *next != token) *next++;
if (*next) *next++ = 0;
if (argc > MAX_ARG_NR) return -1;
argc++;
}
return argc;
}
void cmd_echo(int argc, char **argv) {
for (int i = 1; i < argc; i++) {
2024-05-21 21:09:04 +08:00
if (i == 1) print("");
else print(" ");
print(argv[i]);
2024-04-09 23:46:56 +08:00
}
2024-05-21 21:09:04 +08:00
print("\n");
2024-04-09 23:46:56 +08:00
}
2024-05-18 23:44:25 +08:00
void cmd_proc(int argc, char **argv) {
2024-04-13 22:03:54 +08:00
if (argc <= 1) {
2024-05-21 21:09:04 +08:00
printf("[Shell-PROC]: If there are too few parameters.\n");
2024-04-13 22:03:54 +08:00
return;
}
2024-05-18 23:44:25 +08:00
if (!strcmp("list", argv[1])) {
2024-04-13 22:03:54 +08:00
print_proc();
2024-05-18 23:44:25 +08:00
} else if (!strcmp("kill", argv[1])) {
2024-04-13 22:03:54 +08:00
if (argc <= 2) {
2024-05-21 21:09:04 +08:00
printf("[Shell-PROC-kill]: If there are too few parameters.\n");
2024-04-13 22:03:54 +08:00
return;
}
2024-05-18 23:44:25 +08:00
int pid = strtol(argv[2], NULL, 10);
2024-04-13 22:03:54 +08:00
task_kill(pid);
2024-05-18 23:44:25 +08:00
} else {
2024-05-21 21:09:04 +08:00
printf("[Shell-[PROC]]: Unknown parameter\n");
2024-04-13 22:03:54 +08:00
return;
}
2024-04-09 23:46:56 +08:00
}
2024-05-18 23:44:25 +08:00
void cmd_date() {
printf("System Time: %s\n", get_date_time());
printf("Memory Usage: [%dKB] | All Size: [%dMB]\n", memory_usage() / 1024,
(KHEAP_START + KHEAP_INITIAL_SIZE) / 1024 / 1024);
2024-04-09 23:46:56 +08:00
print_cpu_id();
2024-05-18 23:44:25 +08:00
2024-05-21 21:09:04 +08:00
print("\n");
2024-04-09 23:46:56 +08:00
}
void cmd_ls() {
2024-05-18 23:44:25 +08:00
List *ls = vfs_listfile("");
int files = 0, dirs = 0;
for (int i = 1; FindForCount(i, ls) != NULL; i++) {
vfs_file *d = (vfs_file *) FindForCount(i, ls)->val;
char *type;
if (d->type == DIR) {
type = "<DIR>"; //文件夹
dirs++;
} else if (d->type == HID) {
type = "<HID>"; //隐藏文件
files++;
} else {
type = "<FILE>"; //文件
files++;
}
printf("%-13s %-6s %d \n", d->name, type, d->size / 1024);
kfree(d);
}
printf("-----------------------------------------\n");
printf("Name Type Size(KB)\n"
"All directory: %d | All Files: %d\n",dirs, files);
2024-04-09 23:46:56 +08:00
2024-05-18 23:44:25 +08:00
DeleteList(ls);
kfree(ls);
2024-04-09 23:46:56 +08:00
}
void cmd_mkdir(int argc, char **argv) {
if (argc == 1) {
2024-05-21 21:09:04 +08:00
printf("[Shell-MKDIR]: If there are too few parameters, please specify the directory name.\n");
2024-04-09 23:46:56 +08:00
return;
}
2024-05-13 20:30:47 +08:00
2024-05-18 23:44:25 +08:00
vfs_file *file = get_cur_file(argv[1]);
if(file != NULL){
printf("This file is exist.\n");
return;
}
if(!vfs_createdict(argv[1])){
printf("Illegal create.\n");
} else printf("Create directory: %s\n", argv[1]);
2024-04-09 23:46:56 +08:00
}
void cmd_del(int argc, char **argv) {
if (argc == 1) {
2024-05-21 21:09:04 +08:00
print("[Shell-DEL]: If there are too few parameters, please specify the folder name.\n");
2024-04-09 23:46:56 +08:00
return;
}
2024-05-13 20:30:47 +08:00
2024-05-18 23:44:25 +08:00
vfs_file *file = get_cur_file(argv[1]);
if(file == NULL){
printf("Cannot found file.\n");
return;
}
bool b;
if(file->type == DIR){
b = vfs_deldir(argv[1]);
} else b = vfs_delfile(argv[1]);
if(!b){
printf("Illegal delete!\n");
}
2024-04-09 23:46:56 +08:00
}
2024-05-18 23:44:25 +08:00
void cmd_reset() {
2024-04-13 22:22:25 +08:00
reset_kernel();
}
2024-05-18 23:44:25 +08:00
void cmd_shutdown() {
2024-04-14 17:14:26 +08:00
shutdown_kernel();
}
2024-05-18 23:44:25 +08:00
void cmd_debug() {
2024-05-21 21:09:04 +08:00
screen_clear();
2024-05-19 18:05:00 +08:00
2024-05-18 23:44:25 +08:00
printf("%s for x86 [Version %s] \n", OS_NAME, OS_VERSION);
2024-05-21 21:09:04 +08:00
printf("Copyright 2024 XIAOYI12 (Build by GCC i686-elf-tools)\n");
2024-04-15 00:24:36 +08:00
extern int acpi_enable_flag;
extern uint8_t *rsdp_address;
2024-05-21 21:09:04 +08:00
printf("ACPI: Enable: %s | RSDP Address: 0x%08x\n", acpi_enable_flag ? "ENABLE" : "DISABLE",
2024-05-18 23:44:25 +08:00
rsdp_address);
2024-04-15 00:24:36 +08:00
int index = 0;
2024-05-18 23:44:25 +08:00
print_proc_t(&index, get_current(), get_current()->next, 0);
printf("Process Runnable: %d\n", index);
2024-04-15 00:24:36 +08:00
cmd_date();
2024-05-13 20:52:43 +08:00
for (int i = 0; i < 10; i++) {
if (vdisk_ctl[i].flag) {
vdisk vd = vdisk_ctl[i];
char id = i + ('A');
2024-05-18 23:44:25 +08:00
printf("[DISK-%c]: Size: %dMB | Name: %s\n", id, vd.size, vd.DriveName);
2024-05-13 20:52:43 +08:00
}
}
2024-04-15 00:24:36 +08:00
printf(" > > > > ====[Registers Info]==== < < < <\n");
register uint32_t eax asm("eax"),
2024-05-18 23:44:25 +08:00
ecx asm("ecx"),
esp asm("esp"),
ebp asm("ebp"),
ebx asm("ebx"),
esi asm("esi"),
edi asm("edi");
printf("EAX: 0x%08x | EBX 0x%08x | ECX 0x%08x | ESP 0x%08x \n", eax, ebx, ecx, esp);
printf("ESI: 0x%08x | EDI 0x%08x | EBP 0x%08x | EFLAGS 0x%08x\n", esi, edi, ebp, get_current()->context.eflags);
}
void cmd_cd(int argc, char **argv) {
if (argc == 1) {
2024-05-21 21:09:04 +08:00
print("[Shell-CD]: If there are too few parameters, please specify the path.\n");
2024-05-18 23:44:25 +08:00
return;
}
if (vfs_change_path(argv[1]) == 0) printf("Invalid path.\n");
2024-04-15 00:24:36 +08:00
}
2024-05-18 23:44:25 +08:00
void cmd_disk(int argc, char **argv) {
if (argc > 1) {
if (!strcmp("list", argv[1])) {
2024-05-13 20:52:43 +08:00
printf("[Disk]: Loaded disk - ");
for (int i = 0; i < 10; i++) {
if (vdisk_ctl[i].flag) {
vdisk vd = vdisk_ctl[i];
char id = i + ('A');
2024-05-18 23:44:25 +08:00
printf("(%c) ", id, vd.size, vd.DriveName);
2024-05-13 20:52:43 +08:00
}
}
printf("\n");
return;
}
2024-05-18 23:44:25 +08:00
if (strlen(argv[1]) > 1) {
2024-05-21 21:09:04 +08:00
printf("[DISK]: Cannot found disk.\n");
2024-05-13 20:52:43 +08:00
return;
}
2024-04-20 02:43:22 +08:00
2024-05-13 20:52:43 +08:00
for (int i = 0; i < 10; i++) {
if (vdisk_ctl[i].flag) {
vdisk vd = vdisk_ctl[i];
char id = i + ('A');
2024-05-18 23:44:25 +08:00
if (id == (argv[1][0])) {
2024-05-13 20:52:43 +08:00
printf("[Disk(%c)]: \n"
" Size: %dMB\n"
" Name: %s\n"
" WriteAddress: 0x%08x\n"
2024-05-18 23:44:25 +08:00
" ReadAddress: 0x%08x\n", id, vd.size, vd.DriveName, vd.Write, vd.Read);
2024-05-13 20:52:43 +08:00
return;
}
}
}
2024-05-21 21:09:04 +08:00
printf("[DISK]: Cannot found disk.\n");
2024-04-20 02:43:22 +08:00
return;
}
2024-05-13 20:52:43 +08:00
printf("[Disk]: Loaded disk - ");
for (int i = 0; i < 10; i++) {
if (vdisk_ctl[i].flag) {
vdisk vd = vdisk_ctl[i];
char id = i + ('A');
2024-05-18 23:44:25 +08:00
printf("(%c) ", id, vd.size, vd.DriveName);
2024-05-13 20:52:43 +08:00
}
}
printf("\n");
2024-04-20 02:43:22 +08:00
}
2024-05-18 23:44:25 +08:00
char *user() {
2024-05-13 22:17:38 +08:00
printf("\n\nPlease type your username and password.\n");
char com[MAX_COMMAND_LEN];
2024-05-18 23:44:25 +08:00
char *username = "admin";
char *password = "12345678";
while (1) {
2024-05-13 22:17:38 +08:00
print("username/> ");
if (gets(com, MAX_COMMAND_LEN) <= 0) continue;
2024-05-18 23:44:25 +08:00
if (!strcmp(username, com)) {
2024-05-13 22:17:38 +08:00
break;
} else printf("Cannot found username, Please check your info.\n");
}
2024-05-18 23:44:25 +08:00
while (1) {
2024-05-13 22:17:38 +08:00
print("password/> ");
if (gets(com, MAX_COMMAND_LEN) <= 0) continue;
2024-05-18 23:44:25 +08:00
if (!strcmp(password, com)) {
2024-05-13 22:17:38 +08:00
break;
} else printf("Unknown password, Please check your info.\n");
}
return username;
}
2024-05-18 23:44:25 +08:00
void setup_shell() {
char *user1 = "default";//user();
2024-05-21 21:09:04 +08:00
screen_clear();
2024-05-21 22:40:15 +08:00
printf("Welcome to %s %s (CPOS Kernel x86_64)\n"
"\n"
" * SourceCode: https://github.com/xiaoyi1212/CoolPotOS\n"
" * Website: https://github.com/plos-clan\n"
"\n"
" System information as of %s \n"
"\n"
" Processes: %d\n"
" Users logged in: %s\n"
" Memory usage: %d B \n"
"\n"
"Copyright 2024 XIAOYI12 (Build by GCC i686-elf-tools)\n"
,OS_NAME
,OS_VERSION
,get_date_time()
,get_procs()
,user1
,memory_usage());
2024-04-09 23:46:56 +08:00
char com[MAX_COMMAND_LEN];
char *argv[MAX_ARG_NR];
int argc = -1;
2024-05-18 23:44:25 +08:00
char *buffer[255];
2024-04-09 23:46:56 +08:00
while (1) {
2024-05-18 23:44:25 +08:00
vfs_getPath(buffer);
2024-05-21 22:40:15 +08:00
printf("%s@localhost: %s\\$ ", user1, buffer);
2024-04-09 23:46:56 +08:00
if (gets(com, MAX_COMMAND_LEN) <= 0) continue;
argc = cmd_parse(com, argv, ' ');
if (argc == -1) {
2024-05-21 21:09:04 +08:00
print("[Shell]: Error: out of arguments buffer\n");
2024-04-09 23:46:56 +08:00
continue;
}
if (!strcmp("version", argv[0]))
2024-05-18 23:44:25 +08:00
printf("%s for x86 [%s]\n", OS_NAME, OS_VERSION);
2024-04-09 23:46:56 +08:00
else if (!strcmp("echo", argv[0]))
cmd_echo(argc, argv);
else if (!strcmp("clear", argv[0]))
2024-05-21 21:09:04 +08:00
screen_clear();
2024-04-09 23:46:56 +08:00
else if (!strcmp("proc", argv[0]))
2024-04-13 22:03:54 +08:00
cmd_proc(argc, argv);
2024-04-09 23:46:56 +08:00
else if (!strcmp("sysinfo", argv[0]))
cmd_date();
else if (!strcmp("ls", argv[0]))
cmd_ls();
else if (!strcmp("mkdir", argv[0]))
cmd_mkdir(argc, argv);
else if (!strcmp("del", argv[0]) || !strcmp("rm", argv[0]))
cmd_del(argc, argv);
else if (!strcmp("reset", argv[0]))
cmd_reset();
2024-05-18 23:44:25 +08:00
else if (!strcmp("shutdown", argv[0]) || !strcmp("exit", argv[0]))
2024-04-14 17:14:26 +08:00
cmd_shutdown();
2024-04-15 00:24:36 +08:00
else if (!strcmp("debug", argv[0]))
cmd_debug();
2024-05-13 20:52:43 +08:00
else if (!strcmp("disk", argv[0]))
2024-05-18 23:44:25 +08:00
cmd_disk(argc, argv);
else if (!strcmp("cd", argv[0]))
cmd_cd(argc, argv);
2024-04-09 23:46:56 +08:00
else if (!strcmp("help", argv[0]) || !strcmp("?", argv[0]) || !strcmp("h", argv[0])) {
2024-05-21 21:09:04 +08:00
print("-=[CoolPotShell Helper]=-\n");
print("help ? h Print shell help info.\n");
print("version Print os version.\n");
print("echo <msg> Print message.\n");
print("ls List all files.\n");
print("mkdir <name> Make a directory.\n");
print("del rm <name> Delete a file.\n");
print("sysinfo Print system info.\n");
print("proc [kill<pid>|list] Lists all running processes.\n");
print("reset Reset OS.\n");
print("shutdown exit Shutdown OS.\n");
print("debug Print os debug info.\n");
print("disk [list|<ID>] List or view disks.\n");
print("cd <path> Change shell top directory.\n");
} else printf("\033[Shell]: Unknown command '%s'.\n", argv[0]);
2024-04-09 23:46:56 +08:00
}
}