Compare commits
38 Commits
v0.0.1
...
v1.0.0-pre.2
| Author | SHA1 | Date | |
|---|---|---|---|
| 55fcc019e4 | |||
| 5d758c2998 | |||
| 2e2c3d5e9e | |||
| 6477cde367 | |||
| 4f4ae80a14 | |||
| 8e9007fdfe | |||
| 6e8df4e0e1 | |||
| f4c76f7b5a | |||
| 5e7b41ab68 | |||
| 9a4b5ee9ef | |||
| ceb389de0d | |||
| 6faf33d944 | |||
| 46f0f31ed7 | |||
| e48d5f0d5b | |||
| dc014292cb | |||
| cb7c43f8af | |||
| 6bddc6a888 | |||
| 7d23ddbc11 | |||
| 9aba1a9ad8 | |||
| d344e9bbd3 | |||
| 33de5e981e | |||
| aefd962baf | |||
| 983f9b7393 | |||
| 13067902a8 | |||
| 345cd35f19 | |||
| c2b9571c2d | |||
| 3f7f216c92 | |||
| 70d50dda92 | |||
| 70e9ec2fc3 | |||
| 110b7ffca8 | |||
| 26fb19a7f5 | |||
| bcb1795218 | |||
| fb0d8adb8f | |||
| 2c4d27f915 | |||
| 96fb957991 | |||
| 0cbee45ad2 | |||
| 142a61a843 | |||
| f83f2051ef |
+131
-1
@@ -7,6 +7,126 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
## [1.0.0-pre.2] - 2026-03-06
|
||||
|
||||
### Fixed
|
||||
- Module `init` is no longer called if dependencies are not satisfied. The library handle is unloaded and a dep error is returned.
|
||||
|
||||
## [1.0.0-pre.1] - 2026-03-06
|
||||
|
||||
### Added
|
||||
- **Dependency System**: Full module dependency declaration and resolution
|
||||
- Modules declare dependencies via an exported `stk_mod_deps` symbol, no stk headers required in modules, only the memory layout contract must be respected (`{ char[64], char[32] }` sentinel-terminated array)
|
||||
- Version constraint operators: `=` (exact), `>=` (minimum, default), `^` (compatible — same major, greater or equal minor/patch)
|
||||
- Version defaults to `0.0.0` if not provided or unparseable
|
||||
- Dependency validation on `stk_init` and every `stk_poll` cycle
|
||||
- Topological sort with cycle detection, load and unload ordering enforced
|
||||
- All dependency failures logged before returning
|
||||
- **Cascade Unload**: When a module is removed, all dependents cascade unload automatically and are added to the pending queue. Cascades repeat until the loaded set is stable
|
||||
- **Pending Queue**: Modules that fail dependency validation are deferred. When their dependencies become available they are retried and loaded automatically. Entries are removed if their file is deleted
|
||||
|
||||
### Changed
|
||||
- `stk_set_module_dependencies_fn` renamed to `stk_set_module_deps_sym`. Deps are now an exported array symbol, not a function
|
||||
- Default deps symbol name changed from `stk_mod_dependencies` to `stk_mod_deps`
|
||||
- `stk_dep_t` added to public header `stk.h`
|
||||
- `stk_mod_t` field order changed to largest-to-smallest for correct memory alignment with zero padding waste
|
||||
- Dependency validation failure during `stk_init` no longer fatal, affected modules are deferred to the pending queue
|
||||
- Poll cycle now logs the full loaded module list as a single summary after any event that changes the loaded set, instead of logging each module individually
|
||||
|
||||
### Notes
|
||||
- This release marks Phase 1 complete: hot-reloading foundation with dependency management
|
||||
- Memory layout contract for `stk_mod_deps`: sentinel-terminated array of `{ char[64], char[32] }`. Modules do not need to include `stk.h`, define the struct inline or via your engine's own header as long as the layout matches
|
||||
|
||||
## [0.1.3] - 2026-02-25
|
||||
|
||||
### Added
|
||||
- **Module Metadata**: Optional metadata support for modules
|
||||
- `stk_mod_name` - human-readable module name
|
||||
- `stk_mod_version` - semantic version string
|
||||
- `stk_mod_description` - module description
|
||||
- All three are optional, missing symbols are not errors
|
||||
- Metadata stored in tight-packed arrays with index mappings
|
||||
- Setter functions to override default symbol names before `stk_init()`
|
||||
|
||||
## [0.1.2] - 2026-02-15
|
||||
|
||||
### Fixed
|
||||
- **Windows**
|
||||
- Module reload now works correctly, idk how it was broken
|
||||
- Temp directory creation fixed
|
||||
- `.tmp` directory now hidden via FILE_ATTRIBUTE_HIDDEN
|
||||
- Test build fixed
|
||||
- Force cmd.exe shell in gmake.mk
|
||||
- Fix bash syntax errors when running build.bat
|
||||
- **All Platforms**: platform_mkdir now checks if directory exists before creating
|
||||
|
||||
## [0.1.1] - 2026-02-14
|
||||
|
||||
### Fixed
|
||||
- **Logging**: Corrected log level severity order in enum
|
||||
- Reversed order so DEBUG (0) < INFO (1) < WARN (2) < ERROR (3)
|
||||
- Fixes filtering logic where ERROR/WARN were incorrectly blocked
|
||||
- Default INFO level now properly shows INFO, WARN, and ERROR while filtering DEBUG
|
||||
|
||||
## [0.1.0] - 2026-02-14
|
||||
|
||||
### Fixed
|
||||
- **C89 Compliance**: Removed stdint.h dependency (C99 feature)
|
||||
- Replaced all uint8_t with unsigned char throughout codebase
|
||||
- Ensures strict C89 compliance for maximum portability
|
||||
|
||||
### Added
|
||||
- **Flags system**: Centralized bitfield for boolean state and settings
|
||||
- Replaces stk_initialized with stk_flags for efficient memory usage
|
||||
- Single byte packs all boolean flags (STK_FLAG_INITIALIZED, STK_FLAG_LOGGING_ENABLED)
|
||||
- Runtime-changeable logging control via stk_set_logging_enabled()
|
||||
|
||||
- **Enhanced logging system**: Complete rewrite with modern features
|
||||
- Log levels: ERROR, WARN, INFO, DEBUG with runtime filtering
|
||||
- Timestamps: yyyy-mm-dd HH:MM:SS.mmm format (platform-specific implementation)
|
||||
- Configurable log output stream (stk_set_log_output)
|
||||
- Configurable log prefix (stk_set_log_prefix, defaults to "stk")
|
||||
- Configurable minimum log level (stk_set_log_level, defaults to INFO)
|
||||
- Platform abstraction for timestamps (GetLocalTime on Windows, gettimeofday on POSIX)
|
||||
|
||||
### Changed
|
||||
- **BREAKING**: stk_log() signature changed from stk_log(FILE *fp, ...) to stk_log(stk_log_level_t level, ...)
|
||||
- **BREAKING**: All uint8_t types replaced with unsigned char
|
||||
- All internal STK messages now use appropriate log levels
|
||||
- Setting log output to NULL disabls logging
|
||||
|
||||
### Notes
|
||||
- This release completes Phase 1 logging improvements
|
||||
- Dependency management still in progress for Phase 1 completion
|
||||
|
||||
## [0.0.4] - 2026-02-11
|
||||
|
||||
### Fixed
|
||||
- **Linux**: Fixed segfault from invalid module indices during extremely rapid file changes
|
||||
- Added validation check to skip stale UNLOAD/RELOAD events for already-unloaded modules
|
||||
- Prevents is_mod_loaded() returning -1 from being used as array index (SIZE_MAX)
|
||||
- Fixed event count mismatch where loops would run more iterations than valid indices populated
|
||||
- Completes the Linux hot-reload stability fixes from v0.0.2
|
||||
|
||||
## [0.0.3] - 2026-02-10
|
||||
|
||||
### Fixed
|
||||
- **Compilation**: Fixed GCC `-Wrestrict` warning in Linux directory watching code by replacing `strncpy` with `memmove` for overlapping memory operations
|
||||
- Ensures clean compilation without warnings while maintaining identical runtime behavior
|
||||
- Uses semantically correct function for moving memory within the same buffer
|
||||
|
||||
## [0.0.2] - 2026-02-09
|
||||
|
||||
### Fixed
|
||||
- **Linux**: Fixed segfaults during rapid module reloads when file changes are detected in quick succession
|
||||
- Enabled file readiness checks on Linux (previously only used on Windows/BSD) to prevent loading partially-written shared libraries
|
||||
- Fixed inotify event deduplication to actually remove duplicate events instead of just marking them
|
||||
- Reordered reload operations to only unload old module after successfully copying new version, preventing invalid state when copy fails
|
||||
- **All platforms**: Improved reload safety by deferring module unload until after successful file copy
|
||||
|
||||
### Changed
|
||||
- Made `is_file_ready()` check available on all Unix platforms (was previously excluded on Linux)
|
||||
|
||||
## [0.0.1] - 2026-02-01
|
||||
|
||||
### Added
|
||||
@@ -25,5 +145,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
- Dependency management and versioning not yet implemented
|
||||
- API is unstable and subject to change in future releases
|
||||
|
||||
[Unreleased]: https://github.com/anth64/stk/compare/v0.0.1...HEAD
|
||||
[Unreleased]: https://github.com/anth64/stk/compare/v1.0.0-pre.2...HEAD
|
||||
[1.0.0-pre.2]: https://github.com/anth64/stk/compare/v1.0.0-pre.1...v1.0.0-pre.2
|
||||
[1.0.0-pre.1]: https://github.com/anth64/stk/compare/v0.1.3...v1.0.0-pre.1
|
||||
[0.1.3]: https://github.com/anth64/stk/releases/tag/v0.1.3
|
||||
[0.1.2]: https://github.com/anth64/stk/releases/tag/v0.1.2
|
||||
[0.1.1]: https://github.com/anth64/stk/releases/tag/v0.1.1
|
||||
[0.1.1]: https://github.com/anth64/stk/releases/tag/v0.1.1
|
||||
[0.1.0]: https://github.com/anth64/stk/releases/tag/v0.1.0
|
||||
[0.0.4]: https://github.com/anth64/stk/releases/tag/v0.0.4
|
||||
[0.0.3]: https://github.com/anth64/stk/releases/tag/v0.0.3
|
||||
[0.0.2]: https://github.com/anth64/stk/releases/tag/v0.0.2
|
||||
[0.0.1]: https://github.com/anth64/stk/releases/tag/v0.0.1
|
||||
|
||||
@@ -131,6 +131,42 @@ cc -shared -o my_module.dll my_module.c
|
||||
|
||||
Place the compiled module in the `mods/` directory (default), and `stk` will automatically load it and watch for changes.
|
||||
|
||||
### Module Metadata
|
||||
|
||||
Modules can optionally export metadata functions:
|
||||
|
||||
```c
|
||||
const char *stk_mod_name(void) { return "My Module"; }
|
||||
const char *stk_mod_version(void) { return "1.2.0"; }
|
||||
const char *stk_mod_description(void) { return "Does something useful"; }
|
||||
```
|
||||
|
||||
All three are optional. Version defaults to `0.0.0` if not exported or unparseable.
|
||||
|
||||
### Declaring Dependencies
|
||||
|
||||
Modules declare dependencies via an exported sentinel-terminated array. No stk headers are required in the module. The only requirement is that the memory layout matches: `{ char[64], char[32] }`.
|
||||
|
||||
```c
|
||||
typedef struct {
|
||||
char id[64];
|
||||
char version[32];
|
||||
} dep_t;
|
||||
|
||||
dep_t stk_mod_deps[] = {
|
||||
{ "physics", ">=2.0.0" },
|
||||
{ "renderer", "^1.0.0" },
|
||||
{ "", "" } /* sentinel */
|
||||
};
|
||||
```
|
||||
|
||||
Version constraint operators:
|
||||
- `=1.0.0` exact match
|
||||
- `>=2.0.0` minimum version, also the default if no operator is specified
|
||||
- `^1.0.0` same major, greater or equal minor/patch
|
||||
|
||||
If a dependency is removed at runtime, all affected modules are unloaded and queued. When the dependency comes back, they load automatically.
|
||||
|
||||
### Configuration
|
||||
|
||||
```c
|
||||
@@ -146,6 +182,18 @@ stk_set_module_init_fn("my_init");
|
||||
/* Set custom shutdown function name (default: "stk_mod_shutdown") */
|
||||
stk_set_module_shutdown_fn("my_shutdown");
|
||||
|
||||
/* Set function name to get module name (default: "stk_mod_name") */
|
||||
stk_set_module_name_fn("my_mod_name");
|
||||
|
||||
/* Set function name to get module version (default: "stk_mod_version") */
|
||||
stk_set_module_version_fn("my_mod_version");
|
||||
|
||||
/* Set function name to get module description (default: "stk_mod_description") */
|
||||
stk_set_module_description_fn("my_mod_description");
|
||||
|
||||
/* Set deps array symbol name (default: "stk_mod_deps") */
|
||||
stk_set_module_deps_sym("my_mod_deps");
|
||||
|
||||
/*
|
||||
* All the above functions must be called before stk_init()
|
||||
* if the defaults need to be changed.
|
||||
@@ -156,7 +204,7 @@ stk_init();
|
||||
### API Reference
|
||||
|
||||
#### Initialization
|
||||
- `uint8_t stk_init(void)` - Initialize stk, returns `STK_INIT_SUCCESS` on success
|
||||
- `unsigned char stk_init(void)` - Initialize stk, returns `STK_INIT_SUCCESS` on success
|
||||
- `void stk_shutdown(void)` - Shutdown and cleanup all modules
|
||||
|
||||
#### Runtime
|
||||
@@ -168,24 +216,40 @@ stk_init();
|
||||
- `void stk_set_tmp_dir_name(const char *name)` - Set temp directory name
|
||||
- `void stk_set_module_init_fn(const char *name)` - Set module init function name
|
||||
- `void stk_set_module_shutdown_fn(const char *name)` - Set module shutdown function name
|
||||
- `void stk_set_module_name_fn(const char *name);` - Set module name function name
|
||||
- `void stk_set_module_version_fn(const char *name);` - Set module version function name
|
||||
- `void stk_set_module_description_fn(const char *name);` - Set module description function name
|
||||
- `void stk_set_module_deps_sym(const char *name)` - Set module deps array symbol name (default: `stk_mod_deps`)
|
||||
|
||||
#### Logging
|
||||
- `void stk_set_logging_enabled(unsigned char enabled)` - Enable/disable all logging
|
||||
- `unsigned char stk_is_logging_enabled(void)` - Query logging state
|
||||
- `void stk_set_log_output(FILE *fp)` - Set log output stream (default: stdout, NULL disables)
|
||||
- `void stk_set_log_prefix(const char *prefix)` - Set log prefix (default: "stk")
|
||||
- `void stk_set_log_level(stk_log_level_t level)` - Set minimum log level (default: INFO)
|
||||
|
||||
**Log Levels:** `STK_LOG_ERROR`, `STK_LOG_WARN`, `STK_LOG_INFO`, `STK_LOG_DEBUG`
|
||||
|
||||
---
|
||||
|
||||
## Project Status
|
||||
|
||||
**Current Version:** 0.0.1 (Pre-release)
|
||||
|
||||
This is an early release proving the core hot-reload foundation. Phase 1 is still in progress.
|
||||
**Current Version:** 1.0.0-pre.1
|
||||
|
||||
### What Works
|
||||
- Cross-platform module loading and hot-reloading
|
||||
- File watching (inotify/kqueue/FindFirstFile)
|
||||
- Basic error handling
|
||||
- Robust hot-reload even during extremely rapid file changes
|
||||
- Enhanced logging with levels, timestamps, and filtering
|
||||
- Runtime-configurable logging behavior
|
||||
- Optional module metadata (name, version, description)
|
||||
- Dependency declaration, validation, and versioning
|
||||
- Cascade unload when dependencies are removed
|
||||
- Pending queue with automatic retry when deps become available
|
||||
- Topological sort with cycle detection
|
||||
|
||||
### In Progress (Phase 1)
|
||||
- Complete logging system (log levels, verbosity, output configuration)
|
||||
- Module metadata (name, version, description)
|
||||
- Dependency management and versioning
|
||||
### Phase 2
|
||||
- WASM module support
|
||||
|
||||
See [CHANGELOG.md](CHANGELOG.md) for detailed release notes.
|
||||
|
||||
|
||||
+29
-2
@@ -2,12 +2,16 @@
|
||||
#define STK_H
|
||||
|
||||
#include "stk_version.h"
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/* Buffers */
|
||||
#define STK_LOG_PREFIX_BUFFER 64
|
||||
#define STK_MOD_DEP_OPERATOR_BUFFER 3
|
||||
#define STK_MOD_DESC_BUFFER 256
|
||||
#define STK_MOD_DIR_BUFFER 256
|
||||
#define STK_MOD_ID_BUFFER 64
|
||||
#define STK_MOD_NAME_BUFFER 128
|
||||
#define STK_MOD_VERSION_BUFFER 32
|
||||
#define STK_PATH_MAX 256
|
||||
#define STK_PATH_MAX_OS 4096
|
||||
|
||||
@@ -23,6 +27,9 @@
|
||||
#define STK_MOD_LIBRARY_LOAD_ERROR 2
|
||||
#define STK_MOD_SYMBOL_NOT_FOUND_ERROR 3
|
||||
#define STK_MOD_REALLOC_FAILURE 4
|
||||
#define STK_MOD_DEP_NOT_FOUND_ERROR 5
|
||||
#define STK_MOD_DEP_VERSION_MISMATCH_ERROR 6
|
||||
#define STK_MOD_DEP_CIRCULAR_ERROR 7
|
||||
|
||||
/* Platform return codes */
|
||||
#define STK_PLATFORM_OPERATION_SUCCESS 0
|
||||
@@ -31,6 +38,15 @@
|
||||
#define STK_PLATFORM_REMOVE_DIR_ERROR 3
|
||||
#define STK_PLATFORM_REMOVE_FILE_ERROR 4
|
||||
|
||||
/* Settings flags */
|
||||
#define STK_FLAG_INITIALIZED 0x01
|
||||
#define STK_FLAG_LOGGING_ENABLED 0x02
|
||||
|
||||
/* Dependency constraint operators */
|
||||
#define STK_DEP_MIN 0
|
||||
#define STK_DEP_EXACT 1
|
||||
#define STK_DEP_COMPAT 2
|
||||
|
||||
#if defined(__linux__) || defined(_WIN32)
|
||||
#define STK_EVENT_BUFFER 4096
|
||||
#endif
|
||||
@@ -55,7 +71,12 @@ typedef enum {
|
||||
STK_MOD_RELOAD
|
||||
} stk_module_event_t;
|
||||
|
||||
uint8_t stk_init(void);
|
||||
typedef struct {
|
||||
char id[STK_MOD_ID_BUFFER];
|
||||
char version[STK_MOD_VERSION_BUFFER];
|
||||
} stk_dep_t;
|
||||
|
||||
unsigned char stk_init(void);
|
||||
void stk_shutdown(void);
|
||||
size_t stk_module_count(void);
|
||||
size_t stk_poll(void);
|
||||
@@ -63,6 +84,12 @@ void stk_set_mod_dir(const char *path);
|
||||
void stk_set_tmp_dir_name(const char *name);
|
||||
void stk_set_module_init_fn(const char *name);
|
||||
void stk_set_module_shutdown_fn(const char *name);
|
||||
void stk_set_logging_enabled(unsigned char enabled);
|
||||
void stk_set_module_name_fn(const char *name);
|
||||
void stk_set_module_version_fn(const char *name);
|
||||
void stk_set_module_description_fn(const char *name);
|
||||
void stk_set_module_deps_sym(const char *name);
|
||||
unsigned char stk_is_logging_enabled(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
+12
-1
@@ -7,7 +7,18 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void stk_log(FILE *fp, const char *fmt, ...);
|
||||
typedef enum {
|
||||
STK_LOG_DEBUG,
|
||||
STK_LOG_INFO,
|
||||
STK_LOG_WARN,
|
||||
STK_LOG_ERROR
|
||||
} stk_log_level_t;
|
||||
|
||||
void stk_set_log_output(FILE *fp);
|
||||
void stk_set_log_prefix(const char *prefix);
|
||||
void stk_set_log_level(stk_log_level_t min_level);
|
||||
|
||||
void stk_log(stk_log_level_t level, const char *fmt, ...);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
#ifndef STK_VERSION_H
|
||||
#define STK_VERSION_H
|
||||
|
||||
#define STK_VERSION_MAJOR 0
|
||||
#define STK_VERSION_MAJOR 1
|
||||
#define STK_VERSION_MINOR 0
|
||||
#define STK_VERSION_PATCH 1
|
||||
#define STK_VERSION_PATCH 0
|
||||
|
||||
#define STK_STRINGIFY_HELPER(x) #x
|
||||
#define STK_STRINGIFY(x) STK_STRINGIFY_HELPER(x)
|
||||
|
||||
+541
-109
@@ -1,6 +1,6 @@
|
||||
#include "platform.h"
|
||||
#include "stk.h"
|
||||
#include <stdint.h>
|
||||
#include "stk_log.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
@@ -9,23 +9,115 @@
|
||||
typedef int (*stk_init_mod_func)(void);
|
||||
typedef void (*stk_shutdown_mod_func)(void);
|
||||
|
||||
typedef struct {
|
||||
unsigned char major;
|
||||
unsigned char minor;
|
||||
unsigned char patch;
|
||||
char op;
|
||||
} stk_version_t;
|
||||
|
||||
typedef struct {
|
||||
char desc[STK_MOD_DESC_BUFFER];
|
||||
char name[STK_MOD_NAME_BUFFER];
|
||||
char id[STK_MOD_ID_BUFFER];
|
||||
char version[STK_MOD_VERSION_BUFFER];
|
||||
void *handle;
|
||||
stk_init_mod_func init;
|
||||
stk_shutdown_mod_func shutdown;
|
||||
stk_dep_t *deps;
|
||||
size_t dep_count;
|
||||
} stk_mod_t;
|
||||
|
||||
void *platform_load_library(const char *path);
|
||||
void platform_unload_library(void *handle);
|
||||
void *platform_get_symbol(void *handle, const char *symbol);
|
||||
|
||||
char (*stk_module_ids)[STK_MOD_ID_BUFFER] = NULL;
|
||||
void **stk_handles = NULL;
|
||||
stk_init_mod_func *stk_inits = NULL;
|
||||
stk_shutdown_mod_func *stk_shutdowns = NULL;
|
||||
stk_mod_t *stk_modules = NULL;
|
||||
|
||||
extern uint8_t stk_initialized;
|
||||
extern unsigned char stk_flags;
|
||||
|
||||
static char stk_mod_init_name[STK_MOD_FUNC_NAME_BUFFER] = "stk_mod_init";
|
||||
static char stk_mod_shutdown_name[STK_MOD_FUNC_NAME_BUFFER] =
|
||||
"stk_mod_shutdown";
|
||||
static char stk_mod_name_fn[STK_MOD_FUNC_NAME_BUFFER] = "stk_mod_name";
|
||||
static char stk_mod_version_fn[STK_MOD_FUNC_NAME_BUFFER] = "stk_mod_version";
|
||||
static char stk_mod_description_fn[STK_MOD_FUNC_NAME_BUFFER] =
|
||||
"stk_mod_description";
|
||||
static char stk_mod_deps_sym[STK_MOD_FUNC_NAME_BUFFER] = "stk_mod_deps";
|
||||
|
||||
size_t module_count = 0;
|
||||
|
||||
static char (*stk_pending)[STK_PATH_MAX_OS] = NULL;
|
||||
static size_t stk_pending_count = 0;
|
||||
|
||||
void stk_pending_free(void)
|
||||
{
|
||||
if (stk_pending) {
|
||||
free(stk_pending);
|
||||
stk_pending = NULL;
|
||||
}
|
||||
|
||||
stk_pending_count = 0;
|
||||
}
|
||||
|
||||
static stk_version_t stk_parse_version(const char *str)
|
||||
{
|
||||
stk_version_t v;
|
||||
v.major = 0;
|
||||
v.minor = 0;
|
||||
v.patch = 0;
|
||||
v.op = '>';
|
||||
|
||||
if (!str || !*str)
|
||||
return v;
|
||||
|
||||
if (*str == '=' && *(str + 1) != '=') {
|
||||
v.op = '=';
|
||||
str++;
|
||||
} else if (*str == '>' && *(str + 1) == '=') {
|
||||
v.op = '>';
|
||||
str += 2;
|
||||
} else if (*str == '^') {
|
||||
v.op = '^';
|
||||
str++;
|
||||
}
|
||||
|
||||
v.major = (unsigned char)strtol(str, (char **)&str, 10);
|
||||
if (*str == '.')
|
||||
str++;
|
||||
v.minor = (unsigned char)strtol(str, (char **)&str, 10);
|
||||
if (*str == '.')
|
||||
str++;
|
||||
v.patch = (unsigned char)strtol(str, NULL, 10);
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
static int stk_compare_version(stk_version_t a, stk_version_t b)
|
||||
{
|
||||
if (a.major != b.major)
|
||||
return a.major - b.major;
|
||||
if (a.minor != b.minor)
|
||||
return a.minor - b.minor;
|
||||
return a.patch - b.patch;
|
||||
}
|
||||
|
||||
static int stk_validate_constraint(const char *constraint, const char *loaded)
|
||||
{
|
||||
stk_version_t req = stk_parse_version(constraint);
|
||||
stk_version_t have = stk_parse_version(loaded);
|
||||
int cmp = stk_compare_version(have, req);
|
||||
|
||||
switch (req.op) {
|
||||
case '=':
|
||||
return cmp == 0;
|
||||
case '^':
|
||||
return have.major == req.major && cmp >= 0;
|
||||
default:
|
||||
return cmp >= 0;
|
||||
}
|
||||
}
|
||||
|
||||
size_t stk_module_count(void) { return module_count; }
|
||||
|
||||
void extract_module_id(const char *path, char *out_id)
|
||||
@@ -43,7 +135,7 @@ void extract_module_id(const char *path, char *out_id)
|
||||
*dot = '\0';
|
||||
}
|
||||
|
||||
uint8_t is_valid_module_file(const char *filename)
|
||||
unsigned char is_valid_module_file(const char *filename)
|
||||
{
|
||||
const char *ext;
|
||||
size_t name_len;
|
||||
@@ -65,63 +157,294 @@ int is_mod_loaded(const char *module_name)
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < module_count; i++)
|
||||
if (strncmp(stk_module_ids[i], module_name,
|
||||
if (strncmp(stk_modules[i].id, module_name,
|
||||
STK_MOD_ID_BUFFER) == 0)
|
||||
return i;
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
uint8_t stk_module_load(const char *path, int index)
|
||||
unsigned char stk_validate_dependencies(size_t count)
|
||||
{
|
||||
size_t i, d;
|
||||
int found;
|
||||
unsigned char result = STK_MOD_INIT_SUCCESS;
|
||||
|
||||
for (i = 0; i < count; i++) {
|
||||
if (stk_modules[i].dep_count == 0)
|
||||
continue;
|
||||
|
||||
for (d = 0; d < stk_modules[i].dep_count; d++) {
|
||||
found = is_mod_loaded(stk_modules[i].deps[d].id);
|
||||
if (found < 0) {
|
||||
stk_log(STK_LOG_ERROR,
|
||||
"Module '%s' requires '%s'",
|
||||
stk_modules[i].id,
|
||||
stk_modules[i].deps[d].id);
|
||||
result = STK_MOD_DEP_NOT_FOUND_ERROR;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!stk_modules[i].deps[d].version[0])
|
||||
continue;
|
||||
|
||||
if (!stk_validate_constraint(
|
||||
stk_modules[i].deps[d].version,
|
||||
stk_modules[found].version)) {
|
||||
stk_log(
|
||||
STK_LOG_ERROR,
|
||||
"Module '%s' requires '%s' %s but has %s",
|
||||
stk_modules[i].id,
|
||||
stk_modules[i].deps[d].id,
|
||||
stk_modules[i].deps[d].version,
|
||||
stk_modules[found].version);
|
||||
result = STK_MOD_DEP_VERSION_MISMATCH_ERROR;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
unsigned char stk_topo_sort(size_t count, size_t *order)
|
||||
{
|
||||
size_t *in_degree = NULL;
|
||||
size_t *queue = NULL;
|
||||
size_t head, tail, sorted, i, d;
|
||||
int dep_index;
|
||||
unsigned char result = STK_MOD_INIT_SUCCESS;
|
||||
|
||||
if (count == 0)
|
||||
goto done;
|
||||
|
||||
in_degree = malloc(count * sizeof(size_t));
|
||||
queue = malloc(count * sizeof(size_t));
|
||||
|
||||
if (!in_degree || !queue) {
|
||||
result = STK_MOD_REALLOC_FAILURE;
|
||||
goto done;
|
||||
}
|
||||
|
||||
for (i = 0; i < count; i++)
|
||||
in_degree[i] = 0;
|
||||
|
||||
for (i = 0; i < count; i++)
|
||||
for (d = 0; d < stk_modules[i].dep_count; d++) {
|
||||
dep_index = is_mod_loaded(stk_modules[i].deps[d].id);
|
||||
if (dep_index >= 0)
|
||||
in_degree[i]++;
|
||||
}
|
||||
|
||||
head = tail = sorted = 0;
|
||||
|
||||
for (i = 0; i < count; i++)
|
||||
if (in_degree[i] == 0)
|
||||
queue[tail++] = i;
|
||||
|
||||
while (head < tail) {
|
||||
size_t mod = queue[head++];
|
||||
order[sorted++] = mod;
|
||||
|
||||
for (i = 0; i < count; i++) {
|
||||
for (d = 0; d < stk_modules[i].dep_count; d++) {
|
||||
dep_index =
|
||||
is_mod_loaded(stk_modules[i].deps[d].id);
|
||||
if (dep_index != (int)mod)
|
||||
continue;
|
||||
if (--in_degree[i] == 0)
|
||||
queue[tail++] = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (sorted != count) {
|
||||
size_t j;
|
||||
int in_order;
|
||||
for (i = 0; i < count; i++) {
|
||||
in_order = 0;
|
||||
for (j = 0; j < sorted; j++) {
|
||||
if (order[j] == i) {
|
||||
in_order = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!in_order)
|
||||
stk_log(STK_LOG_ERROR,
|
||||
"Circular dependency detected with %s",
|
||||
stk_modules[i].id);
|
||||
}
|
||||
|
||||
result = STK_MOD_DEP_CIRCULAR_ERROR;
|
||||
}
|
||||
done:
|
||||
if (in_degree)
|
||||
free(in_degree);
|
||||
if (queue)
|
||||
free(queue);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
unsigned char stk_module_load(const char *path, int index)
|
||||
{
|
||||
void *handle;
|
||||
stk_init_mod_func init_func;
|
||||
stk_shutdown_mod_func shutdown_func;
|
||||
char module_id[STK_MOD_ID_BUFFER];
|
||||
size_t len;
|
||||
union {
|
||||
void *obj;
|
||||
stk_init_mod_func init_func;
|
||||
stk_shutdown_mod_func shutdown_func;
|
||||
const char *(*meta_func)(void);
|
||||
} u;
|
||||
size_t len;
|
||||
const char *meta_str;
|
||||
const stk_dep_t *deps;
|
||||
size_t dep_count;
|
||||
stk_dep_t *dep_arr;
|
||||
|
||||
handle = platform_load_library(path);
|
||||
if (!handle)
|
||||
return STK_MOD_LIBRARY_LOAD_ERROR;
|
||||
|
||||
u.obj = platform_get_symbol(handle, stk_mod_init_name);
|
||||
init_func = u.init_func;
|
||||
|
||||
u.obj = platform_get_symbol(handle, stk_mod_shutdown_name);
|
||||
shutdown_func = u.shutdown_func;
|
||||
|
||||
if (!init_func || !shutdown_func) {
|
||||
if (!u.obj) {
|
||||
platform_unload_library(handle);
|
||||
return STK_MOD_SYMBOL_NOT_FOUND_ERROR;
|
||||
}
|
||||
stk_modules[index].init = u.init_func;
|
||||
|
||||
u.obj = platform_get_symbol(handle, stk_mod_shutdown_name);
|
||||
if (!u.obj) {
|
||||
platform_unload_library(handle);
|
||||
return STK_MOD_SYMBOL_NOT_FOUND_ERROR;
|
||||
}
|
||||
stk_modules[index].shutdown = u.shutdown_func;
|
||||
|
||||
extract_module_id(path, module_id);
|
||||
|
||||
if (init_func() != STK_MOD_INIT_SUCCESS) {
|
||||
u.obj = platform_get_symbol(handle, stk_mod_deps_sym);
|
||||
if (u.obj) {
|
||||
const stk_dep_t *early_deps = (const stk_dep_t *)u.obj;
|
||||
size_t early_dep_count = 0;
|
||||
size_t di;
|
||||
int found;
|
||||
|
||||
while (early_deps[early_dep_count].id[0] != '\0')
|
||||
early_dep_count++;
|
||||
|
||||
for (di = 0; di < early_dep_count; di++) {
|
||||
found = is_mod_loaded(early_deps[di].id);
|
||||
if (found < 0) {
|
||||
platform_unload_library(handle);
|
||||
return STK_MOD_DEP_NOT_FOUND_ERROR;
|
||||
}
|
||||
if (early_deps[di].version[0] &&
|
||||
!stk_validate_constraint(
|
||||
early_deps[di].version,
|
||||
stk_modules[found].version)) {
|
||||
platform_unload_library(handle);
|
||||
return STK_MOD_DEP_VERSION_MISMATCH_ERROR;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (stk_modules[index].init() != STK_MOD_INIT_SUCCESS) {
|
||||
platform_unload_library(handle);
|
||||
return STK_MOD_INIT_FAILURE;
|
||||
}
|
||||
|
||||
stk_modules[index].handle = handle;
|
||||
|
||||
len = strlen(module_id);
|
||||
if (len >= STK_MOD_ID_BUFFER)
|
||||
len = STK_MOD_ID_BUFFER - 1;
|
||||
memcpy(stk_modules[index].id, module_id, len);
|
||||
stk_modules[index].id[len] = '\0';
|
||||
|
||||
memcpy(stk_module_ids[index], module_id, len);
|
||||
stk_module_ids[index][len] = '\0';
|
||||
stk_modules[index].name[0] = '\0';
|
||||
u.obj = platform_get_symbol(handle, stk_mod_name_fn);
|
||||
if (u.obj) {
|
||||
meta_str = u.meta_func();
|
||||
if (meta_str) {
|
||||
strncpy(stk_modules[index].name, meta_str,
|
||||
STK_MOD_NAME_BUFFER - 1);
|
||||
stk_modules[index].name[STK_MOD_NAME_BUFFER - 1] = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
stk_handles[index] = handle;
|
||||
stk_inits[index] = init_func;
|
||||
stk_shutdowns[index] = shutdown_func;
|
||||
stk_modules[index].version[0] = '\0';
|
||||
u.obj = platform_get_symbol(handle, stk_mod_version_fn);
|
||||
if (u.obj) {
|
||||
meta_str = u.meta_func();
|
||||
if (meta_str) {
|
||||
stk_version_t v = stk_parse_version(meta_str);
|
||||
if (v.major == 0 && v.minor == 0 && v.patch == 0 &&
|
||||
meta_str[0] != '0') {
|
||||
strncpy(stk_modules[index].version, "0.0.0",
|
||||
STK_MOD_VERSION_BUFFER - 1);
|
||||
} else {
|
||||
strncpy(stk_modules[index].version, meta_str,
|
||||
STK_MOD_VERSION_BUFFER - 1);
|
||||
}
|
||||
stk_modules[index].version[STK_MOD_VERSION_BUFFER - 1] =
|
||||
'\0';
|
||||
}
|
||||
}
|
||||
if (!stk_modules[index].version[0])
|
||||
strncpy(stk_modules[index].version, "0.0.0",
|
||||
STK_MOD_VERSION_BUFFER - 1);
|
||||
|
||||
stk_modules[index].desc[0] = '\0';
|
||||
u.obj = platform_get_symbol(handle, stk_mod_description_fn);
|
||||
if (u.obj) {
|
||||
meta_str = u.meta_func();
|
||||
if (meta_str) {
|
||||
strncpy(stk_modules[index].desc, meta_str,
|
||||
STK_MOD_DESC_BUFFER - 1);
|
||||
stk_modules[index].desc[STK_MOD_DESC_BUFFER - 1] = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
stk_modules[index].deps = NULL;
|
||||
stk_modules[index].dep_count = 0;
|
||||
u.obj = platform_get_symbol(handle, stk_mod_deps_sym);
|
||||
if (!u.obj)
|
||||
goto skip_deps;
|
||||
|
||||
deps = (const stk_dep_t *)u.obj;
|
||||
|
||||
dep_count = 0;
|
||||
while (deps[dep_count].id[0] != '\0')
|
||||
dep_count++;
|
||||
|
||||
if (dep_count == 0)
|
||||
goto skip_deps;
|
||||
|
||||
dep_arr = malloc(dep_count * sizeof(stk_dep_t));
|
||||
if (!dep_arr)
|
||||
goto skip_deps;
|
||||
|
||||
{
|
||||
size_t d;
|
||||
for (d = 0; d < dep_count; d++) {
|
||||
strncpy(dep_arr[d].id, deps[d].id,
|
||||
STK_MOD_ID_BUFFER - 1);
|
||||
dep_arr[d].id[STK_MOD_ID_BUFFER - 1] = '\0';
|
||||
strncpy(dep_arr[d].version, deps[d].version,
|
||||
STK_MOD_VERSION_BUFFER - 1);
|
||||
dep_arr[d].version[STK_MOD_VERSION_BUFFER - 1] = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
stk_modules[index].deps = dep_arr;
|
||||
stk_modules[index].dep_count = dep_count;
|
||||
|
||||
skip_deps:
|
||||
return STK_MOD_INIT_SUCCESS;
|
||||
}
|
||||
|
||||
uint8_t stk_module_load_init(const char *path, int index)
|
||||
unsigned char stk_module_load_init(const char *path, int index)
|
||||
{
|
||||
int result;
|
||||
result = stk_module_load(path, index);
|
||||
@@ -134,48 +457,51 @@ uint8_t stk_module_load_init(const char *path, int index)
|
||||
|
||||
void stk_module_unload(size_t index)
|
||||
{
|
||||
stk_shutdowns[index]();
|
||||
platform_unload_library(stk_handles[index]);
|
||||
stk_handles[index] = NULL;
|
||||
stk_inits[index] = NULL;
|
||||
stk_shutdowns[index] = NULL;
|
||||
stk_module_ids[index][0] = '\0';
|
||||
stk_modules[index].shutdown();
|
||||
platform_unload_library(stk_modules[index].handle);
|
||||
|
||||
stk_modules[index].handle = NULL;
|
||||
stk_modules[index].init = NULL;
|
||||
stk_modules[index].shutdown = NULL;
|
||||
stk_modules[index].id[0] = '\0';
|
||||
stk_modules[index].name[0] = '\0';
|
||||
stk_modules[index].version[0] = '\0';
|
||||
stk_modules[index].desc[0] = '\0';
|
||||
|
||||
if (stk_modules[index].deps) {
|
||||
free(stk_modules[index].deps);
|
||||
stk_modules[index].deps = NULL;
|
||||
}
|
||||
stk_modules[index].dep_count = 0;
|
||||
}
|
||||
|
||||
void stk_module_free_memory(void)
|
||||
{
|
||||
free(stk_module_ids);
|
||||
free(stk_handles);
|
||||
free(stk_inits);
|
||||
free(stk_shutdowns);
|
||||
|
||||
stk_module_ids = NULL;
|
||||
stk_handles = NULL;
|
||||
stk_inits = NULL;
|
||||
stk_shutdowns = NULL;
|
||||
if (stk_modules) {
|
||||
size_t i;
|
||||
for (i = 0; i < module_count; i++) {
|
||||
if (stk_modules[i].deps)
|
||||
free(stk_modules[i].deps);
|
||||
}
|
||||
free(stk_modules);
|
||||
stk_modules = NULL;
|
||||
}
|
||||
module_count = 0;
|
||||
stk_pending_free();
|
||||
}
|
||||
|
||||
uint8_t stk_module_init_memory(size_t capacity)
|
||||
unsigned char stk_module_init_memory(size_t capacity)
|
||||
{
|
||||
stk_module_ids = malloc(capacity * sizeof(*stk_module_ids));
|
||||
stk_handles = malloc(capacity * sizeof(void *));
|
||||
stk_inits = malloc(capacity * sizeof(stk_init_mod_func));
|
||||
stk_shutdowns = malloc(capacity * sizeof(stk_shutdown_mod_func));
|
||||
|
||||
if (!stk_module_ids || !stk_handles || !stk_inits || !stk_shutdowns) {
|
||||
stk_module_free_memory();
|
||||
stk_modules = malloc(capacity * sizeof(stk_mod_t));
|
||||
if (!stk_modules)
|
||||
return STK_INIT_MEMORY_ERROR;
|
||||
}
|
||||
|
||||
return STK_INIT_SUCCESS;
|
||||
}
|
||||
|
||||
uint8_t stk_module_realloc_memory(size_t new_capacity)
|
||||
unsigned char stk_module_realloc_memory(size_t new_capacity)
|
||||
{
|
||||
char (*new_module_ids)[STK_MOD_ID_BUFFER] = NULL;
|
||||
void **new_handles = NULL;
|
||||
stk_init_mod_func *new_inits = NULL;
|
||||
stk_shutdown_mod_func *new_shutdowns = NULL;
|
||||
stk_mod_t *new_modules;
|
||||
size_t i, copy_count;
|
||||
|
||||
if (new_capacity == 0) {
|
||||
@@ -183,62 +509,30 @@ uint8_t stk_module_realloc_memory(size_t new_capacity)
|
||||
return 0;
|
||||
}
|
||||
|
||||
new_module_ids = malloc(new_capacity * sizeof(*stk_module_ids));
|
||||
new_handles = malloc(new_capacity * sizeof(*new_handles));
|
||||
new_inits = malloc(new_capacity * sizeof(stk_init_mod_func));
|
||||
new_shutdowns = malloc(new_capacity * sizeof(stk_shutdown_mod_func));
|
||||
|
||||
if (!new_module_ids || !new_handles || !new_inits || !new_shutdowns) {
|
||||
if (new_module_ids)
|
||||
free(new_module_ids);
|
||||
|
||||
if (new_handles)
|
||||
free(new_handles);
|
||||
|
||||
if (new_inits)
|
||||
free(new_inits);
|
||||
|
||||
if (new_shutdowns)
|
||||
free(new_shutdowns);
|
||||
|
||||
new_modules = malloc(new_capacity * sizeof(stk_mod_t));
|
||||
if (!new_modules)
|
||||
return STK_MOD_REALLOC_FAILURE;
|
||||
}
|
||||
|
||||
copy_count =
|
||||
(module_count < new_capacity) ? module_count : new_capacity;
|
||||
|
||||
if (stk_module_ids) {
|
||||
for (i = 0; i < copy_count; i++) {
|
||||
strncpy(new_module_ids[i], stk_module_ids[i],
|
||||
STK_MOD_ID_BUFFER - 1);
|
||||
new_module_ids[i][STK_MOD_ID_BUFFER - 1] = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
if (stk_handles)
|
||||
memcpy(new_handles, stk_handles, copy_count * sizeof(void *));
|
||||
|
||||
if (stk_inits)
|
||||
memcpy(new_inits, stk_inits,
|
||||
copy_count * sizeof(stk_init_mod_func));
|
||||
|
||||
if (stk_shutdowns)
|
||||
memcpy(new_shutdowns, stk_shutdowns,
|
||||
copy_count * sizeof(stk_shutdown_mod_func));
|
||||
for (i = 0; i < copy_count; i++)
|
||||
new_modules[i] = stk_modules[i];
|
||||
|
||||
for (i = copy_count; i < new_capacity; i++) {
|
||||
new_module_ids[i][0] = '\0';
|
||||
new_handles[i] = NULL;
|
||||
new_inits[i] = NULL;
|
||||
new_shutdowns[i] = NULL;
|
||||
new_modules[i].handle = NULL;
|
||||
new_modules[i].init = NULL;
|
||||
new_modules[i].shutdown = NULL;
|
||||
new_modules[i].id[0] = '\0';
|
||||
new_modules[i].name[0] = '\0';
|
||||
new_modules[i].version[0] = '\0';
|
||||
new_modules[i].desc[0] = '\0';
|
||||
new_modules[i].deps = NULL;
|
||||
new_modules[i].dep_count = 0;
|
||||
}
|
||||
|
||||
stk_module_free_memory();
|
||||
|
||||
stk_module_ids = new_module_ids;
|
||||
stk_handles = new_handles;
|
||||
stk_inits = new_inits;
|
||||
stk_shutdowns = new_shutdowns;
|
||||
free(stk_modules);
|
||||
stk_modules = new_modules;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -252,20 +546,158 @@ void stk_module_unload_all(void)
|
||||
stk_module_free_memory();
|
||||
}
|
||||
|
||||
void stk_set_module_init_fn(const char *name)
|
||||
void stk_pending_add(const char *path)
|
||||
{
|
||||
if (!name || stk_initialized)
|
||||
char (*new_pending)[STK_PATH_MAX_OS];
|
||||
size_t i;
|
||||
|
||||
new_pending = malloc((stk_pending_count + 1) * sizeof(*stk_pending));
|
||||
if (!new_pending)
|
||||
return;
|
||||
|
||||
strncpy(stk_mod_init_name, name, STK_MOD_FUNC_NAME_BUFFER - 1);
|
||||
stk_mod_init_name[STK_MOD_FUNC_NAME_BUFFER - 1] = '\0';
|
||||
for (i = 0; i < stk_pending_count; i++)
|
||||
memcpy(new_pending[i], stk_pending[i], STK_PATH_MAX_OS);
|
||||
|
||||
free(stk_pending);
|
||||
stk_pending = new_pending;
|
||||
|
||||
strncpy(stk_pending[stk_pending_count], path, STK_PATH_MAX_OS - 1);
|
||||
stk_pending[stk_pending_count][STK_PATH_MAX_OS - 1] = '\0';
|
||||
stk_pending_count++;
|
||||
}
|
||||
|
||||
void stk_pending_remove(const char *id)
|
||||
{
|
||||
size_t i, write;
|
||||
char pending_id[STK_MOD_ID_BUFFER];
|
||||
|
||||
if (!stk_pending_count)
|
||||
return;
|
||||
|
||||
write = 0;
|
||||
for (i = 0; i < stk_pending_count; i++) {
|
||||
extract_module_id(stk_pending[i], pending_id);
|
||||
if (strncmp(pending_id, id, STK_MOD_ID_BUFFER) == 0)
|
||||
continue;
|
||||
if (write != i)
|
||||
memcpy(stk_pending[write], stk_pending[i],
|
||||
STK_PATH_MAX_OS);
|
||||
write++;
|
||||
}
|
||||
stk_pending_count = write;
|
||||
}
|
||||
|
||||
size_t stk_pending_retry(void)
|
||||
{
|
||||
size_t i, d, loaded = 0;
|
||||
unsigned char deps_satisfied;
|
||||
unsigned char result;
|
||||
void *handle;
|
||||
union {
|
||||
void *obj;
|
||||
const char *(*meta_func)(void);
|
||||
} u;
|
||||
const stk_dep_t *deps;
|
||||
size_t dep_count;
|
||||
int found;
|
||||
|
||||
if (!stk_pending_count)
|
||||
return 0;
|
||||
|
||||
for (i = 0; i < stk_pending_count; i++) {
|
||||
handle = platform_load_library(stk_pending[i]);
|
||||
if (!handle)
|
||||
continue;
|
||||
|
||||
u.obj = platform_get_symbol(handle, stk_mod_deps_sym);
|
||||
if (!u.obj) {
|
||||
platform_unload_library(handle);
|
||||
goto attempt_load;
|
||||
}
|
||||
|
||||
deps = (const stk_dep_t *)u.obj;
|
||||
dep_count = 0;
|
||||
while (deps[dep_count].id[0] != '\0')
|
||||
dep_count++;
|
||||
|
||||
deps_satisfied = 1;
|
||||
for (d = 0; d < dep_count; d++) {
|
||||
found = is_mod_loaded(deps[d].id);
|
||||
if (found < 0) {
|
||||
deps_satisfied = 0;
|
||||
break;
|
||||
}
|
||||
if (deps[d].version[0] &&
|
||||
!stk_validate_constraint(
|
||||
deps[d].version, stk_modules[found].version)) {
|
||||
deps_satisfied = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
platform_unload_library(handle);
|
||||
|
||||
if (!deps_satisfied)
|
||||
continue;
|
||||
|
||||
attempt_load:
|
||||
if (stk_module_realloc_memory(module_count + 1) != 0)
|
||||
continue;
|
||||
|
||||
result = stk_module_load(stk_pending[i], module_count);
|
||||
if (result != STK_MOD_INIT_SUCCESS)
|
||||
continue;
|
||||
|
||||
module_count++;
|
||||
loaded++;
|
||||
|
||||
memcpy(stk_pending[i], stk_pending[stk_pending_count - 1],
|
||||
STK_PATH_MAX_OS);
|
||||
stk_pending_count--;
|
||||
i--;
|
||||
}
|
||||
|
||||
if (stk_pending_count == 0)
|
||||
stk_pending_free();
|
||||
|
||||
return loaded;
|
||||
}
|
||||
|
||||
static void stk_set_fn_name(char *dst, const char *name)
|
||||
{
|
||||
if (!name || (stk_flags & STK_FLAG_INITIALIZED))
|
||||
return;
|
||||
|
||||
strncpy(dst, name, STK_MOD_FUNC_NAME_BUFFER - 1);
|
||||
dst[STK_MOD_FUNC_NAME_BUFFER - 1] = '\0';
|
||||
}
|
||||
|
||||
void stk_set_module_init_fn(const char *name)
|
||||
{
|
||||
stk_set_fn_name(stk_mod_init_name, name);
|
||||
}
|
||||
|
||||
void stk_set_module_shutdown_fn(const char *name)
|
||||
{
|
||||
if (!name || stk_initialized)
|
||||
return;
|
||||
stk_set_fn_name(stk_mod_shutdown_name, name);
|
||||
}
|
||||
|
||||
strncpy(stk_mod_shutdown_name, name, STK_MOD_FUNC_NAME_BUFFER - 1);
|
||||
stk_mod_shutdown_name[STK_MOD_FUNC_NAME_BUFFER - 1] = '\0';
|
||||
void stk_set_module_name_fn(const char *name)
|
||||
{
|
||||
stk_set_fn_name(stk_mod_name_fn, name);
|
||||
}
|
||||
|
||||
void stk_set_module_version_fn(const char *name)
|
||||
{
|
||||
stk_set_fn_name(stk_mod_version_fn, name);
|
||||
}
|
||||
|
||||
void stk_set_module_description_fn(const char *name)
|
||||
{
|
||||
stk_set_fn_name(stk_mod_description_fn, name);
|
||||
}
|
||||
|
||||
void stk_set_module_deps_sym(const char *name)
|
||||
{
|
||||
stk_set_fn_name(stk_mod_deps_sym, name);
|
||||
}
|
||||
|
||||
+165
-117
@@ -1,39 +1,34 @@
|
||||
#include "stk.h"
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifndef _WIN32
|
||||
#include <dirent.h>
|
||||
#include <dlfcn.h>
|
||||
#include <sys/file.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/time.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#if defined(__linux__)
|
||||
#include <dirent.h>
|
||||
#include <dlfcn.h>
|
||||
#include <sys/inotify.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#elif defined(_WIN32)
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <dirent.h>
|
||||
#include <dlfcn.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/event.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
int is_mod_loaded(const char *module_name);
|
||||
uint8_t is_valid_module_file(const char *filename);
|
||||
unsigned char is_valid_module_file(const char *filename);
|
||||
void extract_module_id(const char *path, char *out_id);
|
||||
|
||||
#ifndef __linux__
|
||||
static uint8_t is_file_ready(const char *dir_path, const char *filename)
|
||||
static unsigned char is_file_ready(const char *dir_path, const char *filename)
|
||||
{
|
||||
char full_path[STK_PATH_MAX_OS];
|
||||
#ifdef _WIN32
|
||||
@@ -80,6 +75,7 @@ static uint8_t is_file_ready(const char *dir_path, const char *filename)
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifndef __linux__
|
||||
typedef struct {
|
||||
char filename[STK_PATH_MAX];
|
||||
#ifdef _WIN32
|
||||
@@ -108,12 +104,32 @@ typedef struct {
|
||||
} platform_watch_context_t;
|
||||
#endif
|
||||
|
||||
uint8_t platform_mkdir(const char *path)
|
||||
unsigned char platform_mkdir(const char *path)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
return CreateDirectoryA(path, NULL) ? STK_PLATFORM_OPERATION_SUCCESS
|
||||
: STK_PLATFORM_MKDIR_ERROR;
|
||||
DWORD attrib;
|
||||
|
||||
attrib = GetFileAttributesA(path);
|
||||
|
||||
if (attrib != INVALID_FILE_ATTRIBUTES &&
|
||||
(attrib & FILE_ATTRIBUTE_DIRECTORY))
|
||||
return STK_PLATFORM_OPERATION_SUCCESS;
|
||||
|
||||
if (!CreateDirectoryA(path, NULL))
|
||||
return STK_PLATFORM_MKDIR_ERROR;
|
||||
|
||||
if (strrchr(path, '\\') && *(strrchr(path, '\\') + 1) == '.')
|
||||
SetFileAttributesA(path, FILE_ATTRIBUTE_HIDDEN);
|
||||
else if (!strrchr(path, '\\') && path[0] == '.')
|
||||
SetFileAttributesA(path, FILE_ATTRIBUTE_HIDDEN);
|
||||
|
||||
return STK_PLATFORM_OPERATION_SUCCESS;
|
||||
#else
|
||||
struct stat st;
|
||||
|
||||
if (stat(path, &st) == 0 && S_ISDIR(st.st_mode))
|
||||
return STK_PLATFORM_OPERATION_SUCCESS;
|
||||
|
||||
return mkdir(path, 0755) == 0 ? STK_PLATFORM_OPERATION_SUCCESS
|
||||
: STK_PLATFORM_MKDIR_ERROR;
|
||||
#endif
|
||||
@@ -130,7 +146,7 @@ int platform_remove_file(const char *path)
|
||||
#endif
|
||||
}
|
||||
|
||||
uint8_t platform_copy_file(const char *from, const char *to)
|
||||
unsigned char platform_copy_file(const char *from, const char *to)
|
||||
{
|
||||
char buf[STK_PATH_MAX_OS];
|
||||
int ret = STK_PLATFORM_FILE_COPY_ERROR;
|
||||
@@ -146,7 +162,6 @@ uint8_t platform_copy_file(const char *from, const char *to)
|
||||
FILE *src = NULL, *dst = NULL;
|
||||
char tmp_path[STK_PATH_MAX_OS];
|
||||
size_t n;
|
||||
#ifndef __linux__
|
||||
char dir_path[STK_PATH_MAX_OS];
|
||||
const char *filename;
|
||||
|
||||
@@ -163,7 +178,6 @@ uint8_t platform_copy_file(const char *from, const char *to)
|
||||
|
||||
if (!is_file_ready(dir_path, filename))
|
||||
goto done;
|
||||
#endif
|
||||
|
||||
sprintf(tmp_path, "%s.tmp", to);
|
||||
|
||||
@@ -205,7 +219,7 @@ done:
|
||||
return ret;
|
||||
}
|
||||
|
||||
uint8_t platform_remove_dir(const char *path)
|
||||
unsigned char platform_remove_dir(const char *path)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
WIN32_FIND_DATAA fd;
|
||||
@@ -286,128 +300,127 @@ void *platform_get_symbol(void *h, const char *s)
|
||||
#endif
|
||||
}
|
||||
|
||||
char (*platform_directory_init_scan(const char *dir_path,
|
||||
size_t *out_count))[STK_PATH_MAX]
|
||||
{
|
||||
size_t count = 0, i = 0, name_len;
|
||||
char(*list)[STK_PATH_MAX] = NULL;
|
||||
char (*platform_directory_init_scan(const char *dir_path, size_t *out_count))
|
||||
[STK_PATH_MAX] {
|
||||
size_t count = 0, i = 0, name_len;
|
||||
char (*list)[STK_PATH_MAX] = NULL;
|
||||
#ifdef _WIN32
|
||||
WIN32_FIND_DATAA fd;
|
||||
HANDLE h;
|
||||
char s[STK_PATH_MAX_OS];
|
||||
WIN32_FIND_DATAA fd;
|
||||
HANDLE h;
|
||||
char s[STK_PATH_MAX_OS];
|
||||
|
||||
sprintf(s, "%s\\*", dir_path);
|
||||
h = FindFirstFileA(s, &fd);
|
||||
if (h == INVALID_HANDLE_VALUE)
|
||||
goto create_and_exit;
|
||||
sprintf(s, "%s\\*", dir_path);
|
||||
h = FindFirstFileA(s, &fd);
|
||||
if (h == INVALID_HANDLE_VALUE)
|
||||
goto create_and_exit;
|
||||
|
||||
do {
|
||||
if ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
|
||||
continue;
|
||||
if (is_valid_module_file(fd.cFileName))
|
||||
count++;
|
||||
} while (FindNextFileA(h, &fd));
|
||||
do {
|
||||
if ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
|
||||
continue;
|
||||
if (is_valid_module_file(fd.cFileName))
|
||||
count++;
|
||||
} while (FindNextFileA(h, &fd));
|
||||
|
||||
FindClose(h);
|
||||
FindClose(h);
|
||||
|
||||
if (count == 0)
|
||||
goto exit;
|
||||
if (count == 0)
|
||||
goto exit;
|
||||
|
||||
list = malloc(count * sizeof(*list));
|
||||
if (!list)
|
||||
goto exit;
|
||||
list = malloc(count * sizeof(*list));
|
||||
if (!list)
|
||||
goto exit;
|
||||
|
||||
h = FindFirstFileA(s, &fd);
|
||||
if (h == INVALID_HANDLE_VALUE)
|
||||
goto exit;
|
||||
h = FindFirstFileA(s, &fd);
|
||||
if (h == INVALID_HANDLE_VALUE)
|
||||
goto exit;
|
||||
|
||||
do {
|
||||
if ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
|
||||
continue;
|
||||
if (is_valid_module_file(fd.cFileName) && i < count) {
|
||||
name_len = strlen(fd.cFileName);
|
||||
if (name_len >= STK_PATH_MAX)
|
||||
name_len = STK_PATH_MAX - 1;
|
||||
do {
|
||||
if ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
|
||||
continue;
|
||||
if (is_valid_module_file(fd.cFileName) && i < count) {
|
||||
name_len = strlen(fd.cFileName);
|
||||
if (name_len >= STK_PATH_MAX)
|
||||
name_len = STK_PATH_MAX - 1;
|
||||
|
||||
memcpy(list[i], fd.cFileName, name_len);
|
||||
list[i][name_len] = '\0';
|
||||
i++;
|
||||
}
|
||||
} while (FindNextFileA(h, &fd));
|
||||
memcpy(list[i], fd.cFileName, name_len);
|
||||
list[i][name_len] = '\0';
|
||||
i++;
|
||||
}
|
||||
} while (FindNextFileA(h, &fd));
|
||||
|
||||
FindClose(h);
|
||||
goto exit;
|
||||
FindClose(h);
|
||||
goto exit;
|
||||
|
||||
create_and_exit:
|
||||
platform_mkdir(dir_path);
|
||||
exit:
|
||||
*out_count = i;
|
||||
return list;
|
||||
create_and_exit:
|
||||
platform_mkdir(dir_path);
|
||||
exit:
|
||||
*out_count = i;
|
||||
return list;
|
||||
#else
|
||||
DIR *d;
|
||||
struct dirent *e;
|
||||
struct stat st;
|
||||
char f[STK_PATH_MAX_OS];
|
||||
DIR *d;
|
||||
struct dirent *e;
|
||||
struct stat st;
|
||||
char f[STK_PATH_MAX_OS];
|
||||
|
||||
d = opendir(dir_path);
|
||||
if (!d)
|
||||
goto create_and_exit;
|
||||
d = opendir(dir_path);
|
||||
if (!d)
|
||||
goto create_and_exit;
|
||||
|
||||
count_loop:
|
||||
e = readdir(d);
|
||||
if (!e)
|
||||
goto count_done;
|
||||
count_loop:
|
||||
e = readdir(d);
|
||||
if (!e)
|
||||
goto count_done;
|
||||
|
||||
sprintf(f, "%s/%s", dir_path, e->d_name);
|
||||
if (!is_valid_module_file(e->d_name))
|
||||
goto count_loop;
|
||||
sprintf(f, "%s/%s", dir_path, e->d_name);
|
||||
if (!is_valid_module_file(e->d_name))
|
||||
goto count_loop;
|
||||
|
||||
if (stat(f, &st) != 0 || !S_ISREG(st.st_mode))
|
||||
goto count_loop;
|
||||
if (stat(f, &st) != 0 || !S_ISREG(st.st_mode))
|
||||
goto count_loop;
|
||||
|
||||
count++;
|
||||
goto count_loop;
|
||||
count++;
|
||||
goto count_loop;
|
||||
|
||||
count_done:
|
||||
if (count == 0)
|
||||
goto close_and_exit;
|
||||
count_done:
|
||||
if (count == 0)
|
||||
goto close_and_exit;
|
||||
|
||||
rewinddir(d);
|
||||
list = malloc(count * sizeof(*list));
|
||||
if (!list)
|
||||
goto close_and_exit;
|
||||
rewinddir(d);
|
||||
list = malloc(count * sizeof(*list));
|
||||
if (!list)
|
||||
goto close_and_exit;
|
||||
|
||||
fill_loop:
|
||||
e = readdir(d);
|
||||
if (!e || i >= count)
|
||||
goto close_and_exit;
|
||||
fill_loop:
|
||||
e = readdir(d);
|
||||
if (!e || i >= count)
|
||||
goto close_and_exit;
|
||||
|
||||
sprintf(f, "%s/%s", dir_path, e->d_name);
|
||||
if (!is_valid_module_file(e->d_name))
|
||||
goto fill_loop;
|
||||
sprintf(f, "%s/%s", dir_path, e->d_name);
|
||||
if (!is_valid_module_file(e->d_name))
|
||||
goto fill_loop;
|
||||
|
||||
if (stat(f, &st) != 0 || !S_ISREG(st.st_mode))
|
||||
goto fill_loop;
|
||||
if (stat(f, &st) != 0 || !S_ISREG(st.st_mode))
|
||||
goto fill_loop;
|
||||
|
||||
name_len = strlen(e->d_name);
|
||||
if (name_len >= STK_PATH_MAX) {
|
||||
name_len = STK_PATH_MAX - 1;
|
||||
}
|
||||
memcpy(list[i++], e->d_name, name_len);
|
||||
list[i - 1][name_len] = '\0';
|
||||
goto fill_loop;
|
||||
name_len = strlen(e->d_name);
|
||||
if (name_len >= STK_PATH_MAX) {
|
||||
name_len = STK_PATH_MAX - 1;
|
||||
}
|
||||
memcpy(list[i++], e->d_name, name_len);
|
||||
list[i - 1][name_len] = '\0';
|
||||
goto fill_loop;
|
||||
|
||||
create_and_exit:
|
||||
platform_mkdir(dir_path);
|
||||
*out_count = 0;
|
||||
return NULL;
|
||||
create_and_exit:
|
||||
platform_mkdir(dir_path);
|
||||
*out_count = 0;
|
||||
return NULL;
|
||||
|
||||
close_and_exit:
|
||||
closedir(d);
|
||||
*out_count = i;
|
||||
return list;
|
||||
close_and_exit:
|
||||
closedir(d);
|
||||
*out_count = i;
|
||||
return list;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
#if !defined(__linux__) && !defined(_WIN32)
|
||||
static void update_watches(platform_watch_context_t *ctx)
|
||||
@@ -665,7 +678,7 @@ stk_module_event_t *platform_directory_watch_check(
|
||||
int fd = (int)(long)handle;
|
||||
char buf[STK_EVENT_BUFFER];
|
||||
ssize_t len;
|
||||
size_t index = 0, count = 0, i;
|
||||
size_t index = 0, count = 0, i, write_index;
|
||||
stk_module_event_t *evs;
|
||||
char *ptr, *end;
|
||||
struct inotify_event *e;
|
||||
@@ -763,6 +776,19 @@ stk_module_event_t *platform_directory_watch_check(
|
||||
}
|
||||
}
|
||||
|
||||
write_index = 0;
|
||||
for (i = 0; i < index; ++i) {
|
||||
if (evs[i] != -1) {
|
||||
if (write_index != i) {
|
||||
evs[write_index] = evs[i];
|
||||
memmove((*file_list)[write_index],
|
||||
(*file_list)[i], STK_PATH_MAX);
|
||||
}
|
||||
write_index++;
|
||||
}
|
||||
}
|
||||
index = write_index;
|
||||
|
||||
*out_count = index;
|
||||
return evs;
|
||||
|
||||
@@ -1000,3 +1026,25 @@ no_change:
|
||||
return NULL;
|
||||
#endif
|
||||
}
|
||||
|
||||
void platform_get_timestamp(char *buffer, size_t size)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
SYSTEMTIME st;
|
||||
GetLocalTime(&st);
|
||||
sprintf(buffer, "%04d-%02d-%02d %02d:%02d:%02d.%03d", st.wYear,
|
||||
st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond,
|
||||
st.wMilliseconds);
|
||||
#else
|
||||
struct timeval tv;
|
||||
struct tm *tm_info;
|
||||
|
||||
gettimeofday(&tv, NULL);
|
||||
tm_info = localtime(&tv.tv_sec);
|
||||
|
||||
sprintf(buffer, "%04d-%02d-%02d %02d:%02d:%02d.%03d",
|
||||
tm_info->tm_year + 1900, tm_info->tm_mon + 1, tm_info->tm_mday,
|
||||
tm_info->tm_hour, tm_info->tm_min, tm_info->tm_sec,
|
||||
(int)(tv.tv_usec / 1000));
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#include "stk.h"
|
||||
#include "platform.h"
|
||||
#include "stk_log.h"
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@@ -9,18 +8,26 @@
|
||||
typedef int (*stk_init_mod_func)(void);
|
||||
typedef void (*stk_shutdown_mod_func)(void);
|
||||
|
||||
extern void **stk_handles;
|
||||
extern stk_init_mod_func *stk_inits;
|
||||
extern stk_shutdown_mod_func *stk_shutdowns;
|
||||
extern char (*stk_module_ids)[STK_MOD_ID_BUFFER];
|
||||
typedef struct {
|
||||
char desc[STK_MOD_DESC_BUFFER];
|
||||
char name[STK_MOD_NAME_BUFFER];
|
||||
char id[STK_MOD_ID_BUFFER];
|
||||
char version[STK_MOD_VERSION_BUFFER];
|
||||
void *handle;
|
||||
stk_init_mod_func init;
|
||||
stk_shutdown_mod_func shutdown;
|
||||
stk_dep_t *deps;
|
||||
size_t dep_count;
|
||||
} stk_mod_t;
|
||||
|
||||
extern stk_mod_t *stk_modules;
|
||||
extern size_t module_count;
|
||||
|
||||
uint8_t stk_initialized = 0;
|
||||
unsigned char stk_flags = STK_FLAG_LOGGING_ENABLED;
|
||||
|
||||
static char stk_mod_dir[STK_PATH_MAX_OS] = "mods";
|
||||
static char stk_tmp_name[STK_MOD_ID_BUFFER] = ".tmp";
|
||||
static char stk_tmp_dir[STK_PATH_MAX_OS] = "mods/.tmp";
|
||||
static char stk_tmp_dir[STK_PATH_MAX_OS] = "";
|
||||
static void *watch_handle = NULL;
|
||||
|
||||
char (*platform_directory_init_scan(const char *path,
|
||||
@@ -30,20 +37,25 @@ void platform_directory_watch_stop(void *handle);
|
||||
stk_module_event_t *platform_directory_watch_check(
|
||||
void *handle, char (**file_list)[STK_PATH_MAX], size_t *out_count,
|
||||
char (*loaded_module_ids)[STK_MOD_ID_BUFFER], const size_t loaded_count);
|
||||
uint8_t platform_mkdir(const char *path);
|
||||
uint8_t platform_copy_file(const char *from, const char *to);
|
||||
uint8_t platform_remove_dir(const char *path);
|
||||
unsigned char platform_mkdir(const char *path);
|
||||
unsigned char platform_copy_file(const char *from, const char *to);
|
||||
unsigned char platform_remove_dir(const char *path);
|
||||
|
||||
void extract_module_id(const char *path, char *out_id);
|
||||
int is_mod_loaded(const char *module_id);
|
||||
|
||||
size_t stk_module_count(void);
|
||||
uint8_t stk_module_load(const char *path, int index);
|
||||
uint8_t stk_module_load_init(const char *path, int index);
|
||||
uint8_t stk_module_init_memory(size_t capacity);
|
||||
uint8_t stk_module_realloc_memory(size_t new_capacity);
|
||||
unsigned char stk_module_load(const char *path, int index);
|
||||
unsigned char stk_module_load_init(const char *path, int index);
|
||||
unsigned char stk_module_init_memory(size_t capacity);
|
||||
unsigned char stk_module_realloc_memory(size_t new_capacity);
|
||||
void stk_module_unload(size_t index);
|
||||
void stk_module_unload_all(void);
|
||||
unsigned char stk_validate_dependencies(size_t count);
|
||||
unsigned char stk_topo_sort(size_t count, size_t *order);
|
||||
void stk_pending_add(const char *path);
|
||||
void stk_pending_remove(const char *id);
|
||||
size_t stk_pending_retry(void);
|
||||
|
||||
static void build_path(char *dest, size_t dest_size, const char *dir,
|
||||
const char *file)
|
||||
@@ -65,19 +77,59 @@ static const char *stk_error_string(int error_code)
|
||||
return "init failure";
|
||||
case STK_MOD_REALLOC_FAILURE:
|
||||
return "memory reallocation failed";
|
||||
case STK_MOD_DEP_NOT_FOUND_ERROR:
|
||||
return "dependency not found";
|
||||
case STK_MOD_DEP_VERSION_MISMATCH_ERROR:
|
||||
return "dependency version mismatch";
|
||||
case STK_MOD_DEP_CIRCULAR_ERROR:
|
||||
return "circular dependency detected";
|
||||
default:
|
||||
return "unknown error";
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t stk_init(void)
|
||||
static void stk_log_module(size_t index)
|
||||
{
|
||||
const char *name =
|
||||
stk_modules[index].name[0] ? stk_modules[index].name : NULL;
|
||||
const char *desc =
|
||||
stk_modules[index].desc[0] ? stk_modules[index].desc : NULL;
|
||||
|
||||
if (name && desc)
|
||||
stk_log(STK_LOG_INFO, " %s v%s - %s (%s)",
|
||||
stk_modules[index].id, stk_modules[index].version, desc,
|
||||
name);
|
||||
else if (name)
|
||||
stk_log(STK_LOG_INFO, " %s v%s (%s)", stk_modules[index].id,
|
||||
stk_modules[index].version, name);
|
||||
else if (desc)
|
||||
stk_log(STK_LOG_INFO, " %s v%s - %s", stk_modules[index].id,
|
||||
stk_modules[index].version, desc);
|
||||
else
|
||||
stk_log(STK_LOG_INFO, " %s v%s", stk_modules[index].id,
|
||||
stk_modules[index].version);
|
||||
}
|
||||
|
||||
static void stk_log_modules(void)
|
||||
{
|
||||
size_t i;
|
||||
stk_log(STK_LOG_INFO, "Loaded modules (%lu):", module_count);
|
||||
for (i = 0; i < module_count; i++)
|
||||
stk_log_module(i);
|
||||
}
|
||||
|
||||
unsigned char stk_init(void)
|
||||
{
|
||||
char (*files)[STK_PATH_MAX] = NULL;
|
||||
size_t file_count, i, successful_loads = 0;
|
||||
char full_path[STK_PATH_MAX_OS];
|
||||
char tmp_path[STK_PATH_MAX_OS];
|
||||
int load_result;
|
||||
unsigned char dep_result;
|
||||
size_t *order = NULL;
|
||||
|
||||
platform_mkdir(stk_mod_dir);
|
||||
build_path(stk_tmp_dir, sizeof(stk_tmp_dir), stk_mod_dir, stk_tmp_name);
|
||||
if (platform_mkdir(stk_tmp_dir) != STK_PLATFORM_OPERATION_SUCCESS) {
|
||||
char (*test_scan)[STK_PATH_MAX];
|
||||
size_t test_count;
|
||||
@@ -87,7 +139,7 @@ uint8_t stk_init(void)
|
||||
if (test_scan)
|
||||
free(test_scan);
|
||||
if (!test_scan && test_count == 0) {
|
||||
stk_log(stderr,
|
||||
stk_log(STK_LOG_ERROR,
|
||||
"FATAL: Cannot create temp directory: %s",
|
||||
stk_tmp_dir);
|
||||
return STK_INIT_TMPDIR_ERROR;
|
||||
@@ -97,7 +149,7 @@ uint8_t stk_init(void)
|
||||
files = platform_directory_init_scan(stk_mod_dir, &file_count);
|
||||
|
||||
if (file_count > 0 && stk_module_init_memory(file_count) != 0) {
|
||||
stk_log(stderr, "FATAL: Memory allocation failed");
|
||||
stk_log(STK_LOG_ERROR, "FATAL: Memory allocation failed");
|
||||
return STK_INIT_MEMORY_ERROR;
|
||||
}
|
||||
|
||||
@@ -110,7 +162,8 @@ uint8_t stk_init(void)
|
||||
|
||||
if (platform_copy_file(full_path, tmp_path) !=
|
||||
STK_PLATFORM_OPERATION_SUCCESS) {
|
||||
stk_log(stderr, "Failed to copy %s to temp directory",
|
||||
stk_log(STK_LOG_ERROR,
|
||||
"Failed to copy %s to temp directory",
|
||||
files[i]);
|
||||
continue;
|
||||
}
|
||||
@@ -118,33 +171,65 @@ uint8_t stk_init(void)
|
||||
load_result = stk_module_load_init(tmp_path, successful_loads);
|
||||
|
||||
if (load_result != STK_MOD_INIT_SUCCESS) {
|
||||
stk_log(stderr, "Failed to load module %s: %s",
|
||||
stk_log(STK_LOG_ERROR, "Failed to load module %s: %s",
|
||||
files[i], stk_error_string(load_result));
|
||||
} else {
|
||||
successful_loads++;
|
||||
}
|
||||
}
|
||||
|
||||
if (successful_loads < file_count) {
|
||||
if (successful_loads < file_count)
|
||||
stk_module_realloc_memory(successful_loads);
|
||||
}
|
||||
|
||||
free(files);
|
||||
|
||||
if (module_count == 0)
|
||||
goto scanned;
|
||||
|
||||
dep_result = stk_validate_dependencies(module_count);
|
||||
if (dep_result != STK_MOD_INIT_SUCCESS) {
|
||||
size_t j;
|
||||
char mod_tmp_path[STK_PATH_MAX_OS];
|
||||
stk_log(STK_LOG_WARN,
|
||||
"Some modules have unmet dependencies, deferring");
|
||||
for (j = 0; j < module_count; j++) {
|
||||
if (stk_modules[j].dep_count > 0) {
|
||||
build_path(mod_tmp_path, sizeof(mod_tmp_path),
|
||||
stk_tmp_dir, stk_modules[j].id);
|
||||
strncat(mod_tmp_path, STK_MODULE_EXT,
|
||||
sizeof(mod_tmp_path) -
|
||||
strlen(mod_tmp_path) - 1);
|
||||
stk_pending_add(mod_tmp_path);
|
||||
stk_module_unload(j);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
order = malloc(module_count * sizeof(size_t));
|
||||
if (order) {
|
||||
dep_result = stk_topo_sort(module_count, order);
|
||||
if (dep_result != STK_MOD_INIT_SUCCESS)
|
||||
stk_log(STK_LOG_ERROR, "Dependency sort failed: %s",
|
||||
stk_error_string(dep_result));
|
||||
free(order);
|
||||
}
|
||||
|
||||
scanned:
|
||||
watch_handle = platform_directory_watch_start(stk_mod_dir);
|
||||
if (!watch_handle) {
|
||||
stk_log(stderr, "FATAL: Cannot start directory watch on %s",
|
||||
stk_log(STK_LOG_ERROR,
|
||||
"FATAL: Cannot start directory watch on %s",
|
||||
stk_mod_dir);
|
||||
stk_module_unload_all();
|
||||
return STK_INIT_WATCH_ERROR;
|
||||
}
|
||||
|
||||
stk_log(stdout, "stk v%s initialized! Loaded %lu mod%s from %s/",
|
||||
STK_VERSION_STRING, module_count, module_count != 1 ? "s" : "",
|
||||
stk_mod_dir);
|
||||
stk_log(STK_LOG_INFO, "stk v%s initialized, watching %s/",
|
||||
STK_VERSION_STRING, stk_mod_dir);
|
||||
if (module_count > 0)
|
||||
stk_log_modules();
|
||||
|
||||
stk_initialized = 1;
|
||||
stk_flags |= STK_FLAG_INITIALIZED;
|
||||
return STK_INIT_SUCCESS;
|
||||
}
|
||||
|
||||
@@ -159,12 +244,13 @@ void stk_shutdown(void)
|
||||
|
||||
if (platform_remove_dir(stk_tmp_dir) !=
|
||||
STK_PLATFORM_OPERATION_SUCCESS) {
|
||||
stk_log(stderr, "Warning: failed to remove temp directory %s",
|
||||
stk_log(STK_LOG_WARN,
|
||||
"Warning: failed to remove temp directory %s",
|
||||
stk_tmp_dir);
|
||||
}
|
||||
|
||||
stk_initialized = 0;
|
||||
stk_log(stdout, "stk shutdown");
|
||||
stk_flags &= ~STK_FLAG_INITIALIZED;
|
||||
stk_log(STK_LOG_INFO, "stk shutdown");
|
||||
}
|
||||
|
||||
size_t stk_poll(void)
|
||||
@@ -172,8 +258,7 @@ size_t stk_poll(void)
|
||||
char (*file_list)[STK_PATH_MAX] = NULL;
|
||||
stk_module_event_t *events = NULL;
|
||||
size_t i, file_count = 0, reload_count = 0, load_count = 0,
|
||||
unload_count = 0, reload_index = 0, load_index = 0,
|
||||
unload_index = 0;
|
||||
unload_count = 0;
|
||||
int *reloaded_mod_indices = NULL, *reloaded_mod_file_indices = NULL,
|
||||
*unloaded_mod_indices = NULL, *loaded_mod_indices = NULL;
|
||||
size_t remaining_loads, new_capacity, holes_to_fill;
|
||||
@@ -182,10 +267,27 @@ size_t stk_poll(void)
|
||||
char mod_id[STK_MOD_ID_BUFFER];
|
||||
int load_result;
|
||||
size_t successful_appends = 0;
|
||||
char (*module_ids)[STK_MOD_ID_BUFFER] = NULL;
|
||||
unsigned char dep_result;
|
||||
size_t *order = NULL;
|
||||
|
||||
if (module_count > 0) {
|
||||
module_ids = malloc(module_count * sizeof(*module_ids));
|
||||
if (module_ids) {
|
||||
for (i = 0; i < module_count; i++) {
|
||||
strncpy(module_ids[i], stk_modules[i].id,
|
||||
STK_MOD_ID_BUFFER - 1);
|
||||
module_ids[i][STK_MOD_ID_BUFFER - 1] = '\0';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
events = platform_directory_watch_check(
|
||||
watch_handle, &file_list, &file_count, module_ids, module_count);
|
||||
|
||||
if (module_ids)
|
||||
free(module_ids);
|
||||
|
||||
events = platform_directory_watch_check(watch_handle, &file_list,
|
||||
&file_count, stk_module_ids,
|
||||
module_count);
|
||||
if (!events)
|
||||
goto finish_poll;
|
||||
|
||||
@@ -208,20 +310,31 @@ size_t stk_poll(void)
|
||||
unloaded_mod_indices = malloc(unload_count * sizeof(int));
|
||||
loaded_mod_indices = malloc(load_count * sizeof(int));
|
||||
|
||||
reload_count = 0;
|
||||
unload_count = 0;
|
||||
load_count = 0;
|
||||
|
||||
for (i = 0; i < file_count; ++i) {
|
||||
int mod_index;
|
||||
extract_module_id(file_list[i], mod_id);
|
||||
switch (events[i]) {
|
||||
case STK_MOD_LOAD:
|
||||
loaded_mod_indices[load_index++] = i;
|
||||
loaded_mod_indices[load_count++] = i;
|
||||
break;
|
||||
case STK_MOD_RELOAD:
|
||||
reloaded_mod_file_indices[reload_index] = i;
|
||||
reloaded_mod_indices[reload_index++] =
|
||||
is_mod_loaded(mod_id);
|
||||
mod_index = is_mod_loaded(mod_id);
|
||||
if (mod_index >= 0) {
|
||||
reloaded_mod_file_indices[reload_count] = i;
|
||||
reloaded_mod_indices[reload_count] = mod_index;
|
||||
reload_count++;
|
||||
}
|
||||
break;
|
||||
case STK_MOD_UNLOAD:
|
||||
unloaded_mod_indices[unload_index++] =
|
||||
is_mod_loaded(mod_id);
|
||||
mod_index = is_mod_loaded(mod_id);
|
||||
if (mod_index >= 0) {
|
||||
unloaded_mod_indices[unload_count] = mod_index;
|
||||
unload_count++;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -234,16 +347,16 @@ size_t stk_poll(void)
|
||||
handle_grow:
|
||||
remaining_loads = load_count - unload_count;
|
||||
new_capacity = module_count + remaining_loads;
|
||||
if (stk_module_realloc_memory(new_capacity) != STK_MOD_INIT_SUCCESS) {
|
||||
if (stk_module_realloc_memory(new_capacity) != STK_MOD_INIT_SUCCESS)
|
||||
goto free_poll;
|
||||
}
|
||||
|
||||
begin_operations:
|
||||
for (i = 0; i < unload_count; ++i)
|
||||
for (i = 0; i < unload_count; ++i) {
|
||||
stk_log(STK_LOG_INFO, "Unloaded module: %s",
|
||||
stk_modules[unloaded_mod_indices[i]].id);
|
||||
stk_pending_remove(stk_modules[unloaded_mod_indices[i]].id);
|
||||
stk_module_unload(unloaded_mod_indices[i]);
|
||||
|
||||
for (i = 0; i < reload_count; ++i)
|
||||
stk_module_unload(reloaded_mod_indices[i]);
|
||||
}
|
||||
|
||||
for (i = 0; i < reload_count; ++i) {
|
||||
int file_index = reloaded_mod_file_indices[i];
|
||||
@@ -254,19 +367,20 @@ begin_operations:
|
||||
build_path(tmp_path, sizeof(tmp_path), stk_tmp_dir,
|
||||
file_list[file_index]);
|
||||
|
||||
stk_module_unload(mod_index);
|
||||
|
||||
if (platform_copy_file(full_path, tmp_path) !=
|
||||
STK_PLATFORM_OPERATION_SUCCESS) {
|
||||
stk_log(stderr, "Failed to copy %s for reload",
|
||||
stk_log(STK_LOG_ERROR, "Failed to copy %s for reload",
|
||||
file_list[file_index]);
|
||||
continue;
|
||||
}
|
||||
|
||||
load_result = stk_module_load(tmp_path, mod_index);
|
||||
if (load_result != STK_MOD_INIT_SUCCESS) {
|
||||
stk_log(stderr, "Failed to reload module %s: %s",
|
||||
if (load_result != STK_MOD_INIT_SUCCESS)
|
||||
stk_log(STK_LOG_ERROR, "Failed to reload module %s: %s",
|
||||
file_list[file_index],
|
||||
stk_error_string(load_result));
|
||||
}
|
||||
}
|
||||
|
||||
holes_to_fill = (load_count < unload_count) ? load_count : unload_count;
|
||||
@@ -281,17 +395,16 @@ begin_operations:
|
||||
|
||||
if (platform_copy_file(full_path, tmp_path) !=
|
||||
STK_PLATFORM_OPERATION_SUCCESS) {
|
||||
stk_log(stderr, "Failed to copy %s for loading",
|
||||
stk_log(STK_LOG_ERROR, "Failed to copy %s for loading",
|
||||
file_list[file_index]);
|
||||
continue;
|
||||
}
|
||||
|
||||
load_result = stk_module_load(tmp_path, target_index);
|
||||
if (load_result != STK_MOD_INIT_SUCCESS) {
|
||||
stk_log(stderr, "Failed to load module %s: %s",
|
||||
if (load_result != STK_MOD_INIT_SUCCESS)
|
||||
stk_log(STK_LOG_ERROR, "Failed to load module %s: %s",
|
||||
file_list[file_index],
|
||||
stk_error_string(load_result));
|
||||
}
|
||||
}
|
||||
|
||||
if (load_count > unload_count)
|
||||
@@ -300,7 +413,7 @@ begin_operations:
|
||||
if (unload_count > load_count)
|
||||
goto trim_arrays;
|
||||
|
||||
goto free_poll;
|
||||
goto validate_deps;
|
||||
|
||||
append_modules:
|
||||
for (; i < load_count; ++i) {
|
||||
@@ -313,7 +426,7 @@ append_modules:
|
||||
|
||||
if (platform_copy_file(full_path, tmp_path) !=
|
||||
STK_PLATFORM_OPERATION_SUCCESS) {
|
||||
stk_log(stderr, "Failed to copy %s for loading",
|
||||
stk_log(STK_LOG_ERROR, "Failed to copy %s for loading",
|
||||
file_list[file_index]);
|
||||
continue;
|
||||
}
|
||||
@@ -321,7 +434,7 @@ append_modules:
|
||||
load_result = stk_module_load(tmp_path, module_count +
|
||||
successful_appends);
|
||||
if (load_result != STK_MOD_INIT_SUCCESS) {
|
||||
stk_log(stderr, "Failed to load module %s: %s",
|
||||
stk_log(STK_LOG_ERROR, "Failed to load module %s: %s",
|
||||
file_list[file_index],
|
||||
stk_error_string(load_result));
|
||||
} else {
|
||||
@@ -331,11 +444,10 @@ append_modules:
|
||||
|
||||
module_count += successful_appends;
|
||||
|
||||
if (successful_appends < (load_count - holes_to_fill)) {
|
||||
if (successful_appends < (load_count - holes_to_fill))
|
||||
stk_module_realloc_memory(module_count);
|
||||
}
|
||||
|
||||
goto free_poll;
|
||||
goto validate_deps;
|
||||
|
||||
trim_arrays:
|
||||
write_pos = unloaded_mod_indices[holes_to_fill];
|
||||
@@ -345,12 +457,8 @@ trim_arrays:
|
||||
}
|
||||
|
||||
for (read_pos = write_pos + 1; read_pos < module_count; ++read_pos) {
|
||||
if (stk_handles[read_pos] != NULL) {
|
||||
stk_handles[write_pos] = stk_handles[read_pos];
|
||||
stk_inits[write_pos] = stk_inits[read_pos];
|
||||
stk_shutdowns[write_pos] = stk_shutdowns[read_pos];
|
||||
memcpy(stk_module_ids[write_pos],
|
||||
stk_module_ids[read_pos], STK_MOD_ID_BUFFER);
|
||||
if (stk_modules[read_pos].handle != NULL) {
|
||||
stk_modules[write_pos] = stk_modules[read_pos];
|
||||
++write_pos;
|
||||
}
|
||||
}
|
||||
@@ -358,6 +466,82 @@ trim_arrays:
|
||||
module_count = write_pos;
|
||||
stk_module_realloc_memory(module_count);
|
||||
|
||||
validate_deps:
|
||||
if (module_count == 0)
|
||||
goto free_poll;
|
||||
|
||||
{
|
||||
size_t cascade_indices[STK_PATH_MAX];
|
||||
size_t cascade_count;
|
||||
char cascade_tmp_path[STK_PATH_MAX_OS];
|
||||
size_t j, k, cascade_write;
|
||||
|
||||
do {
|
||||
cascade_count = 0;
|
||||
|
||||
for (j = 0; j < module_count; j++) {
|
||||
if (stk_modules[j].dep_count == 0)
|
||||
continue;
|
||||
for (k = 0; k < stk_modules[j].dep_count; k++) {
|
||||
if (is_mod_loaded(
|
||||
stk_modules[j].deps[k].id) <
|
||||
0) {
|
||||
cascade_indices
|
||||
[cascade_count++] = j;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (cascade_count == 0)
|
||||
break;
|
||||
|
||||
for (j = 0; j < cascade_count; j++) {
|
||||
size_t index = cascade_indices[j];
|
||||
stk_log(STK_LOG_WARN,
|
||||
"Unloading '%s': unmet dependencies",
|
||||
stk_modules[index].id);
|
||||
build_path(cascade_tmp_path,
|
||||
sizeof(cascade_tmp_path),
|
||||
stk_tmp_dir, stk_modules[index].id);
|
||||
strncat(cascade_tmp_path, STK_MODULE_EXT,
|
||||
sizeof(cascade_tmp_path) -
|
||||
strlen(cascade_tmp_path) - 1);
|
||||
stk_pending_add(cascade_tmp_path);
|
||||
stk_module_unload(index);
|
||||
}
|
||||
|
||||
cascade_write = 0;
|
||||
for (j = 0; j < module_count; j++) {
|
||||
if (stk_modules[j].handle != NULL) {
|
||||
if (cascade_write != j)
|
||||
stk_modules[cascade_write] =
|
||||
stk_modules[j];
|
||||
cascade_write++;
|
||||
}
|
||||
}
|
||||
module_count = cascade_write;
|
||||
|
||||
} while (cascade_count > 0);
|
||||
}
|
||||
|
||||
if (module_count > 0)
|
||||
stk_module_realloc_memory(module_count);
|
||||
|
||||
order = malloc(module_count * sizeof(size_t));
|
||||
if (order) {
|
||||
dep_result = stk_topo_sort(module_count, order);
|
||||
if (dep_result != STK_MOD_INIT_SUCCESS)
|
||||
stk_log(STK_LOG_ERROR, "Dependency sort failed: %s",
|
||||
stk_error_string(dep_result));
|
||||
free(order);
|
||||
}
|
||||
|
||||
stk_pending_retry();
|
||||
|
||||
if (module_count > 0)
|
||||
stk_log_modules();
|
||||
|
||||
free_poll:
|
||||
free(reloaded_mod_indices);
|
||||
free(reloaded_mod_file_indices);
|
||||
@@ -372,7 +556,7 @@ finish_poll:
|
||||
|
||||
void stk_set_mod_dir(const char *path)
|
||||
{
|
||||
if (!path || stk_initialized)
|
||||
if (!path || (stk_flags & STK_FLAG_INITIALIZED))
|
||||
return;
|
||||
|
||||
strncpy(stk_mod_dir, path, STK_PATH_MAX_OS - 1);
|
||||
@@ -390,7 +574,7 @@ void stk_set_mod_dir(const char *path)
|
||||
|
||||
void stk_set_tmp_dir_name(const char *name)
|
||||
{
|
||||
if (!name || stk_initialized)
|
||||
if (!name || (stk_flags & STK_FLAG_INITIALIZED))
|
||||
return;
|
||||
|
||||
strncpy(stk_tmp_name, name, STK_MOD_ID_BUFFER - 1);
|
||||
@@ -403,3 +587,16 @@ void stk_set_tmp_dir_name(const char *name)
|
||||
strncat(stk_tmp_dir, stk_tmp_name,
|
||||
STK_PATH_MAX_OS - strlen(stk_tmp_dir) - 1);
|
||||
}
|
||||
|
||||
void stk_set_logging_enabled(unsigned char enabled)
|
||||
{
|
||||
if (enabled)
|
||||
stk_flags |= STK_FLAG_LOGGING_ENABLED;
|
||||
else
|
||||
stk_flags &= ~STK_FLAG_LOGGING_ENABLED;
|
||||
}
|
||||
|
||||
unsigned char stk_is_logging_enabled(void)
|
||||
{
|
||||
return (stk_flags & STK_FLAG_LOGGING_ENABLED) != 0;
|
||||
}
|
||||
|
||||
+77
-5
@@ -1,14 +1,86 @@
|
||||
#include "stk_log.h"
|
||||
#include "stk.h"
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
void stk_log(FILE *fp, const char *fmt, ...)
|
||||
#define STK_LOG_TIMESTAMP_BUFFER 32
|
||||
|
||||
extern unsigned char stk_flags;
|
||||
void platform_get_timestamp(char *buffer, size_t size);
|
||||
|
||||
static FILE *log_output = NULL;
|
||||
static char log_prefix[STK_LOG_PREFIX_BUFFER] = "stk";
|
||||
static stk_log_level_t min_log_level = STK_LOG_INFO;
|
||||
|
||||
static const char *get_level_string(stk_log_level_t level)
|
||||
{
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
char *level_str = "";
|
||||
switch (level) {
|
||||
case STK_LOG_ERROR:
|
||||
level_str = "ERROR";
|
||||
break;
|
||||
case STK_LOG_WARN:
|
||||
level_str = "WARN";
|
||||
break;
|
||||
case STK_LOG_INFO:
|
||||
level_str = "INFO";
|
||||
break;
|
||||
case STK_LOG_DEBUG:
|
||||
level_str = "DEBUG";
|
||||
break;
|
||||
}
|
||||
|
||||
vfprintf(fp, fmt, args);
|
||||
fputc('\n', fp);
|
||||
return level_str;
|
||||
}
|
||||
|
||||
void stk_set_log_output(FILE *fp)
|
||||
{
|
||||
if (fp == NULL)
|
||||
stk_flags &= ~STK_FLAG_LOGGING_ENABLED;
|
||||
|
||||
log_output = fp;
|
||||
}
|
||||
|
||||
void stk_set_log_prefix(const char *prefix)
|
||||
{
|
||||
if (!prefix) {
|
||||
log_prefix[0] = '\0';
|
||||
return;
|
||||
}
|
||||
|
||||
strncpy(log_prefix, prefix, STK_LOG_PREFIX_BUFFER - 1);
|
||||
log_prefix[STK_LOG_PREFIX_BUFFER - 1] = '\0';
|
||||
}
|
||||
|
||||
void stk_set_log_level(stk_log_level_t level) { min_log_level = level; }
|
||||
|
||||
void stk_log(stk_log_level_t level, const char *fmt, ...)
|
||||
{
|
||||
FILE *output;
|
||||
const char *level_str;
|
||||
char timestamp[STK_LOG_TIMESTAMP_BUFFER];
|
||||
va_list args;
|
||||
|
||||
if (!(stk_flags & STK_FLAG_LOGGING_ENABLED))
|
||||
return;
|
||||
|
||||
if (level < min_log_level)
|
||||
return;
|
||||
|
||||
output = log_output ? log_output : stdout;
|
||||
level_str = get_level_string(level);
|
||||
platform_get_timestamp(timestamp, sizeof(timestamp));
|
||||
|
||||
if (log_prefix[0] != '\0')
|
||||
fprintf(output, "%s [%s] [%s] ", timestamp, log_prefix,
|
||||
level_str);
|
||||
else
|
||||
fprintf(output, "%s [%s] ", timestamp, level_str);
|
||||
|
||||
va_start(args, fmt);
|
||||
vfprintf(output, fmt, args);
|
||||
va_end(args);
|
||||
|
||||
fputc('\n', output);
|
||||
}
|
||||
|
||||
+9
-5
@@ -22,23 +22,27 @@ test_program: test.c
|
||||
test_mod$(MODULE_EXT): test_mod.c
|
||||
$(CC) $(CFLAGS) -fPIC -shared -o $@ test_mod.c
|
||||
|
||||
test_mod_dep$(MODULE_EXT): test_mod_dep.c
|
||||
$(CC) $(CFLAGS) -fPIC -shared -o $@ test_mod_dep.c
|
||||
|
||||
setup:
|
||||
@mkdir -p mods
|
||||
@cp -f test_mod$(MODULE_EXT) mods/ 2>/dev/null || true
|
||||
@echo "Test environment ready: mods/ directory with test_mod$(MODULE_EXT)"
|
||||
@cp -f test_mod_dep$(MODULE_EXT) mods/ 2>/dev/null || true
|
||||
@echo "Test environment ready: mods/ directory with test modules"
|
||||
|
||||
run: test_program test_mod$(MODULE_EXT) setup
|
||||
run: test_program test_mod$(MODULE_EXT) test_mod_dep$(MODULE_EXT) setup
|
||||
@echo "Running integration test (CTRL+C to exit)..."
|
||||
@./test_program
|
||||
|
||||
test: test_program test_mod$(MODULE_EXT) setup
|
||||
test: test_program test_mod$(MODULE_EXT) test_mod_dep$(MODULE_EXT) setup
|
||||
@echo "=== stk Integration Test ==="
|
||||
@echo "1. Starting test program"
|
||||
@echo "2. Will load test_mod$(MODULE_EXT) from mods/"
|
||||
@echo "2. Will load test_mod and test_mod_dep from mods/"
|
||||
@echo "3. Press CTRL+C to exit"
|
||||
@echo "============================="
|
||||
@./test_program || echo "Test completed."
|
||||
|
||||
clean:
|
||||
rm -f test_program test_mod$(MODULE_EXT)
|
||||
rm -f test_program test_mod$(MODULE_EXT) test_mod_dep$(MODULE_EXT)
|
||||
rm -rf mods/
|
||||
|
||||
+10
-3
@@ -3,6 +3,8 @@ CFLAGS = -Wall -Wpedantic -I../include -std=c89
|
||||
LDFLAGS = -L../bin/debug -lstk
|
||||
|
||||
ifeq ($(OS),Windows_NT)
|
||||
SHELL := cmd.exe
|
||||
.SHELLFLAGS := /c
|
||||
MODULE_EXT = .dll
|
||||
EXE_EXT = .exe
|
||||
else
|
||||
@@ -21,16 +23,21 @@ test_program$(EXE_EXT): test.c
|
||||
test_mod$(MODULE_EXT): test_mod.c
|
||||
$(CC) $(CFLAGS) -fPIC -shared -o $@ test_mod.c
|
||||
|
||||
test_mod_dep$(MODULE_EXT): test_mod_dep.c
|
||||
$(CC) $(CFLAGS) -fPIC -shared -o $@ test_mod_dep.c
|
||||
|
||||
setup:
|
||||
ifeq ($(OS),Windows_NT)
|
||||
@if not exist mods mkdir mods
|
||||
@if exist test_mod.dll copy /Y test_mod.dll mods\ >nul 2>&1
|
||||
@if exist test_mod_dep.dll copy /Y test_mod_dep.dll mods\ >nul 2>&1
|
||||
else
|
||||
@mkdir -p mods
|
||||
@cp -f test_mod.so mods/ 2>/dev/null || true
|
||||
@cp -f test_mod_dep.so mods/ 2>/dev/null || true
|
||||
endif
|
||||
|
||||
test: test_program$(EXE_EXT) test_mod$(MODULE_EXT) setup
|
||||
test: test_program$(EXE_EXT) test_mod$(MODULE_EXT) test_mod_dep$(MODULE_EXT) setup
|
||||
ifeq ($(OS),Windows_NT)
|
||||
@set PATH=../bin/debug;%PATH% && cmd /C "test_program.exe"
|
||||
else
|
||||
@@ -39,9 +46,9 @@ endif
|
||||
|
||||
clean:
|
||||
ifeq ($(OS),Windows_NT)
|
||||
@del /Q test_program.exe test_mod.dll 2>nul || true
|
||||
@del /Q test_program.exe test_mod.dll test_mod_dep.dll 2>nul || true
|
||||
@rmdir /S /Q mods 2>nul || true
|
||||
else
|
||||
@rm -f test_program test_mod.so
|
||||
@rm -f test_program test_mod.so test_mod_dep.so
|
||||
@rm -rf mods
|
||||
endif
|
||||
|
||||
+5
-4
@@ -31,8 +31,8 @@ void inthand(int signum)
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
uint8_t init_result;
|
||||
uint64_t iterations = 0;
|
||||
unsigned char init_result;
|
||||
size_t iterations = 0;
|
||||
|
||||
printf("stk test - CTRL+C to exit\n");
|
||||
|
||||
@@ -55,12 +55,13 @@ int main(int argc, char **argv)
|
||||
while (!stop) {
|
||||
size_t events = stk_poll();
|
||||
if (events > 0)
|
||||
printf("Poll: %lu module event(s) detected\n", (unsigned long) events);
|
||||
printf("Poll: %lu module event(s) detected\n",
|
||||
(unsigned long)events);
|
||||
|
||||
iterations++;
|
||||
if (iterations % 5 == 0) {
|
||||
printf("Still running... (iteration %lu)\n",
|
||||
(unsigned long) iterations);
|
||||
(unsigned long)iterations);
|
||||
}
|
||||
#ifdef _WIN32
|
||||
Sleep(1000);
|
||||
|
||||
+5
-2
@@ -2,8 +2,11 @@
|
||||
|
||||
int stk_mod_init(void)
|
||||
{
|
||||
printf("test mod initialized!\n");
|
||||
printf("test_mod initialized!\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
void stk_mod_shutdown(void) { printf("test mod shut down.\n"); }
|
||||
void stk_mod_shutdown(void) { printf("test_mod shut down.\n"); }
|
||||
|
||||
const char *stk_mod_name(void) { return "Test Module"; }
|
||||
const char *stk_mod_version(void) { return "1.0.0"; }
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
#include <stdio.h>
|
||||
|
||||
typedef struct {
|
||||
char id[64];
|
||||
char version[32];
|
||||
} dep_t;
|
||||
|
||||
int stk_mod_init(void)
|
||||
{
|
||||
printf("test_mod_dep initialized!\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
void stk_mod_shutdown(void) { printf("test_mod_dep shut down.\n"); }
|
||||
|
||||
const char *stk_mod_name(void) { return "Dependent Test Module"; }
|
||||
const char *stk_mod_version(void) { return "1.0.0"; }
|
||||
|
||||
dep_t stk_mod_deps[] = {{"test_mod", ">=1.0.0"}, {"", ""}};
|
||||
Reference in New Issue
Block a user