From: Ayabusa Date: Thu, 11 Jun 2026 17:51:02 +0000 (+0200) Subject: cleaned up cli file X-Git-Url: https://git.ayabusa.dev/?a=commitdiff_plain;h=f5242336b32141b0a6ca2d21ec17d3fec8eb77e6;p=thoryum.git cleaned up cli file --- diff --git a/Makefile b/Makefile index d1dd355..d5c85f7 100644 --- a/Makefile +++ b/Makefile @@ -1,27 +1,21 @@ -CC = musl-gcc -CFLAGS = -Wall -Wextra -g -static -O3 -LIBS = /usr/local/lib/libcurl.a /usr/local/lib/libzip.a -lzip -lssl -lcrypto -lz -lbz2 -llzma -lzstd -lnghttp2 +DBG_CFLAGS = -Wall -Wextra -g -fsanitize=address +PROD_CFLAGS = -Wall -Wextra -Os +LIBS = -lzip -lcurl SRC = src/main.c src/cpu.c src/downloader.c src/cli.c src/unzip.c OBJ = build/main.o build/cpu.o build/downloader.o build/cli.o build/unzip.o RES = build/Thoryum -all: setup $(OBJ) build - $(CC) $(CFLAGS) $(OBJ) -o $(RES) $(LIBS) - -build/main.o: src/main.c - $(CC) $(CFLAGS) -c $^ -o $@ $(LIBS) - -build/cpu.o: src/cpu.c - $(CC) $(CFLAGS) -c $^ -o $@ $(LIBS) +release: CFLAGS = $(PROD_CFLAGS) +debug: CFLAGS = $(DBG_CFLAGS) -build/downloader.o: src/downloader.c - $(CC) $(CFLAGS) -c $^ -o $@ $(LIBS) +release: setup $(OBJ) + $(CC) $(CFLAGS) $(OBJ) -o $(RES) $(LIBS) -build/cli.o: src/cli.c - $(CC) $(CFLAGS) -c $^ -o $@ $(LIBS) +debug: setup $(OBJ) + $(CC) $(CFLAGS) $(OBJ) -o $(RES) $(LIBS) -build/unzip.o: src/unzip.c - $(CC) $(CFLAGS) -c $^ -o $@ $(LIBS) +build/%.o: src/%.c + $(CC) $(CFLAGS) -c $< -o $@ setup: mkdir -p build @@ -29,8 +23,5 @@ setup: run: ./$(RES) -debug: - gdb -tui $(RES) - clean: $(RM) $(OBJ) $(RES) diff --git a/src/cli.c b/src/cli.c index fdaf60d..fe6e287 100644 --- a/src/cli.c +++ b/src/cli.c @@ -2,6 +2,7 @@ #include #include #include +#include #include "cli.h" extern char *optarg; // What the hell is that glibc ??? @@ -19,24 +20,25 @@ struct ctx *init_ctx(int argc, char **argv) { check_help(argc, argv); struct ctx *ctx = calloc(1, sizeof(struct ctx)); - int opt; + if (!ctx) + err(1, "Failed to allocate space for the context, abort.\n"); + int opt; while ((opt = getopt(argc, argv, "ycl:")) != -1) { switch (opt) { - case 'y': - ctx->silent = 1; - break; - case 'c': - ctx->mode = UPDATE; - break; - case 'l': - ctx->install_loc = strdup(optarg); - break; - default: // bozo - fprintf(stderr, "Usage: %s [-h] [-c] [-y] [-l path]\n", - argv[0]); - free(ctx); - exit(EXIT_FAILURE); + case 'y': + ctx->silent = 1; + break; + case 'c': + ctx->mode = UPDATE; + break; + case 'l': + ctx->install_loc = strdup(optarg); + break; + default: // bozo + fprintf(stderr, "Usage: %s [-h] [-c] [-y] [-l path]\n", argv[0]); + free(ctx); + exit(EXIT_FAILURE); } } if (!ctx->install_loc) @@ -44,24 +46,29 @@ struct ctx *init_ctx(int argc, char **argv) { return ctx; } -void *free_ctx(struct ctx *ctx) { - free(ctx->install_loc); - if (ctx->opti_name) - free(ctx->opti_name); - free(ctx); - return NULL; +void free_ctx(struct ctx **ctx) { + if (!ctx || !*ctx) + return; + free((*ctx)->install_loc); + if ((*ctx)->opti_name) + free((*ctx)->opti_name); + free(*ctx); + *ctx = NULL; } void print_ctx(struct ctx *ctx) { - printf( - "Thoryum context:\n" - "Mode = %s\n" - "Install location = '%s'\n" - "Silent = %s\n", - ctx->mode ? "UPDATE" : "NORMAL", - ctx->install_loc, - ctx->silent ? "YES" : "NOPE" - ); + if (!ctx) + printf("Thorium context: (null)\n"); + else + printf( + "Thoryum context:\n" + "Mode = %s\n" + "Install location = '%s'\n" + "Silent = %s\n", + ctx->mode ? "UPDATE" : "NORMAL", + ctx->install_loc, + ctx->silent ? "YES" : "NOPE" + ); } void print_help() { diff --git a/src/cli.h b/src/cli.h index 858b090..339003d 100644 --- a/src/cli.h +++ b/src/cli.h @@ -14,7 +14,7 @@ struct ctx { }; struct ctx *init_ctx(int argc, char **argv); -void *free_ctx(struct ctx *ctx); +void free_ctx(struct ctx **ctx); void print_ctx(struct ctx *ctx); void print_help();