fix some 0->NULLs
[mesa.git] / src / mesa / drivers / dri / common / xmlconfig.c
index a6d4c8ab941d9af4aa2af35a70d526d82277e2af..b635894fe53858bb0064a22c0f808924394cf56e 100644 (file)
 
 #undef GET_PROGRAM_NAME
 
-#if defined(__GNU_LIBRARY__) || defined(__GLIBC__)
+#if (defined(__GNU_LIBRARY__) || defined(__GLIBC__)) && !defined(__UCLIBC__)
+#    if !defined(__GLIBC__) || (__GLIBC__ < 2)
+/* These aren't declared in any libc5 header */
+extern char *program_invocation_name, *program_invocation_short_name;
+#    endif
 #    define GET_PROGRAM_NAME() program_invocation_short_name
 #elif defined(__FreeBSD__) && (__FreeBSD__ >= 2)
 #    include <osreldate.h>
 #endif
 
 #if !defined(GET_PROGRAM_NAME)
-#    if defined(OpenBSD) || defined(NetBSD)
-/* This is a hack. It's said to work on OpenBSD, NetBSD and GNU. It's
+#    if defined(OpenBSD) || defined(NetBSD) || defined(__UCLIBC__)
+/* This is a hack. It's said to work on OpenBSD, NetBSD and GNU.
+ * Rogelio M.Serrano Jr. reported it's also working with UCLIBC. It's
  * used as a last resort, if there is no documented facility available. */
 static const char *__getProgramName () {
     extern const char *__progname;
-    return progname;
+    char * arg = strrchr(__progname, '/');
+    if (arg)
+        return arg+1;
+    else
+        return __progname;
 }
 #        define GET_PROGRAM_NAME() __getProgramName()
 #    else
 #        define GET_PROGRAM_NAME() ""
-#        warning "Per application configuration won't with your OS version work."
+#        warning "Per application configuration won't work with your OS version."
 #    endif
 #endif
 
@@ -137,6 +146,136 @@ static GLuint bsearchStr (const XML_Char *name,
        return count;
 }
 
+/** \brief Locale-independent integer parser.
+ *
+ * Works similar to strtol. Leading space is NOT skipped. The input
+ * number may have an optional sign. Radix is specified by base. If
+ * base is 0 then decimal is assumed unless the input number is
+ * prefixed by 0x or 0X for hexadecimal or 0 for octal. After
+ * returning tail points to the first character that is not part of
+ * the integer number. If no number was found then tail points to the
+ * start of the input string. */
+static GLint strToI (const XML_Char *string, const XML_Char **tail, int base) {
+    GLint radix = base == 0 ? 10 : base;
+    GLint result = 0;
+    GLint sign = 1;
+    GLboolean numberFound = GL_FALSE;
+    const XML_Char *start = string;
+
+    assert (radix >= 2 && radix <= 36);
+
+    if (*string == '-') {
+       sign = -1;
+       string++;
+    } else if (*string == '+')
+       string++;
+    if (base == 0 && *string == '0') {
+       numberFound = GL_TRUE; 
+       if (*(string+1) == 'x' || *(string+1) == 'X') {
+           radix = 16;
+           string += 2;
+       } else {
+           radix = 8;
+           string++;
+       }
+    }
+    do {
+       GLint digit = -1;
+       if (radix <= 10) {
+           if (*string >= '0' && *string < '0' + radix)
+               digit = *string - '0';
+       } else {
+           if (*string >= '0' && *string <= '9')
+               digit = *string - '0';
+           else if (*string >= 'a' && *string < 'a' + radix - 10)
+               digit = *string - 'a' + 10;
+           else if (*string >= 'A' && *string < 'A' + radix - 10)
+               digit = *string - 'A' + 10;
+       }
+       if (digit != -1) {
+           numberFound = GL_TRUE;
+           result = radix*result + digit;
+           string++;
+       } else
+           break;
+    } while (GL_TRUE);
+    *tail = numberFound ? string : start;
+    return sign * result;
+}
+
+/** \brief Locale-independent floating-point parser.
+ *
+ * Works similar to strtod. Leading space is NOT skipped. The input
+ * number may have an optional sign. '.' is interpreted as decimal
+ * point and may occor at most once. Optionally the number may end in
+ * [eE]<exponent>, where <exponent> is an integer as recognized by
+ * strToI. In that case the result is number * 10^exponent. After
+ * returning tail points to the first character that is not part of
+ * the floating point number. If no number was found then tail points
+ * to the start of the input string.
+ *
+ * Uses two passes for maximum accuracy. */
+static GLfloat strToF (const XML_Char *string, const XML_Char **tail) {
+    GLint nDigits = 0, pointPos, exponent;
+    GLfloat sign = 1.0f, result = 0.0f, scale;
+    const XML_Char *start = string, *numStart;
+
+    /* sign */
+    if (*string == '-') {
+       sign = -1.0f;
+       string++;
+    } else if (*string == '+')
+       string++;
+
+    /* first pass: determine position of decimal point, number of
+     * digits, exponent and the end of the number. */
+    numStart = string;
+    while (*string >= '0' && *string <= '9') {
+       string++;
+       nDigits++;
+    }
+    pointPos = nDigits;
+    if (*string == '.') {
+       string++;
+       while (*string >= '0' && *string <= '9') {
+           string++;
+           nDigits++;
+       }
+    }
+    if (nDigits == 0) {
+       /* no digits, no number */
+       *tail = start;
+       return 0.0f;
+    }
+    *tail = string;
+    if (*string == 'e' || *string == 'E') {
+       const XML_Char *expTail;
+       exponent = strToI (string+1, &expTail, 10);
+       if (expTail == string+1)
+           exponent = 0;
+       else
+           *tail = expTail;
+    } else
+       exponent = 0;
+    string = numStart;
+
+    /* scale of the first digit */
+    scale = sign * (GLfloat)pow (10.0, (GLdouble)(pointPos-1 + exponent));
+
+    /* second pass: parse digits */
+    do {
+       if (*string != '.') {
+           assert (*string >= '0' && *string <= '9');
+           result += scale * (GLfloat)(*string - '0');
+           scale *= 0.1f;
+           nDigits--;
+       }
+       string++;
+    } while (nDigits > 0);
+
+    return result;
+}
+
 /** \brief Parse a value of a given type. */
 static GLboolean parseValue (driOptionValue *v, driOptionType type,
                             const XML_Char *string) {
@@ -157,10 +296,10 @@ static GLboolean parseValue (driOptionValue *v, driOptionType type,
        break;
       case DRI_ENUM: /* enum is just a special integer */
       case DRI_INT:
-       v->_int = strtol (string, (char **)&tail, 0);
+       v->_int = strToI (string, &tail, 0);
        break;
       case DRI_FLOAT:
-       v->_float = strtod (string, (char **)&tail);
+       v->_float = strToF (string, &tail);
        break;
     }
 
@@ -504,20 +643,25 @@ static void optInfoEndElem (void *userData, const XML_Char *name) {
     }
 }
 
