Config file path can be changed by command line option "--config <path>"

This commit is contained in:
Andreas Eversberg 2021-03-11 07:38:00 +01:00
parent ca9d531643
commit 3d9481177b
18 changed files with 76 additions and 30 deletions

View File

@ -242,11 +242,11 @@ int main_amps_tacs(const char *name, int argc, char *argv[])
/* handle options / config file */
add_options();
if (!tacs) {
rc = options_config_file("~/.osmocom/analog/amps.conf", handle_options);
rc = options_config_file(argc, argv, "~/.osmocom/analog/amps.conf", handle_options);
} else if (!jtacs) {
rc = options_config_file("~/.osmocom/analog/tacs.conf", handle_options);
rc = options_config_file(argc, argv, "~/.osmocom/analog/tacs.conf", handle_options);
} else {
rc = options_config_file("~/.osmocom/analog/jtacs.conf", handle_options);
rc = options_config_file(argc, argv, "~/.osmocom/analog/jtacs.conf", handle_options);
}
if (rc < 0)
return 0;

View File

@ -138,7 +138,7 @@ int main(int argc, char *argv[])
/* handle options / config file */
add_options();
rc = options_config_file("~/.osmocom/analog/anetz.conf", handle_options);
rc = options_config_file(argc, argv, "~/.osmocom/analog/anetz.conf", handle_options);
if (rc < 0)
return 0;
argi = options_command_line(argc, argv, handle_options);

View File

@ -139,7 +139,7 @@ int main(int argc, char *argv[])
/* handle options / config file */
add_options();
rc = options_config_file("~/.osmocom/analog/bnetz.conf", handle_options);
rc = options_config_file(argc, argv, "~/.osmocom/analog/bnetz.conf", handle_options);
if (rc < 0)
return 0;
argi = options_command_line(argc, argv, handle_options);

View File

@ -479,7 +479,7 @@ int main(int argc, char *argv[])
/* handle options / config file */
add_options();
rc = options_config_file("~/.osmocom/analog/cnetz.conf", handle_options);
rc = options_config_file(argc, argv, "~/.osmocom/analog/cnetz.conf", handle_options);
if (rc < 0)
return 0;
argi = options_command_line(argc, argv, handle_options);

View File

@ -75,6 +75,11 @@ void print_help(const char *arg0)
{
printf("Usage: %s [options] -M <mode>\n\n", arg0);
/* - - */
printf(" -h --help\n");
printf(" This help\n");
printf(" --config [~/]<path to config file>\n");
printf(" Give a config file to use. If it starts with '~/', path is at home dir.\n");
printf(" Each line in config file is one option, '-' or '--' must not be given!\n");
printf(" -T --am791x-type 7910 | 7911\n");
printf(" Give modem chip type. (Default = 791%d)\n", am791x_type);
printf(" -M --mc <mode>\n");
@ -262,7 +267,7 @@ int main(int argc, char *argv[])
/* handle options / config file */
add_options();
rc = options_config_file("~/.osmocom/analog/datenklo.conf", handle_options);
rc = options_config_file(argc, argv, "~/.osmocom/analog/datenklo.conf", handle_options);
if (rc < 0)
return 0;
argi = options_command_line(argc, argv, handle_options);

View File

@ -183,7 +183,7 @@ int main(int argc, char *argv[])
/* handle options / config file */
add_options();
rc = options_config_file("~/.osmocom/analog/eurosignal.conf", handle_options);
rc = options_config_file(argc, argv, "~/.osmocom/analog/eurosignal.conf", handle_options);
if (rc < 0)
return 0;
argi = options_command_line(argc, argv, handle_options);

View File

@ -70,7 +70,7 @@ void print_help(const char *arg0)
printf(" Give Metering pulse duration is seconds (default = %.2f).\n", gebuehren);
printf(" --ignore-link-monitor\n");
printf(" Don't do any link error checking at MTP.\n");
printf(" -C --config <filename>\n");
printf(" -C --bs-config <filename>\n");
printf(" Give DKO config file (6 KBytes tape file) to be loaded at boot time.\n");
main_mobile_print_hotkeys();
}
@ -90,7 +90,7 @@ static void add_options(void)
option_add('G', "gebuehren", 1);
option_add(OPT_ALARMS, "alarms", 1);
option_add(OPT_IGNORE_LINK_MONITOR, "ignore-link-monitor", 0);
option_add('C', "config", 1);
option_add('C', "bs-config", 1);
}
static int handle_options(int short_option, int argi, char **argv)
@ -179,7 +179,7 @@ int main(int argc, char *argv[])
/* handle options / config file */
add_options();
rc = options_config_file("~/.osmocom/cnetz/fuvst.conf", handle_options);
rc = options_config_file(argc, argv, "~/.osmocom/cnetz/fuvst.conf", handle_options);
if (rc < 0)
return 0;
argi = options_command_line(argc, argv, handle_options);

View File

