]> git.itanic.dy.fi Git - glucose/commitdiff
Read options from command line
authorTimo Kokkonen <kaapeli@itanic.dy.fi>
Thu, 28 Apr 2011 18:49:18 +0000 (21:49 +0300)
committerTimo Kokkonen <kaapeli@itanic.dy.fi>
Thu, 28 Apr 2011 18:49:18 +0000 (21:49 +0300)
The usb device node and verbosity level is supported.

Signed-off-by: Timo Kokkonen <kaapeli@itanic.dy.fi>
Makefile
main.c
options.c [new file with mode: 0644]
options.h [new file with mode: 0644]

index cbbfd551367f13af2ef32dbbc46f233a1550c5af..190671e9c735011bf348e96a935bc3b2b5cf567d 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -2,7 +2,7 @@ CC = gcc
 LD = ld
 CFLAGS = -Wall -O2 -g
 
-GLUCOSE_OBJS = main.o hiddev.o utils.o
+GLUCOSE_OBJS = main.o hiddev.o utils.o options.o
 
 ALL_OBJS = $(GLUCOSE_OBJS)
 ALL_DEBS = $(shell echo " "$(ALL_OBJS) | sed -e "s,[^ ]*\.a,,g" -e     \
diff --git a/main.c b/main.c
index a350154067dbc459c2d4bb5597267cb86a862316..02be854afaa1561f17309d52c2448a9ba7e334a5 100644 (file)
--- a/main.c
+++ b/main.c
@@ -9,6 +9,7 @@
 
 #include "hiddev.h"
 #include "utils.h"
+#include "options.h"
 
 
 struct msg {
@@ -273,8 +274,16 @@ int communicate(int fd, int uc)
 int main(int argc, char *argv[])
 {
        int fd, usage_code;
+       struct user_options opts;
 
-       fd = hiddev_open(argv[1], &usage_code);
+       read_args(argc, argv, &opts);
+
+       if (opts.usbdev == NULL) {
+               trace(0, "USB dev name needs to be give with '-d' parameter\n");
+               return 1;
+       }
+
+       fd = hiddev_open(opts.usbdev, &usage_code);
        if (fd < 0)
                return 1;
 
diff --git a/options.c b/options.c
new file mode 100644 (file)
index 0000000..4e2a539
--- /dev/null
+++ b/options.c
@@ -0,0 +1,44 @@
+#include <getopt.h>
+#include <string.h>
+
+#include "options.h"
+
+int read_args(int argc, char *argv[], struct user_options *opts)
+{
+        int option_index = 0, c;
+        static struct option long_options[] = {
+                { .val = 'd', .name = "device", .has_arg = 1, },
+                { .val = 'v', .name = "verbose", },
+        };
+        char short_options[] = "d:v";
+
+       memset(opts, 0, sizeof(*opts));
+
+        while (1) {
+                c = getopt_long(argc, argv, short_options, long_options,
+                                &option_index);
+
+                if (c == -1)
+                        break;
+
+                switch (c) {
+               case 'd':
+                       opts->usbdev = optarg;
+                       break;
+               case 'v':
+                       opts->trace_level++;
+                case '?':
+                       return -1;
+                }
+        }
+
+        while (optind < argc) {
+               /*
+                * Some day we do something useful here with the rest
+                * of the options.. Maybe
+                */
+               optind++;
+        }
+
+       return 0;
+}
diff --git a/options.h b/options.h
new file mode 100644 (file)
index 0000000..8e629fb
--- /dev/null
+++ b/options.h
@@ -0,0 +1,11 @@
+#ifndef _OPTIONS_H_
+#define _OPTINOS_H_
+
+struct user_options {
+       char *usbdev;
+       int trace_level;
+};
+
+int read_args(int argc, char *argv[], struct user_options *opts);
+
+#endif