summaryrefslogtreecommitdiffstats
path: root/nest
diff options
context:
space:
mode:
authorMartin Mares <mj@ucw.cz>1999-10-31 18:47:47 +0100
committerMartin Mares <mj@ucw.cz>1999-10-31 18:47:47 +0100
commitbc2fb68098faaf09393437a7743285d2af71d102 (patch)
tree94b82b1648296c7a7fdb35063a524a1a7fe90228 /nest
parentb9672a845f7ff7d2441e21746566eacc51f274b7 (diff)
downloadbird-bc2fb68098faaf09393437a7743285d2af71d102.tar
bird-bc2fb68098faaf09393437a7743285d2af71d102.zip
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.
Diffstat (limited to 'nest')
-rw-r--r--nest/cli.c50
-rw-r--r--nest/cli.h2
-rw-r--r--nest/config.Y5
3 files changed, 52 insertions, 5 deletions
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, <N>, [[Flashes each LED <N> times]]) NUM { cli_msg(0, "%d", $3); } ;
+CF_CLI(TEST, 1, 2) { cli_msg(0, "OK"); }
+
CF_CODE
CF_END