]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
move init FR_TYPE_PAIR_CURSOR dcursor into tmpl_dcursor.c
authorAlan T. DeKok <aland@freeradius.org>
Tue, 13 May 2025 17:54:47 +0000 (13:54 -0400)
committerAlan T. DeKok <aland@freeradius.org>
Tue, 13 May 2025 17:54:47 +0000 (13:54 -0400)
src/lib/server/tmpl_dcursor.c
src/lib/server/tmpl_dcursor.h
src/lib/unlang/xlat_eval.c

index ac7ddbfeed73d0c266b7b174d87c49f7145838ca..ba7d1b00c2873bbe405cef8f71eeb356686acd34 100644 (file)
@@ -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
index 30f8375e3102fa8fbf97605bc97963e520dd9395..80973ec69f8a9790e0b18db9b418da8d434df810 100644 (file)
@@ -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);
index c505c0f1b86de48d781649a7af76fa3f8b159c2a..74b493a5be9a21d85be4db94a2d632e94cd64b15 100644 (file)
@@ -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