Added few more stubs so that control reaches to DestroyDevice().
[mesa.git] / src / util / xmlconfig.c
index 5264f2598b4f06e7a6ffaba9409eaa1d8968ab67..a02e47e0ae57ab68f2666cf828939a07594a9dc9 100644 (file)
 
 #include <limits.h>
 #include <stdarg.h>
+#include <stdbool.h>
+#include <stdint.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 #include <assert.h>
 #include <expat.h>
 #include <errno.h>
 #include <dirent.h>
 #include <fnmatch.h>
+#include <regex.h>
+#include "strndup.h"
 #include "xmlconfig.h"
 #include "u_process.h"
+#include "os_file.h"
 
+/* For systems like Hurd */
+#ifndef PATH_MAX
+#define PATH_MAX 4096
+#endif
+
+static bool
+be_verbose(void)
+{
+   const char *s = getenv("MESA_DEBUG");
+   if (!s)
+      return true;
+
+   return strstr(s, "silent") == NULL;
+}
 
 /** \brief Find an option in an option cache with the name as key */
 static uint32_t
@@ -539,9 +559,11 @@ parseOptInfoAttr(struct OptInfoData *data, const XML_Char **attr)
     defaultVal = getenv (cache->info[opt].name);
     if (defaultVal != NULL) {
       /* don't use XML_WARNING, we want the user to see this! */
-        fprintf (stderr,
-                 "ATTENTION: default value of option %s overridden by environment.\n",
-                 cache->info[opt].name);
+        if (be_verbose()) {
+            fprintf(stderr,
+                    "ATTENTION: default value of option %s overridden by environment.\n",
+                    cache->info[opt].name);
+        }
     } else
         defaultVal = attrVal[OA_DEFAULT];
     if (!parseValue (&cache->values[opt], cache->info[opt].type, defaultVal))
@@ -692,6 +714,10 @@ struct OptConfData {
     int screenNum;
     const char *driverName, *execName;
     const char *kernelDriverName;
+    const char *engineName;
+    const char *applicationName;
+    uint32_t engineVersion;
+    uint32_t applicationVersion;
     uint32_t ignoringDevice;
     uint32_t ignoringApp;
     uint32_t inDriConf;
@@ -702,12 +728,13 @@ struct OptConfData {
 
 /** \brief Elements in configuration files. */
 enum OptConfElem {
-    OC_APPLICATION = 0, OC_DEVICE, OC_DRICONF, OC_OPTION, OC_COUNT
+    OC_APPLICATION = 0, OC_DEVICE, OC_DRICONF, OC_ENGINE, OC_OPTION, OC_COUNT
 };
 static const XML_Char *OptConfElems[] = {
     [OC_APPLICATION]  = "application",
     [OC_DEVICE] = "device",
     [OC_DRICONF] = "driconf",
+    [OC_ENGINE]  = "engine",
     [OC_OPTION] = "option",
 };
 
@@ -736,19 +763,118 @@ parseDeviceAttr(struct OptConfData *data, const XML_Char **attr)
     }
 }
 
