]>
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.
11 #include "nest/bird.h"
12 #include "lib/resource.h"
13 #include "client/client.h"
22 static struct cmd_info command_table
[] = {
23 #include "conf/commands.h"
26 /* FIXME: There should exist some system of aliases, so that `show' can be abbreviated as `s' etc. */
29 struct cmd_node
*sibling
, *son
, **plastson
;
35 static struct cmd_node cmd_root
;
42 cmd_root
.plastson
= &cmd_root
.son
;
44 for(i
=0; i
<sizeof(command_table
) / sizeof(struct cmd_info
); i
++)
46 struct cmd_info
*cmd
= &command_table
[i
];
47 struct cmd_node
*old
, *new;
48 char *c
= cmd
->command
;
54 while (*c
&& *c
!= ' ')
56 for(new=old
->son
; new; new=new->sibling
)
57 if (new->len
== c
-d
&& !memcmp(new->token
, d
, c
-d
))
61 new = xmalloc(sizeof(struct cmd_node
) + c
-d
);
63 old
->plastson
= &new->sibling
;
64 new->sibling
= new->son
= NULL
;
65 new->plastson
= &new->son
;
68 memcpy(new->token
, d
, c
-d
);
80 cmd_display_help(struct cmd_info
*c
)
82 char buf
[strlen(c
->command
) + strlen(c
->args
) + 4];
84 sprintf(buf
, "%s %s", c
->command
, c
->args
);
85 printf("%-45s %s\n", buf
, c
->help
);
88 static struct cmd_node
*
89 cmd_find_abbrev(struct cmd_node
*root
, char *cmd
, int len
)
91 struct cmd_node
*m
, *best
= NULL
, *best2
= NULL
;
93 for(m
=root
->son
; m
; m
=m
->sibling
)
95 if (m
->len
== len
&& !memcmp(m
->token
, cmd
, len
))
97 if (m
->len
> len
&& !memcmp(m
->token
, cmd
, len
))
103 return best2
? NULL
: best
;
107 cmd_help(char *cmd
, int len
)
109 char *end
= cmd
+ len
;
110 struct cmd_node
*n
, *m
;
116 if (*cmd
== ' ' || *cmd
== '\t')
122 while (cmd
< end
&& *cmd
!= ' ' && *cmd
!= '\t')
124 m
= cmd_find_abbrev(n
, z
, cmd
-z
);
129 if (n
->cmd
&& n
->cmd
->is_real_cmd
)
130 cmd_display_help(n
->cmd
);
131 for (m
=n
->son
; m
; m
=m
->sibling
)
132 cmd_display_help(m
->cmd
);