From: Alan T. DeKok Date: Sat, 20 Feb 2021 17:57:12 +0000 (-0500) Subject: add cf_section_has_parent() X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=f753336d5dc3538a6dc7704df9270d52ecd8a531;p=thirdparty%2Ffreeradius-server.git add cf_section_has_parent() --- diff --git a/src/lib/server/cf_util.c b/src/lib/server/cf_util.c index 27340b43bee..221a764506d 100644 --- a/src/lib/server/cf_util.c +++ b/src/lib/server/cf_util.c @@ -987,6 +987,46 @@ CONF_SECTION *cf_section_find_in_parent(CONF_SECTION const *cs, return NULL; } + +/** Find a parent CONF_SECTION with name1 and optionally name2. + * + * @param[in] cs The section we're searching in. + * @param[in] name1 of the section we're searching for. Special value CF_IDENT_ANY + * can be used to match any name1 value. + * @param[in] name2 of the section we're searching for. Special value CF_IDENT_ANY + * can be used to match any name2 value. + * @return + * - The first matching subsection. + * - NULL if no subsections match. + */ +CONF_SECTION *cf_section_has_parent(CONF_SECTION const *cs, + char const *name1, char const *name2) +{ + CONF_ITEM *parent; + + for (parent = cf_parent(cf_section_to_item(cs)); + parent != NULL; + parent = cf_parent(parent)) { + CONF_SECTION *found = cf_item_to_section(parent); + + if (IS_WILDCARD(name1)) return found; + + if (strcmp(found->name1, name1) != 0) continue; + + if (IS_WILDCARD(name2)) return found; + + if (!name2) { + if (!found->name2) return found; + + return NULL; + } + + if (strcmp(found->name2, name2) == 0) return found; + } + + return NULL; +} + /** Find a pair in a #CONF_SECTION * * @param[in] cs the #CONF_SECTION to search in. diff --git a/src/lib/server/cf_util.h b/src/lib/server/cf_util.h index dfa46a55e27..6086a324f56 100644 --- a/src/lib/server/cf_util.h +++ b/src/lib/server/cf_util.h @@ -152,6 +152,8 @@ CONF_SECTION *cf_section_find_next(CONF_SECTION const *cs, CONF_SECTION const *s char const *name1, char const *name2); CONF_SECTION *cf_section_find_in_parent(CONF_SECTION const *cs, char const *name1, char const *name2); +CONF_SECTION *cf_section_has_parent(CONF_SECTION const *cs, + char const *name1, char const *name2); char const *cf_section_value_find(CONF_SECTION const *, char const *attr);