forked from tamirzb/xgetres
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathxrescat.c
97 lines (88 loc) · 2.61 KB
/
xrescat.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <stdio.h>
#include <getopt.h>
#include <X11/Xresource.h>
static int print_xresource(const char *resource, const char *defaultValue)
{
int ret;
XrmInitialize();
Display *display = XOpenDisplay(NULL);
if (NULL == display) {
fprintf(stderr, "Can't open display\n");
ret = -1;
goto cleanup;
}
char *resource_manager = XResourceManagerString(display);
if (NULL == resource_manager) {
fprintf(stderr, "Can't obtain RESOURCE_MANAGER\n");
ret = -1;
goto cleanup;
}
XrmDatabase db = XrmGetStringDatabase(resource_manager);
if (NULL == db) {
fprintf(stderr, "Can't open resource database\n");
ret = -1;
goto cleanup;
}
XrmValue value;
char *type;
if (XrmGetResource(db, resource, resource, &type, &value)) {
// last character of returned string is null, so skip that when
// checking the value of the actual last character in the string
if( value.addr[0] == '"' && value.addr[value.size-2] == '"') {
// surrounding quotes are present at start and and of string,
// exclude them.
printf("%.*s", value.size-3, &value.addr[1]);
} else {
printf("%s", value.addr);
}
ret = 0;
} else { // Resource not found
if (defaultValue != NULL) {
// Use default
printf("%s", defaultValue);
ret = 0;
} else {
// Error
ret = 1;
}
}
cleanup:
if (NULL != display) {
XCloseDisplay(display);
}
return ret;
}
#define USAGE "Usage: %s [OPTION] RESOURCE [DEFAULT VALUE]\n"
int main(int argc, char * const argv[])
{
struct option options[] = {
{"help", no_argument, NULL, 'h'},
{"version", no_argument, NULL, 'v'},
{0, 0, 0, 0}
};
switch(getopt_long(argc, argv, "hv", options, NULL)) {
case 'h':
printf(USAGE
"Get the value of the X resource named RESOURCE\n"
"\n"
"Options:\n"
" -h, --help Prints this help message\n"
" -v, --version Prints the version\n",
argv[0]);
return 0;
case 'v':
printf("xrescat " VERSION "\n");
return 0;
case -1:
if (2 == argc) {
return print_xresource(argv[1], NULL);
} else if (3 == argc) {
return print_xresource(argv[1], argv[2]);
}
default:
fprintf(stderr, USAGE
"Try '%s -h' for more information\n",
argv[0], argv[0]);
return -1;
}
}