From 05da5ef05778d0653685a20e9e5847b221a54f59 Mon Sep 17 00:00:00 2001 From: Mosder Date: Thu, 23 Jul 2026 13:59:15 +0200 Subject: refactor: generalized command parsing and moved it into a separate file --- main.c | 56 ++++++++++---------------------------------------------- 1 file changed, 10 insertions(+), 46 deletions(-) (limited to 'main.c') diff --git a/main.c b/main.c index eadb516..38dfc36 100644 --- a/main.c +++ b/main.c @@ -1,54 +1,18 @@ -#include -#include - -#include "analyzer/analyzer.h" -#include "analyzer/capture.h" -#include "analyzer/utils/fs.h" - -// prints help -void print_help() { - printf("A bare-bones analyzer for the game Broken Ranks\n"); - printf("\nUsage:\n"); - printf(" branal - start live analysis\n"); - printf(" branal (l | live) - start live analysis (same as above)\n"); - printf(" branal (c | capture) [file] - start capturing packets to [file].\n"); - printf(" If [file] not given - captures to $HOME/.local/share/branal/captures\n"); - printf(" branal (f | file) - analyze the specified \n"); - printf(" branal (h | help) - display this help\n"); -} - -// starts live analyzer mode -void start_live_analyzer() { - // TODO: implement - printf("Not implemented\n"); -} +#include "commands.h" +#include "utils/fs.h" int main(int argc, char *argv[]) { create_necessary_directories(); - // start live analysis if no arguments provided - if (argc < 2) { - start_live_analyzer(); - return 0; - } + // start first command if no arguments provided + if (argc < 2) + return commands[0].handler(argc, argv); - if (strcmp("l", argv[1]) == 0 || strcmp("live", argv[1]) == 0) { - start_live_analyzer(); - } - else if (strcmp("c", argv[1]) == 0 || strcmp("capture", argv[1]) == 0) { - capturing_mode(argc >= 3 ? argv[2] : NULL); - } - else if (strcmp("h", argv[1]) == 0 || strcmp("help", argv[1]) == 0) { - print_help(); - } - else if (argc >= 3 && (strcmp("f", argv[1]) == 0 || strcmp("file", argv[1]) == 0)) { - analyze_file(argv[2]); - } - else { - printf("Command not recognized\n"); - printf("Use \"branal (h | help)\" for help\n"); - return 1; + for (size_t i = 0; i < commands_count; i++) { + if (is_command(commands[i], argv[1])) + return commands[i].handler(argc, argv); } - return 0; + command_not_recognized(); + return 1; } -- cgit v1.2.3