Click here to Skip to main content
15,887,453 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
<pre>#include <stdbool.h>
#include <kernel/tty.h>
#include <stdint.h>
#include <stdio.h>
#include <stddef.h>
#include <string.h>
#include <stdarg.h>
#include "GDTandIDT.h"
#include "mm.h"
#include "schedule.h"
#include "fs.h"
#include "initrd.h"

#define MAX_INPUT_LENGTH 256

char read_char() {}

void print_char(char c) {}

int strcmp(const char* str1, const char* str2) {
    while (*str1 && (*str1 == *str2)) {
        str1++;
        str2++; }
    return *(unsigned char*)str1 - *(unsigned char*)str2; }

void read_command(char* buffer, size_t size) {
    size_t i = 0;
    char c;
    while (i < size - 1 && (c = read_char()) != '\n') {
        buffer[i++] = c;
        print_char(c); }
    buffer[i] = '\0';
    print_char('\n'); }

void execute_command(char* command) {
    if (strcmp(command, "help") == 0) {
        printf("Available commands:\n");
        printf(" - help: Display available commands\n");
        printf(" - version: Display kernel version\n"); }
    else if (strcmp(command, "version") == 0) {
        printf("Kernel version 1.0\n"); }
    else { printf("Unknown command. Type 'help' to see available commands.\n"); } }

void print_prompt() {
    printf("> "); }

void kernel_main(void) {
    init_gdt();
    init_idt();
    terminal_initialize();
    init_dynamic_mem();
    
    size_t allocation_size = 1024;
    void* allocated_memory;
    int allocation_count = 0;
    char command[MAX_INPUT_LENGTH];
    
    while ((allocated_memory = malloc(allocation_size)) != NULL) {
        allocation_count++;
        allocation_size *= 2; }
    
    if (allocated_memory != NULL) {
        *(int*)allocated_memory = 42; }
    
    free(allocated_memory);
    
    while (1) {
        print_prompt();
        read_command(command, MAX_INPUT_LENGTH);
        execute_command(command);
        schedule();
        kbd(); } }


It show me something like this when I type

> Unknown command. Type 'help' to see available commands.
a> Unknown command. Type 'help' to see available commands.

What I have tried:

I was try to fix it but I only make spaghetti code :"(
Posted
Comments
Richard MacCutchan 20-Nov-23 5:38am    
You only have two valid commands, "help" and "version". So anything else is invalid. But I do not see how the above could work since both read_char and print_char do not do anything.
Andre Oosthuizen 20-Nov-23 12:49pm    
I got lost after line 10, time to put the whiskey away.

1 solution

You need to understand the basics of it before you can use the prompts - Guide to code a simple shell in C | Tutorial[^]

This should point you in the right direction.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900