gpt4 book ai didi

检查 X11 扩展名

转载 作者:行者123 更新时间:2023-11-30 15:04:53 31 4
gpt4 key购买 nike

我正在编写一个 shell 脚本,该脚本需要改变其行为,并根据特定 X11 扩展的存在或不存在为被调用程序提供不同的选项。我有一个可行的解决方案,但我希望有一个更干净的解决方案。我愿意考虑一个简单的 C 程序来进行测试并返回结果。这是我作为一个最小的功能示例工作的:

#!/bin/sh
xdpyinfo |sed -nr '/^number of extensions/,/^[^ ]/s/^ *//p' | \
grep -q $EXTENSION && echo present

我认为有一种方法可以简化 sed、grep,但我真的不想解析 xdpyinfo

最佳答案

您也有 C 标签,所以我建议您自己执行 xdpyinfo。以下 C 程序仅打印扩展名:

#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);
}

使用例如 GCC 进行编译

gcc -O3 -g3  -W -Wall -Wextra  xdpyinfo1.0.2.c  $(pkg-config --cflags --libs x11)  -o xdpyinfo1.0.2

(应该给出有关未使用的 argc 的警告,但这是无害的)

只需将 printf() 更改为您想要的格式即可。

关于检查 X11 扩展名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40115567/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com