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