matos/userspace/main_user.c

79 lines
2.0 KiB
C
Raw Permalink Normal View History

2021-11-05 23:02:23 +01:00
#include "libc.h"
2022-09-03 23:41:33 +02:00
#include "stdarg.h"
2023-03-21 23:11:56 +01:00
#include "tiny.h"
2021-11-06 00:13:40 +01:00
int func_help()
{
printf("\nAvailable Commands:\n");
printf(" suicide\n");
printf(" help\n");
printf(" syscall5\n");
2022-08-09 16:15:00 +02:00
printf(" alloc\n");
2023-03-21 23:11:56 +01:00
printf(" tiny: a tiny C interpreter\n");
2021-11-06 00:13:40 +01:00
return 0;
}
int func_suicide()
{
printf("User is about to suicide\n");
int *yolo = 0;
*yolo = 1;
return 0;
}
2022-09-03 23:41:33 +02:00
void *initialHeap = 0;
int func_alloc()
{
2022-08-09 16:15:00 +02:00
2022-09-03 23:41:33 +02:00
if (initialHeap == 0) {
2022-08-09 16:15:00 +02:00
initialHeap = brk(0);
}
2022-09-03 23:41:33 +02:00
printf("Testing allocation\n");
int allocSize = 4096 * 2;
void *currentHeap = brk(0);
if (currentHeap - initialHeap < allocSize) {
brk(initialHeap + allocSize);
2022-08-09 16:15:00 +02:00
}
2022-09-03 23:41:33 +02:00
int *allocatedData = (int *)initialHeap;
for (unsigned int i = 0; i < allocSize / sizeof(int); i++) {
allocatedData[i] = i;
2022-08-09 16:15:00 +02:00
}
2022-09-03 23:41:33 +02:00
printf("Success\n");
2022-08-09 16:15:00 +02:00
return 0;
}
2021-11-05 23:02:23 +01:00
int main(int argc, char *argv[])
{
(void)argc;
(void)argv;
2021-11-06 00:13:40 +01:00
char buf[64];
2021-11-08 22:52:46 +01:00
printf("Shell starting... type \"help\" for help\n");
2021-11-06 00:13:40 +01:00
while (1) {
printf(">");
if (readline(buf, sizeof(buf)))
continue;
if (strcmp(buf, "help") == 0) {
func_help();
continue;
}
if (strcmp(buf, "suicide") == 0) {
func_suicide();
continue;
}
if (strcmp(buf, "syscall5") == 0) {
testSycall5(1, 2, 3, 4, 5);
continue;
}
2022-08-09 16:15:00 +02:00
if (strcmp(buf, "alloc") == 0) {
func_alloc();
continue;
}
2023-03-21 23:11:56 +01:00
if (strcmp(buf, "tiny") == 0) {
func_tiny();
continue;
}
2021-11-06 00:13:40 +01:00
}
2021-11-05 23:02:23 +01:00
return 0;
}