aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJedidiah Barber <contact@jedbarber.id.au>2025-04-15 00:41:02 +1200
committerJedidiah Barber <contact@jedbarber.id.au>2025-04-15 00:41:02 +1200
commitf422ec5ac8102abb99a92d4742b4baa88130b75f (patch)
tree8fd8daebd34504dfd0398a46dedbd7635648d726
parent78bf43af446adf83fdc0554c63ccdb631836d916 (diff)
Executable now knows where it is, installs and finds logo image correctly, better build config
-rw-r--r--adapad.gpr106
-rw-r--r--hereiam/here_i_am.adb89
-rw-r--r--hereiam/here_i_am.ads25
-rw-r--r--hereiam/whereami.c815
-rw-r--r--hereiam/whereami.h69
-rw-r--r--src/images.adb44
-rw-r--r--src/images.ads17
-rw-r--r--src/windows.ads15
-rw-r--r--to_do.txt2
9 files changed, 1161 insertions, 21 deletions
diff --git a/adapad.gpr b/adapad.gpr
index 2d88316..dacc020 100644
--- a/adapad.gpr
+++ b/adapad.gpr
@@ -1,39 +1,127 @@
--- goal is for executable to be no more than 90-100kB
--- when dynamically linked, to match leafpad
+with
-
-with "fltkada";
+ "fltkada";
project AdaPad is
- for Languages use ("Ada");
+ for Languages use ("Ada", "C");
- for Source_Dirs use ("src/**");
+ for Source_Dirs use ("hereiam", "src");
for Object_Dir use "obj";
for Exec_Dir use "bin";
for Main use ("main.adb");
+ type Build_Kind is ("release", "debug");
+
+ Ver : Build_Kind := external ("build", "release");
+
+
package Builder is
- for Executable("main.adb") use "adapad";
+ for Default_Switches ("Ada") use ("-j4", "-m");
+
+ case Ver is
+
+ when "release" =>
+ null;
+
+ when "debug" =>
+ for Default_Switches ("Ada") use Builder'Default_Switches ("Ada") & "-g";
+
+ end case;
+
+ for Executable ("main.adb") use "adapad";
end Builder;
+ Ada_Common :=
+ ("-gnaty"
+ & "4" -- indentation
+ & "a" -- attribute casing
+ & "A" -- array attribute indices
+ & "b" -- blanks at end of lines
+ & "c" -- two space comments
+ & "e" -- end/exit labels
+ & "f" -- no form feeds or vertical tabs
+ & "h" -- no horizontal tabs
+ & "i" -- if/then layout
+ & "k" -- keyword casing
+ & "l" -- reference manual layout
+ & "M100" -- max line length
+ & "n" -- package Standard casing
+ & "p" -- pragma casing
+ & "r" -- identifier casing
+ & "t", -- token separation
+ "-gnatw"
+ & "a" -- various warning modes
+ & "F" -- don't check for unreferenced formal parameters
+ & "J" -- don't check for obsolescent feature use
+ & "U"); -- don't check for unused entities
+
+ C_Common :=
+ ("-Wall",
+ "-Werror",
+ "-Wextra",
+ "-Wpedantic");
+
package Compiler is
- for Default_Switches("Ada") use ("-gnaty4aAbcefhiklM100nprt");
+ case Ver is
+
+ when "release" =>
+ for Default_Switches ("Ada") use Ada_Common & "-O3" & "-gnatn";
+ for Default_Switches ("C") use C_Common & "-O3";
+
+ when "debug" =>
+ for Default_Switches ("Ada") use Ada_Common & "-O0" & "-gnata" & "-gnato" & "-g";
+ for Default_Switches ("C") use C_Common & "-O0";
+
+ end case;
end Compiler;
+ package Binder is
+ for Default_Switches ("Ada") use ("-shared");
+
+ case Ver is
+
+ when "release" =>
+ null;
+
+ when "debug" =>
+ for Default_Switches ("Ada") use Binder'Default_Switches ("Ada") & "-Es";
+
+ end case;
+ end Binder;
+
+
package Linker is
- for Default_Switches("Ada") use ("-lfltk", "-lfltk_images");
+ case Ver is
+
+ when "release" =>
+ null;
+
+ when "debug" =>
+ for Default_Switches ("Ada") use ("-g");
+
+ end case;
+
+ for Default_Switches ("Ada") use
+ Linker'Default_Switches ("Ada") & ("-lfltk", "-lfltk_images");
end Linker;
+ package Install is
+ for Artifacts ("share") use ("share/*");
+ for Required_Artifacts ("share") use ("share/adapad/logo.png");
+ for Mode use "usage";
+ end Install;
+
+
end AdaPad;
diff --git a/hereiam/here_i_am.adb b/hereiam/here_i_am.adb
new file mode 100644
index 0000000..879e07e
--- /dev/null
+++ b/hereiam/here_i_am.adb
@@ -0,0 +1,89 @@
+
+
+-- Programmed by Jedidiah Barber
+-- Released into the public domain
+
+
+with
+
+ Ada.Assertions,
+ Interfaces.C.Strings,
+ System.Storage_Elements;
+
+use type
+
+ Interfaces.C.int;
+
+
+package body Here_I_Am is
+
+
+ package Storage renames System.Storage_Elements;
+
+ Null_Pointer : constant Storage.Integer_Address := Storage.To_Integer (System.Null_Address);
+
+
+
+
+ function wai_getExecutablePath
+ (Buffer : in Interfaces.C.Strings.chars_ptr;
+ Length : in Interfaces.C.int;
+ Dir : in Storage.Integer_Address)
+ return Interfaces.C.int;
+ pragma Import (C, wai_getExecutablePath, "wai_getExecutablePath");
+ pragma Inline (wai_getExecutablePath);
+
+ function wai_getModulePath
+ (Buffer : in Interfaces.C.Strings.chars_ptr;
+ Length : in Interfaces.C.int;
+ Dir : in Storage.Integer_Address)
+ return Interfaces.C.int;
+ pragma Import (C, wai_getModulePath, "wai_getModulePath");
+ pragma Inline (wai_getModulePath);
+
+
+
+
+ function Executable
+ return String
+ is
+ Path_Length : constant Interfaces.C.int :=
+ wai_getExecutablePath (Interfaces.C.Strings.Null_Ptr, 0, Null_Pointer);
+ Data_Buffer : aliased Interfaces.C.char_array :=
+ (1 .. Interfaces.C.size_t (Path_Length) => Interfaces.C.nul);
+ Code : constant Interfaces.C.int := wai_getExecutablePath
+ (Interfaces.C.Strings.To_Chars_Ptr (Data_Buffer'Unchecked_Access),
+ Path_Length,
+ Null_Pointer);
+ begin
+ pragma Assert (Code >= 0);
+ return Interfaces.C.To_Ada (Data_Buffer, False);
+ exception
+ when Ada.Assertions.Assertion_Error => raise Where_Is_Error with
+ "wai_getExecutablePath returned int value of " & Interfaces.C.int'Image (Code);
+ end Executable;
+
+
+ function Module
+ return String
+ is
+ Path_Length : constant Interfaces.C.int :=
+ wai_getModulePath (Interfaces.C.Strings.Null_Ptr, 0, Null_Pointer);
+ Data_Buffer : aliased Interfaces.C.char_array :=
+ (1 .. Interfaces.C.size_t (Path_Length) => Interfaces.C.nul);
+ Code : constant Interfaces.C.int := wai_getModulePath
+ (Interfaces.C.Strings.To_Chars_Ptr (Data_Buffer'Unchecked_Access),
+ Path_Length,
+ Null_Pointer);
+ begin
+ pragma Assert (Code >= 0);
+ return Interfaces.C.To_Ada (Data_Buffer, False);
+ exception
+ when Ada.Assertions.Assertion_Error => raise Where_Is_Error with
+ "wai_getModulePath returned int value of " & Interfaces.C.int'Image (Code);
+ end Module;
+
+
+end Here_I_Am;
+
+
diff --git a/hereiam/here_i_am.ads b/hereiam/here_i_am.ads
new file mode 100644
index 0000000..367ac57
--- /dev/null
+++ b/hereiam/here_i_am.ads
@@ -0,0 +1,25 @@
+
+
+-- Programmed by Jedidiah Barber
+-- Released into the public domain
+
+
+package Here_I_Am is
+
+
+ Where_Is_Error : exception;
+
+
+ -- Provides the full name and path of the running executable
+ function Executable
+ return String;
+
+
+ -- Provides the full name and path of the running library
+ function Module
+ return String;
+
+
+end Here_I_Am;
+
+
diff --git a/hereiam/whereami.c b/hereiam/whereami.c
new file mode 100644
index 0000000..a8a649d
--- /dev/null
+++ b/hereiam/whereami.c
@@ -0,0 +1,815 @@
+// (‑●‑●)> dual licensed under the WTFPL v2 and MIT licenses
+// without any warranty.
+// by Gregory Pakosz (@gpakosz)
+// https://github.com/gpakosz/whereami
+
+// in case you want to #include "whereami.c" in a larger compilation unit
+#if !defined(WHEREAMI_H)
+#include <whereami.h>
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#if defined(__linux__) || defined(__CYGWIN__)
+#undef _DEFAULT_SOURCE
+#define _DEFAULT_SOURCE
+#elif defined(__APPLE__)
+#undef _DARWIN_C_SOURCE
+#define _DARWIN_C_SOURCE
+#define _DARWIN_BETTER_REALPATH
+#endif
+
+#if !defined(WAI_MALLOC) || !defined(WAI_FREE) || !defined(WAI_REALLOC)
+#include <stdlib.h>
+#endif
+
+#if !defined(WAI_MALLOC)
+#define WAI_MALLOC(size) malloc(size)
+#endif
+
+#if !defined(WAI_FREE)
+#define WAI_FREE(p) free(p)
+#endif
+
+#if !defined(WAI_REALLOC)
+#define WAI_REALLOC(p, size) realloc(p, size)
+#endif
+
+#ifndef WAI_NOINLINE
+#if defined(_MSC_VER)
+#define WAI_NOINLINE __declspec(noinline)
+#elif defined(__GNUC__)
+#define WAI_NOINLINE __attribute__((noinline))
+#else
+#error unsupported compiler
+#endif
+#endif
+
+#if defined(_MSC_VER)
+#define WAI_RETURN_ADDRESS() _ReturnAddress()
+#elif defined(__GNUC__)
+#define WAI_RETURN_ADDRESS() __builtin_extract_return_addr(__builtin_return_address(0))
+#else
+#error unsupported compiler
+#endif
+
+#if defined(_WIN32)
+
+#ifndef WIN32_LEAN_AND_MEAN
+#define WIN32_LEAN_AND_MEAN
+#endif
+#if defined(_MSC_VER)
+#pragma warning(push, 3)
+#endif
+#include <windows.h>
+#include <intrin.h>
+#if defined(_MSC_VER)
+#pragma warning(pop)
+#endif
+#if (_MSC_VER >= 1900)
+#include <stdbool.h>
+#else
+#define bool int
+#define false 0
+#define true 1
+#endif
+
+static int WAI_PREFIX(getModulePath_)(HMODULE module, char* out, int capacity, int* dirname_length)
+{
+ wchar_t buffer1[MAX_PATH];
+ wchar_t buffer2[MAX_PATH];
+ wchar_t* path = NULL;
+ int length = -1;
+ bool ok;
+
+ for (ok = false; !ok; ok = true)
+ {
+ DWORD size;
+ int length_, length__;
+
+ size = GetModuleFileNameW(module, buffer1, sizeof(buffer1) / sizeof(buffer1[0]));
+
+ if (size == 0)
+ break;
+ else if (size == (DWORD)(sizeof(buffer1) / sizeof(buffer1[0])))
+ {
+ DWORD size_ = size;
+ do
+ {
+ wchar_t* path_;
+
+ path_ = (wchar_t*)WAI_REALLOC(path, sizeof(wchar_t) * size_ * 2);
+ if (!path_)
+ break;
+ size_ *= 2;
+ path = path_;
+ size = GetModuleFileNameW(module, path, size_);
+ }
+ while (size == size_);
+
+ if (size == size_)
+ break;
+ }
+ else
+ path = buffer1;
+
+ if (!_wfullpath(buffer2, path, MAX_PATH))
+ break;
+ length_ = (int)wcslen(buffer2);
+ length__ = WideCharToMultiByte(CP_UTF8, 0, buffer2, length_ , out, capacity, NULL, NULL);
+
+ if (length__ == 0)
+ length__ = WideCharToMultiByte(CP_UTF8, 0, buffer2, length_, NULL, 0, NULL, NULL);
+ if (length__ == 0)
+ break;
+
+ if (length__ <= capacity && dirname_length)
+ {
+ int i;
+
+ for (i = length__ - 1; i >= 0; --i)
+ {
+ if (out[i] == '\\')
+ {
+ *dirname_length = i;
+ break;
+ }
+ }
+ }
+
+ length = length__;
+ }
+
+ if (path != buffer1)
+ WAI_FREE(path);
+
+ return ok ? length : -1;
+}
+
+WAI_NOINLINE WAI_FUNCSPEC
+int WAI_PREFIX(getExecutablePath)(char* out, int capacity, int* dirname_length)
+{
+ return WAI_PREFIX(getModulePath_)(NULL, out, capacity, dirname_length);
+}
+
+WAI_NOINLINE WAI_FUNCSPEC
+int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length)
+{
+ HMODULE module;
+ int length = -1;
+
+#if defined(_MSC_VER)
+#pragma warning(push)
+#pragma warning(disable: 4054)
+#endif
+ if (GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, (LPCTSTR)WAI_RETURN_ADDRESS(), &module))
+#if defined(_MSC_VER)
+#pragma warning(pop)
+#endif
+ {
+ length = WAI_PREFIX(getModulePath_)(module, out, capacity, dirname_length);
+ }
+
+ return length;
+}
+
+#elif defined(__linux__) || defined(__CYGWIN__) || defined(__sun) || defined(WAI_USE_PROC_SELF_EXE)
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#if defined(__linux__)
+#include <linux/limits.h>
+#else
+#include <limits.h>
+#endif
+#ifndef __STDC_FORMAT_MACROS
+#define __STDC_FORMAT_MACROS
+#endif
+#include <inttypes.h>
+#include <stdbool.h>
+
+#if !defined(WAI_PROC_SELF_EXE)
+#if defined(__sun)
+#define WAI_PROC_SELF_EXE "/proc/self/path/a.out"
+#else
+#define WAI_PROC_SELF_EXE "/proc/self/exe"
+#endif
+#endif
+
+WAI_FUNCSPEC
+int WAI_PREFIX(getExecutablePath)(char* out, int capacity, int* dirname_length)
+{
+ char buffer[PATH_MAX];
+ char* resolved = NULL;
+ int length = -1;
+ bool ok;
+
+ for (ok = false; !ok; ok = true)
+ {
+ resolved = realpath(WAI_PROC_SELF_EXE, buffer);
+ if (!resolved)
+ break;
+
+ length = (int)strlen(resolved);
+ if (length <= capacity)
+ {
+ memcpy(out, resolved, length);
+
+ if (dirname_length)
+ {
+ int i;
+
+ for (i = length - 1; i >= 0; --i)
+ {
+ if (out[i] == '/')
+ {
+ *dirname_length = i;
+ break;
+ }
+ }
+ }
+ }
+ }
+
+ return ok ? length : -1;
+}
+
+#if !defined(WAI_PROC_SELF_MAPS_RETRY)
+#define WAI_PROC_SELF_MAPS_RETRY 5
+#endif
+
+#if !defined(WAI_PROC_SELF_MAPS)
+#if defined(__sun)
+#define WAI_PROC_SELF_MAPS "/proc/self/map"
+#else
+#define WAI_PROC_SELF_MAPS "/proc/self/maps"
+#endif
+#endif
+
+#if !defined(WAI_STRINGIZE)
+#define WAI_STRINGIZE(s)
+#define WAI_STRINGIZE_(s) #s
+#endif
+
+#if defined(__ANDROID__) || defined(ANDROID)
+#include <fcntl.h>
+#include <sys/mman.h>
+#include <unistd.h>
+#endif
+#include <stdbool.h>
+
+WAI_NOINLINE WAI_FUNCSPEC
+int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length)
+{
+ int length = -1;
+ FILE* maps = NULL;
+
+ for (int r = 0; r < WAI_PROC_SELF_MAPS_RETRY; ++r)
+ {
+ maps = fopen(WAI_PROC_SELF_MAPS, "r");
+ if (!maps)
+ break;
+
+ for (;;)
+ {
+ char buffer[128 + PATH_MAX];
+ uintptr_t low, high;
+ char perms[5];
+ uint64_t offset;
+ uint32_t major, minor, inode;
+ char path[PATH_MAX + 1];
+
+ if (!fgets(buffer, sizeof(buffer), maps))
+ break;
+
+ if (sscanf(buffer, "%" SCNxPTR "-%" SCNxPTR " %s %" SCNx64 " %x:%x %u %" WAI_STRINGIZE(PATH_MAX) "[^\n]\n", &low, &high, perms, &offset, &major, &minor, &inode, path) == 8)
+ {
+ void* _addr = WAI_RETURN_ADDRESS();
+ uintptr_t addr = (uintptr_t)_addr;
+ if (low <= addr && addr <= high)
+ {
+ char* resolved;
+
+ resolved = realpath(path, buffer);
+ if (!resolved)
+ break;
+
+ length = (int)strlen(resolved);
+#if defined(__ANDROID__) || defined(ANDROID)
+ if (length > 4
+ &&buffer[length - 1] == 'k'
+ &&buffer[length - 2] == 'p'
+ &&buffer[length - 3] == 'a'
+ &&buffer[length - 4] == '.')
+ {
+ int fd = open(path, O_RDONLY);
+ if (fd == -1)
+ {
+ length = -1; // retry
+ break;
+ }
+
+ char* begin = (char*)mmap(0, offset, PROT_READ, MAP_SHARED, fd, 0);
+ if (begin == MAP_FAILED)
+ {
+ close(fd);
+ length = -1; // retry
+ break;
+ }
+
+ char* p = begin + offset - 30; // minimum size of local file header
+ while (p >= begin) // scan backwards
+ {
+ if (*((uint32_t*)p) == 0x04034b50UL) // local file header signature found
+ {
+ uint16_t length_ = *((uint16_t*)(p + 26));
+
+ if (length + 2 + length_ < (int)sizeof(buffer))
+ {
+ memcpy(&buffer[length], "!/", 2);
+ memcpy(&buffer[length + 2], p + 30, length_);
+ length += 2 + length_;
+ }
+
+ break;
+ }
+
+ --p;
+ }
+
+ munmap(begin, offset);
+ close(fd);
+ }
+#endif
+ if (length <= capacity)
+ {
+ memcpy(out, resolved, length);
+
+ if (dirname_length)
+ {
+ int i;
+
+ for (i = length - 1; i >= 0; --i)
+ {
+ if (out[i] == '/')
+ {
+ *dirname_length = i;
+ break;
+ }
+ }
+ }
+ }
+
+ break;
+ }
+ }
+ }
+
+ fclose(maps);
+ maps = NULL;
+
+ if (length != -1)
+ break;
+ }
+
+ return length;
+}
+
+#elif defined(__APPLE__)
+
+#include <mach-o/dyld.h>
+#include <limits.h>
+#include <stdlib.h>
+#include <string.h>
+#include <dlfcn.h>
+#include <stdbool.h>
+
+WAI_FUNCSPEC
+int WAI_PREFIX(getExecutablePath)(char* out, int capacity, int* dirname_length)
+{
+ char buffer1[PATH_MAX];
+ char buffer2[PATH_MAX];
+ char* path = buffer1;
+ char* resolved = NULL;
+ int length = -1;
+ bool ok;
+
+ for (ok = false; !ok; ok = true)
+ {
+ uint32_t size = (uint32_t)sizeof(buffer1);
+ if (_NSGetExecutablePath(path, &size) == -1)
+ {
+ path = (char*)WAI_MALLOC(size);
+ if (!_NSGetExecutablePath(path, &size))
+ break;
+ }
+
+ resolved = realpath(path, buffer2);
+ if (!resolved)
+ break;
+
+ length = (int)strlen(resolved);
+ if (length <= capacity)
+ {
+ memcpy(out, resolved, length);
+
+ if (dirname_length)
+ {
+ int i;
+
+ for (i = length - 1; i >= 0; --i)
+ {
+ if (out[i] == '/')
+ {
+ *dirname_length = i;
+ break;
+ }
+ }
+ }
+ }
+ }
+
+ if (path != buffer1)
+ WAI_FREE(path);
+
+ return ok ? length : -1;
+}
+
+WAI_NOINLINE WAI_FUNCSPEC
+int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length)
+{
+ char buffer[PATH_MAX];
+ char* resolved = NULL;
+ int length = -1;
+
+ for(;;)
+ {
+ Dl_info info;
+
+ if (dladdr(WAI_RETURN_ADDRESS(), &info))
+ {
+ resolved = realpath(info.dli_fname, buffer);
+ if (!resolved)
+ break;
+
+ length = (int)strlen(resolved);
+ if (length <= capacity)
+ {
+ memcpy(out, resolved, length);
+
+ if (dirname_length)
+ {
+ int i;
+
+ for (i = length - 1; i >= 0; --i)
+ {
+ if (out[i] == '/')
+ {
+ *dirname_length = i;
+ break;
+ }
+ }
+ }
+ }
+ }
+
+ break;
+ }
+
+ return length;
+}
+
+#elif defined(__QNXNTO__)
+
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <dlfcn.h>
+#include <stdbool.h>
+
+#if !defined(WAI_PROC_SELF_EXE)
+#define WAI_PROC_SELF_EXE "/proc/self/exefile"
+#endif
+
+WAI_FUNCSPEC
+int WAI_PREFIX(getExecutablePath)(char* out, int capacity, int* dirname_length)
+{
+ char buffer1[PATH_MAX];
+ char buffer2[PATH_MAX];
+ char* resolved = NULL;
+ FILE* self_exe = NULL;
+ int length = -1;
+ bool ok;
+
+ for (ok = false; !ok; ok = true)
+ {
+ self_exe = fopen(WAI_PROC_SELF_EXE, "r");
+ if (!self_exe)
+ break;
+
+ if (!fgets(buffer1, sizeof(buffer1), self_exe))
+ break;
+
+ resolved = realpath(buffer1, buffer2);
+ if (!resolved)
+ break;
+
+ length = (int)strlen(resolved);
+ if (length <= capacity)
+ {
+ memcpy(out, resolved, length);
+
+ if (dirname_length)
+ {
+ int i;
+
+ for (i = length - 1; i >= 0; --i)
+ {
+ if (out[i] == '/')
+ {
+ *dirname_length = i;
+ break;
+ }
+ }
+ }
+ }
+ }
+
+ fclose(self_exe);
+
+ return ok ? length : -1;
+}
+
+WAI_FUNCSPEC
+int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length)
+{
+ char buffer[PATH_MAX];
+ char* resolved = NULL;
+ int length = -1;
+
+ for(;;)
+ {
+ Dl_info info;
+
+ if (dladdr(WAI_RETURN_ADDRESS(), &info))
+ {
+ resolved = realpath(info.dli_fname, buffer);
+ if (!resolved)
+ break;
+
+ length = (int)strlen(resolved);
+ if (length <= capacity)
+ {
+ memcpy(out, resolved, length);
+
+ if (dirname_length)
+ {
+ int i;
+
+ for (i = length - 1; i >= 0; --i)
+ {
+ if (out[i] == '/')
+ {
+ *dirname_length = i;
+ break;
+ }
+ }
+ }
+ }
+ }
+
+ break;
+ }
+
+ return length;
+}
+
+#elif defined(__DragonFly__) || defined(__FreeBSD__) || \
+ defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__)
+
+#include <limits.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/sysctl.h>
+#include <dlfcn.h>
+#include <stdbool.h>
+
+#if defined(__OpenBSD__)
+
+#include <unistd.h>
+
+WAI_FUNCSPEC
+int WAI_PREFIX(getExecutablePath)(char* out, int capacity, int* dirname_length)
+{
+ char buffer1[4096];
+ char buffer2[PATH_MAX];
+ char buffer3[PATH_MAX];
+ char** argv = (char**)buffer1;
+ char* resolved = NULL;
+ int length = -1;
+ bool ok;
+
+ for (ok = false; !ok; ok = true)
+ {
+ int mib[4] = { CTL_KERN, KERN_PROC_ARGS, getpid(), KERN_PROC_ARGV };
+ size_t size;
+
+ if (sysctl(mib, 4, NULL, &size, NULL, 0) != 0)
+ break;
+
+ if (size > sizeof(buffer1))
+ {
+ argv = (char**)WAI_MALLOC(size);
+ if (!argv)
+ break;
+ }
+
+ if (sysctl(mib, 4, argv, &size, NULL, 0) != 0)
+ break;
+
+ if (strchr(argv[0], '/'))
+ {
+ resolved = realpath(argv[0], buffer2);
+ if (!resolved)
+ break;
+ }
+ else
+ {
+ const char* PATH = getenv("PATH");
+ if (!PATH)
+ break;
+
+ size_t argv0_length = strlen(argv[0]);
+
+ const char* begin = PATH;
+ while (1)
+ {
+ const char* separator = strchr(begin, ':');
+ const char* end = separator ? separator : begin + strlen(begin);
+
+ if (end - begin > 0)
+ {
+ if (*(end -1) == '/')
+ --end;
+
+ if (((end - begin) + 1 + argv0_length + 1) <= sizeof(buffer2))
+ {
+ memcpy(buffer2, begin, end - begin);
+ buffer2[end - begin] = '/';
+ memcpy(buffer2 + (end - begin) + 1, argv[0], argv0_length + 1);
+
+ resolved = realpath(buffer2, buffer3);
+ if (resolved)
+ break;
+ }
+ }
+
+ if (!separator)
+ break;
+
+ begin = ++separator;
+ }
+
+ if (!resolved)
+ break;
+ }
+
+ length = (int)strlen(resolved);
+ if (length <= capacity)
+ {
+ memcpy(out, resolved, length);
+
+ if (dirname_length)
+ {
+ int i;
+
+ for (i = length - 1; i >= 0; --i)
+ {
+ if (out[i] == '/')
+ {
+ *dirname_length = i;
+ break;
+ }
+ }
+ }
+ }
+ }
+
+ if (argv != (char**)buffer1)
+ WAI_FREE(argv);
+
+ return ok ? length : -1;
+}
+
+#else
+
+WAI_FUNCSPEC
+int WAI_PREFIX(getExecutablePath)(char* out, int capacity, int* dirname_length)
+{
+ char buffer1[PATH_MAX];
+ char buffer2[PATH_MAX];
+ char* path = buffer1;
+ char* resolved = NULL;
+ int length = -1;
+ bool ok;
+
+ for (ok = false; !ok; ok = true)
+ {
+#if defined(__NetBSD__)
+ int mib[4] = { CTL_KERN, KERN_PROC_ARGS, -1, KERN_PROC_PATHNAME };
+#else
+ int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
+#endif
+ size_t size = sizeof(buffer1);
+
+ if (sysctl(mib, 4, path, &size, NULL, 0) != 0)
+ break;
+
+ resolved = realpath(path, buffer2);
+ if (!resolved)
+ break;
+
+ length = (int)strlen(resolved);
+ if (length <= capacity)
+ {
+ memcpy(out, resolved, length);
+
+ if (dirname_length)
+ {
+ int i;
+
+ for (i = length - 1; i >= 0; --i)
+ {
+ if (out[i] == '/')
+ {
+ *dirname_length = i;
+ break;
+ }
+ }
+ }
+ }
+ }
+
+ return ok ? length : -1;
+}
+
+#endif
+
+WAI_NOINLINE WAI_FUNCSPEC
+int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length)
+{
+ char buffer[PATH_MAX];
+ char* resolved = NULL;
+ int length = -1;
+
+ for(;;)
+ {
+ Dl_info info;
+
+ if (dladdr(WAI_RETURN_ADDRESS(), &info))
+ {
+ resolved = realpath(info.dli_fname, buffer);
+ if (!resolved)
+ break;
+
+ length = (int)strlen(resolved);
+ if (length <= capacity)
+ {
+ memcpy(out, resolved, length);
+
+ if (dirname_length)
+ {
+ int i;
+
+ for (i = length - 1; i >= 0; --i)
+ {
+ if (out[i] == '/')
+ {
+ *dirname_length = i;
+ break;
+ }
+ }
+ }
+ }
+ }
+
+ break;
+ }
+
+ return length;
+}
+
+#else
+
+#error unsupported platform
+
+#endif
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/hereiam/whereami.h b/hereiam/whereami.h
new file mode 100644
index 0000000..8e58c81
--- /dev/null
+++ b/hereiam/whereami.h
@@ -0,0 +1,69 @@
+// (‑●‑●)> dual licensed under the WTFPL v2 and MIT licenses
+// without any warranty.
+// by Gregory Pakosz (@gpakosz)
+// https://github.com/gpakosz/whereami
+
+#ifndef WHEREAMI_H
+#define WHEREAMI_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifndef WAI_FUNCSPEC
+ #define WAI_FUNCSPEC
+#endif
+#ifndef WAI_PREFIX
+#define WAI_PREFIX(function) wai_##function
+#endif
+
+/**
+ * Returns the path to the current executable.
+ *
+ * Usage:
+ * - first call `int length = wai_getExecutablePath(NULL, 0, NULL);` to
+ * retrieve the length of the path
+ * - allocate the destination buffer with `path = (char*)malloc(length + 1);`
+ * - call `wai_getExecutablePath(path, length, NULL)` again to retrieve the
+ * path
+ * - add a terminal NUL character with `path[length] = '\0';`
+ *
+ * @param out destination buffer, optional
+ * @param capacity destination buffer capacity
+ * @param dirname_length optional recipient for the length of the dirname part
+ * of the path. Available only when `capacity` is large enough to retrieve the
+ * path.
+ *
+ * @return the length of the executable path on success (without a terminal NUL
+ * character), otherwise `-1`
+ */
+WAI_FUNCSPEC
+int WAI_PREFIX(getExecutablePath)(char* out, int capacity, int* dirname_length);
+
+/**
+ * Returns the path to the current module
+ *
+ * Usage:
+ * - first call `int length = wai_getModulePath(NULL, 0, NULL);` to retrieve
+ * the length of the path
+ * - allocate the destination buffer with `path = (char*)malloc(length + 1);`
+ * - call `wai_getModulePath(path, length, NULL)` again to retrieve the path
+ * - add a terminal NUL character with `path[length] = '\0';`
+ *
+ * @param out destination buffer, optional
+ * @param capacity destination buffer capacity
+ * @param dirname_length optional recipient for the length of the dirname part
+ * of the path. Available only when `capacity` is large enough to retrieve the
+ * path.
+ *
+ * @return the length of the module path on success (without a terminal NUL
+ * character), otherwise `-1`
+ */
+WAI_FUNCSPEC
+int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // #ifndef WHEREAMI_H
diff --git a/src/images.adb b/src/images.adb
new file mode 100644
index 0000000..07897d1
--- /dev/null
+++ b/src/images.adb
@@ -0,0 +1,44 @@
+
+
+with
+
+ Ada.Command_Line,
+ Ada.Directories,
+ FLTK.Images.RGB.PNG,
+ Here_I_Am;
+
+
+package body Images is
+
+
+ package ACom renames Ada.Command_Line;
+ package ADir renames Ada.Directories;
+
+
+ Dummy : constant FLTK.Color_Component_Array := (1 .. 0 => 0);
+
+
+
+
+ function Load_Logo
+ return FLTK.Images.RGB.RGB_Image'Class is
+ begin
+ declare
+ Base_Path : String := ADir.Containing_Directory (Here_I_Am.Executable);
+ Logo_Path : String :=
+ ADir.Compose
+ (ADir.Compose -- No matter how many times I write this, still looks weird...
+ (ADir.Compose
+ (ADir.Compose (Base_Path, ".."), "share"), "adapad"), "logo.png");
+ begin
+ return FLTK.Images.RGB.PNG.Forge.Create (Logo_Path);
+ end;
+ exception
+ when others =>
+ return FLTK.Images.RGB.Forge.Create (Dummy, 0, 0);
+ end Load_Logo;
+
+
+end Images;
+
+
diff --git a/src/images.ads b/src/images.ads
new file mode 100644
index 0000000..e6d44f5
--- /dev/null
+++ b/src/images.ads
@@ -0,0 +1,17 @@
+
+
+with
+
+ FLTK.Images.RGB;
+
+
+package Images is
+
+
+ function Load_Logo
+ return FLTK.Images.RGB.RGB_Image'Class;
+
+
+end Images;
+
+
diff --git a/src/windows.ads b/src/windows.ads
index 8050467..222efcf 100644
--- a/src/windows.ads
+++ b/src/windows.ads
@@ -3,12 +3,8 @@
with
FLTK.Widgets.Groups.Windows.Double,
- FLTK.Images.RGB.PNG;
-
-private with
-
- Ada.Command_Line,
- Ada.Directories;
+ FLTK.Images.RGB,
+ Images;
package Windows is
@@ -27,12 +23,9 @@ private
(Item : in out FLTK.Widgets.Widget'Class);
- Origin : String := Ada.Directories.Containing_Directory
- (Ada.Directories.Full_Name (Ada.Command_Line.Command_Name));
-
- Logo : FLTK.Images.RGB.PNG.PNG_Image :=
- FLTK.Images.RGB.PNG.Forge.Create (Origin & "/../share/adapad/logo.png");
+ Logo : FLTK.Images.RGB.RGB_Image'Class := Images.Load_Logo;
end Windows;
+
diff --git a/to_do.txt b/to_do.txt
index 4750289..bc02ac0 100644
--- a/to_do.txt
+++ b/to_do.txt
@@ -3,6 +3,6 @@
To Do:
- suppress unnecessary left/right scrollbar
-- eliminate image/text_buffer runtime warnings
- add print functionality
+