move to semver everywhere

This commit is contained in:
jacekpoz 2024-02-10 14:11:26 +01:00
parent 92a9fb163f
commit dfa4ea2f44
No known key found for this signature in database
GPG key ID: 94E812A8B12AAE3C
8 changed files with 80 additions and 34 deletions

View file

@ -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/%=%)

5
mod.h
View file

@ -1,11 +1,10 @@
#ifndef _MODFETCH_MOD_H
#define _MODFETCH_MOD_H
#include "semver.h"
#include <stdint.h>
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);

View file

@ -1,3 +1,4 @@
#include "semver.h"
#include <ctype.h>
#include <dlfcn.h>
#include <errno.h>
@ -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());
}

View file

@ -8,9 +8,13 @@
#include <stdio.h>
#include <stdlib.h>
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) {

View file

@ -10,9 +10,13 @@
#include <string.h>
#include <sys/types.h>
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) {

View file

@ -12,9 +12,13 @@
#include <string.h>
#include <stdlib.h>
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;

28
semver.c Normal file
View file

@ -0,0 +1,28 @@
#include "semver.h"
#include <stdio.h>
#include <stdlib.h>
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;
}

17
semver.h Normal file
View file

@ -0,0 +1,17 @@
#ifndef _MODFETCH_SEMVER_H
#define _MODFETCH_SEMVER_H
#include <stdbool.h>
#include <stdint.h>
typedef struct {
uint8_t major;
uint8_t minor;
uint8_t patch;
} semver;
bool cmpsv(semver, semver);
char *svtoa(semver);
#endif // _MODFETCH_SEMVER_H