]> git.sur5r.net Git - ngadmin/blob - cli/admin.c
CLI: reunited commands in one file.
[ngadmin] / cli / admin.c
1
2 #include <stdio.h>
3
4 #include <readline/readline.h>
5 #include <readline/history.h>
6
7 #include "common.h"
8 #include "commands.h"
9
10
11
12
13 #define MAXCOM  32
14
15
16 int cont=1;
17
18
19
20
21 const struct TreeNode* getSubCom (char **com, int n, int *t) {
22  
23  int i;
24  const struct TreeNode *cur, *next;
25  
26  
27  cur=&coms;
28  for (i=0; i<n; ++i) {
29   
30   // we have reached a terminal command, exit
31   if ( cur->sub==NULL ) break;
32   
33   // search sub command in sub command array
34   for (next=cur->sub; next->name!=NULL && strcmp(next->name, com[i])!=0; ++next);
35   
36   // sub command not found, exit
37   if ( next->name==NULL ) {
38    next=NULL;
39    break;
40   }
41   
42   // next command is now the current one
43   cur=next;
44   
45  }
46  
47  *t=i;
48  
49  
50  return cur;
51  
52 }
53
54
55
56
57 const struct TreeNode *compcur;
58
59
60
61 char* my_generator (const char* text, int state) {
62  
63  static int len;
64  static const struct TreeNode *tn;
65  const char *name;
66  
67  
68  if ( compcur==NULL ) { // sub command not found
69   return NULL;
70  } else if ( state==0 ) {
71   tn=compcur->sub;
72   if ( tn==NULL ) { // terminal command
73    return NULL;
74   }
75   len=strlen(text);
76  }
77  
78  
79  
80  while ( (name=tn->name)!=NULL ) {
81   ++tn;
82   
83   if ( strncmp(name, text, len)==0 ) {
84    return strdup(name);
85   }
86   
87  }
88  
89  
90  return NULL;
91  
92 }
93
94
95
96 char** my_completion (const char *text, int start, int end UNUSED) {
97  
98  char **matches=NULL;
99  char *line, *com[MAXCOM];
100  int i, n;
101  
102  
103  
104  memset(com, 0, MAXCOM*sizeof(char*));
105  line=strdup(rl_line_buffer);
106  line[start]=0;
107  trim(line, start);
108  n=explode(line, com, MAXCOM);
109  free(line);
110  
111  compcur=getSubCom(com, n, &i);
112  
113  if ( i<n ) compcur=NULL;
114  matches=rl_completion_matches(text, my_generator);
115  
116  
117  return matches;
118  
119 }
120
121
122
123
124 int main (int argc, char **argv) {
125  
126  char *line, *com[MAXCOM];
127  struct ngadmin *nga=NULL;
128  struct timeval tv;
129  const struct TreeNode *cur, *next;
130  int i, n;
131  
132  
133  
134  if ( argc<2 ) {
135   printf("Usage: %s <interface>\n", argv[0]);
136   return 1;
137  }
138  
139  memset(com, 0, MAXCOM*sizeof(char*));
140  
141  if ( (nga=ngadmin_init(argv[1]))==NULL ) {
142   fprintf(stderr, "Initialization error\n");
143   goto end;
144  }
145  
146  // set timeout
147  tv.tv_sec=3;
148  tv.tv_usec=0;
149  ngadmin_setTimeout(nga, &tv);
150  
151  
152  //rl_bind_key('\t', rl_abort); // disable auto completion
153  rl_attempted_completion_function=my_completion;
154  
155  
156  while ( cont ) {
157   
158   //rl_bind_key('\t', rl_complete); // enable auto-complete
159   
160   if ( (line=readline("> "))==NULL ) goto end;
161   trim(line, strlen(line));
162   if ( *line!=0 ) add_history(line);
163   n=explode(line, com, MAXCOM);
164   free(line);
165   
166   if ( n==0 ) {
167    continue;
168   }
169   
170   cur=getSubCom(com, n, &i);
171   
172   if ( i<n ) { // commands left unchecked
173    
174    if ( cur->hasArgs ) { // left "commands" are in fact parameters
175     cur->comfunc(n-i, (const char**)&com[i], nga);
176    } else if ( i==0 ) { // root command
177     printf("unknown command\n");
178    } else if ( cur->sub==NULL ) { // terminal command without arguments
179     printf("%s as no subcommand and takes no parameter\n", com[i-1]);
180    } else { // intermediate command
181     printf("unknown %s subcommand\n", com[i-1]);
182    }
183    
184   } else {
185    
186    if ( cur->comfunc==NULL ) { // intermediate command
187     // print available subcommands
188     for (next=cur->sub; next!=NULL && next->name!=NULL; ++next) {
189      printf("%s ", next->name);
190     }
191     printf("\n");
192     
193    } else { // terminal command
194     cur->comfunc(0, NULL, nga); 
195    }
196    
197   }
198   
199   
200   for (i=0; i<MAXCOM; i++) {
201    if ( com[i]!=NULL ) {
202     free(com[i]);
203     com[i]=NULL;
204    }
205   }
206   
207  }
208  
209  
210  end:
211  ngadmin_close(nga);
212  
213  
214  return 0;
215  
216 }
217
218