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