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