/************************************************************************* * (C) 2010 AITNET ltd - Sofia/Bulgaria - * by Michael Pounov * * $Author: misho $ * $Id: cli.c,v 1.1.2.1 2010/03/09 12:44:40 misho Exp $ * *************************************************************************/ #include "global.h" static int cmd_exit(void *cmds, FILE *out, char ** __restrict args) { return 1; } static int cmd_help(void *cmds, FILE *out, char ** __restrict args) { register int i; ioCommands_t *commands = cmds; if (!cmds) return -1; if (!args) { fprintf(out, "\n"); for (i = 0; commands[i].cmd_name; i++) fprintf(out, "%s\t\t%s\n", commands[i].cmd_name, commands[i].cmd_doc); } else { if (!args[1]) fprintf(out, "Help screen::\n"); else if (!strncmp(args[1], "---", 3)) return 0; for (i = 0; commands[i].cmd_name; i++) { if (args[1] && strcmp(args[1], commands[i].cmd_name)) continue; fprintf(out, "%s%s\t\t%s\n", args[1] ? "Syntax::\n\t" : "", commands[i].cmd_name, args[1] ? commands[i].cmd_help : commands[i].cmd_doc); } } return 0; } static int cmd_unsupported(void *cmds, FILE *out, char ** __restrict args) { fprintf(out, "Command %s not supported in this version ...\n", args[0]); return 0; } // ------------------------------------------------------------ #pragma GCC visibility push(hidden) ioCommands_t io_stdCmds[] = { { "test", cmd_unsupported, "Test - Don`t use default command structure!", "test ", NULL }, { "-------", NULL, "---------------------", NULL, NULL }, { "help", cmd_help, "Help screen", "help [command] ", NULL }, { "exit", cmd_exit, "Exit from console", "exit ", NULL }, { NULL, NULL, NULL, NULL } }; #pragma GCC visibility pop // ------------------------------------------------------------ /* * ioCLIInit() Initialize CLI features * @cmdList = Commands list * @out = Output handle * @cmdComplete = Completion function * @cmdEntry = Compentry function * return: none */ void ioCLIInit(ioCommands_t *cmdList, FILE *out, 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; } /* * ioCLIExec() Execute CLI main loop * @cmdList = Commands list * @out = Output handle * @csPrompt = Prompt text * return: -1 error, 0 = exit w/^+D, 1 done. */ int ioCLIExec(ioCommands_t *cmdList, FILE *out, const char *csPrompt) { char *line, *s, *t, **app, *items[20]; int ret = 0; register int i; ioCommands_t *cmd = NULL; if (!cmdList) return -1; do { line = readline(csPrompt); if (!line) { // ^+d fprintf(out, "\n"); fflush(out); break; } // clear whitespaces for (s = line; isspace(*s); s++); if (*s) { for (t = s + strlen(s) - 1; t > s && isspace(*t); t--); *++t = 0; } if (*s) { add_history(s); memset(items, 0, sizeof(char*) * 20); for (app = items; app < items + 19 && (*app = strsep(&s, " \t")); *app ? app++ : app); /* for (i = 0; i < 20; i++) printf("i=%d %s\n", i, items[i]); */ // exec_cmd ... for (cmd = NULL, i = 0; cmdList[i].cmd_name; i++) if (*items[0] && !strncmp(cmdList[i].cmd_name, items[0], strlen(items[0]))) { cmd = &cmdList[i]; break; } if (!cmd) { fprintf(out, "Command '%s' not found!\n", items[0]); ret = -1; } else ret = cmd->cmd_func(cmdList, out, items); } free(line); } while (ret < 1); return ret; }