12 #ifdef HAVE_LIBREADLINE
13 #include <readline/readline.h>
14 #include <readline/history.h>
24 int main_loop_continue;
27 static const struct TreeNode* getSubCom (char **com, int n, int *t)
30 const struct TreeNode *cur, *next;
34 for (i = 0; i < n; i++) {
35 /* we have reached a terminal command, exit */
39 /* search sub command in sub command array */
40 for (next = cur->sub; next->name != NULL && strcmp(next->name, com[i]) != 0; next++);
42 /* sub command not found, exit */
43 if (next->name == NULL)
46 /* next command is now the current one */
57 #ifdef HAVE_LIBREADLINE
58 static const struct TreeNode *compcur;
61 static char* my_generator (const char* text, int state)
64 static const struct TreeNode *tn;
68 if (compcur == NULL) {
69 /* sub command not found */
71 } else if (state == 0) {
76 if (tn == NULL) /* terminal command */
79 while ((name = tn++->name) != NULL) {
80 if (strncmp(name, text, len) == 0)
89 static char** my_completion (const char *text, int start, int end UNUSED)
91 char **matches = NULL;
92 char *line, *com[MAXCOM];
96 memset(com, 0, MAXCOM * sizeof(char*));
97 line = strdup(rl_line_buffer);
100 n = explode(line, com, MAXCOM);
103 compcur = getSubCom(com, n, &i);
107 matches = rl_completion_matches(text, my_generator);
109 for (i = 0; com[i] != NULL; i++)
115 #endif /* HAVE_LIBREADLINE */
118 static struct ngadmin *nga;
119 static sigjmp_buf jmpbuf;
120 static struct termios orig_term;
121 struct termios current_term;
125 NORET static void handler (int sig)
131 printf("interrupt\n");
133 current_term.c_lflag |= ECHO;
134 tcsetattr(STDIN_FILENO, TCSANOW, ¤t_term);
137 siglongjmp(jmpbuf, 1);
142 tcsetattr(STDIN_FILENO, TCSANOW, &orig_term);
149 static int pre_login (const struct ether_addr *mac, int retries)
151 const struct swi_attr *sa;
155 for (i = 1; retries <= 0 || i <= retries; i++) {
159 err = ngadmin_scan(nga);
165 /* search switch with requested MAC */
166 sa = ngadmin_getSwitchTab(nga, &n);
168 if (memcmp(mac, &sa[n].mac, ETH_ALEN) == 0)
173 printf("no switch found\n");
186 err = ngadmin_login(nga, n);
196 int main (int argc, char **argv)
198 static const struct option opts[] = {
199 {"batch", no_argument, NULL, 'a'},
200 {"keep-broadcasting", no_argument, NULL, 'b'},
201 {"force-interface", no_argument, NULL, 'f'},
202 {"global-broadcast", no_argument, NULL, 'g'},
203 {"help", no_argument, NULL, 'h'},
204 {"interface", required_argument, NULL, 'i'},
205 {"mac", required_argument, NULL, 'm'},
206 {"password", required_argument, NULL, 'p'},
207 {"retries", required_argument, NULL, 'r'},
208 {"timeout", required_argument, NULL, 't'},
211 char *line, *com[MAXCOM];
212 const char *iface = "eth0", *password = NULL;
214 bool kb = false, force = false, global = false;
216 const struct TreeNode *cur, *next;
217 struct ether_addr *mac = NULL;
218 int i, n, retries = 3;
221 tcgetattr(STDIN_FILENO, &orig_term);
222 current_term = orig_term;
223 #ifdef HAVE_LIBREADLINE
231 while ((n = getopt_long(argc, argv, "abfghi:m:p:r:t:", opts, NULL)) != -1) {
251 printf("usage: %s [-a] [-b] [-f] [-g] [-i <interface>] [-m <MAC>] [-p <password>]\n", argv[0]);
259 mac = ether_aton(optarg);
261 printf("invalid MAC\n");
271 retries = strtol(optarg, NULL, 0);
275 timeout = strtof(optarg, NULL);
279 printf("unknown option: \"%s\"\n", argv[optind - 1]);
288 printf("unknown trailing options\n");
293 memset(com, 0, MAXCOM * sizeof(char*));
295 nga = ngadmin_init(iface);
297 fprintf(stderr, "initialization error\n");
303 tv.tv_sec = (int)timeout;
304 tv.tv_usec = (int)((timeout - (float)tv.tv_sec) * 1.e6f);
305 ngadmin_setTimeout(nga, &tv);
309 if (kb && ngadmin_setKeepBroadcasting(nga, true) != ERR_OK)
312 if (force && ngadmin_forceInterface(nga) != ERR_OK)
315 if (global && ngadmin_useGlobalBroadcast(nga, true) != ERR_OK)
318 /* non-TTY inputs are automatically set to batch mode */
319 if (!isatty(STDIN_FILENO))
322 if (password != NULL)
323 ngadmin_setPassword(nga, password);
325 signal(SIGTERM, handler);
326 signal(SIGINT, handler);
328 /* automatic scan & login when switch MAC is specified on the command line */
329 if (mac != NULL && pre_login(mac, retries) != 0)
333 #ifdef HAVE_LIBREADLINE
334 /* initialize readline functions */
335 rl_attempted_completion_function = my_completion;
336 rl_completion_entry_function = my_generator;
338 sigsetjmp(jmpbuf, 1);
342 main_loop_continue = 1;
344 while (main_loop_continue) {
345 /* read user input */
349 n = getline(&line, (size_t*)&i, stdin);
350 #ifdef HAVE_LIBREADLINE
352 line = readline("> ");
354 if (n < 0 || line == NULL)
357 /* split string into words */
358 trim(line, strlen(line));
359 n = explode(line, com, MAXCOM);
365 #ifdef HAVE_LIBREADLINE
372 cur = getSubCom(com, n, &i);
374 if (cur->sub != NULL) {
375 /* not terminal command */
378 printf("unknown command: %s\n", com[i]);
380 /* intermediate command, remaining string */
381 printf("unknown %s subcommand: %s\n", com[i - 1], com[i]);
383 /* intermediate command, no remaining string */
384 /* print available subcommands */
385 for (next = cur->sub; next->name != NULL; next++)
386 printf("%s ", next->name);
389 } else if (cur->comfunc == NULL) {
390 /* erroneous terminal command without function */
391 printf("terminal command without function\n");
393 /* execute terminal command */
394 cur->comfunc(n - i, (const char**)&com[i], nga);
397 for (i = 0; com[i] != NULL; i++) {