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 *line, *com[MAXCOM];
93 memset(com, 0, MAXCOM * sizeof(char*));
94 line = strdup(rl_line_buffer);
97 n = explode(line, com, MAXCOM);
100 compcur = getSubCom(com, n, &i);
101 for (i = 0; com[i] != NULL; i++)
104 if (i < n) /* unknown command */
106 else if (compcur->sub == NULL) /* terminal command */
107 return rl_completion_matches(text, rl_filename_completion_function);
108 else /* intermediate command */
109 return rl_completion_matches(text, my_generator);
111 #endif /* HAVE_LIBREADLINE */
114 int main_loop_continue;
115 static struct ngadmin *nga;
116 static sigjmp_buf jmpbuf;
117 static struct termios orig_term;
118 struct termios current_term;
122 NORET static void handler (int sig)
128 printf("interrupt\n");
130 current_term.c_lflag |= ECHO;
131 tcsetattr(STDIN_FILENO, TCSANOW, ¤t_term);
133 if (!batch && main_loop_continue)
134 siglongjmp(jmpbuf, 1);
139 tcsetattr(STDIN_FILENO, TCSANOW, &orig_term);
146 static int pre_login (const struct ether_addr *mac, int retries)
148 const struct swi_attr *sa;
152 for (i = 1; retries <= 0 || i <= retries; i++) {
156 err = ngadmin_scan(nga);
162 /* search switch with requested MAC */
163 sa = ngadmin_getSwitchTab(nga, &n);
165 if (memcmp(mac, &sa[n].mac, ETH_ALEN) == 0)
170 printf("no switch found\n");
183 err = ngadmin_login(nga, n);
193 int main (int argc, char **argv)
195 static const struct option opts[] = {
196 {"batch", no_argument, NULL, 'a'},
197 {"keep-broadcasting", no_argument, NULL, 'b'},
198 {"force-interface", no_argument, NULL, 'f'},
199 {"help", no_argument, NULL, 'h'},
200 {"interface", required_argument, NULL, 'i'},
201 {"local-broadcast", no_argument, NULL, 'l'},
202 {"mac", required_argument, NULL, 'm'},
203 {"password", required_argument, NULL, 'p'},
204 {"retries", required_argument, NULL, 'r'},
205 {"timeout", required_argument, NULL, 't'},
208 char *line, *com[MAXCOM];
209 const char *iface = "eth0", *password = NULL;
211 bool kb = false, force = false, global = true;
213 const struct TreeNode *cur, *next;
214 struct ether_addr *mac = NULL;
215 int i, n, retries = 3;
218 tcgetattr(STDIN_FILENO, &orig_term);
219 current_term = orig_term;
220 #ifdef HAVE_LIBREADLINE
228 while ((n = getopt_long(argc, argv, "abfhi:lm:p:r:t:", opts, NULL)) != -1) {
244 printf("usage: %s [-a] [-b] [-f] [-g] [-i <interface>] [-m <MAC>] [-p <password>]\n", argv[0]);
256 mac = ether_aton(optarg);
258 printf("invalid MAC\n");
268 retries = strtol(optarg, NULL, 0);
272 timeout = strtof(optarg, NULL);
276 printf("unknown option: \"%s\"\n", argv[optind - 1]);
285 printf("unknown trailing options\n");
290 memset(com, 0, MAXCOM * sizeof(char*));
292 nga = ngadmin_init(iface);
294 fprintf(stderr, "initialization error\n");
300 tv.tv_sec = (int)timeout;
301 tv.tv_usec = (int)((timeout - (float)tv.tv_sec) * 1.e6f);
302 ngadmin_setTimeout(nga, &tv);
306 if (ngadmin_setKeepBroadcasting(nga, kb) != ERR_OK)
309 if (force && ngadmin_forceInterface(nga) != ERR_OK)
312 if (ngadmin_useGlobalBroadcast(nga, global) != ERR_OK)
315 /* non-TTY inputs are automatically set to batch mode */
316 if (!isatty(STDIN_FILENO))
319 if (password != NULL)
320 ngadmin_setPassword(nga, password);
322 signal(SIGTERM, handler);
323 signal(SIGINT, handler);
325 /* automatic scan & login when switch MAC is specified on the command line */
326 if (mac != NULL && pre_login(mac, retries) != 0)
330 /* in batch mode, we must be logged to continue */
331 if (ngadmin_getCurrentSwitch(nga) == NULL) {
332 printf("must be logged\n");
336 #ifdef HAVE_LIBREADLINE
337 /* initialize readline functions */
338 rl_attempted_completion_function = my_completion;
339 rl_completion_entry_function = my_generator;
341 sigsetjmp(jmpbuf, 1);
345 main_loop_continue = 1;
346 while (main_loop_continue) {
347 /* read user input */
351 n = getline(&line, (size_t*)&i, stdin);
352 #ifdef HAVE_LIBREADLINE
354 line = readline("> ");
356 if (n < 0 || line == NULL)
359 /* split string into words */
360 trim(line, strlen(line));
361 n = explode(line, com, MAXCOM);
367 #ifdef HAVE_LIBREADLINE
374 cur = getSubCom(com, n, &i);
376 if (cur->sub != NULL) {
377 /* not terminal command */
380 printf("unknown command: %s\n", com[i]);
382 /* intermediate command, remaining string */
383 printf("unknown %s subcommand: %s\n", com[i - 1], com[i]);
385 /* intermediate command, no remaining string */
386 /* print available subcommands */
387 for (next = cur->sub; next->name != NULL; next++)
388 printf("%s ", next->name);
391 } else if (cur->comfunc == NULL) {
392 /* erroneous terminal command without function */
393 printf("terminal command without function\n");
395 /* execute terminal command */
396 cur->comfunc(n - i, (const char**)&com[i], nga);
399 for (i = 0; com[i] != NULL; i++) {