expression: default to print binary operations using nominal representation
Pablo Neira Ayuso
maintainerRFC PATCH 1/2 expression: default to print binary operations using nominal representation
Since b59e65c ("scanner: add aliases to symbols for easier interaction with most shells") we have nominal aliases, default to these for the output representation.
--- src/expression.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-)
src/expression.c +10 -10
@@ -351,17 +351,17 @@ const char *expr_op_symbols[] = { [OP_INVALID] = "invalid", [OP_HTON] = "hton", [OP_NTOH] = "ntoh", - [OP_AND] = "&", - [OP_OR] = "|", - [OP_XOR] = "^", - [OP_LSHIFT] = "<<", - [OP_RSHIFT] = ">>", + [OP_AND] = "and", + [OP_OR] = "or", + [OP_XOR] = "xor", + [OP_LSHIFT] = "lshift", + [OP_RSHIFT] = "rshift", [OP_EQ] = NULL, - [OP_NEQ] = "!=", - [OP_LT] = "<", - [OP_GT] = ">", - [OP_LTE] = "<=", - [OP_GTE] = ">=", + [OP_NEQ] = "ne", + [OP_LT] = "lt", + [OP_GT] = "gt", + [OP_LTE] = "le", + [OP_GTE] = "ge", [OP_RANGE] = "within range", [OP_LOOKUP] = NULL, };
Signature · 1 line
1.7.10.4
Pablo Neira Ayuso
maintainerPATCH 2/2 expression: fix printing of binary operation
This patch adds a special print function for the relational case in which == is assumed, so it's not printed. It also fixes the output of binary operations from:
& 0x00000003 0x00000001
to:
and 0x00000003 == 0x00000001
An example output of how thing are left after this patch follows:
% nft list table filter
table ip filter {
chain output {
type filter hook output priority 0;
meta mark and 0x00000003 == 0x00000001 counter packets 0 bytes 0
meta mark 0x00000003 counter packets 0 bytes 0
}
}--- src/expression.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-)
src/expression.c +10 -1
@@ -411,7 +411,9 @@ static void binop_expr_print(const struct expr *expr)printf (" %s " , expr_op_symbols[expr->op]); else printf(" "); +expr_print (expr->right); +printf (" ==" ); }static void binop_expr_clone (struct expr *new,const struct expr *expr)
@@ -447,10 +449,17 @@ struct expr *binop_expr_alloc(const struct location *loc, enum ops op,return expr; } +static void relational_expr_print (const struct expr *expr) +{ +expr_print (expr->left); +printf (" " ); +expr_print (expr->right); +} +static const struct expr_ops relational_expr_ops = { .type =EXPR_RELATIONAL , .name ="relational" , - .print = binop_expr_print, + .print = relational_expr_print, .destroy = binop_expr_destroy, };
Signature · 1 line
1.7.10.4
Quoted 3 lines · Pablo Neira Ayuso
Since b59e65c ("scanner: add aliases to symbols for easier interaction with most shells") we have nominal aliases, default to these for the output representation.
I was thinking about this before, but didn't mention it since you hadn't included it in your first patch. I'd prefer to stick to the IMO more readable existing form. Since we don't support deleting rules by rule specification, there should be no reason to copy and paste the output to the command line again.
Quoted 39 lines
Signed-off-by Pablo Neira Ayuso <[email protected]>--- src/expression.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-)src/expression.c +10
-10@@ -351,17 +351,17 @@ const char *expr_op_symbols[] = { [OP_INVALID] = "invalid", [OP_HTON] = "hton", [OP_NTOH] = "ntoh", - [OP_AND] = "&", - [OP_OR] = "|", - [OP_XOR] = "^", - [OP_LSHIFT] = "<<", - [OP_RSHIFT] = ">>", + [OP_AND] = "and", + [OP_OR] = "or", + [OP_XOR] = "xor", + [OP_LSHIFT] = "lshift", + [OP_RSHIFT] = "rshift", [OP_EQ] = NULL, - [OP_NEQ] = "!=", - [OP_LT] = "<", - [OP_GT] = ">", - [OP_LTE] = "<=", - [OP_GTE] = ">=", + [OP_NEQ] = "ne", + [OP_LT] = "lt", + [OP_GT] = "gt", + [OP_LTE] = "le", + [OP_GTE] = "ge", [OP_RANGE] = "within range", [OP_LOOKUP] = NULL, };Signature · 1 line
1.7.10.4
Re: [PATCH 2/2] expression: fix printing of binary operation
In reply to Pablo Neira AyusoQuoted 15 lines · Pablo Neira Ayuso
This patch adds a special print function for the relational case in which == is assumed, so it's not printed. It also fixes the output of binary operations from:
&
0x000000030x00000001to:
and
0x00000003==0x00000001src/expression.c +0
-0
src/expression.c static void binop_expr_print(const struct expr *expr) · line 416
Quoted 6 lines
src/expression.c +2
-0@@ -411,7 +411,9 @@ static void binop_expr_print(const struct expr *expr)printf (" %s " , expr_op_symbols[expr->op]); else printf(" "); +expr_print (expr->right); +printf (" ==" );
That doesn't look right, binops can also occur outside of relational expressions. I'd suggest to special case OP_EQ and not print it by default unless the LHS is an EXPR_BINOP.
Pablo Neira Ayuso
maintainerRe: [PATCH 2/2] expression: fix printing of binary operation
Quoted 11 lines · Patrick McHardy
Quoted 10 lines · Pablo Neira Ayuso
This patch adds a special print function for the relational case in which == is assumed, so it's not printed. It also fixes the output of binary operations from:
&
0x000000030x00000001to:
and
0x00000003==0x00000001
Quoted 15 lines
src/expression.c static void binop_expr_print(const struct expr *expr) · line 416
Quoted 11 lines
src/expression.c +2
-0@@ -411,7 +411,9 @@ static void binop_expr_print(const struct expr *expr)printf (" %s " , expr_op_symbols[expr->op]); else printf(" "); +expr_print (expr->right); +printf (" ==" );That doesn't look right, binops can also occur outside of relational expressions. I'd suggest to special case
OP_EQand not print it by default unless the LHS is anEXPR_BINOP.
Indeed, this can be !=.
Re: [PATCH 2/2] expression: fix printing of binary operation
In reply to Patrick McHardyQuoted 11 lines · Patrick McHardy
Quoted 10 lines · Pablo Neira Ayuso
This patch adds a special print function for the relational case in which == is assumed, so it's not printed. It also fixes the output of binary operations from:
&
0x000000030x00000001to:
and
0x00000003==0x00000001
Quoted 15 lines
src/expression.c static void binop_expr_print(const struct expr *expr) · line 416
Quoted 11 lines
src/expression.c +2
-0@@ -411,7 +411,9 @@ static void binop_expr_print(const struct expr *expr)printf (" %s " , expr_op_symbols[expr->op]); else printf(" "); +expr_print (expr->right); +printf (" ==" );That doesn't look right, binops can also occur outside of relational expressions. I'd suggest to special case
OP_EQand not print it by default unless the LHS is anEXPR_BINOP.
Something like this:
src/expression.c +4 -2
@@ -356,7 +356,7 @@ const char *expr_op_symbols[] = { [OP_XOR] = "^", [OP_LSHIFT] = "<<", [OP_RSHIFT] = ">>", - [OP_EQ] = NULL, + [OP_EQ] = "==", [OP_NEQ] = "!=", [OP_LT] = "<", [OP_GT] = ">",
@@ -407,7 +407,9 @@ struct expr *unary_expr_alloc(const struct location *loc,static void binop_expr_print (const struct expr *expr) {expr_print (expr->left); -if (expr_op_symbols[expr->op] !=NULL ) +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 (" " );
Re: [PATCH 2/2] expression: fix printing of binary operation
In reply to Pablo Neira AyusoQuoted 28 lines · Pablo Neira Ayuso
Quoted 11 lines · Patrick McHardy
Quoted 10 lines · Pablo Neira Ayuso
This patch adds a special print function for the relational case in which == is assumed, so it's not printed. It also fixes the output of binary operations from:
&
0x000000030x00000001to:
and
0x00000003==0x00000001Quoted 15 lines
src/expression.c static void binop_expr_print(const struct expr *expr) · line 416
Quoted 11 lines
src/expression.c +2
-0@@ -411,7 +411,9 @@ static void binop_expr_print(const struct expr *expr)printf (" %s " , expr_op_symbols[expr->op]); else printf(" "); +expr_print (expr->right); +printf (" ==" );That doesn't look right, binops can also occur outside of relational expressions. I'd suggest to special case
OP_EQand not print it by default unless the LHS is anEXPR_BINOP.
Quoted 1 line
Indeed, this can be !=.
Yep, that as well. The patch I just sent should handle it correctly.
Quoted 10 lines · Patrick McHardy
Quoted 3 lines · Pablo Neira Ayuso
Since b59e65c ("scanner: add aliases to symbols for easier interaction with most shells") we have nominal aliases, default to these for the output representation.
I was thinking about this before, but didn't mention it since you hadn't included it in your first patch. I'd prefer to stick to the IMO more readable existing form. Since we don't support deleting rules by rule specification, there should be no reason to copy and paste the output to the command line again.
What about a file to be loaded with `nft -f'?
In fact, I think most of users will end with a config file to load with `nft -f'.
It may be annoying getting the output different to what you wrote.
I think this patch means that the symbol-free syntax is the default, and I personally like that.
Signature · 5 lines
Arturo Borrero González -- To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 12 lines · Arturo Borrero Gonzalez
Quoted 10 lines · Patrick McHardy
Quoted 3 lines · Pablo Neira Ayuso
Since b59e65c ("scanner: add aliases to symbols for easier interaction with most shells") we have nominal aliases, default to these for the output representation.
I was thinking about this before, but didn't mention it since you hadn't included it in your first patch. I'd prefer to stick to the IMO more readable existing form. Since we don't support deleting rules by rule specification, there should be no reason to copy and paste the output to the command line again.
Quoted 3 lines
What about a file to be loaded with `nft -f'?
In fact, I think most of users will end with a config file to load with `nft -f'.
Indeed, and those users don't need the long syntax since the shell is not involved.
Quoted 1 line
It may be annoying getting the output different to what you wrote.
That may happen for other reasons anyways, especially with binops we optimize away redundant expressions and calculate constants in userspace.
Quoted 2 lines
I think this patch means that the symbol-free syntax is the default, and I personally like that.
I don't, its longer and harder to read. It has its justification for parsing on the command line, but as you say, most users won't use it that way.
Pablo Neira Ayuso
maintainerRe: [PATCH 2/2] expression: fix printing of binary operation
In reply to Patrick McHardyQuoted 45 lines · Patrick McHardy
Quoted 11 lines · Patrick McHardy
Quoted 10 lines · Pablo Neira Ayuso
This patch adds a special print function for the relational case in which == is assumed, so it's not printed. It also fixes the output of binary operations from:
&
0x000000030x00000001to:
and
0x00000003==0x00000001Quoted 15 lines
src/expression.c static void binop_expr_print(const struct expr *expr) · line 416
Quoted 11 lines
src/expression.c +2
-0@@ -411,7 +411,9 @@ static void binop_expr_print(const struct expr *expr)printf (" %s " , expr_op_symbols[expr->op]); else printf(" "); +expr_print (expr->right); +printf (" ==" );That doesn't look right, binops can also occur outside of relational expressions. I'd suggest to special case
OP_EQand not print it by default unless the LHS is anEXPR_BINOP.Something like this:
src/expression.c +1
-1@@ -356,7 +356,7 @@ const char *expr_op_symbols[] = { [OP_XOR] = "^", [OP_LSHIFT] = "<<", [OP_RSHIFT] = ">>", - [OP_EQ] = NULL, + [OP_EQ] = "==", [OP_NEQ] = "!=", [OP_LT] = "<", [OP_GT] = ">",
src/expression.c struct expr *unary_expr_alloc(const struct location *loc, · line 415
Quoted 10 lines
src/expression.c +3
-1@@ -407,7 +407,9 @@ struct expr *unary_expr_alloc(const struct location *loc,static void binop_expr_print (const struct expr *expr) {expr_print (expr->left); -if (expr_op_symbols[expr->op] !=NULL ) +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 (" " );
This looks a bit more complicated. To my understanding, the right-hand side of the relational tree contains the value. The left-hand side contains the binop tree, whose left-hand side is the meta mark and the right-hand side is the value to apply the operation. The print function doesn't have context to know what's on the right-hand side of the upper relational expression. Thinking how to fix this...
Re: [PATCH 2/2] expression: fix printing of binary operation
Quoted 33 lines · Pablo Neira Ayuso
Quoted 32 lines · Patrick McHardy
Quoted 4 lines · Patrick McHardy
That doesn't look right, binops can also occur outside of relational expressions. I'd suggest to special case
OP_EQand not print it by default unless the LHS is anEXPR_BINOP.Something like this:
src/expression.c +4
-2@@ -356,7 +356,7 @@ const char *expr_op_symbols[] = { [OP_XOR] = "^", [OP_LSHIFT] = "<<", [OP_RSHIFT] = ">>", - [OP_EQ] = NULL, + [OP_EQ] = "==", [OP_NEQ] = "!=", [OP_LT] = "<", [OP_GT] = ">",@@ -407,7 +407,9 @@ struct expr *unary_expr_alloc(const struct location *loc,static void binop_expr_print (const struct expr *expr) {expr_print (expr->left); -if (expr_op_symbols[expr->op] !=NULL ) +if (expr_op_symbols[expr->op] && + (expr->op !=OP_EQ || + expr->left->ops->type ==EXPR_BINOP ))printf (" %s " , expr_op_symbols[expr->op]);else
Quoted 8 lines
src/expression.c struct expr *unary_expr_alloc(const struct location *loc, · line 415
Quoted 1 line
src/expression.c +0
-0printf (" " );This looks a bit more complicated. To my understanding, the right-hand side of the relational tree contains the value. The left-hand side contains the binop tree, whose left-hand side is the meta mark and the right-hand side is the value to apply the operation. The print function doesn't have context to know what's on the right-hand side of the upper relational expression. Thinking how to fix this...
This OP_EQ case is the upper relational expression. Try it, it works fine :)
Pablo Neira Ayuso
maintainerRe: [PATCH 2/2] expression: fix printing of binary operation
Quoted 43 lines · Patrick McHardy
Quoted 33 lines · Pablo Neira Ayuso
Quoted 32 lines · Patrick McHardy
Quoted 4 lines · Patrick McHardy
That doesn't look right, binops can also occur outside of relational expressions. I'd suggest to special case
OP_EQand not print it by default unless the LHS is anEXPR_BINOP.Something like this:
src/expression.c +4
-2@@ -356,7 +356,7 @@ const char *expr_op_symbols[] = { [OP_XOR] = "^", [OP_LSHIFT] = "<<", [OP_RSHIFT] = ">>", - [OP_EQ] = NULL, + [OP_EQ] = "==", [OP_NEQ] = "!=", [OP_LT] = "<", [OP_GT] = ">",@@ -407,7 +407,9 @@ struct expr *unary_expr_alloc(const struct location *loc,static void binop_expr_print (const struct expr *expr) {expr_print (expr->left); -if (expr_op_symbols[expr->op] !=NULL ) +if (expr_op_symbols[expr->op] && + (expr->op !=OP_EQ || + expr->left->ops->type ==EXPR_BINOP ))printf (" %s " , expr_op_symbols[expr->op]);else Quoted 8 lines
src/expression.c struct expr *unary_expr_alloc(const struct location *loc, · line 415
Quoted 1 line
src/expression.c +0
-0printf (" " );This looks a bit more complicated. To my understanding, the right-hand side of the relational tree contains the value. The left-hand side contains the binop tree, whose left-hand side is the meta mark and the right-hand side is the value to apply the operation. The print function doesn't have context to know what's on the right-hand side of the upper relational expression. Thinking how to fix this...
Quoted 1 line
This
OP_EQcase is the upper relational expression. Try it, it works fine :)
Indeed, thanks Patrick.