@ -167,7 +167,7 @@ int main(int argc, char *argv[])
/* handle options / config file */
add_options();
rc = options_config_file("~/.osmocom/analog/imts.conf", handle_options);
rc = options_config_file(argc, argv, "~/.osmocom/analog/imts.conf", handle_options);
if (rc < 0)
return 0;
argi = options_command_line(argc, argv, handle_options);

View File

@ -134,7 +134,7 @@ int main(int argc, char *argv[])
/* handle options / config file */
add_options();
rc = options_config_file("~/.osmocom/analog/jollycom.conf", handle_options);
rc = options_config_file(argc, argv, "~/.osmocom/analog/jollycom.conf", handle_options);
if (rc < 0)
return 0;
argi = options_command_line(argc, argv, handle_options);

View File

@ -98,6 +98,9 @@ void main_mobile_print_help(const char *arg0, const char *ext_usage)
/* - - */
printf(" -h --help\n");
printf(" This help\n");
printf(" --config [~/]<path to config file>\n");
printf(" Give a config file to use. If it starts with '~/', path is at home dir.\n");
printf(" Each line in config file is one option, '-' or '--' must not be given!\n");
printf(" -v --verbose <level> | <level>,<category>[,<category>[,...]] | list\n");
printf(" Use 'list' to get a list of all levels and categories\n");
printf(" Verbose level: digit of debug level (default = '%d')\n", debuglevel);

View File

@ -61,8 +61,12 @@ void option_add(int short_option, const char *long_option, int parameter_count)
{
option_t *option;
/* check if option already exists */
/* check if option already exists or is not allowed */
for (option = option_head; option; option = option->next) {
if (!strcmp(option->long_option, "config")) {
PDEBUG(DOPTIONS, DEBUG_ERROR, "Option '%s' is not allowed to add, please fix!\n", option->long_option);
abort();
}
if (option->short_option == short_option
|| !strcmp(option->long_option, long_option)) {
PDEBUG(DOPTIONS, DEBUG_ERROR, "Option '%s' added twice, please fix!\n", option->long_option);
@ -83,24 +87,32 @@ void option_add(int short_option, const char *long_option, int parameter_count)
option_tailp = &(option->next);
}
int options_config_file(const char *config_file, int (*handle_options)(int short_option, int argi, char *argv[]))
int options_config_file(int argc, char *argv[], const char *config_file, int (*handle_options)(int short_option, int argi, char *argv[]))
{
static const char *home;
char config[256];
FILE *fp;
char buffer[256], opt[256], param[256], *p, *argv[16];
char buffer[256], opt[256], param[256], *p, *args[16];
char params[1024];
int line;
int rc = 1;
int i, j, quote;
option_t *option;
/* open config file */
home = getenv("HOME");
if (home == NULL)
return 1;
sprintf(config, "%s/%s", home, config_file + 2);
/* select for alternative config file */
if (argc > 2 && !strcmp(argv[1], "--config"))
config_file = argv[2];
/* add home directory */
if (config_file[0] == '~' && config_file[1] == '/') {
home = getenv("HOME");
if (home == NULL)
return 1;
sprintf(config, "%s/%s", home, config_file + 2);
} else
strcpy(config, config_file);
/* open config file */
fp = fopen(config, "r");
if (!fp) {
PDEBUG(DOPTIONS, DEBUG_INFO, "Config file '%s' seems not to exist, using command line options only.\n", config);
@ -179,7 +191,7 @@ int options_config_file(const char *config_file, int (*handle_options)(int short
param[j++] = *p++;
}
param[j] = '\0';
argv[i] = options_strdup(param);
args[i] = options_strdup(param);
sprintf(strchr(params, '\0'), " '%s'", param);
/* skip white spaces behind option */
while (*p > '\0' && *p <= ' ')
@ -206,7 +218,7 @@ int options_config_file(const char *config_file, int (*handle_options)(int short
PDEBUG(DOPTIONS, DEBUG_ERROR, "Given option '%s' in config file '%s' at line %d requires %d parameter(s), use '-h' for help!\n", opt, config_file, line, option->parameter_count);
return -EINVAL;
}
rc = handle_options(option->short_option, 0, argv);
rc = handle_options(option->short_option, 0, args);
if (rc <= 0)
goto done;
first_option = 0;
@ -227,6 +239,19 @@ int options_command_line(int argc, char *argv[], int (*handle_options)(int short
int rc;
for (argi = 1; argi < argc; argi++) {
/* --config */
if (!strcmp(argv[argi], "--config")) {
if (argi > 1) {
PDEBUG(DOPTIONS, DEBUG_ERROR, "Given command line option '%s' must be the first option specified, use '-h' for help!\n", argv[argi]);
return -EINVAL;
}
if (argc <= 2) {
PDEBUG(DOPTIONS, DEBUG_ERROR, "Given command line option '%s' requires 1 parameter, use '-h' for help!\n", argv[argi]);
return -EINVAL;
}
argi += 1;
continue;
}
if (argv[argi][0] == '-') {
if (argv[argi][1] != '-') {
if (strlen(argv[argi]) != 2) {

View File

@ -1,7 +1,7 @@
char *options_strdup(const char *s);
void option_add(int short_option, const char *long_option, int parameter_count);
int options_config_file(const char *config_file, int (*handle_options)(int short_option, int argi, char *argv[]));
int options_config_file(int argc, char *argv[], const char *config_file, int (*handle_options)(int short_option, int argi, char *argv[]));
int options_command_line(int argc, char *argv[], int (*handle_options)(int short_option, int argi, char *argv[]));
int option_is_first(void);
void options_free(void);

View File

@ -273,7 +273,7 @@ int main(int argc, char *argv[])
/* handle options / config file */
add_options();
rc = options_config_file("~/.osmocom/analog/nmt.conf", handle_options);
rc = options_config_file(argc, argv, "~/.osmocom/analog/nmt.conf", handle_options);
if (rc < 0)
return 0;
argi = options_command_line(argc, argv, handle_options);

View File

@ -261,7 +261,7 @@ int main(int argc, char *argv[])
/* handle options / config file */
add_options();
rc = options_config_file("~/.osmocom/analog/radiocom2000.conf", handle_options);
rc = options_config_file(argc, argv, "~/.osmocom/analog/radiocom2000.conf", handle_options);
if (rc < 0)
return 0;
argi = options_command_line(argc, argv, handle_options);

View File

@ -106,6 +106,11 @@ void print_help(const char *arg0)
printf("Usage: %s --sdr-soapy|--sdr-uhd <sdr options> -f <frequency> -M <modulation> -R|-T [options]\n", arg0);
/* - - */
printf("\noptions:\n");
printf(" -h --help\n");
printf(" This help\n");
printf(" --config [~/]<path to config file>\n");
printf(" Give a config file to use. If it starts with '~/', path is at home dir.\n");
printf(" Each line in config file is one option, '-' or '--' must not be given!\n");
printf(" -f --frequency <frequency>\n");
printf(" Give frequency in Hertz.\n");
printf(" -s --samplerate <sample rate>\n");
@ -298,7 +303,7 @@ int main(int argc, char *argv[])
/* handle options / config file */
add_options();
rc = options_config_file("~/.osmocom/analog/radio.conf", handle_options);
rc = options_config_file(argc, argv, "~/.osmocom/analog/radio.conf", handle_options);
if (rc < 0)
return 0;
argi = options_command_line(argc, argv, handle_options);

View File

@ -68,6 +68,9 @@ void print_help(const char *arg0)
printf("General options:\n");
printf(" -h --help\n");
printf(" This help\n");
printf(" --config [~/]<path to config file>\n");
printf(" Give a config file to use. If it starts with '~/', path is at home dir.\n");
printf(" Each line in config file is one option, '-' or '--' must not be given!\n");
printf(" -v --verbose <level> | <level>,<category>[,<category>[,...]] | list\n");
printf(" Use 'list' to get a list of all levels and categories\n");
printf(" Verbose level: digit of debug level (default = '%d')\n", debuglevel);
@ -334,7 +337,7 @@ int main(int argc, char *argv[])
debuglevel = DEBUG_INFO;
add_options();
rc = options_config_file("~/.osmocom/analog/sim.conf", handle_options);
rc = options_config_file(argc, argv, "~/.osmocom/analog/sim.conf", handle_options);
if (rc < 0)
return 0;

View File

@ -95,6 +95,11 @@ void print_help(const char *arg0)
printf(" tx-img [<image>] Transmit natural image or given image file\n");
printf(" Use 4:3 image with 574 lines for best result.\n");
printf("\ngeneral options:\n");
printf(" -h --help\n");
printf(" This help\n");
printf(" --config [~/]<path to config file>\n");
printf(" Give a config file to use. If it starts with '~/', path is at home dir.\n");
printf(" Each line in config file is one option, '-' or '--' must not be given!\n");
printf(" -f --frequency <frequency>\n");
printf(" Give frequency in Hertz.\n");
printf(" -c --channel <channel>\n");
@ -489,7 +494,7 @@ int main(int argc, char *argv[])
/* handle options / config file */
add_options();
rc = options_config_file("~/.osmocom/analog/osmotv.conf", handle_options);
rc = options_config_file(argc, argv, "~/.osmocom/analog/osmotv.conf", handle_options);
if (rc < 0)
return 0;
argi = options_command_line(argc, argv, handle_options);

View File

@ -82,7 +82,7 @@ int main(int argc, char *argv[])
/* handle options / config file */
add_options();
rc = options_config_file("~/.osmocom/analog/zeitansage.conf", handle_options);
rc = options_config_file(argc, argv, "~/.osmocom/analog/zeitansage.conf", handle_options);
if (rc < 0)
return 0;
argi = options_command_line(argc, argv, handle_options);