File:  [ELWIX - Embedded LightWeight unIX -] / libaitcli / inc / aitcli.h
Revision 1.3.2.1: download - view: text, annotated - select for diffs - revision graph
Wed Mar 16 17:43:07 2011 UTC (13 years, 3 months ago) by misho
Branches: cli2_1
Diff to: branchpoint 1.3: preferred, unified
add new telnet calls

    1: /*************************************************************************
    2: * (C) 2010 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
    3: *  by Michael Pounov <misho@openbsd-bg.org>
    4: *
    5: * $Author: misho $
    6: * $Id: aitcli.h,v 1.3.2.1 2011/03/16 17:43:07 misho Exp $
    7: *
    8: *************************************************************************/
    9: #ifndef __AITCLI_H
   10: #define __AITCLI_H
   11: 
   12: 
   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;
  154: };
  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 */
  187: 
  188: typedef struct {
  189: 	char			line_mode;
  190: 
  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 */
  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: 
  218: 
  219: /* TELNET support for CLI */
  220: 
  221: #define MAX_SUB_LEN	255
  222: 
  223: struct telnetAttrs {
  224: 	unsigned char ta_cmd;
  225: 	unsigned char ta_opt;
  226: 	unsigned char ta_sublen;
  227: 	unsigned char ta_sub[MAX_SUB_LEN];
  228: };
  229: 
  230: 
  231: #ifndef NDEBUG
  232: /*
  233:  * cli_telnetDumpAttrs() Telnet debug attributes list, if NDEBUG defined not include
  234:  * @attr = attributes list
  235:  * @nAttr = attributes list size
  236:  * return: none
  237: */
  238: void cli_telnetDumpAttrs(struct telnetAttrs *attr, int nAttr);
  239: #else
  240: extern void cli_telnetDumpAttrs(struct telnetAttrs *, int);
  241: #endif
  242: 
  243: 
  244: /*
  245:  * cli_telnetRecv() Telnet receive commands, negotiate with telnet peer
  246:  * @sock = socket for communication
  247:  * @attr = received attributes list, must be free after use, but if NULL receive in binary mode
  248:  * @nAttr = received attributes list size, if is NULL receive in binary mode
  249:  * @pdata = received data in supplied buffer
  250:  * @datLen = buffer pdata size
  251:  * return: 0 not present data; -1 error:: can`t read; -2 timeout; -3 EOF; >0 number of received bytes
  252: */
  253: int cli_telnetRecv(int sock, struct telnetAttrs **attr, int *nAttr, void *data, int datLen);
  254: /*
  255:  * cli_telnetSend() Telnet send commands, negotiate with telnet peer
  256:  * @sock = socket for communication
  257:  * @attr = send attributes list
  258:  * @nAttr = send attributes list size
  259:  * @data = data for send
  260:  * @datLen = data size
  261:  * @Term = Terminate with GA (Go Ahead), 1 send after data GA command
  262:  * return: 0 not sended commands; -1 error:: can`t send; >0 number of sended bytes
  263: */
  264: int cli_telnetSend(int sock, struct telnetAttrs *attr, int nAttr, void *data, int datLen, int Term);
  265: 
  266: /*
  267:  * cli_telnet_Get_SubOpt() Telnet get sub option function
  268:  * @attr = input attribute
  269:  * @code = sub-option code for opt
  270:  * @data = sub-option data
  271:  * @datLen = data size set max size in input, output return copy size
  272:  * return: -1 can`t get option; !=-1 option code
  273: */
  274: inline int cli_telnet_Get_SubOpt(struct telnetAttrs *attr, unsigned char *code, 
  275: 		void *data, unsigned char *datLen);
  276: /*
  277:  * cli_telnet_Set_SubOpt() Telnet set sub option function
  278:  * @attr = output attribute
  279:  * @opt = attribute option
  280:  * @code = sub-option code for opt, if 0xff not specified
  281:  * @data = sub-option data, if NULL not specified
  282:  * @datLen = data size, if 0 not specified
  283:  * return: -1 can`t set sub-otion; 0 ok
  284: */
  285: inline int cli_telnet_Set_SubOpt(struct telnetAttrs *attr, unsigned char opt, unsigned char code, 
  286: 		void *data, unsigned char datLen);
  287: 
  288: /*
  289:  * cli_telnet_GetCmd() Telnet get command
  290:  * @attr = input attribute
  291:  * return: -1 can`t get command; !=-1 command <<24 return sublen, <<8 return option, <<0 command
  292: */
  293: inline unsigned int cli_telnet_GetCmd(struct telnetAttrs *attr);
  294: /*
  295:  * cli_telnet_SetCmd() Telnet set command
  296:  * @attr = input attribute
  297:  * @cmd = command
  298:  * @opt = option, if 0xff not specified
  299:  * @arg1 = sub-option code, if 0xff not specified
  300:  * @arg2 = sub-option data, if NULL not specified
  301:  * @arg3 = sub-option data size, if 0 not specified data
  302:  * return: -1 can`t set command; !=-1 ok
  303: */
  304: inline int cli_telnet_SetCmd(struct telnetAttrs *attr, unsigned char cmd, unsigned char opt, ...);
  305: 
  306: /*
  307:  * cli_telnet_Answer() Automatic generate commands answer to send from telnet
  308:  * @caps = Array of capability options
  309:  * @nCaps = number of capability options
  310:  * @attr = input attribute
  311:  * @nAttr = number of input attributes
  312:  * @ans = output answered attributes, must be free() after use
  313:  * @Ans = number of output answered attributes
  314:  * return: -1 can`t answer; !=-1 ok
  315: */
  316: int cli_telnet_Answer(unsigned char *caps, int nCaps, struct telnetAttrs *attr, int nAttr, 
  317: 		struct telnetAttrs **ans, int *Ans);
  318: 
  319: 
  320: // Add capability option, x = 0 false, 1 true
  321: #define ADD_CAPS(opt, x)	((opt) | (x) ? 0x80 : 0)
  322: // Is supported this option ...
  323: #define SUP_CAPS(caps)		((caps) & 0x80)
  324: // get capability option
  325: #define CAP(caps)		((caps) & 0x7f)
  326: 
  327: 
  328: /* CLI Helper functions */
  329: 
  330: /*
  331:  * cli_Cmd_Unsupported() Builtin helper function for unsupported commands
  332:  * @buffer = CLI buffer
  333:  * @idx = Selected command ID
  334:  * @args = Parsed arguments array
  335:  * return: RETCODE_OK ok
  336: */
  337: int cli_Cmd_Unsupported(void * __restrict buffer, int idx, char ** __restrict args);
  338: 
  339: 
  340: /* CLI Functions */
  341: 
  342: /*
  343:  * cli_BindKey() Bind function to key
  344:  * @key = key structure
  345:  * @buffer = CLI buffer
  346:  * return: RETCODE_ERR error, RETCODE_OK ok, >0 bind at position
  347: */
  348: int cli_BindKey(bindkey_t * __restrict key, linebuffer_t * __restrict buffer);
  349: 
  350: 
  351: /*
  352:  * cli_addCommand() Add command to CLI session
  353:  * @buffer = CLI buffer
  354:  * @csCmd = Command name
  355:  * @cliLevel = Level in CLI, -1 unprivi(view from all), 0 main config, 1 sub config ...
  356:  * @funcCmd = Callback function when user call command
  357:  * @csInfo = Inline information for command
  358:  * @csHelp = Help line when call help
  359:  * return: RETCODE_ERR error, RETCODE_OK ok
  360: */
  361: int
  362: cli_addCommand(linebuffer_t * __restrict buffer, const char *csCmd, int cliLevel, cmd_func_t funcCmd, 
  363: 		const char *csInfo, const char *csHelp);
  364: /*
  365:  * cli_delCommand() Delete command from CLI session
  366:  * @buffer = CLI buffer
  367:  * @csCmd = Command name
  368:  * @cliLevel = Level in CLI, -1 unprivi(view from all), 0 main config, 1 sub config ...
  369:  * return: RETCODE_ERR error, RETCODE_OK ok
  370: */
  371: int
  372: cli_delCommand(linebuffer_t * __restrict buffer, const char *csCmd, int cliLevel);
  373: /*
  374:  * cli_updCommand() Update command in CLI session
  375:  * @buffer = CLI buffer
  376:  * @csCmd = Command name
  377:  * @cliLevel = Level in CLI, -1 unprivi(view from all), 0 main config, 1 sub config ...
  378:  * @funcCmd = Callback function when user call command
  379:  * @csInfo = Inline information for command
  380:  * @csHelp = Help line when call help
  381:  * return: RETCODE_ERR error, RETCODE_OK ok
  382: */
  383: int
  384: cli_updCommand(linebuffer_t * __restrict buffer, const char *csCmd, int cliLevel, cmd_func_t funcCmd, 
  385: 		const char *csInfo, const char *csHelp);
  386: 
  387: 
  388: /*
  389:  * cli_addHistory() Add line to history
  390:  * @buffer = CLI buffer
  391:  * @str = Add custom text or if NULL use readed line from CLI buffer
  392:  * return: RETCODE_ERR error, RETCODE_OK ok
  393: */
  394: int cli_addHistory(linebuffer_t * __restrict buffer, const char * __restrict str);
  395: /*
  396:  * cli_saveHistory() Save history to file
  397:  * @buffer = CLI buffer
  398:  * @histfile = History filename, if NULL will be use default name
  399:  * @lines = Maximum history lines to save
  400:  * return: RETCODE_ERR error, RETCODE_OK ok
  401: */
  402: int cli_saveHistory(linebuffer_t * __restrict buffer, const char *histfile, int lines);
  403: /*
  404:  * cli_loadHistory() Load history from file
  405:  * @buffer = CLI buffer
  406:  * @histfile = History filename, if NULL will be use default name
  407:  * return: RETCODE_ERR error, RETCODE_OK ok
  408: */
  409: int cli_loadHistory(linebuffer_t * __restrict buffer, const char *histfile);
  410: /*
  411:  * cli_resetHistory() Reset history search in CLI session
  412:  * @buffer = CLI buffer
  413:  * return: none
  414: */
  415: inline void cli_resetHistory(linebuffer_t * __restrict buffer);
  416: 
  417: /*
  418:  * cli_freeLine() Clear entire line
  419:  * @buffer = CLI buffer
  420:  * return: RETCODE_ERR error, RETCODE_OK ok
  421: */
  422: inline int cli_freeLine(linebuffer_t * __restrict buffer);
  423: /*
  424:  * cli_setPrompt() Set new prompt for CLI session
  425:  * @buffer = CLI buffer
  426:  * @prompt = new text for prompt or if NULL disable prompt
  427:  * return: none
  428: */
  429: inline void cli_setPrompt(linebuffer_t * __restrict buffer, const char *prompt);
  430: /*
  431:  * cli_Printf() Send message to CLI session
  432:  * @buffer = CLI buffer
  433:  * @fmt = printf format string
  434:  * @... = arguments defined in fmt
  435:  * return: none
  436: */
  437: inline void cli_Printf(linebuffer_t * __restrict buffer, char *fmt, ...);
  438: /*
  439:  * cli_PrintHelp() Print help screen
  440:  * @buffer = CLI buffer
  441:  * return: none
  442: */
  443: inline void cli_PrintHelp(linebuffer_t * __restrict buffer);
  444: 
  445: /*
  446:  * cliEnd() Clear data, Free resources and close CLI session
  447:  * @buffer = CLI buffer
  448:  * return: RETCODE_ERR error, RETCODE_OK ok
  449: */
  450: void cliEnd(linebuffer_t * __restrict buffer);
  451: /*
  452:  * cliInit() Start CLI session, allocate memory for resources and bind keys
  453:  * @fin = Input device handle
  454:  * @fout = Output device handle
  455:  * @prompt = text for prompt, if NULL disable prompt
  456:  * return: NULL if error or !=NULL CLI buffer
  457: */
  458: linebuffer_t *cliInit(int fin, int fout, const char *prompt);
  459: 
  460: /*
  461:  * cliInitLine() Init CLI input line terminal
  462:  * @buffer = CLI buffer
  463:  * return: none
  464: */
  465: int cliInitLine(linebuffer_t * __restrict buffer);
  466: /*
  467:  * cliReadLine() Read line from opened CLI session
  468:  * @buffer = CLI buffer
  469:  * return: NULL if error or !=NULL readed line, must be free after use!
  470: */
  471: char *cliReadLine(linebuffer_t * __restrict buffer);
  472: 
  473: /*
  474:  * cliLoop() CLI main loop
  475:  * @buffer = CLI buffer
  476:  * @csHistFile = History file name
  477:  * return: RETCODE_ERR error, RETCODE_OK ok
  478: */
  479: int cliLoop(linebuffer_t * __restrict buffer, const char *csHistFile);
  480: /*
  481:  * cliNetLoop() CLI network main loop binded to socket
  482:  * @buffer = CLI buffer
  483:  * @csHistFile = History file name
  484:  * @sock = client socket
  485:  * @term = stdin termios
  486:  * @win = window size of tty
  487:  * return: RETCODE_ERR error, RETCODE_OK ok
  488: */
  489: int cliNetLoop(linebuffer_t * __restrict buffer, const char *csHistFile, int sock);
  490: 
  491: 
  492: #endif

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