From 23778b818cce2ff87c83763bcb267e8f10ebf22e Mon Sep 17 00:00:00 2001 From: Mineplay Date: Fri, 17 Oct 2025 12:35:32 -0500 Subject: [PATCH 1/7] feat(console ouput): added enum for error codes --- Include/Iony/Utils/Error.h | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Include/Iony/Utils/Error.h diff --git a/Include/Iony/Utils/Error.h b/Include/Iony/Utils/Error.h new file mode 100644 index 0000000..9fed0c9 --- /dev/null +++ b/Include/Iony/Utils/Error.h @@ -0,0 +1,31 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ----------------------------------------------------------------------------- + * File: Error.h + * Description: + * This file contains the ErrorCode enum that defines the error codes for the + * library. + * + * Author: Mineplay + * ----------------------------------------------------------------------------- + */ + #ifndef IONY_ERROR + #define IONY_ERROR + +typedef enum { + IONY_ERROR_NONE = 0, + IONY_ERROR_INVALID_POINTER = 1, +} FledastyError; + +#endif From bdc3b26f97bb01693c306d906f06001668a2fdb4 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Fri, 17 Oct 2025 12:43:15 -0500 Subject: [PATCH 2/7] feat(console output): added print function --- Include/Iony/Core/Console.h | 29 +++++++++++++++++++++++++++++ Include/Iony/Utils/Error.h | 2 +- Src/Core/Console.c | 36 ++++++++++++++++++++++++++++++++++++ Tests/Main.c | 8 ++++++-- 4 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 Include/Iony/Core/Console.h create mode 100644 Src/Core/Console.c diff --git a/Include/Iony/Core/Console.h b/Include/Iony/Core/Console.h new file mode 100644 index 0000000..246ac68 --- /dev/null +++ b/Include/Iony/Core/Console.h @@ -0,0 +1,29 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ----------------------------------------------------------------------------- + * File: Console.h + * Description: + * This file contains the functions for reading and writting to the console. + * + * Author: Mineplay + * ----------------------------------------------------------------------------- + */ +#ifndef IONY_CONSOLE +#define IONY_CONSOLE + +#include "../Utils/Error.h" + +IonyError iony_print(char *text); + +#endif diff --git a/Include/Iony/Utils/Error.h b/Include/Iony/Utils/Error.h index 9fed0c9..4c45b41 100644 --- a/Include/Iony/Utils/Error.h +++ b/Include/Iony/Utils/Error.h @@ -26,6 +26,6 @@ typedef enum { IONY_ERROR_NONE = 0, IONY_ERROR_INVALID_POINTER = 1, -} FledastyError; +} IonyError; #endif diff --git a/Src/Core/Console.c b/Src/Core/Console.c new file mode 100644 index 0000000..e6088af --- /dev/null +++ b/Src/Core/Console.c @@ -0,0 +1,36 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ----------------------------------------------------------------------------- + * File: Console.c + * Description: + * This file contains the functions for reading and writting to the console. + * + * Author: Mineplay + * ----------------------------------------------------------------------------- + */ +#include "../../Include/Iony/Core/Console.h" + +IonyError iony_print(char *text) { + if (text == ((void*)0)) { + return IONY_ERROR_INVALID_POINTER; + } + + #if defined(_WIN32) + + #elif defined(__linux__) + + #endif + + return IONY_ERROR_NONE; +} diff --git a/Tests/Main.c b/Tests/Main.c index a0ba57d..97f2c4c 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -19,9 +19,13 @@ * Author: Mineplay * ----------------------------------------------------------------------------- */ -#include +#include +#include int main(void) { - printf("Hello, World!\n"); + if (iony_print("Hello, World!\n") != IONY_ERROR_NONE) { + return -1; + } + return 0; } \ No newline at end of file From 2357205470e6b7ff45bda19e445b77eafeebc5e0 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Fri, 17 Oct 2025 13:16:22 -0500 Subject: [PATCH 3/7] feat(console output): implemented printing to console for windows --- Src/Core/Console.c | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/Src/Core/Console.c b/Src/Core/Console.c index e6088af..0f26387 100644 --- a/Src/Core/Console.c +++ b/Src/Core/Console.c @@ -21,15 +21,29 @@ */ #include "../../Include/Iony/Core/Console.h" +#if defined(_WIN32) +#include +#elif defined(__GNUC__) + +#endif + IonyError iony_print(char *text) { - if (text == ((void*)0)) { + if (text == 0) { return IONY_ERROR_INVALID_POINTER; } - #if defined(_WIN32) - - #elif defined(__linux__) + unsigned long text_length = 0; + while (text[text_length] != '\0') { + text_length += 1; + } + #if defined(_WIN32) + static HANDLE standard_console_out_handle = GetStdHandle(STD_OUTPUT_HANDLE); + + DWORD characters_written; + WriteFile(standard_console_out_handle, text, text_length, &characters_written, 0); + #elif defined(__GNUC__) + #endif return IONY_ERROR_NONE; From a627d5ead559c7985ce01e591858b158636b3071 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Fri, 17 Oct 2025 13:46:23 -0500 Subject: [PATCH 4/7] feat(console output): implemented printing for linux using syscall --- Include/Iony/Utils/System.h | 99 +++++++++++++++++++++++++++++++++++++ Src/Core/Console.c | 6 +-- 2 files changed, 102 insertions(+), 3 deletions(-) create mode 100644 Include/Iony/Utils/System.h diff --git a/Include/Iony/Utils/System.h b/Include/Iony/Utils/System.h new file mode 100644 index 0000000..c462428 --- /dev/null +++ b/Include/Iony/Utils/System.h @@ -0,0 +1,99 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ----------------------------------------------------------------------------- + * File: System.h + * Description: + * This file contains specific system helper functions and constants for linux + * and windows. + * + * Author: Mineplay + * ----------------------------------------------------------------------------- + */ +#ifndef IONY_SYSTEM +#define IONY_SYSTEM + +#if defined(__GNUC__) +#if defined(__x86_64__) +#define SYS_WRITE 1 +#elif defined(__i386__) +#define SYS_WRITE 4 +#elif defined(__aarch64__) +#define SYS_WRITE 64 +#elif defined(__arm__) +#define SYS_WRITE 4 +#endif + +static inline long iony_system_call(long number, long argument1, long argument2, long argument3, long argument4, long argument5, long argument6) { + long result = 0; + #if defined(__x86_64__) + asm volatile( + "movq %1, %%rax\n" + "movq %2, %%rdi\n" + "movq %3, %%rsi\n" + "movq %4, %%rdx\n" + "movq %5, %%r10\n" + "movq %6, %%r8\n" + "movq %7, %%r9\n" + "syscall\n" + "movq %%rax, %0\n" + : "=r"(result) + : "r"(number), "r"(argument1), "r"(argument2), "r"(argument3), "r"(argument4), "r"(argument5), "r"(argument6) + : "rax", "rdi", "rsi", "rdx", "r10", "r8", "r9", "memory" + ); + #elif defined(__i386__) + asm volatile( + "movl %1, %%eax\n" + "movl %2, %%ebx\n" + "movl %3, %%ecx\n" + "movl %4, %%edx\n" + "movl %5, %%esi\n" + "movl %6, %%edi\n" + "movl %7, %%ebp\n" + "int $0x80\n" + "movl %%eax, %0\n" + : "=r"(result) + : "r"(number), "r"(argument1), "r"(argument2), "r"(argument3),"r"(argument4), "r"(argument5), "r"(argument6) + : "eax","ebx","ecx","edx","esi","edi","ebp","memory" + ); + #elif defined(__aarch64__) + register long x0 asm("x0") = argument1; + register long x1 asm("x1") = argument2; + register long x2 asm("x2") = argument3; + register long x3 asm("x3") = argument4; + register long x4 asm("x4") = argument5; + register long x5 asm("x5") = argument6; + register long x8 asm("x8") = number; + + asm volatile("svc 0" : "+r"(x0) : "r"(x1), "r"(x2), "r"(x3), "r"(x4), "r"(x5), "r"(x8) : "memory"); + result = x0; + #elif defined(__arm__) + register long r0 asm("r0") = argument1; + register long r1 asm("r1") = argument2; + register long r2 asm("r2") = argument3; + register long r3 asm("r3") = argument4; + register long r4 asm("r4") = argument5; + register long r5 asm("r5") = argument6; + register long r7 asm("r7") = number; + + asm volatile("swi 0" : "+r"(r0) : "r"(r1), "r"(r2), "r"(r3), "r"(r4), "r"(r5), "r"(r7) : "memory"); + result = r0; + #else + #error "Unsupported architecture" + #endif + + return result; +} +#endif + +#endif diff --git a/Src/Core/Console.c b/Src/Core/Console.c index 0f26387..9e43e1d 100644 --- a/Src/Core/Console.c +++ b/Src/Core/Console.c @@ -24,7 +24,7 @@ #if defined(_WIN32) #include #elif defined(__GNUC__) - +#include "../../Include/Iony/Utils/System.h" #endif IonyError iony_print(char *text) { @@ -32,7 +32,7 @@ IonyError iony_print(char *text) { return IONY_ERROR_INVALID_POINTER; } - unsigned long text_length = 0; + long text_length = 0; while (text[text_length] != '\0') { text_length += 1; } @@ -43,7 +43,7 @@ IonyError iony_print(char *text) { DWORD characters_written; WriteFile(standard_console_out_handle, text, text_length, &characters_written, 0); #elif defined(__GNUC__) - + iony_system_call(SYS_WRITE, 1, (long)text, text_length, 0, 0, 0); #endif return IONY_ERROR_NONE; From 39eb052853ea9c6eb71ea70499e86457a4936d39 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Fri, 17 Oct 2025 14:06:53 -0500 Subject: [PATCH 5/7] feat(console output): added error handling to print function --- Include/Iony/Core/Console.h | 2 +- Include/Iony/Utils/Error.h | 1 + Src/Core/Console.c | 15 +++++++-------- Tests/Main.c | 2 +- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Include/Iony/Core/Console.h b/Include/Iony/Core/Console.h index 246ac68..73d07d8 100644 --- a/Include/Iony/Core/Console.h +++ b/Include/Iony/Core/Console.h @@ -24,6 +24,6 @@ #include "../Utils/Error.h" -IonyError iony_print(char *text); +IonyError iony_print(char *text, long length); #endif diff --git a/Include/Iony/Utils/Error.h b/Include/Iony/Utils/Error.h index 4c45b41..e5e4d2a 100644 --- a/Include/Iony/Utils/Error.h +++ b/Include/Iony/Utils/Error.h @@ -26,6 +26,7 @@ typedef enum { IONY_ERROR_NONE = 0, IONY_ERROR_INVALID_POINTER = 1, + IONY_ERROR_SYSTEM_CALL_FAILED = 2 } IonyError; #endif diff --git a/Src/Core/Console.c b/Src/Core/Console.c index 9e43e1d..fe469d7 100644 --- a/Src/Core/Console.c +++ b/Src/Core/Console.c @@ -27,23 +27,22 @@ #include "../../Include/Iony/Utils/System.h" #endif -IonyError iony_print(char *text) { +IonyError iony_print(char *text, long length) { if (text == 0) { return IONY_ERROR_INVALID_POINTER; } - long text_length = 0; - while (text[text_length] != '\0') { - text_length += 1; - } - #if defined(_WIN32) static HANDLE standard_console_out_handle = GetStdHandle(STD_OUTPUT_HANDLE); DWORD characters_written; - WriteFile(standard_console_out_handle, text, text_length, &characters_written, 0); + if (!WriteFile(standard_console_out_handle, text, length, &characters_written, 0)) { + return IONY_ERROR_SYSTEM_CALL_FAILED; + } #elif defined(__GNUC__) - iony_system_call(SYS_WRITE, 1, (long)text, text_length, 0, 0, 0); + if (iony_system_call(SYS_WRITE, 1, (long)text, length, 0, 0, 0) < 0) { + return IONY_ERROR_SYSTEM_CALL_FAILED; + } #endif return IONY_ERROR_NONE; diff --git a/Tests/Main.c b/Tests/Main.c index 97f2c4c..357c7c9 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -23,7 +23,7 @@ #include int main(void) { - if (iony_print("Hello, World!\n") != IONY_ERROR_NONE) { + if (iony_print("Hello, World!\n", 15) != IONY_ERROR_NONE) { return -1; } From c9ff202bc38a20b15ca00c7a7b48bfd9f63f4fd0 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Fri, 17 Oct 2025 15:18:02 -0500 Subject: [PATCH 6/7] fix(console output): fixed windows console output handle initialization --- Include/Iony/Core/Console.h | 2 +- Src/Core/Console.c | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/Include/Iony/Core/Console.h b/Include/Iony/Core/Console.h index 73d07d8..d52719b 100644 --- a/Include/Iony/Core/Console.h +++ b/Include/Iony/Core/Console.h @@ -24,6 +24,6 @@ #include "../Utils/Error.h" -IonyError iony_print(char *text, long length); +IonyError iony_print(const char *text, long length); #endif diff --git a/Src/Core/Console.c b/Src/Core/Console.c index fe469d7..a4b351e 100644 --- a/Src/Core/Console.c +++ b/Src/Core/Console.c @@ -27,14 +27,17 @@ #include "../../Include/Iony/Utils/System.h" #endif -IonyError iony_print(char *text, long length) { +IonyError iony_print(const char *text, const long length) { if (text == 0) { return IONY_ERROR_INVALID_POINTER; } #if defined(_WIN32) - static HANDLE standard_console_out_handle = GetStdHandle(STD_OUTPUT_HANDLE); - + static HANDLE standard_console_out_handle = 0; + if (standard_console_out_handle == 0) { + standard_console_out_handle = GetStdHandle(STD_OUTPUT_HANDLE); + } + DWORD characters_written; if (!WriteFile(standard_console_out_handle, text, length, &characters_written, 0)) { return IONY_ERROR_SYSTEM_CALL_FAILED; From 09791a9d31a23e8131a378fc18f2cb34cd7f5d5e Mon Sep 17 00:00:00 2001 From: Mineplay Date: Fri, 17 Oct 2025 15:25:57 -0500 Subject: [PATCH 7/7] refactor(console output): changed asm to __asm__ in system call function --- Include/Iony/Utils/System.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Include/Iony/Utils/System.h b/Include/Iony/Utils/System.h index c462428..71c8a66 100644 --- a/Include/Iony/Utils/System.h +++ b/Include/Iony/Utils/System.h @@ -37,7 +37,7 @@ static inline long iony_system_call(long number, long argument1, long argument2, long argument3, long argument4, long argument5, long argument6) { long result = 0; #if defined(__x86_64__) - asm volatile( + __asm__ volatile( "movq %1, %%rax\n" "movq %2, %%rdi\n" "movq %3, %%rsi\n" @@ -52,7 +52,7 @@ static inline long iony_system_call(long number, long argument1, long argument2, : "rax", "rdi", "rsi", "rdx", "r10", "r8", "r9", "memory" ); #elif defined(__i386__) - asm volatile( + __asm__ volatile( "movl %1, %%eax\n" "movl %2, %%ebx\n" "movl %3, %%ecx\n" @@ -75,7 +75,7 @@ static inline long iony_system_call(long number, long argument1, long argument2, register long x5 asm("x5") = argument6; register long x8 asm("x8") = number; - asm volatile("svc 0" : "+r"(x0) : "r"(x1), "r"(x2), "r"(x3), "r"(x4), "r"(x5), "r"(x8) : "memory"); + __asm__ volatile("svc 0" : "+r"(x0) : "r"(x1), "r"(x2), "r"(x3), "r"(x4), "r"(x5), "r"(x8) : "memory"); result = x0; #elif defined(__arm__) register long r0 asm("r0") = argument1; @@ -86,7 +86,7 @@ static inline long iony_system_call(long number, long argument1, long argument2, register long r5 asm("r5") = argument6; register long r7 asm("r7") = number; - asm volatile("swi 0" : "+r"(r0) : "r"(r1), "r"(r2), "r"(r3), "r"(r4), "r"(r5), "r"(r7) : "memory"); + __asm__ volatile("swi 0" : "+r"(r0) : "r"(r1), "r"(r2), "r"(r3), "r"(r4), "r"(r5), "r"(r7) : "memory"); result = r0; #else #error "Unsupported architecture"