Merge pull request #144 from pcc/config-section-name

Match user input against config section names if pattern matching was…
This commit is contained in:
Martin Lund 2022-04-21 01:29:24 +02:00 committed by GitHub
commit 2e0b0386dc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 6 deletions

View file

@ -182,6 +182,9 @@ Sections can be used to group settings and their names are only used for readabi
.TP .TP
tio will try to match the user input to a section pattern to get the tty and other options. tio will try to match the user input to a section pattern to get the tty and other options.
.TP
If pattern matching fails, tio will try to match the user input to a section name.
.TP .TP
Options without any section name sets the default options. Options without any section name sets the default options.

View file

@ -162,14 +162,14 @@ static int data_handler(void *user, const char *section, const char *name,
} }
/** /**
* section_search_handler() - walk config file to find section matching user input * section_pattern_search_handler() - walk config file to find section matching user input
* *
* INIH handler used to resolve the section matching the user's input. * INIH handler used to resolve the section matching the user's input.
* This will look for the pattern element of each section and try to match it * This will look for the pattern element of each section and try to match it
* with the user input. * with the user input.
*/ */
static int section_search_handler(void *user, const char *section, const char static int section_pattern_search_handler(void *user, const char *section, const char *varname,
*varname, const char *varval) const char *varval)
{ {
UNUSED(user); UNUSED(user);
@ -190,6 +190,28 @@ static int section_search_handler(void *user, const char *section, const char
return 0; return 0;
} }
/**
* section_pattern_search_handler() - walk config file to find section matching user input
*
* INIH handler used to resolve the section matching the user's input.
* This will try to match the user input against a section with the name of the user input.
*/
static int section_name_search_handler(void *user, const char *section, const char *varname,
const char *varval)
{
UNUSED(user);
UNUSED(varname);
UNUSED(varval);
if (!strcmp(section, c->user))
{
/* section name matches as plain text */
asprintf(&c->section_name, "%s", section);
}
return 0;
}
static int resolve_config_file(void) static int resolve_config_file(void)
{ {
asprintf(&c->path, "%s/tio/tiorc", getenv("XDG_CONFIG_HOME")); asprintf(&c->path, "%s/tio/tiorc", getenv("XDG_CONFIG_HOME"));
@ -237,11 +259,15 @@ void config_file_parse(const int argc, char *argv[])
return; return;
} }
ret = ini_parse(c->path, section_search_handler, NULL); ret = ini_parse(c->path, section_pattern_search_handler, NULL);
if (!c->section_name) if (!c->section_name)
{ {
debug_printf("unable to match user input to configuration section (%d)\n", ret); ret = ini_parse(c->path, section_name_search_handler, NULL);
return; if (!c->section_name)
{
debug_printf("unable to match user input to configuration section (%d)\n", ret);
return;
}
} }
ret = ini_parse(c->path, data_handler, NULL); ret = ini_parse(c->path, data_handler, NULL);