Pass unitsize to srd_session_send() directly.
[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 <glib.h>
21 #include "sigrok-cli.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 /*
53  * Defines a callback function that generates an error if an
54  * option occurs twice.
55  */
56 #define CHECK_ONCE(option) \
57 static gboolean check_ ## option                                          \
58         (const gchar *option_name, const gchar *value,                    \
59         gpointer data, GError **error)                                    \
60 {                                                                         \
61         (void)data;                                                       \
62                                                                           \
63         static gboolean seen = FALSE;                                     \
64         if (seen) {                                                       \
65                 g_set_error(error, G_OPTION_ERROR, G_OPTION_ERROR_FAILED, \
66                             "superfluous option \"%s\"", option_name);    \
67                 return FALSE;                                             \
68         }                                                                 \
69                                                                           \
70         option = g_strdup(value);                                         \
71         seen = TRUE;                                                      \
72         return TRUE;                                                      \
73 }
74
75 CHECK_ONCE(opt_drv)
76 CHECK_ONCE(opt_config)
77 CHECK_ONCE(opt_input_format)
78 CHECK_ONCE(opt_output_format)
79 CHECK_ONCE(opt_transform_module)
80 CHECK_ONCE(opt_channels)
81 CHECK_ONCE(opt_channel_group)
82 CHECK_ONCE(opt_triggers)
83 #ifdef HAVE_SRD
84 CHECK_ONCE(opt_pds)
85 CHECK_ONCE(opt_pd_stack)
86 CHECK_ONCE(opt_pd_annotations)
87 CHECK_ONCE(opt_pd_meta)
88 CHECK_ONCE(opt_pd_binary)
89 #endif
90 CHECK_ONCE(opt_time)
91 CHECK_ONCE(opt_samples)
92 CHECK_ONCE(opt_frames)
93 CHECK_ONCE(opt_get)
94
95 #undef CHECK_STR_ONCE
96
97 static gchar **input_file_array = NULL;
98 static gchar **output_file_array = NULL;
99
100 static const GOptionEntry optargs[] = {
101         {"version", 'V', 0, G_OPTION_ARG_NONE, &opt_version,
102                         "Show version and support list", NULL},
103         {"loglevel", 'l', 0, G_OPTION_ARG_INT, &opt_loglevel,
104                         "Set loglevel (5 is most verbose)", NULL},
105         {"driver", 'd', 0, G_OPTION_ARG_CALLBACK, &check_opt_drv,
106                         "The driver to use", NULL},
107         {"config", 'c', 0, G_OPTION_ARG_CALLBACK, &check_opt_config,
108                         "Specify device configuration options", NULL},
109         {"input-file", 'i', 0, G_OPTION_ARG_FILENAME_ARRAY, &input_file_array,
110                         "Load input from file", NULL},
111         {"input-format", 'I', 0, G_OPTION_ARG_CALLBACK, &check_opt_input_format,
112                         "Input format", NULL},
113         {"output-file", 'o', 0, G_OPTION_ARG_FILENAME_ARRAY, &output_file_array,
114                         "Save output to file", NULL},
115         {"output-format", 'O', 0, G_OPTION_ARG_CALLBACK, &check_opt_output_format,
116                         "Output format", NULL},
117         {"transform-module", 'T', 0, G_OPTION_ARG_CALLBACK, &check_opt_transform_module,
118                         "Transform module", NULL},
119         {"channels", 'C', 0, G_OPTION_ARG_CALLBACK, &check_opt_channels,
120                         "Channels to use", NULL},
121         {"channel-group", 'g', 0, G_OPTION_ARG_CALLBACK, &check_opt_channel_group,
122                         "Channel groups", NULL},
123         {"triggers", 't', 0, G_OPTION_ARG_CALLBACK, &check_opt_triggers,
124                         "Trigger configuration", NULL},
125         {"wait-trigger", 'w', 0, G_OPTION_ARG_NONE, &opt_wait_trigger,
126                         "Wait for trigger", NULL},
127 #ifdef HAVE_SRD
128         {"protocol-decoders", 'P', 0, G_OPTION_ARG_CALLBACK, &check_opt_pds,
129                         "Protocol decoders to run", NULL},
130         {"protocol-decoder-stack", 'S', 0, G_OPTION_ARG_CALLBACK, &check_opt_pd_stack,
131                         "Protocol decoder stack", NULL},
132         {"protocol-decoder-annotations", 'A', 0, G_OPTION_ARG_CALLBACK, &check_opt_pd_annotations,
133                         "Protocol decoder annotation(s) to show", NULL},
134         {"protocol-decoder-meta", 'M', 0, G_OPTION_ARG_CALLBACK, &check_opt_pd_meta,
135                         "Protocol decoder meta output to show", NULL},
136         {"protocol-decoder-binary", 'B', 0, G_OPTION_ARG_CALLBACK, &check_opt_pd_binary,
137                         "Protocol decoder binary output to show", NULL},
138 #endif
139         {"scan", 0, 0, G_OPTION_ARG_NONE, &opt_scan_devs,
140                         "Scan for devices", NULL},
141         {"show", 0, 0, G_OPTION_ARG_NONE, &opt_show,
142                         "Show device detail", NULL},
143         {"time", 0, 0, G_OPTION_ARG_CALLBACK, &check_opt_time,
144                         "How long to sample (ms)", NULL},
145         {"samples", 0, 0, G_OPTION_ARG_CALLBACK, &check_opt_samples,
146                         "Number of samples to acquire", NULL},
147         {"frames", 0, 0, G_OPTION_ARG_CALLBACK, &check_opt_frames,
148                         "Number of frames to acquire", NULL},
149         {"continuous", 0, 0, G_OPTION_ARG_NONE, &opt_continuous,
150                         "Sample continuously", NULL},
151         {"get", 0, 0, G_OPTION_ARG_CALLBACK, &check_opt_get, "Get device option only", NULL},
152         {"set", 0, 0, G_OPTION_ARG_NONE, &opt_set, "Set device options only", NULL},
153         {NULL, 0, 0, 0, NULL, NULL, NULL}
154 };
155
156 /*
157  * Parses the command line and sets all the 'opt_...' variables.
158  * Returns zero on success, non-zero otherwise.
159  */
160 int parse_options(int argc, char **argv)
161 {
162         GError *error = NULL;
163         GOptionContext *context = g_option_context_new(NULL);
164         int ret = 1;
165
166         g_option_context_add_main_entries(context, optargs, NULL);
167
168         if (!g_option_context_parse(context, &argc, &argv, &error)) {
169                 g_critical("%s", error->message);
170                 goto done;
171         }
172
173         /*
174          * Because of encoding issues with filenames (mentioned in the glib
175          * documentation), we don't check them with a callback function, but
176          * collect them into arrays and then check if the arrays contain at
177          * most one element.
178          */
179         if (NULL != input_file_array) {
180                 if (NULL != input_file_array[0] && NULL != input_file_array[1]) {
181                         g_critical("option \"--input-file/-i\" only allowed once");
182                         goto done;
183                 }
184                 opt_input_file = g_strdup(input_file_array[0]);
185         }
186
187         if (NULL != output_file_array) {
188                 if (NULL != output_file_array[0] && NULL != output_file_array[1]) {
189                         g_critical("option \"--output-file/-o\" only allowed once");
190                         goto done;
191                 }
192                 opt_output_file = g_strdup(output_file_array[0]);
193         }
194
195         if (1 != argc) {
196                 g_critical("superfluous command line argument \"%s\"", argv[1]);
197                 goto done;
198         }
199
200         ret = 0;
201
202 done:
203         g_option_context_free(context);
204         g_strfreev(input_file_array);
205         g_strfreev(output_file_array);
206         input_file_array = NULL;
207         output_file_array = NULL;
208
209         return ret;
210 }
211
212 void show_help(void)
213 {
214         GOptionContext *context = g_option_context_new(NULL);
215         g_option_context_add_main_entries(context, optargs, NULL);
216
217         char *help = g_option_context_get_help(context, TRUE, NULL);
218         printf("%s", help);
219         g_free(help);
220
221         g_option_context_free(context);
222 }