From a7bd9c2cc623ec47eaec1e1e8a155997644fb443 Mon Sep 17 00:00:00 2001 From: servostar Date: Mon, 3 Jun 2024 18:35:23 +0200 Subject: [PATCH] added os module --- lib/CMakeLists.txt | 3 +++ lib/src/capi.h | 14 ++++++++++++++ lib/src/def.gem | 2 +- lib/src/def/api.h | 3 ++- lib/src/io/impl.c | 5 +++-- lib/src/mem/impl.c | 5 +++-- lib/src/os.gem | 25 +++++++++++++++++++++++++ lib/src/os/api.h | 18 ++++++++++++++++++ lib/src/os/impl.c | 36 ++++++++++++++++++++++++++++++++++++ 9 files changed, 105 insertions(+), 6 deletions(-) create mode 100644 lib/src/capi.h create mode 100644 lib/src/os.gem create mode 100644 lib/src/os/api.h create mode 100644 lib/src/os/impl.c diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index ffd62ff..6bc0d38 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -20,3 +20,6 @@ add_library(io ${STDLIB_IO_SOURCE_FILES}) file(GLOB_RECURSE STDLIB_MEM_SOURCE_FILES src/mem/*.c) add_library(mem ${STDLIB_MEM_SOURCE_FILES}) + +file(GLOB_RECURSE STDLIB_OS_SOURCE_FILES src/os/*.c) +add_library(os ${STDLIB_OS_SOURCE_FILES}) \ No newline at end of file diff --git a/lib/src/capi.h b/lib/src/capi.h new file mode 100644 index 0000000..0ef22ab --- /dev/null +++ b/lib/src/capi.h @@ -0,0 +1,14 @@ +// +// Created by servostar on 6/3/24. +// + +#ifndef GEMSTONE_STD_LIB_CAPI_H +#define GEMSTONE_STD_LIB_CAPI_H + +#if defined(_WIN32) || defined (_WIN64) +#define PLATFORM_WINDOWS +#elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__linux__) +#define PLATFORM_POSIX +#endif + +#endif // GEMSTONE_STD_LIB_CAPI_H diff --git a/lib/src/def.gem b/lib/src/def.gem index 232d474..5bd344f 100644 --- a/lib/src/def.gem +++ b/lib/src/def.gem @@ -31,7 +31,7 @@ type signed double double float: f128 # String constant -type ref u8: str +type ref u8: cstr # C style void pointer replacement diff --git a/lib/src/def/api.h b/lib/src/def/api.h index 94ad84d..a807865 100644 --- a/lib/src/def/api.h +++ b/lib/src/def/api.h @@ -6,6 +6,7 @@ #define GEMSTONE_STD_LIB_DEF_H_ #include +#include typedef uint8_t u8; typedef uint16_t u16; @@ -20,7 +21,7 @@ typedef int64_t i64; typedef float f32; typedef double f64; -typedef u8* str; +typedef u8* cstr; typedef u8* ptr; diff --git a/lib/src/io/impl.c b/lib/src/io/impl.c index 19fe716..f56c561 100644 --- a/lib/src/io/impl.c +++ b/lib/src/io/impl.c @@ -1,7 +1,8 @@ #include +#include -#if defined(_WIN32) || defined (_WIN64) +#if defined(PLATFORM_WINDOWS) // Compile for Windows @@ -34,7 +35,7 @@ void flush(handle dev) { FlushFileBuffers((HANDLE) dev); } -#elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__linux__) +#elif defined(PLATFORM_POSIX) // Compile for Linux and BSD diff --git a/lib/src/mem/impl.c b/lib/src/mem/impl.c index 95863ac..9d384a8 100644 --- a/lib/src/mem/impl.c +++ b/lib/src/mem/impl.c @@ -1,7 +1,8 @@ #include +#include -#if defined(_WIN32) || defined (_WIN64) +#if defined(PLATFORM_WINDOWS) #include @@ -22,7 +23,7 @@ void heap_free(u8* ptr) { HeapFree(heap, ptr); } -#elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__linux__) +#elif defined(PLATFORM_POSIX) #include diff --git a/lib/src/os.gem b/lib/src/os.gem new file mode 100644 index 0000000..9f51a45 --- /dev/null +++ b/lib/src/os.gem @@ -0,0 +1,25 @@ +# Author: Sven Vogel +# Edited: 03.06.2024 +# License: GPL-2.0 + +# ,----------------------------------------. +# | Operating System | +# `----------------------------------------` + +import "def.gem" + +# Return a hard coded C string identifying the underlying operating system +# Will return one of the following: +# - "windows" (for Windows 7, Windows 10 and Windows 11) +# - "unix" (for GNU/Linux and BSD) +fun getPlatformName(out cstr: name) + +# Return a C string to the value of an environment varible named +# after the C string name. +fun getEnvVar(in cstr: name)(out cstr: value) + +# Set the value of an environment variable with name to value. +fun setEnvVar(in cstr: name, in cstr: value) + +# Unset a specific environment variable +fun unsetEnvVar(in cstr: name) \ No newline at end of file diff --git a/lib/src/os/api.h b/lib/src/os/api.h new file mode 100644 index 0000000..3ed7a1f --- /dev/null +++ b/lib/src/os/api.h @@ -0,0 +1,18 @@ +// +// Created by servostar on 6/3/24. +// + +#ifndef GEMSTONE_STD_LIB_OS_H +#define GEMSTONE_STD_LIB_OS_H + +#include + +void getPlatformName(cstr* name); + +void getEnvVar(cstr name, cstr* value); + +void setEnvVar(cstr name, cstr value); + +void unsetEnvVar(cstr name); + +#endif // GEMSTONE_STD_LIB_OS_H diff --git a/lib/src/os/impl.c b/lib/src/os/impl.c new file mode 100644 index 0000000..7bed097 --- /dev/null +++ b/lib/src/os/impl.c @@ -0,0 +1,36 @@ +// +// Created by servostar on 6/3/24. +// + +#include +#include + +#if defined(PLATFORM_WINDOWS) + +void getPlatformName(cstr* name) { + *name = (u8*) "windows"; +} + +#elif defined(PLATFORM_POSIX) + +void getPlatformName(cstr * name) { + *name = (u8*) "posix"; +} + +#endif + +// Implementation based on libc + +#include + +void getEnvVar(cstr name, cstr* value) { + *value = (cstr) getenv((char*) name); +} + +void setEnvVar(cstr name, cstr value) { + setenv((char*) name, (char*) value, true); +} + +void unsetEnvVar(cstr name) { + unsetenv((char*) name); +}