From bc2fb68098faaf09393437a7743285d2af71d102 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Sun, 31 Oct 1999 17:47:47 +0000 Subject: 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. --- nest/cli.c | 50 +++++++++++++++++++++++++++++++++++++++++++++----- nest/cli.h | 2 ++ nest/config.Y | 5 +++++ 3 files changed, 52 insertions(+), 5 deletions(-) (limited to 'nest') diff --git a/nest/cli.c b/nest/cli.c index f094a7c..41907b3 100644 --- a/nest/cli.c +++ b/nest/cli.c @@ -7,8 +7,9 @@ */ #include "nest/bird.h" -#include "lib/string.h" #include "nest/cli.h" +#include "conf/conf.h" +#include "lib/string.h" pool *cli_pool; @@ -88,6 +89,47 @@ cli_free_out(cli *c) } } +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); +} + static int cli_event(void *data) { @@ -106,10 +148,7 @@ cli_event(void *data) if (err < 0) cli_printf(c, 9000, "Command too long"); else - { - cli_printf(c, -9001, "Parse error in:"); - cli_printf(c, 9001, c->rx_buf); - } + cli_command(c); } if (cli_write(c)) { @@ -133,6 +172,7 @@ cli_new(void *priv) c->tx_buf = c->tx_pos = c->tx_write = NULL; c->cont = cli_hello; c->last_reply = 0; + c->parser_pool = lp_new(c->pool, 4096); ev_schedule(c->event); return c; } diff --git a/nest/cli.h b/nest/cli.h index 9670f69..d4413ff 100644 --- a/nest/cli.h +++ b/nest/cli.h @@ -31,9 +31,11 @@ typedef struct cli { void (*cont)(struct cli *c); void *rover; /* Private to continuation routine */ int last_reply; + struct linpool *parser_pool; /* Pool used during parsing */ } cli; extern pool *cli_pool; +extern struct cli *this_cli; /* Used during parsing */ /* Functions to be called by command handlers */ diff --git a/nest/config.Y b/nest/config.Y index 91cdad1..6459462 100644 --- a/nest/config.Y +++ b/nest/config.Y @@ -182,6 +182,11 @@ password_list: } ; +/* Core commands */ + +CF_CLI(TEST LEDS, , [[Flashes each LED times]]) NUM { cli_msg(0, "%d", $3); } ; +CF_CLI(TEST, 1, 2) { cli_msg(0, "OK"); } + CF_CODE CF_END -- cgit v1.2.3