Reputation: 453
I am writing a shell script that needs to differ its behavior and provide different options to called programs based on the presence or absence of particular X11 extensions. I have a working solution, but I am hoping for a cleaner solution. I am open to considering a simple c program to do the test and return the result. Here is what I have working as a minimal functional example:
#!/bin/sh
xdpyinfo |sed -nr '/^number of extensions/,/^[^ ]/s/^ *//p' | \
grep -q $EXTENSION && echo present
I think there is a way to simplify the sed,grep but I really would prefer not to parse xdpyinfo
.
Upvotes: 2
Views: 1351
Reputation: 5525
You have the C-tag, too, so let me suggest to do the xdpyinfo
yourself. The following C program prints just the extensions:
#include <X11/Xlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int compare(const void *a, const void *b)
{
return strcmp(*(char **) a, *(char **) b);
}
static void print_extension_info(Display * dpy)
{
int n = 0, i;
char **extlist = XListExtensions(dpy, &n);
printf("number of extensions: %d\n", n);
if (extlist) {
qsort(extlist, n, sizeof(char *), compare);
for (i = 0; i < n; i++) {
printf(" %s\n", extlist[i]);
}
}
// TODO: it might not be a good idea to free extlist, check
}
int main()
{
Display *dpy;
char *displayname = NULL;
dpy = XOpenDisplay(displayname);
if (!dpy) {
fprintf(stderr, "Unable to open display \"%s\".\n",
XDisplayName(displayname));
exit(EXIT_FAILURE);
}
print_extension_info(dpy);
XCloseDisplay(dpy);
exit(EXIT_SUCCESS);
}
Compile with e.g.: GCC
gcc -O3 -g3 -W -Wall -Wextra xdpyinfo1.0.2.c $(pkg-config --cflags --libs x11) -o xdpyinfo1.0.2
(should give a warning about unused argc but that's harmless)
Just change the printf()
's to the format you want.
Upvotes: 4