]> git.itanic.dy.fi Git - log-plotter/blob - options.c
options: Make trace level adjustable
[log-plotter] / options.c
1 #include <getopt.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6 #include "options.h"
7 #include "debug.h"
8
9 static void set_default_options(struct plotter_options *opts)
10 {
11         bzero(opts, sizeof(*opts));
12
13         opts->baud_rate = 128000;
14         opts->device_path = "/dev/ttyUSB0";
15 }
16
17 int read_args(int argc, char *argv[], struct plotter_options *opts)
18 {
19         int option_index = 0, c;
20         static struct option long_options[] = {
21                 { .val = 'd', .name = "device", .has_arg = 1, },
22                 { .val = 'o', .name = "output", .has_arg = 1 },
23                 { .val = 'o', .name = "baud", .has_arg = 1 },
24                 { .val = 'v', .name = "verbose", .has_arg = 2 },
25                 { .val = 'q', .name = "quiet", },
26         };
27         char short_options[] = "d:o:b:vq";
28
29         set_default_options(opts);
30
31         while (1) {
32                 c = getopt_long(argc, argv, short_options, long_options,
33                                 &option_index);
34
35                 if (c == -1)
36                         break;
37
38                 switch (c) {
39                 case 'd':
40                         opts->device_path = optarg;
41                         break;
42                 case 'o':
43                         opts->output_path = optarg;
44                         break;
45                 case 'b':
46                         opts->baud_rate = atoi(optarg);
47                         break;
48                 case 'v':
49                         trace_level++;
50                         pr_debug("Increased trace level to %d\n", trace_level);
51                         break;
52                 case 'q':
53                         trace_level--;
54                         pr_debug("Degreased trace level to %d\n", trace_level);
55                         break;
56                 case '?':
57                         return -1;
58                 }
59         }
60
61         while (optind < argc) {
62                 /* The rest of the options, ignored */
63                 optind++;
64         }
65
66         return 0;
67 }