]> git.itanic.dy.fi Git - log-plotter/commitdiff
Add command line parsing
authorTimo Kokkonen <timo.t.kokkonen@iki.fi>
Sun, 6 Oct 2013 13:46:58 +0000 (16:46 +0300)
committerTimo Kokkonen <timo.t.kokkonen@iki.fi>
Sun, 6 Oct 2013 13:51:16 +0000 (16:51 +0300)
Add generic commnd line parsing code for adjusting the run time
configurable parameters.

Signed-off-by: Timo Kokkonen <timo.t.kokkonen@iki.fi>
Makefile
main.c
options.c [new file with mode: 0644]
options.h [new file with mode: 0644]

index bcea3eb8ca9eaf124bfd7cae7f96a851c74a6733..e02d333be42a162c531835aacd3296b72fa7792e 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -2,7 +2,7 @@ CC=gcc
 LD=ld
 CFLAGS=-Wall -O2 -g -fPIC
 
 LD=ld
 CFLAGS=-Wall -O2 -g -fPIC
 
-LOG-PLOTTER_OBJS = baud.o main.o
+LOG-PLOTTER_OBJS = baud.o main.o options.o
 
 ALL_OBJS = $(LOG-PLOTTER_OBJS)
 ALL_DEBS = $(patsubst %.o,.%.o.d,$(ALL_OBJS))
 
 ALL_OBJS = $(LOG-PLOTTER_OBJS)
 ALL_DEBS = $(patsubst %.o,.%.o.d,$(ALL_OBJS))
diff --git a/main.c b/main.c
index dd6c7c090a600705da207112849957a85a221990..d81f7333e25f062c28e15e4b43f3f9e77335c6f6 100644 (file)
--- a/main.c
+++ b/main.c
@@ -2,30 +2,29 @@
 #include <unistd.h>
 #include <sys/epoll.h>
 
 #include <unistd.h>
 #include <sys/epoll.h>
 
+#include "options.h"
 #include "baud.h"
 
 int main(int argc, char *argv[])
 {
        struct epoll_event ev;
 #include "baud.h"
 
 int main(int argc, char *argv[])
 {
        struct epoll_event ev;
+       struct plotter_options options;
        int fd, baud, ret;
        int epoll_fd;
        int fd, baud, ret;
        int epoll_fd;
-       char *device;
        char buf[256];
 
        char buf[256];
 
-       if (argc < 2) {
-               printf("Usage: %s SERIAL_DEVICE\n", argv[0]);
-               return 1;
-       }
 
 
-       baud = 128000;
-       device = argv[1];
+       if (read_args(argc, argv, &options))
+               return 1;
 
 
-       fd = open_at_baud(device, &baud);
+       baud = options.baud_rate;
+       fd = open_at_baud(options.device_path, &baud);
        if (fd < 0)
                return 1;
 
        if (fd < 0)
                return 1;
 
-       if (baud != 128000) {
-               printf("Failed to set baudrate to 128000, only got %d\n", baud);
+       if (baud != options.baud_rate) {
+               printf("Failed to set baudrate to %d, only got %d\n",
+                       options.baud_rate, baud);
                close(fd);
                return 1;
        }
                close(fd);
                return 1;
        }
diff --git a/options.c b/options.c
new file mode 100644 (file)
index 0000000..38b29a3
--- /dev/null
+++ b/options.c
@@ -0,0 +1,56 @@
+#include <getopt.h>
+#include <string.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "options.h"
+
+static void set_default_options(struct plotter_options *opts)
+{
+       bzero(opts, sizeof(*opts));
+
+       opts->baud_rate = 128000;
+       opts->device_path = "/dev/ttyUSB0";
+}
+
+int read_args(int argc, char *argv[], struct plotter_options *opts)
+{
+       int option_index = 0, c;
+       static struct option long_options[] = {
+               { .val = 'd', .name = "device", .has_arg = 1, },
+               { .val = 'o', .name = "output", .has_arg = 1 },
+               { .val = 'o', .name = "baud", .has_arg = 1 },
+       };
+       char short_options[] = "d:o:b:";
+
+       set_default_options(opts);
+
+       while (1) {
+               c = getopt_long(argc, argv, short_options, long_options,
+                               &option_index);
+
+               if (c == -1)
+                       break;
+
+               switch (c) {
+               case 'd':
+                       opts->device_path = optarg;
+                       break;
+               case 'o':
+                       opts->output_path = optarg;
+                       break;
+               case 'b':
+                       opts->baud_rate = atoi(optarg);
+                       break;
+               case '?':
+                       return -1;
+               }
+       }
+
+       while (optind < argc) {
+               /* The rest of the options, ignored */
+               optind++;
+       }
+
+       return 0;
+}
diff --git a/options.h b/options.h
new file mode 100644 (file)
index 0000000..5312e42
--- /dev/null
+++ b/options.h
@@ -0,0 +1,12 @@
+#ifndef _OPTIONS_H
+#define _OPTIONS_H
+
+struct plotter_options {
+       char *device_path;
+       char *output_path;
+       int baud_rate;
+};
+
+int read_args(int argc, char *argv[], struct plotter_options *opts);
+
+#endif