]>
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
;
35 static struct cmd_node cmd_root
;
42 cmd_root
.plastson
= &cmd_root
.son
;
44 for(i
=0; i
<ARRAY_SIZE(command_table
); i
++)
46 struct cmd_info
*cmd
= &command_table
[i
];
47 struct cmd_node
*old
, *new;
48 char *c
= cmd
->command
;
54 while (*c
&& !isspace(*c
))
56 for(new=old
->son
; new; new=new->sibling
)
57 if (new->len
== c
-d
&& !memcmp(new->token
, d
, c
-d
))
61 int size
= sizeof(struct cmd_node
) + c
-d
;
65 old
->plastson
= &new->sibling
;
66 new->plastson
= &new->son
;
68 memcpy(new->token
, d
, c
-d
);
82 cmd_do_display_help(struct cmd_info
*c
)
84 char buf
[strlen(c
->command
) + strlen(c
->args
) + 4];
86 sprintf(buf
, "%s %s", c
->command
, c
->args
);
87 printf("%-45s %s\n", buf
, c
->help
);
91 cmd_display_help(struct cmd_info
*c1
, struct cmd_info
*c2
)
94 cmd_do_display_help(c1
);
96 cmd_do_display_help(c2
);
99 static struct cmd_node
*
100 cmd_find_abbrev(struct cmd_node
*root
, char *cmd
, int len
, int *pambiguous
)
102 struct cmd_node
*m
, *best
= NULL
, *best2
= NULL
;
105 for(m
=root
->son
; m
; m
=m
->sibling
)
107 if (m
->len
== len
&& !memcmp(m
->token
, cmd
, len
))
109 if (m
->len
> len
&& !memcmp(m
->token
, cmd
, len
))
124 cmd_list_ambiguous(struct cmd_node
*root
, char *cmd
, int len
)
128 for(m
=root
->son
; m
; m
=m
->sibling
)
129 if (m
->len
> len
&& !memcmp(m
->token
, cmd
, len
))
130 cmd_display_help(m
->help
, m
->cmd
);
134 cmd_help(char *cmd
, int len
)
136 char *end
= cmd
+ len
;
137 struct cmd_node
*n
, *m
;
150 while (cmd
< end
&& !isspace(*cmd
))
152 m
= cmd_find_abbrev(n
, z
, cmd
-z
, &ambig
);
155 cmd_list_ambiguous(n
, z
, cmd
-z
);
162 cmd_display_help(n
->cmd
, NULL
);
163 for (m
=n
->son
; m
; m
=m
->sibling
)
164 cmd_display_help(m
->help
, m
->cmd
);
168 cmd_find_common_match(struct cmd_node
*root
, char *cmd
, int len
, int *pcount
, char *buf
)
175 for(m
=root
->son
; m
; m
=m
->sibling
)
177 if (m
->len
< len
|| memcmp(m
->token
, cmd
, len
))
182 strcpy(buf
, m
->token
+ len
);
188 while (i
< best
&& i
< m
->len
- len
&& buf
[i
] == m
->token
[len
+i
])
197 cmd_complete(char *cmd
, int len
, char *buf
, int again
)
200 char *end
= cmd
+ len
;
202 struct cmd_node
*n
, *m
;
204 int ambig
, cnt
= 0, common
;
206 /* Find the last word we want to complete */
207 for(fin
=end
; fin
> start
&& !isspace(fin
[-1]); fin
--)
210 /* Find the context */
212 while (cmd
< fin
&& n
->son
)
220 while (cmd
< fin
&& !isspace(*cmd
))
222 m
= cmd_find_abbrev(n
, z
, cmd
-z
, &ambig
);
228 cmd_list_ambiguous(n
, z
, cmd
-z
);
237 /* Completion of parameters is not yet supported */
241 /* We know the context, let's try to complete */
242 common
= cmd_find_common_match(n
, fin
, end
-fin
, &cnt
, buf
);
259 cmd_list_ambiguous(n
, fin
, end
-fin
);
265 cmd_expand(char *cmd
)
267 struct cmd_node
*n
, *m
;
281 while (*c
&& !isspace(*c
))
283 m
= cmd_find_abbrev(n
, b
, c
-b
, &ambig
);
288 puts("Ambiguous command, possible expansions are:");
289 cmd_list_ambiguous(n
, b
, c
-b
);
297 puts("No such command. Press `?' for help.");
300 b
= xmalloc(strlen(n
->cmd
->command
) + strlen(args
) + 1);
301 sprintf(b
, "%s%s", n
->cmd
->command
, args
);