]> git.sur5r.net Git - ngadmin/blobdiff - cli/src/admin.c
Cli: switch to filename completion after terminal commands
[ngadmin] / cli / src / admin.c
index bb4b310fd721afb234fe10a1cc647bf82c7d61b7..ffc567f1929e5422e7d10a3b3813830d0cf60496 100644 (file)
@@ -1,12 +1,18 @@
 
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
 #include <stdio.h>
 #include <signal.h>
 #include <unistd.h>
 #include <setjmp.h>
 
 #include <getopt.h>
+#ifdef HAVE_LIBREADLINE
 #include <readline/readline.h>
 #include <readline/history.h>
+#endif
 
 #include "common.h"
 #include "commands.h"
@@ -15,8 +21,6 @@
 #define MAXCOM 32
 
 
-int main_loop_continue = 1;
-
 
 static const struct TreeNode* getSubCom (char **com, int n, int *t)
 {
@@ -48,6 +52,7 @@ static const struct TreeNode* getSubCom (char **com, int n, int *t)
 }
 
 
+#ifdef HAVE_LIBREADLINE
 static const struct TreeNode *compcur;
 
 
@@ -81,7 +86,6 @@ static char* my_generator (const char* text, int state)
 
 static char** my_completion (const char *text, int start, int end UNUSED)
 {
-       char **matches = NULL;
        char *line, *com[MAXCOM];
        int i, n;
        
@@ -94,19 +98,20 @@ static char** my_completion (const char *text, int start, int end UNUSED)
        free(line);
        
        compcur = getSubCom(com, n, &i);
-       
-       if (i < n)
-               compcur = NULL;
-       matches = rl_completion_matches(text, my_generator);
-       
        for (i = 0; com[i] != NULL; i++)
                free(com[i]);
        
-       
-       return matches;
+       if (i < n) /* unknown command */
+               return NULL;
+       else if (compcur->sub == NULL) /* terminal command */
+               return rl_completion_matches(text, rl_filename_completion_function);
+       else /* intermediate command */
+               return rl_completion_matches(text, my_generator);
 }
+#endif /* HAVE_LIBREADLINE */
 
 
+int main_loop_continue;
 static struct ngadmin *nga;
 static sigjmp_buf jmpbuf;
 static struct termios orig_term;
@@ -125,7 +130,7 @@ NORET static void handler (int sig)
                current_term.c_lflag |= ECHO;
                tcsetattr(STDIN_FILENO, TCSANOW, &current_term);
                
-               if (!batch)
+               if (!batch && main_loop_continue)
                        siglongjmp(jmpbuf, 1);
        
        default:
@@ -151,7 +156,7 @@ static int pre_login (const struct ether_addr *mac, int retries)
                err = ngadmin_scan(nga);
                if (err < 0) {
                        printErrCode(err);
-                       continue;
+                       return err;
                }
                
                /* search switch with requested MAC */
@@ -191,9 +196,9 @@ int main (int argc, char **argv)
                {"batch", no_argument, NULL, 'a'},
                {"keep-broadcasting", no_argument, NULL, 'b'},
                {"force-interface", no_argument, NULL, 'f'},
-               {"global-broadcast", no_argument, NULL, 'g'},
                {"help", no_argument, NULL, 'h'},
                {"interface", required_argument, NULL, 'i'},
+               {"local-broadcast", no_argument, NULL, 'l'},
                {"mac", required_argument, NULL, 'm'},
                {"password", required_argument, NULL, 'p'},
                {"retries", required_argument, NULL, 'r'},
@@ -203,7 +208,7 @@ int main (int argc, char **argv)
        char *line, *com[MAXCOM];
        const char *iface = "eth0", *password = NULL;
        float timeout = 0.f;
-       bool kb = false, force = false, global = false;
+       bool kb = false, force = false, global = true;
        struct timeval tv;
        const struct TreeNode *cur, *next;
        struct ether_addr *mac = NULL;
@@ -212,11 +217,15 @@ int main (int argc, char **argv)
        
        tcgetattr(STDIN_FILENO, &orig_term);
        current_term = orig_term;
+#ifdef HAVE_LIBREADLINE
        batch = false;
+#else
+       batch = true;
+#endif
        
        opterr = 0;
        
-       while ((n = getopt_long(argc, argv, "abfghi:m:p:r:t:", opts, NULL)) != -1) {
+       while ((n = getopt_long(argc, argv, "abfhi:lm:p:r:t:", opts, NULL)) != -1) {
                switch (n) {
                
                case 'a':
@@ -231,10 +240,6 @@ int main (int argc, char **argv)
                        force = true;
                        break;
                
-               case 'g':
-                       global = true;
-                       break;
-               
                case 'h':
                        printf("usage: %s [-a] [-b] [-f] [-g] [-i <interface>] [-m <MAC>] [-p <password>]\n", argv[0]);
                        goto end;
@@ -243,6 +248,10 @@ int main (int argc, char **argv)
                        iface = optarg;
                        break;
                
+               case 'l':
+                       global = false;
+                       break;
+               
                case 'm':
                        mac = ether_aton(optarg);
                        if (mac == NULL) {
@@ -294,13 +303,13 @@ int main (int argc, char **argv)
        }
        
        
-       if (kb && ngadmin_setKeepBroadcasting(nga, true) != ERR_OK)
+       if (ngadmin_setKeepBroadcasting(nga, kb) != ERR_OK)
                goto end;
        
        if (force && ngadmin_forceInterface(nga) != ERR_OK)
                goto end;
        
-       if (global && ngadmin_useGlobalBroadcast(nga, true) != ERR_OK)
+       if (ngadmin_useGlobalBroadcast(nga, global) != ERR_OK)
                goto end;
        
        /* non-TTY inputs are automatically set to batch mode */
@@ -324,21 +333,26 @@ int main (int argc, char **argv)
                        goto end;
                }
        } else {
+#ifdef HAVE_LIBREADLINE
                /* initialize readline functions */
                rl_attempted_completion_function = my_completion;
                rl_completion_entry_function = my_generator;
                
                sigsetjmp(jmpbuf, 1);
+#endif
        }
        
+       main_loop_continue = 1;
        while (main_loop_continue) {
                /* read user input */
                line = NULL;
                n = 0;
                if (batch)
                        n = getline(&line, (size_t*)&i, stdin);
+#ifdef HAVE_LIBREADLINE
                else
                        line = readline("> ");
+#endif
                if (n < 0 || line == NULL)
                        goto end;
                
@@ -350,8 +364,10 @@ int main (int argc, char **argv)
                        free(line);
                        continue;
                } else {
+#ifdef HAVE_LIBREADLINE
                        if (!batch)
                                add_history(line);
+#endif
                        free(line);
                }