From 9e2ae03d1dc3b0d9a32a748320b9ed1565fa1374 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Tue, 12 Mar 2013 11:28:40 +0100 Subject: [PATCH] Fix completion of add/del/get/set commands. --- bash_completion.d/tinc | 9 +++---- src/tincctl.c | 59 ++++++++++++++++-------------------------- 2 files changed, 26 insertions(+), 42 deletions(-) diff --git a/bash_completion.d/tinc b/bash_completion.d/tinc index dd41874c..4c64fa24 100644 --- a/bash_completion.d/tinc +++ b/bash_completion.d/tinc @@ -5,7 +5,7 @@ _tinc() { prev="${COMP_WORDS[COMP_CWORD-1]}" opts="-c -d -D -K -n -o -L -R -U --config --no-detach --debug --net --option --mlock --logfile --pidfile --chroot --user --help --version" confvars="Address AddressFamily BindToAddress BindToInterface Broadcast Cipher ClampMSS Compression ConnectTo DecrementTTL Device DeviceType Digest DirectOnly ECDSAPrivateKeyFile ECDSAPublicKey ECDSAPublicKeyFile ExperimentalProtocol Forwarding GraphDumpFile Hostnames IffOneQueue IndirectData Interface KeyExpire LocalDiscovery MACExpire MACLength MaxOutputBufferSize MaxTimeout Mode Name PMTU PMTUDiscovery PingInterval PingTimeout Port PriorityInheritance PrivateKeyFile ProcessPriority Proxy PublicKeyFile ReplayWindow StrictSubnets Subnet TCPOnly TunnelServer UDPRcvBuf UDPSndBuf VDEGroup VDEPort Weight" - commands="config connect debug disconnect dump edit export export-all generate-ecdsa-keys generate-keys generate-rsa-keys help import info init log pcap pid purge reload restart retry start stop top version" + commands="add connect debug del disconnect dump edit export export-all generate-ecdsa-keys generate-keys generate-rsa-keys get help import info init log pcap pid purge reload restart retry set start stop top version" case ${prev} in -c|--config) @@ -53,12 +53,9 @@ _tinc() { fi COMPREPLY=( $(compgen -W "${commands}" -- ${cur}) ) case $prev in - config) - COMPREPLY=( $(compgen -W "get set add del ${confvars}" -- ${cur}) ) - return 0 - ;; get|set|add|del) COMPREPLY=( $(compgen -W "${confvars}" -- ${cur}) ) + return 0 ;; dump|reachable) COMPREPLY=( $(compgen -W "reachable nodes edges subnets connections graph" -- ${cur}) ) @@ -80,4 +77,4 @@ _tincctl() { } complete -F _tincd tincd -complete -F _tincctl tincctl +complete -F _tincctl tinc diff --git a/src/tincctl.c b/src/tincctl.c index cb9740ef..e022cdd5 100644 --- a/src/tincctl.c +++ b/src/tincctl.c @@ -1974,6 +1974,7 @@ static int cmd_exchange_all(int argc, char *argv[]) { static const struct { const char *command; int (*function)(int argc, char *argv[]); + bool hidden; } commands[] = { {"start", cmd_start}, {"stop", cmd_stop}, @@ -1989,13 +1990,11 @@ static const struct { {"pcap", cmd_pcap}, {"log", cmd_log}, {"pid", cmd_pid}, - {"config", cmd_config}, + {"config", cmd_config, true}, {"add", cmd_config}, {"del", cmd_config}, {"get", cmd_config}, {"set", cmd_config}, - {"change", cmd_config}, - {"replace", cmd_config}, {"init", cmd_init}, {"generate-keys", cmd_generate_keys}, {"generate-rsa-keys", cmd_generate_rsa_keys}, @@ -2022,7 +2021,7 @@ static char *complete_command(const char *text, int state) { i++; while(commands[i].command) { - if(!strncasecmp(commands[i].command, text, strlen(text))) + if(!commands[i].hidden && !strncasecmp(commands[i].command, text, strlen(text))) return xstrdup(commands[i].command); i++; } @@ -2049,42 +2048,24 @@ static char *complete_dump(const char *text, int state) { } static char *complete_config(const char *text, int state) { - const char *sub[] = {"get", "set", "add", "del"}; static int i; - if(!state) { + + if(!state) i = 0; - if(!strchr(rl_line_buffer + 7, ' ')) - i = -4; - else { - bool found = false; - for(int i = 0; i < 4; i++) { - if(!strncasecmp(rl_line_buffer + 7, sub[i], strlen(sub[i])) && rl_line_buffer[7 + strlen(sub[i])] == ' ') { - found = true; - break; - } - } - if(!found) - return NULL; - } - } else { + else i++; - } - while(i < 0 || variables[i].name) { - if(i < 0 && !strncasecmp(sub[i + 4], text, strlen(text))) - return xstrdup(sub[i + 4]); - if(i >= 0) { - char *dot = strchr(text, '.'); - if(dot) { - if((variables[i].type & VAR_HOST) && !strncasecmp(variables[i].name, dot + 1, strlen(dot + 1))) { - char *match; - xasprintf(&match, "%.*s.%s", dot - text, text, variables[i].name); - return match; - } - } else { - if(!strncasecmp(variables[i].name, text, strlen(text))) - return xstrdup(variables[i].name); + while(variables[i].name) { + char *dot = strchr(text, '.'); + if(dot) { + if((variables[i].type & VAR_HOST) && !strncasecmp(variables[i].name, dot + 1, strlen(dot + 1))) { + char *match; + xasprintf(&match, "%.*s.%s", dot - text, text, variables[i].name); + return match; } + } else { + if(!strncasecmp(variables[i].name, text, strlen(text))) + return xstrdup(variables[i].name); } i++; } @@ -2137,7 +2118,13 @@ static char **completion (const char *text, int start, int end) { matches = rl_completion_matches(text, complete_command); else if(!strncasecmp(rl_line_buffer, "dump ", 5)) matches = rl_completion_matches(text, complete_dump); - else if(!strncasecmp(rl_line_buffer, "config ", 7)) + else if(!strncasecmp(rl_line_buffer, "add ", 4)) + matches = rl_completion_matches(text, complete_config); + else if(!strncasecmp(rl_line_buffer, "del ", 4)) + matches = rl_completion_matches(text, complete_config); + else if(!strncasecmp(rl_line_buffer, "get ", 4)) + matches = rl_completion_matches(text, complete_config); + else if(!strncasecmp(rl_line_buffer, "set ", 4)) matches = rl_completion_matches(text, complete_config); else if(!strncasecmp(rl_line_buffer, "info ", 5)) matches = rl_completion_matches(text, complete_info); -- 2.39.5