[Mesa-dev] [PATCH 1/2] xmlconfig: read more config files from drirc.d/

Qiang Yu Qiang.Yu at amd.com
Wed Aug 1 11:10:07 UTC 2018


Add two places for hosting drirc config files:
/usr/share/drirc.d/ /etc/drirc.d/

Driver and application can put their drirc files in
these places with name xxx.conf. Config files will be
read and applied in file name alphabete order.

So there are four places for drirc listed in order:
1. /usr/share/drirc.d/
2. /etc/drirc.d/
3. /etc/drirc
4. ~/.drirc

Signed-off-by: Qiang Yu <Qiang.Yu at amd.com>
---
 docs/autoconf.html   |  11 +++++-
 src/util/Makefile.am |   1 +
 src/util/meson.build |   4 +-
 src/util/xmlconfig.c | 104 ++++++++++++++++++++++++++++++++++-----------------
 4 files changed, 83 insertions(+), 37 deletions(-)

diff --git a/docs/autoconf.html b/docs/autoconf.html
index df243c2..5f1a006 100644
--- a/docs/autoconf.html
+++ b/docs/autoconf.html
@@ -90,8 +90,15 @@ tree.</p>
 <dt><code>--sysconfdir=DIR</code></dt>
 <dd><p>This option specifies the directory where the configuration
 files will be installed. The default is <code>${prefix}/etc</code>.
-Currently there's only one config file provided when dri drivers are
-enabled - it's <code>drirc</code>.</p>
+Currently when dri drivers are enabled, <code>drirc drirc.d/</code>
+are in this place.</p>
+</dd>
+
+<dt><code>--datadir=DIR</code></dt>
+<dd><p>This option specifies the directory where the data files will
+be installed. The default is <code>${prefix}/share</code>.
+Currently when dri drivers are enabled, <code>drirc.d/</code> is at
+this place.</p>
 </dd>
 
 <dt><code>--enable-static, --disable-shared</code></dt>
diff --git a/src/util/Makefile.am b/src/util/Makefile.am
index bafb574..8d8c156 100644
--- a/src/util/Makefile.am
+++ b/src/util/Makefile.am
@@ -67,6 +67,7 @@ libxmlconfig_la_CFLAGS = \
 	-I$(top_srcdir)/include \
 	-I$(top_srcdir)/src \
 	-DSYSCONFDIR=\"$(sysconfdir)\" \
+	-DDATADIR=\"$(datadir)\" \
 	$(VISIBILITY_CFLAGS) \
 	$(EXPAT_CFLAGS)
 libxmlconfig_la_LIBADD = $(EXPAT_LIBS) -lm
diff --git a/src/util/meson.build b/src/util/meson.build
index 8c91be8..108524b 100644
--- a/src/util/meson.build
+++ b/src/util/meson.build
@@ -121,7 +121,9 @@ libxmlconfig = static_library(
   c_args : [
     c_msvc_compat_args, c_vis_args,
     '-DSYSCONFDIR="@0@"'.format(
-      join_paths(get_option('prefix'), get_option('sysconfdir'))
+      join_paths(get_option('prefix'), get_option('sysconfdir')),
+    '-DDATADIR="@0@"'.format(
+      join_paths(get_option('prefix'), get_option('datadir'))
     ),
   ],
   build_by_default : false,
diff --git a/src/util/xmlconfig.c b/src/util/xmlconfig.c
index d384791..a3869ac 100644
--- a/src/util/xmlconfig.c
+++ b/src/util/xmlconfig.c
@@ -36,6 +36,8 @@
 #include <math.h>
 #include <unistd.h>
 #include <errno.h>
+#include <dirent.h>
+#include <fnmatch.h>
 #include "xmlconfig.h"
 #include "process.h"
 
@@ -866,9 +868,8 @@ initOptionCache(driOptionCache *cache, const driOptionCache *info)
     }
 }
 
-/** \brief Parse the named configuration file */
 static void
-parseOneConfigFile(XML_Parser p)
+_parseOneConfigFile(XML_Parser p)
 {
 #define BUF_SIZE 0x1000
     struct OptConfData *data = (struct OptConfData *)XML_GetUserData (p);
@@ -907,17 +908,75 @@ parseOneConfigFile(XML_Parser p)
 #undef BUF_SIZE
 }
 
