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

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 $
1.1.1.1.2.4! misho       6: * $Id: aitcli.h,v 1.1.1.1.2.3 2010/04/19 23:02:47 misho Exp $
1.1       misho       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
1.1.1.1.2.4! misho      35:  * return: -1 error, != -1 printed chars
1.1       misho      36: */
                     37: inline int cli_Printf(FILE *out, const char *csFormat, ...);
                     38: 
                     39: /*
1.1.1.1.2.4! misho      40:  * cliInit() Initialize Readline
        !            41:  * @csProg = program name
        !            42:  * return: none
        !            43: */
        !            44: inline void cliInit(const char *csProg);
        !            45: /*
        !            46:  * cliNetInit() Initialize Readline if CLI bind to socket
        !            47:  * @csProg = program name
        !            48:  * @pty = Master pty
        !            49:  * @term = stdin termios
        !            50:  * return: none
        !            51: */
        !            52: void cliNetInit(const char *csProg, int pty, struct termios *term);
        !            53: /*
1.1.1.1.2.1  misho      54:  * cliTTY() Initialize I/O TTY CLI features
1.1.1.1.2.3  misho      55:  * @term = terminal name
1.1.1.1.2.1  misho      56:  * @inp = input handle
                     57:  * @out = output handle
1.1.1.1.2.4! misho      58:  * @win = window size
        !            59:  * return: -1 error, != -1 ok
1.1.1.1.2.1  misho      60: */
1.1.1.1.2.4! misho      61: inline int cliTTY(const char *term, FILE *inp, FILE *out, struct winsize *win);
1.1.1.1.2.1  misho      62: /*
1.1       misho      63:  * cliComp() Initialize completion CLI features
                     64:  * @cmdComplete = Completion function
                     65:  * @cmdEntry = Compentry function
                     66:  * return: none
                     67: */
                     68: inline void cliComp(cli_Completion_t *cmdComplete, cli_CompEntry_t *cmdEntry);
                     69: /*
                     70:  * cliExec() Execute CLI main loop
                     71:  * @cmdList = Commands list
                     72:  * @csPrompt = Prompt text
                     73:  * return: -1 error, 0 = exit w/^+D, 1 done.
                     74: */
1.1.1.1.2.2  misho      75: int cliExec(cliCommands_t *cmdList, const char *csPrompt);
1.1       misho      76: 
                     77: 
                     78: /*
                     79:  * cli_PrintHelp() Helper print for missing command arguments
                     80:  * @out = Output stream
                     81:  * @cmds = Commands list
                     82:  * @idx = Selected command ID
                     83:  * return: -1 error, !=-1 ok
                     84:  * return: none
                     85: */
                     86: inline int cli_PrintHelp(FILE *out, void *cmds, int idx);
                     87: 
                     88: /*
                     89:  * cli_Cmd_Unsupported() Builtin helper function for unsupported commands
                     90:  * @cmds = Commands list
                     91:  * @idx = Selected command ID
                     92:  * @out = Output handle
                     93:  * @args = Parsed arguments array
                     94:  * return: -1 error, 0 = ok, 1 exit from Cli!
                     95: */
                     96: int cli_Cmd_Unsupported(void *cmds, int idx, FILE *out, char ** __restrict args);
                     97: /*
                     98:  * cli_Cmd_Help() Builtin helper function for Help screen
                     99:  * @cmds = Commands list
                    100:  * @idx = Selected command ID
                    101:  * @out = Output handle
                    102:  * @args = Parsed arguments array
                    103:  * return: -1 error, 0 = ok
                    104: */
                    105: int cli_Cmd_Help(void *cmds, int idx, FILE *out, char ** __restrict args);
                    106: /*
                    107:  * cli_Cmd_Exit() Builtin helper function for Exit from Cli
                    108:  * @cmds = Commands list
                    109:  * @idx = Selected command ID
                    110:  * @out = Output handle
                    111:  * @args = Parsed arguments array
                    112:  * return: 1 exit from Cli!
                    113: */
                    114: int cli_Cmd_Exit(void *cmds, int idx, FILE *out, char ** __restrict args);
                    115: 
                    116: 
                    117: /*
                    118:  * cli_Register_Commands - Declare helper function for register and export Commands variable
                    119: */
                    120: #define CLI_REGISTER_COMMANDS(CMDS)    \
                    121:        extern cliCommands_t CMDS[];
                    122: /*
                    123:  * cli_Make_Comp_Commands - Declare helper function for Commands completion arguments
                    124: */
                    125: #define CLI_MAKE_COMP_COMMANDS(FUNC, CMDS)     \
                    126:        char *FUNC(const char *text, int state) \
                    127:        { \
                    128:                register int i; \
                    129:                int len = strlen(text); \
                    130:                for (i = state; CMDS[i].cmd_name; i++) { \
                    131:                        if (strncmp(CMDS[i].cmd_name, "---", 3) && \
                    132:                                        !strncmp(CMDS[i].cmd_name, text, len)) \
                    133:                                return strdup(CMDS[i].cmd_name); \
                    134:                } \
                    135:                return NULL; \
                    136:        }
                    137: 
                    138: /*
                    139:  * cli_Make_Comp_Args - Declare helper function for Arguments completion
                    140: */
                    141: #define CLI_MAKE_COMP_ARGS(FUNC, ARGS) \
                    142:        char *FUNC(const char *text __attribute__((unused)), int state) \
                    143:        { \
                    144:                while (ARGS[state]) \
                    145:                        return strdup(ARGS[state]); \
                    146:                return NULL; \
                    147:        }
                    148: 
                    149: /*
                    150:  * cli_Comp_Filename() Builtin helper function for filename completion arguments
                    151:  * @text = Text line
                    152:  * @state = Position state
                    153:  * return: NULL not found filename, != NULL filename
                    154: */
                    155: char *cli_Comp_Filename(const char *text, int state);
                    156: 
                    157: 
                    158: #endif

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