From 8dc996f6d89652d71c6c4fff87f7923bbfbb9a34 Mon Sep 17 00:00:00 2001 From: Jan200101 Date: Fri, 11 Dec 2020 20:13:43 +0100 Subject: move command definitions (as well as helpers) to macros --- src/command.h | 32 ++++++++++++++++++++++++++++++++ src/dxvk.c | 37 ++++++++++++------------------------- src/dxvk.h | 14 ++++++++------ src/lutris.c | 27 ++++----------------------- src/lutris.h | 10 ++++++---- src/main.c | 24 +++--------------------- src/main.h | 6 ++++-- src/wine.c | 39 +++++++++++++-------------------------- src/wine.h | 16 +++++++++------- 9 files changed, 91 insertions(+), 114 deletions(-) create mode 100644 src/command.h (limited to 'src') diff --git a/src/command.h b/src/command.h new file mode 100644 index 0000000..9aeb07e --- /dev/null +++ b/src/command.h @@ -0,0 +1,32 @@ +#ifndef COMMAND_H +#define COMMAND_H + +#include +#include "common.h" + +#define COMMAND(GROUP, COMMAND)\ +int GROUP##_##COMMAND(int argc, char** argv) + + +#define COMMAND_HELP(GROUP, MSG) \ + COMMAND(GROUP, help) \ + { \ + fprintf(stderr, USAGE_STR MSG " \n\nList of commands:\n"); \ + print_help(GROUP##_commands, ARRAY_LEN(GROUP##_commands)); \ + return 0; \ + } + +#define COMMAND_GROUP(GROUP) \ + int GROUP(int argc, char** argv) \ + + +#define COMMAND_GROUP_FUNC(GROUP) \ + COMMAND_GROUP(GROUP) \ + { \ + if (argc > 1) \ + for (int i = 0; i < ARRAY_LEN(GROUP##_commands); ++i) \ + if (!strcmp(GROUP##_commands[i].name, argv[1])) return GROUP##_commands[i].func(argc-1, argv+1); \ + return GROUP##_help(argc-1, argv+1); \ + } + +#endif \ No newline at end of file diff --git a/src/dxvk.c b/src/dxvk.c index 67e42aa..2cd71b0 100644 --- a/src/dxvk.c +++ b/src/dxvk.c @@ -15,26 +15,15 @@ const static struct Command dxvk_commands[] = { { .name = "download", .func = dxvk_download, .description = "download and install a dxvk version" }, + { .name = "remove", .func = dxvk_remove, .description = "remove a dxvk version" }, { .name = "list", .func = dxvk_list, .description = "list available dxvk versions" }, { .name = "install", .func = dxvk_install, .description = "run the DXVK installer" }, { .name = "list-installed", .func = dxvk_installed, .description = "list installed dxvk versions" }, }; -int dxvk(int argc, char** argv) -{ - if (argc > 1) - { - for (int i = 0; i < ARRAY_LEN(dxvk_commands); ++i) - { - if (!strcmp(dxvk_commands[i].name, argv[1])) return dxvk_commands[i].func(argc-1, argv+1); - } - } - - return dxvk_help(argc-1, argv+1); -} - +COMMAND_GROUP_FUNC(dxvk) -int dxvk_download(int argc, char** argv) +COMMAND(dxvk, download) { if (argc == 2) { @@ -93,7 +82,12 @@ int dxvk_download(int argc, char** argv) return 0; } -int dxvk_list(int argc, char** argv) +COMMAND(dxvk, remove) +{ + return 0; +} + +COMMAND(dxvk, list) { struct json_object* runner = fetchJSON(DXVK_API); @@ -114,7 +108,7 @@ int dxvk_list(int argc, char** argv) return 0; } -int dxvk_install(int argc, char** argv) +COMMAND(dxvk, install) { if (argc > 1) { @@ -154,7 +148,7 @@ int dxvk_install(int argc, char** argv) return 0; } -int dxvk_installed(int argc, char** argv) +COMMAND(dxvk, installed) { char dxvkdir[PATH_MAX]; getDXVKDir(dxvkdir, sizeof(dxvkdir)); @@ -181,11 +175,4 @@ int dxvk_installed(int argc, char** argv) return 0; } -int dxvk_help(int argc, char** argv) -{ - fprintf(stderr, USAGE_STR " dxvk \n\nList of commands:\n"); - - print_help(dxvk_commands, ARRAY_LEN(dxvk_commands)); - - return 0; -} \ No newline at end of file +COMMAND_HELP(dxvk, " dxvk"); \ No newline at end of file diff --git a/src/dxvk.h b/src/dxvk.h index e68814a..0741e2c 100644 --- a/src/dxvk.h +++ b/src/dxvk.h @@ -3,11 +3,13 @@ #define DXVKSETUP "/setup_dxvk.sh" -int dxvk(int, char**); -int dxvk_download(int, char**); -int dxvk_install(int, char**); -int dxvk_installed(int, char**); -int dxvk_list(int, char**); -int dxvk_help(int, char**); +#include "command.h" + +COMMAND_GROUP(dxvk); +COMMAND(dxvk, download); +COMMAND(dxvk, list); +COMMAND(dxvk, install); +COMMAND(dxvk, installed); +COMMAND(dxvk, help); #endif \ No newline at end of file diff --git a/src/lutris.c b/src/lutris.c index c57c866..75d66e0 100644 --- a/src/lutris.c +++ b/src/lutris.c @@ -16,21 +16,9 @@ const static struct Command lutris_commands[] = { { .name = "info", .func = lutris_info, .description = "show information about a lutris script" }, }; -int lutris(int argc, char** argv) -{ - if (argc > 1) - { - for (int i = 0; i < ARRAY_LEN(lutris_commands); ++i) - { - if (!strcmp(lutris_commands[i].name, argv[1])) return lutris_commands[i].func(argc-1, argv+1); - } - } - - return lutris_help(argc-1, argv+1); -} +COMMAND_GROUP_FUNC(lutris) - -int lutris_install(int argc, char** argv) +COMMAND(lutris, install) { if (argc == 2) { @@ -149,7 +137,7 @@ int lutris_install(int argc, char** argv) return 0; } -int lutris_info(int argc, char** argv) +COMMAND(lutris, info) { if (argc == 2) { @@ -207,14 +195,7 @@ int lutris_info(int argc, char** argv) return 0; } -int lutris_help(int argc, char** argv) -{ - puts(USAGE_STR " lutris \n\nList of commands:"); - - print_help(lutris_commands, ARRAY_LEN(lutris_commands)); - - return 0; -} +COMMAND_HELP(lutris, " lutris") void lutris_getInstallerURL(char* buffer, char* name, size_t size) { diff --git a/src/lutris.h b/src/lutris.h index 3dfcef2..d0fe782 100644 --- a/src/lutris.h +++ b/src/lutris.h @@ -135,10 +135,12 @@ struct script_t { enum errors error; }; -int lutris(int, char**); -int lutris_install(int, char**); -int lutris_info(int, char**); -int lutris_help(int, char**); +#include "command.h" + +COMMAND_GROUP(lutris); +COMMAND(lutris, install); +COMMAND(lutris, info); +COMMAND(lutris, help); void lutris_getInstallerURL(char*, char*, size_t); struct script_t lutris_getInstaller(char*); diff --git a/src/main.c b/src/main.c index 352694f..13364dc 100644 --- a/src/main.c +++ b/src/main.c @@ -18,20 +18,9 @@ const static struct Command main_commands[] = { }; -int main(int argc, char** argv) -{ - if (argc > 1) - { - for (int i = 0; i < ARRAY_LEN(main_commands); ++i) - { - if (!strcmp(main_commands[i].name, argv[1])) return main_commands[i].func(argc-1, argv+1); - } - } - - return main_help(argc-1, argv+1); -} +COMMAND_GROUP_FUNC(main) -int main_env(int argc, char** argv) +COMMAND(main, env) { char buffer[PATH_MAX]; @@ -51,11 +40,4 @@ int main_env(int argc, char** argv) return 0; } -int main_help(int argc, char** argv) -{ - puts(USAGE_STR " \n\nList of commands:"); - - print_help(main_commands, ARRAY_LEN(main_commands)); - - return 0; -} \ No newline at end of file +COMMAND_HELP(main, "") \ No newline at end of file diff --git a/src/main.h b/src/main.h index ef535fd..9a26b6a 100644 --- a/src/main.h +++ b/src/main.h @@ -1,7 +1,9 @@ #ifndef MAIN_H #define MAIN_H -int main_help(int, char**); -int main_env(int, char**); +#include "command.h" + +COMMAND(main, env); +COMMAND(main, help); #endif \ No newline at end of file diff --git a/src/wine.c b/src/wine.c index 85fd36a..42d5cfc 100644 --- a/src/wine.c +++ b/src/wine.c @@ -20,27 +20,16 @@ const static struct Command wine_commands[] = { { .name = "download", .func = wine_download, .description = "download and extract a wine version" }, + { .name = "remove", .func = wine_remove, .description = "remove a wine version" }, { .name = "list", .func = wine_list, .description = "list installable wine versions" }, { .name = "run", .func = wine_run, .description = "run an installed wine version" }, { .name = "list-installed", .func = wine_installed, .description = "list already installed wine versions" }, { .name = "env", .func = wine_env, .description = "add wine to your PATH in a POSIX shell"}, }; -int wine(int argc, char** argv) -{ - if (argc > 1) - { - for (int i = 0; i < ARRAY_LEN(wine_commands); ++i) - { - if (!strcmp(wine_commands[i].name, argv[1])) return wine_commands[i].func(argc-1, argv+1); - } - } - - - return wine_help(argc-1, argv+1); -} +COMMAND_GROUP_FUNC(wine) -int wine_download(int argc, char** argv) +COMMAND(wine, download) { if (argc == 2) { @@ -98,7 +87,12 @@ int wine_download(int argc, char** argv) return 0; } -int wine_list(int argc, char** argv) +COMMAND(wine, remove) +{ + return 0; +} + +COMMAND(wine, list) { struct json_object* runner = fetchJSON(WINE_API); @@ -122,7 +116,7 @@ int wine_list(int argc, char** argv) return 0; } -int wine_run(int argc, char** argv) +COMMAND(wine, run) { if (argc > 1) { @@ -200,7 +194,7 @@ int wine_run(int argc, char** argv) return 0; } -int wine_installed(int argc, char** argv) +COMMAND(wine, installed) { char winedir[PATH_MAX]; getWineDir(winedir, sizeof(winedir)); @@ -228,7 +222,7 @@ int wine_installed(int argc, char** argv) return 0; } -int wine_env(int argc, char** argv) +COMMAND(wine, env) { if (argc > 1) { @@ -310,14 +304,7 @@ int wine_env(int argc, char** argv) return 0; } -int wine_help(int argc, char** argv) -{ - fprintf(stderr, USAGE_STR " wine \n\nList of commands:\n"); - - print_help(wine_commands, ARRAY_LEN(wine_commands)); - - return 0; -} +COMMAND_HELP(wine, " wine"); enum wine_type_t check_wine_ver(char* winepath, size_t size) { diff --git a/src/wine.h b/src/wine.h index 0a634d2..48994ba 100644 --- a/src/wine.h +++ b/src/wine.h @@ -4,6 +4,8 @@ #define WINEBIN "/bin/wine" #define PROTONBIN "/dist/bin/wine" +#include "command.h" + // enum type used to represent if the installed wine version is proton or normal wine // this is mainly used to know wheither to call WINEBIN or PROTONBIN enum wine_type_t { @@ -12,13 +14,13 @@ enum wine_type_t { WINE_PROTON }; -int wine(int, char**); -int wine_download(int, char**); -int wine_list(int, char**); -int wine_run(int, char**); -int wine_installed(int, char**); -int wine_env(int, char**); -int wine_help(int, char**); +COMMAND_GROUP(wine); +COMMAND(wine, download); +COMMAND(wine, list); +COMMAND(wine, run); +COMMAND(wine, installed); +COMMAND(wine, env); +COMMAND(wine, help); enum wine_type_t check_wine_ver(char*, size_t); -- cgit v1.2.3