aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJan200101 <sentrycraft123@gmail.com>2021-06-13 18:45:41 +0200
committerJan200101 <sentrycraft123@gmail.com>2021-06-13 18:45:41 +0200
commit2352079d0c54955f69b9070fc022c5f099906805 (patch)
tree53cadfa7d7ae2545ae5c6428c1f4e694203f2e57
parente83d0567b736bb86515ebd651eb9e2a921dcaab3 (diff)
downloadpolecat-2352079d0c54955f69b9070fc022c5f099906805.tar.gz
polecat-2352079d0c54955f69b9070fc022c5f099906805.zip
rework command group logic to work linear, replace return with EXIT_*
-rw-r--r--src/command.h70
-rw-r--r--src/common.c2
-rw-r--r--src/common.h4
-rw-r--r--src/dxvk.c14
-rw-r--r--src/lutris.c8
-rw-r--r--src/main.c6
-rw-r--r--src/wine.c16
7 files changed, 55 insertions, 65 deletions
diff --git a/src/command.h b/src/command.h
index 5b6136f..5db65cf 100644
--- a/src/command.h
+++ b/src/command.h
@@ -30,7 +30,7 @@
{ \
fprintf(stderr, USAGE_STR MSG " <command>\n"); \
print_help(GROUP##_commands, ARRAY_LEN(GROUP##_commands), GROUP##_flags, ARRAY_LEN(GROUP##_flags)); \
- return 0; \
+ return EXIT_FAILURE; \
}
/*
@@ -41,52 +41,40 @@
#define COMMAND_GROUP(GROUP) \
int GROUP(int argc, char** argv)
-#define COMMAND_GROUP_BODY_COMMANDS(GROUP) \
- for (unsigned long 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);
-
-#define COMMAND_GROUP_BODY_FLAGS(GROUP) \
- uint8_t found; \
- \
- for (int j = 1; j < argc; ++j) \
- { \
- found = 0; \
- if (argv[j][0] != '-') continue; \
- if(!strcmp(argv[0], "wine") && !strcmp(argv[1], "run")) break;\
- \
- for (unsigned long i = 0; i < ARRAY_LEN(GROUP##_flags); ++i) \
- { \
- if ((GROUP##_flags[i].variant & ONE && argv[j][1] == GROUP##_flags[i].name[0]) || \
- (GROUP##_flags[i].variant & TWO && argv[j][1] == '-' \
- && !strcmp(GROUP##_flags[i].name, argv[j]+2))) \
- { \
- found = 1; \
- int retval = GROUP##_flags[i].func(0, NULL); \
- if (GROUP##_flags[i].returns) return retval; \
- } \
- } \
- \
- if (!found) \
- { \
- fprintf(stderr, NAME ": '%s' is not a flag\n", argv[j]); \
- return 0; \
- } \
- }
-
/*
* the body is split up so we can construct our own group function for
* e.g. ARGV0 parsing
*/
-#define COMMAND_GROUP_BODY(GROUP, FIRST, SECOND) \
+#define COMMAND_GROUP_BODY(GROUP) \
if (argc > 1) \
{ \
- COMMAND_GROUP_BODY_##FIRST(GROUP) \
- COMMAND_GROUP_BODY_##SECOND(GROUP) \
- \
- fprintf(stderr, NAME ": '%s' is not a command\n", argv[1]); \
- return 0; \
+ for (int i = 1; i < argc; ++i) \
+ { \
+ for (unsigned long j = 0; j < ARRAY_LEN(GROUP##_commands); ++j) \
+ { \
+ if (!strcmp(GROUP##_commands[j].name, argv[i])) \
+ { \
+ return GROUP##_commands[j].func(argc-i, argv+i); \
+ } \
+ } \
+ \
+ if (argv[i][0] != '-') continue; \
+ \
+ for (unsigned long j = 0; j < ARRAY_LEN(GROUP##_flags); ++j) \
+ { \
+ if ((GROUP##_flags[j].variant & ONE && argv[i][1] == GROUP##_flags[j].name[0] \
+ && argv[i][2] == '\0') || \
+ (GROUP##_flags[j].variant & TWO && argv[i][1] == '-' \
+ && !strcmp(GROUP##_flags[j].name, argv[i]+2))) \
+ { \
+ int retval = GROUP##_flags[j].func(0, NULL); \
+ if (GROUP##_flags[j].returns) return retval; \
+ } \
+ } \
+ } \
} \
- return GROUP##_help(argc-1, argv+1);
+ return GROUP##_help(argc-1, argv+1); \
+
/*
* the main command group function is only suppose to find given command
@@ -100,7 +88,7 @@
#define COMMAND_GROUP_FUNC(GROUP) \
COMMAND_GROUP(GROUP) \
{ \
- COMMAND_GROUP_BODY(GROUP, FLAGS, COMMANDS) \
+ COMMAND_GROUP_BODY(GROUP) \
} \
diff --git a/src/common.c b/src/common.c
index c5ee354..f12b3d6 100644
--- a/src/common.c
+++ b/src/common.c
@@ -17,7 +17,7 @@ int set_no_net(UNUSED int argc, UNUSED char** argv)
puts("[NO_NET] set");
#endif
no_net = 1;
- return 0;
+ return EXIT_SUCCESS;
}
void print_help(const struct Command* commands, const size_t commands_size,
diff --git a/src/common.h b/src/common.h
index 21bd727..d4cce1d 100644
--- a/src/common.h
+++ b/src/common.h
@@ -12,6 +12,8 @@
extern uint8_t no_net;
+typedef int (*func_t)(int, char**);
+
struct MemoryStruct {
uint8_t* memory;
size_t size;
@@ -19,7 +21,7 @@ struct MemoryStruct {
struct Command {
char* name;
- int (*func)(int, char**);
+ func_t func;
char* description;
};
diff --git a/src/dxvk.c b/src/dxvk.c
index 4ffbddd..962198f 100644
--- a/src/dxvk.c
+++ b/src/dxvk.c
@@ -96,7 +96,7 @@ COMMAND(dxvk, download)
{
fprintf(stderr, USAGE_STR " dxvk download <version>\n\nversions are obtained via '" NAME " dxvk list'\n");
}
- return 0;
+ return EXIT_SUCCESS;
}
COMMAND(dxvk, remove)
@@ -115,7 +115,7 @@ COMMAND(dxvk, remove)
if (!isDir(dxvkpath))
{
fprintf(stderr, "'%s' is not an downloaded DXVK version\n", dxvkver);
- return 0;
+ return EXIT_SUCCESS;
}
int retval = removeDir(dxvkpath);
@@ -133,7 +133,7 @@ COMMAND(dxvk, remove)
fprintf(stderr, USAGE_STR " dxvk remove <version>\n\nInstalled dxvk versions can be obtained by using '" NAME " dxvk list-installed\n");
- return 0;
+ return EXIT_SUCCESS;
}
COMMAND(dxvk, list)
@@ -156,7 +156,7 @@ COMMAND(dxvk, list)
}
}
- return 0;
+ return EXIT_SUCCESS;
}
COMMAND(dxvk, install)
@@ -173,7 +173,7 @@ COMMAND(dxvk, install)
if (!isDir(dxvkpath))
{
fprintf(stderr, "'%s' is not an downloaded DXVK version\n", dxvkver);
- return 0;
+ return EXIT_SUCCESS;
}
strncat(dxvkpath, DXVKSETUP, sizeof(dxvkpath) - strlen(dxvkpath) - 1);
@@ -207,7 +207,7 @@ COMMAND(dxvk, install)
}
- return 0;
+ return EXIT_SUCCESS;
}
COMMAND(dxvk, installed)
@@ -241,7 +241,7 @@ COMMAND(dxvk, installed)
closedir(dir);
}
- return 0;
+ return EXIT_SUCCESS;
}
COMMAND_HELP(dxvk, " dxvk")
diff --git a/src/lutris.c b/src/lutris.c
index e2903ee..f1c4e2d 100644
--- a/src/lutris.c
+++ b/src/lutris.c
@@ -81,7 +81,7 @@ COMMAND(lutris, search)
puts(USAGE_STR " lutris search <text>\nSearch for games and list their slug");
}
- return 0;
+ return EXIT_SUCCESS;
}
COMMAND(lutris, list)
@@ -137,7 +137,7 @@ COMMAND(lutris, list)
puts(USAGE_STR " lutris list <slug>\nList game installers");
}
- return 0;
+ return EXIT_SUCCESS;
}
COMMAND(lutris, install)
@@ -296,7 +296,7 @@ COMMAND(lutris, install)
lutris_freeInstaller(&installer);
}
- return 0;
+ return EXIT_SUCCESS;
}
COMMAND(lutris, info)
@@ -379,7 +379,7 @@ COMMAND(lutris, info)
{
puts(USAGE_STR " lutris info <installer-name>\nThe Name of an installer can be found by viewing the install script of an installer");
}
- return 0;
+ return EXIT_SUCCESS;
}
COMMAND_HELP(lutris, " lutris")
diff --git a/src/main.c b/src/main.c
index d865ecf..653a1b3 100644
--- a/src/main.c
+++ b/src/main.c
@@ -57,7 +57,7 @@ COMMAND_GROUP(main)
atexit(free_nargv);
}
#endif
- COMMAND_GROUP_BODY(main, COMMANDS, FLAGS)
+ COMMAND_GROUP_BODY(main)
}
COMMAND(main, env)
@@ -74,14 +74,14 @@ COMMAND(main, env)
printf("data dir\t\t%s\n", buffer);
- return 0;
+ return EXIT_SUCCESS;
}
COMMAND(main, version)
{
puts(VERSION);
- return 0;
+ return EXIT_SUCCESS;
}
COMMAND_HELP(main, "")
diff --git a/src/wine.c b/src/wine.c
index 4db7d4f..e843b35 100644
--- a/src/wine.c
+++ b/src/wine.c
@@ -103,7 +103,7 @@ COMMAND(wine, download)
{
fprintf(stderr, USAGE_STR " wine download [versions]\n\nversions are obtained via '" NAME " wine list'\n");
}
- return 0;
+ return EXIT_SUCCESS;
}
@@ -135,7 +135,7 @@ COMMAND(wine, remove)
if (!isDir(winepath))
{
fprintf(stderr, "'%s' is not an installed wine version\n", winever);
- return 0;
+ return EXIT_FAILURE;
}
}
@@ -154,7 +154,7 @@ COMMAND(wine, remove)
fprintf(stderr, USAGE_STR " wine remove <version>\n\nInstalled wine versions can be obtained by using '" NAME " wine list-installed\n");
- return 0;
+ return EXIT_SUCCESS;
}
COMMAND(wine, list)
@@ -181,7 +181,7 @@ COMMAND(wine, list)
json_object_put(runner);
}
- return 0;
+ return EXIT_SUCCESS;
}
COMMAND(wine, run)
@@ -250,7 +250,7 @@ COMMAND(wine, run)
fprintf(stderr, "Specify a what wine version to run.\nUse '" NAME " wine list-installed' to list available versions\n");
- return 0;
+ return EXIT_SUCCESS;
}
COMMAND(wine, installed)
@@ -285,7 +285,7 @@ COMMAND(wine, installed)
closedir(dir);
}
- return 0;
+ return EXIT_SUCCESS;
}
COMMAND(wine, env)
@@ -319,7 +319,7 @@ COMMAND(wine, env)
if (!isDir(winepath))
{
fprintf(stderr, "'%s' is not an installed wine version\n", winever);
- return 0;
+ return EXIT_FAILURE;
}
}
@@ -378,7 +378,7 @@ COMMAND(wine, env)
fprintf(stderr, "Specify a what wine version to run.\nUse '" NAME " wine list-installed' to list available versions\n");
}
- return 0;
+ return EXIT_SUCCESS;
}
COMMAND_HELP(wine, " wine")