Added int argc and char** argv

This commit is contained in:
2024-05-31 10:19:05 +02:00
parent eef7bf1aa2
commit 13bac619fc
2 changed files with 53 additions and 39 deletions
+1 -2
View File
@@ -117,8 +117,7 @@ void read(char* buffer) {
} }
} }
buffer[len++] = c; // Add newline putc(c); // Print the newline
putc(c);
buffer[len] = '\0'; // Null terminate buffer[len] = '\0'; // Null terminate
} }
+52 -37
View File
@@ -76,51 +76,64 @@ void main() {
terminal->fg = FG_COLOR; terminal->fg = FG_COLOR;
terminal->bg = BG_COLOR; terminal->bg = BG_COLOR;
clear(); clear();
printf("\x1b[2Mkern.\n\n\x1b[8M"); printf("kern.\n\n");
// Run Interactive Shell Program // Run Interactive Shell Program
// TODO: Make it in another file // TODO: Make it in another file
while (1) { while (1) {
printf(PROMPT); printf(PROMPT);
char input[256]; char input[256];
char* inputPtr = input;
read(input); read(input);
if (input[0] == '\n') continue; if (*input == '\0') continue;
char* args; int argc = 0;
if ((args = strchr(input, ' ')) == NULL) { char* argv[10] = { NULL };
args = strchr(input, '\n');
while (*inputPtr == ' ') inputPtr++;
argv[argc++] = inputPtr;
while (*inputPtr != '\0') {
if (*inputPtr == ' ') {
*inputPtr++ = '\0';
while (*inputPtr == ' ') inputPtr++; // Skip extra spaces
argv[argc++] = inputPtr;
} else {
inputPtr++;
}
} }
*args++ = '\0'; // Separate command from args if (strcmp(argv[0], "clear") == 0) {
if (strcmp(input, "clear") == 0) {
clear(); clear();
} else if (strcmp(input, "echo") == 0) { } else if (strcmp(argv[0], "echo") == 0) {
char* str = args; if (argc < 2) continue;
char* printStr = args;
while (*args != '\0') { char* str = argv[1];
if (*args == '\\') { char* printStr = argv[1];
args++;
if (*args == 'n') *str++ = '\n'; while (*argv[1] != '\0') {
else if (*args == 'b') *str++ = '\b'; if (*argv[1] == '\\') {
else if (*args == 'e') *str++ = '\x1b'; argv[1]++;
else if (*args == '\\') *str++ = '\\'; if (*argv[1] == 'n') *str++ = '\n';
else if (*argv[1] == 'b') *str++ = '\b';
else if (*argv[1] == 'e') *str++ = '\x1b';
else if (*argv[1] == '\\') *str++ = '\\';
else { else {
*str++ = '\\'; *str++ = '\\';
*str++ = *args; *str++ = *argv[1];
} }
args++; argv[1]++;
} else { } else {
*str++ = *args++; *str++ = *argv[1]++;
} }
} }
*str = '\0'; *str = '\0';
printf("%s", printStr); printf("%s\n", printStr);
} else if (strcmp(input, "exit") == 0) { } else if (strcmp(argv[0], "exit") == 0) {
break; break;
} else if (strcmp(input, "gfx") == 0) { } else if (strcmp(argv[0], "gfx") == 0) {
clear(); clear();
// Draw some stuff :) // Draw some stuff :)
@@ -153,7 +166,7 @@ void main() {
getc(); // Wait until we get a 'q' getc(); // Wait until we get a 'q'
clear(); clear();
} else if (strcmp(input, "help") == 0) { } else if (strcmp(argv[0], "help") == 0) {
printf("Available Commands:\n"); printf("Available Commands:\n");
printf(" clear | Clears the screen\n"); printf(" clear | Clears the screen\n");
printf(" echo | Prints a message to stdout\n"); printf(" echo | Prints a message to stdout\n");
@@ -166,20 +179,20 @@ void main() {
printf(" sleep | Sleeps for input number of seconds\n"); printf(" sleep | Sleeps for input number of seconds\n");
printf(" soundtest | Plays a tune :)\n"); printf(" soundtest | Plays a tune :)\n");
printf(" test | Performs tests\n"); printf(" test | Performs tests\n");
} else if (strcmp(input, "ls") == 0) { } else if (strcmp(argv[0], "ls") == 0) {
printFiletable(filetable); printFiletable(filetable);
} else if (strcmp(input, "memmap") == 0) { } else if (strcmp(argv[0], "memmap") == 0) {
printPhysicalMemmap(); printPhysicalMemmap();
} else if (strcmp(input, "reboot") == 0) { } else if (strcmp(argv[0], "reboot") == 0) {
outb(0x64, 0xFE); outb(0x64, 0xFE);
} else if (strcmp(input, "sleep") == 0) { } else if (strcmp(argv[0], "sleep") == 0) {
if (*args == '\0') continue; if (*argv[1] == '\0') continue;
uint32_t ms = atoi(args) * 1000; // Convert seconds to miliseconds uint32_t ms = atoi(argv[1]) * 1000; // Convert seconds to miliseconds
if (ms == 0) continue; if (ms == 0) continue;
sleep(ms); sleep(ms);
} else if (strcmp(input, "soundtest") == 0) { } else if (strcmp(argv[0], "soundtest") == 0) {
printf("Playing...\n"); printf("Playing...\n");
enableSpeaker(); enableSpeaker();
@@ -204,13 +217,13 @@ void main() {
rest(120); rest(120);
disableSpeaker(); disableSpeaker();
} else if (strcmp(input, "test") == 0) { } else if (strcmp(argv[0], "test") == 0) {
printf("\x1b[1MError: 'test' is not available.\x1b[8M"); printf("\x1b[1MError: 'test' is not available.\x1b[8M");
} else { } else {
fileEntry_t* file = findFile(input); fileEntry_t* file = findFile(argv[0]);
if (file == NULL) { if (file == NULL) {
printf("Command not found: %s\n", input); printf("Command not found: %s\n", argv[0]);
continue; continue;
} }
@@ -228,7 +241,7 @@ void main() {
} }
} }
readFile(input, entryPoint); readFile(argv[0], entryPoint);
if (strcmp(file->filetype, "bin") == 0) { if (strcmp(file->filetype, "bin") == 0) {
// Reset malloc // Reset malloc
@@ -237,7 +250,9 @@ void main() {
mallocPhysicalAddr = 0; mallocPhysicalAddr = 0;
mallocPages = 0; mallocPages = 0;
((void(*)(void))entryPoint)(); // Call program // Call program
// TODO: Use return value
((int(*)(int argc, char** argv))entryPoint)(argc, argv);
for (uint32_t i = 0, virtualAddr = mallocVirtualAddr; i < mallocPages; i++, virtualAddr += PAGE_SIZE) { for (uint32_t i = 0, virtualAddr = mallocVirtualAddr; i < mallocPages; i++, virtualAddr += PAGE_SIZE) {
ptEntry_t* page = getPage(virtualAddr); ptEntry_t* page = getPage(virtualAddr);