Compare commits
8 commits
main
...
i3-console
| Author | SHA1 | Date | |
|---|---|---|---|
| acee93cf39 | |||
| dad7db99d8 | |||
| 7447d6ab45 | |||
| 20db36cd82 | |||
| 65ea3ab572 | |||
| 17d4c0e0f9 | |||
| 7499404f63 | |||
| 1c5e1e046b |
6 changed files with 240 additions and 32 deletions
|
|
@ -10,12 +10,18 @@ file(GLOB_RECURSE SRC_FILES "${PROJECT_SOURCE_DIR}/Src/*.c")
|
|||
add_library(Iony STATIC ${SRC_FILES})
|
||||
add_executable(IonyTest ${PROJECT_SOURCE_DIR}/Tests/Main.c)
|
||||
|
||||
target_link_libraries(Iony PRIVATE ${HALLOCY_LIB_PATH})
|
||||
target_include_directories(IonyTest PRIVATE ${PROJECT_SOURCE_DIR}/Tests)
|
||||
target_link_libraries(IonyTest Iony)
|
||||
|
||||
if (MSVC)
|
||||
target_compile_options(Iony PRIVATE /W4 /Zl)
|
||||
target_compile_options(Iony PRIVATE /W4 /Zl /GS- /kernel)
|
||||
|
||||
target_link_options(IonyTest PRIVATE /NODEFAULTLIB /ENTRY:_start)
|
||||
|
||||
target_link_libraries(Iony PRIVATE kernel32)
|
||||
target_link_libraries(IonyTest PRIVATE kernel32)
|
||||
else()
|
||||
target_compile_options(Iony PRIVATE -Wall -Wextra -pedantic)
|
||||
target_compile_options(Iony PRIVATE -Wall -Wextra -pedantic -ffreestanding -fno-builtin)
|
||||
target_link_options(Iony PRIVATE -nostdlib)
|
||||
target_link_options(IonyTest PRIVATE -nostdlib)
|
||||
endif()
|
||||
|
|
@ -24,10 +24,33 @@
|
|||
|
||||
#include "../Utils/Error.h"
|
||||
|
||||
IonyError iony_console_print(const char *text, long length);
|
||||
typedef enum {
|
||||
IONY_CONSOLE_COLOR_BLACK = 0,
|
||||
IONY_CONSOLE_COLOR_BLUE = 1,
|
||||
IONY_CONSOLE_COLOR_GREEN = 2,
|
||||
IONY_CONSOLE_COLOR_CYAN = 3,
|
||||
IONY_CONSOLE_COLOR_RED = 4,
|
||||
IONY_CONSOLE_COLOR_MAGENTA = 5,
|
||||
IONY_CONSOLE_COLOR_BROWN = 6,
|
||||
IONY_CONSOLE_COLOR_LIGHT_GRAY = 7,
|
||||
IONY_CONSOLE_COLOR_DARK_GRAY = 8,
|
||||
IONY_CONSOLE_COLOR_LIGHT_BLUE = 9,
|
||||
IONY_CONSOLE_COLOR_LIGHT_GREEN = 10,
|
||||
IONY_CONSOLE_COLOR_LIGHT_CYAN = 11,
|
||||
IONY_CONSOLE_COLOR_LIGHT_RED = 12,
|
||||
IONY_CONSOLE_COLOR_LIGHT_MAGENTA = 13,
|
||||
IONY_CONSOLE_COLOR_YELLOW = 14,
|
||||
IONY_CONSOLE_COLOR_WHITE = 15
|
||||
} IonyConsoleColor;
|
||||
|
||||
IonyError iony_console_print(const char *text, unsigned long length);
|
||||
IonyError iony_console_error(const char *text, unsigned long length);
|
||||
IonyError iony_console_clear(void);
|
||||
|
||||
IonyError iony_console_get_character(char *character);
|
||||
IonyError iony_console_get_line(char *character_string, long max_length, long *characters_read);
|
||||
IonyError iony_console_get_line(char *character_string, long max_length, unsigned long *characters_read);
|
||||
|
||||
IonyError iony_console_set_color(IonyConsoleColor color);
|
||||
IonyError iony_console_reset_color(void);
|
||||
|
||||
#endif
|
||||
|
|
|
|||
32
Include/Iony/Utils/Base.h
Normal file
32
Include/Iony/Utils/Base.h
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
/*
|
||||
* 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 base c types like bool.
|
||||
*
|
||||
* Author: Mineplay
|
||||
* -----------------------------------------------------------------------------
|
||||
*/
|
||||
#ifndef IONY_BOOL
|
||||
#define IONY_BOOL
|
||||
|
||||
#define IONY_NULL ((void*)0)
|
||||
|
||||
typedef enum {
|
||||
IONY_BOOL_FALSE = 0,
|
||||
IONY_BOOL_TRUE = 1
|
||||
} IonyBool;
|
||||
|
||||
#endif
|
||||
|
|
@ -23,19 +23,26 @@
|
|||
#ifndef IONY_SYSTEM
|
||||
#define IONY_SYSTEM
|
||||
|
||||
#if defined(__GNUC__)
|
||||
#if defined(_WIN32)
|
||||
#include <windows.h>
|
||||
#elif defined(__GNUC__)
|
||||
|
||||
#if defined(__x86_64__)
|
||||
#define IONY_SYS_READ 0
|
||||
#define IONY_SYS_WRITE 1
|
||||
#define IONY_SYS_EXIT 60
|
||||
#elif defined(__i386__)
|
||||
#define IONY_SYS_READ 3
|
||||
#define IONY_SYS_WRITE 4
|
||||
#define IONY_SYS_EXIT 1
|
||||
#elif defined(__aarch64__)
|
||||
#define IONY_SYS_READ 63
|
||||
#define IONY_SYS_WRITE 64
|
||||
#define IONY_SYS_EXIT 93
|
||||
#elif defined(__arm__)
|
||||
#define IONY_SYS_READ 3
|
||||
#define IONY_SYS_WRITE 4
|
||||
#define IONY_SYS_EXIT 1
|
||||
#endif
|
||||
|
||||
static inline long iony_system_call(long number, long argument1, long argument2, long argument3, long argument4, long argument5, long argument6) {
|
||||
|
|
@ -98,6 +105,16 @@ static inline long iony_system_call(long number, long argument1, long argument2,
|
|||
|
||||
return result;
|
||||
}
|
||||
#else
|
||||
#error "Unsupported architecture"
|
||||
#endif
|
||||
|
||||
static inline void iony_exit_process(int exit_code) {
|
||||
#if defined(_WIN32)
|
||||
ExitProcess(exit_code);
|
||||
#elif defined(__GNUC__)
|
||||
iony_system_call(IONY_SYS_EXIT, exit_code, 0, 0, 0, 0, 0);
|
||||
#endif
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -20,24 +20,45 @@
|
|||
* -----------------------------------------------------------------------------
|
||||
*/
|
||||
#include "../../Include/Iony/Core/Console.h"
|
||||
#include "Iony/Utils/Error.h"
|
||||
|
||||
#include "../../Include/Iony/Utils/System.h"
|
||||
#include "../../Include/Iony/Utils/Base.h"
|
||||
|
||||
#if defined(_WIN32)
|
||||
#include <windows.h>
|
||||
static HANDLE iony_standard_console_out = IONY_NULL;
|
||||
static HANDLE iony_standard_console_in = IONY_NULL;
|
||||
static HANDLE iony_standard_console_error = IONY_NULL;
|
||||
|
||||
static HANDLE iony_standard_console_out = 0;
|
||||
static HANDLE iony_standard_console_in = 0;
|
||||
static WORD iony_original_console_attributes = 0;
|
||||
#elif defined(__GNUC__)
|
||||
#include "../../Include/Iony/Utils/System.h"
|
||||
static const char IONY_RESET_COLOR[] = "\033[0m";
|
||||
static const char IONY_ANSI_COLORS[][8] = {
|
||||
"\x1B[0;30m",
|
||||
"\x1B[0;34m",
|
||||
"\x1B[0;32m",
|
||||
"\x1B[0;36m",
|
||||
"\x1B[0;31m",
|
||||
"\x1B[0;35m",
|
||||
"\x1B[0;33m",
|
||||
"\x1B[0;37m",
|
||||
"\x1B[1;30m",
|
||||
"\x1B[1;34m",
|
||||
"\x1B[1;32m",
|
||||
"\x1B[1;36m",
|
||||
"\x1B[1;31m",
|
||||
"\x1B[1;35m",
|
||||
"\x1B[1;33m",
|
||||
"\x1B[1;37m",
|
||||
};
|
||||
#endif
|
||||
|
||||
IonyError iony_console_print(const char *text, const long length) {
|
||||
if (text == 0) {
|
||||
IonyError iony_console_print(const char *text, const unsigned long length) {
|
||||
if (text == IONY_NULL) {
|
||||
return IONY_ERROR_INVALID_POINTER;
|
||||
}
|
||||
|
||||
#if defined(_WIN32)
|
||||
if (iony_standard_console_out == 0) {
|
||||
if (iony_standard_console_out == IONY_NULL) {
|
||||
iony_standard_console_out = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||
if (iony_standard_console_out == INVALID_HANDLE_VALUE) {
|
||||
return IONY_ERROR_HANDLE_NOT_FOUND;
|
||||
|
|
@ -49,7 +70,33 @@ IonyError iony_console_print(const char *text, const long length) {
|
|||
return IONY_ERROR_SYSTEM_CALL_FAILED;
|
||||
}
|
||||
#elif defined(__GNUC__)
|
||||
if (iony_system_call(IONY_SYS_WRITE, 1, (long)text, length, 0, 0, 0) < 0) {
|
||||
if (iony_system_call(IONY_SYS_WRITE, 1, (long)text, (long)length, 0, 0, 0) < 0) {
|
||||
return IONY_ERROR_SYSTEM_CALL_FAILED;
|
||||
}
|
||||
#endif
|
||||
|
||||
return IONY_ERROR_NONE;
|
||||
}
|
||||
|
||||
IonyError iony_console_error(const char *text, unsigned long length) {
|
||||
if (text == IONY_NULL) {
|
||||
return IONY_ERROR_INVALID_POINTER;
|
||||
}
|
||||
|
||||
#if defined(_WIN32)
|
||||
if (iony_standard_console_error == IONY_NULL) {
|
||||
iony_standard_console_error = GetStdHandle(STD_ERROR_HANDLE);
|
||||
if (iony_standard_console_error == INVALID_HANDLE_VALUE) {
|
||||
return IONY_ERROR_HANDLE_NOT_FOUND;
|
||||
}
|
||||
}
|
||||
|
||||
DWORD characters_written;
|
||||
if (!WriteFile(iony_standard_console_error, text, length, &characters_written, 0)) {
|
||||
return IONY_ERROR_SYSTEM_CALL_FAILED;
|
||||
}
|
||||
#elif defined(__GNUC__)
|
||||
if (iony_system_call(IONY_SYS_WRITE, 2, (long)text, (long)length, 0, 0, 0) < 0) {
|
||||
return IONY_ERROR_SYSTEM_CALL_FAILED;
|
||||
}
|
||||
#endif
|
||||
|
|
@ -59,7 +106,7 @@ IonyError iony_console_print(const char *text, const long length) {
|
|||
|
||||
IonyError iony_console_clear(void) {
|
||||
#if defined(_WIN32)
|
||||
if (iony_standard_console_out == 0) {
|
||||
if (iony_standard_console_out == IONY_NULL) {
|
||||
iony_standard_console_out = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||
if (iony_standard_console_out == INVALID_HANDLE_VALUE) {
|
||||
return IONY_ERROR_HANDLE_NOT_FOUND;
|
||||
|
|
@ -93,12 +140,12 @@ IonyError iony_console_clear(void) {
|
|||
}
|
||||
|
||||
IonyError iony_console_get_character(char *character) {
|
||||
if (character == 0) {
|
||||
if (character == IONY_NULL) {
|
||||
return IONY_ERROR_INVALID_POINTER;
|
||||
}
|
||||
|
||||
#if defined(_WIN32)
|
||||
if (iony_standard_console_in == 0) {
|
||||
if (iony_standard_console_in == IONY_NULL) {
|
||||
iony_standard_console_in = GetStdHandle(STD_INPUT_HANDLE);
|
||||
if (iony_standard_console_in == INVALID_HANDLE_VALUE) {
|
||||
return IONY_ERROR_HANDLE_NOT_FOUND;
|
||||
|
|
@ -109,6 +156,12 @@ IonyError iony_console_get_character(char *character) {
|
|||
if (!ReadFile(iony_standard_console_in, character, 1, &characters_read, 0)) {
|
||||
return IONY_ERROR_SYSTEM_CALL_FAILED;
|
||||
}
|
||||
|
||||
if (*character == '\r') {
|
||||
if (!ReadFile(iony_standard_console_in, character, 1, &characters_read, 0)) {
|
||||
return IONY_ERROR_SYSTEM_CALL_FAILED;
|
||||
}
|
||||
}
|
||||
#elif defined(__GNUC__)
|
||||
if (iony_system_call(IONY_SYS_READ, 0, (long)character, 1, 0, 0, 0) < 0) {
|
||||
return IONY_ERROR_SYSTEM_CALL_FAILED;
|
||||
|
|
@ -118,20 +171,19 @@ IonyError iony_console_get_character(char *character) {
|
|||
return IONY_ERROR_NONE;
|
||||
}
|
||||
|
||||
IonyError iony_console_get_line(char *character_string, const long max_length, long *characters_read) {
|
||||
if (character_string == 0) {
|
||||
IonyError iony_console_get_line(char *character_string, const long max_length, unsigned long *characters_read) {
|
||||
if (character_string == IONY_NULL) {
|
||||
return IONY_ERROR_INVALID_POINTER;
|
||||
}
|
||||
|
||||
#if defined(_WIN32)
|
||||
if (iony_standard_console_in == 0) {
|
||||
if (iony_standard_console_in == IONY_NULL) {
|
||||
iony_standard_console_in = GetStdHandle(STD_INPUT_HANDLE);
|
||||
if (iony_standard_console_in == INVALID_HANDLE_VALUE) {
|
||||
return IONY_ERROR_HANDLE_NOT_FOUND;
|
||||
}
|
||||
}
|
||||
|
||||
DWORD characters_read;
|
||||
if (!ReadFile(iony_standard_console_in, character_string, max_length, characters_read, 0)) {
|
||||
return IONY_ERROR_SYSTEM_CALL_FAILED;
|
||||
}
|
||||
|
|
@ -146,3 +198,63 @@ IonyError iony_console_get_line(char *character_string, const long max_length, l
|
|||
|
||||
return IONY_ERROR_NONE;
|
||||
}
|
||||
|
||||
IonyError iony_console_set_color(IonyConsoleColor color) {
|
||||
#if defined(_WIN32)
|
||||
if (iony_standard_console_out == IONY_NULL) {
|
||||
iony_standard_console_out = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||
if (iony_standard_console_out == INVALID_HANDLE_VALUE) {
|
||||
return IONY_ERROR_HANDLE_NOT_FOUND;
|
||||
}
|
||||
}
|
||||
|
||||
if (iony_original_console_attributes == 0) {
|
||||
CONSOLE_SCREEN_BUFFER_INFO console_info;
|
||||
if (!GetConsoleScreenBufferInfo(iony_standard_console_out, &console_info)) {
|
||||
return IONY_ERROR_SYSTEM_CALL_FAILED;
|
||||
}
|
||||
|
||||
iony_original_console_attributes = console_info.wAttributes;
|
||||
}
|
||||
|
||||
if (SetConsoleTextAttribute(iony_standard_console_out, color) == 0) {
|
||||
return IONY_ERROR_SYSTEM_CALL_FAILED;
|
||||
}
|
||||
#elif defined(__GNUC__)
|
||||
if (iony_system_call(IONY_SYS_WRITE, 1, (long)IONY_ANSI_COLORS[color], sizeof(IONY_ANSI_COLORS[color]), 0, 0, 0) < 0) {
|
||||
return IONY_ERROR_SYSTEM_CALL_FAILED;
|
||||
}
|
||||
#endif
|
||||
|
||||
return IONY_ERROR_NONE;
|
||||
}
|
||||
|
||||
IonyError iony_console_reset_color(void) {
|
||||
#if defined(_WIN32)
|
||||
if (iony_standard_console_out == IONY_NULL) {
|
||||
iony_standard_console_out = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||
if (iony_standard_console_out == INVALID_HANDLE_VALUE) {
|
||||
return IONY_ERROR_HANDLE_NOT_FOUND;
|
||||
}
|
||||
}
|
||||
|
||||
if (iony_original_console_attributes == 0) {
|
||||
CONSOLE_SCREEN_BUFFER_INFO console_info;
|
||||
if (!GetConsoleScreenBufferInfo(iony_standard_console_out, &console_info)) {
|
||||
return IONY_ERROR_SYSTEM_CALL_FAILED;
|
||||
}
|
||||
|
||||
iony_original_console_attributes = console_info.wAttributes;
|
||||
}
|
||||
|
||||
if (SetConsoleTextAttribute(iony_standard_console_out, iony_original_console_attributes) == 0) {
|
||||
return IONY_ERROR_SYSTEM_CALL_FAILED;
|
||||
}
|
||||
#elif defined(__GNUC__)
|
||||
if (iony_system_call(IONY_SYS_WRITE, 1, (long)IONY_RESET_COLOR, sizeof(IONY_RESET_COLOR), 0, 0, 0) < 0) {
|
||||
return IONY_ERROR_SYSTEM_CALL_FAILED;
|
||||
}
|
||||
#endif
|
||||
|
||||
return IONY_ERROR_NONE;
|
||||
}
|
||||
|
|
|
|||
38
Tests/Main.c
38
Tests/Main.c
|
|
@ -19,40 +19,58 @@
|
|||
* Author: Mineplay
|
||||
* -----------------------------------------------------------------------------
|
||||
*/
|
||||
#include <Iony/Utils/System.h>
|
||||
#include <Iony/Utils/Error.h>
|
||||
#include <Iony/Core/Console.h>
|
||||
|
||||
int main(void) {
|
||||
void _start(void) {
|
||||
if (iony_console_print("Press enter to continue...", 27) != IONY_ERROR_NONE) {
|
||||
return -1;
|
||||
iony_console_error("[!] Failed to print to console.\n", 33);
|
||||
iony_exit_process(-1);
|
||||
}
|
||||
|
||||
char character = ' ';
|
||||
if (iony_console_get_character(&character) != IONY_ERROR_NONE) {
|
||||
return -1;
|
||||
iony_console_error("[!] Failed to get character.\n", 30);
|
||||
iony_exit_process(-1);
|
||||
}
|
||||
|
||||
if (iony_console_clear() != IONY_ERROR_NONE) {
|
||||
return -1;
|
||||
iony_console_error("[!] Failed to clear the console.\n", 34);
|
||||
iony_exit_process(-1);
|
||||
}
|
||||
|
||||
if (iony_console_print("Enter your name: ", 18) != IONY_ERROR_NONE) {
|
||||
return -1;
|
||||
iony_console_error("[!] Failed to print to console.\n", 33);
|
||||
iony_exit_process(-1);
|
||||
}
|
||||
|
||||
char name_input[256];
|
||||
long characters_read = 0;
|
||||
unsigned long characters_read = 0;
|
||||
if (iony_console_get_line(name_input, 256, &characters_read) != IONY_ERROR_NONE) {
|
||||
return -1;
|
||||
iony_console_error("[!] Failed to get name from console.\n", 38);
|
||||
iony_exit_process(-1);
|
||||
}
|
||||
|
||||
if (iony_console_set_color(IONY_CONSOLE_COLOR_LIGHT_RED) != IONY_ERROR_NONE) {
|
||||
iony_console_error("[!] Failed to set console color.\n", 34);
|
||||
iony_exit_process(-1);
|
||||
}
|
||||
|
||||
if (iony_console_print("Your name is ", 14) != IONY_ERROR_NONE) {
|
||||
return -1;
|
||||
iony_console_error("[!] Failed to print to console.\n", 33);
|
||||
iony_exit_process(-1);
|
||||
}
|
||||
|
||||
if (iony_console_print(name_input, characters_read) != IONY_ERROR_NONE) {
|
||||
return -1;
|
||||
iony_console_error("[!] Failed to print name to console.\n", 38);
|
||||
iony_exit_process(-1);
|
||||
}
|
||||
|
||||
return 0;
|
||||
if (iony_console_reset_color() != IONY_ERROR_NONE) {
|
||||
iony_console_error("[!] Failed to reset console color.\n", 36);
|
||||
iony_exit_process(-1);
|
||||
}
|
||||
|
||||
iony_exit_process(0);
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue