]> git.sur5r.net Git - ngadmin/blob - cli/admin.c
Code reorganized.
[ngadmin] / cli / admin.c
1
2 #include <stdio.h>
3
4 #include <readline/readline.h>
5 #include <readline/history.h>
6
7 #include "command.h"
8 #include "common.h"
9
10
11
12
13 #define MAXCOM  8
14
15
16
17
18
19 int cont=1;
20
21
22 static const struct TreeNode rootNode={.sub={
23  &com_quit,
24  &com_login, 
25  &com_scan, 
26  &com_ports, 
27  &com_password, 
28  &com_list, 
29  &com_firmware, 
30  &com_name, 
31  NULL
32 }};
33
34
35
36 void printErrCode (int err) {
37  
38  
39  switch ( err ) {
40   case ERR_OK: /*printf("ok\n");*/ break;
41   case ERR_NET: printf("network error\n"); break;
42   case ERR_NOTLOG: printf("no switch selected\n"); break;
43   case ERR_BADPASS: printf("wrong password\n"); break;
44   case ERR_BADID: printf("bad switch id\n"); break;
45   case ERR_INVARG: printf("invalid argument\n"); break;
46   case ERR_TIMEOUT: printf("timeout\n"); break;
47   default: printf("unknown status code (%i)\n", err);
48  }
49  
50  
51 }
52
53
54
55 void displaySwitchTab (const struct swi_attr *sa, int nb) {
56  
57  int i=0;
58  
59  
60  if ( nb==0 ) {
61   printf("no switch found\n");
62   return;
63  }
64  
65  
66  printf("Num\tMac\t\t\tProduct\t\tName\t\t\tIP/mask\t\t\t\tDHCP\tPorts\tFirmware\n");
67  
68  for (i=0; i<nb; ++i) {
69   printf("%i\t%s\t%s\t\t%s\t%s/", i, ether_ntoa(&sa[i].mac), sa[i].product, sa[i].name, inet_ntoa(sa[i].nc.ip));
70   printf("%s\t%s\t%i\t%s\n", inet_ntoa(sa[i].nc.netmask), ( sa[i].nc.dhcp ? "Yes" : "No" ), sa[i].ports, sa[i].firmware);
71  }
72  
73  printf("\nfound %i switch(es)\n", nb);
74  
75  
76 }
77
78
79
80
81 int main (int argc, char **argv) {
82  
83  char *line, *com[MAXCOM];
84  struct ngadmin *nga=NULL;
85  struct timeval tv;
86  const struct TreeNode *cur, *next, **tab;
87  int i, n;
88  
89  
90  
91  if ( argc<2 ) {
92   printf("Usage: %s <interface>\n", argv[0]);
93   return 1;
94  }
95  
96  memset(com, 0, MAXCOM*sizeof(char*));
97  
98  if ( (nga=ngadmin_init(argv[1]))==NULL ) {
99   fprintf(stderr, "Initialization error\n");
100   goto end;
101  }
102  
103  // set timeout
104  tv.tv_sec=3;
105  tv.tv_usec=0;
106  ngadmin_setTimeout(nga, &tv);
107  
108  
109  rl_bind_key('\t', rl_abort); // disable auto completion
110  
111  
112  while ( cont ) {
113   
114   if ( (line=readline("> "))==NULL ) goto end;
115   trim(line, strlen(line));
116   if ( *line!=0 ) add_history(line);
117   n=explode(line, com, MAXCOM);
118   free(line);
119   
120   
121   i=0;
122   for (next=&rootNode; i<n; ++i) {
123    cur=next;
124    for (tab=cur->sub; (next=*tab)!=NULL && strcmp(next->name, com[i])!=0; ++tab);
125    if ( next==NULL ) break;
126   }
127   
128   
129   if ( i<n ) { // commands left uncompared
130    
131    if ( cur->hasArgs ) { // left "commands" are in fact parameters
132     cur->comfunc(cur, n-i, (const char**)&com[i], nga);
133    } else {
134     if ( i==0 ) {
135      printf("unknown command\n");
136     } else {
137      printf("unknown %s subcommand\n", com[i-1]);
138     }
139    }
140    
141   } else {
142    
143    cur=next;
144    if ( cur->comfunc==NULL ) {
145     // print available subcommands
146     for (tab=cur->sub; (next=*tab)!=NULL; ++tab) {
147      printf("%s ", next->name);
148     }
149     printf("\n");
150     
151    } else { // terminal command
152    
153     cur->comfunc(cur, 0, NULL, nga);
154     
155    }
156   }
157   
158   /*
159   if ( n==0 ) {
160    // nothing: do nothing
161    
162   } else if ( strcmp(com[0], "timeout")==0 ) {
163    // 
164    
165   }
166   */
167   
168   for (i=0; i<MAXCOM; i++) {
169    if ( com[i]!=NULL ) {
170     free(com[i]);
171     com[i]=NULL;
172    }
173   }
174   
175  }
176  
177  
178  end:
179  ngadmin_close(nga);
180  
181  
182  return 0;
183  
184 }
185
186