]> git.ipfire.org Git - thirdparty/bird.git/commitdiff
Parse CLI commands. We use the same parser as for configuration files (because
authorMartin Mares <mj@ucw.cz>
Sun, 31 Oct 1999 17:47:47 +0000 (17:47 +0000)
committerMartin Mares <mj@ucw.cz>
Sun, 31 Oct 1999 17:47:47 +0000 (17:47 +0000)
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.

conf/cf-lex.l
conf/conf.c
conf/conf.h
conf/confbase.Y
conf/gen_keywords.m4
conf/gen_parser.m4
nest/cli.c
nest/cli.h
nest/config.Y

index 21727a9de74b33085de83d7cb3e77a60c2668579..258ca7e9ba2604b755f65d3ebf0af99f865a68b7 100644 (file)
@@ -34,14 +34,12 @@ static struct keyword {
 
 static struct keyword *kw_hash[KW_HASH_SIZE];
 static struct symbol **sym_hash;
-static int allow_new_symbols;
 
 int conf_lino;
 
 static int cf_hash(byte *c);
 static struct symbol *cf_find_sym(byte *c, unsigned int h0);
 
-pool *cfg_pool;
 linpool *cfg_mem;
 
 int (*cf_read_hook)(byte *buf, unsigned int max);
@@ -54,7 +52,7 @@ int (*cf_read_hook)(byte *buf, unsigned int max);
 
 %option noyywrap
 
-%x COMMENT CCOMM
+%x COMMENT CCOMM CLI
 
 ALPHA [a-zA-Z_]
 DIGIT [0-9]
@@ -121,6 +119,11 @@ WHITE [ \t]
   return SYM;
 }
 
+<CLI>! {
+  BEGIN(INITIAL);
+  return CLI_MARKER;
+}
+
 [={}:;,()+*/%-<>~\[\]] {
   return yytext[0];
 }
@@ -174,17 +177,18 @@ static struct symbol *
 cf_find_sym(byte *c, unsigned int h0)
 {
   unsigned int h = h0 & (SYM_HASH_SIZE-1);
-  struct symbol *s = sym_hash[h];
+  struct symbol *s;
   int l;
 
-  while (s)
-    {
-      if (!strcmp(s->name, c))
-        return s;
-      s = s->next;
-    }
-  if (!allow_new_symbols)
-    return NULL;
+  if (!sym_hash)
+    sym_hash = cfg_allocz(SYM_HASH_SIZE * sizeof(struct keyword *));
+  else
+    for(s = sym_hash[h]; s; s=s->next)
+      {
+       if (!strcmp(s->name, c))
+         return s;
+       s = s->next;
+      }
   l = strlen(c);
   if (l > SYM_MAX_LEN)
     cf_error("Symbol too long");
@@ -230,11 +234,15 @@ cf_define_symbol(struct symbol *sym, int type, void *def)
 }
 
 void
-cf_lex_init(int flag)
+cf_lex_init(int is_cli)
 {
-  if (allow_new_symbols = flag)
-    sym_hash = cfg_allocz(SYM_HASH_SIZE * sizeof(struct keyword *));
+  sym_hash = NULL;
   conf_lino = 1;
+  yyrestart(NULL);
+  if (is_cli)
+    BEGIN(CLI);
+  else
+    BEGIN(INITIAL);
 }
 
 void
index d9bf9d889a99f0574a27108163c69e09645856e2..47d4db4e87301e421ec11d248e80e04d7e4cd29a 100644 (file)
@@ -39,15 +39,12 @@ config_alloc(byte *name)
 int
 config_parse(struct config *c)
 {
-  struct proto_config *p;
-
   debug("Parsing configuration file `%s'\n", c->file_name);
   new_config = c;
-  cfg_pool = c->pool;
   cfg_mem = c->mem;
   if (setjmp(conf_jmpbuf))
     return 0;
-  cf_lex_init(1);
+  cf_lex_init(0);
   cf_lex_init_tables();
   protos_preconfig(c);
   rt_preconfig(c);
@@ -61,6 +58,18 @@ config_parse(struct config *c)
   return 1;
 }
 
+int
+cli_parse(struct config *c)
+{
+  new_config = c;
+  cfg_mem = c->mem;
+  if (setjmp(conf_jmpbuf))
+    return 0;
+  cf_lex_init(1);
+  cf_parse();
+  return 1;
+}
+
 void
 config_free(struct config *c)
 {
index 4f6f030c99ee96d08a84988eb304aebfa0fe1120..d62f1383557547f6192281eb1164c9212c561734 100644 (file)
@@ -30,13 +30,13 @@ extern struct config *config, *new_config;
 
 struct config *config_alloc(byte *name);
 int config_parse(struct config *);
+int cli_parse(struct config *);
 void config_free(struct config *);
 void config_commit(struct config *);
 void cf_error(char *msg, ...) NORET;
 
 /* Pools */
 
-extern pool *cfg_pool;
 extern linpool *cfg_mem;
 
 #define cfg_alloc(size) lp_alloc(cfg_mem, size)
@@ -71,7 +71,7 @@ extern int conf_lino;
 
 void cf_lex_init_tables(void);
 int cf_lex(void);
-void cf_lex_init(int flag);
+void cf_lex_init(int is_cli);
 struct symbol *cf_find_symbol(byte *c);
 struct symbol *cf_default_name(char *prefix, int *counter);
 void cf_define_symbol(struct symbol *symbol, int type, void *def);
index b266d25391c89305f750138984217ef30fa32cbb..4a942878c237b7ac6bff8dd853b4c2463a0f058e 100644 (file)
@@ -16,8 +16,11 @@ CF_HDR
 #include "nest/protocol.h"
 #include "nest/iface.h"
 #include "nest/route.h"
+#include "nest/cli.h"
 #include "filter/filter.h"
 
+#define cli_msg(x...) cli_printf(this_cli, x)
+
 CF_DECLS
 
 %union {
@@ -34,7 +37,7 @@ CF_DECLS
   struct password_item *p;
 }
 
-%token END
+%token END CLI_MARKER
 %token <i> NUM
 %token <i32> RTRID
 %token <a> IPA
@@ -54,9 +57,8 @@ CF_GRAMMAR
 
 /* Basic config file structure */
 
-config: conf_entries END {
-       return 0;
-   }
+config: conf_entries END { return 0; }
+ | CLI_MARKER cli_cmd END { return 0; }
  ;
 
 conf_entries:
index 8bbe4902fc996b1540a237649178d41de1ed186b..37b882b567be6c00f28df0c09949b5272a9d99db 100644 (file)
@@ -2,7 +2,7 @@ m4_divert(-1)m4_dnl
 #
 #      BIRD -- Generator of Configuration Keyword List
 #
-#      (c) 1998 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
+#      (c) 1998--1999 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
 #
 #      Can be freely distributed and used under the terms of the GNU GPL.
 #
@@ -18,6 +18,10 @@ m4_define(CF_keywd, `m4_ifdef([[CF_tok_$1]],,[[m4_define([[CF_tok_$1]],1)CF_hand
 m4_define(CF_KEYWORDS, `m4_define([[CF_toks]],[[]])CF_iterate([[CF_keywd]], [[$@]])m4_ifelse(CF_toks,,,%token[[]]CF_toks
 )DNL')
 
+# CLI commands generate keywords as well
+m4_define(CF_CLI, `CF_KEYWORDS(m4_translit($1, [[ ]], [[,]]))
+')
+
 # As we are processing C source, we must access all M4 primitives via
 # m4_* and also set different quoting convention: `[[' and ']]'
 m4_changequote([[,]])
index a08b330c723badc9de86bfa0fa412e3109d2ee04..8441c83b3dd1a6113b804f8aba75d34b16d5eea5 100644 (file)
@@ -2,7 +2,7 @@ m4_divert(-1)m4_dnl
 #
 #      BIRD -- Generator of Configuration Grammar
 #
-#      (c) 1998 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
+#      (c) 1998--1999 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
 #
 #      Can be freely distributed and used under the terms of the GNU GPL.
 #
@@ -38,6 +38,12 @@ m4_define(CF_dyn_rules,)
 m4_define(CF_ADDTO, `m4_define([[CF_rule_$1]],m4_ifdef([[CF_rule_$1]],CF_rule_$1 | ,[[m4_define([[CF_dyn_rules]],CF_dyn_rules[[CF_RULE($1)
 ]])]])$2)DNL')
 
+# CLI commands
+m4_define(CF_CLI, `m4_define([[CF_cmd]], cmd_[[]]m4_translit($1, [[ ]], _))DNL
+m4_divert(2)CF_KEYWORDS(m4_translit($1, [[ ]], [[,]]))
+m4_divert(3)CF_ADDTO(cli_cmd, CF_cmd)
+CF_cmd: $1 ')
+
 # After all configuration templates end, we finally generate the grammar file.
 m4_m4wrap(`
 m4_divert(0)DNL
index f094a7c404897e5959d89b8d695e0ec39d63fac5..41907b333ec2a2d42535dc3223bea19fcddb1a45 100644 (file)
@@ -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;
 }
index 9670f69a041e71a7e6bc42ac877f83c5cb2e77d9..d4413ffb510ee380cea1d020d8d070e9f183af2f 100644 (file)
@@ -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 */
 
index 91cdad118066c04c8cc51dc340620d25a56d68c4..645946250befce2ae3267186f91f971ea4a6fc37 100644 (file)
@@ -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