]> git.sur5r.net Git - ngadmin/blob - cli/common.c
4a5315c914aa879c395b0cc9157c137702cd29aa
[ngadmin] / cli / common.c
1
2 #include "common.h"
3
4
5
6
7 void printErrCode (int err) {
8  
9  
10  switch ( err ) {
11   case ERR_OK: /*printf("ok\n");*/ break;
12   case ERR_NET: printf("network error\n"); break;
13   case ERR_NOTLOG: printf("no switch selected\n"); break;
14   case ERR_DENIED: printf("access denied\n"); break;
15   case ERR_BADPASS: printf("wrong password\n"); break;
16   case ERR_BADID: printf("bad switch id\n"); break;
17   case ERR_INVARG: printf("invalid argument\n"); break;
18   case ERR_TIMEOUT: printf("timeout\n"); break;
19   case ERR_NOTIMPL: printf("not implemented\n"); break;
20   default: printf("unknown status code (%i)\n", err);
21  }
22  
23  
24 }
25
26
27
28
29 const char* bitrates[]={
30  "nl", 
31  "512K", 
32  "1M", 
33  "2M",  
34  "4M", 
35  "8M", 
36  "16M", 
37  "32M", 
38  "64M", 
39  "128M", 
40  "256M", 
41  "512M", 
42  NULL
43 };
44
45
46 const char* prio[]={
47  NULL, 
48  "high", 
49  "medium", 
50  "normal", 
51  "low", 
52  NULL
53 };
54
55
56
57
58
59
60 int parseBitrate (const char *s) {
61  
62  int i;
63  
64  
65  for (i=0; bitrates[i]!=NULL && strcasecmp(bitrates[i], s)!=0; ++i);
66  
67  
68  return i;
69  
70 }
71
72
73
74 char parsePrio (const char *s) {
75  
76  int i;
77  
78  
79  for (i=1; prio[i]!=NULL && strcasecmp(prio[i], s)!=0; ++i);
80  
81  
82  return (char)i;
83  
84 }
85
86
87
88 void displaySwitchTab (const struct swi_attr *sa, int nb) {
89  
90  int i=0;
91  
92  
93  if ( nb==0 ) {
94   printf("no switch found\n");
95   return;
96  }
97  
98  
99  printf("Num\tMac\t\t\tProduct\t\tName\t\t\tIP/mask\t\t\tDHCP\tPorts\tFirmware\n");
100  
101  for (i=0; i<nb; ++i) {
102   printf("%i\t%s\t%s\t%s\t\t%s/", i, ether_ntoa(&sa[i].mac), sa[i].product, sa[i].name, inet_ntoa(sa[i].nc.ip));
103   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);
104  }
105  
106  printf("\nfound %i switch(es)\n", nb);
107  
108  
109 }
110
111
112
113 // ----------------------------
114 int trim (char *txt, int start) {
115  
116  char *p, c;
117  
118  
119  if ( txt==NULL ) {
120   return 0;
121  }
122  
123  //for (p=txt; *p!=0; p++);
124  p=txt+start;
125  for (p--; p>=txt && ( (c=*p)==' ' || c=='\n' ); *p--=0);
126  
127  
128  return p-txt+1;
129  
130 }
131
132
133
134 // --------------------------------------------------------
135 int explode (const char *commande, char** tab, int maximum) {
136  
137  const char *start, *end;
138  char c;
139  int n=0, len;
140  
141  
142  for (end=commande; ; n++) {
143   
144   for (start=end; (c=*start)==' ' && c!=0; start++);
145   for (end=start; ( (c=*end)!=' ' || n>=maximum-1 ) && c!=0; end++);
146   
147   if ( (len=end-start)==0 ) {
148    break;
149   }
150   
151   tab[n]=malloc(sizeof(char)*(len+1));
152   memcpy(tab[n], start, len);
153   tab[n][len]=0;
154   
155  }
156  
157  
158  return n;
159  
160 }
161
162