0
0
mirror of https://gitlab.nic.cz/labs/bird.git synced 2024-12-23 02:01:55 +00:00

Bash: Complete also in the middle of the line. (And some cleanup, too.)

This commit is contained in:
Jan Moskyto Matejka 2017-05-18 13:54:56 +02:00
parent 5b8eb44c14
commit f1ed108bb2
4 changed files with 21 additions and 13 deletions

View File

@ -26,8 +26,6 @@ function _birdc_complete {
NOW=$2 NOW=$2
PREV=$3 PREV=$3
echo "bagr" >>xxb
case $PREV in case $PREV in
-*([lvr])s) -*([lvr])s)
COMPREPLY=( $(compgen -W "$(find -maxdepth 1 -type s)" -- $NOW) ) COMPREPLY=( $(compgen -W "$(find -maxdepth 1 -type s)" -- $NOW) )
@ -46,7 +44,7 @@ function _birdc_complete {
;; ;;
esac 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 complete -F _bird_complete bird

View File

@ -72,8 +72,8 @@ parse_args(int argc, char **argv)
if ((argc > 1) && !strcmp(argv[1], "-C")) { if ((argc > 1) && !strcmp(argv[1], "-C")) {
complete_init(argc-2, argv+2); complete_init(argc-2, argv+2);
argv += 6; argv += COMPLETE_ARGC + 2;
argc -= 6; argc -= COMPLETE_ARGC + 2;
complete = 1; complete = 1;
} }
@ -113,10 +113,17 @@ parse_args(int argc, char **argv)
tmp = init_cmd = malloc(len); tmp = init_cmd = malloc(len);
for (i = optind; i < argc; i++) for (i = optind; i < argc; i++)
{ {
if (complete && (argv[i] == comp_last))
break;
strcpy(tmp, argv[i]); strcpy(tmp, argv[i]);
tmp += strlen(tmp); tmp += strlen(tmp);
*tmp++ = ' '; *tmp++ = ' ';
} }
if (complete) {
strcpy(tmp, comp_now);
tmp += strlen(comp_now);
} else
tmp[-1] = 0; tmp[-1] = 0;
once = 1; once = 1;

View File

@ -38,6 +38,8 @@ char *cmd_expand(char *cmd);
void complete_init(int argc, char **argv); void complete_init(int argc, char **argv);
int do_complete(char *cmd); int do_complete(char *cmd);
#define COMPLETE_ARGC 3
extern const char *comp_now, *comp_last;
/* die() with system error messages */ /* die() with system error messages */
#define DIE(x, y...) die(x ": %s", ##y, strerror(errno)) #define DIE(x, y...) die(x ": %s", ##y, strerror(errno))

View File

@ -14,8 +14,8 @@
#include "nest/bird.h" #include "nest/bird.h"
#include "client/client.h" #include "client/client.h"
static const char *c_now; static int comp_type, comp_cword;
static int comp_type, comp_cword, comp_point; const char *comp_now, *comp_last;
void complete_init(int argc, char **argv) { void complete_init(int argc, char **argv) {
/* In argv, there are: /* In argv, there are:
@ -26,9 +26,9 @@ void complete_init(int argc, char **argv) {
* ${COMP_WORDS[@]} * ${COMP_WORDS[@]}
*/ */
c_now = argv[0]; comp_now = argv[0];
if (argc < 4) if (argc < COMPLETE_ARGC)
die("Not enough args."); die("Not enough args.");
if (sscanf(argv[1], "%d", &comp_type) != 1) 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) if (sscanf(argv[2], "%d", &comp_cword) != 1)
die("Strange COMP_CWORD=\"%s\".", argv[2]); die("Strange COMP_CWORD=\"%s\".", argv[2]);
if (sscanf(argv[3], "%d", &comp_point) != 1) if (comp_cword + COMPLETE_ARGC >= argc)
die("Strange COMP_POINT=\"%s\".", argv[3]); die("COMP_CWORD=%d points after end of arg list.", comp_cword);
comp_last = argv[COMPLETE_ARGC + comp_cword];
return; return;
} }
@ -50,7 +51,7 @@ int do_complete(char *cmd) {
char buf[256]; char buf[256];
int res = cmd_complete(cmd, strlen(cmd), buf, (comp_type == 63)); int res = cmd_complete(cmd, strlen(cmd), buf, (comp_type == 63));
if (res == 1) if (res == 1)
printf("%s%s\n", c_now, buf); printf("%s%s\n", comp_now, buf);
return 0; return 0;