]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
add cf_item_foreach
authorAlan T. DeKok <aland@freeradius.org>
Fri, 25 Mar 2022 22:11:09 +0000 (18:11 -0400)
committerAlan T. DeKok <aland@freeradius.org>
Sat, 26 Mar 2022 01:52:06 +0000 (21:52 -0400)
which makes things much easier

src/lib/server/cf_file.c
src/lib/server/cf_parse.c
src/lib/server/cf_util.c
src/lib/server/cf_util.h

index 904c08eb425636e6dcbe8b03070c70283d8b3b4c..8ffbbd0bdc1fc4ef69dcacfca9b04f6ab232056f 100644 (file)
@@ -425,8 +425,6 @@ char const *cf_expand_variables(char const *cf, int lineno,
  */
 static bool cf_template_merge(CONF_SECTION *cs, CONF_SECTION const *template)
 {
-       CONF_ITEM *ci = NULL;
-
        if (!cs || !template) return true;
 
        cs->template = NULL;
@@ -436,7 +434,7 @@ static bool cf_template_merge(CONF_SECTION *cs, CONF_SECTION const *template)
         *      current section.  But only if the entries don't
         *      already exist in the current section.
         */
-       while ((ci = fr_dlist_next(&template->item.children, ci))) {
+       cf_item_foreach(&template->item, ci) {
                if (ci->type == CONF_ITEM_PAIR) {
                        CONF_PAIR *cp1, *cp2;
 
@@ -704,9 +702,7 @@ bool cf_file_check(CONF_SECTION *cs, char const *filename, bool check_perms)
  */
 int cf_section_pass2(CONF_SECTION *cs)
 {
-       CONF_ITEM *ci = NULL;
-
-       while ((ci = fr_dlist_next(&cs->item.children, ci))) {
+       cf_item_foreach(&cs->item, ci) {
                char const      *value;
                CONF_PAIR       *cp;
                char            buffer[8192];
@@ -727,8 +723,7 @@ int cf_section_pass2(CONF_SECTION *cs)
                cp->value = talloc_typed_strdup(cp, value);
        }
 
-       ci = NULL;
-       while ((ci = fr_dlist_next(&cs->item.children, ci))) {
+       cf_item_foreach(&cs->item, ci) {
                if (ci->type != CONF_ITEM_SECTION) continue;
 
                if (cf_section_pass2(cf_item_to_section(ci)) < 0) return -1;
@@ -2515,8 +2510,6 @@ static int cf_pair_write(FILE *fp, CONF_PAIR *cp)
 
 int cf_section_write(FILE *fp, CONF_SECTION *cs, int depth)
 {
-       CONF_ITEM       *ci = NULL;
-
        if (!fp || !cs) return -1;
 
        /*
@@ -2554,7 +2547,7 @@ int cf_section_write(FILE *fp, CONF_SECTION *cs, int depth)
         *      Loop over the children.  Either recursing, or opening
         *      a new file.
         */
-       while ((ci = fr_dlist_next(&cs->item.children, ci))) {
+       cf_item_foreach(&cs->item, ci) {
                switch (ci->type) {
                case CONF_ITEM_SECTION:
                        cf_section_write(fp, cf_item_to_section(ci), depth + 1);
index b53c8819e92d363dbd46329b2364cf71a76e7c63..d1746bb8376ecdb390394626d794a147e64de533 100644 (file)
@@ -1030,9 +1030,7 @@ static int cf_section_parse_init(CONF_SECTION *cs, void *base, CONF_PARSER const
 
 static void cf_section_parse_warn(CONF_SECTION *cs)
 {
-       CONF_ITEM *ci = NULL;
-
-       while ((ci = fr_dlist_next(&cs->item.children, ci))) {
+       cf_item_foreach(&cs->item, ci) {
                /*
                 *      Don't recurse on sections. We can only safely
                 *      check conf pairs at the same level as the
index 70dd444a540a85a927ea2e8bc99dab3b755b7c74..36c3f058a886e9f5819ae1895cb09b89c5721afb 100644 (file)
@@ -119,15 +119,13 @@ static CONF_ITEM *cf_find(CONF_ITEM const *parent, CONF_ITEM_TYPE type, char con
         *      No ident1, iterate over the child list
         */
        if (IS_WILDCARD(ident1)) {
-               CONF_ITEM *ci = NULL;
-
-               while ((ci = fr_dlist_next(&parent->children, ci))) {
+               cf_item_foreach(parent, ci) {
                        if (find->type != ci->type) continue;
 
-                       if (cf_ident2_cmp(find, ci) == 0) break;
+                       if (cf_ident2_cmp(find, ci) == 0) return ci;
                }
 
-               return ci;
+               return NULL;
        }
 
        /*
@@ -858,7 +856,6 @@ CONF_SECTION *cf_section_dup(TALLOC_CTX *ctx, CONF_SECTION *parent, CONF_SECTION
 {
        CONF_SECTION    *new, *subcs;
        CONF_PAIR       *cp;
-       CONF_ITEM       *ci = NULL;
 
        new = cf_section_alloc(ctx, parent, name1, name2);
 
@@ -871,7 +868,7 @@ CONF_SECTION *cf_section_dup(TALLOC_CTX *ctx, CONF_SECTION *parent, CONF_SECTION
        cf_filename_set(new, cs->item.filename);
        cf_lineno_set(new, cs->item.lineno);
 
-       while ((ci = fr_dlist_next(&cs->item.children, ci))) {
+       cf_item_foreach(&cs->item, ci) {
                switch (ci->type) {
                case CONF_ITEM_SECTION:
                        subcs = cf_item_to_section(ci);
@@ -2005,8 +2002,6 @@ void _cf_log_by_child(fr_log_type_t type, CONF_SECTION const *parent, char const
  */
 void _cf_debug(CONF_ITEM const *ci)
 {
-       CONF_ITEM const *child = NULL;
-
        /*
         *      Print summary of the item
         */
@@ -2076,7 +2071,7 @@ void _cf_debug(CONF_ITEM const *ci)
         */
        DEBUG("CHILDREN");
 
-       while ((child = fr_dlist_next(&ci->children, child))) {
+       cf_item_foreach(ci, child) {
                char const *in_ident1, *in_ident2;
 
                in_ident1 = fr_rb_find(ci->ident1, child) == child? "in ident1 " : "";
index a8b4771d9610fb74e8ad12a7213adda3f1fe8604..6237153494d44f192a77070cc13a96dd2108533e 100644 (file)
@@ -92,6 +92,15 @@ CONF_ITEM    *_cf_item_remove(CONF_ITEM *parent, CONF_ITEM *child);
 #define                cf_item_next(_ci, _prev) _cf_item_next(CF_TO_ITEM(_ci), _prev)
 CONF_ITEM      *_cf_item_next(CONF_ITEM const *ci, CONF_ITEM const *prev);
 
+/** Iterate over the contents of a list
+ *
+ * @param[in] _ci              to iterate over.
+ * @param[in] _iter            Name of iteration variable.
+ *                             Will be declared in the scope of the loop.
+ */
+#define cf_item_foreach(_ci, _iter) \
+       for (CONF_ITEM *_iter = fr_dlist_head(&(_ci)->children); _iter; _iter = fr_dlist_next(&(_ci)->children, _iter))
+
 #define                cf_root(_cf) _cf_root(CF_TO_ITEM(_cf))
 CONF_SECTION   *_cf_root(CONF_ITEM const *ci);