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

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.6! misho       6: * $Id: aitcli.h,v 1.1.1.1.2.5 2010/04/20 12:09:48 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: 
1.1.1.1.2.5  misho      77: /*
                     78:  * cli_ReadHistory() Read CLI History from file
                     79:  * @csFile = history file name, if NULL default history name is ".aitcli.history"
                     80:  * return: -1 error; != -1 readed ok
                     81: */
                     82: inline int cli_ReadHistory(const char *csFile);
                     83: /*
                     84:  * cli_WriteHistory() Write CLI History to file
                     85:  * @csFile = history file name, if NULL default history name is ".aitcli.history"
1.1.1.1.2.6! misho      86:  * @lineNum = save number of history entry lines, if -1 all lines saved without limit
1.1.1.1.2.5  misho      87:  * return: -1 error; != -1 readed ok
                     88: */
1.1.1.1.2.6! misho      89: inline int cli_WriteHistory(const char *csFile, int lineNum);
1.1.1.1.2.5  misho      90: 
1.1       misho      91: 
                     92: /*
                     93:  * cli_PrintHelp() Helper print for missing command arguments
                     94:  * @out = Output stream
                     95:  * @cmds = Commands list
                     96:  * @idx = Selected command ID
                     97:  * return: -1 error, !=-1 ok
                     98:  * return: none
                     99: */
                    100: inline int cli_PrintHelp(FILE *out, void *cmds, int idx);
                    101: 
                    102: /*
                    103:  * cli_Cmd_Unsupported() Builtin helper function for unsupported commands
                    104:  * @cmds = Commands list
                    105:  * @idx = Selected command ID
                    106:  * @out = Output handle
                    107:  * @args = Parsed arguments array
                    108:  * return: -1 error, 0 = ok, 1 exit from Cli!
                    109: */
                    110: int cli_Cmd_Unsupported(void *cmds, int idx, FILE *out, char ** __restrict args);
                    111: /*
                    112:  * cli_Cmd_Help() Builtin helper function for Help screen
                    113:  * @cmds = Commands list
                    114:  * @idx = Selected command ID
                    115:  * @out = Output handle
                    116:  * @args = Parsed arguments array
                    117:  * return: -1 error, 0 = ok
                    118: */
                    119: int cli_Cmd_Help(void *cmds, int idx, FILE *out, char ** __restrict args);
                    120: /*
                    121:  * cli_Cmd_Exit() Builtin helper function for Exit from Cli
                    122:  * @cmds = Commands list
                    123:  * @idx = Selected command ID
                    124:  * @out = Output handle
                    125:  * @args = Parsed arguments array
                    126:  * return: 1 exit from Cli!
                    127: */
                    128: int cli_Cmd_Exit(void *cmds, int idx, FILE *out, char ** __restrict args);
                    129: 
                    130: 
                    131: /*
                    132:  * cli_Register_Commands - Declare helper function for register and export Commands variable
                    133: */
                    134: #define CLI_REGISTER_COMMANDS(CMDS)    \
                    135:        extern cliCommands_t CMDS[];
                    136: /*
                    137:  * cli_Make_Comp_Commands - Declare helper function for Commands completion arguments
                    138: */
                    139: #define CLI_MAKE_COMP_COMMANDS(FUNC, CMDS)     \
                    140:        char *FUNC(const char *text, int state) \
                    141:        { \
                    142:                register int i; \
                    143:                int len = strlen(text); \
                    144:                for (i = state; CMDS[i].cmd_name; i++) { \
                    145:                        if (strncmp(CMDS[i].cmd_name, "---", 3) && \
                    146:                                        !strncmp(CMDS[i].cmd_name, text, len)) \
                    147:                                return strdup(CMDS[i].cmd_name); \
                    148:                } \
                    149:                return NULL; \
                    150:        }
                    151: 
                    152: /*
                    153:  * cli_Make_Comp_Args - Declare helper function for Arguments completion
                    154: */
                    155: #define CLI_MAKE_COMP_ARGS(FUNC, ARGS) \
                    156:        char *FUNC(const char *text __attribute__((unused)), int state) \
                    157:        { \
                    158:                while (ARGS[state]) \
                    159:                        return strdup(ARGS[state]); \
                    160:                return NULL; \
                    161:        }
                    162: 
                    163: /*
                    164:  * cli_Comp_Filename() Builtin helper function for filename completion arguments
                    165:  * @text = Text line
                    166:  * @state = Position state
                    167:  * return: NULL not found filename, != NULL filename
                    168: */
                    169: char *cli_Comp_Filename(const char *text, int state);
                    170: 
                    171: 
                    172: #endif

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