]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
Add xlat_init function
authorArran Cudbard-Bell <a.cudbardb@freeradius.org>
Sat, 18 Nov 2017 20:21:11 +0000 (20:21 +0000)
committerArran Cudbard-Bell <a.cudbardb@freeradius.org>
Sat, 18 Nov 2017 20:21:11 +0000 (20:21 +0000)
src/include/xlat.h
src/main/xlat_func.c

index 1510a8ae60be0c8fd858a62dd29fb00d8decf85c..08586c5fb521494160454160de75a1f60f881933 100644 (file)
@@ -115,6 +115,7 @@ int         xlat_register(void *mod_inst, char const *name,
 void           xlat_unregister(void *mod_inst, char const *name, xlat_func_t func);
 void           xlat_unregister_module(void *instance);
 int            xlat_register_redundant(CONF_SECTION *cs);
+int            xlat_init(void);
 void           xlat_free(void);
 
 #ifdef __cplusplus
index 2f6aa035fdbcb3cc7c377a45c93ac65d6c73205a..146da0d6f32ff4725d519d835005c194a85f4880 100644 (file)
@@ -626,62 +626,13 @@ int xlat_register(void *mod_inst, char const *name,
        xlat_t  *c;
        xlat_t  my_xlat;
 
+       if (!xlat_root) xlat_init();
+
        if (!name || !*name) {
                ERROR("%s: Invalid xlat name", __FUNCTION__);
                return -1;
        }
 
-       /*
-        *      First time around, build up the tree...
-        *
-        *      FIXME: This code should be hoisted out of this function,
-        *      and into a global "initialization".  But it isn't critical...
-        */
-       if (!xlat_root) {
-#ifdef WITH_UNLANG
-               int i;
-#endif
-
-               xlat_root = rbtree_create(NULL, xlat_cmp, NULL, RBTREE_FLAG_REPLACE);
-               if (!xlat_root) {
-                       ERROR("%s: Failed to create tree", __FUNCTION__);
-                       return -1;
-               }
-
-#ifdef WITH_UNLANG
-               for (i = 0; xlat_foreach_names[i] != NULL; i++) {
-                       xlat_register(&xlat_foreach_inst[i], xlat_foreach_names[i], xlat_foreach, NULL, NULL, 0, XLAT_DEFAULT_BUF_LEN, true);
-                       c = xlat_find(xlat_foreach_names[i]);
-                       rad_assert(c != NULL);
-                       c->internal = true;
-               }
-#endif
-
-#define XLAT_REGISTER(_x) xlat_register(NULL, STRINGIFY(_x), xlat_ ## _x, NULL, NULL, 0, XLAT_DEFAULT_BUF_LEN, true); \
-               c = xlat_find(STRINGIFY(_x)); \
-               rad_assert(c != NULL); \
-               c->internal = true
-
-               XLAT_REGISTER(integer);
-               XLAT_REGISTER(strlen);
-               XLAT_REGISTER(length);
-               XLAT_REGISTER(hex);
-               XLAT_REGISTER(tag);
-               XLAT_REGISTER(string);
-               XLAT_REGISTER(xlat);
-               XLAT_REGISTER(map);
-               XLAT_REGISTER(module);
-               XLAT_REGISTER(debug_attr);
-#if defined(HAVE_REGEX) && defined(HAVE_PCRE)
-               XLAT_REGISTER(regex);
-#endif
-
-               xlat_register(&xlat_foreach_inst[0], "debug", xlat_debug, NULL, NULL, 0, XLAT_DEFAULT_BUF_LEN, true);
-               c = xlat_find("debug");
-               rad_assert(c != NULL);
-               c->internal = true;
-       }
-
        /*
         *      If it already exists, replace the instance.
         */
@@ -1023,6 +974,65 @@ int xlat_register_redundant(CONF_SECTION *cs)
        return 0;
 }
 
+/** Global initialisation for xlat
+ *
+ * @note Free memory with #xlat_free
+ *
+ * @return
+ *     - 0 on success.
+ *     - -1 on failure.
+ */
+int xlat_init(void)
+{
+       if (xlat_root) return 0;
+
+       xlat_t  *c;
+
+#ifdef WITH_UNLANG
+       int i;
+#endif
+
+       xlat_root = rbtree_create(NULL, xlat_cmp, NULL, RBTREE_FLAG_REPLACE);
+       if (!xlat_root) {
+               ERROR("%s: Failed to create tree", __FUNCTION__);
+               return -1;
+       }
+
+#ifdef WITH_UNLANG
+       for (i = 0; xlat_foreach_names[i] != NULL; i++) {
+               xlat_register(&xlat_foreach_inst[i], xlat_foreach_names[i], xlat_foreach, NULL, NULL, 0, XLAT_DEFAULT_BUF_LEN, true);
+               c = xlat_find(xlat_foreach_names[i]);
+               rad_assert(c != NULL);
+               c->internal = true;
+       }
+#endif
+
+#define XLAT_REGISTER(_x) xlat_register(NULL, STRINGIFY(_x), xlat_ ## _x, NULL, NULL, 0, XLAT_DEFAULT_BUF_LEN, true); \
+       c = xlat_find(STRINGIFY(_x)); \
+       rad_assert(c != NULL); \
+       c->internal = true
+
+       XLAT_REGISTER(integer);
+       XLAT_REGISTER(strlen);
+       XLAT_REGISTER(length);
+       XLAT_REGISTER(hex);
+       XLAT_REGISTER(tag);
+       XLAT_REGISTER(string);
+       XLAT_REGISTER(xlat);
+       XLAT_REGISTER(map);
+       XLAT_REGISTER(module);
+       XLAT_REGISTER(debug_attr);
+#if defined(HAVE_REGEX) && defined(HAVE_PCRE)
+       XLAT_REGISTER(regex);
+#endif
+
+       xlat_register(&xlat_foreach_inst[0], "debug", xlat_debug, NULL, NULL, 0, XLAT_DEFAULT_BUF_LEN, true);
+       c = xlat_find("debug");
+       rad_assert(c != NULL);
+       c->internal = true;
+
+       return 0;
+}
 
 /** De-register all xlat functions, used mainly for debugging.
  *