Annotation of libaitcli/inc/aitcli.h, revision 1.1

1.1     ! misho       1: /*************************************************************************
        !             2: * (C) 2010 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
        !             3: *  by Michael Pounov <misho@openbsd-bg.org>
        !             4: *
        !             5: * $Author: misho $
        !             6: * $Id: aitio.h,v 1.3.2.6 2010/04/06 14:26:55 misho Exp $
        !             7: *
        !             8: *************************************************************************/
        !             9: #ifndef __AITCLI_H
        !            10: #define __AITCLI_H
        !            11: 
        !            12: 
        !            13: struct tagCLICmd {
        !            14:        const char *cmd_name;
        !            15:        int (*cmd_func)(void *, int, FILE *, char **);
        !            16:        const char *cmd_doc;
        !            17:        const char *cmd_help;
        !            18:        char *(*cmd_comp)(const char *, int);
        !            19: };
        !            20: 
        !            21: typedef struct tagCLICmd cliCommands_t;
        !            22: typedef char *cli_CompEntry_t(const char *, int);
        !            23: typedef char **cli_Completion_t(const char *, int, int);
        !            24: 
        !            25: 
        !            26: // cli_GetErrno() Get error code of last operation
        !            27: inline int cli_GetErrno();
        !            28: // cli_GetError() Get error text of last operation
        !            29: inline const char *cli_GetError();
        !            30: 
        !            31: /*
        !            32:  * cli_Printf() Printf CLI features
        !            33:  * @out = Output stream
        !            34:  * @csFormat = Printf format string
        !            35:  * return: none
        !            36: */
        !            37: inline int cli_Printf(FILE *out, const char *csFormat, ...);
        !            38: 
        !            39: /*
        !            40:  * cliComp() Initialize completion CLI features
        !            41:  * @cmdComplete = Completion function
        !            42:  * @cmdEntry = Compentry function
        !            43:  * return: none
        !            44: */
        !            45: inline void cliComp(cli_Completion_t *cmdComplete, cli_CompEntry_t *cmdEntry);
        !            46: /*
        !            47:  * cliExec() Execute CLI main loop
        !            48:  * @cmdList = Commands list
        !            49:  * @out = Output handle
        !            50:  * @csPrompt = Prompt text
        !            51:  * return: -1 error, 0 = exit w/^+D, 1 done.
        !            52: */
        !            53: int cliExec(cliCommands_t *cmdList, FILE *out, const char *csPrompt);
        !            54: 
        !            55: 
        !            56: /*
        !            57:  * cli_PrintHelp() Helper print for missing command arguments
        !            58:  * @out = Output stream
        !            59:  * @cmds = Commands list
        !            60:  * @idx = Selected command ID
        !            61:  * return: -1 error, !=-1 ok
        !            62:  * return: none
        !            63: */
        !            64: inline int cli_PrintHelp(FILE *out, void *cmds, int idx);
        !            65: 
        !            66: /*
        !            67:  * cli_Cmd_Unsupported() Builtin helper function for unsupported commands
        !            68:  * @cmds = Commands list
        !            69:  * @idx = Selected command ID
        !            70:  * @out = Output handle
        !            71:  * @args = Parsed arguments array
        !            72:  * return: -1 error, 0 = ok, 1 exit from Cli!
        !            73: */
        !            74: int cli_Cmd_Unsupported(void *cmds, int idx, FILE *out, char ** __restrict args);
        !            75: /*
        !            76:  * cli_Cmd_Help() Builtin helper function for Help screen
        !            77:  * @cmds = Commands list
        !            78:  * @idx = Selected command ID
        !            79:  * @out = Output handle
        !            80:  * @args = Parsed arguments array
        !            81:  * return: -1 error, 0 = ok
        !            82: */
        !            83: int cli_Cmd_Help(void *cmds, int idx, FILE *out, char ** __restrict args);
        !            84: /*
        !            85:  * cli_Cmd_Exit() Builtin helper function for Exit from Cli
        !            86:  * @cmds = Commands list
        !            87:  * @idx = Selected command ID
        !            88:  * @out = Output handle
        !            89:  * @args = Parsed arguments array
        !            90:  * return: 1 exit from Cli!
        !            91: */
        !            92: int cli_Cmd_Exit(void *cmds, int idx, FILE *out, char ** __restrict args);
        !            93: 
        !            94: 
        !            95: /*
        !            96:  * cli_Register_Commands - Declare helper function for register and export Commands variable
        !            97: */
        !            98: #define CLI_REGISTER_COMMANDS(CMDS)    \
        !            99:        extern cliCommands_t CMDS[];
        !           100: /*
        !           101:  * cli_Make_Comp_Commands - Declare helper function for Commands completion arguments
        !           102: */
        !           103: #define CLI_MAKE_COMP_COMMANDS(FUNC, CMDS)     \
        !           104:        char *FUNC(const char *text, int state) \
        !           105:        { \
        !           106:                register int i; \
        !           107:                int len = strlen(text); \
        !           108:                for (i = state; CMDS[i].cmd_name; i++) { \
        !           109:                        if (strncmp(CMDS[i].cmd_name, "---", 3) && \
        !           110:                                        !strncmp(CMDS[i].cmd_name, text, len)) \
        !           111:                                return strdup(CMDS[i].cmd_name); \
        !           112:                } \
        !           113:                return NULL; \
        !           114:        }
        !           115: 
        !           116: /*
        !           117:  * cli_Make_Comp_Args - Declare helper function for Arguments completion
        !           118: */
        !           119: #define CLI_MAKE_COMP_ARGS(FUNC, ARGS) \
        !           120:        char *FUNC(const char *text __attribute__((unused)), int state) \
        !           121:        { \
        !           122:                while (ARGS[state]) \
        !           123:                        return strdup(ARGS[state]); \
        !           124:                return NULL; \
        !           125:        }
        !           126: 
        !           127: /*
        !           128:  * cli_Comp_Filename() Builtin helper function for filename completion arguments
        !           129:  * @text = Text line
        !           130:  * @state = Position state
        !           131:  * return: NULL not found filename, != NULL filename
        !           132: */
        !           133: char *cli_Comp_Filename(const char *text, int state);
        !           134: 
        !           135: 
        !           136: #endif

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>