[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

[PATCH] netcfg: use Choices-C for interface choices



The attached patch makes netcfg use Choices-C for interface choices
rather than the current weird hack whereby you normally have to preseed
the full English description of the interface but netcfg will let you
get away with just preseeding the interface name.

Does anyone care if preseeding the full interface description stops
being supported? That is, you would no longer be able to say:

  d-i netcfg/choose_interface select eth0: Ethernet or FireWire (IEEE 1394)

... but would always just use (as previously supported but not
mandatory):

  d-i netcfg/choose_interface select eth0

I figure that the full-description syntax was liable to change anyway as
it was constructed from the description of a debconf template.

This patch is untested for now, although it does at least compile.

Cheers,

-- 
Colin Watson                                       [cjwatson@debian.org]
Index: debian/netcfg-common.templates
===================================================================
--- debian/netcfg-common.templates	(revision 47536)
+++ debian/netcfg-common.templates	(working copy)
@@ -28,6 +28,7 @@ _Description: Name server addresses:
 
 Template: netcfg/choose_interface
 Type: select
+Choices-C: ${ifchoices-c}
 Choices: ${ifchoices}
 _Description: Primary network interface:
  Your system has multiple network interfaces. Choose the one to use as
Index: netcfg-common.c
===================================================================
--- netcfg-common.c	(revision 47536)
+++ netcfg-common.c	(working copy)
@@ -415,10 +415,10 @@ int netcfg_get_interface(struct debconfc
                          int *numif, char* defif)
 {
     char *inter = NULL, **ifs;
-    size_t len;
+    size_t len_c, len;
     int ret, i, asked;
     int num_interfaces = 0;
-    char *ptr = NULL;
+    char *ptr_c = NULL, *ptr = NULL;
     char *ifdsc = NULL;
     char *old_selection = NULL;
     
@@ -430,7 +430,9 @@ int netcfg_get_interface(struct debconfc
     if (!(ptr = malloc(128)))
         goto error;
     
+    len_c = 128;
     len = 128;
+    *ptr_c = '\0';
     *ptr = '\0';
     
     num_interfaces = get_all_ifs(1, &ifs);
@@ -445,28 +447,35 @@ int netcfg_get_interface(struct debconfc
     old_selection = strdup(client->value);
     
     for (i = 0; i < num_interfaces; i++) {
-        size_t newchars;
+        size_t newchars_c, newchars;
         char *temp = NULL;
         
         inter = ifs[i];
         
         interface_down(inter);
         ifdsc = get_ifdsc(client, inter);
+        newchars_c = strlen(inter) + 3; /* ", " + NUL */
         newchars = strlen(inter) + strlen(ifdsc) + 5; /* ": , " + NUL */
+        if (len_c < (strlen(ptr_c) + newchars_c)) {
+            if (!(ptr_c = realloc(ptr_c, len_c + newchars_c + 128)))
+                goto error;
+            len_c += newchars_c + 128;
+        }
         if (len < (strlen(ptr) + newchars)) {
             if (!(ptr = realloc(ptr, len + newchars + 128)))
                 goto error;
             len += newchars + 128;
         }
         
+        if (num_interfaces > 1 && strcmp(defif, inter) == 0)
+            debconf_set(client, "netcfg/choose_interface", inter);
+
+        di_snprintfcat(ptr_c, len_c, "%s, ", inter);
+
         temp = malloc(newchars);
         
         snprintf(temp, newchars, "%s: %s", inter, ifdsc);
         
-        if (num_interfaces > 1 &&
-            ((strcmp(defif, inter) == 0) || (strcmp(defif, temp) == 0)))
-            debconf_set(client, "netcfg/choose_interface", temp);
-        
         di_snprintfcat(ptr, len, "%s, ", temp);
         
         free(temp);
@@ -476,23 +485,29 @@ int netcfg_get_interface(struct debconfc
     if (num_interfaces == 0) {
         debconf_input(client, "high", "netcfg/no_interfaces");
         ret = debconf_go(client);
+        free(ptr_c);
         free(ptr);
         free(old_selection);
         *numif = 0;
         return ret;
     }
     else if (num_interfaces == 1) {
-        inter = ptr;
+        inter = ptr_c;
         *numif = 1;
         free(old_selection);
     }
     else if (num_interfaces > 1) {
         *numif = num_interfaces;
         /* remove the trailing ", ", which confuses cdebconf */
+        ptr_c[strlen(ptr_c) - 2] = '\0';
         ptr[strlen(ptr) - 2] = '\0';
         
+        debconf_subst(client, "netcfg/choose_interface", "ifchoices-c", ptr_c);
         debconf_subst(client, "netcfg/choose_interface", "ifchoices", ptr);
+        free(ptr_c);
         free(ptr);
+        ptr_c = NULL;
+        ptr = NULL;
         
         asked = (debconf_input(client, "critical", "netcfg/choose_interface") == 0);
         ret = debconf_go(client);
@@ -515,24 +530,23 @@ int netcfg_get_interface(struct debconfc
         if (!inter)
             netcfg_die(client);
     }
-    
-    /* grab just the interface name, not the description too */
-    *interface = inter;
-    /* Note that the question may be preseeded to just the interface name,
-     * with no colon after it. Allow for this case. */
-    ptr = strchr(inter, ':');
-    if (ptr != NULL) {
-        *ptr = '\0';
-    }
-    *interface = strdup(*interface);
-    
+
+    *interface = strdup(inter);
+
     /* Free allocated memory */
     while (ifs && *ifs)
         free(*ifs++);
-    
+
+    if (ptr_c)
+        free(ptr_c);
+    if (ptr)
+        free(ptr);
+
     return 0;
 
  error:
+    if (ptr_c)
+        free(ptr_c);
     if (ptr)
         free(ptr);
     

Reply to: