diff --git a/src/configfile.c b/src/configfile.c index ef7b690..c9703dc 100644 --- a/src/configfile.c +++ b/src/configfile.c @@ -275,10 +275,9 @@ static int resolve_config_file(void) return -EINVAL; } -void config_file_parse(const int argc, char *argv[]) +void config_file_parse(void) { int ret; - int i; c = malloc(sizeof(struct config_t)); memset(c, 0, sizeof(struct config_t)); @@ -290,14 +289,8 @@ void config_file_parse(const int argc, char *argv[]) return; } - for (i = 1; i < argc; i++) - { - if (argv[i][0] != '-') - { - c->user = argv[i]; - break; - } - } + // Set user input which may be tty device or sub config + c->user = option.tty_device; if (!c->user) { @@ -333,6 +326,8 @@ void config_file_parse(const int argc, char *argv[]) fprintf(stderr, "Error: Unable to parse configuration file (%d)", ret); exit(EXIT_FAILURE); } + + atexit(&config_exit); } void config_exit(void) @@ -350,7 +345,7 @@ void config_exit(void) free(c); } -void config_file_print() +void config_file_print(void) { if (c->path != NULL) { diff --git a/src/configfile.h b/src/configfile.h index 57ae2a1..8fbea07 100644 --- a/src/configfile.h +++ b/src/configfile.h @@ -38,6 +38,6 @@ struct config_t char *map; }; -void config_file_print(); -void config_file_parse(const int argc, char *argv[]); +void config_file_print(void); +void config_file_parse(void); void config_exit(void); diff --git a/src/main.c b/src/main.c index 972320b..2791104 100644 --- a/src/main.c +++ b/src/main.c @@ -42,13 +42,15 @@ int main(int argc, char *argv[]) /* Add error exit handler */ atexit(&error_exit); - /* Parse configuration file */ - config_file_parse(argc, argv); - atexit(&config_exit); - - /* Parse command-line options */ + /* Parse command-line options (1st pass) */ options_parse(argc, argv); + /* Parse configuration file */ + config_file_parse(); + + /* Parse command-line options (2nd pass) */ + options_parse_final(argc, argv); + /* List available serial devices */ if (option.list_devices) { diff --git a/src/options.c b/src/options.c index e3b7895..77dd6a6 100644 --- a/src/options.c +++ b/src/options.c @@ -351,7 +351,7 @@ void options_parse(int argc, char *argv[]) if (strlen(option.tty_device) == 0) { - printf("Error: Missing device name\n"); + printf("Error: Missing device or config name\n"); exit(EXIT_FAILURE); } @@ -365,3 +365,16 @@ void options_parse(int argc, char *argv[]) exit(EXIT_FAILURE); } } + +void options_parse_final(int argc, char *argv[]) +{ + /* Preserve tty device which may have been set by configuration file */ + const char *tty_device = option.tty_device; + + /* Do 2nd pass to override settings set by configuration file */ + optind = 1; // Reset option index to restart scanning of argv + options_parse(argc, argv); + + /* Restore tty device */ + option.tty_device = tty_device; +} diff --git a/src/options.h b/src/options.h index b9bbcf0..313e2a2 100644 --- a/src/options.h +++ b/src/options.h @@ -63,3 +63,4 @@ extern struct option_t option; void options_print(); void options_parse(int argc, char *argv[]); +void options_parse_final(int argc, char *argv[]);