]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
move process $INCLUDE to its own function
authorAlan T. DeKok <aland@freeradius.org>
Wed, 27 Sep 2023 14:51:11 +0000 (10:51 -0400)
committerAlan T. DeKok <aland@freeradius.org>
Wed, 27 Sep 2023 14:59:57 +0000 (10:59 -0400)
src/lib/util/dict_tokenize.c

index e1b15d9898182ede8934947ad34977e0eefb6882..619692218088e3d0acf28d16a5dee3224724e141 100644 (file)
@@ -1264,6 +1264,78 @@ static int dict_read_process_enum(dict_tokenize_ctx_t *ctx, char **argv, int arg
        return 0;
 }
 
+static int _dict_from_file(dict_tokenize_ctx_t *ctx,
+                          char  const *dir_name, char const *filename,
+                          char const *src_file, int src_line);
+
+/*
+ *     Process the $INCLUDE command
+ */
+static int dict_read_process_include(dict_tokenize_ctx_t *ctx, char **argv, int argc, char const *dir, char *fn, int line)
+{
+       int rcode;
+       int stack_depth = ctx->stack_depth;
+
+       /*
+        *      Allow "$INCLUDE" or "$INCLUDE-", but
+        *      not anything else.
+        */
+       if ((argv[0][8] != '\0') && ((argv[0][8] != '-') || (argv[0][9] != '\0'))) {
+               fr_strerror_printf("Invalid keyword '%s'", argv[0]);
+               return -1;
+       }
+
+       if (argc != 2) {
+               fr_strerror_printf("Unexpected text after $INCLUDE at %s[%d]", fr_cwd_strip(fn), line);
+               return -1;
+       }
+
+       /*
+        *      Allow limited macro capability, so
+        *      people don't have to remember where
+        *      the root dictionaries are located.
+        */
+       if (strncmp(argv[1], "${dictdir}/", 11) != 0) {
+               rcode = _dict_from_file(ctx, dir, argv[1], fn, line);
+       } else {
+               rcode = _dict_from_file(ctx, fr_dict_global_ctx_dir(), argv[1] + 11, fn, line);
+       }
+
+       if ((rcode == -2) && (argv[0][8] == '-')) {
+               fr_strerror_clear(); /* delete all errors */
+               return 0;
+       }
+
+       if (rcode < 0) {
+               fr_strerror_printf_push("from $INCLUDE at %s[%d]", fr_cwd_strip(fn), line);
+               return -1;
+       }
+
+       if (ctx->stack_depth < stack_depth) {
+               fr_strerror_printf_push("unexpected END-??? in $INCLUDE at %s[%d]",
+                                       fr_cwd_strip(fn), line);
+               return -1;
+       }
+
+       while (ctx->stack_depth > stack_depth) {
+               if (ctx->stack[ctx->stack_depth].nest == NEST_NONE) {
+                       ctx->stack_depth--;
+                       continue;
+               }
+
+               fr_strerror_printf_push("BEGIN-??? without END-... in file $INCLUDEd from %s[%d]",
+                                       fr_cwd_strip(fn), line);
+               return -1;
+       }
+
+       /*
+        *      Reset the filename.
+        */
+       ctx->stack[ctx->stack_depth].filename = fn;
+
+       return 0;
+}
+
 /*
  *     Process the MEMBER command
  */
@@ -1994,7 +2066,7 @@ static int dict_finalise(dict_tokenize_ctx_t *ctx)
  * @param[in] dir_name Directory containing the dictionary we're loading.
  * @param[in] filename we're parsing.
  * @param[in] src_file The including file.
- * @param[in] src_line Line on which the $INCLUDE or $INCLUDE- statement was found.
+ * @param[in] src_line Line on which the $INCLUDE or $NCLUDE- statement was found.
  * @return
  *     - 0 on success.
  *     - -1 on failure.
@@ -2270,15 +2342,6 @@ static int _dict_from_file(dict_tokenize_ctx_t *ctx,
                 *      See if we need to import another dictionary.
                 */
                if (strncasecmp(argv[0], "$INCLUDE", 8) == 0) {
-                       int ret;
-                       int stack_depth = ctx->stack_depth;
-
-                       /*
-                        *      Allow "$INCLUDE" or "$INCLUDE-", but
-                        *      not anything else.
-                        */
-                       if ((argv[0][8] != '\0') && ((argv[0][8] != '-') || (argv[0][9] != '\0'))) goto invalid_keyword;
-
                        /*
                         *      Included files operate on a copy of the context.
                         *
@@ -2289,49 +2352,7 @@ static int _dict_from_file(dict_tokenize_ctx_t *ctx,
                         *      attribute", then it won't affect the
                         *      parent.
                         */
-
-                       /*
-                        *      Allow limited macro capability, so
-                        *      people don't have to remember where
-                        *      the root dictionaries are located.
-                        */
-                       if (strncmp(argv[1], "${dictdir}/", 11) != 0) {
-                               ret = _dict_from_file(ctx, dir, argv[1], fn, line);
-                       } else {
-                               ret = _dict_from_file(ctx, fr_dict_global_ctx_dir(), argv[1] + 11, fn, line);
-                       }
-
-                       if ((ret == -2) && (argv[0][8] == '-')) {
-                               fr_strerror_clear(); /* delete all errors */
-                               ret = 0;
-                       }
-
-                       if (ret < 0) {
-                               fr_strerror_printf_push("from $INCLUDE at %s[%d]", fr_cwd_strip(fn), line);
-                               goto error;
-                       }
-
-                       if (ctx->stack_depth < stack_depth) {
-                               fr_strerror_printf_push("unexpected END-??? in $INCLUDE at %s[%d]",
-                                                       fr_cwd_strip(fn), line);
-                               goto error;
-                       }
-
-                       while (ctx->stack_depth > stack_depth) {
-                               if (ctx->stack[ctx->stack_depth].nest == NEST_NONE) {
-                                       ctx->stack_depth--;
-                                       continue;
-                               }
-
-                               fr_strerror_printf_push("BEGIN-??? without END-... in file $INCLUDEd from %s[%d]",
-                                                       fr_cwd_strip(fn), line);
-                               goto error;
-                       }
-
-                       /*
-                        *      Reset the filename.
-                        */
-                       ctx->stack[ctx->stack_depth].filename = fn;
+                       if (dict_read_process_include(ctx, argv, argc, dir, fn, line) < 0) goto error;
                        continue;
                } /* $INCLUDE */
 
@@ -2730,7 +2751,6 @@ static int _dict_from_file(dict_tokenize_ctx_t *ctx,
                /*
                 *      Any other string: We don't recognize it.
                 */
-       invalid_keyword:
                fr_strerror_printf_push("Invalid keyword '%s'", argv[0]);
                goto error;
        }