[MERGED] osmo-bsc[master]: abisip-find: add getopt option parsing in preparation for a ...

2017-12-26 Thread Harald Welte
Harald Welte has submitted this change and it was merged.

Change subject: abisip-find: add getopt option parsing in preparation for a new 
option
..


abisip-find: add getopt option parsing in preparation for a new option

Subsequent patch I4201876431029b303dbd10e46492228379c9782a will add the -l
cmdline option. Add getopt in a separate step here to keep the patch lean.

Change-Id: Idba1a89753510fe6d409277b20c2db86c1b8f7f8
---
M src/ipaccess/abisip-find.c
1 file changed, 55 insertions(+), 7 deletions(-)

Approvals:
  Harald Welte: Looks good to me, approved
  Jenkins Builder: Verified



diff --git a/src/ipaccess/abisip-find.c b/src/ipaccess/abisip-find.c
index c459161..46205dd 100644
--- a/src/ipaccess/abisip-find.c
+++ b/src/ipaccess/abisip-find.c
@@ -24,13 +24,63 @@
 #include 
 #include 
 #include 
-
+#include 
 
 #include 
 #include 
 #include 
 #include 
 #include 
+
+static struct {
+   const char *ifname;
+} cmdline_opts = {
+   .ifname = NULL,
+};
+
+static void print_help()
+{
+   printf("\n");
+   printf("Usage: abisip-find [-l] []\n");
+   printf("Specify the outgoing network interface,\n"
+  "e.g. 'eth0'\n");
+}
+
+static void handle_options(int argc, char **argv)
+{
+   while (1) {
+   int option_index = 0, c;
+   static struct option long_options[] = {
+   {"help", 0, 0, 'h'},
+   {0, 0, 0, 0}
+   };
+
+   c = getopt_long(argc, argv, "h",
+   long_options, _index);
+   if (c == -1)
+   break;
+
+   switch (c) {
+   case 'h':
+   print_help();
+   exit(EXIT_SUCCESS);
+   default:
+   /* catch unknown options *as well as* missing 
arguments. */
+   fprintf(stderr, "Error in command line options. 
Exiting. Try --help.\n");
+   exit(EXIT_FAILURE);
+   break;
+   }
+   }
+
+   if (argc - optind > 0)
+   cmdline_opts.ifname = argv[optind++];
+
+   if (argc - optind > 0) {
+   fprintf(stderr, "Error: too many arguments\n");
+   print_help();
+   exit(EXIT_FAILURE);
+   }
+}
 
 static int udp_sock(const char *ifname)
 {
@@ -173,22 +223,20 @@
 int main(int argc, char **argv)
 {
struct osmo_fd bfd;
-   char *ifname = NULL;
int rc;
 
printf("abisip-find (C) 2009 by Harald Welte\n");
printf("This is FREE SOFTWARE with ABSOLUTELY NO WARRANTY\n\n");
 
-   if (argc < 2) {
+   handle_options(argc, argv);
+
+   if (!cmdline_opts.ifname)
fprintf(stdout, "you might need to specify the outgoing\n"
" network interface, e.g. ``%s eth0''\n", argv[0]);
-   } else {
-   ifname = argv[1];
-   }
 
bfd.cb = bfd_cb;
bfd.when = BSC_FD_READ | BSC_FD_WRITE;
-   bfd.fd = udp_sock(ifname);
+   bfd.fd = udp_sock(cmdline_opts.ifname);
if (bfd.fd < 0) {
perror("Cannot create local socket for broadcast udp");
exit(1);

-- 
To view, visit https://gerrit.osmocom.org/5586
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Idba1a89753510fe6d409277b20c2db86c1b8f7f8
Gerrit-PatchSet: 1
Gerrit-Project: osmo-bsc
Gerrit-Branch: master
Gerrit-Owner: Neels Hofmeyr 
Gerrit-Reviewer: Harald Welte 
Gerrit-Reviewer: Jenkins Builder


osmo-bsc[master]: abisip-find: add getopt option parsing in preparation for a ...

2017-12-26 Thread Harald Welte

Patch Set 1: Code-Review+2

-- 
To view, visit https://gerrit.osmocom.org/5586
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: comment
Gerrit-Change-Id: Idba1a89753510fe6d409277b20c2db86c1b8f7f8
Gerrit-PatchSet: 1
Gerrit-Project: osmo-bsc
Gerrit-Branch: master
Gerrit-Owner: Neels Hofmeyr 
Gerrit-Reviewer: Harald Welte 
Gerrit-Reviewer: Jenkins Builder
Gerrit-HasComments: No


[PATCH] osmo-bsc[master]: abisip-find: add getopt option parsing in preparation for a ...

2017-12-25 Thread Neels Hofmeyr

Review at  https://gerrit.osmocom.org/5586

abisip-find: add getopt option parsing in preparation for a new option

Subsequent patch I4201876431029b303dbd10e46492228379c9782a will add the -l
cmdline option. Add getopt in a separate step here to keep the patch lean.

Change-Id: Idba1a89753510fe6d409277b20c2db86c1b8f7f8
---
M src/ipaccess/abisip-find.c
1 file changed, 55 insertions(+), 7 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/osmo-bsc refs/changes/86/5586/1

diff --git a/src/ipaccess/abisip-find.c b/src/ipaccess/abisip-find.c
index c459161..46205dd 100644
--- a/src/ipaccess/abisip-find.c
+++ b/src/ipaccess/abisip-find.c
@@ -24,13 +24,63 @@
 #include 
 #include 
 #include 
-
+#include 
 
 #include 
 #include 
 #include 
 #include 
 #include 
+
+static struct {
+   const char *ifname;
+} cmdline_opts = {
+   .ifname = NULL,
+};
+
+static void print_help()
+{
+   printf("\n");
+   printf("Usage: abisip-find [-l] []\n");
+   printf("Specify the outgoing network interface,\n"
+  "e.g. 'eth0'\n");
+}
+
+static void handle_options(int argc, char **argv)
+{
+   while (1) {
+   int option_index = 0, c;
+   static struct option long_options[] = {
+   {"help", 0, 0, 'h'},
+   {0, 0, 0, 0}
+   };
+
+   c = getopt_long(argc, argv, "h",
+   long_options, _index);
+   if (c == -1)
+   break;
+
+   switch (c) {
+   case 'h':
+   print_help();
+   exit(EXIT_SUCCESS);
+   default:
+   /* catch unknown options *as well as* missing 
arguments. */
+   fprintf(stderr, "Error in command line options. 
Exiting. Try --help.\n");
+   exit(EXIT_FAILURE);
+   break;
+   }
+   }
+
+   if (argc - optind > 0)
+   cmdline_opts.ifname = argv[optind++];
+
+   if (argc - optind > 0) {
+   fprintf(stderr, "Error: too many arguments\n");
+   print_help();
+   exit(EXIT_FAILURE);
+   }
+}
 
 static int udp_sock(const char *ifname)
 {
@@ -173,22 +223,20 @@
 int main(int argc, char **argv)
 {
struct osmo_fd bfd;
-   char *ifname = NULL;
int rc;
 
printf("abisip-find (C) 2009 by Harald Welte\n");
printf("This is FREE SOFTWARE with ABSOLUTELY NO WARRANTY\n\n");
 
-   if (argc < 2) {
+   handle_options(argc, argv);
+
+   if (!cmdline_opts.ifname)
fprintf(stdout, "you might need to specify the outgoing\n"
" network interface, e.g. ``%s eth0''\n", argv[0]);
-   } else {
-   ifname = argv[1];
-   }
 
bfd.cb = bfd_cb;
bfd.when = BSC_FD_READ | BSC_FD_WRITE;
-   bfd.fd = udp_sock(ifname);
+   bfd.fd = udp_sock(cmdline_opts.ifname);
if (bfd.fd < 0) {
perror("Cannot create local socket for broadcast udp");
exit(1);

-- 
To view, visit https://gerrit.osmocom.org/5586
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Idba1a89753510fe6d409277b20c2db86c1b8f7f8
Gerrit-PatchSet: 1
Gerrit-Project: osmo-bsc
Gerrit-Branch: master
Gerrit-Owner: Neels Hofmeyr