[PATCH 02/16] xf86AutoConfig: Simplify builtin-config splitting.

Jamey Sharp jamey at minilop.net
Tue May 25 16:53:13 PDT 2010


The only functional change is that blank lines in the builtin config are
passed on to the parser now, and they weren't before. The parser ignores
them but they do show up in the log. Delete the extra '\n' characters
after each "EndSection" if you care.

- Reference builtinConfig directly instead of passing its address to a
  separate function---the abstraction was not helpful.

- Use strchr instead of strtok to find newlines, so the input doesn't
  need to be copied, and we can use pointer math instead of strlen.

- Only append NULL to the list when done generating it.

- Drop 'const' from builtinConfig: the strings are not constant, and GCC
  warns about passing const strings to free(3).

Signed-off-by: Jamey Sharp <jamey at minilop.net>
---
 hw/xfree86/common/xf86AutoConfig.c |   58 +++++++++++++++---------------------
 1 files changed, 24 insertions(+), 34 deletions(-)

diff --git a/hw/xfree86/common/xf86AutoConfig.c b/hw/xfree86/common/xf86AutoConfig.c
index 8947a4f..a01cc76 100644
--- a/hw/xfree86/common/xf86AutoConfig.c
+++ b/hw/xfree86/common/xf86AutoConfig.c
@@ -85,7 +85,7 @@
 #define BUILTIN_LAYOUT_SECTION_POST \
 	"EndSection\n\n"
 
-static const char **builtinConfig = NULL;
+static char **builtinConfig = NULL;
 static int builtinLines = 0;
 
 static void listPossibleVideoDrivers(char *matches[], int nmatches);
@@ -97,47 +97,35 @@ static void listPossibleVideoDrivers(char *matches[], int nmatches);
  */
 
 static void
-AppendToList(const char *s, const char ***list, int *lines)
+AppendLine(char *newstr)
 {
-    char *str, *newstr, *p;
-
-    str = xnfstrdup(s);
-    for (p = strtok(str, "\n"); p; p = strtok(NULL, "\n")) {
-	(*lines)++;
-	*list = xnfrealloc(*list, (*lines + 1) * sizeof(**list));
-	newstr = xnfalloc(strlen(p) + 2);
-	strcpy(newstr, p);
-	strcat(newstr, "\n");
-	(*list)[*lines - 1] = newstr;
-	(*list)[*lines] = NULL;
-    }
-    free(str);
-}
-
-static void
-FreeList(const char ***list, int *lines)
-{
-    int i;
-
-    for (i = 0; i < *lines; i++) {
-	if ((*list)[i])
-	    free((*list)[i]);
-    }
-    free(*list);
-    *list = NULL;
-    *lines = 0;
+    builtinConfig = xnfrealloc(builtinConfig, (builtinLines + 1) * sizeof(*builtinConfig));
+    builtinConfig[builtinLines++] = newstr;
 }
 
 static void
 FreeConfig(void)
 {
-    FreeList(&builtinConfig, &builtinLines);
+    int i;
+    for (i = 0; i < builtinLines; i++)
+	free(builtinConfig[i]);
+    free(builtinConfig);
+    builtinConfig = NULL;
+    builtinLines = 0;
 }
 
 static void
 AppendToConfig(const char *s)
 {
-    AppendToList(s, &builtinConfig, &builtinLines);
+    const char *p;
+    while ((p = strchr(s, '\n'))) {
+	int length = p - s + 1;
+	char *newstr = xnfalloc(length + 1);
+	memcpy(newstr, s, length);
+	newstr[length] = '\0';
+	s = p + 1;
+	AppendLine(newstr);
+    }
 }
 
 Bool
@@ -145,7 +133,7 @@ xf86AutoConfig(void)
 {
     char *deviceList[20];
     char **p;
-    const char **cp;
+    char **cp;
     char buf[1024];
     ConfigStatus ret;
 
@@ -169,9 +157,11 @@ xf86AutoConfig(void)
 	free(*p);
     }
 
+    AppendLine(NULL);
+
     xf86MsgVerb(X_DEFAULT, 0,
 		"Using default built-in configuration (%d lines)\n",
-		builtinLines);
+		builtinLines - 1);
 
     xf86MsgVerb(X_DEFAULT, 3, "--- Start of built-in configuration ---\n");
     for (cp = builtinConfig; *cp; cp++)
@@ -179,7 +169,7 @@ xf86AutoConfig(void)
     xf86MsgVerb(X_DEFAULT, 3, "--- End of built-in configuration ---\n");
 
     xf86initConfigFiles();
-    xf86setBuiltinConfig(builtinConfig);
+    xf86setBuiltinConfig((const char **) builtinConfig);
     ret = xf86HandleConfigFile(TRUE);
     FreeConfig();
 
-- 
1.7.0



More information about the xorg-devel mailing list