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