[PATCH] xrdb: Add actual querying capabilities to -query
Michele Guerini Rocco
rnhmjoj at inventati.org
Tue Jun 22 13:58:58 UTC 2021
> NTL it is better for the general look if you use a name
> for sign-off that is more easy to pronounce.
Sure, I've changed it to my real name.
> IMHO, the idea is good. I am struggling a bit with
> reusing the query option but thats not a showstopper.
> Lets see if a discussion comes up and if no one objects
> ping me next WE.
You're right, after reading the manual more carefully, it's clear this
would change the meaning of invocations like `xrdb -query filename` and
will likely break some script. It's better to just add a new option to
be sure it's backward compatible.
Here's the new iteration:
-- PATCH STARTS 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. This patch adds a new `-get` option to search and print the
content of a single property by name.
Things I tested:
- `xrdb -get prop` prints the value of `prop` when it exists in the
resource database.
- `xrdb -get prop` doesn't print anything when `prop` doesn't exist
in the resource database.
- `xrdb -get 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: Michele Guerini Rocco <rnhmjoj at inventati.org>
---
man/xrdb.man | 4 ++++
xrdb.c | 25 ++++++++++++++++++++++++-
2 files changed, 28 insertions(+), 1 deletion(-)
diff --git a/man/xrdb.man b/man/xrdb.man
index 7ed14fc..85c748b 100644
--- a/man/xrdb.man
+++ b/man/xrdb.man
@@ -251,6 +251,10 @@ option. The
option can be used to merge the contents of properties back into the input
resource file without damaging preprocessor commands.
.TP 8
+.B \-get \fIname\fP
+This option indicates that the current content of the property matching
+\fIname\fP should be printed onto the standard output.
+.TP 8
.B \-load
This option indicates that the input should be loaded as the new value
of the specified properties, replacing whatever was there (i.e.
diff --git a/xrdb.c b/xrdb.c
index 3f6e533..e93ce12 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>
@@ -84,6 +85,7 @@
#define OPLOAD 4
#define OPMERGE 5
#define OPOVERRIDE 6
+#define OPGET 7
#define BACKUP_SUFFIX ".bak" /* for editing */
@@ -126,6 +128,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 *resource_name = NULL;
static Bool dont_execute = False;
static Bool show_cpp = False;
static String defines;
@@ -786,6 +789,7 @@ Syntax(const char *errmsg)
" -nocpp do not use a preprocessor\n"
" -E show preprocessor command & processed input file\n"
" -query query resources\n"
+ " -get name get the content of a resource\n"
" -load load resources from file [default]\n"
" -override add in resources from file\n"
" -merge merge resources from file & sort\n"
@@ -984,6 +988,13 @@ main(int argc, char *argv[])
oper = OPQUERY;
continue;
}
+ else if (isabbreviation("-get", arg, 2)) {
+ oper = OPGET;
+ if (++i >= argc)
+ Syntax("-get requires an argument");
+ resource_name = argv[i];
+ continue;
+ }
else if (isabbreviation("-load", arg, 2)) {
oper = OPLOAD;
continue;
@@ -1284,7 +1295,19 @@ Process(int scrno, Bool doScreen, Bool execute)
}
else if (oper == OPQUERY) {
if (xdefs)
- printf("%s", xdefs); /* fputs broken in SunOS 4.0 */
+ fputs(xdefs, stdout);
+ }
+ else if (oper == OPGET) {
+ if (xdefs && resource_name != NULL) {
+ char *type = NULL;
+ XrmValue value;
+ XrmDatabase xrdb = XrmGetStringDatabase(xdefs);
+ Bool found = XrmGetResource(xrdb, resource_name,
+ resource_name, &type, &value);
+ if (found == True && value.addr != NULL)
+ printf("%s\n", value.addr);
+ XrmDestroyDatabase(xrdb);
+ }
}
else if (oper == OPREMOVE) {
if (xdefs)
--
2.31.1
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 228 bytes
Desc: not available
URL: <https://lists.x.org/archives/xorg-devel/attachments/20210622/d8761908/attachment.sig>
More information about the xorg-devel
mailing list