cli: stop immediately if SRD PD registration fails
[sigrok-cli.git] / sigrok-cli.c
index f13289e56d38ade95f937b6b32df23d974e39628..359c34b8fd54877dd43f6583cf867b2effb63e7a 100644 (file)
@@ -63,22 +63,38 @@ static gchar *opt_samples = NULL;
 static gchar *opt_continuous = NULL;
 
 static GOptionEntry optargs[] = {
-       {"version", 'V', 0, G_OPTION_ARG_NONE, &opt_version, "Show version and support list", NULL},
-       {"loglevel", 'l', 0, G_OPTION_ARG_INT, &opt_loglevel, "Select libsigrok/libsigrokdecode loglevel", NULL},
-       {"list-devices", 'D', 0, G_OPTION_ARG_NONE, &opt_list_devs, "Scan for devices", NULL},
-       {"device", 'd', 0, G_OPTION_ARG_STRING, &opt_dev, "Use specified device", NULL},
-       {"input-file", 'i', 0, G_OPTION_ARG_FILENAME, &opt_input_file, "Load input from file", NULL},
-       {"input-format", 'I', 0, G_OPTION_ARG_STRING, &opt_input_format, "Input format", NULL},
-       {"output-file", 'o', 0, G_OPTION_ARG_FILENAME, &opt_output_file, "Save output to file", NULL},
-       {"output-format", 'O', 0, G_OPTION_ARG_STRING, &opt_output_format, "Output format", NULL},
-       {"probes", 'p', 0, G_OPTION_ARG_STRING, &opt_probes, "Probes to use", NULL},
-       {"triggers", 't', 0, G_OPTION_ARG_STRING, &opt_triggers, "Trigger configuration", NULL},
-       {"wait-trigger", 'w', 0, G_OPTION_ARG_NONE, &opt_wait_trigger, "Wait for trigger", NULL},
-       {"protocol-decoders", 'a', 0, G_OPTION_ARG_STRING, &opt_pds, "Protocol decoders to run", NULL},
-       {"protocol-decoder-stack", 's', 0, G_OPTION_ARG_STRING, &opt_pd_stack, "Protocol decoder stack", NULL},
-       {"time", 0, 0, G_OPTION_ARG_STRING, &opt_time, "How long to sample (ms)", NULL},
-       {"samples", 0, 0, G_OPTION_ARG_STRING, &opt_samples, "Number of samples to acquire", NULL},
-       {"continuous", 0, 0, G_OPTION_ARG_NONE, &opt_continuous, "Sample continuously", NULL},
+       {"version", 'V', 0, G_OPTION_ARG_NONE, &opt_version,
+                       "Show version and support list", NULL},
+       {"loglevel", 'l', 0, G_OPTION_ARG_INT, &opt_loglevel,
+                       "Select libsigrok/libsigrokdecode loglevel", NULL},
+       {"list-devices", 'D', 0, G_OPTION_ARG_NONE, &opt_list_devs,
+                       "Scan for devices", NULL},
+       {"device", 'd', 0, G_OPTION_ARG_STRING, &opt_dev,
+                       "Use specified device", NULL},
+       {"input-file", 'i', 0, G_OPTION_ARG_FILENAME, &opt_input_file,
+                       "Load input from file", NULL},
+       {"input-format", 'I', 0, G_OPTION_ARG_STRING, &opt_input_format,
+                       "Input format", NULL},
+       {"output-file", 'o', 0, G_OPTION_ARG_FILENAME, &opt_output_file,
+                       "Save output to file", NULL},
+       {"output-format", 'O', 0, G_OPTION_ARG_STRING, &opt_output_format,
+                       "Output format", NULL},
+       {"probes", 'p', 0, G_OPTION_ARG_STRING, &opt_probes,
+                       "Probes to use", NULL},
+       {"triggers", 't', 0, G_OPTION_ARG_STRING, &opt_triggers,
+                       "Trigger configuration", NULL},
+       {"wait-trigger", 'w', 0, G_OPTION_ARG_NONE, &opt_wait_trigger,
+                       "Wait for trigger", NULL},
+       {"protocol-decoders", 'a', 0, G_OPTION_ARG_STRING, &opt_pds,
+                       "Protocol decoders to run", NULL},
+       {"protocol-decoder-stack", 's', 0, G_OPTION_ARG_STRING, &opt_pd_stack,
+                       "Protocol decoder stack", NULL},
+       {"time", 0, 0, G_OPTION_ARG_STRING, &opt_time,
+                       "How long to sample (ms)", NULL},
+       {"samples", 0, 0, G_OPTION_ARG_STRING, &opt_samples,
+                       "Number of samples to acquire", NULL},
+       {"continuous", 0, 0, G_OPTION_ARG_NONE, &opt_continuous,
+                       "Sample continuously", NULL},
        {NULL, 0, 0, 0, NULL, NULL, NULL}
 };
 
