From 4d0fcab10e91ad5962837f7dd428f5bca1c8c980 Mon Sep 17 00:00:00 2001 From: David Härdeman Date: Thu, 25 Jun 2020 17:01:24 +0200 Subject: Flesh out minecctl some more --- shared/ansi-colors.h | 12 ++++++++++++ shared/config-parser.c | 38 +++++++++++++++++++++++++++++++++++++- shared/config-parser.h | 5 +++++ shared/meson.build | 4 +++- shared/rcon-protocol.h | 1 + shared/utils.h | 1 + 6 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 shared/ansi-colors.h (limited to 'shared') diff --git a/shared/ansi-colors.h b/shared/ansi-colors.h new file mode 100644 index 0000000..ba89c46 --- /dev/null +++ b/shared/ansi-colors.h @@ -0,0 +1,12 @@ +#ifndef fooansicolorshfoo +#define fooansicolorshfoo + +#define ANSI_RED "\x1B[0;31m" +#define ANSI_GREEN "\x1B[0;32m" +#define ANSI_YELLOW "\x1B[0;33m" +#define ANSI_BLUE "\x1B[0;34m" +#define ANSI_MAGENTA "\x1B[0;35m" +#define ANSI_GREY "\x1B[0;38;5;245m" +#define ANSI_NORMAL "\x1B[0m" + +#endif diff --git a/shared/config-parser.c b/shared/config-parser.c index 9f294f4..1f44db4 100644 --- a/shared/config-parser.c +++ b/shared/config-parser.c @@ -6,9 +6,12 @@ #include #include #include +#include +#include #include "utils.h" #include "config-parser.h" +#include "config.h" static void eat_whitespace_and_comments(char **pos) @@ -162,7 +165,7 @@ out: return rv; } -static bool +bool strtosockaddrs(const char *str, struct cfg_value *rvalue, bool async) { struct saddr *saddr; @@ -478,3 +481,36 @@ config_parse_header(const char *title, char **buf) return false; } + +bool +is_valid_server_config_filename(struct dirent *dent, const char *filename) +{ + const char *suffix; + + assert_return(!(dent && filename) && !(!dent && !filename), false); + + /* Maybe accept DT_LNK? */ + if (dent) { + switch (dent->d_type) { + case DT_UNKNOWN: + _fallthrough_; + case DT_REG: + break; + default: + return false; + } + filename = dent->d_name; + } + + if (empty_str(filename)) + return false; + if (filename[0] == '.') + return false; + if ((suffix = strrchr(filename, '.')) == NULL) + return false; + if (!streq(suffix, "." SERVER_CONFIG_FILE_SUFFIX)) + return false; + + return true; +} + diff --git a/shared/config-parser.h b/shared/config-parser.h index 7d99e31..7c5703e 100644 --- a/shared/config-parser.h +++ b/shared/config-parser.h @@ -5,6 +5,7 @@ #include #include #include +#include enum cfg_value_type { CFG_VAL_TYPE_INVALID, @@ -47,6 +48,8 @@ struct cfg_value { }; }; +bool strtosockaddrs(const char *str, struct cfg_value *rvalue, bool async); + bool config_parse_line(const char *filename, char **buf, struct cfg_key_value_map *kvmap, int *rkey, const char **rkeyname, @@ -54,4 +57,6 @@ bool config_parse_line(const char *filename, char **buf, bool config_parse_header(const char *title, char **buf); +bool is_valid_server_config_filename(struct dirent *dent, const char *filename); + #endif diff --git a/shared/meson.build b/shared/meson.build index e4e4f29..ccfad4a 100644 --- a/shared/meson.build +++ b/shared/meson.build @@ -6,7 +6,9 @@ srcs_libshared = [ inc_libshared = include_directories('.') -deps_libshared = [] +deps_libshared = [ + dep_config_h, +] lib_libshared = static_library( 'shared', diff --git a/shared/rcon-protocol.h b/shared/rcon-protocol.h index 35997c4..097da1d 100644 --- a/shared/rcon-protocol.h +++ b/shared/rcon-protocol.h @@ -4,6 +4,7 @@ #include #include +/* FIXME: FAIL is an id, not type, LOGIN_OK should be LOGIN_RESPONSE */ enum rcon_packet_type { RCON_PACKET_LOGIN = 3, RCON_PACKET_LOGIN_OK = 2, diff --git a/shared/utils.h b/shared/utils.h index 769d3e3..3ed1c87 100644 --- a/shared/utils.h +++ b/shared/utils.h @@ -40,6 +40,7 @@ extern unsigned debug_mask; #include "list.h" #include "debug.h" #include "external.h" +#include "ansi-colors.h" /* Length of longest DNS name = 253 + trailing dot */ #define FQDN_STR_LEN 254 -- cgit v1.2.3