12 #ifdef HAVE_LIBREADLINE
13 #include <readline/readline.h>
14 #include <readline/history.h>
25 static const struct TreeNode* getSubCom (char **com, int n, int *t)
28 const struct TreeNode *cur, *next;
32 for (i = 0; i < n; i++) {
33 /* we have reached a terminal command, exit */
37 /* search sub command in sub command array */
38 for (next = cur->sub; next->name != NULL && strcmp(next->name, com[i]) != 0; next++);
40 /* sub command not found, exit */
41 if (next->name == NULL)
44 /* next command is now the current one */
55 #ifdef HAVE_LIBREADLINE
56 static const struct TreeNode *compcur;
59 static char* my_generator (const char* text, int state)
62 static const struct TreeNode *tn;
66 if (compcur == NULL) {
67 /* sub command not found */
69 } else if (state == 0) {
74 if (tn == NULL) /* terminal command */
77 while ((name = tn++->name) != NULL) {
78 if (strncmp(name, text, len) == 0)
87 static char** my_completion (const char *text, int start, int end UNUSED)
89 char **matches = NULL;
90 char *line, *com[MAXCOM];
94 memset(com, 0, MAXCOM * sizeof(char*));
95 line = strdup(rl_line_buffer);
98 n = explode(line, com, MAXCOM);
101 compcur = getSubCom(com, n, &i);
105 matches = rl_completion_matches(text, my_generator);
107 for (i = 0; com[i] != NULL; i++)
113 #endif /* HAVE_LIBREADLINE */
116 int main_loop_continue;
117 static struct ngadmin *nga;
118 static sigjmp_buf jmpbuf;
119 static struct termios orig_term;
120 struct termios current_term;
124 NORET static void handler (int sig)
130 printf("interrupt\n");
132 current_term.c_lflag |= ECHO;
133 tcsetattr(STDIN_FILENO, TCSANOW, ¤t_term);
135 if (!batch && main_loop_continue)
136 siglongjmp(jmpbuf, 1);
141 tcsetattr(STDIN_FILENO, TCSANOW, &orig_term);
148 static int pre_login (const struct ether_addr *mac, int retries)
150 const struct swi_attr *sa;
154 for (i = 1; retries <= 0 || i <= retries; i++) {
158 err = ngadmin_scan(nga);
164 /* search switch with requested MAC */
165 sa = ngadmin_getSwitchTab(nga, &n);
167 if (memcmp(mac, &sa[n].mac, ETH_ALEN) == 0)
172 printf("no switch found\n");
185 err = ngadmin_login(nga, n);
195 int main (int argc, char **argv)
197 static const struct option opts[] = {
198 {"batch", no_argument, NULL, 'a'},
199 {"keep-broadcasting", no_argument, NULL, 'b'},
200 {"force-interface", no_argument, NULL, 'f'},
201 {"help", no_argument, NULL, 'h'},
202 {"interface", required_argument, NULL, 'i'},
203 {"local-broadcast", no_argument, NULL, 'l'},
204 {"mac", required_argument, NULL, 'm'},
205 {"password", required_argument, NULL, 'p'},
206 {"retries", required_argument, NULL, 'r'},
207 {"timeout", required_argument, NULL, 't'},
210 char *line, *com[MAXCOM];
211 const char *iface = "eth0", *password = NULL;
213 bool kb = false, force = false, global = true;
215 const struct TreeNode *cur, *next;
216 struct ether_addr *mac = NULL;
217 int i, n, retries = 3;
220 tcgetattr(STDIN_FILENO, &orig_term);
221 current_term = orig_term;
222 #ifdef HAVE_LIBREADLINE
230 while ((n = getopt_long(argc, argv, "abfhi:lm:p:r:t:", opts, NULL)) != -1) {
246 printf("usage: %s [-a] [-b] [-f] [-g] [-i <interface>] [-m <MAC>] [-p <password>]\n", argv[0]);
258 mac = ether_aton(optarg);
260 printf("invalid MAC\n");
270 retries = strtol(optarg, NULL, 0);
274 timeout = strtof(optarg, NULL);
278 printf("unknown option: \"%s\"\n", argv[optind - 1]);
287 printf("unknown trailing options\n");
292 memset(com, 0, MAXCOM * sizeof(char*));
294 nga = ngadmin_init(iface);
296 fprintf(stderr, "initialization error\n");
302 tv.tv_sec = (int)timeout;
303 tv.tv_usec = (int)((timeout - (float)tv.tv_sec) * 1.e6f);
304 ngadmin_setTimeout(nga, &tv);
308 if (ngadmin_setKeepBroadcasting(nga, kb) != ERR_OK)
311 if (force && ngadmin_forceInterface(nga) != ERR_OK)
314 if (ngadmin_useGlobalBroadcast(nga, global) != ERR_OK)
317 /* non-TTY inputs are automatically set to batch mode */
318 if (!isatty(STDIN_FILENO))
321 if (password != NULL)
322 ngadmin_setPassword(nga, password);
324 signal(SIGTERM, handler);
325 signal(SIGINT, handler);
327 /* automatic scan & login when switch MAC is specified on the command line */
328 if (mac != NULL && pre_login(mac, retries) != 0)
332 /* in batch mode, we must be logged to continue */
333 if (ngadmin_getCurrentSwitch(nga) == NULL) {
334 printf("must be logged\n");
338 #ifdef HAVE_LIBREADLINE
339 /* initialize readline functions */
340 rl_attempted_completion_function = my_completion;
341 rl_completion_entry_function = my_generator;
343 sigsetjmp(jmpbuf, 1);
347 main_loop_continue = 1;
348 while (main_loop_continue) {
349 /* read user input */
353 n = getline(&line, (size_t*)&i, stdin);
354 #ifdef HAVE_LIBREADLINE
356 line = readline("> ");
358 if (n < 0 || line == NULL)
361 /* split string into words */
362 trim(line, strlen(line));
363 n = explode(line, com, MAXCOM);
369 #ifdef HAVE_LIBREADLINE
376 cur = getSubCom(com, n, &i);
378 if (cur->sub != NULL) {
379 /* not terminal command */
382 printf("unknown command: %s\n", com[i]);
384 /* intermediate command, remaining string */
385 printf("unknown %s subcommand: %s\n", com[i - 1], com[i]);
387 /* intermediate command, no remaining string */
388 /* print available subcommands */
389 for (next = cur->sub; next->name != NULL; next++)
390 printf("%s ", next->name);
393 } else if (cur->comfunc == NULL) {
394 /* erroneous terminal command without function */
395 printf("terminal command without function\n");
397 /* execute terminal command */
398 cur->comfunc(n - i, (const char**)&com[i], nga);
401 for (i = 0; com[i] != NULL; i++) {