summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRaymaekers Luca <luca@spacehb.net>2025-11-03 23:25:05 +0100
committerRaymaekers Luca <luca@spacehb.net>2025-11-03 23:25:05 +0100
commit4eb0e6ca3adea9f6139299316b0fac1f1369bd8e (patch)
treebb75b8c92c7027ceebd0486241e29640c0a08059
checkpoint
-rwxr-xr-xinstall_lib.sh17
-rwxr-xr-xinstall_system.sh13
-rw-r--r--lr.h10
-rw-r--r--lr_linux.h177
-rw-r--r--lr_macros.h52
-rw-r--r--lr_types.h44
6 files changed, 313 insertions, 0 deletions
diff --git a/install_lib.sh b/install_lib.sh
new file mode 100755
index 0000000..cf3fda3
--- /dev/null
+++ b/install_lib.sh
@@ -0,0 +1,17 @@
+#!/bin/sh
+
+CurrentDir="$(readlink -f ".")"
+
+ScriptDir="$(dirname "$(readlink -f "$0")")"
+cd "$ScriptDir"
+
+Dir="$CurrentDir/${ScriptDir##*/}"
+rm -rf "$Dir"
+mkdir "$Dir"
+
+Files="$(find "$ScriptDir" -type f \( -iname '*.cpp' -o -iname '*.c' -o -iname '*.h' \))"
+
+for File in $Files
+do
+ ln -sf "$File" "$Dir"
+done
diff --git a/install_system.sh b/install_system.sh
new file mode 100755
index 0000000..c3a1196
--- /dev/null
+++ b/install_system.sh
@@ -0,0 +1,13 @@
+#!/bin/sh
+
+set -ue
+
+ThisDir="$(dirname "$(readlink -f "$0")")"
+cd "$ThisDir"
+
+Files="$(find . -name '*.h')"
+
+Dest="/usr/local/include/lr/"
+mkdir -p "$Dest"
+
+cp -v $Files "$Dest"
diff --git a/lr.h b/lr.h
new file mode 100644
index 0000000..fda0b76
--- /dev/null
+++ b/lr.h
@@ -0,0 +1,10 @@
+/* Luca Raymaekers' standard library */
+
+#ifndef LR_H
+#define LR_H
+
+#include "lr_macros.h"
+#include "lr_types.h"
+#include "lr_platform.h"
+
+#endif // LR_H
diff --git a/lr_linux.h b/lr_linux.h
new file mode 100644
index 0000000..9d6545f
--- /dev/null
+++ b/lr_linux.h
@@ -0,0 +1,177 @@
+#ifndef LR_PLATFORM_H
+#define LR_PLATFORM_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/wait.h>
+
+#define LR_COMMAND_OUTPUT_BUFFER_SIZE 2048
+int RunCommandAndGetOutput(char *OutputBuffer, char *Command[])
+{
+ int Result = 0;
+
+ int HandlesLink[2] = {};
+ int WaitStatus = 0;
+ pid_t ChildPID = 0;
+ int Ret = 0;
+
+ char *FilePath = Command[0];
+ int AccessMode = F_OK | X_OK;
+ Ret = access(FilePath, AccessMode);
+
+ if(Ret == 0)
+ {
+ Ret = pipe(HandlesLink);
+ if(Ret != -1)
+ {
+ ChildPID = fork();
+ if(ChildPID != -1)
+ {
+ if(ChildPID == 0)
+ {
+ dup2(HandlesLink[1], STDOUT_FILENO);
+ execve(Command[0], Command, 0);
+ }
+ else
+ {
+ wait(&WaitStatus);
+
+ Result = read(HandlesLink[0], OutputBuffer, LR_COMMAND_OUTPUT_BUFFER_SIZE);
+ if(Result == -1)
+ {
+ Result = 0;
+ }
+ }
+
+ }
+ else
+ {
+ // TODO: Logging
+ }
+ }
+ else
+ {
+ // TODO: Logging
+ }
+ }
+ else
+ {
+ // TODO: Logging
+ }
+
+ return Result;
+}
+
+void RunCommand(char *Command[])
+{
+ int WaitStatus = 0;
+
+ pid_t ChildPid = fork();
+ if(ChildPid != -1)
+ {
+ if(ChildPid == 0)
+ {
+ execve(Command[0], Command, 0);
+ }
+ else
+ {
+ wait(&WaitStatus);
+ }
+ }
+ else
+ {
+ // TODO: Logging
+ }
+}
+
+void RunCommandInPATH(char *Command[])
+{
+#define NameBufferSize 1024
+ char NameBuffer[NameBufferSize] = {0};
+ char *ExecutableFound = 0;
+ char **VarAt = __environ;
+ char Search[] = "PATH=";
+ int MatchedSearch = false;
+
+ while(*VarAt && !MatchedSearch)
+ {
+ MatchedSearch = true;
+
+ for(unsigned int At = 0;
+ (At < ArrayCount(Search)) && (VarAt[At]);
+ At++)
+ {
+ if(Search[At] != VarAt[0][At])
+ {
+ MatchedSearch = false;
+ }
+ }
+
+ VarAt++;
+ }
+
+ if(MatchedSearch)
+ {
+ VarAt--;
+ char *Scan = VarAt[0];
+ while(*Scan && *Scan != '=') Scan++;
+ Scan++;
+
+ while((!ExecutableFound) &&
+ (*Scan) && (Scan != VarAt[1]))
+ {
+ int Len = 0;
+ while(Scan[Len] && Scan[Len] != ':' &&
+ (Scan+Len != VarAt[1])) Len++;
+
+ for(unsigned int At = 0; At < NameBufferSize; At++)
+ {
+ NameBuffer[At] = 0;
+ }
+ int At;
+ for(At = 0; At < Len; At++)
+ {
+ NameBuffer[At] = Scan[At];
+ }
+ NameBuffer[At++] = '/';
+
+ for(char *CharAt = Command[0];
+ *CharAt;
+ CharAt++)
+ {
+ NameBuffer[At++] = *CharAt;
+ }
+ NameBuffer[At] = 0;
+
+ int AccessMode = F_OK | X_OK;
+ int Ret = access(NameBuffer, AccessMode);
+ if(Ret == 0)
+ {
+ ExecutableFound = (char *)NameBuffer;
+ }
+
+ Scan += Len + 1;
+ }
+
+ if(ExecutableFound)
+ {
+ Command[0] = NameBuffer;
+ RunCommand(Command);
+ }
+ }
+}
+
+#define RunSimpleCommandAndGetOutput(Buffer, ...) \
+RunCommandAndGetOutput((Buffer), (char *[]){__VA_ARGS__, 0})
+#define RunSimpleCommand(...) RunCommand((char *[]){__VA_ARGS__, 0})
+#define RunSimpleCommandInPATH(...) RunCommandInPATH((char *[]){__VA_ARGS__, 0})
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif //LR_PLATFORM_H
diff --git a/lr_macros.h b/lr_macros.h
new file mode 100644
index 0000000..a503ad5
--- /dev/null
+++ b/lr_macros.h
@@ -0,0 +1,52 @@
+#ifndef LR_MACROS_H
+#define LR_MACROS_H
+
+// Zero
+#if !defined(COMPILER_MSVC)
+#define COMPILER_MSVC 0
+#endif
+#if !defined(COMPILER_LLVM)
+#define COMPILER_LLVM 0
+#endif
+#if !defined(COMPILER_GNU)
+#define COMPILER_GNU 0
+#endif
+
+// Detect compiler
+#if __clang__
+# define COMPILER_CLANG 1
+#elif _MSC_VER
+# define COMPILER_MSVC 1
+#elif __GNUC__
+# define COMPILER_GNU 1
+#endif
+
+// Push/Pop warnings
+#if defined(COMPILER_GNU)
+# define PUSH_WARNINGS \
+_Pragma("GCC diagnostic push") \
+_Pragma("GCC diagnostic ignored \"-Weverything\"")
+# define POP_WARNINGS \
+_Pragma("GCC diagnostic pop")
+#elif defined(COMPILER_CLANG)
+# define PUSH_WARNINGS \
+_Pragma("clang diagnostic push") \
+_Pragma("clang diagnostic ignored \"-Weverything\"")
+# define POP_WARNINGS \
+_Pragma("clang diagnostic pop")
+#else
+# define PUSH_WARNINGS \
+_Pragma("message \"No compatible compiler found\"")
+# define POP_WARNINGS
+#endif
+
+#define ArrayCount(Array) (sizeof(Array) / sizeof((Array)[0]))
+
+#define Assert(Expression) if(!(Expression)) { __asm__ volatile("int3"); }
+#define DebugBreakOnce do { local_persist b32 X = false; Assert(X); X = true; } while(0)
+#define NullExpression { int X = 0; }
+
+#define Min(A, B) (((A) < (B)) ? (A) : (B))
+#define Max(A, B) (((A) > (B)) ? (A) : (B))
+
+#endif // LR_MACROS_H \ No newline at end of file
diff --git a/lr_types.h b/lr_types.h
new file mode 100644
index 0000000..6bb3527
--- /dev/null
+++ b/lr_types.h
@@ -0,0 +1,44 @@
+#ifndef LR_TYPES_H
+#define LR_TYPES_H
+
+#include <stdint.h>
+#include <stddef.h>
+
+#define internal static
+#define local_persist static
+#define global_variable static
+
+#define Pi32 3.14159265359f
+
+#define Kilobytes(Value) ((Value)*1024LL)
+#define Megabytes(Value) (Kilobytes(Value)*1024LL)
+#define Gigabytes(Value) (Megabytes(Value)*1024LL)
+#define Terabytes(Value) (Gigabytes(Value)*1024LL)
+
+typedef int8_t s8;
+typedef int16_t s16;
+typedef int32_t s32;
+typedef int64_t s64;
+typedef s32 b32;
+
+typedef uint8_t u8;
+typedef uint16_t u16;
+typedef uint32_t u32;
+typedef uint64_t u64;
+
+typedef size_t umm;
+typedef ssize_t smm;
+typedef s32 rune; // utf8 codepoint
+
+typedef float f32;
+typedef double f64;
+
+#define U8Max 0xff
+#define U16Max 0xffff
+#define S32Min ((s32)0x80000000)
+#define S32Max ((s32)0x7fffffff)
+#define U32Min 0
+#define U32Max ((u32)-1)
+#define U64Max ((u64)-1)
+
+#endif //LR_TYPES_H