From 3b8100721032397f507b93c059d3dec0365c7eb0 Mon Sep 17 00:00:00 2001 From: Andreas Eversberg Date: Sat, 19 May 2018 10:56:43 +0200 Subject: Refactoring command line option handling * Use own function to define and parse command line options * Command line options can be defined by config file also * --limesdr allows to auto-set required SDR option for LimeSDR --- src/anetz/Makefile.am | 1 + src/anetz/main.c | 125 ++++++++++++++++++++++---------------------------- 2 files changed, 57 insertions(+), 69 deletions(-) (limited to 'src/anetz') diff --git a/src/anetz/Makefile.am b/src/anetz/Makefile.am index 175404d..eba47db 100644 --- a/src/anetz/Makefile.am +++ b/src/anetz/Makefile.am @@ -18,6 +18,7 @@ anetz_SOURCES = \ anetz_LDADD = \ $(COMMON_LA) \ libgermanton.a \ + $(top_builddir)/src/liboptions/liboptions.a \ $(top_builddir)/src/libdebug/libdebug.a \ $(top_builddir)/src/libmobile/libmobile.a \ $(top_builddir)/src/libdisplay/libdisplay.a \ diff --git a/src/anetz/main.c b/src/anetz/main.c index 617bd88..35fc036 100644 --- a/src/anetz/main.c +++ b/src/anetz/main.c @@ -19,15 +19,16 @@ #include #include -#include #include #include +#include #include #include "../libsample/sample.h" #include "../libmobile/main_mobile.h" #include "../libdebug/debug.h" #include "../libtimer/timer.h" #include "../libmobile/call.h" +#include "../liboptions/options.h" #include "freiton.h" #include "besetztton.h" #include "anetz.h" @@ -69,79 +70,60 @@ void print_help(const char *arg0) main_mobile_print_hotkeys(); } -static int handle_options(int argc, char **argv) +static void add_options(void) +{ + main_mobile_add_options(); + option_add('O', "operator", 1); + option_add('G', "geo", 1); + option_add('V', "page-gain", 1); + option_add('P', "page-sequence", 1); + option_add('S', "squelch", 1); +} + +static int handle_options(int short_option, int argi, char **argv) { - int skip_args = 0; char *p; double gain_db; - static struct option long_options_special[] = { - {"operator", 1, 0, 'O'}, - {"geo", 1, 0, 'G'}, - {"page-gain", 1, 0, 'V'}, - {"page-sequence", 1, 0, 'P'}, - {"squelch", 1, 0, 'S'}, - {0, 0, 0, 0} - }; - - main_mobile_set_options("O:G:V:P:S:", long_options_special); - - while (1) { - int option_index = 0, c; - - c = getopt_long(argc, argv, optstring, long_options, &option_index); - - if (c == -1) - break; - - switch (c) { - case 'O': - strncpy(operator, optarg, sizeof(operator) - 1); - operator[sizeof(operator) - 1] = '\0'; - skip_args += 2; - break; - case 'G': - if (!strcasecmp(optarg, "list")) { - station_list(); - exit(0); - } - if ((p = strchr(optarg, ','))) { - get_station_by_coordinates(atof(optarg), atof(p + 1)); - exit(0); - } - fprintf(stderr, "Invalid geo parameter\n"); - exit(0); - break; - case 'V': - gain_db = atof(optarg); - page_gain = pow(10, gain_db / 20.0); - skip_args += 2; - break; - case 'P': - page_sequence = atoi(optarg); - skip_args += 2; - break; - case 'S': - if (!strcasecmp(optarg, "auto")) - squelch_db = 0.0; - else - squelch_db = atof(optarg); - skip_args += 2; - break; - default: - main_mobile_opt_switch(c, argv[0], &skip_args); + switch (short_option) { + case 'O': + strncpy(operator, argv[argi], sizeof(operator) - 1); + operator[sizeof(operator) - 1] = '\0'; + break; + case 'G': + if (!strcasecmp(argv[argi], "list")) { + station_list(); + return 0; } + if ((p = strchr(argv[argi], ','))) { + get_station_by_coordinates(atof(argv[argi]), atof(p + 1)); + return 0; + } + fprintf(stderr, "Invalid geo parameter\n"); + return -EINVAL; + case 'V': + gain_db = atof(argv[argi]); + page_gain = pow(10, gain_db / 20.0); + break; + case 'P': + page_sequence = atoi(argv[argi]); + break; + case 'S': + if (!strcasecmp(argv[argi], "auto")) + squelch_db = 0.0; + else + squelch_db = atof(argv[argi]); + break; + default: + return main_mobile_handle_options(short_option, argi, argv); } - free(long_options); - - return skip_args; + return 1; } int main(int argc, char *argv[]) { - int rc; - int skip_args; + int rc, argi; const char *station_id = ""; int i; @@ -154,12 +136,17 @@ int main(int argc, char *argv[]) main_mobile_init(); - skip_args = handle_options(argc, argv); - argc -= skip_args; - argv += skip_args; + /* handle options / config file */ + add_options(); + rc = options_config_file("~/.osmocom/analog/anetz.conf", handle_options); + if (rc < 0) + return 0; + argi = options_command_line(argc, argv, handle_options); + if (argi <= 0) + return argi; - if (argc > 1) { - station_id = argv[1]; + if (argi < argc) { + station_id = argv[argi]; if (strlen(station_id) != 5 && strlen(station_id) != 7) { printf("Given station ID '%s' does not have 7 or (the last) 5 digits\n", station_id); return 0; @@ -170,7 +157,7 @@ int main(int argc, char *argv[]) if (!num_kanal) { printf("No channel (\"Kanal\") is specified, I suggest channel 30.\n\n"); - print_help(argv[-skip_args]); + print_help(argv[0]); return 0; } if (use_sdr) { -- cgit v1.2.3