ca6cb1188444a213126f0f7bd83345a742c31d86
[sigrok-cli.git] / show.c
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 #include <string.h>
23
24 static gint sort_inputs(gconstpointer a, gconstpointer b)
25 {
26         return strcmp(sr_input_id_get((struct sr_input_module *)a),
27                         sr_input_id_get((struct sr_input_module *)b));
28 }
29
30 static gint sort_outputs(gconstpointer a, gconstpointer b)
31 {
32         return strcmp(sr_output_id_get((struct sr_output_module *)a),
33                         sr_output_id_get((struct sr_output_module *)b));
34 }
35
36 static gint sort_drivers(gconstpointer a, gconstpointer b)
37 {
38         const struct sr_dev_driver *sdda = a, *sddb = b;
39
40         return strcmp(sdda->name, sddb->name);
41 }
42
43 #ifdef HAVE_SRD
44 static gint sort_pds(gconstpointer a, gconstpointer b)
45 {
46         const struct srd_decoder *sda = a, *sdb = b;
47
48         return strcmp(sda->id, sdb->id);
49 }
50 #endif
51
52 void show_version(void)
53 {
54         struct sr_dev_driver **drivers, *driver;
55         const struct sr_input_module **inputs, *input;
56         const struct sr_output_module **outputs, *output;
57         const GSList *l;
58         GSList *sl;
59         int i;
60 #ifdef HAVE_SRD
61         struct srd_decoder *dec;
62 #endif
63
64         printf("sigrok-cli %s\n\n", VERSION);
65
66         printf("Using libsigrok %s (lib version %s).\n",
67                sr_package_version_string_get(), sr_lib_version_string_get());
68 #ifdef HAVE_SRD
69         printf("Using libsigrokdecode %s (lib version %s).\n\n",
70                srd_package_version_string_get(), srd_lib_version_string_get());
71 #endif
72
73         printf("Supported hardware drivers:\n");
74         drivers = sr_driver_list();
75         for (sl = NULL, i = 0; drivers[i]; i++)
76                 sl = g_slist_append(sl, drivers[i]);
77         sl = g_slist_sort(sl, sort_drivers);
78         for (l = sl; l; l = l->next) {
79                 driver = l->data;
80                 printf("  %-20s %s\n", driver->name, driver->longname);
81         }
82         printf("\n");
83         g_slist_free(sl);
84
85         printf("Supported input formats:\n");
86         inputs = sr_input_list();
87         for (sl = NULL, i = 0; inputs[i]; i++)
88                 sl = g_slist_append(sl, (gpointer)inputs[i]);
89         sl = g_slist_sort(sl, sort_inputs);
90         for (l = sl; l; l = l->next) {
91                 input = l->data;
92                 printf("  %-20s %s\n", sr_input_id_get(input),
93                                 sr_input_description_get(input));
94         }
95         printf("\n");
96         g_slist_free(sl);
97
98         printf("Supported output formats:\n");
99         outputs = sr_output_list();
100         for (sl = NULL, i = 0; outputs[i]; i++)
101                 sl = g_slist_append(sl, (gpointer)outputs[i]);
102         sl = g_slist_sort(sl, sort_outputs);
103         for (l = sl; l; l = l->next) {
104                 output = l->data;
105                 printf("  %-20s %s\n", sr_output_id_get(output),
106                                 sr_output_description_get(output));
107         }
108         printf("\n");
109         g_slist_free(sl);
110
111 #ifdef HAVE_SRD
112         if (srd_init(NULL) == SRD_OK) {
113                 printf("Supported protocol decoders:\n");
114                 srd_decoder_load_all();
115                 sl = g_slist_copy((GSList *)srd_decoder_list());
116                 sl = g_slist_sort(sl, sort_pds);
117                 for (l = sl; l; l = l->next) {
118                         dec = l->data;
119                         printf("  %-20s %s\n", dec->id, dec->longname);
120                         /* Print protocol description upon "-l 3" or higher. */
121                         if (opt_loglevel >= SR_LOG_INFO)
122                                 printf("  %-20s %s\n", "", dec->desc);
123                 }
124                 g_slist_free(sl);
125                 srd_exit();
126         }
127         printf("\n");
128 #endif
129 }
130
131 static gint sort_channels(gconstpointer a, gconstpointer b)
132 {
133         const struct sr_channel *pa = a, *pb = b;
134
135         return pa->index - pb->index;
136 }
137
138 static gboolean config_key_has_cap(const struct sr_dev_inst *sdi,
139            struct sr_channel_group *cg, uint32_t key, uint32_t capability)
140 {
141         struct sr_dev_driver *driver;
142         GVariant *gvar_opts;
143         const uint32_t *opts;
144         gsize num_opts, i;
145
146         driver = sr_dev_inst_driver_get(sdi);
147         if (sr_config_list(driver, sdi, cg, SR_CONF_DEVICE_OPTIONS,
148                         &gvar_opts) != SR_OK)
149                 return FALSE;
150
151         opts = g_variant_get_fixed_array(gvar_opts, &num_opts, sizeof(uint32_t));
152         for (i = 0; i < num_opts; i++) {
153                 if ((opts[i] & SR_CONF_MASK) == key) {
154                         if (opts[i] & capability)
155                                 return TRUE;
156                         else
157                                 return FALSE;
158                 }
159         }
160
161         return FALSE;
162 }
163
164
165 static void print_dev_line(const struct sr_dev_inst *sdi)
166 {
167         struct sr_channel *ch;
168         GSList *sl, *l, *channels;
169         GString *s;
170         GVariant *gvar;
171         struct sr_dev_driver *driver;
172         const char *vendor, *model, *version;
173
174         driver = sr_dev_inst_driver_get(sdi);
175         vendor = sr_dev_inst_vendor_get(sdi);
176         model = sr_dev_inst_model_get(sdi);
177         version = sr_dev_inst_version_get(sdi);
178         channels = sr_dev_inst_channels_get(sdi);
179
180         s = g_string_sized_new(128);
181         g_string_assign(s, driver->name);
182         if (config_key_has_cap(sdi, NULL, SR_CONF_CONN, SR_CONF_GET)
183                         && sr_config_get(driver, sdi, NULL, SR_CONF_CONN, &gvar) == SR_OK) {
184                 g_string_append(s, ":conn=");
185                 g_string_append(s, g_variant_get_string(gvar, NULL));
186                 g_variant_unref(gvar);
187         }
188         g_string_append(s, " - ");
189         if (vendor && vendor[0])
190                 g_string_append_printf(s, "%s ", vendor);
191         if (model && model[0])
192                 g_string_append_printf(s, "%s ", model);
193         if (version && version[0])
194                 g_string_append_printf(s, "%s ", version);
195         if (channels) {
196                 if (g_slist_length(channels) == 1) {
197                         ch = channels->data;
198                         g_string_append_printf(s, "with 1 channel: %s", ch->name);
199                 } else {
200                         sl = g_slist_sort(g_slist_copy(channels), sort_channels);
201                         g_string_append_printf(s, "with %d channels:", g_slist_length(sl));
202                         for (l = sl; l; l = l->next) {
203                                 ch = l->data;
204                                 g_string_append_printf(s, " %s", ch->name);
205                         }
206                         g_slist_free(sl);
207                 }
208         }
209         g_string_append_printf(s, "\n");
210         printf("%s", s->str);
211         g_string_free(s, TRUE);
212
213 }
214
215 void show_dev_list(void)
216 {
217         struct sr_dev_inst *sdi;
218         GSList *devices, *l;
219
220         if (!(devices = device_scan()))
221                 return;
222
223         printf("The following devices were found:\n");
224         for (l = devices; l; l = l->next) {
225                 sdi = l->data;
226                 print_dev_line(sdi);
227         }
228         g_slist_free(devices);
229
230 }
231
232 void show_drv_detail(struct sr_dev_driver *driver)
233 {
234         const struct sr_config_info *srci;
235         GVariant *gvar_opts;
236         const uint32_t *opts;
237         gsize num_elements, i;
238
239         if (sr_config_list(driver, NULL, NULL, SR_CONF_DEVICE_OPTIONS,
240                         &gvar_opts) == SR_OK) {
241                 opts = g_variant_get_fixed_array(gvar_opts, &num_elements,
242                                 sizeof(uint32_t));
243                 if (num_elements) {
244                         printf("Driver functions:\n");
245                         for (i = 0; i < num_elements; i++) {
246                                 if (!(srci = sr_config_info_get(opts[i] & SR_CONF_MASK)))
247                                         continue;
248                                 printf("    %s\n", srci->name);
249                         }
250                 }
251                 g_variant_unref(gvar_opts);
252         }
253
254         if (sr_config_list(driver, NULL, NULL, SR_CONF_SCAN_OPTIONS,
255                         &gvar_opts) == SR_OK) {
256                 opts = g_variant_get_fixed_array(gvar_opts, &num_elements,
257                                 sizeof(uint32_t));
258                 if (num_elements) {
259                         printf("Scan options:\n");
260                         for (i = 0; i < num_elements; i++) {
261                                 if (!(srci = sr_config_info_get(opts[i] & SR_CONF_MASK)))
262                                         continue;
263                                 printf("    %s\n", srci->id);
264                         }
265                 }
266                 g_variant_unref(gvar_opts);
267         }
268 }
269
270 void show_dev_detail(void)
271 {
272         struct sr_dev_driver *driver_from_opt, *driver;
273         struct sr_dev_inst *sdi;
274         const struct sr_config_info *srci;
275         struct sr_channel *ch;
276         struct sr_channel_group *channel_group, *cg;
277         GSList *devices, *cgl, *chl, *channel_groups;
278         GVariant *gvar_opts, *gvar_dict, *gvar_list, *gvar;
279         gsize num_opts, num_elements;
280         double dlow, dhigh, dcur_low, dcur_high;
281         const uint64_t *uint64, p, q, low, high;
282         uint64_t tmp_uint64, cur_low, cur_high, cur_p, cur_q;
283         const uint32_t *opts;
284         const int32_t *int32;
285         uint32_t key, o;
286         unsigned int num_devices, i;
287         char *tmp_str, *s, c;
288         const char **stropts;
289
290         if (parse_driver(opt_drv, &driver_from_opt, NULL)) {
291                 /* A driver was specified, report driver-wide options now. */
292                 show_drv_detail(driver_from_opt);
293         }
294
295         if (!(devices = device_scan())) {
296                 g_critical("No devices found.");
297                 return;
298         }
299
300         num_devices = g_slist_length(devices);
301         if (num_devices > 1) {
302                 g_critical("%d devices found. Use --scan to show them, "
303                                 "and select one to show.", num_devices);
304                 return;
305         }
306
307         sdi = devices->data;
308         g_slist_free(devices);
309         print_dev_line(sdi);
310
311         driver = sr_dev_inst_driver_get(sdi);
312         channel_groups = sr_dev_inst_channel_groups_get(sdi);
313
314         if (sr_dev_open(sdi) != SR_OK) {
315                 g_critical("Failed to open device.");
316                 return;
317         }
318
319         /* Selected channels and channel group may affect which options are
320          * returned, or which values for them. */
321         select_channels(sdi);
322         channel_group = select_channel_group(sdi);
323
324         if (sr_config_list(driver, sdi, channel_group, SR_CONF_DEVICE_OPTIONS,
325                         &gvar_opts) != SR_OK)
326                 /* Driver supports no device instance options. */
327                 return;
328
329         if (channel_groups) {
330                 printf("Channel groups:\n");
331                 for (cgl = channel_groups; cgl; cgl = cgl->next) {
332                         cg = cgl->data;
333                         printf("    %s: channel%s", cg->name,
334                                         g_slist_length(cg->channels) > 1 ? "s" : "");
335                         for (chl = cg->channels; chl; chl = chl->next) {
336                                 ch = chl->data;
337                                 printf(" %s", ch->name);
338                         }
339                         printf("\n");
340                 }
341         }
342
343         printf("Supported configuration options");
344         if (channel_groups) {
345                 if (!channel_group)
346                         printf(" across all channel groups");
347                 else
348                         printf(" on channel group %s", channel_group->name);
349         }
350         printf(":\n");
351         opts = g_variant_get_fixed_array(gvar_opts, &num_opts, sizeof(uint32_t));
352         for (o = 0; o < num_opts; o++) {
353                 key = opts[o] & SR_CONF_MASK;
354                 if (!(srci = sr_config_info_get(key)))
355                         continue;
356
357                 if (key == SR_CONF_TRIGGER_MATCH) {
358                         if (sr_config_list(driver, sdi, channel_group, key,
359                                         &gvar_list) != SR_OK) {
360                                 printf("\n");
361                                 continue;
362                         }
363                         int32 = g_variant_get_fixed_array(gvar_list,
364                                         &num_elements, sizeof(int32_t));
365                         printf("    Supported triggers: ");
366                         for (i = 0; i < num_elements; i++) {
367                                 switch(int32[i]) {
368                                 case SR_TRIGGER_ZERO:
369                                         c = '0';
370                                         break;
371                                 case SR_TRIGGER_ONE:
372                                         c = '1';
373                                         break;
374                                 case SR_TRIGGER_RISING:
375                                         c = 'r';
376                                         break;
377                                 case SR_TRIGGER_FALLING:
378                                         c = 'f';
379                                         break;
380                                 case SR_TRIGGER_EDGE:
381                                         c = 'e';
382                                         break;
383                                 case SR_TRIGGER_OVER:
384                                         c = 'o';
385                                         break;
386                                 case SR_TRIGGER_UNDER:
387                                         c = 'u';
388                                         break;
389                                 default:
390                                         c = 0;
391                                         break;
392                                 }
393                                 if (c)
394                                         printf("%c ", c);
395                         }
396                         printf("\n");
397                         g_variant_unref(gvar_list);
398
399                 } else if (key == SR_CONF_LIMIT_SAMPLES) {
400                         /* If implemented in config_list(), this denotes the
401                          * maximum number of samples a device can send. This
402                          * really applies only to logic analyzers, and then
403                          * only to those that don't support compression, or
404                          * have it turned off by default. The values returned
405                          * are the low/high limits. */
406                         if (sr_config_list(driver, sdi, channel_group, key,
407                                         &gvar) != SR_OK) {
408                                 continue;
409                         }
410                         g_variant_get(gvar, "(tt)", &low, &high);
411                         g_variant_unref(gvar);
412                         printf("    Maximum number of samples: %"PRIu64"\n", high);
413
414                 } else if (key == SR_CONF_SAMPLERATE) {
415                         /* Supported samplerates */
416                         printf("    %s", srci->id);
417                         if (sr_config_list(driver, sdi, channel_group, SR_CONF_SAMPLERATE,
418                                         &gvar_dict) != SR_OK) {
419                                 printf("\n");
420                                 continue;
421                         }
422                         if ((gvar_list = g_variant_lookup_value(gvar_dict,
423                                         "samplerates", G_VARIANT_TYPE("at")))) {
424                                 uint64 = g_variant_get_fixed_array(gvar_list,
425                                                 &num_elements, sizeof(uint64_t));
426                                 printf(" - supported samplerates:\n");
427                                 for (i = 0; i < num_elements; i++) {
428                                         if (!(s = sr_samplerate_string(uint64[i])))
429                                                 continue;
430                                         printf("      %s\n", s);
431                                         g_free(s);
432                                 }
433                                 g_variant_unref(gvar_list);
434                         } else if ((gvar_list = g_variant_lookup_value(gvar_dict,
435                                         "samplerate-steps", G_VARIANT_TYPE("at")))) {
436                                 uint64 = g_variant_get_fixed_array(gvar_list,
437                                                 &num_elements, sizeof(uint64_t));
438                                 /* low */
439                                 if (!(s = sr_samplerate_string(uint64[0])))
440                                         continue;
441                                 printf(" (%s", s);
442                                 g_free(s);
443                                 /* high */
444                                 if (!(s = sr_samplerate_string(uint64[1])))
445                                         continue;
446                                 printf(" - %s", s);
447                                 g_free(s);
448                                 /* step */
449                                 if (!(s = sr_samplerate_string(uint64[2])))
450                                         continue;
451                                 printf(" in steps of %s)\n", s);
452                                 g_free(s);
453                                 g_variant_unref(gvar_list);
454                         }
455                         g_variant_unref(gvar_dict);
456
457                 } else if (srci->datatype == SR_T_UINT64) {
458                         printf("    %s", srci->id);
459                         gvar = NULL;
460                         if (sr_config_get(driver, sdi, channel_group, key,
461                                         &gvar) == SR_OK) {
462                                 tmp_uint64 = g_variant_get_uint64(gvar);
463                                 g_variant_unref(gvar);
464                         } else
465                                 tmp_uint64 = 0;
466                         if (sr_config_list(driver, sdi, channel_group,
467                                         SR_CONF_BUFFERSIZE, &gvar_list) != SR_OK) {
468                                 if (gvar) {
469                                         /* Can't list it, but we have a value to show. */
470                                         printf("%"PRIu64" (current)", tmp_uint64);
471                                 }
472                                 printf("\n");
473                                 continue;
474                         }
475                         uint64 = g_variant_get_fixed_array(gvar_list,
476                                         &num_elements, sizeof(uint64_t));
477                         printf(" - supported values:\n");
478                         for (i = 0; i < num_elements; i++) {
479                                 printf("      %"PRIu64, uint64[i]);
480                                 if (gvar && tmp_uint64 == uint64[i])
481                                         printf(" (current)");
482                                 printf("\n");
483                         }
484                         g_variant_unref(gvar_list);
485
486                 } else if (srci->datatype == SR_T_STRING) {
487                         printf("    %s: ", srci->id);
488                         if (sr_config_get(driver, sdi, channel_group, key,
489                                         &gvar) == SR_OK) {
490                                 tmp_str = g_strdup(g_variant_get_string(gvar, NULL));
491                                 g_variant_unref(gvar);
492                         } else
493                                 tmp_str = NULL;
494
495                         if (sr_config_list(driver, sdi, channel_group, key,
496                                         &gvar) != SR_OK) {
497                                 if (tmp_str) {
498                                         /* Can't list it, but we have a value to show. */
499                                         printf("%s (current)", tmp_str);
500                                 }
501                                 printf("\n");
502                                 g_free(tmp_str);
503                                 continue;
504                         }
505
506                         stropts = g_variant_get_strv(gvar, &num_elements);
507                         for (i = 0; i < num_elements; i++) {
508                                 if (i)
509                                         printf(", ");
510                                 printf("%s", stropts[i]);
511                                 if (tmp_str && !strcmp(tmp_str, stropts[i]))
512                                         printf(" (current)");
513                         }
514                         printf("\n");
515                         g_free(stropts);
516                         g_free(tmp_str);
517                         g_variant_unref(gvar);
518
519                 } else if (srci->datatype == SR_T_UINT64_RANGE) {
520                         printf("    %s: ", srci->id);
521                         if (sr_config_list(driver, sdi, channel_group, key,
522                                         &gvar_list) != SR_OK) {
523                                 printf("\n");
524                                 continue;
525                         }
526
527                         if (sr_config_get(driver, sdi, channel_group, key, &gvar) == SR_OK) {
528                                 g_variant_get(gvar, "(tt)", &cur_low, &cur_high);
529                                 g_variant_unref(gvar);
530                         } else {
531                                 cur_low = 0;
532                                 cur_high = 0;
533                         }
534
535                         num_elements = g_variant_n_children(gvar_list);
536                         for (i = 0; i < num_elements; i++) {
537                                 gvar = g_variant_get_child_value(gvar_list, i);
538                                 g_variant_get(gvar, "(tt)", &low, &high);
539                                 g_variant_unref(gvar);
540                                 if (i)
541                                         printf(", ");
542                                 printf("%"PRIu64"-%"PRIu64, low, high);
543                                 if (low == cur_low && high == cur_high)
544                                         printf(" (current)");
545                         }
546                         printf("\n");
547                         g_variant_unref(gvar_list);
548
549                 } else if (srci->datatype == SR_T_BOOL) {
550                         printf("    %s: ", srci->id);
551                         if (sr_config_get(driver, sdi, channel_group, key,
552                                         &gvar) == SR_OK) {
553                                 if (g_variant_get_boolean(gvar))
554                                         printf("on (current), off\n");
555                                 else
556                                         printf("on, off (current)\n");
557                                 g_variant_unref(gvar);
558                         } else
559                                 printf("on, off\n");
560
561                 } else if (srci->datatype == SR_T_DOUBLE_RANGE) {
562                         printf("    %s: ", srci->id);
563                         if (sr_config_list(driver, sdi, channel_group, key,
564                                         &gvar_list) != SR_OK) {
565                                 printf("\n");
566                                 continue;
567                         }
568
569                         if (sr_config_get(driver, sdi, channel_group, key, &gvar) == SR_OK) {
570                                 g_variant_get(gvar, "(dd)", &dcur_low, &dcur_high);
571                                 g_variant_unref(gvar);
572                         } else {
573                                 dcur_low = 0;
574                                 dcur_high = 0;
575                         }
576
577                         num_elements = g_variant_n_children(gvar_list);
578                         for (i = 0; i < num_elements; i++) {
579                                 gvar = g_variant_get_child_value(gvar_list, i);
580                                 g_variant_get(gvar, "(dd)", &dlow, &dhigh);
581                                 g_variant_unref(gvar);
582                                 if (i)
583                                         printf(", ");
584                                 printf("%.1f-%.1f", dlow, dhigh);
585                                 if (dlow == dcur_low && dhigh == dcur_high)
586                                         printf(" (current)");
587                         }
588                         printf("\n");
589                         g_variant_unref(gvar_list);
590
591                 } else if (srci->datatype == SR_T_FLOAT) {
592                         printf("    %s: ", srci->id);
593                         if (sr_config_get(driver, sdi, channel_group, key,
594                                         &gvar) == SR_OK) {
595                                 printf("%f\n", g_variant_get_double(gvar));
596                                 g_variant_unref(gvar);
597                         } else
598                                 printf("\n");
599
600                 } else if (srci->datatype == SR_T_RATIONAL_PERIOD
601                                 || srci->datatype == SR_T_RATIONAL_VOLT) {
602                         printf("    %s", srci->id);
603                         if (sr_config_get(driver, sdi, channel_group, key,
604                                         &gvar) == SR_OK) {
605                                 g_variant_get(gvar, "(tt)", &cur_p, &cur_q);
606                                 g_variant_unref(gvar);
607                         } else
608                                 cur_p = cur_q = 0;
609
610                         if (sr_config_list(driver, sdi, channel_group,
611                                         key, &gvar_list) != SR_OK) {
612                                 printf("\n");
613                                 continue;
614                         }
615                         printf(" - supported values:\n");
616                         num_elements = g_variant_n_children(gvar_list);
617                         for (i = 0; i < num_elements; i++) {
618                                 gvar = g_variant_get_child_value(gvar_list, i);
619                                 g_variant_get(gvar, "(tt)", &p, &q);
620                                 if (srci->datatype == SR_T_RATIONAL_PERIOD)
621                                         s = sr_period_string(p * q);
622                                 else
623                                         s = sr_voltage_string(p, q);
624                                 printf("      %s", s);
625                                 g_free(s);
626                                 if (p == cur_p && q == cur_q)
627                                         printf(" (current)");
628                                 printf("\n");
629                         }
630                         g_variant_unref(gvar_list);
631
632                 } else {
633
634                         /* Everything else */
635                         printf("    %s\n", srci->id);
636                 }
637         }
638         g_variant_unref(gvar_opts);
639
640         sr_dev_close(sdi);
641
642 }
643
644 #ifdef HAVE_SRD
645 void show_pd_detail(void)
646 {
647         struct srd_decoder *dec;
648         struct srd_decoder_option *o;
649         struct srd_channel *pdch;
650         struct srd_decoder_annotation_row *r;
651         GSList *l, *ll, *ol;
652         int idx;
653         char **pdtokens, **pdtok, *optsep, **ann, *val, *doc;
654
655         pdtokens = g_strsplit(opt_pds, ",", -1);
656         for (pdtok = pdtokens; *pdtok; pdtok++) {
657                 /* Strip options. */
658                 if ((optsep = strchr(*pdtok, ':')))
659                         *optsep = '\0';
660                 if (!(dec = srd_decoder_get_by_id(*pdtok))) {
661                         g_critical("Protocol decoder %s not found.", *pdtok);
662                         return;
663                 }
664                 printf("ID: %s\nName: %s\nLong name: %s\nDescription: %s\n",
665                                 dec->id, dec->name, dec->longname, dec->desc);
666                 printf("License: %s\n", dec->license);
667                 printf("Annotation classes:\n");
668                 if (dec->annotations) {
669                         for (l = dec->annotations; l; l = l->next) {
670                                 ann = l->data;
671                                 printf("- %s: %s\n", ann[0], ann[1]);
672                         }
673                 } else {
674                         printf("None.\n");
675                 }
676                 printf("Annotation rows:\n");
677                 if (dec->annotation_rows) {
678                         for (l = dec->annotation_rows; l; l = l->next) {
679                                 r = l->data;
680                                 printf("- %s (%s): ", r->id, r->desc);
681                                 for (ll = r->ann_classes; ll; ll = ll->next) {
682                                         idx = GPOINTER_TO_INT(ll->data);
683                                         ann = g_slist_nth_data(dec->annotations, idx);
684                                         printf("%s", ann[0]);
685                                         if (ll->next)
686                                                 printf(", ");
687                                 }
688                                 printf("\n");
689                         }
690                 } else {
691                         printf("None.\n");
692                 }
693                 printf("Required channels:\n");
694                 if (dec->channels) {
695                         for (l = dec->channels; l; l = l->next) {
696                                 pdch = l->data;
697                                 printf("- %s (%s): %s\n",
698                                        pdch->id, pdch->name, pdch->desc);
699                         }
700                 } else {
701                         printf("None.\n");
702                 }
703                 printf("Optional channels:\n");
704                 if (dec->opt_channels) {
705                         for (l = dec->opt_channels; l; l = l->next) {
706                                 pdch = l->data;
707                                 printf("- %s (%s): %s\n",
708                                        pdch->id, pdch->name, pdch->desc);
709                         }
710                 } else {
711                         printf("None.\n");
712                 }
713                 printf("Options:\n");
714                 if (dec->options) {
715                         for (l = dec->options; l; l = l->next) {
716                                 o = l->data;
717                                 printf("- %s: %s (", o->id, o->desc);
718                                 for (ol = o->values; ol; ol = ol->next) {
719                                         val = g_variant_print(ol->data, FALSE);
720                                         printf("%s, ", val);
721                                         g_free(val);
722                                 }
723                                 val = g_variant_print(o->def, FALSE);
724                                 printf("default %s)\n", val);
725                                 g_free(val);
726                         }
727                 } else {
728                         printf("None.\n");
729                 }
730                 if ((doc = srd_decoder_doc_get(dec))) {
731                         printf("Documentation:\n%s\n",
732                                doc[0] == '\n' ? doc + 1 : doc);
733                         g_free(doc);
734                 }
735         }
736
737         g_strfreev(pdtokens);
738 }
739 #endif
740
741 void show_input(void)
742 {
743         const struct sr_input_module *imod;
744         const struct sr_option **opts;
745         GSList *l;
746         int i;
747         char *s, **tok;
748
749         tok = g_strsplit(opt_input_format, ":", 0);
750         if (!tok[0] || !(imod = sr_input_find(tok[0])))
751                 g_critical("Input module '%s' not found.", opt_input_format);
752
753         printf("ID: %s\nName: %s\n", sr_input_id_get(imod),
754                         sr_input_name_get(imod));
755         printf("Description: %s\n", sr_input_description_get(imod));
756         if ((opts = sr_input_options_get(imod))) {
757                 printf("Options:\n");
758                 for (i = 0; opts[i]; i++) {
759                         printf("  %s: %s", opts[i]->id, opts[i]->desc);
760                         if (opts[i]->def) {
761                                 s = g_variant_print(opts[i]->def, FALSE);
762                                 printf(" (default %s", s);
763                                 g_free(s);
764                                 if (opts[i]->values) {
765                                         printf(", possible values ");
766                                         for (l = opts[i]->values; l; l = l->next) {
767                                                 s = g_variant_print((GVariant *)l->data, FALSE);
768                                                 printf("%s%s", s, l->next ? ", " : "");
769                                                 g_free(s);
770                                         }
771                                 }
772                                 printf(")");
773                         }
774                         printf("\n");
775                 }
776                 sr_input_options_free(opts);
777         }
778         g_strfreev(tok);
779 }
780
781 void show_output(void)
782 {
783         const struct sr_output_module *omod;
784         const struct sr_option **opts;
785         GSList *l;
786         int i;
787         char *s, **tok;
788
789         tok = g_strsplit(opt_output_format, ":", 0);
790         if (!tok[0] || !(omod = sr_output_find(tok[0])))
791                 g_critical("Output module '%s' not found.", opt_output_format);
792
793         printf("ID: %s\nName: %s\n", sr_output_id_get(omod),
794                         sr_output_name_get(omod));
795         printf("Description: %s\n", sr_output_description_get(omod));
796         if ((opts = sr_output_options_get(omod))) {
797                 printf("Options:\n");
798                 for (i = 0; opts[i]; i++) {
799                         printf("  %s: %s", opts[i]->id, opts[i]->desc);
800                         if (opts[i]->def) {
801                                 s = g_variant_print(opts[i]->def, FALSE);
802                                 printf(" (default %s", s);
803                                 g_free(s);
804                                 if (opts[i]->values) {
805                                         printf(", possible values ");
806                                         for (l = opts[i]->values; l; l = l->next) {
807                                                 s = g_variant_print((GVariant *)l->data, FALSE);
808                                                 printf("%s%s", s, l->next ? ", " : "");
809                                                 g_free(s);
810                                         }
811                                 }
812                                 printf(")");
813                         }
814                         printf("\n");
815                 }
816                 sr_output_options_free(opts);
817         }
818         g_strfreev(tok);
819 }
820