From: Timo Kokkonen Date: Thu, 28 Apr 2011 18:49:18 +0000 (+0300) Subject: Read options from command line X-Git-Url: http://git.itanic.dy.fi/?p=glucose;a=commitdiff_plain;h=fe738f84a3c59070e63dd80224f3c1d141da982a Read options from command line The usb device node and verbosity level is supported. Signed-off-by: Timo Kokkonen --- diff --git a/Makefile b/Makefile index cbbfd55..190671e 100644 --- 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 a350154..02be854 100644 --- 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 index 0000000..4e2a539 --- /dev/null +++ b/options.c @@ -0,0 +1,44 @@ +#include +#include + +#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 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