+/** \brief Parse the named configuration file */
+static void
+parseOneConfigFile(struct OptConfData *data, const char *filename)
+{
+    XML_Parser p;
+
+    p = XML_ParserCreate (NULL); /* use encoding specified by file */
+    XML_SetElementHandler (p, optConfStartElem, optConfEndElem);
+    XML_SetUserData (p, data);
+    data->parser = p;
+    data->name = filename;
+    data->ignoringDevice = 0;
+    data->ignoringApp = 0;
+    data->inDriConf = 0;
+    data->inDevice = 0;
+    data->inApp = 0;
+    data->inOption = 0;
+
+    _parseOneConfigFile (p);
+    XML_ParserFree (p);
+}
+
+static int
+scandir_filter(const struct dirent *ent)
+{
+    if (ent->d_type != DT_REG && ent->d_type != DT_LNK)
+       return 0;
+
+    if (fnmatch("*.conf", ent->d_name, 0))
+       return 0;
+
+    return 1;
+}
+
+/** \brief Parse configuration files in a directory */
+static void
+parseConfigDir(struct OptConfData *data, const char *dirname)
+{
+    int i, count;
+    struct dirent **entries = NULL;
+
+    count = scandir(dirname, &entries, scandir_filter, alphasort);
+    if (count < 0)
+        return;
+
+    for (i = 0; i < count; i++) {
+        char filename[PATH_MAX];
+
+        snprintf(filename, PATH_MAX, "%s/%s", dirname, entries[i]->d_name);
+        parseOneConfigFile(data, filename);
+    }
+
+    if (entries)
+        free(entries);
+}
+
 #ifndef SYSCONFDIR
 #define SYSCONFDIR "/etc"
 #endif
 
+#ifndef DATADIR
+#define DATADIR "/usr/share"
+#endif
+
 void
 driParseConfigFiles(driOptionCache *cache, const driOptionCache *info,
                     int screenNum, const char *driverName)
 {
-    char *filenames[2] = { SYSCONFDIR "/drirc", NULL};
     char *home;
-    uint32_t i;
     struct OptConfData userData;
 
     initOptionCache (cache, info);
@@ -927,39 +986,16 @@ driParseConfigFiles(driOptionCache *cache, const driOptionCache *info,
     userData.driverName = driverName;
     userData.execName = util_get_process_name();
 
+    parseConfigDir(&userData, DATADIR "/drirc.d");
+    parseConfigDir(&userData, SYSCONFDIR "/drirc.d");
+    parseOneConfigFile(&userData, SYSCONFDIR "/drirc");
+
     if ((home = getenv ("HOME"))) {
-        uint32_t len = strlen (home);
-        filenames[1] = malloc(len + 7+1);
-        if (filenames[1] == NULL)
-            __driUtilMessage ("Can't allocate memory for %s/.drirc.", home);
-        else {
-            memcpy (filenames[1], home, len);
-            memcpy (filenames[1] + len, "/.drirc", 7+1);
-        }
-    }
+        char filename[PATH_MAX];
 
-    for (i = 0; i < 2; ++i) {
-        XML_Parser p;
-        if (filenames[i] == NULL)
-            continue;
-
-        p = XML_ParserCreate (NULL); /* use encoding specified by file */
-        XML_SetElementHandler (p, optConfStartElem, optConfEndElem);
-        XML_SetUserData (p, &userData);
-        userData.parser = p;
-        userData.name = filenames[i];
-        userData.ignoringDevice = 0;
-        userData.ignoringApp = 0;
-        userData.inDriConf = 0;
-        userData.inDevice = 0;
-        userData.inApp = 0;
-        userData.inOption = 0;
-
-        parseOneConfigFile (p);
-        XML_ParserFree (p);
+        snprintf(filename, PATH_MAX, "%s/.drirc", home);
+        parseOneConfigFile(&userData, filename);
     }
-
-    free(filenames[1]);
 }
 
 void
-- 
2.7.4



More information about the mesa-dev mailing list