netifd: packet_steering: fix shell error on unset steering_flows option
[openwrt/openwrt.git] / package / network / config / swconfig / src / cli.c
index 90554143e1a25464f3d4b0cc584d94c0505ccfac..2601f346db1301e39b27152923e6bcacf0b50c28 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * swconfig.c: Switch configuration utility
  *
- * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
+ * Copyright (C) 2008 Felix Fietkau <nbd@nbd.name>
  * Copyright (C) 2010 Martin Mares <mj@ucw.cz>
  *
  * This program is free software; you can redistribute it and/or
@@ -101,6 +101,24 @@ speed_str(int speed)
        return "unknown";
 }
 
+static void
+free_attr_val(const struct switch_attr *attr, const struct switch_val *val)
+{
+       switch (attr->type) {
+       case SWITCH_TYPE_STRING:
+               free(val->value.s);
+               break;
+       case SWITCH_TYPE_PORTS:
+               free(val->value.ports);
+               break;
+       case SWITCH_TYPE_LINK:
+               free(val->value.link);
+               break;
+       default:
+               break;
+       }
+}
+
 static void
 print_attr_val(const struct switch_attr *attr, const struct switch_val *val)
 {
@@ -150,8 +168,10 @@ show_attrs(struct switch_dev *dev, struct switch_attr *attr, struct switch_val *
                        printf("\t%s: ", attr->name);
                        if (swlib_get_attr(dev, attr, val) < 0)
                                printf("???");
-                       else
+                       else {
                                print_attr_val(attr, val);
+                               free_attr_val(attr, val);
+                       }
                        putchar('\n');
                }
                attr = attr->next;
@@ -335,10 +355,10 @@ int main(int argc, char **argv)
                if(cvlan > -1)
                        cport = cvlan;
 
-               if(swlib_set_attr_string(dev, a, cport, cvalue) < 0)
+               retval = swlib_set_attr_string(dev, a, cport, cvalue);
+               if (retval < 0)
                {
-                       fprintf(stderr, "failed\n");
-                       retval = -1;
+                       nl_perror(-retval, "Failed to set attribute");
                        goto out;
                }
                break;
@@ -347,13 +367,14 @@ int main(int argc, char **argv)
                        val.port_vlan = cvlan;
                if(cport > -1)
                        val.port_vlan = cport;
-               if(swlib_get_attr(dev, a, &val) < 0)
+               retval = swlib_get_attr(dev, a, &val);
+               if (retval < 0)
                {
-                       fprintf(stderr, "failed\n");
-                       retval = -1;
+                       nl_perror(-retval, "Failed to get attribute");
                        goto out;
                }
                print_attr_val(a, &val);
+               free_attr_val(a, &val);
                putchar('\n');
                break;
        case CMD_LOAD: