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

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.2.2.3 ! misho       6: * $Id: aitcli.h,v 1.2.2.2 2010/06/04 12:46:27 misho Exp $
1.1       misho       7: *
                      8: *************************************************************************/
                      9: #ifndef __AITCLI_H
                     10: #define __AITCLI_H
                     11: 
                     12: 
1.2.2.2   misho      13: #define STRSIZ         256
                     14: 
1.2.2.1   misho      15: /* Key definitions */
                     16: 
                     17: #define        K_F1            "\x1b\x4f\x50"
                     18: #define        K_F2            "\x1b\x4f\x51"
                     19: #define        K_F3            "\x1b\x4f\x52"
                     20: #define        K_F4            "\x1b\x4f\x53"
                     21: #define        K_F5            "\x1b\x5b\x31\x35\x7e"
                     22: #define        K_F6            "\x1b\x5b\x31\x37\x7e"
                     23: #define        K_F7            "\x1b\x5b\x31\x38\x7e"
                     24: #define        K_F8            "\x1b\x5b\x31\x39\x7e"
                     25: #define        K_F9            "\x1b\x5b\x32\x30\x7e"
                     26: #define        K_F10           "\x1b\x5b\x32\x31\x7e"
                     27: #define        K_F11           "\x1b\x5b\x32\x33\x7e"
                     28: #define        K_F12           "\x1b\x5b\x32\x34\x7e"
                     29: #define K_CTRL_F1      "\x1b\x5b\x6b"
                     30: #define K_CTRL_F2      "\x1b\x5b\x6c"
                     31: #define K_CTRL_F3      "\x1b\x5b\x6d"
                     32: #define K_CTRL_F4      "\x1b\x5b\x6e"
                     33: #define K_CTRL_F5      "\x1b\x5b\x6f"
                     34: #define K_CTRL_F6      "\x1b\x5b\x70"
                     35: #define K_CTRL_F7      "\x1b\x5b\x71"
                     36: #define K_CTRL_F8      "\x1b\x5b\x72"
                     37: #define K_CTRL_F9      "\x1b\x5b\x73"
                     38: #define K_CTRL_F10     "\x1b\x5b\x74"
                     39: #define K_CTRL_F11     "\x1b\x5b\x75"
                     40: #define K_CTRL_F12     "\x1b\x5b\x76"
                     41: #define K_CTRL_SH_F1   "\x1b\x5b\x77"
                     42: #define K_CTRL_SH_F2   "\x1b\x5b\x78"
                     43: #define K_CTRL_SH_F3   "\x1b\x5b\x79"
                     44: #define K_CTRL_SH_F4   "\x1b\x5b\x7a"
                     45: #define K_CTRL_SH_F5   "\x1b\x5b\x40"
                     46: #define K_CTRL_SH_F6   "\x1b\x5b\x5b"
                     47: #define K_CTRL_SH_F7   "\x1b\x5b\x5c"
                     48: #define K_CTRL_SH_F8   "\x1b\x5b\x5d"
                     49: #define K_CTRL_SH_F9   "\x1b\x5b\x5e"
                     50: #define K_CTRL_SH_F10  "\x1b\x5b\x5f"
                     51: #define K_CTRL_SH_F11  "\x1b\x5b\x60"
                     52: #define K_CTRL_SH_F12  "\x1b\x5b\x7b"
                     53: 
                     54: #define K_INS          "\x1b\x5b\x32\x7e"
                     55: #define K_DEL          "\x1b\x5b\x33\x7e"
                     56: #define K_PGUP         "\x1b\x5b\x35\x7e"
                     57: #define K_PGDN         "\x1b\x5b\x36\x7e"
                     58: #define K_HOME         "\x1b\x5b\x48"
                     59: #define K_END          "\x1b\x5b\x46"
                     60: #define K_UP           "\x1b\x5b\x41"
                     61: #define K_DOWN         "\x1b\x5b\x42"
                     62: #define K_RIGHT                "\x1b\x5b\x43"
                     63: #define K_LEFT         "\x1b\x5b\x44"
                     64: 
                     65: #define K_NULL         "\x0"
                     66: #define K_CR           "\xd"
                     67: #define K_BTAB         "\x1b\x5b\x5a"
                     68: #define K_TAB          "\x9"
                     69: #define K_ENTER                "\xa"
                     70: #define K_ESC          "\x1b"
                     71: #define K_BACKSPACE    "\x7f"
                     72: #define K_SPACE                "\x20"
                     73: 
                     74: #define K_CTRL_SPACE   K_NULL
                     75: #define K_CTRL_2       K_NULL
                     76: #define K_CTRL_A       "\x1"
                     77: #define K_CTRL_B       "\x2"
                     78: #define K_CTRL_C       "\x3"
                     79: #define K_CTRL_D       "\x4"
                     80: #define K_CTRL_E       "\x5"
                     81: #define K_CTRL_F       "\x6"
                     82: #define K_CTRL_G       "\x7"
                     83: #define K_CTRL_H       "\x8"
                     84: #define K_CTRL_I       K_TAB
                     85: #define K_CTRL_J       K_ENTER
                     86: #define K_CTRL_K       "\xb"
                     87: #define K_CTRL_L       "\xc"
                     88: #define K_CTRL_M       K_ENTER         // K_CR
                     89: #define K_CTRL_N       "\xe"
                     90: #define K_CTRL_O       "\xf"
                     91: #define K_CTRL_P       "\x10"
                     92: #define K_CTRL_Q       "\x11"
                     93: #define K_CTRL_R       "\x12"
                     94: #define K_CTRL_S       "\x13"
                     95: #define K_CTRL_T       "\x14"
                     96: #define K_CTRL_U       "\x15"
                     97: #define K_CTRL_V       "\x16"
                     98: #define K_CTRL_W       "\x17"
                     99: #define K_CTRL_X       "\x18"
                    100: #define K_CTRL_Y       "\x19"
                    101: #define K_CTRL_Z       "\x1a"
                    102: #define K_CTRL_LBRACE  "\x1b"
                    103: #define K_CTRL_PIPE    "\x1c"
                    104: #define K_CTRL_RBRACE  "\x1d"
                    105: #define K_CTRL_6       "\x1e"
                    106: #define K_CTRL__       "\x1f"
                    107: 
                    108: 
                    109: #define K_X_CTRL_INS   "\x1b\x5b\x32\x3b\x35\x7e"
                    110: #define K_X_CTRL_DEL   "\x1b\x5b\x33\x3b\x35\x7e"
                    111: #define K_X_CTRL_PGUP  "\x1b\x5b\x35\x3b\x35\x7e"
                    112: #define K_X_CTRL_PGDN  "\x1b\x5b\x36\x3b\x35\x7e"
                    113: #define K_X_CTRL_HOME  "\x1b\x5b\x31\x3b\x35\x48"
                    114: #define K_X_CTRL_END   "\x1b\x5b\x31\x3b\x35\x46"
                    115: #define K_X_CTRL_UP    "\x1b\x5b\x31\x3b\x35\x41"
                    116: #define K_X_CTRL_DOWN  "\x1b\x5b\x31\x3b\x35\x42"
                    117: #define K_X_CTRL_RIGHT "\x1b\x5b\x31\x3b\x35\x43"
                    118: #define K_X_CTRL_LEFT  "\x1b\x5b\x31\x3b\x35\x44"
                    119: 
                    120: #define K_X_ALT_INS    "\x1b\x5b\x32\x3b\x33\x7e"
                    121: #define K_X_ALT_DEL    "\x1b\x5b\x33\x3b\x33\x7e"
                    122: #define K_X_ALT_PGUP   "\x1b\x5b\x35\x3b\x33\x7e"
                    123: #define K_X_ALT_PGDN   "\x1b\x5b\x36\x3b\x33\x7e"
                    124: #define K_X_ALT_HOME   "\x1b\x5b\x31\x3b\x33\x48"
                    125: #define K_X_ALT_END    "\x1b\x5b\x31\x3b\x33\x46"
                    126: #define K_X_ALT_UP     "\x1b\x5b\x31\x3b\x33\x41"
                    127: #define K_X_ALT_DOWN   "\x1b\x5b\x31\x3b\x33\x42"
                    128: #define K_X_ALT_RIGHT  "\x1b\x5b\x31\x3b\x33\x43"
                    129: #define K_X_ALT_LEFT   "\x1b\x5b\x31\x3b\x33\x44"
                    130: 
                    131: #define K_X_CTL_A_INS  "\x1b\x5b\x32\x3b\x37\x7e"
                    132: #define K_X_CTL_A_DEL  "\x1b\x5b\x33\x3b\x37\x7e"
                    133: #define K_X_CTL_A_PGUP "\x1b\x5b\x35\x3b\x37\x7e"
                    134: #define K_X_CTL_A_PGDN "\x1b\x5b\x36\x3b\x37\x7e"
                    135: #define K_X_CTL_A_HOME "\x1b\x5b\x31\x3b\x37\x48"
                    136: #define K_X_CTL_A_END  "\x1b\x5b\x31\x3b\x37\x46"
                    137: #define K_X_CTL_A_UP   "\x1b\x5b\x31\x3b\x37\x41"
                    138: #define K_X_CTL_A_DOWN "\x1b\x5b\x31\x3b\x37\x42"
                    139: #define K_X_CTL_A_RIGHT        "\x1b\x5b\x31\x3b\x37\x43"
                    140: #define K_X_CTL_A_LEFT "\x1b\x5b\x31\x3b\x37\x44"
                    141: 
                    142: 
                    143: /* History types */
                    144: 
                    145: struct tagHistory {
                    146:        int             hist_len;
                    147:        char            hist_line[BUFSIZ];
                    148: 
                    149:        TAILQ_ENTRY(tagHistory) hist_next;
1.1       misho     150: };
1.2.2.1   misho     151: typedef TAILQ_HEAD(tqHistoryHead, tagHistory) history_t;
                    152: 
                    153: /* Bind keys structure types */
                    154: 
                    155: typedef int (*bindkey_func_t)(int idx, /*linebuffer_t **/ void * __restrict buffer);
                    156: typedef struct {
                    157:        int             key_len;
                    158:        unsigned char   key_ch[8];
                    159:        bindkey_func_t  key_func;
                    160: } bindkey_t;
                    161: 
                    162: 
                    163: /* Main structure, Buffer for CLI work with thread models ;-) special designed by M.Punov */
                    164: 
                    165: typedef struct {
                    166:        char                    line_mode;
                    167: 
                    168:        char                    *line_prompt;
                    169: 
                    170:        int                     line_bol;
                    171:        int                     line_eol;
                    172:        int                     line_len;
                    173:        char                    *line_buf;
                    174: 
                    175:        const struct tagHistory *line_h;
                    176:        history_t               line_history;
1.1       misho     177: 
1.2.2.1   misho     178:        bindkey_t               *line_keys;
1.1       misho     179: 
1.2.2.1   misho     180:        int                     line_in;
                    181:        int                     line_out;
                    182: } linebuffer_t;
                    183: 
                    184: /* Commands structure for CLI */
                    185: 
                    186: typedef int (*cmd_func_t)(linebuffer_t * __restrict buffer, int argc, char ** __restrict argv);
                    187: typedef struct {
                    188:        int                     cmd_level;
                    189: 
                    190:        int                     cmd_min;
                    191:        int                     cmd_len;
                    192:        char                    cmd_name[STRSIZ];
                    193: 
                    194:        char                    cmd_info[STRSIZ];
                    195:        char                    cmd_help[STRSIZ];
                    196: 
                    197:        cmd_func_t              *cmd_func;
                    198: } commands_t;
                    199: 
                    200: 
                    201: /* Error support functions */
1.1       misho     202: 
                    203: // cli_GetErrno() Get error code of last operation
                    204: inline int cli_GetErrno();
                    205: // cli_GetError() Get error text of last operation
                    206: inline const char *cli_GetError();
                    207: 
                    208: 
1.2.2.1   misho     209: /* CLI Functions */
                    210: 
1.1       misho     211: /*
1.2.2.3 ! misho     212:  * cli_BindKey() Bind function to key
        !           213:  * @key = key structure
        !           214:  * @buffer = CLI buffer
        !           215:  * return: RETCODE_ERR error, RETCODE_OK ok, >0 bind at position
        !           216: */
        !           217: int cli_BindKey(bindkey_t * __restrict key, linebuffer_t * __restrict buffer);
        !           218: 
        !           219: /*
        !           220:  * cli_addHistory() Add line to history
        !           221:  * @buffer = CLI buffer
        !           222:  * @str = Add text
        !           223:  * return: RETCODE_ERR error, RETCODE_OK ok
        !           224: */
        !           225: int cli_addHistory(linebuffer_t * __restrict buffer, const char * __restrict str);
        !           226: /*
        !           227:  * cli_saveHistory() Save history to file
        !           228:  * @buffer = CLI buffer
        !           229:  * @histfile = History filename, if NULL will be use default name
        !           230:  * @lines = Maximum history lines to save
        !           231:  * return: RETCODE_ERR error, RETCODE_OK ok
        !           232: */
        !           233: int cli_saveHistory(linebuffer_t * __restrict buffer, const char *histfile, int lines);
        !           234: /*
        !           235:  * cli_loadHistory() Load history from file
        !           236:  * @buffer = CLI buffer
        !           237:  * @histfile = History filename, if NULL will be use default name
        !           238:  * return: RETCODE_ERR error, RETCODE_OK ok
        !           239: */
        !           240: int cli_loadHistory(linebuffer_t * __restrict buffer, const char *histfile);
        !           241: /*
        !           242:  * cli_resetHistory() Reset history search in CLI session
        !           243:  * @buffer = CLI buffer
        !           244:  * return: none
        !           245: */
        !           246: inline void cli_resetHistory(linebuffer_t * __restrict buffer);
        !           247: 
        !           248: /*
        !           249:  * cli_freeLine() Clear entire line
        !           250:  * @buffer = CLI buffer
        !           251:  * return: RETCODE_ERR error, RETCODE_OK ok
        !           252: */
        !           253: inline int cli_freeLine(linebuffer_t * __restrict buffer);
        !           254: /*
        !           255:  * cli_setPrompt() Set new prompt for CLI session
        !           256:  * @buffer = CLI buffer
        !           257:  * @prompt = new text for prompt or if NULL disable prompt
        !           258:  * return: none
        !           259: */
        !           260: inline void cli_setPrompt(linebuffer_t * __restrict buffer, const char *prompt);
        !           261: 
        !           262: /*
        !           263:  * cliEnd() Clear data, Free resources and close CLI session
        !           264:  * @buffer = CLI buffer
        !           265:  * return: RETCODE_ERR error, RETCODE_OK ok
        !           266: */
        !           267: void cliEnd(linebuffer_t * __restrict buffer);
        !           268: /*
        !           269:  * cliInit() Start CLI session, allocate memory for resources and bind keys
        !           270:  * @fin = Input device handle
        !           271:  * @fout = Output device handle
        !           272:  * @prompt = text for prompt, if NULL disable prompt
        !           273:  * return: NULL if error or !=NULL CLI buffer
        !           274: */
        !           275: linebuffer_t *cliInit(int fin, int fout, const char *prompt);
        !           276: 
        !           277: /*
1.1       misho     278:  * cliExec() Execute CLI main loop
                    279:  * @cmdList = Commands list
                    280:  * @csPrompt = Prompt text
                    281:  * return: -1 error, 0 = exit w/^+D, 1 done.
                    282: */
1.2.2.2   misho     283: int cliExec(commands_t *cmdList, const char *csPrompt);
1.2       misho     284: /*
                    285:  * cliNetExec() Execute net CLI main loop
                    286:  * @cmdList = Commands list
                    287:  * @csPrompt = Prompt text
                    288:  * @sock = client socket
                    289:  * @term = stdin termios
                    290:  * @win = window size of tty
                    291:  * return: -1 error, 0 = exit w/^+D, 1 done.
                    292: */
1.2.2.2   misho     293: int cliNetExec(commands_t *cmdList, const char *csPrompt, int sock, struct termios *term, struct winsize *win);
1.2       misho     294: 
1.1       misho     295: 
1.2.2.1   misho     296: /* CLI Helper functions */
1.1       misho     297: 
                    298: /*
                    299:  * cli_Cmd_Unsupported() Builtin helper function for unsupported commands
                    300:  * @cmds = Commands list
                    301:  * @idx = Selected command ID
                    302:  * @out = Output handle
                    303:  * @args = Parsed arguments array
                    304:  * return: -1 error, 0 = ok, 1 exit from Cli!
                    305: */
                    306: int cli_Cmd_Unsupported(void *cmds, int idx, FILE *out, char ** __restrict args);
                    307: /*
                    308:  * cli_Cmd_Help() Builtin helper function for Help screen
                    309:  * @cmds = Commands list
                    310:  * @idx = Selected command ID
                    311:  * @out = Output handle
                    312:  * @args = Parsed arguments array
                    313:  * return: -1 error, 0 = ok
                    314: */
                    315: int cli_Cmd_Help(void *cmds, int idx, FILE *out, char ** __restrict args);
                    316: /*
                    317:  * cli_Cmd_Exit() Builtin helper function for Exit from Cli
                    318:  * @cmds = Commands list
                    319:  * @idx = Selected command ID
                    320:  * @out = Output handle
                    321:  * @args = Parsed arguments array
                    322:  * return: 1 exit from Cli!
                    323: */
                    324: int cli_Cmd_Exit(void *cmds, int idx, FILE *out, char ** __restrict args);
                    325: 
                    326: 
                    327: /*
                    328:  * cli_Register_Commands - Declare helper function for register and export Commands variable
                    329: */
                    330: #define CLI_REGISTER_COMMANDS(CMDS)    \