@@ -273,7 +289,7 @@ static void show_pd_detail(void)
        pdtokens = g_strsplit(opt_pds, ",", -1);
        for (pdtok = pdtokens; *pdtok; pdtok++) {
                if (!(dec = srd_decoder_get_by_id(*pdtok))) {
-                       printf("Protocol decoder %s not found.", *pdtok);
+                       printf("Protocol decoder %s not found.\n", *pdtok);
                        return;
                }
                printf("ID: %s\nName: %s\nLong name: %s\nDescription: %s\n",
@@ -385,9 +401,11 @@ static void datafeed_in(struct sr_dev *dev, struct sr_datafeed_packet *packet)
                                outfile = g_fopen(opt_output_file, "wb");
                        }
                }
-               if (opt_pds)
-                       srd_session_start(num_enabled_probes, unitsize,
-                                       header->samplerate);
+               if (opt_pds) {
+                       if (srd_session_start(num_enabled_probes, unitsize,
+                                       header->samplerate) != SRD_OK)
+                               sr_session_halt();
+               }
                break;
        case SR_DF_END:
                g_debug("cli: Received SR_DF_END");
@@ -495,6 +513,7 @@ static int register_pds(struct sr_dev *dev, const char *pdstring)
 {
        GHashTable *pd_opthash;
        struct srd_decoder_inst *di;
+       int ret;
        char **pdtokens, **pdtok, *pd_name;
 
        /* Avoid compiler warnings. */
@@ -503,6 +522,7 @@ static int register_pds(struct sr_dev *dev, const char *pdstring)
        g_datalist_init(&pd_ann_visible);
        pdtokens = g_strsplit(pdstring, ",", -1);
        pd_opthash = NULL;
+       ret = 0;
        pd_name = NULL;
 
        for (pdtok = pdtokens; *pdtok; pdtok++) {
@@ -515,10 +535,12 @@ static int register_pds(struct sr_dev *dev, const char *pdstring)
                g_hash_table_remove(pd_opthash, "sigrok_key");
                if (srd_decoder_load(pd_name) != SRD_OK) {
                        g_critical("Failed to load protocol decoder %s.", pd_name);
+                       ret = 1;
                        goto err_out;
                }
                if (!(di = srd_inst_new(pd_name, pd_opthash))) {
                        g_critical("Failed to instantiate protocol decoder %s.", pd_name);
+                       ret = 1;
                        goto err_out;
                }
                g_datalist_set_data(&pd_ann_visible, di->inst_id, pd_name);
@@ -527,8 +549,10 @@ static int register_pds(struct sr_dev *dev, const char *pdstring)
                 * is the probe name as specified in the decoder class, and the
                 * value is the probe number i.e. the order in which the PD's
                 * incoming samples are arranged. */
-               if (srd_inst_probe_set_all(di, pd_opthash) != SRD_OK)
+               if (srd_inst_probe_set_all(di, pd_opthash) != SRD_OK) {
+                       ret = 1;
                        goto err_out;
+               }
                g_hash_table_destroy(pd_opthash);
                pd_opthash = NULL;
        }
@@ -540,7 +564,7 @@ err_out:
        if (pd_name)
                g_free(pd_name);
 
-       return 0;
+       return ret;
 }
 
 void show_pd_annotation(struct srd_proto_data *pdata, void *cb_data)
@@ -974,7 +998,7 @@ static void logger(const gchar *log_domain, GLogLevelFlags log_level,
         * All messages, warnings, errors etc. go to stderr (not stdout) in
         * order to not mess up the CLI tool data output, e.g. VCD output.
         */
-       if (log_level & (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_WARNING)
+       if (log_level & (G_LOG_LEVEL_ERROR | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING)
                        || opt_loglevel > SR_LOG_WARN) {
                fprintf(stderr, "%s\n", message);
                fflush(stderr);