diff --git a/Makefile b/Makefile index 4e146b3..031b8e7 100644 --- a/Makefile +++ b/Makefile @@ -8,8 +8,8 @@ CFLAGS += -Wconversion -Wunreachable-code CFLAGS += -D_GNU_SOURCE LDFLAGS = -SRC = modfetch.c -OBJ = $(BIN)/$(SRC:.c=.o) +SRC = modfetch.c semver.c +OBJ = $(SRC:.c=.o) BIN = target TEMPMOD = $(wildcard modules/*.c) MOD = $(TEMPMOD:modules/%=%) diff --git a/mod.h b/mod.h index e67cd47..7c5ca98 100644 --- a/mod.h +++ b/mod.h @@ -1,11 +1,10 @@ #ifndef _MODFETCH_MOD_H #define _MODFETCH_MOD_H +#include "semver.h" #include -uint8_t version_major(void); -uint8_t version_minor(void); -uint8_t version_patch(void); +semver version(void); const char *module_name(void); const char *get(void); diff --git a/modfetch.c b/modfetch.c index f913e52..40d1f3d 100644 --- a/modfetch.c +++ b/modfetch.c @@ -1,3 +1,4 @@ +#include "semver.h" #include #include #include @@ -11,9 +12,11 @@ static const char *PNAME = "modfetch"; -static const uint8_t VERSION_MAJOR = 0; -static const uint8_t VERSION_MINOR = 1; -static const uint8_t VERSION_PATCH = 0; +static const semver VERSION = { + .major = 0, + .minor = 1, + .patch = 0, +}; static const uint8_t MAX_MODULE_NAME_LENGTH = 128; static const uint8_t MAX_MODULES = 128; @@ -22,14 +25,6 @@ static const uint8_t INITIAL_CONFIG_SIZE = 8; static const uint16_t MAX_PATH_LENGTH = 4096; -static const char *version_str(void) { - char *ver; - if (asprintf(&ver, "%d.%d.%d", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH) < 0) { - return NULL; - } - return ver; -} - typedef struct { // path to the binary of this module char *path; @@ -234,7 +229,7 @@ int main(int argc, char *argv[]) { } if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) { - fprintf(stderr, "%s: modular fetch [%s]\n", PNAME, version_str()); + fprintf(stderr, "%s: modular fetch [%s]\n", PNAME, svtoa(VERSION)); fprintf(stderr, "\n"); fprintf(stderr, "OPTIONS\n"); fprintf(stderr, "\t-h, --help\t\t\tdisplays this help text\n"); @@ -269,10 +264,7 @@ int main(int argc, char *argv[]) { // https://stackoverflow.com/questions/14134245/iso-c-void-and-function-pointers #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wpedantic" - uint8_t (*version_major)(void) = dlsym(handle, "version_major"); - uint8_t (*version_minor)(void) = dlsym(handle, "version_minor"); - uint8_t (*version_patch)(void) = dlsym(handle, "version_patch"); - + semver (*version)(void) = dlsym(handle, "version"); const char *(*module_name)(void) = dlsym(handle, "module_name"); const char *(*get)(void) = dlsym(handle, "get"); void (*init)(char**) = dlsym(handle, "init"); @@ -281,10 +273,8 @@ int main(int argc, char *argv[]) { init(current_module.config); (void)module_name; - (void)version_major; - (void)version_minor; - (void)version_patch; - //printf("%s: %d.%d.%d\n", module_name(), version_major(), version_minor(), version_patch()); + (void)version; + // printf("%s: %s\n", module_name(), svtoa(version())); printf("%s\n", get()); } diff --git a/modules/desktop.c b/modules/desktop.c index 7c1c7ce..eb913db 100644 --- a/modules/desktop.c +++ b/modules/desktop.c @@ -8,9 +8,13 @@ #include #include -uint8_t version_major(void) { return 0; } -uint8_t version_minor(void) { return 1; } -uint8_t version_patch(void) { return 0; } +static const semver _version = { + .major = 0, + .minor = 1, + .patch = 0, +}; + +semver version(void) { return _version; } const char *module_name(void) { return "desktop"; } void init(char **config) { diff --git a/modules/os.c b/modules/os.c index b15a2ad..eec18d3 100644 --- a/modules/os.c +++ b/modules/os.c @@ -10,9 +10,13 @@ #include #include -uint8_t version_major(void) { return 0; } -uint8_t version_minor(void) { return 1; } -uint8_t version_patch(void) { return 0; } +static const semver _version = { + .major = 0, + .minor = 1, + .patch = 0, +}; + +semver version(void) { return _version; } const char *module_name(void) { return "os"; } void init(char **config) { diff --git a/modules/sep.c b/modules/sep.c index e3e9916..daf6eaa 100644 --- a/modules/sep.c +++ b/modules/sep.c @@ -12,9 +12,13 @@ #include #include -uint8_t version_major(void) { return 1; } -uint8_t version_minor(void) { return 0; } -uint8_t version_patch(void) { return 0; } +static const semver _version = { + .major = 1, + .minor = 0, + .patch = 0, +}; + +semver version(void) { return _version; } const char *module_name(void) { return "sep"; } static size_t length = 8; diff --git a/semver.c b/semver.c new file mode 100644 index 0000000..08ee34c --- /dev/null +++ b/semver.c @@ -0,0 +1,28 @@ +#include "semver.h" +#include +#include + +uint8_t num_digits(uint8_t x) { + return ((x) / 100) != 0 ? 3 : + ((x) / 10) != 0 ? 2 : + 1; +} + +bool cmpsv(semver s1, semver s2) { + return s1.major == s2.major && + s1.minor == s2.minor && + s1.patch == s2.patch; +} + +char *svtoa(semver s) { + uint8_t major_len = num_digits(s.major); + uint8_t minor_len = num_digits(s.minor); + uint8_t patch_len = num_digits(s.patch); + // \0 dots + size_t len = (size_t)1 + 2 + major_len + minor_len + patch_len; + char *ret = malloc(len * sizeof(char)); + + snprintf(ret, len, "%d.%d.%d", s.major, s.minor, s.patch); + + return ret; +} diff --git a/semver.h b/semver.h new file mode 100644 index 0000000..d0e90b0 --- /dev/null +++ b/semver.h @@ -0,0 +1,17 @@ +#ifndef _MODFETCH_SEMVER_H +#define _MODFETCH_SEMVER_H + +#include +#include + +typedef struct { + uint8_t major; + uint8_t minor; + uint8_t patch; +} semver; + +bool cmpsv(semver, semver); + +char *svtoa(semver); + +#endif // _MODFETCH_SEMVER_H