]> git.ayabusa.dev Git - thoryum.git/commitdiff
cleaned up cli file
authorAyabusa <lebgpub@gmail.com>
Thu, 11 Jun 2026 17:51:02 +0000 (19:51 +0200)
committerAyabusa <lebgpub@gmail.com>
Thu, 11 Jun 2026 17:51:02 +0000 (19:51 +0200)
Makefile
src/cli.c
src/cli.h

index d1dd355fe71027a8943253f1ab1fb528d621e635..d5c85f7f23f47265439154d57aba334ad04764c9 100644 (file)
--- 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)
index fdaf60d681d9c2305135455fc37d9188922fff5e..fe6e2875303694b678ad21e1b94e69e60e0f9af5 100644 (file)
--- a/src/cli.c
+++ b/src/cli.c
@@ -2,6 +2,7 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
+#include <err.h>
 #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() {
index 858b0902289e918951afdd513bb01b8a27cb11d4..339003d29d0e2923e1244bb8c3d5b5f6125cda4b 100644 (file)
--- 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();