From 8775e22bcb37265d9df834ab89bd238569ef13f8 Mon Sep 17 00:00:00 2001
From: Jakob Olsson <jakobols@kth.se>
Date: Mon, 21 May 2018 12:44:42 +0200
Subject: [PATCH] working version of parse_args, add shared debug var

---
 common.c |  4 ++--
 common.h |  5 ++---
 dongle.c | 38 ++++++++++++++++++--------------------
 3 files changed, 22 insertions(+), 25 deletions(-)

diff --git a/common.c b/common.c
index 003139e..bcba134 100644
--- a/common.c
+++ b/common.c
@@ -49,7 +49,7 @@ struct blob_buf json_to_blob(struct json_object *response, struct blob_buf bb)
 	return bb;
 }
 
-void set_debug(int debug_arg)
+void check_debug(void)
 {
-	debug = debug_arg;
+	printf("debug: %d\n", debug);
 }
\ No newline at end of file
diff --git a/common.h b/common.h
index 00bcf59..d99131c 100644
--- a/common.h
+++ b/common.h
@@ -5,7 +5,6 @@
 #include <stdio.h>
 #include <stdbool.h>
 #include <unistd.h>
-#include <getopt.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
@@ -20,7 +19,7 @@
 #include <libubox/blobmsg.h>
 #include <libubus.h>
 
-int debug;
+extern int debug;
 
 #define debug_print(...)                  \
 	do                                    \
@@ -58,5 +57,5 @@ int print_to_ubus(struct json_object *parsed_response, struct ubus_context *ctx,
  */
 struct blob_buf json_to_blob(struct json_object *response, struct blob_buf bb);
 
-void set_debug(int debug_arg);
+void check_debug(void);
 #endif
diff --git a/dongle.c b/dongle.c
index a987655..b3e7091 100644
--- a/dongle.c
+++ b/dongle.c
@@ -1,3 +1,5 @@
+#include <getopt.h>
+
 #include "common.h"
 #include "dongle_apn.h"
 #include "dongle_pin.h"
@@ -15,30 +17,26 @@ int parse_args(int argc, char **argv)
 {
 	char ch;
 
-	printf("hallå1\n");
-	while ((ch = getopt_long(argc, argv, ":d:", long_options, NULL)) != -1) {
-	int debug_arg;
-		printf("hallå2\n");
+	while ((ch = getopt_long(argc, argv, "d:", long_options, NULL)) != -1) {
 		switch (ch) {
-		case 'd':
-			debug_arg = atoi(optarg);
-			if (debug_arg > 1 || debug_arg < 0) {
-				debug_print("%s: option '-%c' is invalid.\n", argv[0], optopt);
+			case 'd':
+				debug = atoi(optarg);
+				if (debug > 1 || debug < 0) {
+					printf("%s: option '-%c' is invalid.\n", argv[0], optopt); //cant exactly do debug print in here...
+					goto fail;
+				}
+				break;
+			case ':':
+				debug_print(stderr, "%s: option '-%c' requires an argument\n", argv[0], optopt);
+				goto fail;
+				break;
+			case '?':
+				debug_print(stderr, "%s: option '-%c' is invalid: ignored\n", argv[0], optopt);
 				goto fail;
-			}
-			printf("hallå3\n");
-			//set_debug(debug_arg);
-			break;
-		case ':':
-			debug_print(stderr, "%s: option '-%c' requires an argument\n", argv[0], optopt);
-			goto fail;
-			break;
-		case '?':
-			debug_print(stderr, "%s: option '-%c' is invalid: ignored\n", argv[0], optopt);
-			goto fail;
-			break;
+				break;
 		}
 	}
+
 	return 0;
 fail:
 	return -1;
-- 
GitLab