]>
Commit | Line | Data |
---|---|---|
cd62e027 JS |
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 | ||
d486cbdd | 20 | #include <config.h> |
cd62e027 | 21 | #include <glib.h> |
662a1e27 | 22 | #include "sigrok-cli.h" |
cd62e027 JS |
23 | |
24 | gboolean opt_version = FALSE; | |
6293db8a | 25 | gboolean opt_list_supported = FALSE; |
cd62e027 JS |
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; | |
551f570c | 36 | gchar **opt_pds = NULL; |
cd62e027 | 37 | #ifdef HAVE_SRD |
cd62e027 JS |
38 | gchar *opt_pd_annotations = NULL; |
39 | gchar *opt_pd_meta = NULL; | |
40 | gchar *opt_pd_binary = NULL; | |
08e9378b | 41 | gboolean opt_pd_samplenum = FALSE; |
cd62e027 JS |
42 | #endif |
43 | gchar *opt_input_format = NULL; | |
44 | gchar *opt_output_format = NULL; | |
6f7b4c5d | 45 | gchar *opt_transform_module = NULL; |
2620358a | 46 | gboolean opt_show = FALSE; |
cd62e027 JS |
47 | gchar *opt_time = NULL; |
48 | gchar *opt_samples = NULL; | |
49 | gchar *opt_frames = NULL; | |
2620358a | 50 | gboolean opt_continuous = FALSE; |
62a64762 | 51 | gchar *opt_get = NULL; |
2620358a | 52 | gboolean opt_set = FALSE; |
cd62e027 | 53 | |
f0f54487 UH |
54 | /* |
55 | * Defines a callback function that generates an error if an | |
56 | * option occurs twice. | |
57 | */ | |
0894b972 JS |
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; \ | |
64 | \ | |
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 | } \ | |
71 | \ | |
72 | option = g_strdup(value); \ | |
73 | seen = TRUE; \ | |
74 | return TRUE; \ | |
75 | } | |
76 | ||
77 | CHECK_ONCE(opt_drv) | |
78 | CHECK_ONCE(opt_config) | |
79 | CHECK_ONCE(opt_input_format) | |
80 | CHECK_ONCE(opt_output_format) | |
6f7b4c5d | 81 | CHECK_ONCE(opt_transform_module) |
0894b972 JS |
82 | CHECK_ONCE(opt_channels) |
83 | CHECK_ONCE(opt_channel_group) | |
84 | CHECK_ONCE(opt_triggers) | |
85 | #ifdef HAVE_SRD | |
0894b972 JS |
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) | |
62a64762 | 93 | CHECK_ONCE(opt_get) |
0894b972 JS |
94 | |
95 | #undef CHECK_STR_ONCE | |
96 | ||
44ac7614 JS |
97 | static gchar **input_file_array = NULL; |
98 | static gchar **output_file_array = NULL; | |
99 | ||
cd62e027 JS |
100 | static const GOptionEntry optargs[] = { |
101 | {"version", 'V', 0, G_OPTION_ARG_NONE, &opt_version, | |
6293db8a UH |
102 | "Show version", NULL}, |
103 | {"list-supported", 'L', 0, G_OPTION_ARG_NONE, &opt_list_supported, | |
104 | "List supported devices/modules/decoders", NULL}, | |
cd62e027 JS |
105 | {"loglevel", 'l', 0, G_OPTION_ARG_INT, &opt_loglevel, |
106 | "Set loglevel (5 is most verbose)", NULL}, | |
0894b972 | 107 | {"driver", 'd', 0, G_OPTION_ARG_CALLBACK, &check_opt_drv, |
cd62e027 | 108 | "The driver to use", NULL}, |
0894b972 | 109 | {"config", 'c', 0, G_OPTION_ARG_CALLBACK, &check_opt_config, |
cd62e027 | 110 | "Specify device configuration options", NULL}, |
44ac7614 | 111 | {"input-file", 'i', 0, G_OPTION_ARG_FILENAME_ARRAY, &input_file_array, |
cd62e027 | 112 | "Load input from file", NULL}, |
0894b972 | 113 | {"input-format", 'I', 0, G_OPTION_ARG_CALLBACK, &check_opt_input_format, |
cd62e027 | 114 | "Input format", NULL}, |
44ac7614 | 115 | {"output-file", 'o', 0, G_OPTION_ARG_FILENAME_ARRAY, &output_file_array, |
cd62e027 | 116 | "Save output to file", NULL}, |
0894b972 | 117 | {"output-format", 'O', 0, G_OPTION_ARG_CALLBACK, &check_opt_output_format, |
cd62e027 | 118 | "Output format", NULL}, |
6f7b4c5d UH |
119 | {"transform-module", 'T', 0, G_OPTION_ARG_CALLBACK, &check_opt_transform_module, |
120 | "Transform module", NULL}, | |
0894b972 | 121 | {"channels", 'C', 0, G_OPTION_ARG_CALLBACK, &check_opt_channels, |
cd62e027 | 122 | "Channels to use", NULL}, |
0894b972 | 123 | {"channel-group", 'g', 0, G_OPTION_ARG_CALLBACK, &check_opt_channel_group, |
cd62e027 | 124 | "Channel groups", NULL}, |
0894b972 | 125 | {"triggers", 't', 0, G_OPTION_ARG_CALLBACK, &check_opt_triggers, |
cd62e027 JS |
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 | |
551f570c | 130 | {"protocol-decoders", 'P', 0, G_OPTION_ARG_STRING_ARRAY, &opt_pds, |
cd62e027 | 131 | "Protocol decoders to run", NULL}, |
0894b972 | 132 | {"protocol-decoder-annotations", 'A', 0, G_OPTION_ARG_CALLBACK, &check_opt_pd_annotations, |
cd62e027 | 133 | "Protocol decoder annotation(s) to show", NULL}, |
0894b972 | 134 | {"protocol-decoder-meta", 'M', 0, G_OPTION_ARG_CALLBACK, &check_opt_pd_meta, |
cd62e027 | 135 | "Protocol decoder meta output to show", NULL}, |
0894b972 | 136 | {"protocol-decoder-binary", 'B', 0, G_OPTION_ARG_CALLBACK, &check_opt_pd_binary, |
cd62e027 | 137 | "Protocol decoder binary output to show", NULL}, |
08e9378b GS |
138 | {"protocol-decoder-samplenum", 0, 0, G_OPTION_ARG_NONE, &opt_pd_samplenum, |
139 | "Show sample numbers in protocol decoder output", NULL}, | |
cd62e027 JS |
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, | |
96d09211 | 144 | "Show device/format/decoder details", NULL}, |
0894b972 | 145 | {"time", 0, 0, G_OPTION_ARG_CALLBACK, &check_opt_time, |
cd62e027 | 146 | "How long to sample (ms)", NULL}, |
0894b972 | 147 | {"samples", 0, 0, G_OPTION_ARG_CALLBACK, &check_opt_samples, |
cd62e027 | 148 | "Number of samples to acquire", NULL}, |
0894b972 | 149 | {"frames", 0, 0, G_OPTION_ARG_CALLBACK, &check_opt_frames, |
cd62e027 JS |
150 | "Number of frames to acquire", NULL}, |
151 | {"continuous", 0, 0, G_OPTION_ARG_NONE, &opt_continuous, | |
152 | "Sample continuously", NULL}, | |
62a64762 | 153 | {"get", 0, 0, G_OPTION_ARG_CALLBACK, &check_opt_get, "Get device option only", NULL}, |
cd62e027 JS |
154 | {"set", 0, 0, G_OPTION_ARG_NONE, &opt_set, "Set device options only", NULL}, |
155 | {NULL, 0, 0, 0, NULL, NULL, NULL} | |
156 | }; | |
157 | ||
f0f54487 UH |
158 | /* |
159 | * Parses the command line and sets all the 'opt_...' variables. | |
160 | * Returns zero on success, non-zero otherwise. | |
161 | */ | |
cd62e027 JS |
162 | int parse_options(int argc, char **argv) |
163 | { | |
164 | GError *error = NULL; | |
165 | GOptionContext *context = g_option_context_new(NULL); | |
166 | int ret = 1; | |
167 | ||
168 | g_option_context_add_main_entries(context, optargs, NULL); | |
169 | ||
170 | if (!g_option_context_parse(context, &argc, &argv, &error)) { | |
171 | g_critical("%s", error->message); | |
172 | goto done; | |
173 | } | |
174 | ||
44ac7614 JS |
175 | /* |
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. | |
180 | */ | |
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; | |
185 | } | |
186 | opt_input_file = g_strdup(input_file_array[0]); | |
187 | } | |
188 | ||
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; | |
193 | } | |
194 | opt_output_file = g_strdup(output_file_array[0]); | |
195 | } | |
196 | ||
41ce2cbb JS |
197 | if (1 != argc) { |
198 | g_critical("superfluous command line argument \"%s\"", argv[1]); | |
199 | goto done; | |
200 | } | |
201 | ||
cd62e027 JS |
202 | ret = 0; |
203 | ||
204 | done: | |
205 | g_option_context_free(context); | |
44ac7614 JS |
206 | g_strfreev(input_file_array); |
207 | g_strfreev(output_file_array); | |
208 | input_file_array = NULL; | |
209 | output_file_array = NULL; | |
cd62e027 JS |
210 | |
211 | return ret; | |
212 | } | |
213 | ||
214 | void show_help(void) | |
215 | { | |
216 | GOptionContext *context = g_option_context_new(NULL); | |
217 | g_option_context_add_main_entries(context, optargs, NULL); | |
218 | ||
219 | char *help = g_option_context_get_help(context, TRUE, NULL); | |
220 | printf("%s", help); | |
221 | g_free(help); | |
222 | ||
223 | g_option_context_free(context); | |
65c1ecf9 GS |
224 | |
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"); | |
cd62e027 | 236 | } |