typedef struct fr_cmd_t fr_cmd_t;
-typedef int (*fr_cmd_func_t)(void *ctx, int argc, char const *argv[]);
+typedef int (*fr_cmd_func_t)(FILE *fp, void *ctx, int argc, char const *argv[]);
typedef struct fr_cmd_tab_info_t {
int argc;
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, int argc, char const *argv[], int max_expansions, char const **expansions);
char const *fr_command_help(fr_cmd_t *head, int argc, char const *argv[]);
-int fr_command_run(fr_cmd_t *head, int argc, char const *argv[]);
-void fr_command_debug(fr_cmd_t *head, FILE *fp);
+int fr_command_run(FILE *fp, fr_cmd_t *head, int argc, char const *argv[]);
+void fr_command_debug(FILE *fp, fr_cmd_t *head);
#ifdef __cplusplus
}
/** Run a particular command
*
- * @param head the head of the hierarchy.
+ * @param fp where the output is sent
+ * @param head the head of the command hierarchy.
* @param argc the number of arguments in the argv array
* @param argv the commands leading up to this one.
* @return
* - <0 on error
* - 0 the command was run successfully
*/
-int fr_command_run(fr_cmd_t *head, int argc, char const *argv[])
+int fr_command_run(FILE *fp, fr_cmd_t *head, int argc, char const *argv[])
{
int i;
fr_cmd_t *cmd, *start;
if (!cmd->syntax) {
if (cmd->func) {
rad_assert(cmd->func != NULL);
- return cmd->func(cmd->ctx, argc - i, &argv[i]);
+ return cmd->func(fp, cmd->ctx, argc - i, &argv[i]);
}
rad_assert(cmd->child != NULL);
fr_value_box_clear(&box);
}
- return cmd->func(cmd->ctx, argc - i, &argv[i]);
+ return cmd->func(fp, cmd->ctx, argc - i, &argv[i]);
}
return 0;
static const char *tabs = "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
-static void fr_command_debug_node(fr_cmd_t *cmd, FILE *fp, int depth)
+static void fr_command_debug_node(FILE *fp, fr_cmd_t *cmd, int depth)
{
fprintf(fp, "%.*s%s\n", depth, tabs, cmd->name);
if (cmd->syntax) fprintf(fp, "%.*s -> %s\n", depth, tabs, cmd->syntax);
if (cmd->help) fprintf(fp, "%.*s ? %s\n", depth, tabs, cmd->help);
}
-static void fr_command_debug_internal(fr_cmd_t *head, FILE *fp, int depth)
+static void fr_command_debug_internal(FILE *fp, fr_cmd_t *head, int depth)
{
fr_cmd_t *cmd;
for (cmd = head; cmd != NULL; cmd = cmd->next) {
- fr_command_debug_node(cmd, fp, depth);
+ fr_command_debug_node(fp, cmd, depth);
if (cmd->child) {
- fr_command_debug_internal(cmd->child, fp, depth + 1);
+ fr_command_debug_internal(fp, cmd->child, depth + 1);
}
}
}
-void fr_command_debug(fr_cmd_t *head, FILE *fp)
+void fr_command_debug(FILE *fp, fr_cmd_t *head)
{
- fr_command_debug_internal(head, fp, 0);
+ fr_command_debug_internal(fp, head, 0);
}
*/
static struct timeval start_time;
-static int fr_uptime(UNUSED void *ctx, UNUSED int argc, UNUSED char const *argv[])
+static int fr_uptime(FILE *fp, UNUSED void *ctx, UNUSED int argc, UNUSED char const *argv[])
{
struct timeval now;
gettimeofday(&now, NULL);
fr_timeval_subtract(&now, &now, &start_time);
-
- printf("Uptime: %u.%06u seconds\n",
- (int) now.tv_sec,
- (int) now.tv_usec);
+ fprintf(fp, "Uptime: %u.%06u seconds\n",
+ (int) now.tv_sec,
+ (int) now.tv_usec);
return 0;
}
}
if (strcmp(line, "help") == 0) {
- fr_command_debug(radmin_cmd, stdout);
+ fr_command_debug(stdout, radmin_cmd);
continue;
}
argc = fr_dict_str_to_argv(line, argv, MAX_ARGV);
- if (fr_command_run(radmin_cmd, argc, const_argv) < 0) {
+ if (fr_command_run(stdout, radmin_cmd, argc, const_argv) < 0) {
fprintf(stderr, "Failing running command: %s\n", fr_strerror());
}
static fr_cmd_t *command_head = NULL;
-static int command_func(UNUSED void *ctx, UNUSED int argc, UNUSED char const *argv[])
+static int command_func(UNUSED FILE *fp, UNUSED void *ctx, UNUSED int argc, UNUSED char const *argv[])
{
return 0;
}
void *walk_ctx = NULL;
printf("Command hierarchy --------\n");
- fr_command_debug(command_head, stdout);
+ fr_command_debug(stdout, command_head);
printf("Command list --------\n");
while (fr_command_walk(command_head, &walk_ctx, NULL, command_walk) == 1) {