]>
git.ipfire.org Git - thirdparty/bird.git/blob - client/commands.c
2 * BIRD Client -- Command Handling
4 * (c) 1999--2000 Martin Mares <mj@ucw.cz>
6 * Can be freely distributed and used under the terms of the GNU GPL.
12 #include "nest/bird.h"
13 #include "lib/resource.h"
14 #include "lib/string.h"
15 #include "client/client.h"
24 static struct cmd_info command_table
[] = {
25 #include "conf/commands.h"
29 struct cmd_node
*sibling
, *son
, **plastson
;
30 struct cmd_info
*cmd
, *help
;
36 static struct cmd_node cmd_root
;
43 cmd_root
.plastson
= &cmd_root
.son
;
45 for(i
=0; i
<ARRAY_SIZE(command_table
); i
++)
47 struct cmd_info
*cmd
= &command_table
[i
];
48 struct cmd_node
*old
, *new;
49 char *c
= cmd
->command
;
55 while (*c
&& !isspace(*c
))
57 for(new=old
->son
; new; new=new->sibling
)
58 if (new->len
== c
-d
&& !memcmp(new->token
, d
, c
-d
))
62 int size
= sizeof(struct cmd_node
) + c
-d
;
66 old
->plastson
= &new->sibling
;
67 new->plastson
= &new->son
;
69 memcpy(new->token
, d
, c
-d
);
70 new->prio
= (new->len
== 3 && (!memcmp(new->token
, "roa", 3) || !memcmp(new->token
, "rip", 3))) ? 0 : 1; /* Hack */
84 cmd_do_display_help(struct cmd_info
*c
)
86 char buf
[strlen(c
->command
) + strlen(c
->args
) + 4];
88 sprintf(buf
, "%s %s", c
->command
, c
->args
);
89 printf("%-45s %s\n", buf
, c
->help
);
93 cmd_display_help(struct cmd_info
*c1
, struct cmd_info
*c2
)
96 cmd_do_display_help(c1
);
98 cmd_do_display_help(c2
);
101 static struct cmd_node
*
102 cmd_find_abbrev(struct cmd_node
*root
, char *cmd
, int len
, int *pambiguous
)
104 struct cmd_node
*m
, *best
= NULL
, *best2
= NULL
;
107 for(m
=root
->son
; m
; m
=m
->sibling
)
109 if (m
->len
== len
&& !memcmp(m
->token
, cmd
, len
))
111 if (m
->len
> len
&& !memcmp(m
->token
, cmd
, len
))
113 if (best
&& best
->prio
> m
->prio
)
115 if (best
&& best
->prio
== m
->prio
)
129 cmd_list_ambiguous(struct cmd_node
*root
, char *cmd
, int len
)
133 for(m
=root
->son
; m
; m
=m
->sibling
)
134 if (m
->len
> len
&& !memcmp(m
->token
, cmd
, len
))
135 cmd_display_help(m
->help
, m
->cmd
);
139 cmd_help(char *cmd
, int len
)
141 char *end
= cmd
+ len
;
142 struct cmd_node
*n
, *m
;
155 while (cmd
< end
&& !isspace(*cmd
))
157 m
= cmd_find_abbrev(n
, z
, cmd
-z
, &ambig
);
160 cmd_list_ambiguous(n
, z
, cmd
-z
);
167 cmd_display_help(n
->cmd
, NULL
);
168 for (m
=n
->son
; m
; m
=m
->sibling
)
169 cmd_display_help(m
->help
, m
->cmd
);
173 cmd_find_common_match(struct cmd_node
*root
, char *cmd
, int len
, int *pcount
, char *buf
)
176 int best
, best_prio
, i
;
181 for(m
=root
->son
; m
; m
=m
->sibling
)
183 if (m
->len
< len
|| memcmp(m
->token
, cmd
, len
))
186 if (best_prio
> m
->prio
)
189 if (best_prio
< m
->prio
)
198 strcpy(buf
, m
->token
+ len
);
205 while (i
< best
&& i
< m
->len
- len
&& buf
[i
] == m
->token
[len
+i
])
214 cmd_complete(char *cmd
, int len
, char *buf
, int again
)
217 char *end
= cmd
+ len
;
219 struct cmd_node
*n
, *m
;
221 int ambig
, cnt
= 0, common
;
223 /* Find the last word we want to complete */
224 for(fin
=end
; fin
> start
&& !isspace(fin
[-1]); fin
--)
227 /* Find the context */
229 while (cmd
< fin
&& n
->son
)
237 while (cmd
< fin
&& !isspace(*cmd
))
239 m
= cmd_find_abbrev(n
, z
, cmd
-z
, &ambig
);
245 cmd_list_ambiguous(n
, z
, cmd
-z
);
254 /* Completion of parameters is not yet supported */
258 /* We know the context, let's try to complete */
259 common
= cmd_find_common_match(n
, fin
, end
-fin
, &cnt
, buf
);
276 cmd_list_ambiguous(n
, fin
, end
-fin
);
282 cmd_expand(char *cmd
)
284 struct cmd_node
*n
, *m
;
298 while (*c
&& !isspace(*c
))
300 m
= cmd_find_abbrev(n
, b
, c
-b
, &ambig
);
305 puts("Ambiguous command, possible expansions are:");
306 cmd_list_ambiguous(n
, b
, c
-b
);
314 puts("No such command. Press `?' for help.");
317 b
= malloc(strlen(n
->cmd
->command
) + strlen(args
) + 1);
318 sprintf(b
, "%s%s", n
->cmd
->command
, args
);