]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
search for the command in info->cmd
authorAlan T. DeKok <aland@freeradius.org>
Mon, 9 Jul 2018 19:27:51 +0000 (15:27 -0400)
committerAlan T. DeKok <aland@freeradius.org>
Mon, 9 Jul 2018 19:32:45 +0000 (15:32 -0400)
and assume that we don't need to call fr_command_find() again

src/include/command.h
src/main/command.c
src/main/radmin.c

index 6ffcd09c976eac36bbff139f4232a6114a4521e6..2f2a517ed1b5219b8a3f18a2b38b0d7f2eac4d44 100644 (file)
@@ -72,7 +72,7 @@ int fr_command_add_multi(TALLOC_CTX *talloc_ctx, fr_cmd_t **heap_p, char const *
 int fr_command_walk(fr_cmd_t *head, void **walk_ctx, void *ctx, fr_cmd_walk_t callback);
 int fr_command_tab_expand(TALLOC_CTX *ctx, fr_cmd_t *head, fr_cmd_info_t *info, int max_expansions, char const **expansions);
 char const *fr_command_help(fr_cmd_t *head, int argc, char *argv[]);
-int fr_command_run(FILE *fp, FILE *fp_err, fr_cmd_t *head, fr_cmd_info_t *info);
+int fr_command_run(FILE *fp, FILE *fp_err, fr_cmd_info_t *info);
 void fr_command_debug(FILE *fp, fr_cmd_t *head);
 void fr_command_list(FILE *fp, int max_depth, fr_cmd_t *head, bool is_head);
 int fr_command_str_to_argv(fr_cmd_t *head, fr_cmd_info_t *info, char *str);
index 84853b3fc50d7f8208eb4ac840470c0fbffeb836..3178a3313ee5b9500f6365a5d28d7c32dfe4d4a3 100644 (file)
@@ -1353,18 +1353,18 @@ int fr_command_tab_expand(TALLOC_CTX *ctx, fr_cmd_t *head, fr_cmd_info_t *info,
  *
  * @param fp   where the output is sent
  * @param fp_err  where the error output is sent
- * @param head the head of the command hierarchy.
  * @param info the structure describing the command to expand
  * @return
  *     - <0 on error
  *     - 0 the command was run successfully
  */
-int fr_command_run(FILE *fp, FILE *fp_err, fr_cmd_t *head, fr_cmd_info_t *info)
+int fr_command_run(FILE *fp, FILE *fp_err, fr_cmd_info_t *info)
 {
-       int i;
-       fr_cmd_t *cmd, *start;
+       int i, rcode;
+       fr_cmd_t *cmd;
+       fr_cmd_info_t my_info;
 
-       start = head;
+       cmd = NULL;
 
        /*
         *      Asked to do nothing, do nothing.
@@ -1372,54 +1372,35 @@ int fr_command_run(FILE *fp, FILE *fp_err, fr_cmd_t *head, fr_cmd_info_t *info)
        if (info->argc == 0) return 0;
 
        for (i = 0; i < info->argc; i++) {
-               int rcode;
-               fr_cmd_info_t my_info;
-
-               cmd = fr_command_find(&start, info->argv[i], NULL);
-               if (!cmd) {
-               no_such_command:
-                       if (info->argc == 1) {
-                               fr_strerror_printf("No such command '%s'", info->argv[i]);
-                       } else {
-                               fr_strerror_printf("No such command '... %s'", info->argv[i]);
-                       }
-                       return -1;
-               }
+               cmd = info->cmd[i];
+               rad_assert(cmd != NULL);
 
-               /*
-                *      Intermediate nodes must have children, and
-                *      must not have callbacks.
-                */
-               if (cmd->intermediate) {
-                       rad_assert(cmd->child != NULL);
-                       rad_assert(cmd->func == NULL);
-                       start = cmd->child;
-                       continue;
-               }
+               if (!cmd->live) return 0;
 
-               if (!cmd->live) goto no_such_command;
+               if (cmd->intermediate) continue;
+               break;
+       }
 
-               /*
-                *      Leaf nodes must have a callback.
-                */
-               rad_assert(cmd->func != NULL);
+       if (!cmd) return 0;
 
-               // @todo - add cmd->min_argc && cmd->max_argc, to track optional things, varargs, etc.
+       /*
+        *      Leaf nodes must have a callback.
+        */
+       rad_assert(cmd->func != NULL);
 
-               /*
-                *      The arguments have already been verified by
-                *      fr_command_str_to_argv().
-                */
-               my_info.argc = info->argc - i - 1;
-               my_info.max_argc = info->max_argc - info->argc;
-               my_info.runnable = true;
-               my_info.argv = &info->argv[i + 1];
-               my_info.box = &info->box[i + 1];
-               rcode = cmd->func(fp, fp_err, cmd->ctx, &my_info);
-               return rcode;
-       }
+       // @todo - add cmd->min_argc && cmd->max_argc, to track optional things, varargs, etc.
 
-       return 0;
+       /*
+        *      The arguments have already been verified by
+        *      fr_command_str_to_argv().
+        */
+       my_info.argc = info->argc - i - 1;
+       my_info.max_argc = info->max_argc - info->argc;
+       my_info.runnable = true;
+       my_info.argv = &info->argv[i + 1];
+       my_info.box = &info->box[i + 1];
+       rcode = cmd->func(fp, fp_err, cmd->ctx, &my_info);
+       return rcode;
 }
 
 
@@ -1819,7 +1800,7 @@ int fr_command_str_to_argv(fr_cmd_t *head, fr_cmd_info_t *info, char *str)
                /*
                 *      Cache the command for later consumption.
                 */
-               if (info->cmd) info->cmd[i] = cmd;
+               info->cmd[i] = cmd;
 
                /*
                 *      There's a child.  Go match it.
index 882887463b852149314ba903d5e2b8ff7cba3c2b..96a79a72c8e22471bc67fd0e8d03658beccfb5f3 100644 (file)
@@ -306,7 +306,7 @@ static void *fr_radmin(UNUSED void *input_ctx)
                 */
                add_history(line);
 
-               if (fr_command_run(stdout, stderr, radmin_cmd, info) < 0) {
+               if (fr_command_run(stdout, stderr, info) < 0) {
                        /*
                         *      @todo - send return code to radmin The
                         *      command MUST have already printed the