console-client: Enable use of a configuration file
However, only initialise a config if the option was provided. This
avoids searching for the default config, which may not exist and is
intended for the server.
Signed-off-by: Andrew Jeffery <andrew@aj.id.au>
Change-Id: I08f736243c1843bfb8855f5ceb69418d59a8775a
diff --git a/Makefile.am b/Makefile.am
index 059ac1b..ce84c4c 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -21,10 +21,14 @@
obmc_console_server_LDFLAGS = $(SYSTEMD_LIBS) -lrt
obmc_console_server_CFLAGS = $(SYSTEMD_CFLAGS)
+obmc_console_client_CPPFLAGS = \
+ -DSYSCONFDIR=\"$(sysconfdir)\"
+
obmc_console_client_SOURCES = \
console-client.c \
console-server.h \
console-socket.c \
+ config.c \
util.c
SUBDIRS = test
diff --git a/console-client.c b/console-client.c
index db8acd3..1d0a489 100644
--- a/console-client.c
+++ b/console-client.c
@@ -253,7 +253,10 @@
struct console_client _client, *client;
struct pollfd pollfds[2];
enum process_rc prc = PROCESS_OK;
+ const char *config_path = NULL;
+ struct config *config = NULL;
const char *socket_id = NULL;
+ const uint8_t *esc = NULL;
int rc;
client = &_client;
@@ -261,18 +264,24 @@
client->esc_type = ESC_TYPE_SSH;
for (;;) {
- rc = getopt(argc, argv, "e:i:");
+ rc = getopt(argc, argv, "c:e:i:");
if (rc == -1)
break;
switch (rc) {
+ case 'c':
+ if (optarg[0] == '\0') {
+ fprintf(stderr, "Config str cannot be empty\n");
+ return EXIT_FAILURE;
+ }
+ config_path = optarg;
+ break;
case 'e':
if (optarg[0] == '\0') {
fprintf(stderr, "Escape str cannot be empty\n");
return EXIT_FAILURE;
}
- client->esc_type = ESC_TYPE_STR;
- client->esc_state.str.str = (const uint8_t*)optarg;
+ esc = (const uint8_t*)optarg;
break;
case 'i':
if (optarg[0] == '\0') {
@@ -285,19 +294,39 @@
fprintf(stderr,
"Usage: %s "
"[-e <escape sequence>]"
- "[-i <socket ID>]\n",
+ "[-i <socket ID>]"
+ "[-c <config>]\n",
argv[0]);
return EXIT_FAILURE;
}
}
+ if (config_path) {
+ config = config_init(config_path);
+ if (!config) {
+ warnx("Can't read configuration, exiting.");
+ return EXIT_FAILURE;
+ }
+
+ if (!esc)
+ esc = (const uint8_t *)config_get_value(config, "escape-sequence");
+
+ if (!socket_id)
+ socket_id = config_get_value(config, "socket-id");
+ }
+
+ if (esc) {
+ client->esc_type = ESC_TYPE_STR;
+ client->esc_state.str.str = esc;
+ }
+
rc = client_init(client, socket_id);
if (rc)
- return EXIT_FAILURE;
+ goto out_config_fini;
rc = client_tty_init(client);
if (rc)
- goto out_fini;
+ goto out_client_fini;
for (;;) {
pollfds[0].fd = client->fd_in;
@@ -322,8 +351,13 @@
break;
}
-out_fini:
+out_client_fini:
client_fini(client);
+
+out_config_fini:
+ if (config_path)
+ config_fini(config);
+
if (prc == PROCESS_ESC)
return EXIT_ESCAPE;
return rc ? EXIT_FAILURE : EXIT_SUCCESS;
diff --git a/test/Makefile.am b/test/Makefile.am
index 1ddf5bd..8fc0839 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -18,3 +18,7 @@
AM_CPPFLAGS = -I$(top_srcdir)
AM_LDFLAGS = $(OESDK_TESTCASE_FLAGS)
+
+test_client_escape_CPPFLAGS = \
+ $(AM_CPPFLAGS) \
+ -DSYSCONFDIR=\"\"
diff --git a/test/test-client-escape.c b/test/test-client-escape.c
index eeae015..921009c 100644
--- a/test/test-client-escape.c
+++ b/test/test-client-escape.c
@@ -19,6 +19,7 @@
#include <stdio.h>
#define read __read
+#include "config.c"
#include "console-socket.c"
#define main __main
#include "console-client.c"