decode: accept annotation rows for -A specs
[sigrok-cli.git] / options.c
blobaf6a7fe962032675b99d770d3ce458af278194ca
1 /*
2 * This file is part of the sigrok-cli project.
4 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
20 #include <config.h>
21 #include <glib.h>
22 #include "sigrok-cli.h"
24 gboolean opt_version = FALSE;
25 gboolean opt_list_supported = FALSE;
26 gboolean opt_list_supported_wiki = FALSE;
27 gint opt_loglevel = SR_LOG_WARN; /* Show errors+warnings by default. */
28 gboolean opt_scan_devs = FALSE;
29 gboolean opt_wait_trigger = FALSE;
30 gchar *opt_input_file = NULL;
31 gchar *opt_output_file = NULL;
32 gchar *opt_drv = NULL;
33 gchar *opt_config = NULL;
34 gchar *opt_channels = NULL;
35 gchar *opt_channel_group = NULL;
36 gchar *opt_triggers = NULL;
37 gchar **opt_pds = NULL;
38 #ifdef HAVE_SRD
39 gchar *opt_pd_annotations = NULL;
40 gchar *opt_pd_meta = NULL;
41 gchar *opt_pd_binary = NULL;
42 gboolean opt_pd_samplenum = FALSE;
43 #endif
44 gchar *opt_input_format = NULL;
45 gchar *opt_output_format = NULL;
46 gchar *opt_transform_module = NULL;
47 gboolean opt_show = FALSE;
48 gchar *opt_time = NULL;
49 gchar *opt_samples = NULL;
50 gchar *opt_frames = NULL;
51 gboolean opt_continuous = FALSE;
52 gchar *opt_get = NULL;
53 gboolean opt_set = FALSE;
56 * Defines a callback function that generates an error if an
57 * option occurs twice.
59 #define CHECK_ONCE(option) \
60 static gboolean check_ ## option \
61 (const gchar *option_name, const gchar *value, \
62 gpointer data, GError **error) \
63 { \
64 (void)data; \
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 } \
73 option = g_strdup(value); \
74 seen = TRUE; \
75 return TRUE; \
78 CHECK_ONCE(opt_drv)
79 CHECK_ONCE(opt_config)
80 CHECK_ONCE(opt_input_format)
81 CHECK_ONCE(opt_output_format)
82 CHECK_ONCE(opt_transform_module)
83 CHECK_ONCE(opt_channels)
84 CHECK_ONCE(opt_channel_group)
85 CHECK_ONCE(opt_triggers)
86 #ifdef HAVE_SRD
87 CHECK_ONCE(opt_pd_annotations)
88 CHECK_ONCE(opt_pd_meta)
89 CHECK_ONCE(opt_pd_binary)
90 #endif
91 CHECK_ONCE(opt_time)
92 CHECK_ONCE(opt_samples)
93 CHECK_ONCE(opt_frames)
94 CHECK_ONCE(opt_get)
96 #undef CHECK_STR_ONCE
98 static gchar **input_file_array = NULL;
99 static gchar **output_file_array = NULL;
101 static 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}
162 * Parses the command line and sets all the 'opt_...' variables.
163 * Returns zero on success, non-zero otherwise.
165 int parse_options(int argc, char **argv)
167 GError *error = NULL;
168 GOptionContext *context = g_option_context_new(NULL);
169 int ret = 1;
171 g_option_context_add_main_entries(context, optargs, NULL);
173 if (!g_option_context_parse(context, &argc, &argv, &error)) {
174 g_critical("%s", error->message);
175 goto done;
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.
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;
189 opt_input_file = g_strdup(input_file_array[0]);
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;
197 opt_output_file = g_strdup(output_file_array[0]);
200 if (1 != argc) {
201 g_critical("superfluous command line argument \"%s\"", argv[1]);
202 goto done;
205 ret = 0;
207 done:
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;
214 return ret;
217 void show_help(void)
219 GOptionContext *context = g_option_context_new(NULL);
220 g_option_context_add_main_entries(context, optargs, NULL);
222 char *help = g_option_context_get_help(context, TRUE, NULL);
223 printf("%s", help);
224 g_free(help);
226 g_option_context_free(context);
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");