From 5d9920868c4fecaab21423cd1f1d190a9cbc0178 Mon Sep 17 00:00:00 2001 From: Randy Jordan Date: Fri, 6 Mar 2026 12:04:53 -0600 Subject: [PATCH] first commit --- LICENSE | 21 ++++++++ Makefile | 110 +++++++++++++++++++++++++++++++++++++++++ README.md | 34 +++++++++++++ include/except.h | 107 +++++++++++++++++++++++++++++++++++++++ include/platform.h | 6 +++ src/except.c | 82 ++++++++++++++++++++++++++++++ src/linux_platform.c | 64 ++++++++++++++++++++++++ src/windows_platform.c | 57 +++++++++++++++++++++ tests/01_platform.c | 6 +++ 9 files changed, 487 insertions(+) create mode 100644 LICENSE create mode 100644 Makefile create mode 100644 README.md create mode 100644 include/except.h create mode 100644 include/platform.h create mode 100644 src/except.c create mode 100644 src/linux_platform.c create mode 100644 src/windows_platform.c create mode 100644 tests/01_platform.c diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..8aa2645 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) [year] [fullname] + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..0d80db7 --- /dev/null +++ b/Makefile @@ -0,0 +1,110 @@ +# Compiler Flags +CC := gcc +CFLAGS := -g -Wall -Wextra -Werror -pedantic -fno-omit-frame-pointer + +# Directory Variables +LIBDIR := lib +OBJ := obj +INC := include +SRC := src +TEST := tests + +# Platform Detection and Library Linking +ifeq ($(OS), Windows_NT) + PLATFORM := win32 + PLATFORM_SRC := $(SRC)/windows_platform.c + PLATFORM_OBJ := $(OBJ)/windows_platform.o + PLATFORM_FLAGS := -D WIN32 + PLATFORM_LIBS := +else + UNAME_S := $(shell uname -s) + ifeq ($(UNAME_S), Linux) + PLATFORM := linux + PLATFORM_SRC := $(SRC)/linux_platform.c + PLATFORM_OBJ := $(OBJ)/linux_platform.o + PLATFORM_FLAGS := -D LINUX + PLATFORM_LIBS := -lX11 + else ifeq ($(UNAME_S), Darwin) + PLATFORM := macos + PLATFORM_SRC := $(SRC)/macos_platform.c + PLATFORM_OBJ := $(OBJ)/macos_platform.o + PLATFORM_FLAGS := -D MACOS + PLATFORM_LIBS := + else + $(error Unsupported platform: $(UNAME_S)) + endif +endif + + +CFLAGS += $(PLATFORM_FLAGS) +# Filepath Pattern Matching +LIB := $(LIBDIR)/lib.a +# Exclude platform files from wildcard, add the correct one back in +SRCS := $(filter-out $(SRC)/windows_platform.c $(SRC)/linux_platform.c $(SRC)/macos_platform.c, \ + $(wildcard $(SRC)/*.c)) $(PLATFORM_SRC) +OBJS := $(patsubst $(SRC)/%.c, $(OBJ)/%.o, $(SRCS)) +TESTS := $(wildcard $(TEST)/*.c) +TESTBINS := $(patsubst $(TEST)/%.c, $(TEST)/bin/%, $(TESTS)) + + +# Commands must be labeled PHONY +.PHONY: all release clean test +# Compiler Release Flags +release: CFLAGS := -Wall -Wextra -Werror -pedantic -fsanitize=address,undefined -fno-omit-frame-pointer -O2 -DNDEBUG $(PLATFORM_FLAGS) +release: clean $(LIB) + + +# Target for compilation. +all: $(LIB) + @echo "Built for platform: $(PLATFORM)" +# Target / Dependencies +$(LIB): $(OBJS) | $(LIBDIR) + $(RM) $(LIB) + ar -cvrs $@ $^ +$(OBJ)/%.o: $(SRC)/%.c $(SRC)/%.h | $(OBJ) + $(CC) $(CFLAGS) -c $< -o $@ +$(OBJ)/%.o: $(SRC)/%.c | $(OBJ) + $(CC) $(CFLAGS) -c $< -o $@ +$(TEST)/bin/%: $(TEST)/%.c $(LIB) | $(TEST)/bin + $(CC) $(CFLAGS) $< $(LIB) $(PLATFORM_LIBS) -o $@ + +# Make directories if none. +$(LIBDIR): + mkdir $@ +$(INC): + mkdir $@ +$(OBJ): + mkdir $@ +$(TEST)/bin: + mkdir $@ + +# Run the tests in the bin folder and track results +test: $(LIB) $(TESTBINS) + @SUCCESS=0; FAILURE=0; \ + RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'; NC='\033[0m'; \ + for t in $(TESTBINS); do \ + NAME=$$(basename $$t); \ + START=$$(date +%s%N); \ + if $$t; then \ + RET=0; \ + else \ + RET=$$?; \ + fi; \ + END=$$(date +%s%N); \ + ELAPSED_NS=$$((END - START)); \ + ELAPSED_MS=$$((ELAPSED_NS / 1000000)); \ + if [ $$RET -eq 0 ]; then \ + printf "%-20s %bPASS%b (%b%4d ms%b)\n" "$$NAME" "$$GREEN" "$$NC" "$$YELLOW" "$$ELAPSED_MS" "$$NC"; \ + SUCCESS=$$((SUCCESS + 1)); \ + else \ + printf "%-20s %bFAIL%b (%b%4d ms%b)\n" "$$NAME" "$$RED" "$$NC" "$$YELLOW" "$$ELAPSED_MS" "$$NC"; \ + FAILURE=$$((FAILURE + 1)); \ + fi; \ + done; \ + printf "\nTests completed\n"; \ + printf "SUCCESS: %b%d%b\n" "$$GREEN" "$$SUCCESS" "$$NC"; \ + printf "FAILURE: %b%d%b\n" "$$RED" "$$FAILURE" "$$NC"; \ + test $$FAILURE -eq 0 + +clean: + $(RM) -r $(LIBDIR) $(OBJ) $(TEST)/bin/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..d9f3382 --- /dev/null +++ b/README.md @@ -0,0 +1,34 @@ +# EG + +## Description +This repository contains eg or "for example" files. That includes this README and LICENSE. +`~/eg`is a $HOME directory for me and is marked as an XDG template folder. +This allows you to easily create files from "templates" on a desktop. +It also serves as a filepath `~/eg` for scripts and automation. + +## Table of Contents + +- [Description](#description) +- [Features](#features) +- [Usage](#usage) +- [Credits / Resources](#credits--resources) +- [License](#license) + +## Features ++ Easy to copy. ++ Defaults to revert to. ++ Templates for easy productivity. + + +## Usage +Just copy and paste into your projects.
+/dev - Files used in development.
+/dot - Files used in configuration.
+ +## Credits / Resources +[Tom Preston-Werner README Driven Development](https://tom.preston-werner.com/2010/08/23/readme-driven-development)
+[Make a README](https://www.makeareadme.com/)
+[Choose a LICENSE](https://choosealicense.com/)
+ +## License +This project is licensed under MIT - see the [LICENSE](LICENSE) file for details. diff --git a/include/except.h b/include/except.h new file mode 100644 index 0000000..58b4d4f --- /dev/null +++ b/include/except.h @@ -0,0 +1,107 @@ +#ifndef EXCEPT_INCLUDED +#define EXCEPT_INCLUDED + +#include +struct Exception { + const char *reason; +}; +typedef struct Exception Exception; + +struct Except_Frame { + struct Except_Frame *prev; + jmp_buf env; + const char *file; + int line; + const struct Exception *exception; +}; +typedef struct Except_Frame Except_Frame; + +enum { EXCEPT_STATE_ENTERED=0, + EXCEPT_STATE_RAISED, + EXCEPT_STATE_HANDLED, + EXCEPT_STATE_FINALIZED, + EXCEPT_STATE_COUNT}; + +extern struct Except_Frame *except_stack; +extern const struct Exception assertion_failed; +void except_raise(const Exception *e, const char *file, int line); + +#undef assert +#ifdef NDEBUG +#define assert(e) ((void)0) +#else +extern void asserted(int e); +#define assert(e) ((void)((e)||(RAISE(assertion_failed),0))) +#endif + +#ifdef WIN32 +#include +extern DWORD except_index; +extern void except_init(void); +extern void except_push(Except_Frame *fp); +extern void except_pop(void); +#endif + +#ifdef WIN32 +#define RAISE(e) except_raise(&(e), __FILE__, __LINE__) +#define RERAISE except_raise(except_frame.exception, \ + except_frame.file, except_frame.line) +#define RETURN switch (except_pop(),0) default: return +#define TRY do { \ + volatile int except_flag; \ + Except_Frame except_frame; \ + if (except_index == -1) \ + except_init(); \ + except_push(&except_frame); \ + except_flag = setjmp(except_frame.env); \ + if (except_flag == EXCEPT_STATE_ENTERED) { +#define EXCEPT(e) \ + if (except_flag == EXCEPT_STATE_ENTERED) except_pop(); \ + } else if (except_frame.exception == &(e)) { \ + except_flag = EXCEPT_STATE_HANDLED; +#define ELSE \ + if (except_flag == EXCEPT_STATE_ENTERED) except_pop(); \ + } else { \ + except_flag = EXCEPT_STATE_HANDLED; +#define FINALLY \ + if (except_flag == EXCEPT_STATE_ENTERED) except_pop(); \ + } { \ + if (except_flag == EXCEPT_STATE_ENTERED) \ + except_flag = EXCEPT_STATE_FINALIZED; +#define END_TRY \ + if (except_flag == EXCEPT_STATE_ENTERED) except_pop(); \ + } if (except_flag == EXCEPT_STATE_RAISED) RERAISE; \ +} while (0) +#else +#define RAISE(e) except_raise(&(e), __FILE__, __LINE__) +#define RERAISE except_raise(except_frame.exception, \ + except_frame.file, except_frame.line) +#define RETURN switch (except_stack = except_stack->prev,0) default: return +#define TRY do { \ + volatile int except_flag; \ + Except_Frame except_frame; \ + except_frame.prev = except_stack; \ + except_stack = &except_frame; \ + except_flag = setjmp(except_frame.env); \ + if (except_flag == EXCEPT_STATE_ENTERED) { +#define EXCEPT(e) \ + if (except_flag == EXCEPT_STATE_ENTERED) except_stack = except_stack->prev; \ + } else if (except_frame.exception == &(e)) { \ + except_flag = EXCEPT_STATE_HANDLED; +#define ELSE \ + if (except_flag == EXCEPT_STATE_ENTERED) except_stack = except_stack->prev; \ + } else { \ + except_flag = EXCEPT_STATE_HANDLED; +#define FINALLY \ + if (except_flag == EXCEPT_STATE_ENTERED) except_stack = except_stack->prev; \ + } { \ + if (except_flag == EXCEPT_STATE_ENTERED) \ + except_flag = EXCEPT_STATE_FINALIZED; +#define END_TRY \ + if (except_flag == EXCEPT_STATE_ENTERED) except_stack = except_stack->prev; \ + } if (except_flag == EXCEPT_STATE_RAISED) RERAISE; \ +} while (0) +#endif + + +#endif // except.h diff --git a/include/platform.h b/include/platform.h new file mode 100644 index 0000000..a991da7 --- /dev/null +++ b/include/platform.h @@ -0,0 +1,6 @@ +#ifndef PLATFORM_INCLUDED +#define PLATFORM_INCLUDED + +extern int plat_main(void); + +#endif // platform.h diff --git a/src/except.c b/src/except.c new file mode 100644 index 0000000..03005b8 --- /dev/null +++ b/src/except.c @@ -0,0 +1,82 @@ +#include +#include +#include "../include/except.h" + +struct Except_Frame *except_stack = NULL; + +const struct Exception assertion_failed = { "Assertion failed" }; +void asserted(int e){ + if(!e){ + RAISE(assertion_failed); + } +} + + +void except_raise(const struct Exception *e, const char *file,int line) +{ +#ifdef WIN32 + Except_Frame *p; + + if (except_index == TLS_OUT_OF_INDEXES) + except_init(); + p = TlsGetValue(except_index); +#else + struct Except_Frame *p = except_stack; +#endif + asserted(e != NULL); + if (p == NULL) { + fprintf(stderr, "Uncaught exception"); + if (e->reason) + fprintf(stderr, " %s", e->reason); + else + fprintf(stderr, " at 0x%p", (void*)e); + if (file && line > 0) + fprintf(stderr, " raised at %s:%d\n", file, line); + fprintf(stderr, "aborting...\n"); + fflush(stderr); + abort(); + } + p->exception = e; + p->file = file; + p->line = line; +#ifdef WIN32 + except_pop(); +#else + except_stack = except_stack->prev; +#endif + longjmp(p->env, EXCEPT_STATE_RAISED); +} + + +#ifdef WIN32 +_CRTIMP void __cdecl _assert(void *, void *, unsigned); +#undef assert +#define assert(e) ((e) || (_assert(#e, __FILE__, __LINE__), 0)) + +DWORD except_index = -1; +void except_init(void) { + BOOL cond; + + except_index = TlsAlloc(); + assert(except_index != TLS_OUT_OF_INDEXES); + cond = TlsSetValue(except_index, NULL); + assert(cond == TRUE); +} + +void except_push(Except_Frame *fp) { + BOOL cond; + + fp->prev = TlsGetValue(except_index); + cond = TlsSetValue(except_index, fp); + assert(cond == TRUE); +} + +void except_pop(void) { + BOOL cond; + Except_Frame *tos = TlsGetValue(except_index); + + cond = TlsSetValue(except_index, tos->prev); + assert(cond == TRUE); +} + +#endif diff --git a/src/linux_platform.c b/src/linux_platform.c new file mode 100644 index 0000000..a4b9241 --- /dev/null +++ b/src/linux_platform.c @@ -0,0 +1,64 @@ +#include +#include +#include +#include + +#include "../include/platform.h" + +int plat_main(void) { + Display *display = XOpenDisplay(NULL); + if (!display) { + fprintf(stderr, "Cannot open display\n"); + return 1; + } + + int screen = DefaultScreen(display); + + Window window = XCreateSimpleWindow( + display, + RootWindow(display, screen), + 100, 100, /* x, y position */ + 640, 480, /* width, height */ + 2, /* border width */ + BlackPixel(display, screen), /* border color */ + WhitePixel(display, screen) /* background color */ + ); + + /* Set window title */ + XStoreName(display, window, "Minimal X11 Window"); + + /* Subscribe to events */ + XSelectInput(display, window, ExposureMask | KeyPressMask | StructureNotifyMask); + + /* Show the window */ + XMapWindow(display, window); + + /* Handle the WM_DELETE_WINDOW protocol so clicking X closes cleanly */ + Atom wm_delete = XInternAtom(display, "WM_DELETE_WINDOW", False); + XSetWMProtocols(display, window, &wm_delete, 1); + + /* Event loop */ + XEvent event; + while (1) { + XNextEvent(display, &event); + + if (event.type == Expose) { + /* Redraw when window is exposed */ + XDrawString(display, window, DefaultGC(display, screen), + 50, 50, "Hello, X11!", 11); + } + else if (event.type == KeyPress) { + /* Close on any key press */ + break; + } + else if (event.type == ClientMessage) { + /* Close when user clicks the window's X button */ + if ((Atom)event.xclient.data.l[0] == wm_delete) + break; + } + } + + XDestroyWindow(display, window); + XCloseDisplay(display); + return 0; +} diff --git a/src/windows_platform.c b/src/windows_platform.c new file mode 100644 index 0000000..a759d5c --- /dev/null +++ b/src/windows_platform.c @@ -0,0 +1,57 @@ +#include +#include "../include/platform.h" + + +// Window procedure - handles messages sent to the window +LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) +{ + switch (msg) + { + case WM_DESTROY: + PostQuitMessage(0); + return 0; + } + return DefWindowProc(hwnd, msg, wParam, lParam); +} + +int WINAPI plat_main(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) +{ + // 1. Register the window class + WNDCLASS wc = {}; + wc.lpfnWndProc = WndProc; + wc.hInstance = hInstance; + wc.lpszClassName = "MyWindowClass"; + wc.hCursor = LoadCursor(NULL, IDC_ARROW); + wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); + + RegisterClass(&wc); + + // 2. Create the window + HWND hwnd = CreateWindowEx( + 0, // Extended style + "MyWindowClass", // Class name + "Hello, Win32!", // Window title + WS_OVERLAPPEDWINDOW, // Style (title bar, border, min/max/close buttons) + CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, // x, y, width, height + NULL, // Parent window + NULL, // Menu + hInstance, + NULL + ); + + if (!hwnd) return -1; + + // 3. Show the window + ShowWindow(hwnd, nCmdShow); + UpdateWindow(hwnd); + + // 4. Message loop + MSG msg = {}; + while (GetMessage(&msg, NULL, 0, 0)) + { + TranslateMessage(&msg); + DispatchMessage(&msg); + } + + return (int)msg.wParam; +} diff --git a/tests/01_platform.c b/tests/01_platform.c new file mode 100644 index 0000000..ca89ea8 --- /dev/null +++ b/tests/01_platform.c @@ -0,0 +1,6 @@ +#define DEBUG +#include "../include/platform.h" +int main(void){ + + return plat_main(); +}