From: Patrick McHardy <kaber@trash.net>
To: pablo@netfilter.org
Cc: fw@strlen.de, netfilter-devel@vger.kernel.org
Subject: [PATCH 2/4] binop: take care of operator precedence when printing binop arguments
Date: Mon, 17 Feb 2014 17:20:50 +0000 [thread overview]
Message-ID: <1392657652-9815-3-git-send-email-kaber@trash.net> (raw)
In-Reply-To: <1392657652-9815-1-git-send-email-kaber@trash.net>
When the argument of a binop is a binop itself, we may need to add parens
if the precedence of the argument is lower then the binop.
Before:
tcp flags & syn | ack == syn | ack
tcp flags & syn | ack != syn | ack
After:
tcp flags & (syn | ack) == syn | ack
tcp flags & (syn | ack) != syn | ack
Signed-off-by: Patrick McHardy <kaber@trash.net>
---
include/expression.h | 2 ++
src/expression.c | 30 ++++++++++++++++++++++++++++--
2 files changed, 30 insertions(+), 2 deletions(-)
diff --git a/include/expression.h b/include/expression.h
index 0633102..5128a5b 100644
--- a/include/expression.h
+++ b/include/expression.h
@@ -80,7 +80,9 @@ enum ops {
OP_FLAGCMP,
/* Set lookup */
OP_LOOKUP,
+ __OP_MAX
};
+#define OP_MAX (__OP_MAX - 1)
extern const char *expr_op_symbols[];
diff --git a/src/expression.c b/src/expression.c
index c856622..6cc79b2 100644
--- a/src/expression.c
+++ b/src/expression.c
@@ -404,16 +404,42 @@ struct expr *unary_expr_alloc(const struct location *loc,
return expr;
}
+static uint8_t expr_binop_precedence[OP_MAX + 1] = {
+ [OP_LSHIFT] = 1,
+ [OP_RSHIFT] = 1,
+ [OP_AND] = 2,
+ [OP_XOR] = 3,
+ [OP_OR] = 4,
+};
+
+static void binop_arg_print(const struct expr *op, const struct expr *arg)
+{
+ bool prec = false;
+
+ if (arg->ops->type == EXPR_BINOP &&
+ expr_binop_precedence[op->op] != 0 &&
+ expr_binop_precedence[op->op] < expr_binop_precedence[arg->op])
+ prec = 1;
+
+ if (prec)
+ printf("(");
+ expr_print(arg);
+ if (prec)
+ printf(")");
+}
+
static void binop_expr_print(const struct expr *expr)
{
- expr_print(expr->left);
+ binop_arg_print(expr, expr->left);
+
if (expr_op_symbols[expr->op] &&
(expr->op != OP_EQ ||
expr->left->ops->type == EXPR_BINOP))
printf(" %s ", expr_op_symbols[expr->op]);
else
printf(" ");
- expr_print(expr->right);
+
+ binop_arg_print(expr, expr->right);
}
static void binop_expr_clone(struct expr *new, const struct expr *expr)
--
1.8.5.3
next prev parent reply other threads:[~2014-02-17 17:20 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-02-17 17:20 [PATCH 0/4] nftables: bitmask and prefix fixes Patrick McHardy
2014-02-17 17:20 ` [PATCH 1/4] evaluate: use flagcmp for single RHS bitmask expression Patrick McHardy
2014-02-17 17:20 ` Patrick McHardy [this message]
2014-02-17 17:20 ` [PATCH 3/4] netlink_delinarize: convert *all* bitmask values into individual bit values Patrick McHardy
2014-02-17 17:20 ` [PATCH 4/4] netlink: fix prefix expression handling 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=1392657652-9815-3-git-send-email-kaber@trash.net \
--to=kaber@trash.net \
--cc=fw@strlen.de \
--cc=netfilter-devel@vger.kernel.org \
--cc=pablo@netfilter.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).