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