🎨 将shell独立成单独的app

This commit is contained in:
fslongjin
2022-05-24 14:36:24 +08:00
parent 1eb9a299b6
commit afeca18206
8 changed files with 41 additions and 18 deletions

10
user/apps/Makefile Normal file
View File

@ -0,0 +1,10 @@
user_apps_sub_dirs=shell
all:
@list='$(user_apps_sub_dirs)'; for subdir in $$list; do \
echo "make all in $$subdir";\
cd $$subdir;\
$(MAKE) all CFLAGS="$(CFLAGS) -I $(shell pwd)";\
cd ..;\
done

4
user/apps/shell/Makefile Normal file
View File

@ -0,0 +1,4 @@
all: shell.o
shell.o: shell.c
gcc $(CFLAGS) -c shell.c -o shell.o

83
user/apps/shell/shell.c Normal file
View File

@ -0,0 +1,83 @@
#include <libc/unistd.h>
#include <libc/stdio.h>
#include <libc/fcntl.h>
#include <libc/stdlib.h>
#include <libKeyboard/keyboard.h>
int main()
{
char string[] = "/333.txt";
uint8_t buf[128] = {0};
char tips_str[] = "The first application 'init.bin' started successfully!\n";
put_string(tips_str, COLOR_GREEN, COLOR_BLACK);
printf("test printf: %s size: %d\n", string, sizeof(string));
char kb_file_path[] = "/dev/keyboard.dev";
int kb_fd = open(kb_file_path, 0);
printf("keyboard fd = %d\n", kb_fd);
while (true)
{
int key = keyboard_analyze_keycode(kb_fd);
if(key)
printf("%c", (char)key);
}
/*
int fd = open(string, 0);
printf("fd=%d\n", fd);
read(fd, buf, 128);
put_string(buf, COLOR_ORANGE, COLOR_BLACK);
lseek(fd, 0, SEEK_SET);
write(fd, tips_str, sizeof(tips_str)-1);
lseek(fd, 0, SEEK_SET);
// 由于暂时没有实现用户态的memset因此先手动清零
for(int i=0;i<128;++i)
buf[i] = 0;
read(fd, buf, 128);
put_string(buf, COLOR_YELLOW, COLOR_BLACK);
close(fd);
void *ptr[256] = {0};
for (int k = 0; k < 2; ++k)
{
printf("try to malloc 256*1M=256MB\n");
uint64_t js = 0;
for (int i = 0; i < 256; ++i)
{
ptr[i] = malloc(1024 * 1024);
js += *(uint64_t *)((uint64_t)(ptr[i]) - sizeof(uint64_t));
// if (*(uint64_t *)((uint64_t)(ptr[i]) - sizeof(uint64_t)) > 0x4008)
// printf("[%ld] start_addr = %#018lx, len = %#010lx\n", i, (uint64_t)(ptr[i]) - 8, *(uint64_t *)((uint64_t)(ptr[i]) - sizeof(uint64_t)));
}
// printf("ptr[0]->len=%lld\n", *(uint64_t *)((uint64_t)ptr[0] - sizeof(uint64_t)));
// printf("ptr[1]->len=%lld\n", *(uint64_t *)((uint64_t)ptr[1] - sizeof(uint64_t)));
// printf("ptr[24]->len=%lld\n", *(uint64_t*)((uint64_t)ptr[24] - sizeof(uint64_t)));
printf("alloc done. total used: %lld bytes\n", js);
printf("try to free...\n");
for (int i = 0; i < 256; ++i)
{
free(ptr[i]);
}
printf("free done!\n");
}
*/
// *p = 'a';
/*
pid_t p = fork();
if(p == 0)
put_string("subproc\n", COLOR_PURPLE, COLOR_BLACK);
else put_string("parent proc\n", COLOR_ORANGE, COLOR_BLACK);
*/
while (1)
;
}

50
user/apps/shell/shell.lds Normal file
View File

@ -0,0 +1,50 @@
OUTPUT_FORMAT("elf64-x86-64","elf64-x86-64","elf64-x86-64")
OUTPUT_ARCH(i386:x86-64)
ENTRY(main)
SECTIONS
{
. = 0x800000;
.text :
{
_text = .;
init.o(.text)
_etext = .;
}
. = ALIGN(8);
.data :
{
_data = .;
*(.data)
_edata = .;
}
rodata_start_pa = .;
.rodata :
{
_rodata = .;
*(.rodata)
_erodata = .;
}
.bss :
{
_bss = .;
*(.bss)
_ebss = .;
}
_end = .;
}