summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Härdeman <david@hardeman.nu>2020-07-07 17:21:39 +0200
committerDavid Härdeman <david@hardeman.nu>2020-07-07 17:21:39 +0200
commit0e2db6328946180c47bc09a486fe38ca0364e2af (patch)
tree61db007b4f1f9c00a26ea7d733eba5c162019c61
parent76ee64b5b3e30706181becbf33cc45d67f39f364 (diff)
Fix repeated calls to server_get_default()
-rw-r--r--minecctl/mc-commands.c4
-rw-r--r--minecctl/minecctl.c34
-rw-r--r--minecctl/server.c20
3 files changed, 38 insertions, 20 deletions
diff --git a/minecctl/mc-commands.c b/minecctl/mc-commands.c
index 133bfc0..7d44451 100644
--- a/minecctl/mc-commands.c
+++ b/minecctl/mc-commands.c
@@ -20,6 +20,10 @@ bool do_mc_pcount(struct cfg *cfg, unsigned *online, unsigned *max)
int fd;
server = server_get_default(cfg);
+ if (!server) {
+ error("failed to get default server");
+ return false;
+ }
fd = connect_any(&server->scfg.remotes, &saddr, &error);
if (fd < 0) {
diff --git a/minecctl/minecctl.c b/minecctl/minecctl.c
index a12d3ce..9a43fc3 100644
--- a/minecctl/minecctl.c
+++ b/minecctl/minecctl.c
@@ -151,7 +151,21 @@ void dump_config(struct cfg *cfg)
_noreturn_ static void usage(bool no_error)
{
- info("Usage: %s [OPTION...] COMMAND\n"
+ info("Usage: %s [OPTIONS...] COMMAND\n"
+ "\n"
+ "Valid commands:\n"
+ " list list known servers\n"
+ " lint check validity of server configuration files\n"
+ " status [SERVER] show status of SERVER (or all known servers)\n"
+ " ping [SERVER] check if SERVER is running\n"
+ " stop [SERVER] stop SERVER\n"
+ " stopall stop all known servers (including ADDR)\n"
+ " pcount [SERVER] get player count for SERVER\n"
+ " console [SERVER] interactive command line for SERVER\n"
+ " cmd [SERVER] CMD send CMD to SERVER\n"
+ " cmds [SERVER] CMDS... send multiple CMDS to SERVER\n"
+ " [SERVER] CMD shorthand for \"cmd [SERVER] CMD\"\n"
+ " [SERVER] shorthand for \"console [SERVER]\"\n"
"\n"
"Valid options:\n"
" -p, --rcon-password=PWD use PWD when connecting via rcon\n"
@@ -168,22 +182,10 @@ _noreturn_ static void usage(bool no_error)
" -d, --debug enable debugging information\n"
" -h, --help print this information\n"
"\n"
- "Valid commands:\n"
- " list list known servers\n"
- " lint check validity of server configuration files\n"
- " status [SERVER] show status of SERVER (or all known servers)\n"
- " ping [SERVER] check if SERVER is running\n"
- " stop [SERVER] stop SERVER\n"
- " stopall stop all known servers (including ADDR)\n"
- " pcount [SERVER] get player count for SERVER\n"
- " console [SERVER] interactive command line for SERVER\n"
- " cmd [SERVER] CMD send CMD to SERVER\n"
- " cmds [SERVER] CMDS... send multiple CMDS to SERVER\n"
- " [SERVER] CMD shorthand for \"cmd [SERVER] CMD\"\n"
- " [SERVER] shorthand for \"console [SERVER]\"\n"
- "\n"
"Note: if ADDR is given as an option, SERVER must be omitted from\n"
- " the command and vice versa.\n",
+ " the command and vice versa.\n"
+ "\n"
+ "See the minecctl(1) man page for details.\n",
program_invocation_short_name, DEFAULT_CFG_DIR);
exit(no_error ? EXIT_FAILURE : EXIT_SUCCESS);
diff --git a/minecctl/server.c b/minecctl/server.c
index fb59d35..979c15f 100644
--- a/minecctl/server.c
+++ b/minecctl/server.c
@@ -20,10 +20,21 @@ bool server_read_config(struct cfg *cfg, struct server *server,
int dfd;
int fd;
- if (!server || !server->scfg.filename || server->file_read ||
- !lineno || !error)
+ if (!error)
return false;
+ if (!server)
+ INVALID("Invalid argument, server missing");
+
+ if (!lineno)
+ INVALID("Invalid argument, lineno not set");
+
+ if (!server->scfg.filename)
+ INVALID("Invalid argument, filename not set");
+
+ if (server->file_read)
+ return true;
+
*lineno = 0;
server->file_read = true;
@@ -77,9 +88,10 @@ struct server *server_get_default(struct cfg *cfg)
if (!server)
die("No servers defined");
- /* FIXME: error msg */
- if (!server_read_config(cfg, server, &lineno, &error))
+ if (!server_read_config(cfg, server, &lineno, &error)) {
+ error("server_read_config error: %s", error);
return NULL;
+ }
return server;
}