/************************************************************************* * (C) 2010 AITNET ltd - Sofia/Bulgaria - * by Michael Pounov * * $Author: misho $ * $Id: aitcli.h,v 1.1.1.1.2.1 2010/04/18 20:42:23 misho Exp $ * *************************************************************************/ #ifndef __AITCLI_H #define __AITCLI_H struct tagCLICmd { const char *cmd_name; int (*cmd_func)(void *, int, FILE *, char **); const char *cmd_doc; const char *cmd_help; char *(*cmd_comp)(const char *, int); }; typedef struct tagCLICmd cliCommands_t; typedef char *cli_CompEntry_t(const char *, int); typedef char **cli_Completion_t(const char *, int, int); // cli_GetErrno() Get error code of last operation inline int cli_GetErrno(); // cli_GetError() Get error text of last operation inline const char *cli_GetError(); /* * cli_Printf() Printf CLI features * @out = Output stream * @csFormat = Printf format string * return: none */ inline int cli_Printf(FILE *out, const char *csFormat, ...); /* * cliTTY() Initialize I/O TTY CLI features * @inp = input handle * @out = output handle * return: none */ inline void cliTTY(FILE *inp, FILE *out); /* * cliComp() Initialize completion CLI features * @cmdComplete = Completion function * @cmdEntry = Compentry function * return: none */ inline void cliComp(cli_Completion_t *cmdComplete, cli_CompEntry_t *cmdEntry); /* * cliExec() Execute CLI main loop * @cmdList = Commands list * @out = Output handle * @csPrompt = Prompt text * return: -1 error, 0 = exit w/^+D, 1 done. */ int cliExec(cliCommands_t *cmdList, FILE *out, const char *csPrompt); /* * cli_PrintHelp() Helper print for missing command arguments * @out = Output stream * @cmds = Commands list * @idx = Selected command ID * return: -1 error, !=-1 ok * return: none */ inline int cli_PrintHelp(FILE *out, void *cmds, int idx); /* * cli_Cmd_Unsupported() Builtin helper function for unsupported commands * @cmds = Commands list * @idx = Selected command ID * @out = Output handle * @args = Parsed arguments array * return: -1 error, 0 = ok, 1 exit from Cli! */ int cli_Cmd_Unsupported(void *cmds, int idx, FILE *out, char ** __restrict args); /* * cli_Cmd_Help() Builtin helper function for Help screen * @cmds = Commands list * @idx = Selected command ID * @out = Output handle * @args = Parsed arguments array * return: -1 error, 0 = ok */ int cli_Cmd_Help(void *cmds, int idx, FILE *out, char ** __restrict args); /* * cli_Cmd_Exit() Builtin helper function for Exit from Cli * @cmds = Commands list * @idx = Selected command ID * @out = Output handle * @args = Parsed arguments array * return: 1 exit from Cli! */ int cli_Cmd_Exit(void *cmds, int idx, FILE *out, char ** __restrict args); /* * cli_Register_Commands - Declare helper function for register and export Commands variable */ #define CLI_REGISTER_COMMANDS(CMDS) \ extern cliCommands_t CMDS[]; /* * cli_Make_Comp_Commands - Declare helper function for Commands completion arguments */ #define CLI_MAKE_COMP_COMMANDS(FUNC, CMDS) \ char *FUNC(const char *text, int state) \ { \ register int i; \ int len = strlen(text); \ for (i = state; CMDS[i].cmd_name; i++) { \ if (strncmp(CMDS[i].cmd_name, "---", 3) && \ !strncmp(CMDS[i].cmd_name, text, len)) \ return strdup(CMDS[i].cmd_name); \ } \ return NULL; \ } /* * cli_Make_Comp_Args - Declare helper function for Arguments completion */ #define CLI_MAKE_COMP_ARGS(FUNC, ARGS) \ char *FUNC(const char *text __attribute__((unused)), int state) \ { \ while (ARGS[state]) \ return strdup(ARGS[state]); \ return NULL; \ } /* * cli_Comp_Filename() Builtin helper function for filename completion arguments * @text = Text line * @state = Position state * return: NULL not found filename, != NULL filename */ char *cli_Comp_Filename(const char *text, int state); #endif