From: Alan T. DeKok Date: Fri, 4 Feb 2022 21:29:54 +0000 (-0500) Subject: move expr printing to callbacks X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=af8892d7b8584b5c50be5914aaae0d5fbb3fda32;p=thirdparty%2Ffreeradius-server.git move expr printing to callbacks and remove xlat_expr_type_t, as it's no longer needed --- diff --git a/src/lib/unlang/xlat_expr.c b/src/lib/unlang/xlat_expr.c index 36f93265fc6..826ff7a2b34 100644 --- a/src/lib/unlang/xlat_expr.c +++ b/src/lib/unlang/xlat_expr.c @@ -127,6 +127,36 @@ static fr_value_box_t *xlat_box(xlat_exp_t *node) return tmpl_value(node->vpt); } +static ssize_t xlat_expr_print_unary(fr_sbuff_t *out, xlat_exp_t const *node, UNUSED void *inst, fr_sbuff_escape_rules_t const *e_rules) +{ + size_t at_in = fr_sbuff_used_total(out); + + FR_SBUFF_IN_STRCPY_RETURN(out, fr_tokens[node->call.func->token]); + xlat_print_node(out, node->child, e_rules); + + return fr_sbuff_used_total(out) - at_in; +} + +static ssize_t xlat_expr_print_binary(fr_sbuff_t *out, xlat_exp_t const *node, UNUSED void *inst, fr_sbuff_escape_rules_t const *e_rules) +{ + size_t at_in = fr_sbuff_used_total(out); + + FR_SBUFF_IN_CHAR_RETURN(out, '('); + xlat_print_node(out, node->child, e_rules); /* prints a space after the first argument */ + + /* + * @todo - when things like "+" support more than 2 arguments, print them all out + * here. + */ + FR_SBUFF_IN_STRCPY_RETURN(out, fr_tokens[node->call.func->token]); + FR_SBUFF_IN_CHAR_RETURN(out, ' '); + xlat_print_node(out, node->child->next, e_rules); + + FR_SBUFF_IN_CHAR_RETURN(out, ')'); + + return fr_sbuff_used_total(out) - at_in; +} + /** Basic purify, but only for expressions and comparisons. * */ @@ -442,8 +472,8 @@ do { \ if (!(xlat = xlat_register(NULL, "op_" STRINGIFY(_name), xlat_func_op_ ## _name, XLAT_FLAG_PURE))) return -1; \ xlat_func_args(xlat, binary_op_xlat_args); \ xlat_internal(xlat); \ + xlat_print_set(xlat, xlat_expr_print_binary); \ xlat->token = _op; \ - xlat->expr_type = XLAT_EXPR_TYPE_BINARY; \ } while (0) #undef XLAT_REGISTER_BINARY_CMP @@ -452,8 +482,8 @@ do { \ if (!(xlat = xlat_register(NULL, "cmp_" STRINGIFY(_name), xlat_func_cmp_ ## _name, XLAT_FLAG_PURE))) return -1; \ xlat_func_args(xlat, binary_op_xlat_args); \ xlat_internal(xlat); \ + xlat_print_set(xlat, xlat_expr_print_binary); \ xlat->token = _op; \ - xlat->expr_type = XLAT_EXPR_TYPE_BINARY; \ } while (0) int xlat_register_expressions(void) @@ -481,14 +511,14 @@ int xlat_register_expressions(void) if (!(xlat = xlat_register(NULL, "logical_and", xlat_func_logical_and, XLAT_FLAG_PURE))) return -1; xlat_func_args(xlat, short_circuit_xlat_args); xlat_internal(xlat); + xlat_print_set(xlat, xlat_expr_print_binary); /* @todo - fix this when we fix the instantiation */ xlat->token = T_LAND; - xlat->expr_type = XLAT_EXPR_TYPE_BINARY; if (!(xlat = xlat_register(NULL, "logical_or", xlat_func_logical_or, XLAT_FLAG_PURE))) return -1; xlat_func_args(xlat, short_circuit_xlat_args); + xlat_print_set(xlat, xlat_expr_print_binary); /* @todo - fix this when we fix the instantiation */ xlat_internal(xlat); xlat->token = T_LOR; - xlat->expr_type = XLAT_EXPR_TYPE_BINARY; /* * -EXPR @@ -497,14 +527,14 @@ int xlat_register_expressions(void) if (!(xlat = xlat_register(NULL, "unary_minus", xlat_func_unary_minus, XLAT_FLAG_PURE))) return -1; xlat_func_args(xlat, unary_minus_xlat_args); xlat_internal(xlat); + xlat_print_set(xlat, xlat_expr_print_unary); xlat->token = T_SUB; - xlat->expr_type = XLAT_EXPR_TYPE_UNARY; if (!(xlat = xlat_register(NULL, "unary_not", xlat_func_unary_not, XLAT_FLAG_PURE))) return -1; xlat_func_args(xlat, unary_not_xlat_args); xlat_internal(xlat); + xlat_print_set(xlat, xlat_expr_print_unary); xlat->token = T_NOT; - xlat->expr_type = XLAT_EXPR_TYPE_UNARY; return 0; } diff --git a/src/lib/unlang/xlat_priv.h b/src/lib/unlang/xlat_priv.h index d4e87f2348b..f7e36e869f9 100644 --- a/src/lib/unlang/xlat_priv.h +++ b/src/lib/unlang/xlat_priv.h @@ -41,19 +41,12 @@ extern "C" { typedef ssize_t (*xlat_print_t)(fr_sbuff_t *in, xlat_exp_t const *self, void *inst, fr_sbuff_escape_rules_t const *e_rules); -typedef enum { - XLAT_EXPR_TYPE_NONE, - XLAT_EXPR_TYPE_UNARY, - XLAT_EXPR_TYPE_BINARY, -} xlat_expr_type_t; - typedef struct xlat_s { fr_rb_node_t node; //!< Entry in the xlat function tree. char const *name; //!< Name of xlat function. xlat_func_t func; //!< async xlat function (async unsafe). bool internal; //!< If true, cannot be redefined. - xlat_expr_type_t expr_type; //!< for expressions fr_token_t token; //!< for expressions module_inst_ctx_t const *mctx; //!< Original module instantiation ctx if this @@ -326,6 +319,7 @@ int xlat_tokenize_expansion(TALLOC_CTX *ctx, xlat_exp_t **head, xlat_flags_t *f int xlat_tokenize_function_args(TALLOC_CTX *ctx, xlat_exp_t **head, xlat_flags_t *flags, fr_sbuff_t *in, tmpl_attr_rules_t const *rules); +ssize_t xlat_print_node(fr_sbuff_t *out, xlat_exp_t const *head, fr_sbuff_escape_rules_t const *e_rules); #ifdef __cplusplus } diff --git a/src/lib/unlang/xlat_tokenize.c b/src/lib/unlang/xlat_tokenize.c index 5f30917570d..8c0a295bf8a 100644 --- a/src/lib/unlang/xlat_tokenize.c +++ b/src/lib/unlang/xlat_tokenize.c @@ -1123,7 +1123,7 @@ void xlat_debug(xlat_exp_t const *node) _xlat_debug(node, 0); } -static ssize_t xlat_print_node(fr_sbuff_t *out, xlat_exp_t const *head, fr_sbuff_escape_rules_t const *e_rules) +ssize_t xlat_print_node(fr_sbuff_t *out, xlat_exp_t const *head, fr_sbuff_escape_rules_t const *e_rules) { ssize_t slen; size_t at_in = fr_sbuff_used_total(out); @@ -1180,31 +1180,7 @@ static ssize_t xlat_print_node(fr_sbuff_t *out, xlat_exp_t const *head, fr_sbuff slen = node->call.func->print(out, node, node->call.inst->data, e_rules); goto done; } - - if (!node->call.func->internal || (node->call.func->expr_type == XLAT_EXPR_TYPE_NONE)) break; - - /* - * Expressions and comparisons. - */ - if (node->call.func->expr_type == XLAT_EXPR_TYPE_UNARY) { - FR_SBUFF_IN_STRCPY_RETURN(out, fr_tokens[node->call.func->token]); - xlat_print_node(out, node->child, e_rules); - - } else { - FR_SBUFF_IN_STRCPY_LITERAL_RETURN(out, "("); - xlat_print_node(out, node->child, e_rules); /* prints a space after the first argument */ - - /* - * @todo - when things like "+" support more than 2 arguments, print them all out - * here. - */ - FR_SBUFF_IN_STRCPY_RETURN(out, fr_tokens[node->call.func->token]); - FR_SBUFF_IN_CHAR_RETURN(out, ' '); - xlat_print_node(out, node->child->next, e_rules); - - FR_SBUFF_IN_STRCPY_LITERAL_RETURN(out, ")"); - } - goto done; + break; default: break;