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