swconfig: backport compatibility fix from r27820
[openwrt-10.03/.git] / package / swconfig / src / uci.c
index 2df837d2787865b01ae5ff2d58f439a99d67e852..1de689ce6e3e303641a36a273064ccf5257044be 100644 (file)
@@ -46,13 +46,19 @@ struct swlib_setting {
 };
 
 struct swlib_setting early_settings[] = {
-       { .name = "reset" },
-       { .name = "enable_vlan" },
+       { .name = "reset", .val = "1" },
+       { .name = "enable_vlan", .val = "1" },
 };
 
 static struct swlib_setting *settings;
 static struct swlib_setting **head;
 
+static bool swlib_match_name(struct switch_dev *dev, const char *name)
+{
+       return (strcmp(name, dev->dev_name) == 0 ||
+               strcmp(name, dev->alias) == 0);
+}
+
 static int
 swlib_map_settings(struct switch_dev *dev, int type, int port_vlan, struct uci_section *s)
 {
@@ -107,6 +113,7 @@ int swlib_apply_from_uci(struct switch_dev *dev, struct uci_package *p)
        struct uci_element *e;
        struct uci_section *s;
        struct uci_option *o;
+       struct uci_ptr ptr;
        struct switch_val val;
        int i;
 
@@ -114,12 +121,29 @@ int swlib_apply_from_uci(struct switch_dev *dev, struct uci_package *p)
        head = &settings;
 
        uci_foreach_element(&p->sections, e) {
+               struct uci_element *n;
+
                s = uci_to_section(e);
 
                if (strcmp(s->type, "switch") != 0)
                        continue;
 
-               if (strcmp(e->name, dev->dev_name) != 0)
+               uci_foreach_element(&s->options, n) {
+                       struct uci_option *o = uci_to_option(n);
+
+                       if (strcmp(n->name, "name") != 0)
+                               continue;
+
+                       if (o->type != UCI_TYPE_STRING)
+                               continue;
+
+                       if (swlib_match_name(dev, o->v.string))
+                               goto found;
+
+                       break;
+               }
+
+               if (!swlib_match_name(dev, e->name))
                        continue;
 
                goto found;
@@ -153,7 +177,7 @@ found:
 
                                if (!strcmp(os->name, "device")) {
                                        devn = o->v.string;
-                                       if (strcmp(devn, dev->dev_name) != 0)
+                                       if (!swlib_match_name(dev, devn))
                                                devn = NULL;
                                } else if (!strcmp(os->name, "port")) {
                                        port = o->v.string;
@@ -178,7 +202,7 @@ found:
 
                                if (!strcmp(os->name, "device")) {
                                        devn = o->v.string;
-                                       if (strcmp(devn, dev->dev_name) != 0)
+                                       if (!swlib_match_name(dev, devn))
                                                devn = NULL;
                                } else if (!strcmp(os->name, "vlan")) {
                                        vlan = o->v.string;