From 3d9481177bab7b30ac1ccb9e4f64f4c672ef5c26 Mon Sep 17 00:00:00 2001 From: Andreas Eversberg Date: Thu, 11 Mar 2021 07:38:00 +0100 Subject: [PATCH] Config file path can be changed by command line option "--config " --- src/amps/amps_tacs_main.c | 6 ++--- src/anetz/main.c | 2 +- src/bnetz/main.c | 2 +- src/cnetz/main.c | 2 +- src/datenklo/main.c | 7 +++++- src/eurosignal/main.c | 2 +- src/fuvst/main.c | 6 ++--- src/imts/main.c | 2 +- src/jolly/main.c | 2 +- src/libmobile/main_mobile.c | 3 +++ src/liboptions/options.c | 45 ++++++++++++++++++++++++++++--------- src/liboptions/options.h | 2 +- src/nmt/main.c | 2 +- src/r2000/main.c | 2 +- src/radio/main.c | 7 +++++- src/sim/main.c | 5 ++++- src/tv/main.c | 7 +++++- src/zeitansage/main.c | 2 +- 18 files changed, 76 insertions(+), 30 deletions(-) diff --git a/src/amps/amps_tacs_main.c b/src/amps/amps_tacs_main.c index 88e7fa5..808eb31 100644 --- a/src/amps/amps_tacs_main.c +++ b/src/amps/amps_tacs_main.c @@ -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; diff --git a/src/anetz/main.c b/src/anetz/main.c index 9cc8086..03e9c99 100644 --- a/src/anetz/main.c +++ b/src/anetz/main.c @@ -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); diff --git a/src/bnetz/main.c b/src/bnetz/main.c index db704f9..fd29a1c 100644 --- a/src/bnetz/main.c +++ b/src/bnetz/main.c @@ -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); diff --git a/src/cnetz/main.c b/src/cnetz/main.c index 2d7d8d8..77459e1 100644 --- a/src/cnetz/main.c +++ b/src/cnetz/main.c @@ -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); diff --git a/src/datenklo/main.c b/src/datenklo/main.c index df79594..4f3977b 100644 --- a/src/datenklo/main.c +++ b/src/datenklo/main.c @@ -75,6 +75,11 @@ void print_help(const char *arg0) { printf("Usage: %s [options] -M \n\n", arg0); /* - - */ + printf(" -h --help\n"); + printf(" This help\n"); + printf(" --config [~/]\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 \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); diff --git a/src/eurosignal/main.c b/src/eurosignal/main.c index e7df606..dcc19a9 100644 --- a/src/eurosignal/main.c +++ b/src/eurosignal/main.c @@ -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); diff --git a/src/fuvst/main.c b/src/fuvst/main.c index 5d97aa2..69d7c20 100755 --- a/src/fuvst/main.c +++ b/src/fuvst/main.c @@ -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 \n"); + printf(" -C --bs-config \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); diff --git a/src/imts/main.c b/src/imts/main.c index 5375b92..d3d7230 100644 --- a/src/imts/main.c +++ b/src/imts/main.c @@ -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); diff --git a/src/jolly/main.c b/src/jolly/main.c index 8f3fec2..b8ed825 100644 --- a/src/jolly/main.c +++ b/src/jolly/main.c @@ -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); diff --git a/src/libmobile/main_mobile.c b/src/libmobile/main_mobile.c index a18c3a0..6c4cd31 100644 --- a/src/libmobile/main_mobile.c +++ b/src/libmobile/main_mobile.c @@ -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 [~/]\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 | ,[,[,...]] | 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); diff --git a/src/liboptions/options.c b/src/liboptions/options.c index 76a7232..d49e698 100644 --- a/src/liboptions/options.c +++ b/src/liboptions/options.c @@ -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) { diff --git a/src/liboptions/options.h b/src/liboptions/options.h index f1d5eba..5f494e0 100644 --- a/src/liboptions/options.h +++ b/src/liboptions/options.h @@ -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); diff --git a/src/nmt/main.c b/src/nmt/main.c index 8401a27..7d4c91e 100644 --- a/src/nmt/main.c +++ b/src/nmt/main.c @@ -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); diff --git a/src/r2000/main.c b/src/r2000/main.c index d8534c4..01361c4 100644 --- a/src/r2000/main.c +++ b/src/r2000/main.c @@ -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); diff --git a/src/radio/main.c b/src/radio/main.c index e004af6..2eb8e43 100644 --- a/src/radio/main.c +++ b/src/radio/main.c @@ -106,6 +106,11 @@ void print_help(const char *arg0) printf("Usage: %s --sdr-soapy|--sdr-uhd -f -M -R|-T [options]\n", arg0); /* - - */ printf("\noptions:\n"); + printf(" -h --help\n"); + printf(" This help\n"); + printf(" --config [~/]\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 \n"); printf(" Give frequency in Hertz.\n"); printf(" -s --samplerate \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); diff --git a/src/sim/main.c b/src/sim/main.c index 5178bef..91cae4b 100644 --- a/src/sim/main.c +++ b/src/sim/main.c @@ -68,6 +68,9 @@ void print_help(const char *arg0) printf("General options:\n"); printf(" -h --help\n"); printf(" This help\n"); + printf(" --config [~/]\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 | ,[,[,...]] | 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; diff --git a/src/tv/main.c b/src/tv/main.c index c1bc392..92e5eaa 100644 --- a/src/tv/main.c +++ b/src/tv/main.c @@ -95,6 +95,11 @@ void print_help(const char *arg0) printf(" tx-img [] 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 [~/]\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 \n"); printf(" Give frequency in Hertz.\n"); printf(" -c --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); diff --git a/src/zeitansage/main.c b/src/zeitansage/main.c index 8ef154c..a77d9bd 100644 --- a/src/zeitansage/main.c +++ b/src/zeitansage/main.c @@ -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);