Fix command-line tty-device|config parsing

Allow user to add options on both sides of the provided config argument.

For example:

 $ tio -b 9600 am64-evm -e

Before, tio only allowed adding arguments after the config argument.

Implemented as simple as possible by introducing two stage option parsing.
This commit is contained in:
Martin Lund 2022-06-11 22:55:56 +02:00
parent bd5f542959
commit a0d4be068b
5 changed files with 30 additions and 19 deletions

View file

@ -275,10 +275,9 @@ static int resolve_config_file(void)
return -EINVAL; return -EINVAL;
} }
void config_file_parse(const int argc, char *argv[]) void config_file_parse(void)
{ {
int ret; int ret;
int i;
c = malloc(sizeof(struct config_t)); c = malloc(sizeof(struct config_t));
memset(c, 0, 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; return;
} }
for (i = 1; i < argc; i++) // Set user input which may be tty device or sub config
{ c->user = option.tty_device;
if (argv[i][0] != '-')
{
c->user = argv[i];
break;
}
}
if (!c->user) 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); fprintf(stderr, "Error: Unable to parse configuration file (%d)", ret);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
atexit(&config_exit);
} }
void config_exit(void) void config_exit(void)
@ -350,7 +345,7 @@ void config_exit(void)
free(c); free(c);
} }
void config_file_print() void config_file_print(void)
{ {
if (c->path != NULL) if (c->path != NULL)
{ {

View file

@ -38,6 +38,6 @@ struct config_t
char *map; char *map;
}; };
void config_file_print(); void config_file_print(void);
void config_file_parse(const int argc, char *argv[]); void config_file_parse(void);
void config_exit(void); void config_exit(void);

View file

@ -42,13 +42,15 @@ int main(int argc, char *argv[])
/* Add error exit handler */ /* Add error exit handler */
atexit(&error_exit); atexit(&error_exit);
/* Parse configuration file */ /* Parse command-line options (1st pass) */
config_file_parse(argc, argv);
atexit(&config_exit);
/* Parse command-line options */
options_parse(argc, argv); 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 */ /* List available serial devices */
if (option.list_devices) if (option.list_devices)
{ {

View file

@ -351,7 +351,7 @@ void options_parse(int argc, char *argv[])
if (strlen(option.tty_device) == 0) if (strlen(option.tty_device) == 0)
{ {
printf("Error: Missing device name\n"); printf("Error: Missing device or config name\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -365,3 +365,16 @@ void options_parse(int argc, char *argv[])
exit(EXIT_FAILURE); 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;
}

View file

@ -63,3 +63,4 @@ extern struct option_t option;
void options_print(); void options_print();
void options_parse(int argc, char *argv[]); void options_parse(int argc, char *argv[]);
void options_parse_final(int argc, char *argv[]);