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