4 #include <readline/readline.h>
5 #include <readline/history.h>
21 const struct TreeNode* getSubCom (char **com, int n, int *t) {
24 const struct TreeNode *cur, *next;
30 // we have reached a terminal command, exit
31 if ( cur->sub==NULL ) break;
33 // search sub command in sub command array
34 for (next=cur->sub; next->name!=NULL && strcmp(next->name, com[i])!=0; ++next);
36 // sub command not found, exit
37 if ( next->name==NULL ) {
42 // next command is now the current one
57 const struct TreeNode *compcur;
61 char* my_generator (const char* text, int state) {
64 static const struct TreeNode *tn;
68 if ( compcur==NULL ) { // sub command not found
70 } else if ( state==0 ) {
72 if ( tn==NULL ) { // terminal command
80 while ( (name=tn->name)!=NULL ) {
83 if ( strncmp(name, text, len)==0 ) {
96 char** my_completion (const char *text, int start, int end UNUSED) {
99 char *line, *com[MAXCOM];
104 memset(com, 0, MAXCOM*sizeof(char*));
105 line=strdup(rl_line_buffer);
108 n=explode(line, com, MAXCOM);
111 compcur=getSubCom(com, n, &i);
113 if ( i<n ) compcur=NULL;
114 matches=rl_completion_matches(text, my_generator);
124 int main (int argc, char **argv) {
126 char *line, *com[MAXCOM];
127 struct ngadmin *nga=NULL;
129 const struct TreeNode *cur, *next;
135 printf("Usage: %s <interface>\n", argv[0]);
139 memset(com, 0, MAXCOM*sizeof(char*));
141 if ( (nga=ngadmin_init(argv[1]))==NULL ) {
142 fprintf(stderr, "Initialization error\n");
149 ngadmin_setTimeout(nga, &tv);
152 //rl_bind_key('\t', rl_abort); // disable auto completion
153 rl_attempted_completion_function=my_completion;
158 //rl_bind_key('\t', rl_complete); // enable auto-complete
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);
170 cur=getSubCom(com, n, &i);
172 if ( i<n ) { // commands left unchecked
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]);
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);
193 } else { // terminal command
194 cur->comfunc(0, NULL, nga);
200 for (i=0; i<MAXCOM; i++) {
201 if ( com[i]!=NULL ) {