--- libaitio/src/Attic/cli.c 2010/03/09 12:44:40 1.1.2.1 +++ libaitio/src/Attic/cli.c 2010/03/11 13:29:56 1.1.2.4 @@ -3,18 +3,32 @@ * by Michael Pounov * * $Author: misho $ -* $Id: cli.c,v 1.1.2.1 2010/03/09 12:44:40 misho Exp $ +* $Id: cli.c,v 1.1.2.4 2010/03/11 13:29:56 misho Exp $ * *************************************************************************/ #include "global.h" -static int cmd_exit(void *cmds, FILE *out, char ** __restrict args) +/* + * io_Cmd_Exit() Builtin helper function for Exit from Cli + * @cmds = Commands list + * @out = Output handle + * @args = Parsed arguments array + * return: 1 exit from Cli! +*/ +int io_Cmd_Exit(void *cmds, FILE *out, char ** __restrict args) { return 1; } -static int cmd_help(void *cmds, FILE *out, char ** __restrict args) +/* + * io_Cmd_Help() Builtin helper function for Help screen + * @cmds = Commands list + * @out = Output handle + * @args = Parsed arguments array + * return: -1 error, 0 = ok +*/ +int io_Cmd_Help(void *cmds, FILE *out, char ** __restrict args) { register int i; ioCommands_t *commands = cmds; @@ -45,7 +59,14 @@ static int cmd_help(void *cmds, FILE *out, char ** __r return 0; } -static int cmd_unsupported(void *cmds, FILE *out, char ** __restrict args) +/* + * io_Cmd_Unsupported() Builtin helper function for unsupported commands + * @cmds = Commands list + * @out = Output handle + * @args = Parsed arguments array + * return: -1 error, 0 = ok, 1 exit from Cli! +*/ +int io_Cmd_Unsupported(void *cmds, FILE *out, char ** __restrict args) { fprintf(out, "Command %s not supported in this version ...\n", args[0]); return 0; @@ -53,13 +74,26 @@ static int cmd_unsupported(void *cmds, FILE *out, char // ------------------------------------------------------------ +/* + * io_Comp_Filename() Builtin helper function for filename completion arguments + * @text = Text line + * @state = Position state + * return: NULL not found filename, != NULL filename +*/ +char *io_Comp_Filename(const char *text, int state) +{ + return rl_filename_completion_function(text, state); +} + +// ------------------------------------------------------------ + #pragma GCC visibility push(hidden) ioCommands_t io_stdCmds[] = { - { "test", cmd_unsupported, "Test - Don`t use default command structure!", "test ", NULL }, + { "test", io_Cmd_Unsupported, "Test - Don`t use default command structure!", "test ", io_Comp_Filename }, { "-------", NULL, "---------------------", NULL, NULL }, - { "help", cmd_help, "Help screen", "help [command] ", NULL }, - { "exit", cmd_exit, "Exit from console", "exit ", NULL }, + { "help", io_Cmd_Help, "Help screen", "help [command] ", NULL }, + { "exit", io_Cmd_Exit, "Exit from console", "exit ", NULL }, { NULL, NULL, NULL, NULL } }; @@ -68,25 +102,13 @@ ioCommands_t io_stdCmds[] = { // ------------------------------------------------------------ /* - * ioCLIInit() Initialize CLI features - * @cmdList = Commands list - * @out = Output handle + * ioCLIComp() Initialize completion CLI features * @cmdComplete = Completion function * @cmdEntry = Compentry function * return: none */ -void ioCLIInit(ioCommands_t *cmdList, FILE *out, io_Completion_t *cmdComplete, io_CompEntry_t *cmdEntry) +inline void ioCLIComp(io_Completion_t *cmdComplete, io_CompEntry_t *cmdEntry) { - ioCommands_t *cmds = cmdList ? cmdList : io_stdCmds; - - inline int inline_help() - { - cmd_help(cmds, out, NULL); - rl_on_new_line(); - return 0; - } - - rl_bind_key('?', inline_help); // command completon rl_attempted_completion_function = cmdComplete; rl_completion_entry_function = cmdEntry; @@ -106,8 +128,51 @@ int ioCLIExec(ioCommands_t *cmdList, FILE *out, const register int i; ioCommands_t *cmd = NULL; - if (!cmdList) - return -1; + inline int inline_help() + { + io_Cmd_Help(cmdList ? cmdList : io_stdCmds, out, NULL); + rl_on_new_line(); + return 0; + } + + char **io_stdCompletion(const char *text, int start, int end) + { + register int i; + char **matches = NULL; + + char *cmdCompGet(const char *text, int state) + { + int len = strlen(text); + + for (i = state; cmdList[i].cmd_name; i++) { + if (strncmp(cmdList[i].cmd_name, "---", 3) && + !strncmp(cmdList[i].cmd_name, text, len)) + return strdup(cmdList[i].cmd_name); + } + + return NULL; + } + + if (!start) + matches = rl_completion_matches(text, cmdCompGet); + else + for (i = 0; cmdList[i].cmd_name; i++) { + if (!cmdList[i].cmd_comp) + continue; + if (!strncmp(rl_line_buffer, cmdList[i].cmd_name, strlen(cmdList[i].cmd_name))) + matches = rl_completion_matches(text, cmdList[i].cmd_comp); + } + + return matches; + } + char *io_stdCompEntry(const char *ignore, int invoking_key) + { + return NULL; + } + + rl_bind_key('?', inline_help); + if (!rl_attempted_completion_function) + ioCLIComp(io_stdCompletion, io_stdCompEntry); do { line = readline(csPrompt);