Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filesystem operations causing "Out of bounds exception" #3109

Open
hubabuba444 opened this issue Dec 5, 2024 · 4 comments
Open

Filesystem operations causing "Out of bounds exception" #3109

hubabuba444 opened this issue Dec 5, 2024 · 4 comments
Labels

Comments

@hubabuba444
Copy link

Area of Cosmos - What area of Cosmos are we dealing with?

Filesystem

Expected Behaviour - What do you think that should happen?

Actual Behaviour - What unexpectedly happens?

Filesystem operations causing Out of bounds exception

Reproduction - How did you get this error to appear?

I'm trying to run Directory.Create and File.WriteAllLines

Version - Were you using the User Kit or Dev Kit? And what User Kit version or Dev Kit commit (Cosmos, IL2CPU, X#)?

I'm using Dev Kit

@hubabuba444 hubabuba444 added the Bug label Dec 5, 2024
@ADev531
Copy link

ADev531 commented Dec 5, 2024

Please show us code, also what commit version is Cosmos

@hubabuba444
Copy link
Author

I'm using devkit latest version
This is the code:
using Cosmos.System.FileSystem;
using System;
using System.Collections.Generic;
using System.IO;

namespace DosOS
{
public class Kernel : Cosmos.System.Kernel
{
private string currentDirectory = @"0:"; // Początkowy folder (root)
private Dictionary<string, string> files = new Dictionary<string, string>(); // Wirtualne pliki
CosmosVFS fs = new Cosmos.System.FileSystem.CosmosVFS();

    protected override void BeforeRun()
    {
        
        Cosmos.System.FileSystem.VFS.VFSManager.RegisterVFS(fs);
        Console.WriteLine("DOSOS System v1.0");
        Console.WriteLine("Type 'help' for a list of commands.");
    }

    protected override void Run()
    {
        Console.Write($"{currentDirectory}> ");
        var input = Console.ReadLine();
        var args = input.Split(' ');

        switch (args[0].ToLower())
        {
            case "cd":
                ChangeDirectory(args);
                break;
            case "edit":
                EditFile(args);
                break;
            case "mkdir":
                MakeDirectory(args);
                break;
            case "cp":
                CopyFile(args);
                break;
            case "basic":
                StartBasic();
                break;
            case "help":
                ShowHelp();
                break;
            default:
                Console.WriteLine("Unknown command. Type 'help' for commands.");
                break;
        }
    }

    private void ChangeDirectory(string[] args)
    {
        if (args.Length < 2)
        {
            Console.WriteLine("Usage: cd [folder]");
            return;
        }

        var targetDir = args[1];
        if (Directory.Exists(Path.Combine(currentDirectory, targetDir)))
        {
            currentDirectory = Path.Combine(currentDirectory, targetDir);
            Console.WriteLine($"Changed to directory {currentDirectory}");
        }
        else
        {
            Console.WriteLine("Directory not found.");
        }
    }

    private void EditFile(string[] args)
    {
        if (args.Length < 2)
        {
            Console.WriteLine("Usage: edit [file]");
            return;
        }

        var fileName = args[1];
        Console.WriteLine($"Editing file {fileName}. Type ':wq' to save and exit.");

        var content = new List<string>();
        while (true)
        {
            var line = Console.ReadLine();
            if (line == ":wq")
            {
                File.WriteAllLines(fileName, content);
                break;
            }
            content.Add(line);
        }
    }

    private void MakeDirectory(string[] args)
    {
        if (args.Length < 2)
        {
            Console.WriteLine("Usage: mkdir [name]");
            return;
        }

        var dirName = args[1];
        var newDir = Path.Combine(currentDirectory, dirName);
        if (!Directory.Exists(newDir))
        {
            Directory.CreateDirectory(newDir);
            Console.WriteLine($"Directory {dirName} created.");
        }
        else
        {
            Console.WriteLine("Directory already exists.");
        }
    }

    private void CopyFile(string[] args)
    {
        if (args.Length < 3)
        {
            Console.WriteLine("Usage: cp [source] [destination]");
            return;
        }

        var source = args[1];
        var destination = args[2];

        if (files.ContainsKey(source))
        {
            files[destination] = files[source];
            Console.WriteLine($"File {source} copied to {destination}.");
        }
        else
        {
            Console.WriteLine("Source file not found.");
        }
    }

    private void StartBasic()
    {
        Console.WriteLine("Starting BASIC interpreter. Type 'EXIT' to quit.");
        BasicInterpreter interpreter = new BasicInterpreter();
        interpreter.Run();
    }

    private void ShowHelp()
    {
        Console.WriteLine("Available commands:");
        Console.WriteLine("cd [folder]     - Change directory");
        Console.WriteLine("edit [file]     - Edit or create a text file");
        Console.WriteLine("mkdir [name]    - Create a new directory");
        Console.WriteLine("cp [src] [dest] - Copy a file");
        Console.WriteLine("basic           - Start BASIC interpreter");
    }
}

public class BasicInterpreter
{
private Dictionary<string, string> variables = new Dictionary<string, string>();
private List program = new List();
private int programCounter = 0;
private bool running = false;

    public void Run()
    {
        Console.WriteLine("BASIC Interpreter v1.0. Type 'EXIT' to quit.");
        while (true)
        {
            if (!running)
            {
                Console.Write("> ");
                var input = Console.ReadLine();
                if (input.Trim().ToUpper() == "EXIT")
                    break;

                ProcessCommand(input);
            }
            else
            {
                if (programCounter < program.Count)
                {
                    ProcessCommand(program[programCounter]);
                    programCounter++;
                }
                else
                {
                    running = false;
                    Console.WriteLine("Program execution finished.");
                }
            }
        }
    }

    private void ProcessCommand(string input)
    {
        var tokens = input.Trim().Split(' ', 2, StringSplitOptions.RemoveEmptyEntries);
        if (tokens.Length == 0) return;

        var command = tokens[0].ToUpper();
        var argument = tokens.Length > 1 ? tokens[1] : string.Empty;

        switch (command)
        {
            case "PRINT":
                Print(argument);
                break;
            case "INPUT":
                Input(argument);
                break;
            case "LET":
                Let(argument);
                break;
            case "IF":
                If(argument);
                break;
            case "GOTO":
                Goto(argument);
                break;
            
            case "END":
                End();
                break;
            case "REM":
                Rem();
                break;
            case "CLS":
                Cls();
                break;
            case "LIST":
                ListProgram();
                break;
            case "RUN":
                RunProgram();
                break;
            case "NEW":
                NewProgram();
                break;
                
            case "STOP":
                Stop();
                break;
            case "FOR":
                For(argument);
                break;
            case "NEXT":
                Next(argument);
                break;
            case "DIM":
                Dim(argument);
                break;
            case "READ":
                Read(argument);
                break;
            case "SAVE":
                SaveProgram(argument);
                break;
            case "LOAD":
                LoadProgram(argument);
                break;
            case "RETURN":
                Return();
                break;
            default:
                Console.WriteLine($"Unknown BASIC command: {command}");
                break;
        }
    }

    private void Print(string argument)
    {
        if (variables.ContainsKey(argument))
            Console.WriteLine(variables[argument]);
        else
            Console.WriteLine(argument);
    }

    private void Input(string argument)
    {
        Console.Write("Input: ");
        variables[argument] = Console.ReadLine();
    }

    private void Let(string argument)
    {
        var parts = argument.Split('=', 2, StringSplitOptions.RemoveEmptyEntries);
        if (parts.Length == 2)
        {
            variables[parts[0].Trim()] = parts[1].Trim();
        }
        else
        {
            Console.WriteLine("Usage: LET variable = value");
        }
    }

    private void If(string argument)
    {
        var parts = argument.Split("THEN", 2, StringSplitOptions.RemoveEmptyEntries);
        if (parts.Length == 2)
        {
            var condition = parts[0].Trim();
            var thenCommand = parts[1].Trim();
            if (EvaluateCondition(condition))
            {
                ProcessCommand(thenCommand);
            }
        }
        else
        {
            Console.WriteLine("Usage: IF condition THEN command");
        }
    }

    private void Goto(string argument)
    {
        if (int.TryParse(argument, out int lineNumber))
        {
            programCounter = program.FindIndex(line => line.StartsWith(lineNumber.ToString()));
            if (programCounter == -1)
                Console.WriteLine($"Line {lineNumber} not found.");
        }
        else
        {
            Console.WriteLine("Usage: GOTO lineNumber");
        }
    }

    private void For(string argument)
    {
        Console.WriteLine("FOR loops not yet implemented.");
    }

    private void Next(string argument)
    {
        Console.WriteLine("NEXT loops not yet implemented.");
    }

    private void End()
    {
        running = false;
    }

    private void Rem()
    {
        // REM (comment) - does nothing
    }

    private void Cls()
    {
        Console.Clear();
    }

    private void ListProgram()
    {
        foreach (var line in program)
        {
            Console.WriteLine(line);
        }
    }

    private void RunProgram()
    {
        running = true;
        programCounter = 0;
    }

    private void NewProgram()
    {
        program.Clear();
        Console.WriteLine("Program cleared.");
    }

    private void SaveProgram(string filename)
    {
        Console.WriteLine($"Saving program to {filename} (not implemented).");
    }

    private void LoadProgram(string filename)
    {
        Console.WriteLine($"Loading program from {filename} (not implemented).");
    }

    private void Dim(string argument)
    {
        Console.WriteLine("DIM arrays not yet implemented.");
    }

    private void Read(string argument)
    {
        Console.WriteLine("READ/DATA not yet implemented.");
    }

    private void Data(string argument)
    {
        Console.WriteLine("READ/DATA not yet implemented.");
    }

    private void Return()
    {
        Console.WriteLine("RETURN not yet implemented.");
    }

    private void Stop()
    {
        running = false;
        Console.WriteLine("Program stopped.");
    }

    private bool EvaluateCondition(string condition)
    {
        // Simple condition evaluator for demonstration
        return condition.Contains("TRUE");
    }
}

}

@ADev531
Copy link

ADev531 commented Dec 8, 2024

ah
you forgot to Path.Combine() the file and current path

@hubabuba444
Copy link
Author

if you are talking about the editfile function it probally not from this
if i run directory.create() for example the same is happend

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants