All of lore.kernel.org
 help / color / mirror / Atom feed
From: Pablo Neira Ayuso <pablo@netfilter.org>
To: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
Cc: netfilter-devel@vger.kernel.org
Subject: Re: [RFC PATCH v2 1/6] rule: allow to print sets in plain format
Date: Wed, 26 Feb 2014 17:44:53 +0100	[thread overview]
Message-ID: <20140226164453.GA22731@localhost> (raw)
In-Reply-To: <20140226160949.18974.67720.stgit@nfdev.cica.es>

Hi Arturo,

On Wed, Feb 26, 2014 at 05:09:49PM +0100, Arturo Borrero Gonzalez wrote:
> Allow to print sets with or without format.
> 
> This is useful in situations where we want to print more or less the same
> the user typed (IOW, in one single line, and with family/table info).
> 
> While at it, make family2str() function public, so it can be used in
> other places.
> 
> Signed-off-by: Arturo Borrero Gonzalez <arturo.borrero.glez@gmail.com>
> ---
>  include/rule.h |    3 +++
>  src/rule.c     |   62 +++++++++++++++++++++++++++++++++++++++++++++++---------
>  2 files changed, 55 insertions(+), 10 deletions(-)
> 
> diff --git a/include/rule.h b/include/rule.h
> index e06444e..b263593 100644
> --- a/include/rule.h
> +++ b/include/rule.h
> @@ -125,6 +125,8 @@ extern void chain_add_hash(struct chain *chain, struct table *table);
>  extern struct chain *chain_lookup(const struct table *table,
>  				  const struct handle *h);
>  
> +extern const char *family2str(unsigned int family);
> +
>  /**
>   * struct rule - nftables rule
>   *
> @@ -193,6 +195,7 @@ extern void set_free(struct set *set);
>  extern void set_add_hash(struct set *set, struct table *table);
>  extern struct set *set_lookup(const struct table *table, const char *name);
>  extern void set_print(const struct set *set);
> +extern void set_print_plain(const struct set *s);
>  
>  /**
>   * enum cmd_ops - command operations
> diff --git a/src/rule.c b/src/rule.c
> index ab96e62..c774664 100644
> --- a/src/rule.c
> +++ b/src/rule.c
> @@ -90,21 +90,37 @@ struct set *set_lookup(const struct table *table, const char *name)
>  	return NULL;
>  }
>  
> -void set_print(const struct set *set)
> +struct print_fmt_options {
> +	const char	*tab;
> +	const char	*nl;
> +	const char	*table;
> +	const char	*family;
> +	const char	*stmt_separator;
> +};
> +
> +static void do_set_print(const struct set *set, struct print_fmt_options *opts)
>  {
>  	const char *delim = "";
>  	const char *type;
>  
>  	type = set->flags & SET_F_MAP ? "map" : "set";
> -	printf("\t%s %s {\n", type, set->handle.set);
> +	printf("%s%s", opts->tab, type);
> +
> +	if (opts->family != NULL)
> +		printf(" %s", opts->family);
> +
> +	if (opts->table != NULL)
> +		printf(" %s", opts->table);
> +
> +	printf(" %s { %s", set->handle.set, opts->nl);
>  
> -	printf("\t\ttype %s", set->keytype->name);
> +	printf("%s%stype %s", opts->tab, opts->tab, set->keytype->name);
>  	if (set->flags & SET_F_MAP)
>  		printf(" : %s", set->datatype->name);
> -	printf("\n");
> +	printf("%s", opts->stmt_separator);
>  
>  	if (set->flags & (SET_F_CONSTANT | SET_F_INTERVAL)) {
> -		printf("\t\tflags ");
> +		printf("%s%sflags ", opts->tab, opts->tab);
>  		if (set->flags & SET_F_CONSTANT) {
>  			printf("%sconstant", delim);
>  			delim = ",";
> @@ -113,15 +129,41 @@ void set_print(const struct set *set)
>  			printf("%sinterval", delim);
>  			delim = ",";
>  		}
> -		printf("\n");
> +		printf("%s", opts->nl);
>  	}
>  
>  	if (set->init != NULL && set->init->size > 0) {
> -		printf("\t\telements = ");
> +		printf("%s%selements = ", opts->tab, opts->tab);
>  		expr_print(set->init);
> -		printf("\n");
> +		printf("%s", opts->nl);
>  	}
> -	printf("\t}\n");
> +	printf("%s}%s", opts->tab, opts->nl);
> +}
> +
> +void set_print(const struct set *s)
> +{
> +	struct print_fmt_options opts;
> +
> +	opts.tab = "\t";
> +	opts.nl = "\n";
> +	opts.table = NULL;
> +	opts.family = NULL;
> +	opts.stmt_separator = "\n";

I think you can use C99 structure initialization:

struct print_fmt_options opts = {
        .tabs           = "\t",
        .nl             = "\n",
        .stmt_separator  = "\n",
};

Note that unspecified fields are already set to zero.

> +
> +	do_set_print(s, &opts);
> +}
> +
> +void set_print_plain(const struct set *s)
> +{
> +	struct print_fmt_options opts;
> +
> +	opts.tab = "";
> +	opts.nl = "";
> +	opts.table = s->handle.table;
> +	opts.family = family2str(s->handle.family);
> +	opts.stmt_separator = ";";

Same thing here.

> +	do_set_print(s, &opts);
>  }
>  
>  struct rule *rule_alloc(const struct location *loc, const struct handle *h)
> @@ -279,7 +321,7 @@ struct chain *chain_lookup(const struct table *table, const struct handle *h)
>  	return NULL;
>  }
>  
> -static const char *family2str(unsigned int family)
> +const char *family2str(unsigned int family)
>  {
>  	switch (family) {
>  		case NFPROTO_IPV4:
> 

  reply	other threads:[~2014-02-26 16:45 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-02-26 16:09 [RFC PATCH v2 0/6] nft events reporting Arturo Borrero Gonzalez
2014-02-26 16:09 ` [RFC PATCH v2 1/6] rule: allow to print sets in plain format Arturo Borrero Gonzalez
2014-02-26 16:44   ` Pablo Neira Ayuso [this message]
2014-02-26 16:09 ` [RFC PATCH v2 2/6] netlink: add netlink_delinearize_set() func Arturo Borrero Gonzalez
2014-02-26 16:10 ` [RFC PATCH v2 3/6] rule: generalize chain_print() Arturo Borrero Gonzalez
2014-02-26 16:10 ` [RFC PATCH v2 4/6] netlink: add netlink_delinearize_chain() func Arturo Borrero Gonzalez
2014-02-26 16:49   ` Pablo Neira Ayuso
2014-02-26 16:10 ` [RFC PATCH v2 5/6] netlink: add netlink_delinearize_table() func Arturo Borrero Gonzalez
2014-02-26 16:10 ` [RFC PATCH v2 6/6] src: add events reporting Arturo Borrero Gonzalez
2014-02-26 17:17   ` Arturo Borrero Gonzalez
2014-02-26 17:27     ` Pablo Neira Ayuso
2014-02-26 17:36       ` Arturo Borrero Gonzalez
2014-02-26 17:19   ` Pablo Neira Ayuso
2014-02-27 14:09 ` [RFC PATCH v2 0/6] nft " Patrick McHardy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20140226164453.GA22731@localhost \
    --to=pablo@netfilter.org \
    --cc=arturo.borrero.glez@gmail.com \
    --cc=netfilter-devel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.