[PATCH 5/7] ipaccess-proxy: more robust option parsing and checking

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/OpenBSC@lists.osmocom.org/.

pablo at gnumonks.org pablo at gnumonks.org
Mon Apr 11 15:04:28 UTC 2011


From: Pablo Neira Ayuso <pablo at gnumonks.org>

With this patch, ipaccess-proxy makes more robust option checking:

$ ./ipaccess-proxy -l 1.1.1.1 -b 2.2.2.2 -e
ERROR: missing mandatory argument for `-e' option

And we return to shell to enforce the user to try again with the
appropriate invocation.

Before this patch, the default getopt_long() error handling was
enabled which displayed this message:

./ipaccess-proxy: option requires an argument -- 'e'

and ipaccess-proxy continued working.

This is generic enough to cover other option that require mandatory
arguments like `--bsc' and `--listen'.
---
 openbsc/src/ipaccess/ipaccess-proxy.c |   15 +++++++++++++++
 1 files changed, 15 insertions(+), 0 deletions(-)

diff --git a/openbsc/src/ipaccess/ipaccess-proxy.c b/openbsc/src/ipaccess/ipaccess-proxy.c
index 55658cf..dd0a739 100644
--- a/openbsc/src/ipaccess/ipaccess-proxy.c
+++ b/openbsc/src/ipaccess/ipaccess-proxy.c
@@ -1133,6 +1133,9 @@ static void handle_options(int argc, char** argv)
 {
 	int options_mask = 0;
 
+	/* disable explicit missing arguments error output from getopt_long */
+	opterr = 0;
+
 	while (1) {
 		int option_index = 0, c;
 		static struct option long_options[] = {
@@ -1176,6 +1179,18 @@ static void handle_options(int argc, char** argv)
 		case 'e':
 			log_set_log_level(stderr_target, atoi(optarg));
 			break;
+		case '?':
+			if (optopt) {
+				printf("ERROR: missing mandatory argument "
+				       "for `%s' option\n", argv[optind-1]);
+			} else {
+				printf("ERROR: unknown option `%s'\n",
+					argv[optind-1]);
+			}
+			print_usage();
+			print_help();
+			exit(EXIT_FAILURE);
+			break;
 		default:
 			/* ignore */
 			break;
-- 
1.7.2.3





More information about the OpenBSC mailing list