encapsulate the mainloop code

Signed-off-by: Daniel Lezcano <daniel.lezcano@free.fr>
Signed-off-by: Amit Kucheria <amit.kucheria@linaro.org>
diff --git a/powerdebug.c b/powerdebug.c
index bfa2153..3a9c0ec 100644
--- a/powerdebug.c
+++ b/powerdebug.c
@@ -162,32 +162,17 @@
 	return 0;
 }
 
-int main(int argc, char **argv)
+int mainloop(struct powerdebug_options *options)
 {
-	int i;
 	int findparent_ncurses = 0, refreshwin = 0;
 	int enter_hit = 0;
 	int firsttime[TOTAL_FEATURE_WINS];
+	int i;
 	char clkname_str[64];
-	struct powerdebug_options *options;
 
 	for (i = 0; i < TOTAL_FEATURE_WINS; i++)
 		firsttime[i] = 1;
 
-	options = malloc(sizeof(*options));
-	if (!options) {
-		fprintf(stderr, "failed to allocated memory\n");
-		return -1;
-	}
-
-	if (getoptions(argc, argv, options)) {
-		usage();
-		return 1;
-	}
-
-	if (init_regulator_ds())
-		return -1;
-
 	while (1) {
 		int key = 0;
 		struct timeval tval;
@@ -333,7 +318,7 @@
 				enter_hit = 1;
 
 			if (keychar == 'Q' && !findparent_ncurses)
-				exit(0);
+				break;
 			if (keychar == 'R') {
 				refreshwin = 1;
 				options->ticktime = 3;
@@ -341,5 +326,30 @@
 				refreshwin = 0;
 		}
 	}
-	exit(0);
+
+	return 0;
+}
+
+int main(int argc, char **argv)
+{
+	struct powerdebug_options *options;
+
+	options = malloc(sizeof(*options));
+	if (!options) {
+		fprintf(stderr, "failed to allocated memory\n");
+		return -1;
+	}
+
+	if (getoptions(argc, argv, options)) {
+		usage();
+		return 1;
+	}
+
+	if (init_regulator_ds())
+		return 1;
+
+	if (mainloop(options))
+		return 1;
+
+	return 0;
 }