-void driParseOptionInfo (driOptionCache *info) {
+void driParseOptionInfo (driOptionCache *info,
+                        const char *configOptions, GLuint nConfigOptions) {
     XML_Parser p;
     int status;
     struct OptInfoData userData;
     struct OptInfoData *data = &userData;
-    GLuint nOptions;
-
-  /* determine hash table size and allocate memory */
+    GLuint realNoptions;
+
+  /* determine hash table size and allocate memory:
+   * 3/2 of the number of options, rounded up, so there remains always
+   * at least one free entry. This is needed for detecting undefined
+   * options in configuration files without getting a hash table overflow.
+   * Round this up to a power of two. */
+    GLuint minSize = (nConfigOptions*3 + 1) / 2;
     GLuint size, log2size;
-    for (size = 1, log2size = 0; size < __driNConfigOptions*3/2;
-        size <<= 1, ++log2size);
+    for (size = 1, log2size = 0; size < minSize; size <<= 1, ++log2size);
     info->tableSize = log2size;
     info->info = CALLOC (size * sizeof (driOptionInfo));
-    info->values = CALLOC (size * sizeof (driOptionInfo));
+    info->values = CALLOC (size * sizeof (driOptionValue));
     if (info->info == NULL || info->values == NULL) {
        fprintf (stderr, "%s: %d: out of memory.\n", __FILE__, __LINE__);
        abort();
@@ -537,21 +681,21 @@ void driParseOptionInfo (driOptionCache *info) {
     userData.inEnum = GL_FALSE;
     userData.curOption = -1;
 
-    status = XML_Parse (p, __driConfigOptions, strlen (__driConfigOptions), 1);
+    status = XML_Parse (p, configOptions, strlen (configOptions), 1);
     if (!status)
        XML_FATAL ("%s.", XML_ErrorString(XML_GetErrorCode(p)));
 
     XML_ParserFree (p);
 
-  /* Check if the actual number of options matches __driNConfigOptions.
+  /* Check if the actual number of options matches nConfigOptions.
    * A mismatch is not fatal (a hash table overflow would be) but we
    * want the driver developer's attention anyway. */
-    nOptions = countOptions (info);
-    if (nOptions != __driNConfigOptions) {
+    realNoptions = countOptions (info);
+    if (realNoptions != nConfigOptions) {
        fprintf (stderr,
-                "Error: __driNConfigOptions (%u) does not match the actual number of options in\n"
+                "Error: nConfigOptions (%u) does not match the actual number of options in\n"
                 "       __driConfigOptions (%u).\n",
-                __driNConfigOptions, nOptions);
+                nConfigOptions, realNoptions);
     }
 }
 
@@ -706,7 +850,7 @@ static void optConfEndElem (void *userData, const XML_Char *name) {
 }
 
 /** \brief Initialize an option cache based on info */
-static void initOptionCache (driOptionCache *cache, driOptionCache *info) {
+static void initOptionCache (driOptionCache *cache, const driOptionCache *info) {
     cache->info = info->info;
     cache->tableSize = info->tableSize;
     cache->values = MALLOC ((1<<info->tableSize) * sizeof (driOptionValue));
@@ -757,7 +901,7 @@ static void parseOneConfigFile (XML_Parser p) {
 #undef BUF_SIZE
 }
 
-void driParseConfigFiles (driOptionCache *cache, driOptionCache *info,
+void driParseConfigFiles (driOptionCache *cache, const driOptionCache *info,
                          GLint screenNum, const char *driverName) {
     char *filenames[2] = {"/etc/drirc", NULL};
     char *home;
@@ -769,11 +913,7 @@ void driParseConfigFiles (driOptionCache *cache, driOptionCache *info,
     userData.cache = cache;
     userData.screenNum = screenNum;
     userData.driverName = driverName;
-#ifndef _SOLO    
     userData.execName = GET_PROGRAM_NAME();
-#else
-    userData.execName = "Solo";
-#endif    
 
     if ((home = getenv ("HOME"))) {
        GLuint len = strlen (home);