+#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"
#define MAXCOM 32
-int main_loop_continue = 1;
-
static const struct TreeNode* getSubCom (char **com, int n, int *t)
{
}
+#ifdef HAVE_LIBREADLINE
static const struct TreeNode *compcur;
static char** my_completion (const char *text, int start, int end UNUSED)
{
- char **matches = NULL;
char *line, *com[MAXCOM];
int i, n;
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;
current_term.c_lflag |= ECHO;
tcsetattr(STDIN_FILENO, TCSANOW, ¤t_term);
- if (!batch)
+ if (!batch && main_loop_continue)
siglongjmp(jmpbuf, 1);
default:
err = ngadmin_scan(nga);
if (err < 0) {
printErrCode(err);
- continue;
+ return err;
}
/* search switch with requested MAC */
{"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'},
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;
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':
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;
iface = optarg;
break;
+ case 'l':
+ global = false;
+ break;
+
case 'm':
mac = ether_aton(optarg);
if (mac == NULL) {
}
- 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 */
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;
free(line);
continue;
} else {
+#ifdef HAVE_LIBREADLINE
if (!batch)
add_history(line);
+#endif
free(line);
}