1.2.2.1   misho     331:        extern commands_t CMDS[];
1.1       misho     332: /*
                    333:  * cli_Make_Comp_Commands - Declare helper function for Commands completion arguments
                    334: */
                    335: #define CLI_MAKE_COMP_COMMANDS(FUNC, CMDS)     \
                    336:        char *FUNC(const char *text, int state) \
                    337:        { \
                    338:                register int i; \
                    339:                int len = strlen(text); \
                    340:                for (i = state; CMDS[i].cmd_name; i++) { \
                    341:                        if (strncmp(CMDS[i].cmd_name, "---", 3) && \
                    342:                                        !strncmp(CMDS[i].cmd_name, text, len)) \
                    343:                                return strdup(CMDS[i].cmd_name); \
                    344:                } \
                    345:                return NULL; \
                    346:        }
                    347: 
                    348: /*
                    349:  * cli_Make_Comp_Args - Declare helper function for Arguments completion
                    350: */
                    351: #define CLI_MAKE_COMP_ARGS(FUNC, ARGS) \
                    352:        char *FUNC(const char *text __attribute__((unused)), int state) \
                    353:        { \
                    354:                while (ARGS[state]) \
                    355:                        return strdup(ARGS[state]); \
                    356:                return NULL; \
                    357:        }
                    358: 
                    359: 
                    360: #endif

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