list_commands.c (1439B)
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 #include "cli/command.h" 6 7 static int cmd_list_commands(int argc, const char **argv) { 8 (void)argc; 9 (void)argv; 10 11 // Find the longest command name for aligned output 12 size_t width = 0; 13 struct cmd_struct *cmd = commands; 14 while (cmd) { 15 size_t len = strlen(cmd->display ? cmd->display : cmd->name[0]); 16 if (len > width) width = len; 17 cmd = cmd->next; 18 } 19 20 cmd = commands; 21 while (cmd) { 22 printf(" %-*s %s\n", (int)width, 23 cmd->display ? cmd->display : cmd->name[0], 24 cmd->description ? cmd->description : ""); 25 cmd = cmd->next; 26 } 27 28 return 0; 29 } 30 31 void __attribute__((constructor)) cmd_list_commands_setup(void) { 32 struct cmd_struct *cmd = calloc(1, sizeof(struct cmd_struct)); 33 if (!cmd) { 34 fprintf(stderr, "Failed to allocate memory for list-commands command\n"); 35 return; 36 } 37 cmd->next = commands; 38 cmd->fn = cmd_list_commands; 39 static const char *list_commands_names[] = {"list-commands", NULL}; 40 cmd->name = list_commands_names; 41 cmd->display = "list-commands"; 42 cmd->description = "List available commands"; 43 cmd->help_text = 44 "supercop list-commands - List available commands\n" 45 "\n" 46 "Usage:\n" 47 " supercop list-commands\n"; 48 commands = cmd; 49 }