From: Alan T. DeKok Date: Fri, 6 Jul 2018 16:29:25 +0000 (-0400) Subject: add fr_command_list() X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=1e255b412bd205c96f67e877e5d84db04e5abc3b;p=thirdparty%2Ffreeradius-server.git add fr_command_list() which lists commands and their syntax --- diff --git a/src/include/command.h b/src/include/command.h index 4b2b52c44d6..bcdd3b32ec8 100644 --- a/src/include/command.h +++ b/src/include/command.h @@ -73,6 +73,7 @@ int fr_command_tab_expand(TALLOC_CTX *ctx, fr_cmd_t *head, fr_cmd_info_t *info, 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); void fr_command_debug(FILE *fp, fr_cmd_t *head); +void fr_command_list(FILE *fp, fr_cmd_t *head); int fr_command_str_to_argv(fr_cmd_t *head, fr_cmd_info_t *info, char *str); #ifdef __cplusplus diff --git a/src/main/command.c b/src/main/command.c index a771306011d..2ca880c6add 100644 --- a/src/main/command.c +++ b/src/main/command.c @@ -1464,6 +1464,53 @@ void fr_command_debug(FILE *fp, fr_cmd_t *head) } +static void fr_command_list_node(FILE *fp, fr_cmd_t *cmd, int depth, char const **argv) +{ + int i; + + if (depth == 0) { + if (!cmd->syntax) { + fprintf(fp, "%s\n", cmd->name); + } else { + fprintf(fp, "%s %s\n", cmd->name, cmd->syntax); + } + + return; + } + + for (i = 0; i < depth; i++) { + fprintf(fp, "%s ", argv[i]); + } + + if (!cmd->syntax) { + fprintf(fp, "\n"); + } else { + fprintf(fp, "%s\n", cmd->syntax); + } +} + +static void fr_command_list_internal(FILE *fp, fr_cmd_t *head, int depth, char const **argv) +{ + fr_cmd_t *cmd; + + for (cmd = head; cmd != NULL; cmd = cmd->next) { + if (cmd->child) { + argv[depth] = cmd->name; + fr_command_list_internal(fp, cmd->child, depth + 1, argv); + } else { + fr_command_list_node(fp, cmd, depth, argv); + } + } +} + +void fr_command_list(FILE *fp, fr_cmd_t *head) +{ + char const *argv[CMD_MAX_ARGV]; + + fr_command_list_internal(fp, head, 0, argv); +} + + static int fr_command_verify_argv(fr_cmd_info_t *info, int start, int verify, int argc, fr_cmd_argv_t **argv_p, bool optional) { char quote;