diff --git a/src/SVM.Advanced/BSDStyleVM/BSDLikeWrapper.cs b/src/SVM.Advanced/BSDStyleVM/BSDLikeWrapper.cs index badb0ef..c6fcb94 100644 --- a/src/SVM.Advanced/BSDStyleVM/BSDLikeWrapper.cs +++ b/src/SVM.Advanced/BSDStyleVM/BSDLikeWrapper.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; +using System.Text; namespace SVM.Advanced.BSDStyleVM { @@ -19,7 +20,15 @@ namespace SVM.Advanced.BSDStyleVM FDs.Add(0, new FileDescripter(Console.OpenStandardInput())); FDs.Add(1, new FileDescripter(Console.OpenStandardOutput())); } - + public int FindAvailableFDID() + { + for (int i = 0; i < int.MaxValue; i++) + { + if (FDs.ContainsKey(i)) continue; + return i; + } + return -1; + } } public class BSDConfig { @@ -38,6 +47,13 @@ namespace SVM.Advanced.BSDStyleVM config.FuncCalls.Add(4, BSDStyleFunctions0.__write); } } + public static class BSDFcntl + { + public const int O_RDONLY = 0x0; + public const int O_WRONLY = 0x01; + public const int O_RDWR = 0x02; + public const int O_ACCMODE = 0x03; + } public static class BSDStyleFunctions0 { public static void __exit(SimpleVirtualMachine machine) @@ -45,7 +61,32 @@ namespace SVM.Advanced.BSDStyleVM var status = machine.registers.GetData(10); Environment.Exit(status); } - public static void __write(SimpleVirtualMachine machine) + public unsafe static void __open(SimpleVirtualMachine machine) + { + if (machine.AdditionalData is BSDLikeWrapper w) + { + var ptr = machine.registers.GetData(10); + var size = machine.registers.GetData(11); + var flag = machine.registers.GetData(12); + var fn = Encoding.UTF8.GetString((byte*)machine.GetPointer(ptr), (int)size); + FileMode fm = default; + FileAccess fa = default; + if ((flag & BSDFcntl.O_WRONLY) == BSDFcntl.O_WRONLY) + { + fa = FileAccess.Write; + } + var fdID = w.FindAvailableFDID(); + if (fdID == -1) return; + var stream = File.Open(fn, fm, fa); + FileDescripter fd = new FileDescripter(stream); + w.FDs.Add(fdID, fd); + } + else + { + Console.WriteLine("Incorrectly set wrapper!"); + } + } + public unsafe static void __write(SimpleVirtualMachine machine) { if (machine.AdditionalData is BSDLikeWrapper w) { diff --git a/src/SVM.Advanced/SVM.Advanced.csproj b/src/SVM.Advanced/SVM.Advanced.csproj index 43ba0d5..e1501ac 100644 --- a/src/SVM.Advanced/SVM.Advanced.csproj +++ b/src/SVM.Advanced/SVM.Advanced.csproj @@ -3,6 +3,7 @@ netstandard2.1 enable + true diff --git a/src/SVM.Assembler.Core/Linker.cs b/src/SVM.Assembler.Core/Linker.cs index 0f1ed5b..97091fd 100644 --- a/src/SVM.Assembler.Core/Linker.cs +++ b/src/SVM.Assembler.Core/Linker.cs @@ -390,6 +390,7 @@ namespace SVM.Assembler.Core } Buffer.BlockCopy(data, 0, data2, sizeof(int), data.Length); context.DataOffsets.Add(item.Key, offset); + Data.Add(data2); } else if (item.Value.StartsWith("file:")) { @@ -404,12 +405,12 @@ namespace SVM.Assembler.Core Buffer.BlockCopy(data, 0, data2, sizeof(int), data.Length); context.DataOffsets.Add(item.Key, offset); offset += (uint)data2.Length; - Data.Add(data); + Data.Add(data2); } else { byte[] data; - string str=Regex.Unescape(item.Value); + string str = Regex.Unescape(item.Value); if (str[0] == '\"' && str[^1] == '\"') { data = Encoding.UTF8.GetBytes(str, 1, str.Length - 2); @@ -417,16 +418,16 @@ namespace SVM.Assembler.Core } else data = Encoding.UTF8.GetBytes(str); - byte[] data2 = new byte[data.Length + sizeof(int)]; + byte[] data2 = new byte[data.Length + sizeof(int) + sizeof(char)]; fixed (byte* ptr = data2) { - int len = data.Length; + int len = data.Length + sizeof(char); ((IntPtr)ptr).SetData(len); } Buffer.BlockCopy(data, 0, data2, sizeof(int), data.Length); context.DataOffsets.Add(item.Key, offset); offset += (uint)data2.Length; - Data.Add(data); + Data.Add(data2); } } foreach (var item in Obj.instructions) diff --git a/src/SVM.Assembler.Core/LinkingContext.cs b/src/SVM.Assembler.Core/LinkingContext.cs index 54a4d60..bb42598 100644 --- a/src/SVM.Assembler.Core/LinkingContext.cs +++ b/src/SVM.Assembler.Core/LinkingContext.cs @@ -20,7 +20,8 @@ namespace SVM.Assembler.Core { if (DataOffsets.TryGetValue(label, out offset)) { - offset += (uint)IntermediateObject.DetermineFinalInstructionCount(this) * (uint)sizeof(SVMInstruction); + //Don't directly give the length there. + offset += (uint)IntermediateObject.DetermineFinalInstructionCount(this) * (uint)sizeof(SVMInstruction)+sizeof(int); return true; } return false;