Fix completion of add/del/get/set commands.
authorGuus Sliepen <guus@tinc-vpn.org>
Tue, 12 Mar 2013 10:28:40 +0000 (11:28 +0100)
committerGuus Sliepen <guus@tinc-vpn.org>
Tue, 12 Mar 2013 10:28:40 +0000 (11:28 +0100)
bash_completion.d/tinc
src/tincctl.c

index dd41874..4c64fa2 100644 (file)
@@ -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
index cb9740e..e022cdd 100644 (file)
@@ -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);