]> git.sur5r.net Git - ngadmin/blob - cli/src/admin.c
Merge branch 'autotools'
[ngadmin] / cli / src / admin.c
1
2 #ifdef HAVE_CONFIG_H
3 #include "config.h"
4 #endif
5
6 #include <stdio.h>
7 #include <signal.h>
8 #include <unistd.h>
9 #include <setjmp.h>
10
11 #include <getopt.h>
12 #ifdef HAVE_LIBREADLINE
13 #include <readline/readline.h>
14 #include <readline/history.h>
15 #endif
16
17 #include "common.h"
18 #include "commands.h"
19
20
21 #define MAXCOM  32
22
23
24
25 static const struct TreeNode* getSubCom (char **com, int n, int *t)
26 {
27         int i;
28         const struct TreeNode *cur, *next;
29         
30         
31         cur = &commands;
32         for (i = 0; i < n; i++) {
33                 /* we have reached a terminal command, exit */
34                 if (cur->sub == NULL)
35                         break;
36                 
37                 /* search sub command in sub command array */
38                 for (next = cur->sub; next->name != NULL && strcmp(next->name, com[i]) != 0; next++);
39                 
40                 /* sub command not found, exit */
41                 if (next->name == NULL)
42                         break;
43                 
44                 /* next command is now the current one */
45                 cur = next;
46         }
47         
48         *t = i;
49         
50         
51         return cur;
52 }
53
54
55 #ifdef HAVE_LIBREADLINE
56 static const struct TreeNode *compcur;
57
58
59 static char* my_generator (const char* text, int state)
60 {
61         static int len;
62         static const struct TreeNode *tn;
63         const char *name;
64         
65         
66         if (compcur == NULL) {
67                 /* sub command not found */
68                 return NULL;
69         } else if (state == 0) {
70                 tn = compcur->sub;
71                 len = strlen(text);
72         }
73         
74         if (tn == NULL) /* terminal command */
75                 return NULL;
76         
77         while ((name = tn++->name) != NULL) {
78                 if (strncmp(name, text, len) == 0)
79                         return strdup(name);
80         }
81         
82         
83         return NULL;
84 }
85
86
87 static char** my_completion (const char *text, int start, int end UNUSED)
88 {
89         char **matches = NULL;
90         char *line, *com[MAXCOM];
91         int i, n;
92         
93         
94         memset(com, 0, MAXCOM * sizeof(char*));
95         line = strdup(rl_line_buffer);
96         line[start] = '\0';
97         trim(line, start);
98         n = explode(line, com, MAXCOM);
99         free(line);
100         
101         compcur = getSubCom(com, n, &i);
102         
103         if (i < n)
104                 compcur = NULL;
105         matches = rl_completion_matches(text, my_generator);
106         
107         for (i = 0; com[i] != NULL; i++)
108                 free(com[i]);
109         
110         
111         return matches;
112 }
113 #endif /* HAVE_LIBREADLINE */
114
115
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;
121 static bool batch;
122
123
124 NORET static void handler (int sig)
125 {
126         switch (sig) {
127         
128         case SIGTERM:
129         case SIGINT:
130                 printf("interrupt\n");
131                 
132                 current_term.c_lflag |= ECHO;
133                 tcsetattr(STDIN_FILENO, TCSANOW, &current_term);
134                 
135                 if (!batch && main_loop_continue)
136                         siglongjmp(jmpbuf, 1);
137         
138         default:
139                 ngadmin_close(nga);
140                 
141                 tcsetattr(STDIN_FILENO, TCSANOW, &orig_term);
142                 
143                 exit(0);
144         }
145 }
146
147
148 static int pre_login (const struct ether_addr *mac, int retries)
149 {
150         const struct swi_attr *sa;
151         int i, n, err;
152         
153         
154         for (i = 1; retries <= 0 || i <= retries; i++) {
155                 /* scan */
156                 printf("scan... ");
157                 fflush(stdout);
158                 err = ngadmin_scan(nga);
159                 if (err < 0) {
160                         printErrCode(err);
161                         return err;
162                 }
163                 
164                 /* search switch with requested MAC */
165                 sa = ngadmin_getSwitchTab(nga, &n);
166                 while (--n >= 0) {
167                         if (memcmp(mac, &sa[n].mac, ETH_ALEN) == 0)
168                                 break;
169                 }
170         
171                 if (n < 0) {
172                         printf("no switch found\n");
173                 } else {
174                         printf("done\n");
175                         break;
176                 }
177         }
178         
179         if (n < 0)
180                 return 1;
181
182         /* login */
183         printf("login... ");
184         fflush(stdout);
185         err = ngadmin_login(nga, n);
186         if (err < 0)
187                 printErrCode(err);
188         else
189                 printf("done\n");
190         
191         return err;
192 }
193
194
195 int main (int argc, char **argv)
196 {
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'},
208                 {0, 0, 0, 0}
209         };
210         char *line, *com[MAXCOM];
211         const char *iface = "eth0", *password = NULL;
212         float timeout = 0.f;
213         bool kb = false, force = false, global = true;
214         struct timeval tv;
215         const struct TreeNode *cur, *next;
216         struct ether_addr *mac = NULL;
217         int i, n, retries = 3;
218         
219         
220         tcgetattr(STDIN_FILENO, &orig_term);
221         current_term = orig_term;
222 #ifdef HAVE_LIBREADLINE
223         batch = false;
224 #else
225         batch = true;
226 #endif
227         
228         opterr = 0;
229         
230         while ((n = getopt_long(argc, argv, "abfhi:lm:p:r:t:", opts, NULL)) != -1) {
231                 switch (n) {
232                 
233                 case 'a':
234                         batch = true;
235                         break;
236                 
237                 case 'b':
238                         kb = true;
239                         break;
240                 
241                 case 'f':
242                         force = true;
243                         break;
244                 
245                 case 'h':
246                         printf("usage: %s [-a] [-b] [-f] [-g] [-i <interface>] [-m <MAC>] [-p <password>]\n", argv[0]);
247                         goto end;
248                 
249                 case 'i':
250                         iface = optarg;
251                         break;
252                 
253                 case 'l':
254                         global = false;
255                         break;
256                 
257                 case 'm':
258                         mac = ether_aton(optarg);
259                         if (mac == NULL) {
260                                 printf("invalid MAC\n");
261                                 goto end;
262                         }
263                         break;
264                 
265                 case 'p':
266                         password = optarg;
267                         break;
268                 
269                 case 'r':
270                         retries = strtol(optarg, NULL, 0);
271                         break;
272                 
273                 case 't':
274                         timeout = strtof(optarg, NULL);
275                         break;
276                 
277                 case '?':
278                         printf("unknown option: \"%s\"\n", argv[optind - 1]);
279                         goto end;
280                 }
281         }
282         
283         argc -= optind;
284         argv += optind;
285         
286         if (argc != 0) {
287                 printf("unknown trailing options\n");
288                 goto end;
289         }
290         
291         
292         memset(com, 0, MAXCOM * sizeof(char*));
293         
294         nga = ngadmin_init(iface);
295         if (nga == NULL) {
296                 fprintf(stderr, "initialization error\n");
297                 goto end;
298         }
299         
300         /* set timeout */
301         if (timeout > 0.f) {
302                 tv.tv_sec = (int)timeout;
303                 tv.tv_usec = (int)((timeout - (float)tv.tv_sec) * 1.e6f);
304                 ngadmin_setTimeout(nga, &tv);
305         }
306         
307         
308         if (ngadmin_setKeepBroadcasting(nga, kb) != ERR_OK)
309                 goto end;
310         
311         if (force && ngadmin_forceInterface(nga) != ERR_OK)
312                 goto end;
313         
314         if (ngadmin_useGlobalBroadcast(nga, global) != ERR_OK)
315                 goto end;
316         
317         /* non-TTY inputs are automatically set to batch mode */
318         if (!isatty(STDIN_FILENO))
319                 batch = true;
320         
321         if (password != NULL)
322                 ngadmin_setPassword(nga, password);
323         
324         signal(SIGTERM, handler);
325         signal(SIGINT, handler);
326         
327         /* automatic scan & login when switch MAC is specified on the command line */
328         if (mac != NULL && pre_login(mac, retries) != 0)
329                 goto end;
330         
331         if (batch) {
332                 /* in batch mode, we must be logged to continue */
333                 if (ngadmin_getCurrentSwitch(nga) == NULL) {
334                         printf("must be logged\n");
335                         goto end;
336                 }
337         } else {
338 #ifdef HAVE_LIBREADLINE
339                 /* initialize readline functions */
340                 rl_attempted_completion_function = my_completion;
341                 rl_completion_entry_function = my_generator;
342                 
343                 sigsetjmp(jmpbuf, 1);
344 #endif
345         }
346         
347         main_loop_continue = 1;
348         while (main_loop_continue) {
349                 /* read user input */
350                 line = NULL;
351                 n = 0;
352                 if (batch)
353                         n = getline(&line, (size_t*)&i, stdin);
354 #ifdef HAVE_LIBREADLINE
355                 else
356                         line = readline("> ");
357 #endif
358                 if (n < 0 || line == NULL)
359                         goto end;
360                 
361                 /* split string into words */
362                 trim(line, strlen(line));
363                 n = explode(line, com, MAXCOM);
364                 
365                 if (n == 0) {
366                         free(line);
367                         continue;
368                 } else {
369 #ifdef HAVE_LIBREADLINE
370                         if (!batch)
371                                 add_history(line);
372 #endif
373                         free(line);
374                 }
375                 
376                 cur = getSubCom(com, n, &i);
377                 
378                 if (cur->sub != NULL) {
379                         /* not terminal command */
380                         if (i == 0) {
381                                 /* root command */
382                                 printf("unknown command: %s\n", com[i]);
383                         } else if (i < n) {
384                                 /* intermediate command, remaining string */
385                                 printf("unknown %s subcommand: %s\n", com[i - 1], com[i]);
386                         } else {
387                                 /* intermediate command, no remaining string */
388                                 /* print available subcommands */
389                                 for (next = cur->sub; next->name != NULL; next++)
390                                         printf("%s ", next->name);
391                                 putchar('\n');
392                         }
393                 } else if (cur->comfunc == NULL) {
394                         /* erroneous terminal command without function */
395                         printf("terminal command without function\n");
396                 } else {
397                         /* execute terminal command */
398                         cur->comfunc(n - i, (const char**)&com[i], nga);
399                 }
400                 
401                 for (i = 0; com[i] != NULL; i++) {
402                         free(com[i]);
403                         com[i] = NULL;
404                 }
405         }
406         
407 end:
408         handler(0);
409 }
410
411