+static bool
+valueInRanges(const driOptionInfo *info, uint32_t value)
+{
+    uint32_t i;
+
+    for (i = 0; i < info->nRanges; i++) {
+       if (info->ranges[i].start._int <= value &&
+           info->ranges[i].end._int >= value)
+           return true;
+    }
+
+    return false;
+}
+
 /** \brief Parse attributes of an application element. */
 static void
 parseAppAttr(struct OptConfData *data, const XML_Char **attr)
 {
     uint32_t i;
     const XML_Char *exec = NULL;
+    const XML_Char *sha1 = NULL;
+    const XML_Char *application_name_match = NULL;
+    const XML_Char *application_versions = NULL;
+    driOptionInfo version_ranges = {
+       .type = DRI_INT,
+    };
+
     for (i = 0; attr[i]; i += 2) {
         if (!strcmp (attr[i], "name")) /* not needed here */;
         else if (!strcmp (attr[i], "executable")) exec = attr[i+1];
+        else if (!strcmp (attr[i], "sha1")) sha1 = attr[i+1];
+        else if (!strcmp (attr[i], "application_name_match"))
+           application_name_match = attr[i+1];
+        else if (!strcmp (attr[i], "application_versions"))
+           application_versions = attr[i+1];
         else XML_WARNING("unknown application attribute: %s.", attr[i]);
     }
-    if (exec && strcmp (exec, data->execName))
+    if (exec && strcmp (exec, data->execName)) {
         data->ignoringApp = data->inApp;
+    } else if (sha1) {
+        /* SHA1_DIGEST_STRING_LENGTH includes terminating null byte */
+        if (strlen(sha1) != (SHA1_DIGEST_STRING_LENGTH - 1)) {
+            XML_WARNING("Incorrect sha1 application attribute");
+            data->ignoringApp = data->inApp;
+        } else {
+            size_t len;
+            char* content;
+            char path[PATH_MAX];
+            if (util_get_process_exec_path(path, ARRAY_SIZE(path)) > 0 &&
+                (content = os_read_file(path, &len))) {
+                uint8_t sha1x[SHA1_DIGEST_LENGTH];
+                char sha1s[SHA1_DIGEST_STRING_LENGTH];
+                _mesa_sha1_compute(content, len, sha1x);
+                _mesa_sha1_format((char*) sha1s, sha1x);
+                free(content);
+
+                if (strcmp(sha1, sha1s)) {
+                    data->ignoringApp = data->inApp;
+                }
+            } else {
+                data->ignoringApp = data->inApp;
+            }
+        }
+    } else if (application_name_match) {
+       regex_t re;
+
+       if (regcomp (&re, application_name_match, REG_EXTENDED|REG_NOSUB) == 0) {
+          if (regexec (&re, data->applicationName, 0, NULL, 0) == REG_NOMATCH)
+             data->ignoringApp = data->inApp;
+          regfree (&re);
+       } else
+          XML_WARNING ("Invalid application_name_match=\"%s\".", application_name_match);
+    }
+    if (application_versions) {
+       if (parseRanges (&version_ranges, application_versions) &&
+           !valueInRanges (&version_ranges, data->applicationVersion))
+          data->ignoringApp = data->inApp;
+    }
+}
+
+/** \brief Parse attributes of an application element. */
+static void
+parseEngineAttr(struct OptConfData *data, const XML_Char **attr)
+{
+    uint32_t i;
+    const XML_Char *engine_name_match = NULL, *engine_versions = NULL;
+    driOptionInfo version_ranges = {
+       .type = DRI_INT,
+    };
+    for (i = 0; attr[i]; i += 2) {
+        if (!strcmp (attr[i], "name")) /* not needed here */;
+        else if (!strcmp (attr[i], "engine_name_match")) engine_name_match = attr[i+1];
+        else if (!strcmp (attr[i], "engine_versions")) engine_versions = attr[i+1];
+        else XML_WARNING("unknown application attribute: %s.", attr[i]);
+    }
+    if (engine_name_match) {
+       regex_t re;
+
+       if (regcomp (&re, engine_name_match, REG_EXTENDED|REG_NOSUB) == 0) {
+          if (regexec (&re, data->engineName, 0, NULL, 0) == REG_NOMATCH)
+             data->ignoringApp = data->inApp;
+          regfree (&re);
+       } else
+          XML_WARNING ("Invalid engine_name_match=\"%s\".", engine_name_match);
+    }
+    if (engine_versions) {
+       if (parseRanges (&version_ranges, engine_versions) &&
+           !valueInRanges (&version_ranges, data->engineVersion))
+          data->ignoringApp = data->inApp;
+    }
+
+    free(version_ranges.ranges);
 }
 
 /** \brief Parse attributes of an option element. */
