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

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