From: Alan T. DeKok Date: Fri, 25 Mar 2022 22:11:09 +0000 (-0400) Subject: add cf_item_foreach X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=38f97472151fc609a0a86f70511997521799b7d6;p=thirdparty%2Ffreeradius-server.git add cf_item_foreach which makes things much easier --- diff --git a/src/lib/server/cf_file.c b/src/lib/server/cf_file.c index 904c08eb42..8ffbbd0bdc 100644 --- a/src/lib/server/cf_file.c +++ b/src/lib/server/cf_file.c @@ -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); diff --git a/src/lib/server/cf_parse.c b/src/lib/server/cf_parse.c index b53c8819e9..d1746bb837 100644 --- a/src/lib/server/cf_parse.c +++ b/src/lib/server/cf_parse.c @@ -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 diff --git a/src/lib/server/cf_util.c b/src/lib/server/cf_util.c index 70dd444a54..36c3f058a8 100644 --- a/src/lib/server/cf_util.c +++ b/src/lib/server/cf_util.c @@ -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 " : ""; diff --git a/src/lib/server/cf_util.h b/src/lib/server/cf_util.h index a8b4771d96..6237153494 100644 --- a/src/lib/server/cf_util.h +++ b/src/lib/server/cf_util.h @@ -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);