cli: complete basic functionality of the interactive mode
Pablo Neira Ayuso
maintainerPATCH 1/1 cli: complete basic functionality of the interactive mode
This patch adds missing code to get basic interactive mode operative via `nft -i', including parsing, evaluation, command execution via netlink and error reporting.
Autocomplete is not yet implemented.
Signed-off-by Pablo Neira Ayuso <[email protected]>
--- include/nftables.h | 4 ++- src/cli.c | 13 ++++++---- src/main.c | 71 +++++++++++++++++++++++++++++----------------------- src/rule.c | 25 +++++++++++++----- 4 files changed, 70 insertions(+), 43 deletions(-)
include/nftables.h +3 -1
@@ -25,7 +25,7 @@ extern unsigned int debug_level;extern const char *include_paths[INCLUDE_PATHS_MAX ];struct parser_state ; -extern int cli_init (void *scanner,struct parser_state *state); +extern int cli_init (struct parser_state *state);extern void cli_exit (void ); extern void cli_display(const char *fmt, va_list ap) __fmtstring(1, 0);
@@ -101,4 +101,6 @@ struct input_descriptor { off_t line_offset; }; +int nft_run (void *scanner,struct parser_state *state,struct list_head *msgs); +#endif /* NFTABLES_NFTABLES_H */
src/cli.c +8 -5
@@ -27,11 +27,13 @@#include <parser.h> #include <erec.h> #include <utils.h> +#include <netlink.h> #define CMDLINE_HISTFILE ".nft.history"static const struct input_descriptor indesc_cli = { .type =INDESC_CLI , + .name ="<cli>" , };static struct parser_state *state;
@@ -86,6 +88,7 @@ static void cli_complete(char *line) {const HIST_ENTRY *hist;const char *c; +LIST_HEAD (msgs); line =cli_append_multiline (line);if (line ==NULL )
@@ -102,10 +105,10 @@ static void cli_complete(char *line)if (hist ==NULL ||strcmp (hist->line, line))add_history (line); +parser_init (state, &msgs);scanner_push_buffer (scanner, &indesc_cli, line); -nft_parse (scanner, state); - -erec_print_list (stdout, state->msgs); +nft_run (scanner, state, &msgs); +erec_print_list (stdout, &msgs);xfree (line); }
@@ -140,7 +143,7 @@ void __fmtstring(1, 0) cli_display(const char *fmt, va_list ap)rl_forced_update_display (); } -int cli_init (void *_scanner,struct parser_state *_state) +int cli_init (struct parser_state *_state) {const char *home;
@@ -159,8 +162,8 @@ int cli_init(void *_scanner, struct parser_state *_state)read_history (histfile);history_set_pos (history_length); - scanner = _scanner; state = _state; + scanner =scanner_init (state);while (!eof)rl_callback_read_char ();
src/main.c +40 -31
110 lines not on this page · raw message
src/rule.c +19 -6
@@ -142,7 +142,6 @@ void rule_print(const struct rule *rule)struct scope *scope_init (struct scope *scope,const struct scope *parent) { scope->parent = parent; -init_list_head (&scope->symbols);return scope; }
@@ -189,6 +188,7 @@ struct chain *chain_alloc(const char *name) chain =xzalloc (sizeof (*chain));init_list_head (&chain->rules); +init_list_head (&chain->scope.symbols);if (name !=NULL ) chain->handle.chain =xstrdup (name);return chain;
@@ -240,6 +240,7 @@ struct table *table_alloc(void) table =xzalloc (sizeof (*table));init_list_head (&table->chains);init_list_head (&table->sets); +init_list_head (&table->scope.symbols);return table; }
@@ -472,14 +473,20 @@ static int do_list_sets(struct netlink_ctx *ctx, const struct location *loc,static int do_command_list (struct netlink_ctx *ctx,struct cmd *cmd) { -struct table *table; -struct chain *chain; +struct table *table =NULL ; +struct chain *chain, *nchain;struct rule *rule, *nrule;struct set *set, *nset; - table =table_alloc (); -handle_merge (&table->handle, &cmd->handle); -table_add_hash (table); +/* No need to allocate the table object when listing all tables */ +if (cmd->handle.table !=NULL ) { + table =table_lookup (&cmd->handle); +if (table ==NULL ) { + table =table_alloc (); +handle_merge (&table->handle, &cmd->handle); +table_add_hash (table); + } + }switch (cmd->obj) {case CMD_OBJ_TABLE :
@@ -546,6 +553,12 @@ static int do_command_list(struct netlink_ctx *ctx, struct cmd *cmd) }table_print (table); + +list_for_each_entry_safe (chain, nchain, &table->chains, list) { +list_del (&chain->list); +chain_free (chain); + } +return 0 ; }
Signature · 1 line
1.7.10.4