aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGerd Hoffmann <kraxel@redhat.com>2020-01-02 15:19:36 +0100
committerGerd Hoffmann <kraxel@redhat.com>2020-01-02 15:19:36 +0100
commit3d93e43f45807eda548f17c3ef7b61823a614972 (patch)
tree70821e8cdf6f56986130316bb1a712be92949c87
parent30ac6fed46b7f8080920310993d14a40afa7649d (diff)
downloaddrminfo-3d93e43f45807eda548f17c3ef7b61823a614972.tar.gz
card & fbdev completion
-rw-r--r--complete.c38
-rw-r--r--complete.h1
-rw-r--r--drminfo.c8
-rw-r--r--drmtest.c1
-rw-r--r--fbinfo.c8
5 files changed, 54 insertions, 2 deletions
diff --git a/complete.c b/complete.c
index 9ce0dbe..3719563 100644
--- a/complete.c
+++ b/complete.c
@@ -1,6 +1,7 @@
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
+#include <unistd.h>
#include <getopt.h>
#include "complete.h"
@@ -10,9 +11,23 @@
" COMPREPLY=( $(compgen -f -- \"$cur\") )\n" \
" ;;\n"
+#define CASE_CARD \
+ " --card)\n" \
+ " words=$(drminfo --complete-card)\n" \
+ " COMPREPLY=( $(compgen -W \"$words\" -- \"$cur\") )\n" \
+ " ;;\n"
+
+#define CASE_FBDEV \
+ " --fbdev)\n" \
+ " words=$(fbinfo --complete-fbdev)\n" \
+ " COMPREPLY=( $(compgen -W \"$words\" -- \"$cur\") )\n" \
+ " ;;\n"
+
void complete_bash(const char *command, struct option *opts)
{
bool have_image = false;
+ bool have_card = false;
+ bool have_fbdev = false;
char opt_all[1024];
char opt_arg[1024];
int pos_all = 0;
@@ -23,6 +38,10 @@ void complete_bash(const char *command, struct option *opts)
/* options with argument completion */
if (strcmp(opts[i].name, "image") == 0) {
have_image = true;
+ } else if (strcmp(opts[i].name, "card") == 0) {
+ have_card = true;
+ } else if (strcmp(opts[i].name, "fbdev") == 0) {
+ have_fbdev = true;
} else if (opts[i].has_arg) {
/* options without argument completion */
@@ -43,11 +62,11 @@ void complete_bash(const char *command, struct option *opts)
printf("_%s_complete()\n"
"{\n"
- " local cur prev\n"
+ " local cur prev words\n"
" cur=\"${COMP_WORDS[COMP_CWORD]}\"\n"
" prev=\"${COMP_WORDS[COMP_CWORD-1]}\"\n"
" case \"$prev\" in\n"
- "%s"
+ "%s%s%s"
" %s)\n"
" COMPREPLY=()\n"
" ;;\n"
@@ -61,5 +80,20 @@ void complete_bash(const char *command, struct option *opts)
"\n",
command,
have_image ? CASE_IMAGE : "",
+ have_card ? CASE_CARD : "",
+ have_fbdev ? CASE_FBDEV : "",
opt_arg, opt_all, command, command);
}
+
+void complete_device_nr(const char *prefix)
+{
+ char filename[128];
+ int nr;
+
+ for (nr = 0;; nr++) {
+ snprintf(filename, sizeof(filename), "%s%d", prefix, nr);
+ if (access(filename, F_OK) < 0)
+ return;
+ printf("%d\n", nr);
+ }
+}
diff --git a/complete.h b/complete.h
index a1fd02b..d3860a8 100644
--- a/complete.h
+++ b/complete.h
@@ -1 +1,2 @@
void complete_bash(const char *command, struct option *opts);
+void complete_device_nr(const char *prefix);
diff --git a/drminfo.c b/drminfo.c
index ebadb80..c211ea2 100644
--- a/drminfo.c
+++ b/drminfo.c
@@ -462,6 +462,7 @@ static void usage(FILE *fp)
enum {
OPT_LONG_LEASE = 0x100,
OPT_LONG_COMP_BASH,
+ OPT_LONG_COMP_CARD,
};
static struct option long_opts[] = {
@@ -511,6 +512,10 @@ static struct option long_opts[] = {
.has_arg = false,
.val = OPT_LONG_COMP_BASH,
},{
+ .name = "complete-card",
+ .has_arg = false,
+ .val = OPT_LONG_COMP_CARD,
+ },{
/* --- with argument --- */
.name = "card",
@@ -591,6 +596,9 @@ int main(int argc, char **argv)
case OPT_LONG_COMP_BASH:
complete_bash("drminfo", long_opts);
exit(0);
+ case OPT_LONG_COMP_CARD:
+ complete_device_nr("/dev/dri/card");
+ exit(0);
case 'h':
usage(stdout);
exit(0);
diff --git a/drmtest.c b/drmtest.c
index a5fd5d8..9d6c5c6 100644
--- a/drmtest.c
+++ b/drmtest.c
@@ -29,6 +29,7 @@
#include "drm-lease.h"
#include "render.h"
#include "image.h"
+#include "complete.h"
/* ------------------------------------------------------------------ */
diff --git a/fbinfo.c b/fbinfo.c
index e3f3bf7..14b8f97 100644
--- a/fbinfo.c
+++ b/fbinfo.c
@@ -32,6 +32,7 @@ static void usage(FILE *fp)
enum {
OPT_LONG_COMP_BASH = 0x100,
+ OPT_LONG_COMP_FBDEV,
};
static struct option long_opts[] = {
@@ -45,6 +46,10 @@ static struct option long_opts[] = {
.has_arg = false,
.val = OPT_LONG_COMP_BASH,
},{
+ .name = "complete-fbdev",
+ .has_arg = false,
+ .val = OPT_LONG_COMP_FBDEV,
+ },{
/* --- with argument --- */
.name = "fbdev",
@@ -71,6 +76,9 @@ int main(int argc, char **argv)
case OPT_LONG_COMP_BASH:
complete_bash("fbinfo", long_opts);
exit(0);
+ case OPT_LONG_COMP_FBDEV:
+ complete_device_nr("/dev/fb");
+ exit(0);
case 'h':
usage(stdout);
exit(0);