From: Alan T. DeKok Date: Tue, 13 May 2025 17:54:47 +0000 (-0400) Subject: move init FR_TYPE_PAIR_CURSOR dcursor into tmpl_dcursor.c X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c9fa78001cb76a8787dbbc42c7f9c6bd96e76518;p=thirdparty%2Ffreeradius-server.git move init FR_TYPE_PAIR_CURSOR dcursor into tmpl_dcursor.c --- diff --git a/src/lib/server/tmpl_dcursor.c b/src/lib/server/tmpl_dcursor.c index ac7ddbfeed7..ba7d1b00c28 100644 --- a/src/lib/server/tmpl_dcursor.c +++ b/src/lib/server/tmpl_dcursor.c @@ -503,6 +503,59 @@ void tmpl_dcursor_clear(tmpl_dcursor_ctx_t *cc) TALLOC_FREE(cc->pool); } +/** Initialize a #tmpl_dcursor_t into a #fr_value_box_t + * + * The #tmpl_dcursor_ctx_t and #tmpl_dcursor_t are associated with the + * input value-box, and will be freed when the value-box is freed. + * + * @param[out] err May be NULL if no error code is required. + * Will be set to: + * - 0 on success. + * - -1 if no matching #fr_pair_t could be found. + * - -2 if list could not be found (doesn't exist in current #request_t). + * - -3 if context could not be found (no parent #request_t available). + * @param[in] vb Where the #tmpl_dursor_t is stored. MUST be talloc'd + * @param[in] request the current request. + * @param[in] vpt specifying the #fr_pair_t type or list to iterate over. + * @return + * - First #fr_pair_t specified by the #tmpl_t. + * - NULL if no matching #fr_pair_t found, and NULL on error. + * + * @see tmpl_cursor_next + */ +fr_pair_t *tmpl_dcursor_value_box_init(int *err, fr_value_box_t *vb, request_t *request, tmpl_t const *vpt) +{ + tmpl_dcursor_ctx_t *cc; + fr_dcursor_t *cursor; + fr_pair_t *vp; + + MEM(cc = talloc(vb, tmpl_dcursor_ctx_t)); + MEM(cursor = talloc(vb, fr_dcursor_t)); + + /* + * The cursor can return something, nothing (-1), or no list (-2) or no context (-3). Of + * these, only the last two are actually errors. + * + * "no matching pair" is a valid answer, and can be passed to the function. + */ + vp = tmpl_dcursor_init(err, vb, cc, cursor, request, vpt); + if (!vp) { + if (!err) return NULL; + + if (*err == -1) { + RWDEBUG("Cursor %s returned no attributes", vpt->name); + } else { + RPEDEBUG("Failed initializing cursor"); + } + + return NULL; + } + + fr_value_box_set_cursor(vb, FR_TYPE_PAIR_CURSOR, cursor, vpt->name); + return vp; +} + + /** Simple pair building callback for use with tmpl_dcursors * * Which always appends the new pair to the tail of the list diff --git a/src/lib/server/tmpl_dcursor.h b/src/lib/server/tmpl_dcursor.h index 30f8375e310..80973ec69f8 100644 --- a/src/lib/server/tmpl_dcursor.h +++ b/src/lib/server/tmpl_dcursor.h @@ -103,4 +103,6 @@ fr_pair_t *tmpl_dcursor_pair_build(fr_pair_t *parent, fr_dcursor_t *cursor, fr_d #define tmpl_dcursor_build_init(_err, _ctx, _cc, _cursor, _request, _vpt, _build, _uctx) \ _tmpl_dcursor_init(_err, _ctx, _cc, _cursor, _request, _vpt, _build, _uctx) +fr_pair_t *tmpl_dcursor_value_box_init(int *err, fr_value_box_t *vb, request_t *request, tmpl_t const *vpt) CC_HINT(nonnull(2,3,4)); + ssize_t tmpl_dcursor_print(fr_sbuff_t *out, tmpl_dcursor_ctx_t const *cc); diff --git a/src/lib/unlang/xlat_eval.c b/src/lib/unlang/xlat_eval.c index c505c0f1b86..74b493a5be9 100644 --- a/src/lib/unlang/xlat_eval.c +++ b/src/lib/unlang/xlat_eval.c @@ -496,8 +496,6 @@ check_non_leaf: { int err; tmpl_t *vpt; - tmpl_dcursor_ctx_t *cc; - fr_dcursor_t *cursor; /* * Cursor names have to be strings, which are completely safe. @@ -526,8 +524,6 @@ check_non_leaf: } fr_value_box_clear_value(vb); - MEM(cc = talloc(vb, tmpl_dcursor_ctx_t)); - MEM(cursor = talloc(vb, fr_dcursor_t)); /* * The cursor can return something, nothing (-1), or no list (-2) or no context (-3). Of @@ -535,16 +531,8 @@ check_non_leaf: * * "no matching pair" is a valid answer, and can be passed to the function. */ - (void) tmpl_dcursor_init(&err, vb, cc, cursor, request, vpt); - if (err < 0) { - if (err < -1) { - RPEDEBUG("Failed initializing cursor"); - return XLAT_ACTION_FAIL; - } - - RWDEBUG("Cursor %s returned no attributes", vpt->name); - } - fr_value_box_set_cursor(vb, FR_TYPE_PAIR_CURSOR, cursor, vpt->name); + (void) tmpl_dcursor_value_box_init(&err, vb, request, vpt); + if (err < -1) return XLAT_ACTION_FAIL; } #undef ESCAPE