From: Alan T. DeKok Date: Wed, 20 Apr 2022 20:22:12 +0000 (-0400) Subject: more cleanups to use macros instead of hard-coded fields X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3a5e7028cd8193968556f10cf19186a4acae813c;p=thirdparty%2Ffreeradius-server.git more cleanups to use macros instead of hard-coded fields --- diff --git a/src/lib/unlang/xlat.c b/src/lib/unlang/xlat.c index 413ff35d5bb..3a9c503f505 100644 --- a/src/lib/unlang/xlat.c +++ b/src/lib/unlang/xlat.c @@ -39,13 +39,14 @@ RCSID("$Id$") typedef struct { TALLOC_CTX *ctx; //!< to allocate boxes and values in. TALLOC_CTX *event_ctx; //!< for temporary events - xlat_exp_t const *exp; + xlat_exp_t const *head; //!< of the xlat list + xlat_exp_t const *exp; //!< current one we're evaluating fr_dcursor_t values; //!< Values aggregated so far. /* * For func and alternate */ - fr_value_box_list_t rhead; //!< Head of the result of a nested + fr_value_box_list_t out; //!< Head of the result of a nested ///< expansion. bool alternate; //!< record which alternate branch we ///< previously took. @@ -153,6 +154,7 @@ int unlang_xlat_timeout_add(request_t *request, ev->request = request; ev->fd = -1; ev->timeout = callback; + fr_assert(state->exp->type == XLAT_FUNC); ev->inst = state->exp->call.inst; ev->thread = xlat_thread_instance_find(state->exp); ev->rctx = rctx; @@ -224,7 +226,8 @@ int unlang_xlat_push(TALLOC_CTX *ctx, bool *p_success, fr_value_box_list_t *out, * Allocate its state, and setup a cursor for the xlat nodes */ MEM(frame->state = state = talloc_zero(stack, unlang_frame_state_xlat_t)); - state->exp = talloc_get_type_abort_const(exp, xlat_exp_t); /* Ensure the node is valid */ + state->head = talloc_get_type_abort_const(exp, xlat_exp_t); /* Ensure the node is valid */ + state->exp = state->head; state->success = p_success; state->ctx = ctx; @@ -232,7 +235,7 @@ int unlang_xlat_push(TALLOC_CTX *ctx, bool *p_success, fr_value_box_list_t *out, * Initialise the input and output lists */ fr_dcursor_init(&state->values, out); - fr_value_box_list_init(&state->rhead); + fr_value_box_list_init(&state->out); return 0; } @@ -244,7 +247,7 @@ static unlang_action_t unlang_xlat_repeat(rlm_rcode_t *p_result, request_t *requ xlat_exp_t const *child = NULL; xa = xlat_frame_eval_repeat(state->ctx, &state->values, &child, - &state->alternate, request, &state->exp, &state->rhead); + &state->alternate, request, state->head, &state->exp, &state->out); switch (xa) { case XLAT_ACTION_PUSH_CHILD: fr_assert(child); @@ -256,8 +259,8 @@ static unlang_action_t unlang_xlat_repeat(rlm_rcode_t *p_result, request_t *requ * at this level. A frame may be used to evaluate * multiple sibling nodes. */ - fr_dlist_talloc_free(&state->rhead); - if (unlang_xlat_push(state->ctx, state->success, &state->rhead, request, child, false) < 0) { + fr_dlist_talloc_free(&state->out); + if (unlang_xlat_push(state->ctx, state->success, &state->out, request, child, false) < 0) { *p_result = RLM_MODULE_FAIL; return UNLANG_ACTION_STOP_PROCESSING; } @@ -303,7 +306,7 @@ static unlang_action_t unlang_xlat(rlm_rcode_t *p_result, request_t *request, un xlat_action_t xa; xlat_exp_t const *child = NULL; - xa = xlat_frame_eval(state->ctx, &state->values, &child, request, &state->exp); + xa = xlat_frame_eval(state->ctx, &state->values, &child, request, state->head, &state->exp); switch (xa) { case XLAT_ACTION_PUSH_CHILD: fr_assert(child); @@ -315,8 +318,8 @@ static unlang_action_t unlang_xlat(rlm_rcode_t *p_result, request_t *request, un * at this level. A frame may be used to evaluate * multiple sibling nodes. */ - fr_dlist_talloc_free(&state->rhead); - if (unlang_xlat_push(state->ctx, state->success, &state->rhead, request, child, false) < 0) { + fr_dlist_talloc_free(&state->out); + if (unlang_xlat_push(state->ctx, state->success, &state->out, request, child, false) < 0) { *p_result = RLM_MODULE_FAIL; return UNLANG_ACTION_STOP_PROCESSING; } @@ -404,7 +407,7 @@ static unlang_action_t unlang_xlat_resume(rlm_rcode_t *p_result, request_t *requ xa = xlat_frame_eval_resume(state->ctx, &state->values, state->resume, state->exp, - request, &state->rhead, state->rctx); + request, &state->out, state->rctx); switch (xa) { case XLAT_ACTION_YIELD: repeatable_set(frame); diff --git a/src/lib/unlang/xlat_eval.c b/src/lib/unlang/xlat_eval.c index 1960587e51d..658dc33b307 100644 --- a/src/lib/unlang/xlat_eval.c +++ b/src/lib/unlang/xlat_eval.c @@ -136,7 +136,7 @@ static char *xlat_fmt_aprint(TALLOC_CTX *ctx, xlat_exp_t const *node) /* * No arguments, just print an empty function. */ - if (!node->call.args) return talloc_asprintf(ctx, "%%%c%s:%c", open, node->call.func->name, close); + if (!xlat_exp_head(node->call.args)) return talloc_asprintf(ctx, "%%%c%s:%c", open, node->call.func->name, close); out = talloc_asprintf(ctx, "%%%c%s:", open, node->call.func->name); pool = talloc_pool(NULL, 128); /* Size of a single child (probably ok...) */ @@ -213,7 +213,7 @@ static inline void xlat_debug_log_expansion(request_t *request, xlat_exp_t const * we print the concatenated arguments list as * well as the original fmt string. */ - if ((node->type == XLAT_FUNC) && !xlat_is_literal(node->call.args)) { + if ((node->type == XLAT_FUNC) && !xlat_is_literal(xlat_exp_head(node->call.args))) { RDEBUG2(" (%%%c%s:%pM%c)", (node->call.func->input_type == XLAT_INPUT_ARGS) ? '(' : '{', node->call.func->name, args, @@ -947,7 +947,6 @@ void xlat_signal(xlat_func_signal_t signal, xlat_exp_t const *exp, * @param[in] ctx to allocate value boxes in. * @param[out] out a list of #fr_value_box_t to append to. * @param[in] resume function to call. - * @param[in] exp Xlat node currently being processed. * @param[in] request the current request. * @param[in] result Previously expanded arguments to this xlat function. * @param[in] rctx Opaque (to us), resume ctx provided by xlat function @@ -1006,13 +1005,14 @@ xlat_action_t xlat_frame_eval_resume(TALLOC_CTX *ctx, fr_dcursor_t *out, * @param[in,out] alternate Whether we processed, or have previously processed * the alternate. * @param[in] request the current request. + * @param[in] head of the list to evaluate * @param[in,out] in xlat node to evaluate. Advanced as we process * additional #xlat_exp_t. * @param[in] result of a previous nested evaluation. */ xlat_action_t xlat_frame_eval_repeat(TALLOC_CTX *ctx, fr_dcursor_t *out, xlat_exp_t const **child, bool *alternate, - request_t *request, xlat_exp_t const **in, + request_t *request, xlat_exp_t const *head, xlat_exp_t const **in, fr_value_box_list_t *result) { xlat_exp_t const *node = *in; @@ -1158,8 +1158,8 @@ xlat_action_t xlat_frame_eval_repeat(TALLOC_CTX *ctx, fr_dcursor_t *out, /* * It's easier if we get xlat_frame_eval to continue evaluating the frame. */ - *in = (*in)->next; /* advance */ - return xlat_frame_eval(ctx, out, child, request, in); + *in = xlat_exp_next(head, *in); /* advance */ + return xlat_frame_eval(ctx, out, child, request, head, in); } /** Converts xlat nodes to value boxes @@ -1175,6 +1175,7 @@ xlat_action_t xlat_frame_eval_repeat(TALLOC_CTX *ctx, fr_dcursor_t *out, * should call us with the same #xlat_exp_t and the * result of the nested evaluation in result. * @param[in] request the current request. + * @param[in] head of the list to evaluate * @param[in,out] in xlat node to evaluate. Advanced as we process * additional #xlat_exp_t. * @return @@ -1189,10 +1190,10 @@ xlat_action_t xlat_frame_eval_repeat(TALLOC_CTX *ctx, fr_dcursor_t *out, * - XLAT_ACTION_FAIL an xlat module failed. */ xlat_action_t xlat_frame_eval(TALLOC_CTX *ctx, fr_dcursor_t *out, xlat_exp_t const **child, - request_t *request, xlat_exp_t const **in) + request_t *request, xlat_exp_t const *head, xlat_exp_t const **in) { - xlat_exp_t const *node = *in; xlat_action_t xa = XLAT_ACTION_DONE; + xlat_exp_t const *node; fr_value_box_list_t result; /* tmp list so debug works correctly */ fr_value_box_list_init(&result); @@ -1201,11 +1202,11 @@ xlat_action_t xlat_frame_eval(TALLOC_CTX *ctx, fr_dcursor_t *out, xlat_exp_t con *child = NULL; - if (!node) return XLAT_ACTION_DONE; + if (!*in) return XLAT_ACTION_DONE; XLAT_DEBUG("** [%i] %s >> entered", unlang_interpret_stack_depth(request), __FUNCTION__); - for (node = *in; node; node = (*in)->next) { + for (node = *in; node; node = xlat_exp_next(head, node)) { *in = node; /* Update node in our caller */ fr_dcursor_tail(out); /* Needed for debugging */ VALUE_BOX_TALLOC_LIST_VERIFY(out->dlist); @@ -1223,7 +1224,7 @@ xlat_action_t xlat_frame_eval(TALLOC_CTX *ctx, fr_dcursor_t *out, xlat_exp_t con * If they're found anywhere else the xlat * parser has an error. */ - fr_assert(((node == *in) && !node->next) || (talloc_array_length(node->fmt) > 1)); + fr_assert(((node == *in) && !xlat_exp_next(head, node)) || (talloc_array_length(node->fmt) > 1)); /* * We unfortunately need to dup the buffer @@ -1294,8 +1295,8 @@ xlat_action_t xlat_frame_eval(TALLOC_CTX *ctx, fr_dcursor_t *out, xlat_exp_t con * Hand back the child node to the caller * for evaluation. */ - if (node->call.args) { - *child = node->call.args; + if (xlat_exp_head(node->call.args)) { + *child = xlat_exp_head(node->call.args); xa = XLAT_ACTION_PUSH_CHILD; goto finish; } @@ -1304,7 +1305,7 @@ xlat_action_t xlat_frame_eval(TALLOC_CTX *ctx, fr_dcursor_t *out, xlat_exp_t con * If there's no children we can just * call the function directly. */ - xa = xlat_frame_eval_repeat(ctx, out, child, NULL, request, in, &result); + xa = xlat_frame_eval_repeat(ctx, out, child, NULL, request, head, in, &result); if (xa != XLAT_ACTION_DONE || (!*in)) goto finish; continue; @@ -1701,7 +1702,7 @@ int xlat_eval_walk(xlat_exp_t *head, xlat_walker_t walker, xlat_type_t type, voi /* * Now evaluate the function's arguments */ - if (node->call.args) { + if (xlat_exp_head(node->call.args)) { ret = xlat_eval_walk(node->call.args, walker, type, uctx); if (ret < 0) return ret; } @@ -1716,7 +1717,7 @@ int xlat_eval_walk(xlat_exp_t *head, xlat_walker_t walker, xlat_type_t type, voi /* * Now evaluate the function's arguments */ - if (node->call.args) { + if (xlat_exp_head(node->call.args)) { ret = xlat_eval_walk(node->call.args, walker, type, uctx); if (ret < 0) return ret; } diff --git a/src/lib/unlang/xlat_expr.c b/src/lib/unlang/xlat_expr.c index cde34bee3a4..115732ff527 100644 --- a/src/lib/unlang/xlat_expr.c +++ b/src/lib/unlang/xlat_expr.c @@ -91,7 +91,7 @@ static fr_slen_t xlat_expr_print_unary(fr_sbuff_t *out, xlat_exp_t const *node, 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->call.args, e_rules); + xlat_print_node(out, xlat_exp_head(node->call.args), e_rules); return fr_sbuff_used_total(out) - at_in; } @@ -101,7 +101,7 @@ static fr_slen_t xlat_expr_print_binary(fr_sbuff_t *out, xlat_exp_t const *node, size_t at_in = fr_sbuff_used_total(out); FR_SBUFF_IN_CHAR_RETURN(out, '('); - xlat_print_node(out, node->call.args, e_rules); /* prints a space after the first argument */ + xlat_print_node(out, xlat_exp_head(node->call.args), e_rules); /* prints a space after the first argument */ /* * @todo - when things like "+" support more than 2 arguments, print them all out @@ -109,7 +109,7 @@ static fr_slen_t xlat_expr_print_binary(fr_sbuff_t *out, xlat_exp_t const *node, */ FR_SBUFF_IN_STRCPY_RETURN(out, fr_tokens[node->call.func->token]); FR_SBUFF_IN_CHAR_RETURN(out, ' '); - xlat_print_node(out, node->call.args->next, e_rules); + xlat_print_node(out, xlat_exp_next(node->call.args, xlat_exp_head(node->call.args)), e_rules); FR_SBUFF_IN_CHAR_RETURN(out, ')'); @@ -123,7 +123,6 @@ static int CC_HINT(nonnull) xlat_purify_expr(xlat_exp_t *node) { int rcode = -1; xlat_t const *func; - xlat_exp_t *arg; fr_value_box_t *dst = NULL, *box; xlat_arg_parser_t const *arg_p; xlat_action_t xa; @@ -155,11 +154,12 @@ static int CC_HINT(nonnull) xlat_purify_expr(xlat_exp_t *node) /* * A child isn't a value-box. We leave it alone. */ - for (arg = node->call.args; arg != NULL; arg = arg->next) { + xlat_exp_foreach(node->call.args, arg) { fr_assert(arg->type == XLAT_GROUP); if (!xlat_is_box(arg->group)) return 0; - if (arg->group->next) return 0; + + if (xlat_exp_next(arg->group, xlat_exp_head(arg->group))) return 0; } fr_value_box_list_init(&input); @@ -170,9 +170,8 @@ static int CC_HINT(nonnull) xlat_purify_expr(xlat_exp_t *node) * have to cast the box to the correct data type (or copy * it), and then add the box to the source list. */ - for (arg = node->call.args, arg_p = func->args; - arg != NULL; - arg = arg->next, arg_p++) { + arg_p = func->args; + xlat_exp_foreach(node->call.args, arg) { MEM(box = fr_value_box_alloc_null(node)); if ((arg_p->type != FR_TYPE_VOID) && (arg_p->type != box->type)) { @@ -188,6 +187,7 @@ static int CC_HINT(nonnull) xlat_purify_expr(xlat_exp_t *node) * cast / copy over-writes the list fields. */ fr_dlist_insert_tail(&input, box); + arg_p++; } /* @@ -203,10 +203,7 @@ static int CC_HINT(nonnull) xlat_purify_expr(xlat_exp_t *node) goto cleanup; } - while ((arg = node->call.args) != NULL) { - node->call.args = arg->next; - talloc_free(arg); - } + xlat_exp_free(&node->call.args); dst = fr_dcursor_head(&cursor); fr_assert(dst != NULL); @@ -412,25 +409,24 @@ static fr_slen_t xlat_expr_print_logical(fr_sbuff_t *out, xlat_exp_t const *node { size_t at_in = fr_sbuff_used_total(out); xlat_logical_inst_t *inst = instance; - xlat_exp_t *current = inst->args; + xlat_exp_t *head = inst->args; /* * We might get called before the node is instantiated. */ - if (!inst->args) current = node->call.args; + if (!head) head = node->call.args; - fr_assert(current != NULL); + fr_assert(head != NULL); FR_SBUFF_IN_CHAR_RETURN(out, '('); - while (current) { - xlat_print_node(out, current, e_rules); + xlat_exp_foreach(head, child) { + xlat_print_node(out, child, e_rules); - if (!current->next) break; + if (!xlat_exp_next(head, child)) break; FR_SBUFF_IN_STRCPY_RETURN(out, fr_tokens[node->call.func->token]); FR_SBUFF_IN_CHAR_RETURN(out, ' '); - current = current->next; } FR_SBUFF_IN_CHAR_RETURN(out, ')'); @@ -774,27 +770,6 @@ static const int precedence[T_TOKEN_LAST] = { while (isspace((int) *fr_sbuff_current(_x))) fr_sbuff_advance(_x, 1); \ } while (0) -#if 0 -static xlat_exp_t *xlat_exp_func_alloc_args(TALLOC_CTX *ctx, char const *name, size_t namelen, int argc) -{ - int i; - xlat_exp_t *node, *arg, **last; - - MEM(node = xlat_exp_alloc(ctx, XLAT_FUNC, name, namelen)); - - last = &node->call.args; - for (i = 0; i < argc; i++) { - MEM(arg = xlat_exp_alloc_null(node)); - xlat_exp_set_type(arg, XLAT_GROUP); - arg->quote = T_BARE_WORD; - *last = arg; - last = &arg->next; - } - - return node; -} -#endif - static ssize_t tokenize_expression(TALLOC_CTX *ctx, xlat_exp_t **head, xlat_flags_t *flags, fr_sbuff_t *in, fr_sbuff_parse_rules_t const *p_rules, tmpl_rules_t const *t_rules, fr_token_t prev, fr_sbuff_parse_rules_t const *bracket_rules); diff --git a/src/lib/unlang/xlat_priv.h b/src/lib/unlang/xlat_priv.h index 36da0cdb609..10bd94b751d 100644 --- a/src/lib/unlang/xlat_priv.h +++ b/src/lib/unlang/xlat_priv.h @@ -290,11 +290,11 @@ xlat_action_t xlat_frame_eval_resume(TALLOC_CTX *ctx, fr_dcursor_t *out, xlat_action_t xlat_frame_eval_repeat(TALLOC_CTX *ctx, fr_dcursor_t *out, xlat_exp_t const **child, bool *alternate, - request_t *request, xlat_exp_t const **in, + request_t *request, xlat_exp_t const *head, xlat_exp_t const **in, fr_value_box_list_t *result) CC_HINT(nonnull(1,2,3,5,6)); xlat_action_t xlat_frame_eval(TALLOC_CTX *ctx, fr_dcursor_t *out, xlat_exp_t const **child, - request_t *request, xlat_exp_t const **in); + request_t *request, xlat_exp_t const *head, xlat_exp_t const **in); int xlat_eval_walk(xlat_exp_t *exp, xlat_walker_t walker, xlat_type_t type, void *uctx); diff --git a/src/lib/unlang/xlat_tokenize.c b/src/lib/unlang/xlat_tokenize.c index 22b3667c2d1..2d296df3614 100644 --- a/src/lib/unlang/xlat_tokenize.c +++ b/src/lib/unlang/xlat_tokenize.c @@ -290,7 +290,7 @@ int xlat_validate_function_mono(xlat_exp_t *node) { fr_assert(node->type == XLAT_FUNC); - if (node->call.func->args && node->call.func->args->required && !node->call.args) { + if (node->call.func->args && node->call.func->args->required && !xlat_exp_head(node->call.args)) { fr_strerror_const("Missing required input"); return -1; } @@ -409,7 +409,7 @@ static inline int xlat_tokenize_function_mono(TALLOC_CTX *ctx, xlat_exp_t **head int xlat_validate_function_args(xlat_exp_t *node) { xlat_arg_parser_t const *arg_p; - xlat_exp_t *arg = node->call.args; + xlat_exp_t *arg = xlat_exp_head(node->call.args); fr_assert(node->type == XLAT_FUNC); @@ -1094,7 +1094,7 @@ static void _xlat_debug(xlat_exp_t const *head, int depth) case XLAT_FUNC: fr_assert(node->call.func != NULL); INFO_INDENT("xlat (%s)", node->call.func->name); - if (node->call.args) { + if (xlat_exp_head(node->call.args)) { INFO_INDENT("{"); _xlat_debug(node->call.args, depth + 1); INFO_INDENT("}"); @@ -1103,7 +1103,7 @@ static void _xlat_debug(xlat_exp_t const *head, int depth) case XLAT_FUNC_UNRESOLVED: INFO_INDENT("xlat-unresolved (%s)", node->fmt); - if (node->call.args) { + if (xlat_exp_head(node->call.args)) { INFO_INDENT("{"); _xlat_debug(node->call.args, depth + 1); INFO_INDENT("}"); @@ -1241,7 +1241,7 @@ ssize_t xlat_print_node(fr_sbuff_t *out, xlat_exp_t const *head, fr_sbuff_escape FR_SBUFF_IN_BSTRCPY_BUFFER_RETURN(out, node->call.func->name); FR_SBUFF_IN_CHAR_RETURN(out, ':'); - if (node->call.args) { + if (xlat_exp_head(node->call.args)) { slen = xlat_print(out, node->call.args, &xlat_escape); if (slen < 0) goto error; } @@ -1251,7 +1251,7 @@ ssize_t xlat_print_node(fr_sbuff_t *out, xlat_exp_t const *head, fr_sbuff_escape FR_SBUFF_IN_BSTRCPY_BUFFER_RETURN(out, node->fmt); FR_SBUFF_IN_CHAR_RETURN(out, ':'); - if (node->call.args) { + if (xlat_exp_head(node->call.args)) { slen = xlat_print(out, node->call.args, &xlat_escape); if (slen < 0) goto error; }