Skip to content

Commit 0176c23

Browse files
committed
add arguments in shell, improve alloc usage, improve libc
1 parent 54f9099 commit 0176c23

File tree

8 files changed

+160
-35
lines changed

8 files changed

+160
-35
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
commit 54f9099f8dbb07eb13ec7c19189144df53bb044f
2+
Author: Alexeev Bronislav <alexeev.dev@mail.ru>
3+
Date: Fri Aug 22 21:18:22 2025 +0700
4+
5+
update makefile
6+
17
commit eb0a7cf5f301e5d63a14cc1afe73ec84fe2653e5
28
Author: Alexeev Bronislav <alexeev.dev@mail.ru>
39
Date: Fri Aug 22 07:06:26 2025 +0700

src/kernel/kernel/kernel.c

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "kernel.h"
1313
#include "utils.h"
1414
#include "../libc/stdio.h"
15+
#include "../libc/mem.h"
1516
#include "../libc/string.h"
1617

1718
void kmain() {
@@ -30,33 +31,52 @@ void kmain() {
3031
kprint("Copyright (C) alexeev-prog\nRepository: https://github.com/alexeev-prog/KintsugiOS\n");
3132

3233
// Уведомление о старте оболочки командной строки
33-
kprint("\nKeramika Busybox v0.1.0 "
34+
kprint("\nKeramika Shell v0.1.0 "
3435
"Type END to halt the CPU\n"
3536
"Type HELP to view commands\n\n!#> ");
3637
}
3738

39+
char** get_args(char *input) {
40+
char *token = strtok(input, " ");
41+
char** args;
42+
int arg_counter = 0;
43+
44+
while(token) {
45+
token = strtok(NULL, " ");
46+
47+
args[arg_counter] = token;
48+
arg_counter++;
49+
}
50+
51+
kfree(token);
52+
53+
return args;
54+
}
55+
3856
void user_input(char *input) {
3957
// Массив структур команд, состоящий из самой команды, подсказки и указателя до void-функции
40-
struct { char *text, *hint; void (*command)(); } commands[] = {
58+
struct { char *text, *hint; void (*command)(char**); } commands[] = {
4159
// Команда Подсказка для команды Указатель до функции
42-
{.text="END", .hint="HALT CPU", .command=&halt_cpu},
43-
{.text="CLEAR", .hint="Clear screen", .command=&clear_screen},
44-
{.text="KMALLOC", .hint="Kernel Page Malloc", .command=&malloc_command_shell},
45-
{.text="QEMUSHUTDOWN", .hint="Shutdown QEMU", .command=&shutdown_qemu},
46-
{.text="INFO", .hint="Get info", .command=&info_command_shell},
47-
{.text="FREEMEMADDR", .hint="Get free mem addr", .command=&print_freememaddr},
48-
{.text="TESTMEM", .hint="Test memory", .command=&test_mem_command},
49-
{.text="KMEMDUMP", .hint="Dump memory", .command=&mem_dump}
60+
{.text="END", .hint="HALT CPU", .command=&halt_cpu},
61+
{.text="CLEAR", .hint="Clear screen", .command=&clear_screen_command},
62+
{.text="AMALLOC", .hint="Kernel Arena Malloc. Usage: AMALLOC <size>", .command=&arena_malloc_command_shell},
63+
{.text="QEMUSHUTDOWN", .hint="Shutdown QEMU", .command=&shutdown_qemu},
64+
{.text="INFO", .hint="Get info", .command=&info_command_shell},
65+
{.text="FREEMEMADDR", .hint="Get free mem addr", .command=&print_freememaddr},
66+
{.text="TESTMEM", .hint="Test memory", .command=&test_mem_command},
67+
{.text="KMEMDUMP", .hint="Dump memory", .command=&mem_dump},
68+
{.text="KMALLOC", .hint="Alloc memory. Usage: KMALLOC <size>", .command=&kmalloc_command}
5069
};
51-
// TODO: добавить поддержку аргументов
5270

5371
int executed = 0;
5472

73+
char** args = get_args(input);
74+
5575
const int commands_length = sizeof(commands) / sizeof(commands[0]);
5676

5777
for (int i = 0; i < commands_length; ++i) {
5878
if (strcmp(input, commands[i].text) == 0) {
59-
commands[i].command();
79+
commands[i].command(args);
6080
executed = 1;
6181
break;
6282
}

src/kernel/kernel/utils.c

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,21 @@
1010
#include "../libc/mem.h"
1111
#include "../libc/string.h"
1212
#include "../cpu/ports.h"
13+
#include "../libc/stdio.h"
1314

14-
void shutdown_qemu() {
15+
void print_freememaddr(char** args) {
16+
get_freememaddr();
17+
}
18+
19+
void clear_screen_command(char** args) {
20+
clear_screen();
21+
}
22+
23+
void shutdown_qemu(char** args) {
1524
outports(0x604, 0x2000);
1625
}
1726

18-
void halt_cpu() {
27+
void halt_cpu(char** args) {
1928
halted_cpu_screen_clear();
2029
kprint_colored("Kintsugi OS 0.1.0\n\n", BLUE_ON_WHITE_CLR_CODE);
2130
kprint_colored("Halted CPU Blue Screen\n", BLUE_ON_WHITE_CLR_CODE);
@@ -25,38 +34,55 @@ void halt_cpu() {
2534
asm volatile("hlt");
2635
}
2736

28-
void help_command_shell() {
37+
void help_command_shell(char** args) {
2938
kprint("END - stopping the CPU\n"
3039
"INFO - info about OS\n"
3140
"PAGE - to request a kmalloc()\n"
3241
"CLEAR - clear the screen\n"
3342
"SHUTDOWN - shutdown QEMU\n");
3443
}
3544

36-
void info_command_shell() {
45+
void info_command_shell(char** args) {
3746
kprint("Kintsugi OS 0.1.0 by alexeev-prog\n");
3847
}
3948

40-
void malloc_command_shell() {
49+
void arena_malloc_command_shell(char** args) {
50+
if (args[0] == NULL) {
51+
kprint("AMALLOC usage: AMALLOC <page size>");
52+
return;
53+
}
54+
4155
u32 phys_addr;
42-
u32 page = kmalloc(16, 1, &phys_addr);
56+
u32 page = kmalloc(strtoint(args[0]), 1, &phys_addr);
4357
char page_str[16] = "";
4458
hex_to_ascii(page, page_str);
4559
char phys_str[16] = "";
4660
hex_to_ascii(phys_addr, phys_str);
4761

48-
kprint("Page address: ");
49-
kprint(page_str);
50-
kprint("\nPhysical address: ");
51-
kprint(phys_str);
52-
kprint("\n");
62+
kprintf("ARENA MALLOC %d bytes\n", strtoint(args[0]));
63+
kprintf("Page address: %s\nPhysical address: %s\n", page_str, phys_str);
5364
}
5465

55-
void mem_dump() {
66+
void mem_dump(char** args) {
5667
kmemdump();
5768
}
5869

59-
void test_mem_command() {
70+
void kmalloc_command(char** args) {
71+
if (args[0] == NULL) {
72+
kprint("KMALLOC usage: KMALLOC <bytes>");
73+
return;
74+
}
75+
76+
int size = strtoint(args[0]);
77+
void* ptr = kmalloc_new(size);
78+
79+
char buf1[32] = "";
80+
hex_to_ascii((int)ptr, buf1);
81+
82+
kprintf("Allocate %d bytes.\nPointer: %s\n", size, buf1);
83+
}
84+
85+
void test_mem_command(char** args) {
6086
void* ptr1 = kmalloc_new(64);
6187
void* ptr2 = kmalloc_new(128);
6288
void* ptr3 = kmalloc_new(256);

src/kernel/kernel/utils.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22
#ifndef UTILS_H
33
#define UTILS_H
44

5-
void shutdown_qemu();
6-
void halt_cpu();
7-
void help_command_shell();
8-
void info_command_shell();
9-
void malloc_command_shell();
10-
void test_mem_command();
11-
void mem_dump();
5+
void print_freememaddr(char** args);
6+
void shutdown_qemu(char** args);
7+
void halt_cpu(char** args);
8+
void help_command_shell(char** args);
9+
void info_command_shell(char** args);
10+
void arena_malloc_command_shell(char** args);
11+
void test_mem_command(char** args);
12+
void mem_dump(char** args);
13+
void clear_screen_command(char** args);
14+
void kmalloc_command(char** args);
1215

1316
#endif

src/kernel/libc/mem.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,15 +167,15 @@ u32 kmalloc(u32 size, int align, u32 *phys_addr) {
167167
hex_to_ascii(size, size_str);
168168
kprint(size_str);
169169
kprint(" bytes at ");
170-
print_freememaddr();
170+
get_freememaddr();
171171
kprint("\n");
172172

173173
u32 ret = free_mem_addr;
174174
free_mem_addr += size;
175175
return ret;
176176
}
177177

178-
void print_freememaddr() {
178+
void get_freememaddr() {
179179
char free_mem_addr_str[16] = "";
180180
hex_to_ascii(free_mem_addr, free_mem_addr_str);
181181

src/kernel/libc/mem.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ typedef struct mem_block {
2222

2323
void memory_copy(u8 *source, u8 *dest, int nbytes);
2424
void memory_set(u8 *dest, u8 val, u32 len);
25-
void print_freememaddr();
25+
void get_freememaddr();
2626
u32 kmalloc(u32 size, int align, u32 *phys_addr);
2727

2828
// Новые функции

src/kernel/libc/string.c

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,19 @@ void int_to_ascii(int n, char str[]) {
1818
reverse(str);
1919
}
2020

21+
int strtoint(char* str) {
22+
int rc = 0;
23+
unsigned i = 0;
24+
// C guarantees that '0'-'9' have consecutive values
25+
while (str[i] >= '0' && str[i] <= '9') {
26+
rc *= 10;
27+
rc += str[i] - '0';
28+
++i;
29+
}
30+
31+
return rc;
32+
}
33+
2134
void hex_to_ascii(int n, char str[]) {
2235
append(str, '0');
2336
append(str, 'x');
@@ -83,3 +96,57 @@ int strcmp(char s1[], char s2[]) {
8396
}
8497
return s1[i] - s2[i];
8598
}
99+
100+
unsigned int is_delim(char c, char *delim) {
101+
while(*delim != '\0') {
102+
if(c == *delim)
103+
return 1;
104+
105+
delim++;
106+
}
107+
return 0;
108+
}
109+
110+
char *strtok(char *src_str, char *delim) {
111+
static char *backup_string; // бекап строки
112+
113+
if(!src_str) {
114+
src_str = backup_string;
115+
}
116+
117+
if(!src_str) {
118+
return NULL;
119+
}
120+
121+
while(1) {
122+
if(is_delim(*src_str, delim)) {
123+
src_str++;
124+
continue;
125+
}
126+
127+
if(*src_str == '\0') {
128+
// конец строки
129+
return NULL;
130+
}
131+
132+
break;
133+
}
134+
char *ret = src_str;
135+
136+
while(1) {
137+
if(*src_str == '\0') {
138+
/*конец входной строки
139+
и следующее выполнение возвратит NULL*/
140+
backup_string = src_str;
141+
return ret;
142+
}
143+
144+
if(is_delim(*src_str, delim)) {
145+
*src_str = '\0';
146+
backup_string = src_str + 1;
147+
return ret;
148+
}
149+
150+
src_str++;
151+
}
152+
}

src/kernel/libc/string.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33

44
void int_to_ascii(int n, char str[]);
55
void hex_to_ascii(int n, char str[]);
6+
int strtoint(char* str);
67
void reverse(char s[]);
78
int strlen(char s[]);
89
void backspace(char s[]);
910
void append(char s[], char n);
1011
int strcmp(char s1[], char s2[]);
1112
void strcpy(char *dest, char *src);
13+
unsigned int is_delim(char c, char *delim);
14+
char *strtok(char *src_str, char *delim);
1215

1316
#endif

0 commit comments

Comments
 (0)