@@ -771,11 +897,14 @@ parseOptConfAttr(struct OptConfData *data, const XML_Char **attr)
             /* don't use XML_WARNING, drirc defines options for all drivers,
              * but not all drivers support them */
             return;
-        else if (getenv (cache->info[opt].name))
+        else if (getenv (cache->info[opt].name)) {
           /* don't use XML_WARNING, we want the user to see this! */
-            fprintf (stderr, "ATTENTION: option value of option %s ignored.\n",
-                     cache->info[opt].name);
-        else if (!parseValue (&cache->values[opt], cache->info[opt].type, value))
+            if (be_verbose()) {
+                fprintf(stderr,
+                        "ATTENTION: option value of option %s ignored.\n",
+                        cache->info[opt].name);
+            }
+        } else if (!parseValue (&cache->values[opt], cache->info[opt].type, value))
             XML_WARNING ("illegal option value: %s.", value);
     }
 }
@@ -808,11 +937,20 @@ optConfStartElem(void *userData, const XML_Char *name,
         if (!data->inDevice)
             XML_WARNING1 ("<application> should be inside <device>.");
         if (data->inApp)
-            XML_WARNING1 ("nested <application> elements.");
+            XML_WARNING1 ("nested <application> or <engine> elements.");
         data->inApp++;
         if (!data->ignoringDevice && !data->ignoringApp)
             parseAppAttr (data, attr);
         break;
+      case OC_ENGINE:
+        if (!data->inDevice)
+            XML_WARNING1 ("<engine> should be inside <device>.");
+        if (data->inApp)
+            XML_WARNING1 ("nested <application> or <engine> elements.");
+        data->inApp++;
+        if (!data->ignoringDevice && !data->ignoringApp)
+            parseEngineAttr (data, attr);
+        break;
       case OC_OPTION:
         if (!data->inApp)
             XML_WARNING1 ("<option> should be inside <application>.");
@@ -842,6 +980,7 @@ optConfEndElem(void *userData, const XML_Char *name)
             data->ignoringDevice = 0;
         break;
       case OC_APPLICATION:
+      case OC_ENGINE:
         if (data->inApp-- == data->ignoringApp)
             data->ignoringApp = 0;
         break;
@@ -938,8 +1077,16 @@ parseOneConfigFile(struct OptConfData *data, const char *filename)
 static int
 scandir_filter(const struct dirent *ent)
 {
+#ifndef DT_REG /* systems without d_type in dirent results */
+    struct stat st;
+
+    if ((lstat(ent->d_name, &st) != 0) ||
+        (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode)))
+       return 0;
+#else
     if (ent->d_type != DT_REG && ent->d_type != DT_LNK)
        return 0;
+#endif
 
     if (fnmatch("*.conf", ent->d_name, 0))
        return 0;
@@ -981,7 +1128,9 @@ parseConfigDir(struct OptConfData *data, const char *dirname)
 void
 driParseConfigFiles(driOptionCache *cache, const driOptionCache *info,
                     int screenNum, const char *driverName,
-                    const char *kernelDriverName)
+                    const char *kernelDriverName,
+                    const char *applicationName, uint32_t applicationVersion,
+                    const char *engineName, uint32_t engineVersion)
 {
     char *home;
     struct OptConfData userData;
@@ -992,6 +1141,10 @@ driParseConfigFiles(driOptionCache *cache, const driOptionCache *info,
     userData.screenNum = screenNum;
     userData.driverName = driverName;
     userData.kernelDriverName = kernelDriverName;
+    userData.applicationName = applicationName ? applicationName : "";
+    userData.applicationVersion = applicationVersion;
+    userData.engineName = engineName ? engineName : "";
+    userData.engineVersion = engineVersion;
     userData.execName = util_get_process_name();
 
     parseConfigDir(&userData, DATADIR "/drirc.d");