summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Härdeman <david@hardeman.nu>2020-06-26 22:50:18 +0200
committerDavid Härdeman <david@hardeman.nu>2020-06-26 22:50:18 +0200
commita6b905895fef7bdd51781def5c003a95983a231e (patch)
tree8fe82c36087ec661b795b9dc438485eabd3668be
parent13877c74295289676cee515ec42b3c1b22ef0bdf (diff)
Pass struct cfg throughout
-rw-r--r--minecctl/minecctl.c62
-rw-r--r--minecctl/server.c2
-rw-r--r--minecctl/server.h2
3 files changed, 32 insertions, 34 deletions
diff --git a/minecctl/minecctl.c b/minecctl/minecctl.c
index 324f0ef..c00a994 100644
--- a/minecctl/minecctl.c
+++ b/minecctl/minecctl.c
@@ -21,10 +21,8 @@
#include "config-parser.h"
#include "config.h"
-static struct cfg *cfg = NULL;
-
static void
-dump_config()
+dump_config(struct cfg *cfg)
{
/* FIXME: return unless debugging enabled */
struct server *server;
@@ -126,7 +124,7 @@ out:
}
static bool
-create_server_from_cmdline_args()
+create_server_from_cmdline_args(struct cfg *cfg)
{
struct server *server;
@@ -180,7 +178,7 @@ do_list(struct cfg *cfg)
}
static inline void
-get_optional_server_arg(char * const **argv, bool more)
+get_optional_server_arg(struct cfg *cfg, char * const **argv, bool more)
{
if (!cfg->rcon_addrstr) {
if (!**argv) {
@@ -203,7 +201,7 @@ get_optional_server_arg(char * const **argv, bool more)
}
static void
-parse_command(char * const *argv)
+parse_command(struct cfg *cfg, char * const *argv)
{
enum commands cmd = CMD_INVALID;
@@ -244,27 +242,27 @@ parse_command(char * const *argv)
cfg->cmd = do_stop_all;
break;
case CMD_STATUS:
- get_optional_server_arg(&argv, false);
+ get_optional_server_arg(cfg, &argv, false);
cfg->cmd = do_status;
break;
case CMD_PING:
- get_optional_server_arg(&argv, false);
+ get_optional_server_arg(cfg, &argv, false);
cfg->cmd = do_ping;
break;
case CMD_STOP:
- get_optional_server_arg(&argv, false);
+ get_optional_server_arg(cfg, &argv, false);
cfg->cmd = do_stop;
break;
case CMD_PCOUNT:
- get_optional_server_arg(&argv, false);
+ get_optional_server_arg(cfg, &argv, false);
cfg->cmd = do_pcount;
break;
case CMD_CONSOLE:
- get_optional_server_arg(&argv, false);
+ get_optional_server_arg(cfg, &argv, false);
cfg->cmd = do_console;
break;
case CMD_COMMAND:
- get_optional_server_arg(&argv, true);
+ get_optional_server_arg(cfg, &argv, true);
if (!*argv) {
error("Missing arguments");
@@ -276,7 +274,7 @@ parse_command(char * const *argv)
break;
case CMD_INVALID:
/* shorthand notation */
- get_optional_server_arg(&argv, true);
+ get_optional_server_arg(cfg, &argv, true);
if (!*argv) {
/* !CMD = console */
@@ -293,7 +291,7 @@ parse_command(char * const *argv)
}
static void
-parse_cmdline(int argc, char * const *argv)
+parse_cmdline(struct cfg *cfg, int argc, char * const *argv)
{
int c;
char *e;
@@ -377,45 +375,45 @@ parse_cmdline(int argc, char * const *argv)
int
main(int argc, char **argv)
{
- int rv = EXIT_FAILURE;
+ struct cfg cfg = {
+ .servers = LIST_HEAD_INIT(cfg.servers),
+ };
struct server *server, *tmp;
+ int rv = EXIT_FAILURE;
debug_mask = DBG_ERROR | DBG_INFO;
set_use_colors();
- cfg = zmalloc(sizeof(*cfg));
- INIT_LIST_HEAD(&cfg->servers);
+ parse_cmdline(&cfg, argc, argv);
- parse_cmdline(argc, argv);
+ server_load_all_known(&cfg);
- server_load_known(cfg);
+ parse_command(&cfg, &argv[optind]);
- parse_command(&argv[optind]);
-
- if (!cfg->cmd) {
+ if (!cfg.cmd) {
error("Failed to parse command");
goto out;
}
- if (cfg->rcon_addrstr || cfg->mc_addrstr)
- if (!create_server_from_cmdline_args())
+ if (cfg.rcon_addrstr || cfg.mc_addrstr)
+ if (!create_server_from_cmdline_args(&cfg))
goto out;
- dump_config();
+
+ dump_config(&cfg);
/* FIXME: Should return bool */
- cfg->cmd(cfg);
+ cfg.cmd(&cfg);
rv = EXIT_SUCCESS;
out:
- list_for_each_entry_safe(server, tmp, &cfg->servers, list)
+ list_for_each_entry_safe(server, tmp, &cfg.servers, list)
server_free(server);
- free_password(&cfg->rcon_password);
- xfree(cfg->rcon_addrstr);
- xfree(cfg->mc_addrstr);
- xfree(cfg->cmdstr);
- xfree(cfg);
+ free_password(&cfg.rcon_password);
+ xfree(cfg.rcon_addrstr);
+ xfree(cfg.mc_addrstr);
+ xfree(cfg.cmdstr);
exit(rv);
}
diff --git a/minecctl/server.c b/minecctl/server.c
index 7526bb6..d19b921 100644
--- a/minecctl/server.c
+++ b/minecctl/server.c
@@ -129,7 +129,7 @@ server_set_default(struct cfg *cfg, const char *name)
}
void
-server_load_known(struct cfg *cfg)
+server_load_all_known(struct cfg *cfg)
{
struct dirent *dent;
DIR *dir;
diff --git a/minecctl/server.h b/minecctl/server.h
index ccccf06..08faa9f 100644
--- a/minecctl/server.h
+++ b/minecctl/server.h
@@ -19,7 +19,7 @@ struct server *server_get_default(struct cfg *cfg);
bool server_set_default(struct cfg *cfg, const char *name);
-void server_load_known(struct cfg *cfg);
+void server_load_all_known(struct cfg *cfg);
void server_free(struct server *server);