]> git.ipfire.org Git - thirdparty/bird.git/commitdiff
Bash: Complete also in the middle of the line. (And some cleanup, too.)
authorJan Moskyto Matejka <mq@ucw.cz>
Thu, 18 May 2017 11:54:56 +0000 (13:54 +0200)
committerJan Moskyto Matejka <mq@ucw.cz>
Thu, 18 May 2017 11:54:56 +0000 (13:54 +0200)
bird-complete.bash
client/client.c
client/client.h
client/complete.c

index 16795da389ae286b265aaa6ba274aabf3f4bc525..4d9f90071a18fea0a41c783e13f242cb2395af48 100644 (file)
@@ -26,8 +26,6 @@ function _birdc_complete {
   NOW=$2
   PREV=$3
 
-  echo "bagr" >>xxb
-
   case $PREV in
     -*([lvr])s)
       COMPREPLY=( $(compgen -W "$(find -maxdepth 1 -type s)" -- $NOW) )
@@ -46,7 +44,7 @@ function _birdc_complete {
       ;;
   esac
 
-  COMPREPLY=( $($CMD -C "$NOW" "$COMP_TYPE" "$COMP_CWORD" "$COMP_POINT" "${COMP_WORDS[@]}") )
+  COMPREPLY=( $($CMD -C "$NOW" "$COMP_TYPE" "$COMP_CWORD" "${COMP_WORDS[@]}") )
 }
 
 complete -F _bird_complete bird
index 032c7fe2e941846f09f97f9447f376701706f3ea..b4e2c60518d434906230a1a4740b966693572f2a 100644 (file)
@@ -72,8 +72,8 @@ parse_args(int argc, char **argv)
 
   if ((argc > 1) && !strcmp(argv[1], "-C")) {
     complete_init(argc-2, argv+2);
-    argv += 6;
-    argc -= 6;
+    argv += COMPLETE_ARGC + 2;
+    argc -= COMPLETE_ARGC + 2;
     complete = 1;
   }
 
@@ -113,11 +113,18 @@ parse_args(int argc, char **argv)
       tmp = init_cmd = malloc(len);
       for (i = optind; i < argc; i++)
        {
+         if (complete && (argv[i] == comp_last))
+           break;
          strcpy(tmp, argv[i]);
          tmp += strlen(tmp);
          *tmp++ = ' ';
        }
-      tmp[-1] = 0;
+
+      if (complete) {
+       strcpy(tmp, comp_now);
+       tmp += strlen(comp_now);
+      } else
+       tmp[-1] = 0;
 
       once = 1;
       interactive = 0;
index 7e2c438a8a6f75964e7378de9541b68e5978bc8e..28b5416973b778fa69e007bc30180d563dd84389 100644 (file)
@@ -38,6 +38,8 @@ char *cmd_expand(char *cmd);
 
 void complete_init(int argc, char **argv);
 int do_complete(char *cmd);
+#define COMPLETE_ARGC  3
+extern const char *comp_now, *comp_last;
 
 /* die() with system error messages */
 #define DIE(x, y...) die(x ": %s", ##y, strerror(errno))
index 4e478b9e7ea25ba4dacd0a74fff681cdc03edb59..559f3c65f0371c19282cd22a98b2355f93f00e91 100644 (file)
@@ -14,8 +14,8 @@
 #include "nest/bird.h"
 #include "client/client.h"
 
-static const char *c_now;
-static int comp_type, comp_cword, comp_point;
+static int comp_type, comp_cword;
+const char *comp_now, *comp_last;
 
 void complete_init(int argc, char **argv) {
   /* In argv, there are:
@@ -26,9 +26,9 @@ void complete_init(int argc, char **argv) {
    * ${COMP_WORDS[@]}
    */
 
-  c_now = argv[0];
+  comp_now = argv[0];
 
-  if (argc < 4)
+  if (argc < COMPLETE_ARGC)
     die("Not enough args.");
 
   if (sscanf(argv[1], "%d", &comp_type) != 1)
@@ -37,9 +37,10 @@ void complete_init(int argc, char **argv) {
   if (sscanf(argv[2], "%d", &comp_cword) != 1)
     die("Strange COMP_CWORD=\"%s\".", argv[2]);
 
-  if (sscanf(argv[3], "%d", &comp_point) != 1)
-    die("Strange COMP_POINT=\"%s\".", argv[3]);
+  if (comp_cword + COMPLETE_ARGC >= argc)
+    die("COMP_CWORD=%d points after end of arg list.", comp_cword);
 
+  comp_last = argv[COMPLETE_ARGC + comp_cword];
   return;
 }
 
@@ -50,7 +51,7 @@ int do_complete(char *cmd) {
   char buf[256];
   int res = cmd_complete(cmd, strlen(cmd), buf, (comp_type == 63));
   if (res == 1)
-    printf("%s%s\n", c_now, buf);
+    printf("%s%s\n", comp_now, buf);
 
     
   return 0;