#include "timestamp.h"
#include "alert.h"
-static struct config_t *c;
+struct config_t
+{
+ const char *user;
+
+ char *path;
+ char *section_name;
+ char *match;
+
+ char *tty;
+ char *flow;
+ char *parity;
+ char *log_filename;
+ char *socket;
+ char *map;
+};
+
+static struct config_t c;
static int get_match(const char *input, const char *pattern, char **match)
{
UNUSED(user);
// If section matches current section being parsed
- if (!strcmp(section, c->section_name))
+ if (!strcmp(section, c.section_name))
{
// Set configuration parameter if found
if (!strcmp(name, "tty"))
{
- asprintf(&c->tty, value, c->match);
- option.tty_device = c->tty;
+ asprintf(&c.tty, value, c.match);
+ option.tty_device = c.tty;
}
else if (!strcmp(name, "baudrate"))
{
}
else if (!strcmp(name, "flow"))
{
- asprintf(&c->flow, "%s", value);
- option.flow = c->flow;
+ asprintf(&c.flow, "%s", value);
+ option.flow = c.flow;
}
else if (!strcmp(name, "stopbits"))
{
}
else if (!strcmp(name, "parity"))
{
- asprintf(&c->parity, "%s", value);
- option.parity = c->parity;
+ asprintf(&c.parity, "%s", value);
+ option.parity = c.parity;
}
else if (!strcmp(name, "output-delay"))
{
}
else if (!strcmp(name, "log-file"))
{
- asprintf(&c->log_filename, "%s", value);
- option.log_filename = c->log_filename;
+ asprintf(&c.log_filename, "%s", value);
+ option.log_filename = c.log_filename;
}
else if (!strcmp(name, "log-strip"))
{
}
else if (!strcmp(name, "map"))
{
- asprintf(&c->map, "%s", value);
- option.map = c->map;
+ asprintf(&c.map, "%s", value);
+ option.map = c.map;
}
else if (!strcmp(name, "color"))
{
}
else if (!strcmp(name, "socket"))
{
- asprintf(&c->socket, "%s", value);
- option.socket = c->socket;
+ asprintf(&c.socket, "%s", value);
+ option.socket = c.socket;
}
else if (!strcmp(name, "prefix-ctrl-key"))
{
if (strcmp(varname, "pattern"))
return 0;
- if (!strcmp(varval, c->user))
+ if (!strcmp(varval, c.user))
{
/* pattern matches as plain text */
- asprintf(&c->section_name, "%s", section);
+ asprintf(&c.section_name, "%s", section);
}
- else if (get_match(c->user, varval, &c->match) > 0)
+ else if (get_match(c.user, varval, &c.match) > 0)
{
/* pattern matches as regex */
- asprintf(&c->section_name, "%s", section);
+ asprintf(&c.section_name, "%s", section);
}
return 0;
UNUSED(varname);
UNUSED(varval);
- if (!strcmp(section, c->user))
+ if (!strcmp(section, c.user))
{
/* section name matches as plain text */
- asprintf(&c->section_name, "%s", section);
+ asprintf(&c.section_name, "%s", section);
+ }
+
+ return 0;
+}
+
+static int section_name_print_handler(void *user, const char *section, const char *varname,
+ const char *varval)
+{
+ UNUSED(user);
+ UNUSED(varname);
+ UNUSED(varval);
+
+ static char *section_previous = "";
+
+ if (strcmp(section, section_previous) != 0)
+ {
+ printf("%s ", section);
+ section_previous = strdup(section);
}
return 0;
static int resolve_config_file(void)
{
- asprintf(&c->path, "%s/tio/tiorc", getenv("XDG_CONFIG_HOME"));
- if (!access(c->path, F_OK))
+ asprintf(&c.path, "%s/tio/tiorc", getenv("XDG_CONFIG_HOME"));
+ if (!access(c.path, F_OK))
{
return 0;
}
- free(c->path);
+ free(c.path);
- asprintf(&c->path, "%s/.config/tio/tiorc", getenv("HOME"));
- if (!access(c->path, F_OK))
+ asprintf(&c.path, "%s/.config/tio/tiorc", getenv("HOME"));
+ if (!access(c.path, F_OK))
{
return 0;
}
- free(c->path);
+ free(c.path);
- asprintf(&c->path, "%s/.tiorc", getenv("HOME"));
- if (!access(c->path, F_OK))
+ asprintf(&c.path, "%s/.tiorc", getenv("HOME"));
+ if (!access(c.path, F_OK))
{
return 0;
}
- free(c->path);
+ free(c.path);
- c->path = NULL;
+ c.path = NULL;
return -EINVAL;
}
+void config_file_show_sub_configurations(void)
+{
+ memset(&c, 0, sizeof(struct config_t));
+
+ // Find config file
+ if (resolve_config_file() != 0)
+ {
+ // None found - stop parsing
+ return;
+ }
+
+ ini_parse(c.path, section_name_print_handler, NULL);
+}
+
void config_file_parse(void)
{
int ret;
- c = malloc(sizeof(struct config_t));
- if (!c)
- {
- tio_error_printf("Insufficient memory allocation");
- exit(EXIT_FAILURE);
- }
- memset(c, 0, sizeof(struct config_t));
+ memset(&c, 0, sizeof(struct config_t));
// Find config file
if (resolve_config_file() != 0)
}
// Set user input which may be tty device or sub config
- c->user = option.tty_device;
+ c.user = option.tty_device;
- if (!c->user)
+ if (!c.user)
{
return;
}
// Parse default (unnamed) settings
- asprintf(&c->section_name, "%s", "");
- ret = ini_parse(c->path, data_handler, NULL);
+ asprintf(&c.section_name, "%s", "");
+ ret = ini_parse(c.path, data_handler, NULL);
if (ret < 0)
{
tio_error_printf("Unable to parse configuration file (%d)", ret);
exit(EXIT_FAILURE);
}
- free(c->section_name);
- c->section_name = NULL;
+ free(c.section_name);
+ c.section_name = NULL;
// Find matching section
- ret = ini_parse(c->path, section_pattern_search_handler, NULL);
- if (!c->section_name)
+ ret = ini_parse(c.path, section_pattern_search_handler, NULL);
+ if (!c.section_name)
{
- ret = ini_parse(c->path, section_name_search_handler, NULL);
- if (!c->section_name)
+ ret = ini_parse(c.path, section_name_search_handler, NULL);
+ if (!c.section_name)
{
tio_debug_printf("Unable to match user input to configuration section (%d)", ret);
return;
}
// Parse settings of found section (sub config)
- ret = ini_parse(c->path, data_handler, NULL);
+ ret = ini_parse(c.path, data_handler, NULL);
if (ret < 0)
{
tio_error_printf("Unable to parse configuration file (%d)", ret);
void config_exit(void)
{
- free(c->tty);
- free(c->flow);
- free(c->parity);
- free(c->log_filename);
- free(c->map);
-
- free(c->match);
- free(c->section_name);
- free(c->path);
-
- free(c);
+ free(c.tty);
+ free(c.flow);
+ free(c.parity);
+ free(c.log_filename);
+ free(c.map);
+
+ free(c.match);
+ free(c.section_name);
+ free(c.path);
}
void config_file_print(void)
{
- if (c->path != NULL)
+ if (c.path != NULL)
{
- tio_printf(" Path: %s", c->path);
- if (c->section_name != NULL)
+ tio_printf(" Path: %s", c.path);
+ if (c.section_name != NULL)
{
- tio_printf(" Active sub-configuration: %s", c->section_name);
+ tio_printf(" Active sub-configuration: %s", c.section_name);
}
}
}