]> sigrok.org Git - sigrok-cli.git/blob - options.c
Implement support for running transform modules.
[sigrok-cli.git] / options.c
1 /*
2  * This file is part of the sigrok-cli project.
3  *
4  * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "sigrok-cli.h"
21 #include <glib.h>
22
23 gboolean opt_version = FALSE;
24 gint opt_loglevel = SR_LOG_WARN; /* Show errors+warnings by default. */
25 gboolean opt_scan_devs = FALSE;
26 gboolean opt_wait_trigger = FALSE;
27 gchar *opt_input_file = NULL;
28 gchar *opt_output_file = NULL;
29 gchar *opt_drv = NULL;
30 gchar *opt_config = NULL;
31 gchar *opt_channels = NULL;
32 gchar *opt_channel_group = NULL;
33 gchar *opt_triggers = NULL;
34 gchar *opt_pds = NULL;
35 #ifdef HAVE_SRD
36 gchar *opt_pd_stack = NULL;
37 gchar *opt_pd_annotations = NULL;
38 gchar *opt_pd_meta = NULL;
39 gchar *opt_pd_binary = NULL;
40 #endif
41 gchar *opt_input_format = NULL;
42 gchar *opt_output_format = NULL;
43 gchar *opt_transform_module = NULL;
44 gchar *opt_show = NULL;
45 gchar *opt_time = NULL;
46 gchar *opt_samples = NULL;
47 gchar *opt_frames = NULL;
48 gchar *opt_continuous = NULL;
49 gchar *opt_get = NULL;
50 gchar *opt_set = NULL;
51
52 /* defines a callback function that generates
53    an error if an option occurs twice */
54 #define CHECK_ONCE(option) \
55 static gboolean check_ ## option                                          \
56         (const gchar *option_name, const gchar *value,                    \
57         gpointer data, GError **error)                                    \
58 {                                                                         \
59         (void)data;                                                       \
60                                                                           \
61         static gboolean seen = FALSE;                                     \
62         if (seen) {                                                       \
63                 g_set_error(error, G_OPTION_ERROR, G_OPTION_ERROR_FAILED, \
64                             "superfluous option \"%s\"", option_name);    \
65                 return FALSE;                                             \
66         }                                                                 \
67                                                                           \
68         option = g_strdup(value);                                         \
69         seen = TRUE;                                                      \
70         return TRUE;                                                      \
71 }
72
73 CHECK_ONCE(opt_drv)
74 CHECK_ONCE(opt_config)
75 CHECK_ONCE(opt_input_format)
76 CHECK_ONCE(opt_output_format)
77 CHECK_ONCE(opt_transform_module)
78 CHECK_ONCE(opt_channels)
79 CHECK_ONCE(opt_channel_group)
80 CHECK_ONCE(opt_triggers)
81 #ifdef HAVE_SRD
82 CHECK_ONCE(opt_pds)
83 CHECK_ONCE(opt_pd_stack)
84 CHECK_ONCE(opt_pd_annotations)
85 CHECK_ONCE(opt_pd_meta)
86 CHECK_ONCE(opt_pd_binary)
87 #endif
88 CHECK_ONCE(opt_time)
89 CHECK_ONCE(opt_samples)
90 CHECK_ONCE(opt_frames)
91 CHECK_ONCE(opt_get)
92
93 #undef CHECK_STR_ONCE
94
95 static gchar **input_file_array = NULL;
96 static gchar **output_file_array = NULL;
97
98 static const GOptionEntry optargs[] = {
99         {"version", 'V', 0, G_OPTION_ARG_NONE, &opt_version,
100                         "Show version and support list", NULL},
101         {"loglevel", 'l', 0, G_OPTION_ARG_INT, &opt_loglevel,
102                         "Set loglevel (5 is most verbose)", NULL},
103         {"driver", 'd', 0, G_OPTION_ARG_CALLBACK, &check_opt_drv,
104                         "The driver to use", NULL},
105         {"config", 'c', 0, G_OPTION_ARG_CALLBACK, &check_opt_config,
106                         "Specify device configuration options", NULL},
107         {"input-file", 'i', 0, G_OPTION_ARG_FILENAME_ARRAY, &input_file_array,
108                         "Load input from file", NULL},
109         {"input-format", 'I', 0, G_OPTION_ARG_CALLBACK, &check_opt_input_format,
110                         "Input format", NULL},
111         {"output-file", 'o', 0, G_OPTION_ARG_FILENAME_ARRAY, &output_file_array,
112                         "Save output to file", NULL},
113         {"output-format", 'O', 0, G_OPTION_ARG_CALLBACK, &check_opt_output_format,
114                         "Output format", NULL},
115         {"transform-module", 'T', 0, G_OPTION_ARG_CALLBACK, &check_opt_transform_module,
116                         "Transform module", NULL},
117         {"channels", 'C', 0, G_OPTION_ARG_CALLBACK, &check_opt_channels,
118                         "Channels to use", NULL},
119         {"channel-group", 'g', 0, G_OPTION_ARG_CALLBACK, &check_opt_channel_group,
120                         "Channel groups", NULL},
121         {"triggers", 't', 0, G_OPTION_ARG_CALLBACK, &check_opt_triggers,
122                         "Trigger configuration", NULL},
123         {"wait-trigger", 'w', 0, G_OPTION_ARG_NONE, &opt_wait_trigger,
124                         "Wait for trigger", NULL},
125 #ifdef HAVE_SRD
126         {"protocol-decoders", 'P', 0, G_OPTION_ARG_CALLBACK, &check_opt_pds,
127                         "Protocol decoders to run", NULL},
128         {"protocol-decoder-stack", 'S', 0, G_OPTION_ARG_CALLBACK, &check_opt_pd_stack,
129                         "Protocol decoder stack", NULL},
130         {"protocol-decoder-annotations", 'A', 0, G_OPTION_ARG_CALLBACK, &check_opt_pd_annotations,
131                         "Protocol decoder annotation(s) to show", NULL},
132         {"protocol-decoder-meta", 'M', 0, G_OPTION_ARG_CALLBACK, &check_opt_pd_meta,
133                         "Protocol decoder meta output to show", NULL},
134         {"protocol-decoder-binary", 'B', 0, G_OPTION_ARG_CALLBACK, &check_opt_pd_binary,
135                         "Protocol decoder binary output to show", NULL},
136 #endif
137         {"scan", 0, 0, G_OPTION_ARG_NONE, &opt_scan_devs,
138                         "Scan for devices", NULL},
139         {"show", 0, 0, G_OPTION_ARG_NONE, &opt_show,
140                         "Show device detail", NULL},
141         {"time", 0, 0, G_OPTION_ARG_CALLBACK, &check_opt_time,
142                         "How long to sample (ms)", NULL},
143         {"samples", 0, 0, G_OPTION_ARG_CALLBACK, &check_opt_samples,
144                         "Number of samples to acquire", NULL},
145         {"frames", 0, 0, G_OPTION_ARG_CALLBACK, &check_opt_frames,
146                         "Number of frames to acquire", NULL},
147         {"continuous", 0, 0, G_OPTION_ARG_NONE, &opt_continuous,
148                         "Sample continuously", NULL},
149         {"get", 0, 0, G_OPTION_ARG_CALLBACK, &check_opt_get, "Get device option only", NULL},
150         {"set", 0, 0, G_OPTION_ARG_NONE, &opt_set, "Set device options only", NULL},
151         {NULL, 0, 0, 0, NULL, NULL, NULL}
152 };
153
154 /* Parses the command line and sets all the 'opt_...' variables.
155    Returns zero on success, non-zero otherwise. */
156 int parse_options(int argc, char **argv)
157 {
158         GError *error = NULL;
159         GOptionContext *context = g_option_context_new(NULL);
160         int ret = 1;
161
162         g_option_context_add_main_entries(context, optargs, NULL);
163
164         if (!g_option_context_parse(context, &argc, &argv, &error)) {
165                 g_critical("%s", error->message);
166                 goto done;
167         }
168
169         /*
170          * Because of encoding issues with filenames (mentioned in the glib
171          * documentation), we don't check them with a callback function, but
172          * collect them into arrays and then check if the arrays contain at
173          * most one element.
174          */
175         if (NULL != input_file_array) {
176                 if (NULL != input_file_array[0] && NULL != input_file_array[1]) {
177                         g_critical("option \"--input-file/-i\" only allowed once");
178                         goto done;
179                 }
180                 opt_input_file = g_strdup(input_file_array[0]);
181         }
182
183         if (NULL != output_file_array) {
184                 if (NULL != output_file_array[0] && NULL != output_file_array[1]) {
185                         g_critical("option \"--output-file/-o\" only allowed once");
186                         goto done;
187                 }
188                 opt_output_file = g_strdup(output_file_array[0]);
189         }
190
191         if (1 != argc) {
192                 g_critical("superfluous command line argument \"%s\"", argv[1]);
193                 goto done;
194         }
195
196         ret = 0;
197
198 done:
199         g_option_context_free(context);
200         g_strfreev(input_file_array);
201         g_strfreev(output_file_array);
202         input_file_array = NULL;
203         output_file_array = NULL;
204
205         return ret;
206 }
207
208 void show_help(void)
209 {
210         GOptionContext *context = g_option_context_new(NULL);
211         g_option_context_add_main_entries(context, optargs, NULL);
212
213         char *help = g_option_context_get_help(context, TRUE, NULL);
214         printf("%s", help);
215         g_free(help);
216
217         g_option_context_free(context);
218 }