1999-10-29 12:10:10 +00:00
|
|
|
/*
|
|
|
|
* BIRD Internet Routing Daemon -- Command-Line Interface
|
|
|
|
*
|
|
|
|
* (c) 1999 Martin Mares <mj@ucw.cz>
|
|
|
|
*
|
|
|
|
* Can be freely distributed and used under the terms of the GNU GPL.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nest/bird.h"
|
|
|
|
#include "nest/cli.h"
|
Parse CLI commands. We use the same parser as for configuration files (because
we want to allow filter and similar complex constructs to be used in commands
and we should avoid code duplication), only with CLI_MARKER token prepended
before the whole input.
Defined macro CF_CLI(cmd, args, help) for defining CLI commands in .Y files.
The first argument specifies the command itself, the remaining two arguments
are copied to the help file (er, will be copied after the help file starts
to exist). This macro automatically creates a skeleton rule for the command,
you only need to append arguments as in:
CF_CLI(STEAL MONEY, <$>, [[Steal <$> US dollars or equivalent in any other currency]]): NUM {
cli_msg(0, "%d$ stolen", $3);
} ;
Also don't forget to reset lexer state between inputs.
1999-10-31 17:47:47 +00:00
|
|
|
#include "conf/conf.h"
|
|
|
|
#include "lib/string.h"
|
1999-10-29 12:10:10 +00:00
|
|
|
|
|
|
|
pool *cli_pool;
|
|
|
|
|
|
|
|
void
|
|
|
|
cli_printf(cli *c, int code, char *msg, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
byte buf[1024];
|
1999-10-31 15:43:44 +00:00
|
|
|
int cd = code;
|
|
|
|
int size, cnt;
|
1999-10-29 12:10:10 +00:00
|
|
|
struct cli_out *o;
|
|
|
|
|
|
|
|
va_start(args, msg);
|
1999-10-31 15:43:44 +00:00
|
|
|
if (cd < 0)
|
|
|
|
{
|
|
|
|
cd = -cd;
|
|
|
|
if (cd == c->last_reply)
|
|
|
|
size = bsprintf(buf, " ");
|
|
|
|
else
|
|
|
|
size = bsprintf(buf, "%04d-", cd);
|
|
|
|
}
|
1999-10-29 12:10:10 +00:00
|
|
|
else
|
1999-10-31 15:43:44 +00:00
|
|
|
size = bsprintf(buf, "%04d ", cd);
|
|
|
|
c->last_reply = cd;
|
|
|
|
cnt = bvsnprintf(buf+size, sizeof(buf)-size-1, msg, args);
|
|
|
|
if (cnt < 0)
|
|
|
|
{
|
|
|
|
cli_printf(c, code < 0 ? -8000 : 8000, "<line overflow>");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
size += cnt;
|
1999-10-29 12:10:10 +00:00
|
|
|
buf[size++] = '\n';
|
|
|
|
if (!(o = c->tx_write) || o->wpos + size > o->end)
|
|
|
|
{
|
1999-10-31 15:43:44 +00:00
|
|
|
if (!o && c->tx_buf)
|
|
|
|
o = c->tx_buf;
|
1999-10-29 12:10:10 +00:00
|
|
|
else
|
1999-10-31 15:43:44 +00:00
|
|
|
{
|
|
|
|
o = mb_alloc(c->pool, sizeof(struct cli_out) + CLI_TX_BUF_SIZE);
|
|
|
|
if (c->tx_write)
|
|
|
|
c->tx_write->next = o;
|
|
|
|
else
|
|
|
|
c->tx_buf = o;
|
|
|
|
o->next = NULL;
|
|
|
|
o->wpos = o->outpos = o->buf;
|
|
|
|
o->end = o->buf + CLI_TX_BUF_SIZE;
|
|
|
|
}
|
1999-10-29 12:10:10 +00:00
|
|
|
c->tx_write = o;
|
1999-10-31 15:43:44 +00:00
|
|
|
if (!c->tx_pos)
|
|
|
|
c->tx_pos = o;
|
1999-10-29 12:10:10 +00:00
|
|
|
}
|
|
|
|
memcpy(o->wpos, buf, size);
|
|
|
|
o->wpos += size;
|
|
|
|
}
|
|
|
|
|
1999-10-31 15:43:44 +00:00
|
|
|
static void
|
|
|
|
cli_hello(cli *c)
|
|
|
|
{
|
|
|
|
cli_printf(c, 1, "BIRD " BIRD_VERSION " ready.");
|
|
|
|
c->cont = NULL;
|
|
|
|
}
|
|
|
|
|
1999-10-29 12:10:10 +00:00
|
|
|
static void
|
|
|
|
cli_free_out(cli *c)
|
|
|
|
{
|
|
|
|
struct cli_out *o, *p;
|
|
|
|
|
|
|
|
if (o = c->tx_buf)
|
|
|
|
{
|
1999-10-31 15:43:44 +00:00
|
|
|
c->tx_write = NULL;
|
1999-10-29 12:10:10 +00:00
|
|
|
o->wpos = o->outpos = o->buf;
|
|
|
|
while (p = o->next)
|
|
|
|
{
|
|
|
|
o->next = p->next;
|
|
|
|
mb_free(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Parse CLI commands. We use the same parser as for configuration files (because
we want to allow filter and similar complex constructs to be used in commands
and we should avoid code duplication), only with CLI_MARKER token prepended
before the whole input.
Defined macro CF_CLI(cmd, args, help) for defining CLI commands in .Y files.
The first argument specifies the command itself, the remaining two arguments
are copied to the help file (er, will be copied after the help file starts
to exist). This macro automatically creates a skeleton rule for the command,
you only need to append arguments as in:
CF_CLI(STEAL MONEY, <$>, [[Steal <$> US dollars or equivalent in any other currency]]): NUM {
cli_msg(0, "%d$ stolen", $3);
} ;
Also don't forget to reset lexer state between inputs.
1999-10-31 17:47:47 +00:00
|
|
|
static byte *cli_rh_pos;
|
|
|
|
static unsigned int cli_rh_len;
|
|
|
|
static int cli_rh_trick_flag;
|
|
|
|
struct cli *this_cli;
|
|
|
|
|
|
|
|
static int
|
|
|
|
cli_cmd_read_hook(byte *buf, unsigned int max)
|
|
|
|
{
|
|
|
|
if (!cli_rh_trick_flag)
|
|
|
|
{
|
|
|
|
cli_rh_trick_flag = 1;
|
|
|
|
buf[0] = '!';
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (max > cli_rh_len)
|
|
|
|
max = cli_rh_len;
|
|
|
|
memcpy(buf, cli_rh_pos, max);
|
|
|
|
cli_rh_pos += max;
|
|
|
|
cli_rh_len -= max;
|
|
|
|
return max;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cli_command(struct cli *c)
|
|
|
|
{
|
|
|
|
struct config f;
|
|
|
|
int res;
|
|
|
|
|
|
|
|
f.pool = NULL;
|
|
|
|
f.mem = c->parser_pool;
|
|
|
|
cf_read_hook = cli_cmd_read_hook;
|
|
|
|
cli_rh_pos = c->rx_buf;
|
|
|
|
cli_rh_len = strlen(c->rx_buf);
|
|
|
|
cli_rh_trick_flag = 0;
|
|
|
|
this_cli = c;
|
|
|
|
res = cli_parse(&f);
|
|
|
|
lp_flush(c->parser_pool);
|
|
|
|
if (!res)
|
|
|
|
cli_printf(c, 9001, f.err_msg);
|
|
|
|
}
|
|
|
|
|
1999-10-29 12:10:10 +00:00
|
|
|
static int
|
|
|
|
cli_event(void *data)
|
|
|
|
{
|
|
|
|
cli *c = data;
|
|
|
|
int err;
|
|
|
|
|
1999-10-31 15:43:44 +00:00
|
|
|
if (c->tx_pos)
|
|
|
|
;
|
|
|
|
else if (c->cont)
|
|
|
|
c->cont(c);
|
|
|
|
else
|
1999-10-29 12:10:10 +00:00
|
|
|
{
|
1999-10-31 15:43:44 +00:00
|
|
|
err = cli_get_command(c);
|
|
|
|
if (!err)
|
|
|
|
return 0;
|
|
|
|
if (err < 0)
|
|
|
|
cli_printf(c, 9000, "Command too long");
|
|
|
|
else
|
Parse CLI commands. We use the same parser as for configuration files (because
we want to allow filter and similar complex constructs to be used in commands
and we should avoid code duplication), only with CLI_MARKER token prepended
before the whole input.
Defined macro CF_CLI(cmd, args, help) for defining CLI commands in .Y files.
The first argument specifies the command itself, the remaining two arguments
are copied to the help file (er, will be copied after the help file starts
to exist). This macro automatically creates a skeleton rule for the command,
you only need to append arguments as in:
CF_CLI(STEAL MONEY, <$>, [[Steal <$> US dollars or equivalent in any other currency]]): NUM {
cli_msg(0, "%d$ stolen", $3);
} ;
Also don't forget to reset lexer state between inputs.
1999-10-31 17:47:47 +00:00
|
|
|
cli_command(c);
|
1999-10-29 12:10:10 +00:00
|
|
|
}
|
1999-10-31 15:43:44 +00:00
|
|
|
if (cli_write(c))
|
|
|
|
{
|
|
|
|
cli_free_out(c);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
1999-10-29 12:10:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cli *
|
|
|
|
cli_new(void *priv)
|
|
|
|
{
|
|
|
|
pool *p = rp_new(cli_pool, "CLI");
|
|
|
|
cli *c = mb_alloc(p, sizeof(cli));
|
|
|
|
|
|
|
|
c->pool = p;
|
|
|
|
c->priv = priv;
|
|
|
|
c->event = ev_new(p);
|
|
|
|
c->event->hook = cli_event;
|
|
|
|
c->event->data = c;
|
|
|
|
c->tx_buf = c->tx_pos = c->tx_write = NULL;
|
1999-10-31 15:43:44 +00:00
|
|
|
c->cont = cli_hello;
|
|
|
|
c->last_reply = 0;
|
Parse CLI commands. We use the same parser as for configuration files (because
we want to allow filter and similar complex constructs to be used in commands
and we should avoid code duplication), only with CLI_MARKER token prepended
before the whole input.
Defined macro CF_CLI(cmd, args, help) for defining CLI commands in .Y files.
The first argument specifies the command itself, the remaining two arguments
are copied to the help file (er, will be copied after the help file starts
to exist). This macro automatically creates a skeleton rule for the command,
you only need to append arguments as in:
CF_CLI(STEAL MONEY, <$>, [[Steal <$> US dollars or equivalent in any other currency]]): NUM {
cli_msg(0, "%d$ stolen", $3);
} ;
Also don't forget to reset lexer state between inputs.
1999-10-31 17:47:47 +00:00
|
|
|
c->parser_pool = lp_new(c->pool, 4096);
|
1999-10-31 15:43:44 +00:00
|
|
|
ev_schedule(c->event);
|
1999-10-29 12:10:10 +00:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cli_kick(cli *c)
|
|
|
|
{
|
1999-10-31 15:43:44 +00:00
|
|
|
if (!c->cont && !c->tx_pos)
|
|
|
|
ev_schedule(c->event);
|
1999-10-29 12:10:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cli_written(cli *c)
|
|
|
|
{
|
|
|
|
cli_free_out(c);
|
1999-10-31 15:43:44 +00:00
|
|
|
ev_schedule(c->event);
|
1999-10-29 12:10:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cli_free(cli *c)
|
|
|
|
{
|
|
|
|
rfree(c->pool);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cli_init(void)
|
|
|
|
{
|
|
|
|
cli_pool = rp_new(&root_pool, "CLI");
|
|
|
|
}
|