<commit_before>#include "ksh.h"

#include "string.h"
#include "array_utils.h"

#include "ezfs.h"

BOOL ksh_take_fb_control()
{
    RegisterKeyboardHook(&ksh_kb_hook);

    promptText = "|>\0";
    promptLength = strlen(promptText);
    
    cursorColumn = 0;
    
    lines = (char**)malloc(sizeof(char*) * KSH_LINES_IN_MEMORY);
    lines[0] = (char*)malloc(sizeof(char) * KSH_MAX_LINE_LENGTH);
    strcpy(lines[0], "");
    
    for(int i = 1; i < KSH_LINES_IN_MEMORY; i++)
    {
        lines[i] = NULL;
    }
    
    fbClear();
    
    fbMoveCursor(0, ksh_get_line_number_from_bottom(0));
    
    ksh_render_line(0);
    
    return TRUE;
}

void ksh_fb_release()
{
    DeregisterKeyboardHook(&ksh_kb_hook);
}

void ksh_write(const char* characters)
{
    ksh_push_lines();
    
    size_t cLen = strlen(characters);
    
    for(size_t i = 0; i < cLen; i++)
    {
        if(characters[i] == '\n')
        {
            ksh_push_lines();
        }
        else
        {
            char* line = ksh_get_current_type_line();
            line[cursorColumn] = characters[i];
            cursorColumn++;
        }
    }
    
    char* line = ksh_get_current_type_line();
    
    line[cursorColumn] = '\0';
}

void ksh_write_line(const char* line)
{
    ksh_write(line);
    ksh_write("");
}

void ksh_update()
{
    
}

void ksh_render_line(int nb)
{
    if(nb >= KSH_LINES_IN_MEMORY && lines[nb] == NULL)
        return;
    
    int screenRow = ksh_get_line_number_from_bottom(nb);
    
    if(screenRow < 0)
        return;
    
    fbMoveCursor(0, screenRow);
    
    if(ksh_is_current_type_line(nb))
    {
        // Draw prompt
        fbPutString(promptText);
    }
    
    fbPutString(lines[nb]);
}

void ksh_process_command(char* commandline)
{
    Debugger();
    
    if(strcmp(commandline, "") == 0)
    {
        return;
    }
    
    size_t nb = 0;
    char** parts = strspl(commandline, " ", &nb);
    
    BOOL success = FALSE;
    
    if(nb > 0)
    {
        if(strcmp(parts[0], "echo") == 0)
        {
            if(nb > 1)
            {
                for(size_t i = 1; i < nb; i++)
                {
                    ksh_write(parts[i]);
                }
                
                success = TRUE;
            }
        }
        else if(strcmp(parts[0], "fread") == 0)
        {
            if(nb > 1)
            {
                char* fileName = parts[1];
                
                file_h file = ezfs_find_file(fileName);
                if(file != FILE_NOT_FOUND)
                {
                    uint8_t* buf = NULL;
                    size_t bytesRead = ezfs_read_file(file, &buf);
                    
                    char* bufToStr = (char*)malloc(sizeof(char) * bytesRead + 1);<commit_msg>Added a function to write patterns to disk.<commit_after>#include "ksh.h"

#include "string.h"
#include "array_utils.h"

#include "ezfs.h"
#include "ata_driver.h"

BOOL ksh_take_fb_control()
{
    RegisterKeyboardHook(&ksh_kb_hook);

    promptText = "|>\0";
    promptLength = strlen(promptText);
    
    cursorColumn = 0;
    
    lines = (char**)malloc(sizeof(char*) * KSH_LINES_IN_MEMORY);
    lines[0] = (char*)malloc(sizeof(char) * KSH_MAX_LINE_LENGTH);
    strcpy(lines[0], "");
    
    for(int i = 1; i < KSH_LINES_IN_MEMORY; i++)
    {
        lines[i] = NULL;
    }
    
    fbClear();
    
    fbMoveCursor(0, ksh_get_line_number_from_bottom(0));
    
    ksh_render_line(0);
    
    return TRUE;
}

void ksh_fb_release()
{
    DeregisterKeyboardHook(&ksh_kb_hook);
}

void ksh_write(const char* characters)
{
    ksh_push_lines();
    
    size_t cLen = strlen(characters);
    
    for(size_t i = 0; i < cLen; i++)
    {
        if(characters[i] == '\n')
        {
            ksh_push_lines();
        }
        else
        {
            char* line = ksh_get_current_type_line();
            line[cursorColumn] = characters[i];
            cursorColumn++;
        }
    }
    
    char* line = ksh_get_current_type_line();
    
    line[cursorColumn] = '\0';
}

void ksh_write_line(const char* line)
{
    ksh_write(line);
    ksh_write("");
}

void ksh_update()
{
    
}

void ksh_render_line(int nb)
{
    if(nb >= KSH_LINES_IN_MEMORY && lines[nb] == NULL)
        return;
    
    int screenRow = ksh_get_line_number_from_bottom(nb);
    
    if(screenRow < 0)
        return;
    
    fbMoveCursor(0, screenRow);
    
    if(ksh_is_current_type_line(nb))
    {
        // Draw prompt
        fbPutString(promptText);
    }
    
    fbPutString(lines[nb]);
}

void ksh_process_command(char* commandline)
{
    if(strcmp(commandline, "") == 0)
    {
        return;
    }
    
    size_t nb = 0;
    char** parts = strspl(commandline, " ", &nb);
    
    BOOL success = FALSE;
    
    if(nb > 0)
    {
        if(strcmp(parts[0], "echo") == 0)
        {
            if(nb > 1)
            {
                for(size_t i = 1; i < nb; i++)
                {
                    ksh_write(parts[i]);
                }
                
                success = TRUE;
            }
        }
        else if(strcmp(parts[0], "dwp") == 0)
        {
            // dwp 512 255 1500
            if(nb > 3)
            {
                char* bytesAmount = parts[1];
                char* pattern = parts[2];
                char* address = parts[3];
                
                uint32_t amountAsDWord = (uint32_t)s_to_d(bytesAmount);
                uint8_t patternAsByte = (uint8_t)s_to_d(pattern);
                int addressAsQWord = (int)s_to_d(address);
                
                uint8_t* writeData = (uint8_t*)malloc(amountAsDWord * sizeof(uint8_t));
                
                Debugger();
                array_set_pattern(writeData, &patternAsByte, amountAsDWord, 1);
                
                write_data(writeData, amountAsDWord, addressAsQWord);
                
                success = TRUE;
            }
        }
        else if(strcmp(parts[0], "fread") == 0)
        {
            if(nb > 1)
            {
                char* fileName = parts[1];
                
                file_h file = ezfs_find_file(fileName);
                if(file != FILE_NOT_FOUND)
                {
                    uint8_t* buf = NULL;
                    size_t bytesRead = ezfs_read_file(file, &buf);
                    
                    char* bufToStr = (char*)malloc(sizeof(char) * bytesRead + 1);
{'fraction_non_alphanumeric': 0.12375156054931336, 'fraction_numerical': 0.011860174781523096, 'mean_word_length': 1.6, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3147696', 'n_tokens_mistral': 2278, 'n_tokens_neox': 2198, 'n_words': 365}