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