4 #include <readline/readline.h>
5 #include <readline/history.h>
22 static const struct TreeNode rootNode={.sub={
36 void printErrCode (int 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);
55 void displaySwitchTab (const struct swi_attr *sa, int nb) {
61 printf("no switch found\n");
66 printf("Num\tMac\t\t\tProduct\t\tName\t\t\tIP/mask\t\t\t\tDHCP\tPorts\tFirmware\n");
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);
73 printf("\nfound %i switch(es)\n", nb);
81 int main (int argc, char **argv) {
83 char *line, *com[MAXCOM];
84 struct ngadmin *nga=NULL;
86 const struct TreeNode *cur, *next, **tab;
92 printf("Usage: %s <interface>\n", argv[0]);
96 memset(com, 0, MAXCOM*sizeof(char*));
98 if ( (nga=ngadmin_init(argv[1]))==NULL ) {
99 fprintf(stderr, "Initialization error\n");
106 ngadmin_setTimeout(nga, &tv);
109 rl_bind_key('\t', rl_abort); // disable auto completion
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);
122 for (next=&rootNode; i<n; ++i) {
124 for (tab=cur->sub; (next=*tab)!=NULL && strcmp(next->name, com[i])!=0; ++tab);
125 if ( next==NULL ) break;
129 if ( i<n ) { // commands left uncompared
131 if ( cur->hasArgs ) { // left "commands" are in fact parameters
132 cur->comfunc(cur, n-i, (const char**)&com[i], nga);
135 printf("unknown command\n");
137 printf("unknown %s subcommand\n", com[i-1]);
144 if ( cur->comfunc==NULL ) {
145 // print available subcommands
146 for (tab=cur->sub; (next=*tab)!=NULL; ++tab) {
147 printf("%s ", next->name);
151 } else { // terminal command
153 cur->comfunc(cur, 0, NULL, nga);
160 // nothing: do nothing
162 } else if ( strcmp(com[0], "timeout")==0 ) {
168 for (i=0; i<MAXCOM; i++) {
169 if ( com[i]!=NULL ) {