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