[PATCH] xrdb: Add actual querying capabilities to -query

rnhmjoj rnhmjoj at inventati.org
Sun Jun 20 11:28:32 UTC 2021


Hi all,

I've opened a pull request on gitlab[1] months ago, but apparently
it hasn't been noticed, so I'm sending the patch here too. It's a
pretty simple change to xrdb but I think it's very useful: I've been
using it for a while and it simplified my shell scripts a lot.

[1]: https://gitlab.freedesktop.org/xorg/app/xrdb/-/merge_requests/2

--- ORIGINAL PATCH HERE ---

The world is littered with broken grep commands because `xrdb -query`
can only dump the database and doesn't implement this simple search
feature.

Things I tested:

  - `xrdb -query` without arguments works the same as before

  - `xrdb -query prop` prints the value of `prop` when it exists in the
    resource database.

  - `xrdb -query prop` doesn't print anything when `prop` doesn't exist
    in the resource database.

  - `xrdb -query prop` doesn't leak any memory. I run it in valgrind
    with the `--leak-check=full` option.

PS: I think Sun took care of the fputs thing by now.

Signed-off-by: rnhmjoj <rnhmjoj at inventati.org>
---
 xrdb.c | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)

diff --git a/xrdb.c b/xrdb.c
index 3f6e533..4b69363 100644
--- a/xrdb.c
+++ b/xrdb.c
@@ -48,6 +48,7 @@
 #include <X11/Xatom.h>
 #include <X11/Xos.h>
 #include <X11/Xmu/SysUtil.h>
+#include <X11/Xresource.h>
 #include <stdio.h>
 #include <ctype.h>
 #include <errno.h>
@@ -126,6 +127,7 @@ static char *editFile = NULL;
 static const char *cpp_program = NULL;
 static const char * const cpp_locations[] = { CPP };
 static const char *backup_suffix = BACKUP_SUFFIX;
+static const char *query_property = NULL;
 static Bool dont_execute = False;
 static Bool show_cpp = False;
 static String defines;
@@ -785,7 +787,7 @@ Syntax(const char *errmsg)
             " -cpp filename       preprocessor to use [%s]\n"
             " -nocpp              do not use a preprocessor\n"
             " -E                  show preprocessor command & processed input file\n"
-            " -query              query resources\n"
+            " -query [property]   query resources\n"
             " -load               load resources from file [default]\n"
             " -override           add in resources from file\n"
             " -merge              merge resources from file & sort\n"
@@ -982,6 +984,8 @@ main(int argc, char *argv[])
             }
             else if (isabbreviation("-query", arg, 2)) {
                 oper = OPQUERY;
+                if (i+1 < argc && argv[i+1][0] != '-')
+                    query_property = argv[++i];
                 continue;
             }
             else if (isabbreviation("-load", arg, 2)) {
@@ -1283,8 +1287,18 @@ Process(int scrno, Bool doScreen, Bool execute)
         printf("%s\n", defines.val);
     }
     else if (oper == OPQUERY) {
-        if (xdefs)
-            printf("%s", xdefs);        /* fputs broken in SunOS 4.0 */
+        if (xdefs && query_property != NULL) {
+            char *type = NULL;
+            XrmValue value;
+            XrmDatabase xrdb = XrmGetStringDatabase(xdefs);
+            Bool found = XrmGetResource(xrdb, query_property,
+                                        query_property, &type, &value);
+            if (found == True && value.addr != NULL)
+                printf("%s\n", value.addr);
+            XrmDestroyDatabase(xrdb);
+        }
+        else if (xdefs)
+            fputs(xdefs, stdout);
     }
     else if (oper == OPREMOVE) {
         if (xdefs)
-- 
2.31.1



More information about the xorg-devel mailing list