valgrind: Clear more unfreed memory issues
[sigrok-cli.git] / parsers.c
index ad5f37925f7d0e156e9158e0865f813c2de6d70d..5ec655f78ad3dce8a05f01186a2a7ec61febb437 100644 (file)
--- a/parsers.c
+++ b/parsers.c
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#include "sigrok-cli.h"
+#include <config.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdint.h>
 #include <string.h>
 #include <glib.h>
-
-extern struct sr_context *sr_ctx;
+#include "sigrok-cli.h"
 
 struct sr_channel *find_channel(GSList *channellist, const char *channelname)
 {
@@ -45,13 +44,15 @@ struct sr_channel *find_channel(GSList *channellist, const char *channelname)
 GSList *parse_channelstring(struct sr_dev_inst *sdi, const char *channelstring)
 {
        struct sr_channel *ch;
-       GSList *channellist;
+       GSList *channellist, *channels;
        int ret, n, b, e, i;
        char **tokens, **range, **names, *eptr, str[8];
 
+       channels = sr_dev_inst_channels_get(sdi);
+
        if (!channelstring || !channelstring[0])
                /* Use all channels by default. */
-               return g_slist_copy(sdi->channels);
+               return g_slist_copy(channels);
 
        ret = SR_OK;
        range = NULL;
@@ -65,10 +66,12 @@ GSList *parse_channelstring(struct sr_dev_inst *sdi, const char *channelstring)
                        break;
                }
                if (strchr(tokens[i], '-')) {
-                       /* A range of channels in the form a-b. This will only work
+                       /*
+                        * A range of channels in the form a-b. This will only work
                         * if the channels are named as numbers -- so every channel
                         * in the range must exist as a channel name string in the
-                        * device. */
+                        * device.
+                        */
                        range = g_strsplit(tokens[i], "-", 2);
                        if (!range[0] || !range[1] || range[2]) {
                                /* Need exactly two arguments. */
@@ -102,7 +105,7 @@ GSList *parse_channelstring(struct sr_dev_inst *sdi, const char *channelstring)
                                        ret = SR_ERR;
                                        break;
                                }
-                               ch = find_channel(sdi->channels, str);
+                               ch = find_channel(channels, str);
                                if (!ch) {
                                        g_critical("unknown channel '%d'.", b);
                                        ret = SR_ERR;
@@ -127,7 +130,7 @@ range_fail:
                                break;
                        }
 
-                       ch = find_channel(sdi->channels, names[0]);
+                       ch = find_channel(channels, names[0]);
                        if (!ch) {
                                g_critical("unknown channel '%s'.", names[0]);
                                g_strfreev(names);
@@ -185,7 +188,7 @@ int parse_triggerstring(const struct sr_dev_inst *sdi, const char *s,
        struct sr_channel *ch;
        struct sr_trigger_stage *stage;
        GVariant *gvar;
-       GSList *l;
+       GSList *l, *channels;
        gsize num_matches;
        gboolean found_match, error;
        const int32_t *matches;
@@ -193,8 +196,12 @@ int parse_triggerstring(const struct sr_dev_inst *sdi, const char *s,
        unsigned int j;
        int t, i;
        char **tokens, *sep;
+       struct sr_dev_driver *driver;
 
-       if (sr_config_list(sdi->driver, sdi, NULL, SR_CONF_TRIGGER_MATCH,
+       driver = sr_dev_inst_driver_get(sdi);
+       channels = sr_dev_inst_channels_get(sdi);
+
+       if (maybe_config_list(driver, sdi, NULL, SR_CONF_TRIGGER_MATCH,
                        &gvar) != SR_OK) {
                g_critical("Device doesn't support any triggers.");
                return FALSE;
@@ -212,7 +219,7 @@ int parse_triggerstring(const struct sr_dev_inst *sdi, const char *s,
                }
                *sep++ = 0;
                ch = NULL;
-               for (l = sdi->channels; l; l = l->next) {
+               for (l = channels; l; l = l->next) {
                        ch = l->data;
                        if (ch->enabled && !strcmp(ch->name, tokens[i]))
                                break;
@@ -402,12 +409,15 @@ int parse_driver(char *arg, struct sr_dev_driver **driver, GSList **drvopts)
        int i;
        char *drvname;
 
+       if (!arg)
+               return FALSE;
+
        drvargs = parse_generic_arg(arg, TRUE);
 
        drvname = g_strdup(g_hash_table_lookup(drvargs, "sigrok_key"));
        g_hash_table_remove(drvargs, "sigrok_key");
        *driver = NULL;
-       drivers = sr_driver_list();
+       drivers = sr_driver_list(sr_ctx);
        for (i = 0; drivers[i]; i++) {
                if (strcmp(drivers[i]->name, drvname))
                        continue;
@@ -441,4 +451,3 @@ int parse_driver(char *arg, struct sr_dev_driver **driver, GSList **drvopts)
 
        return TRUE;
 }
-