File:  [ELWIX - Embedded LightWeight unIX -] / libaitcli / src / aitcli.c
Revision 1.8.2.3: download - view: text, annotated - select for diffs - revision graph
Tue Oct 8 09:28:15 2013 UTC (10 years, 10 months ago) by misho
Branches: cli3_3
Diff to: branchpoint 1.8: preferred, unified
revert link_level

    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.c,v 1.8.2.3 2013/10/08 09:28:15 misho Exp $
    7: *
    8: **************************************************************************
    9: The ELWIX and AITNET software is distributed under the following
   10: terms:
   11: 
   12: All of the documentation and software included in the ELWIX and AITNET
   13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
   14: 
   15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
   16: 	by Michael Pounov <misho@elwix.org>.  All rights reserved.
   17: 
   18: Redistribution and use in source and binary forms, with or without
   19: modification, are permitted provided that the following conditions
   20: are met:
   21: 1. Redistributions of source code must retain the above copyright
   22:    notice, this list of conditions and the following disclaimer.
   23: 2. Redistributions in binary form must reproduce the above copyright
   24:    notice, this list of conditions and the following disclaimer in the
   25:    documentation and/or other materials provided with the distribution.
   26: 3. All advertising materials mentioning features or use of this software
   27:    must display the following acknowledgement:
   28: This product includes software developed by Michael Pounov <misho@elwix.org>
   29: ELWIX - Embedded LightWeight unIX and its contributors.
   30: 4. Neither the name of AITNET nor the names of its contributors
   31:    may be used to endorse or promote products derived from this software
   32:    without specific prior written permission.
   33: 
   34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
   35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   37: ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   44: SUCH DAMAGE.
   45: */
   46: #include "global.h"
   47: #include "cli.h"
   48: 
   49: 
   50: #pragma GCC visibility push(hidden)
   51: 
   52: int cli_Errno;
   53: char cli_Error[STRSIZ];
   54: 
   55: #pragma GCC visibility pop
   56: 
   57: // cli_GetErrno() Get error code of last operation
   58: int
   59: cli_GetErrno()
   60: {
   61: 	return cli_Errno;
   62: }
   63: 
   64: // cli_GetError() Get error text of last operation
   65: const char *
   66: cli_GetError()
   67: {
   68: 	return cli_Error;
   69: }
   70: 
   71: // cli_SetErr() Set error to variables for internal use!!!
   72: void
   73: cli_SetErr(int eno, char *estr, ...)
   74: {
   75: 	va_list lst;
   76: 
   77: 	cli_Errno = eno;
   78: 	memset(cli_Error, 0, sizeof cli_Error);
   79: 	va_start(lst, estr);
   80: 	vsnprintf(cli_Error, sizeof cli_Error, estr, lst);
   81: 	va_end(lst);
   82: }
   83: 
   84: // ------------------------------------------------------------
   85: 
   86: static inline void
   87: clrscrEOL(linebuffer_t * __restrict buf)
   88: {
   89: 	register int i;
   90: 
   91: 	if (buf && buf->line_prompt) {
   92: 		write(buf->line_out, K_CR, 1);
   93: 
   94: 		for (i = 0; i < buf->line_len; i++)
   95: 			write(buf->line_out, K_SPACE, 1);
   96: 	}
   97: }
   98: 
   99: static inline void
  100: printfEOL(linebuffer_t * __restrict buf, int len, int prompt)
  101: {
  102: 	if (buf) {
  103: 		if (prompt && buf->line_prompt) {
  104: 			write(buf->line_out, K_CR, 1);
  105: 			write(buf->line_out, buf->line_prompt, buf->line_bol);
  106: 		}
  107: 
  108: 		write(buf->line_out, buf->line_buf, len == -1 ? 
  109: 				buf->line_eol - buf->line_bol: len);
  110: 	}
  111: }
  112: 
  113: static inline void
  114: printfCR(linebuffer_t * __restrict buf, int prompt)
  115: {
  116: 	if (buf && prompt && buf->line_prompt) {
  117: 		write(buf->line_out, K_CR, 1);
  118: 		write(buf->line_out, buf->line_prompt, buf->line_bol);
  119: 	}
  120: }
  121: 
  122: static inline void
  123: printfNL(linebuffer_t * __restrict buf, int prompt)
  124: {
  125: 	if (buf) {
  126: 		write(buf->line_out, K_ENTER, 1);
  127: 
  128: 		if (prompt)
  129: 			if (prompt && buf->line_prompt)
  130: 				write(buf->line_out, buf->line_prompt, buf->line_bol);
  131: 	}
  132: }
  133: 
  134: // ------------------------------------------------------------
  135: 
  136: static int
  137: bufCHAR(int idx, void * __restrict cli_buffer)
  138: {
  139: 	linebuffer_t *buf = cli_buffer;
  140: 	int pos;
  141: 
  142: 	if (!cli_buffer || idx < 0 || idx > MAX_BINDKEY)
  143: 		return RETCODE_ERR;
  144: 
  145: 	pos = buf->line_eol - buf->line_bol;
  146: 
  147: 	if (buf->line_mode == LINEMODE_INS)
  148: 		memmove(buf->line_buf + pos + buf->line_keys[idx].key_len, buf->line_buf + pos, 
  149: 				buf->line_len - buf->line_eol);
  150: 	if (buf->line_mode == LINEMODE_INS || buf->line_eol == buf->line_len - 1)
  151: 		buf->line_len += buf->line_keys[idx].key_len;
  152: 	buf->line_eol += buf->line_keys[idx].key_len;
  153: 
  154: 	memcpy(buf->line_buf + pos, buf->line_keys[idx].key_ch, buf->line_keys[idx].key_len);
  155: 	buf->line_buf[buf->line_len - 1] = 0;
  156: 
  157: 	write(buf->line_out, buf->line_keys[idx].key_ch, buf->line_keys[idx].key_len);
  158: 
  159: 	if (buf->line_mode == LINEMODE_INS) {
  160: 		write(buf->line_out, (const u_char*) buf->line_buf + pos + buf->line_keys[idx].key_len, 
  161: 				buf->line_len - buf->line_eol);
  162: 		printfEOL(buf, -1, 1);
  163: 	}
  164: 	return RETCODE_OK;
  165: }
  166: 
  167: static int
  168: bufEOL(int idx, void * __restrict cli_buffer)
  169: {
  170: 	if (!cli_buffer || idx < 0 || idx > MAX_BINDKEY)
  171: 		return RETCODE_ERR;
  172: 
  173: 	printfCR(cli_buffer, 1);
  174: 	return RETCODE_EOL;
  175: }
  176: 
  177: static int
  178: bufEOF(int idx, void * __restrict cli_buffer)
  179: {
  180: 	/*
  181: 	if (!cli_buffer || idx < 0 || idx > MAX_BINDKEY)
  182: 		return RETCODE_ERR;
  183: 	*/
  184: 
  185: 	return RETCODE_EOF;
  186: }
  187: 
  188: static int
  189: bufUP(int idx, void * __restrict cli_buffer)
  190: {
  191: 	linebuffer_t *buf = cli_buffer;
  192: 	int pos;
  193: 
  194: 	if (!cli_buffer || idx < 0 || idx > MAX_BINDKEY)
  195: 		return RETCODE_ERR;
  196: 
  197: 	if (!buf->line_h)
  198: 		buf->line_h = TAILQ_FIRST(&buf->line_history);
  199: 	else
  200: 		buf->line_h = TAILQ_NEXT(buf->line_h, hist_next);
  201: 	if (!buf->line_h)
  202: 		return RETCODE_OK;
  203: 
  204: 	clrscrEOL(buf);
  205: 	cli_freeLine(buf);
  206: 
  207: 	pos = buf->line_eol - buf->line_bol;
  208: 
  209: 	buf->line_len += buf->line_h->hist_len;
  210: 	buf->line_eol += buf->line_h->hist_len;
  211: 
  212: 	memcpy(buf->line_buf + pos, buf->line_h->hist_line, buf->line_h->hist_len);
  213: 	buf->line_buf[buf->line_len - 1] = 0;
  214: 
  215: 	printfEOL(buf, -1, 1);
  216: 	return RETCODE_OK;
  217: }
  218: 
  219: static int
  220: bufDOWN(int idx, void * __restrict cli_buffer)
  221: {
  222: 	linebuffer_t *buf = cli_buffer;
  223: 	int pos;
  224: 
  225: 	if (!cli_buffer || idx < 0 || idx > MAX_BINDKEY)
  226: 		return RETCODE_ERR;
  227: 
  228: 	if (!buf->line_h)
  229: 		buf->line_h = TAILQ_LAST(&buf->line_history, tqHistoryHead);
  230: 	else
  231: 		buf->line_h = TAILQ_PREV(buf->line_h, tqHistoryHead, hist_next);
  232: 	if (!buf->line_h)
  233: 		return RETCODE_OK;
  234: 
  235: 	clrscrEOL(buf);
  236: 	cli_freeLine(buf);
  237: 
  238: 	pos = buf->line_eol - buf->line_bol;
  239: 
  240: 	buf->line_len += buf->line_h->hist_len;
  241: 	buf->line_eol += buf->line_h->hist_len;
  242: 
  243: 	memcpy(buf->line_buf + pos, buf->line_h->hist_line, buf->line_h->hist_len);
  244: 	buf->line_buf[buf->line_len - 1] = 0;
  245: 
  246: 	printfEOL(buf, -1, 1);
  247: 	return RETCODE_OK;
  248: }
  249: 
  250: static int
  251: bufCLR(int idx, void * __restrict cli_buffer)
  252: {
  253: 	if (!cli_buffer || idx < 0 || idx > MAX_BINDKEY)
  254: 		return RETCODE_ERR;
  255: 
  256: 	clrscrEOL(cli_buffer);
  257: 	cli_freeLine(cli_buffer);
  258: 
  259: 	printfCR(cli_buffer, 1);
  260: 	return RETCODE_OK;
  261: }
  262: 
  263: static int
  264: bufBS(int idx, void * __restrict cli_buffer)
  265: {
  266: 	linebuffer_t *buf = cli_buffer;
  267: 
  268: 	if (!cli_buffer || idx < 0 || idx > MAX_BINDKEY)
  269: 		return RETCODE_ERR;
  270: 
  271: 	if (buf->line_bol < buf->line_eol) {
  272: 		clrscrEOL(buf);
  273: 
  274: 		buf->line_eol--;
  275: 		buf->line_len--;
  276: 		memmove(buf->line_buf + buf->line_eol - buf->line_bol, 
  277: 				buf->line_buf + buf->line_eol - buf->line_bol + 1, 
  278: 				buf->line_len - buf->line_eol);
  279: 		buf->line_buf[buf->line_len - 1] = 0;
  280: 
  281: 		printfEOL(buf, buf->line_len - 1, 1);
  282: 		printfEOL(buf, -1, 1);
  283: 	}
  284: 
  285: 	return RETCODE_OK;
  286: }
  287: 
  288: static int
  289: bufBTAB(int idx, void * __restrict cli_buffer)
  290: {
  291: 	linebuffer_t *buf = cli_buffer;
  292: 
  293: 	if (!cli_buffer || idx < 0 || idx > MAX_BINDKEY)
  294: 		return RETCODE_ERR;
  295: 
  296: 	if (buf->line_bol < buf->line_eol) {
  297: 		clrscrEOL(buf);
  298: 
  299: 		buf->line_len = buf->line_eol - buf->line_bol + 1;
  300: 		buf->line_buf[buf->line_len - 1] = 0;
  301: 
  302: 		printfEOL(buf, -1, 1);
  303: 	}
  304: 
  305: 	return RETCODE_OK;
  306: }
  307: 
  308: static int
  309: bufMODE(int idx, void * __restrict cli_buffer)
  310: {
  311: 	linebuffer_t *buf = cli_buffer;
  312: 
  313: 	if (!cli_buffer || idx < 0 || idx > MAX_BINDKEY)
  314: 		return RETCODE_ERR;
  315: 
  316: 	buf->line_mode = !buf->line_mode ? LINEMODE_OVER : LINEMODE_INS;
  317: 	return RETCODE_OK;
  318: }
  319: 
  320: static int
  321: bufBEGIN(int idx, void * __restrict cli_buffer)
  322: {
  323: 	linebuffer_t *buf = cli_buffer;
  324: 
  325: 	if (!cli_buffer || idx < 0 || idx > MAX_BINDKEY)
  326: 		return RETCODE_ERR;
  327: 
  328: 	buf->line_eol = buf->line_bol;
  329: 
  330: 	printfCR(buf, 1);
  331: 	return RETCODE_OK;
  332: }
  333: 
  334: static int
  335: bufEND(int idx, void * __restrict cli_buffer)
  336: {
  337: 	linebuffer_t *buf = cli_buffer;
  338: 
  339: 	if (!cli_buffer || idx < 0 || idx > MAX_BINDKEY)
  340: 		return RETCODE_ERR;
  341: 
  342: 	buf->line_eol = buf->line_len - 1;
  343: 
  344: 	printfEOL(buf, -1, 1);
  345: 	return RETCODE_OK;
  346: }
  347: 
  348: static int
  349: bufLEFT(int idx, void * __restrict cli_buffer)
  350: {
  351: 	linebuffer_t *buf = cli_buffer;
  352: 
  353: 	if (!cli_buffer || idx < 0 || idx > MAX_BINDKEY)
  354: 		return RETCODE_ERR;
  355: 
  356: 	if (buf->line_bol < buf->line_eol)
  357: 		printfEOL(buf, --buf->line_eol - buf->line_bol, 1);
  358: 
  359: 	return RETCODE_OK;
  360: }
  361: 
  362: static int
  363: bufRIGHT(int idx, void * __restrict cli_buffer)
  364: {
  365: 	linebuffer_t *buf = cli_buffer;
  366: 
  367: 	if (!cli_buffer || idx < 0 || idx > MAX_BINDKEY)
  368: 		return RETCODE_ERR;
  369: 
  370: 	if (buf->line_eol < buf->line_len - 1)
  371: 		printfEOL(buf, ++buf->line_eol - buf->line_bol, 1);
  372: 
  373: 	return RETCODE_OK;
  374: }
  375: 
  376: static int
  377: bufDEL(int idx, void * __restrict cli_buffer)
  378: {
  379: 	linebuffer_t *buf = cli_buffer;
  380: 
  381: 	if (!cli_buffer || idx < 0 || idx > MAX_BINDKEY)
  382: 		return RETCODE_ERR;
  383: 
  384: 	clrscrEOL(buf);
  385: 
  386: 	buf->line_len--;
  387: 	memmove(buf->line_buf + buf->line_eol - buf->line_bol, 
  388: 			buf->line_buf + buf->line_eol - buf->line_bol + 1, 
  389: 			buf->line_len - buf->line_eol);
  390: 	buf->line_buf[buf->line_len - 1] = 0;
  391: 
  392: 	printfEOL(buf, buf->line_len - 1, 1);
  393: 	printfEOL(buf, -1, 1);
  394: 
  395: 	return RETCODE_OK;
  396: }
  397: 
  398: static int
  399: bufComp(int idx, void * __restrict cli_buffer)
  400: {
  401: 	linebuffer_t *buf = cli_buffer;
  402: 	char *str, *s, **app, *items[MAX_PROMPT_ITEMS], szLine[STRSIZ];
  403: 	register int i, j;
  404: 	struct tagCommand *cmd, *c;
  405: 	int pos, ret = RETCODE_OK;
  406: 
  407: 	if (!cli_buffer || idx < 0 || idx > MAX_BINDKEY)
  408: 		return RETCODE_ERR;
  409: 
  410: 	str = e_strdup(buf->line_buf);
  411: 	if (!str)
  412: 		return RETCODE_ERR;
  413: 	else {
  414: 		s = str;
  415: 		str_Trim(s);
  416: 	}
  417: 
  418: 	i = j = 0;
  419: 	c = NULL;
  420: 	memset(szLine, 0, STRSIZ);
  421: 	if (*s) {
  422: 		memset(items, 0, sizeof(char*) * MAX_PROMPT_ITEMS);
  423: 		for (app = items, i = 0; app < items + MAX_PROMPT_ITEMS - 1 && 
  424: 				(*app = strsep(&s, " \t")); 
  425: 			 	*app ? i++ : i, *app ? app++ : app);
  426: 
  427: 		if (i) {
  428: 			SLIST_FOREACH(cmd, &buf->line_cmds, cmd_next) {
  429: 				if (cmd->cmd_level == buf->line_level && 
  430: 						!strncmp(cmd->cmd_name, items[0], 
  431: 							strlen(items[0]))) {
  432: 					if (strncmp(cmd->cmd_name, CLI_CMD_SEP, 
  433: 								strlen(CLI_CMD_SEP))) {
  434: 						j++;
  435: 						c = cmd;
  436: 						strlcat(szLine, " ", STRSIZ);
  437: 						strlcat(szLine, cmd->cmd_name, STRSIZ);
  438: 					}
  439: 				}
  440: 			}
  441: 
  442: 			if (i > 1 && c) {
  443: 				/* we are on argument of command and has complition info */
  444: 				j++;	// always must be j > 1 ;) for arguments
  445: 				strlcpy(szLine, c->cmd_info, STRSIZ);
  446: 			}
  447: 		} else {
  448: 			/* we have valid char but i == 0, this case is illegal */
  449: 			ret = RETCODE_ERR;
  450: 			goto endcomp;
  451: 		}
  452: 	} else {
  453: 		/* we on 0 position of prompt, show commands for this level */
  454: 		SLIST_FOREACH(cmd, &buf->line_cmds, cmd_next) {
  455: 			if (cmd->cmd_level == buf->line_level)
  456: 				if (strncmp(cmd->cmd_name, CLI_CMD_SEP, strlen(CLI_CMD_SEP))) {
  457: 					j++;
  458: 					c = cmd;
  459: 					strlcat(szLine, " ", STRSIZ);
  460: 					strlcat(szLine, cmd->cmd_name, STRSIZ);
  461: 				}
  462: 		}
  463: 	}
  464: 
  465: 	/* completion show actions ... */
  466: 	if (j > 1 && c) {
  467: 		printfNL(buf, 0);
  468: 		write(buf->line_out, szLine, strlen(szLine));
  469: 		printfNL(buf, 1);
  470: 		printfEOL(buf, buf->line_len - 1, 1);
  471: 		printfEOL(buf, -1, 1);
  472: 	}
  473: 	if (j == 1 && c) {
  474: 		clrscrEOL(buf);
  475: 		cli_freeLine(buf);
  476: 
  477: 		pos = buf->line_eol - buf->line_bol;
  478: 
  479: 		buf->line_len += c->cmd_len + 1;
  480: 		buf->line_eol += c->cmd_len + 1;
  481: 
  482: 		memcpy(buf->line_buf + pos, c->cmd_name, c->cmd_len);
  483: 		buf->line_buf[pos + c->cmd_len] = (u_char) *K_SPACE;
  484: 		buf->line_buf[buf->line_len - 1] = 0;
  485: 
  486: 		printfEOL(buf, -1, 1);
  487: 	}
  488: 
  489: endcomp:
  490: 	e_free(str);
  491: 	return ret;
  492: }
  493: 
  494: static int
  495: bufHelp(int idx, void * __restrict cli_buffer)
  496: {
  497: 	linebuffer_t *buf = cli_buffer;
  498: 
  499: 	if (!cli_buffer || idx < 0 || idx > MAX_BINDKEY)
  500: 		return RETCODE_ERR;
  501: 
  502: 	cli_Cmd_Help(buf, -1, NULL);
  503: 
  504: 	printfEOL(buf, buf->line_len - 1, 1);
  505: 	printfEOL(buf, -1, 1);
  506: 	return RETCODE_OK;
  507: }
  508: 
  509: 
  510: /*
  511:  * cli_Printf() - Send message to CLI session
  512:  *
  513:  * @cli_buffer = CLI buffer
  514:  * @fmt = printf format string
  515:  * @... = arguments defined in fmt
  516:  * return: none
  517: */
  518: void
  519: cli_Printf(linebuffer_t * __restrict cli_buffer, char *fmt, ...)
  520: {
  521: 	va_list lst;
  522: 	FILE *f;
  523: 
  524: 	if (fmt) {
  525: 		f = fdopen(cli_buffer->line_out, "a");
  526: 		if (!f) {
  527: 			LOGERR;
  528: 			return;
  529: 		}
  530: 
  531: 		va_start(lst, fmt);
  532: 		vfprintf(f, fmt, lst);
  533: 		va_end(lst);
  534: 	} else
  535: 		cli_SetErr(EINVAL, "Invalid input parameters ...");
  536: }
  537: 
  538: /*
  539:  * cli_PrintHelp() - Print help screen
  540:  *
  541:  * @cli_buffer = CLI buffer
  542:  * return: none
  543: */
  544: void
  545: cli_PrintHelp(linebuffer_t * __restrict cli_buffer)
  546: {
  547: 	if (cli_buffer) {
  548: 		bufHelp(0, cli_buffer);
  549: 		clrscrEOL(cli_buffer);
  550: 	} else
  551: 		cli_SetErr(EINVAL, "Invalid input parameters ...");
  552: }
  553: 
  554: 
  555: /*
  556:  * cli_BindKey() - Bind function to key
  557:  *
  558:  * @key = key structure
  559:  * @cli_buffer = CLI buffer
  560:  * return: RETCODE_ERR error, RETCODE_OK ok, >0 bind at position
  561: */
  562: int
  563: cli_BindKey(bindkey_t * __restrict key, linebuffer_t * __restrict cli_buffer)
  564: {
  565: 	register int i;
  566: 
  567: 	if (!key || !cli_buffer) {
  568: 		cli_SetErr(EINVAL, "Invalid input parameters ...");
  569: 		return RETCODE_ERR;
  570: 	}
  571: 
  572: 	for (i = 0; i < MAX_BINDKEY; i++)
  573: 		if (key->key_len == cli_buffer->line_keys[i].key_len && 
  574: 				!memcmp(key->key_ch, cli_buffer->line_keys[i].key_ch, 
  575: 					key->key_len)) {
  576: 			cli_buffer->line_keys[i].key_func = key->key_func;
  577: 			return i;
  578: 		}
  579: 
  580: 	return RETCODE_OK;
  581: }
  582: 
  583: 
  584: /*
  585:  * cli_addCommand() - Add command to CLI session
  586:  *
  587:  * @cli_buffer = CLI buffer
  588:  * @csCmd = Command name
  589:  * @cliLevel = Level in CLI, -1 unprivi(view from all), 0 main config, 1 sub config ...
  590:  * @funcCmd = Callback function when user call command
  591:  * @csInfo = Inline information for command
  592:  * @csHelp = Help line when call help
  593:  * return: RETCODE_ERR error, RETCODE_OK ok
  594: */
  595: int
  596: cli_addCommand(linebuffer_t * __restrict cli_buffer, const char *csCmd, 
  597: 		int cliLevel, cmd_func_t funcCmd, 
  598: 		const char *csInfo, const char *csHelp)
  599: {
  600: 	struct tagCommand *cmd;
  601: 
  602: 	if (!cli_buffer || !csCmd) {
  603: 		cli_SetErr(EINVAL, "Invalid input parameters ...");
  604: 		return RETCODE_ERR;
  605: 	}
  606: 
  607: 	cmd = e_malloc(sizeof(struct tagCommand));
  608: 	if (!cmd) {
  609: 		LOGERR;
  610: 		return RETCODE_ERR;
  611: 	} else
  612: 		memset(cmd, 0, sizeof(struct tagCommand));
  613: 
  614: 	cmd->cmd_level = cliLevel;
  615: 	cmd->cmd_func = funcCmd;
  616: 	cmd->cmd_len = strlcpy(cmd->cmd_name, csCmd, STRSIZ);
  617: 	if (csInfo)
  618: 		strlcpy(cmd->cmd_info, csInfo, STRSIZ);
  619: 	if (csHelp)
  620: 		strlcpy(cmd->cmd_help, csHelp, STRSIZ);
  621: 	SLIST_INSERT_HEAD(&cli_buffer->line_cmds, cmd, cmd_next);
  622: 	return RETCODE_OK;
  623: }
  624: 
  625: /*
  626:  * cli_delCommand() - Delete command from CLI session
  627:  *
  628:  * @cli_buffer = CLI buffer
  629:  * @csCmd = Command name
  630:  * @cliLevel = Level in CLI, -1 unprivi(view from all), 0 main config, 1 sub config ...
  631:  * return: RETCODE_ERR error, RETCODE_OK ok
  632: */
  633: int
  634: cli_delCommand(linebuffer_t * __restrict cli_buffer, const char *csCmd, int cliLevel)
  635: {
  636: 	struct tagCommand *cmd;
  637: 	int ret = RETCODE_OK;
  638: 
  639: 	if (!cli_buffer || !csCmd) {
  640: 		cli_SetErr(EINVAL, "Invalid input parameters ...");
  641: 		return RETCODE_ERR;
  642: 	}
  643: 
  644: 	SLIST_FOREACH(cmd, &cli_buffer->line_cmds, cmd_next) 
  645: 		if (cmd->cmd_level == cliLevel && !strcmp(cmd->cmd_name, csCmd)) {
  646: 			ret = 1;
  647: 			SLIST_REMOVE(&cli_buffer->line_cmds, cmd, tagCommand, cmd_next);
  648: 			e_free(cmd);
  649: 			break;
  650: 		}
  651: 
  652: 	return ret;
  653: }
  654: 
  655: /*
  656:  * cli_updCommand() - Update command in CLI session
  657:  *
  658:  * @cli_buffer = CLI buffer
  659:  * @csCmd = Command name
  660:  * @cliLevel = Level in CLI, -1 unprivi(view from all), 0 main config, 1 sub config ...
  661:  * @funcCmd = Callback function when user call command
  662:  * @csInfo = Inline information for command
  663:  * @csHelp = Help line when call help
  664:  * return: RETCODE_ERR error, RETCODE_OK ok
  665: */
  666: int
  667: cli_updCommand(linebuffer_t * __restrict cli_buffer, const char *csCmd, 
  668: 		int cliLevel, cmd_func_t funcCmd, 
  669: 		const char *csInfo, const char *csHelp)
  670: {
  671: 	struct tagCommand *cmd;
  672: 	int ret = RETCODE_OK;
  673: 
  674: 	if (!cli_buffer || !csCmd) {
  675: 		cli_SetErr(EINVAL, "Invalid input parameters ...");
  676: 		return RETCODE_ERR;
  677: 	}
  678: 
  679: 	SLIST_FOREACH(cmd, &cli_buffer->line_cmds, cmd_next) 
  680: 		if (cmd->cmd_level == cliLevel && !strcmp(cmd->cmd_name, csCmd)) {
  681: 			ret = 1;
  682: 
  683: 			if (funcCmd)
  684: 				cmd->cmd_func = funcCmd;
  685: 			if (csInfo)
  686: 				strlcpy(cmd->cmd_info, csInfo, STRSIZ);
  687: 			if (csHelp)
  688: 				strlcpy(cmd->cmd_help, csHelp, STRSIZ);
  689: 
  690: 			break;
  691: 		}
  692: 
  693: 	return ret;
  694: }
  695: 
  696: 
  697: /*
  698:  * cli_addHistory() - Add line to history
  699:  *
  700:  * @cli_buffer = CLI buffer
  701:  * @str = Add custom text or if NULL use readed line from CLI buffer
  702:  * return: RETCODE_ERR error, RETCODE_OK ok
  703: */
  704: int
  705: cli_addHistory(linebuffer_t * __restrict cli_buffer, const char * __restrict str)
  706: {
  707: 	struct tagHistory *h;
  708: 
  709: 	if (!cli_buffer) {
  710: 		cli_SetErr(EINVAL, "Invalid input parameters ...");
  711: 		return RETCODE_ERR;
  712: 	}
  713: 
  714: 	if (!(h = e_malloc(sizeof(struct tagHistory)))) {
  715: 		LOGERR;
  716: 		return RETCODE_ERR;
  717: 	} else
  718: 		memset(h, 0, sizeof(struct tagHistory));
  719: 
  720: 	if (str) {
  721: 		if (!*str) {
  722: 			e_free(h);
  723: 			return RETCODE_OK;
  724: 		}
  725: 
  726: 		h->hist_len = strlcpy(h->hist_line, str, BUFSIZ);
  727: 	} else {
  728: 		if (!*cli_buffer->line_buf || cli_buffer->line_len < 2) {
  729: 			e_free(h);
  730: 			return RETCODE_OK;
  731: 		}
  732: 
  733: 		memcpy(h->hist_line, cli_buffer->line_buf, (h->hist_len = cli_buffer->line_len));
  734: 		str_Trim(h->hist_line);
  735: 		h->hist_len = strlen(h->hist_line);
  736: 	}
  737: 
  738: 	TAILQ_INSERT_HEAD(&cli_buffer->line_history, h, hist_next);
  739: 	return h->hist_len;
  740: }
  741: 
  742: /*
  743:  * cli_saveHistory() - Save history to file
  744:  *
  745:  * @cli_buffer = CLI buffer
  746:  * @histfile = History filename, if NULL will be use default name
  747:  * @lines = Maximum history lines to save
  748:  * return: RETCODE_ERR error, RETCODE_OK ok
  749: */
  750: int
  751: cli_saveHistory(linebuffer_t * __restrict cli_buffer, const char *histfile, int lines)
  752: {
  753: 	FILE *f;
  754: 	mode_t mode;
  755: 	char szFName[MAXPATHLEN];
  756: 	struct tagHistory *h;
  757: 
  758: 	if (!cli_buffer) {
  759: 		cli_SetErr(EINVAL, "Invalid input parameters ...");
  760: 		return RETCODE_ERR;
  761: 	}
  762: 	if (!histfile)
  763: 		strlcpy(szFName, HISTORY_FILE, MAXPATHLEN);
  764: 	else
  765: 		strlcpy(szFName, histfile, MAXPATHLEN);
  766: 
  767: 	mode = umask(0177);
  768: 	f = fopen(szFName, "w");
  769: 	if (!f) {
  770: 		LOGERR;
  771: 		return RETCODE_ERR;
  772: 	}
  773: 
  774: 	TAILQ_FOREACH(h, &cli_buffer->line_history, hist_next) {
  775: 		fprintf(f, "%s\n", h->hist_line);
  776: 
  777: 		if (lines)
  778: 			lines--;
  779: 		else
  780: 			break;
  781: 	}
  782: 
  783: 	fclose(f);
  784: 	umask(mode);
  785: 
  786: 	return RETCODE_OK;
  787: }
  788: 
  789: /*
  790:  * cli_loadHistory() - Load history from file
  791:  *
  792:  * @cli_buffer = CLI buffer
  793:  * @histfile = History filename, if NULL will be use default name
  794:  * return: RETCODE_ERR error, RETCODE_OK ok
  795: */
  796: int
  797: cli_loadHistory(linebuffer_t * __restrict cli_buffer, const char *histfile)
  798: {
  799: 	FILE *f;
  800: 	char szFName[MAXPATHLEN], buf[BUFSIZ];
  801: 	struct tagHistory *h;
  802: 
  803: 	if (!cli_buffer) {
  804: 		cli_SetErr(EINVAL, "Invalid input parameters ...");
  805: 		return RETCODE_ERR;
  806: 	}
  807: 	if (!histfile)
  808: 		strlcpy(szFName, HISTORY_FILE, MAXPATHLEN);
  809: 	else
  810: 		strlcpy(szFName, histfile, MAXPATHLEN);
  811: 
  812: 	f = fopen(szFName, "r");
  813: 	if (!f)
  814: 		return RETCODE_OK;
  815: 
  816: 	while (fgets(buf, BUFSIZ, f)) {
  817: 		if (!*buf || *buf == '#')
  818: 			continue;
  819: 		else
  820: 			str_Trim(buf);
  821: 
  822: 		if (!(h = e_malloc(sizeof(struct tagHistory)))) {
  823: 			LOGERR;
  824: 			fclose(f);
  825: 			return RETCODE_ERR;
  826: 		} else
  827: 			memset(h, 0, sizeof(struct tagHistory));
  828: 
  829: 		h->hist_len = strlcpy(h->hist_line, buf, BUFSIZ);
  830: 		TAILQ_INSERT_TAIL(&cli_buffer->line_history, h, hist_next);
  831: 	}
  832: 
  833: 	fclose(f);
  834: 
  835: 	return RETCODE_OK;
  836: }
  837: 
  838: /*
  839:  * cli_resetHistory() - Reset history search in CLI session
  840:  *
  841:  * @cli_buffer = CLI buffer
  842:  * return: none
  843: */
  844: void
  845: cli_resetHistory(linebuffer_t * __restrict cli_buffer)
  846: {
  847: 	cli_buffer->line_h = NULL;
  848: }
  849: 
  850: 
  851: /*
  852:  * cli_freeLine() - Clear entire line
  853:  *
  854:  * @cli_buffer = CLI buffer
  855:  * return: RETCODE_ERR error, RETCODE_OK ok
  856: */
  857: int
  858: cli_freeLine(linebuffer_t * __restrict cli_buffer)
  859: {
  860: 	int code = RETCODE_ERR;
  861: 
  862: 	if (cli_buffer) {
  863: 		if (cli_buffer->line_buf)
  864: 			e_free(cli_buffer->line_buf);
  865: 
  866: 		cli_buffer->line_buf = e_malloc(BUFSIZ);
  867: 		if (cli_buffer->line_buf) {
  868: 			memset(cli_buffer->line_buf, 0, BUFSIZ);
  869: 			cli_buffer->line_eol = cli_buffer->line_bol;
  870: 			cli_buffer->line_len = 1 + cli_buffer->line_eol;
  871: 
  872: 			code = RETCODE_OK;
  873: 		} else
  874: 			LOGERR;
  875: 	} else
  876: 		cli_SetErr(EINVAL, "Invalid input parameters ...");
  877: 
  878: 	return code;
  879: }
  880: 
  881: /*
  882:  * cli_setPrompt() - Set new prompt for CLI session
  883:  *
  884:  * @cli_buffer = CLI buffer
  885:  * @prompt = new text for prompt or if NULL disable prompt
  886:  * return: none
  887: */
  888: void
  889: cli_setPrompt(linebuffer_t * __restrict cli_buffer, const char *prompt)
  890: {
  891: 	if (cli_buffer) {
  892: 		if (cli_buffer->line_prompt) {
  893: 			e_free(cli_buffer->line_prompt);
  894: 			cli_buffer->line_prompt = NULL;
  895: 			cli_buffer->line_bol = 0;
  896: 		}
  897: 
  898: 		if (prompt) {
  899: 			cli_buffer->line_prompt = e_strdup(prompt);
  900: 			if (cli_buffer->line_prompt) {
  901: 				cli_buffer->line_bol = strlen(cli_buffer->line_prompt);
  902: 				cli_buffer->line_eol = cli_buffer->line_bol;
  903: 				cli_buffer->line_len = 1 + cli_buffer->line_eol;
  904: 			} else
  905: 				LOGERR;
  906: 		}
  907: 	} else
  908: 		cli_SetErr(EINVAL, "Invalid input parameters ...");
  909: }
  910: 
  911: 
  912: /*
  913:  * cliEnd() - Clear data, Free resources and close CLI session
  914:  *
  915:  * @cli_buffer = CLI buffer
  916:  * return: RETCODE_ERR error, RETCODE_OK ok
  917: */
  918: void
  919: cliEnd(linebuffer_t * __restrict cli_buffer)
  920: {
  921: 	struct tagHistory *h;
  922: 	struct tagCommand *c;
  923: 
  924: 	if (cli_buffer) {
  925: 		while ((c = SLIST_FIRST(&cli_buffer->line_cmds))) {
  926: 			SLIST_REMOVE_HEAD(&cli_buffer->line_cmds, cmd_next);
  927: 			e_free(c);
  928: 		}
  929: 		while ((h = TAILQ_FIRST(&cli_buffer->line_history))) {
  930: 			TAILQ_REMOVE(&cli_buffer->line_history, h, hist_next);
  931: 			e_free(h);
  932: 		}
  933: 
  934: 		if (cli_buffer->line_prompt)
  935: 			e_free(cli_buffer->line_prompt);
  936: 
  937: 		if (cli_buffer->line_keys)
  938: 			e_free(cli_buffer->line_keys);
  939: 		if (cli_buffer->line_buf)
  940: 			e_free(cli_buffer->line_buf);
  941: 
  942: 		e_free(cli_buffer);
  943: 		cli_buffer = NULL;
  944: 	} else
  945: 		cli_SetErr(EINVAL, "Invalid input parameters ...");
  946: }
  947: 
  948: /*
  949:  * cliInit() - Start CLI session, allocate memory for resources and bind keys
  950:  *
  951:  * @fin = Input device handle
  952:  * @fout = Output device handle
  953:  * @prompt = text for prompt, if NULL disable prompt
  954:  * return: NULL if error or !=NULL CLI buffer
  955: */
  956: linebuffer_t *
  957: cliInit(int fin, int fout, const char *prompt)
  958: {
  959: 	linebuffer_t *cli_buffer;
  960: 	bindkey_t *keys;
  961: 	register int i;
  962: 
  963: 	/* init buffer */
  964: 	cli_buffer = e_malloc(sizeof(linebuffer_t));
  965: 	if (!cli_buffer) {
  966: 		LOGERR;
  967: 		return NULL;
  968: 	} else {
  969: 		memset(cli_buffer, 0, sizeof(linebuffer_t));
  970: 
  971: 		cli_buffer->line_in = fin;
  972: 		cli_buffer->line_out = fout;
  973: 
  974: 		TAILQ_INIT(&cli_buffer->line_history);
  975: 		SLIST_INIT(&cli_buffer->line_cmds);
  976: 
  977: 		if (prompt) {
  978: 			cli_buffer->line_prompt = e_strdup(prompt);
  979: 			if (!cli_buffer->line_prompt) {
  980: 				LOGERR;
  981: 				e_free(cli_buffer);
  982: 				return NULL;
  983: 			} else
  984: 				cli_buffer->line_eol = cli_buffer->line_bol = 
  985: 					strlen(cli_buffer->line_prompt);
  986: 		} else
  987: 			cli_buffer->line_mode = LINEMODE_OVER;
  988: 	}
  989: 	cli_buffer->line_buf = e_malloc(BUFSIZ);
  990: 	if (!cli_buffer->line_buf) {
  991: 		LOGERR;
  992: 		if (cli_buffer->line_prompt)
  993: 			e_free(cli_buffer->line_prompt);
  994: 		e_free(cli_buffer);
  995: 		return NULL;
  996: 	} else {
  997: 		memset(cli_buffer->line_buf, 0, BUFSIZ);
  998: 		cli_buffer->line_len = 1 + cli_buffer->line_eol;
  999: 	}
 1000: 	keys = e_calloc(MAX_BINDKEY + 1, sizeof(bindkey_t));
 1001: 	if (!keys) {
 1002: 		LOGERR;
 1003: 		if (cli_buffer->line_prompt)
 1004: 			e_free(cli_buffer->line_prompt);
 1005: 		e_free(cli_buffer->line_buf);
 1006: 		e_free(cli_buffer);
 1007: 		return NULL;
 1008: 	} else
 1009: 		memset(keys, 0, sizeof(bindkey_t) * (MAX_BINDKEY + 1));
 1010: 
 1011: 	/* add helper functions */
 1012: 	cli_addCommand(cli_buffer, "exit", 0, cli_Cmd_Exit, "exit <cr>", "Exit from console");
 1013: 	cli_addCommand(cli_buffer, "help", 0, cli_Cmd_Help, "help [command] <cr>", "Help screen");
 1014: 	cli_addCommand(cli_buffer, "-------", 0, NULL, "-------------------------", NULL);
 1015: 
 1016: 	/* fill key bindings */
 1017: 	/* ascii chars & ctrl+chars */
 1018: 	for (i = 0; i < 256; i++) {
 1019: 		*keys[i].key_ch = (u_char) i;
 1020: 		keys[i].key_len = 1;
 1021: 
 1022: 		if (!i || i == *K_CTRL_D)
 1023: 			keys[i].key_func = bufEOF;
 1024: 		if (i == *K_CTRL_M || i == *K_CTRL_J)
 1025: 			keys[i].key_func = bufEOL;
 1026: 		if (cli_buffer->line_prompt && (i == *K_CTRL_H || i == *K_BACKSPACE))
 1027: 			keys[i].key_func = bufBS;
 1028: 		if (cli_buffer->line_prompt && i == *K_CTRL_C)
 1029: 			keys[i].key_func = bufCLR;
 1030: 		if (cli_buffer->line_prompt && i == *K_CTRL_A)
 1031: 			keys[i].key_func = bufBEGIN;
 1032: 		if (cli_buffer->line_prompt && i == *K_CTRL_E)
 1033: 			keys[i].key_func = bufEND;
 1034: 		if (cli_buffer->line_prompt && i == *K_TAB)
 1035: 			keys[i].key_func = bufComp;
 1036: 		if (i >= *K_SPACE && i < *K_BACKSPACE)
 1037: 			keys[i].key_func = bufCHAR;
 1038: 		if (i > *K_BACKSPACE && i < 0xff)
 1039: 			keys[i].key_func = bufCHAR;
 1040: 		if (cli_buffer->line_prompt && i == '?')
 1041: 			keys[i].key_func = bufHelp;
 1042: 	}
 1043: 	/* alt+chars */
 1044: 	for (i = 256; i < 512; i++) {
 1045: 		keys[i].key_ch[0] = 0x1b;
 1046: 		keys[i].key_ch[1] = (u_char) i - 256;
 1047: 		keys[i].key_len = 2;
 1048: 	}
 1049: 
 1050: 	/* 3 bytes */
 1051: 	keys[i].key_len = sizeof K_F1 - 1;
 1052: 	memcpy(keys[i].key_ch, K_F1, keys[i].key_len);
 1053: 	i++;
 1054: 	keys[i].key_len = sizeof K_F2 - 1;
 1055: 	memcpy(keys[i].key_ch, K_F2, keys[i].key_len);
 1056: 	i++;
 1057: 	keys[i].key_len = sizeof K_F3 - 1;
 1058: 	memcpy(keys[i].key_ch, K_F3, keys[i].key_len);
 1059: 	i++;
 1060: 	keys[i].key_len = sizeof K_F4 - 1;
 1061: 	memcpy(keys[i].key_ch, K_F4, keys[i].key_len);
 1062: 	i++;
 1063: 	keys[i].key_len = sizeof K_CTRL_SH_F1 - 1;
 1064: 	memcpy(keys[i].key_ch, K_CTRL_SH_F1, keys[i].key_len);
 1065: 	i++;
 1066: 	keys[i].key_len = sizeof K_CTRL_SH_F2 - 1;
 1067: 	memcpy(keys[i].key_ch, K_CTRL_SH_F2, keys[i].key_len);
 1068: 	i++;
 1069: 	keys[i].key_len = sizeof K_CTRL_SH_F3 - 1;
 1070: 	memcpy(keys[i].key_ch, K_CTRL_SH_F3, keys[i].key_len);
 1071: 	i++;
 1072: 	keys[i].key_len = sizeof K_CTRL_SH_F4 - 1;
 1073: 	memcpy(keys[i].key_ch, K_CTRL_SH_F4, keys[i].key_len);
 1074: 	i++;
 1075: 	keys[i].key_len = sizeof K_CTRL_SH_F5 - 1;
 1076: 	memcpy(keys[i].key_ch, K_CTRL_SH_F5, keys[i].key_len);
 1077: 	i++;
 1078: 	keys[i].key_len = sizeof K_CTRL_SH_F6 - 1;
 1079: 	memcpy(keys[i].key_ch, K_CTRL_SH_F6, keys[i].key_len);
 1080: 	i++;
 1081: 	keys[i].key_len = sizeof K_CTRL_SH_F7 - 1;
 1082: 	memcpy(keys[i].key_ch, K_CTRL_SH_F7, keys[i].key_len);
 1083: 	i++;
 1084: 	keys[i].key_len = sizeof K_CTRL_SH_F8 - 1;
 1085: 	memcpy(keys[i].key_ch, K_CTRL_SH_F8, keys[i].key_len);
 1086: 	i++;
 1087: 	keys[i].key_len = sizeof K_CTRL_SH_F9 - 1;
 1088: 	memcpy(keys[i].key_ch, K_CTRL_SH_F9, keys[i].key_len);
 1089: 	i++;
 1090: 	keys[i].key_len = sizeof K_CTRL_SH_F10 - 1;
 1091: 	memcpy(keys[i].key_ch, K_CTRL_SH_F10, keys[i].key_len);
 1092: 	i++;
 1093: 	keys[i].key_len = sizeof K_CTRL_SH_F11 - 1;
 1094: 	memcpy(keys[i].key_ch, K_CTRL_SH_F11, keys[i].key_len);
 1095: 	i++;
 1096: 	keys[i].key_len = sizeof K_CTRL_SH_F12 - 1;
 1097: 	memcpy(keys[i].key_ch, K_CTRL_SH_F12, keys[i].key_len);
 1098: 	i++;
 1099: 	keys[i].key_len = sizeof K_CTRL_F1 - 1;
 1100: 	memcpy(keys[i].key_ch, K_CTRL_F1, keys[i].key_len);
 1101: 	i++;
 1102: 	keys[i].key_len = sizeof K_CTRL_F2 - 1;
 1103: 	memcpy(keys[i].key_ch, K_CTRL_F2, keys[i].key_len);
 1104: 	i++;
 1105: 	keys[i].key_len = sizeof K_CTRL_F3 - 1;
 1106: 	memcpy(keys[i].key_ch, K_CTRL_F3, keys[i].key_len);
 1107: 	i++;
 1108: 	keys[i].key_len = sizeof K_CTRL_F4 - 1;
 1109: 	memcpy(keys[i].key_ch, K_CTRL_F4, keys[i].key_len);
 1110: 	i++;
 1111: 	keys[i].key_len = sizeof K_CTRL_F5 - 1;
 1112: 	memcpy(keys[i].key_ch, K_CTRL_F5, keys[i].key_len);
 1113: 	i++;
 1114: 	keys[i].key_len = sizeof K_CTRL_F6 - 1;
 1115: 	memcpy(keys[i].key_ch, K_CTRL_F6, keys[i].key_len);
 1116: 	i++;
 1117: 	keys[i].key_len = sizeof K_CTRL_F7 - 1;
 1118: 	memcpy(keys[i].key_ch, K_CTRL_F7, keys[i].key_len);
 1119: 	i++;
 1120: 	keys[i].key_len = sizeof K_CTRL_F8 - 1;
 1121: 	memcpy(keys[i].key_ch, K_CTRL_F8, keys[i].key_len);
 1122: 	i++;
 1123: 	keys[i].key_len = sizeof K_CTRL_F9 - 1;
 1124: 	memcpy(keys[i].key_ch, K_CTRL_F9, keys[i].key_len);
 1125: 	i++;
 1126: 	keys[i].key_len = sizeof K_CTRL_F10 - 1;
 1127: 	memcpy(keys[i].key_ch, K_CTRL_F10, keys[i].key_len);
 1128: 	i++;
 1129: 	keys[i].key_len = sizeof K_CTRL_F11 - 1;
 1130: 	memcpy(keys[i].key_ch, K_CTRL_F11, keys[i].key_len);
 1131: 	i++;
 1132: 	keys[i].key_len = sizeof K_CTRL_F12 - 1;
 1133: 	memcpy(keys[i].key_ch, K_CTRL_F12, keys[i].key_len);
 1134: 	i++;
 1135: 	keys[i].key_len = sizeof K_HOME - 1;
 1136: 	if (cli_buffer->line_prompt)
 1137: 		keys[i].key_func = bufBEGIN;
 1138: 	memcpy(keys[i].key_ch, K_HOME, keys[i].key_len);
 1139: 	i++;
 1140: 	keys[i].key_len = sizeof K_END - 1;
 1141: 	if (cli_buffer->line_prompt)
 1142: 		keys[i].key_func = bufEND;
 1143: 	memcpy(keys[i].key_ch, K_END, keys[i].key_len);
 1144: 	i++;
 1145: 	keys[i].key_len = sizeof K_UP - 1;
 1146: 	if (cli_buffer->line_prompt)
 1147: 		keys[i].key_func = bufUP;
 1148: 	memcpy(keys[i].key_ch, K_UP, keys[i].key_len);
 1149: 	i++;
 1150: 	keys[i].key_len = sizeof K_DOWN - 1;
 1151: 	if (cli_buffer->line_prompt)
 1152: 		keys[i].key_func = bufDOWN;
 1153: 	memcpy(keys[i].key_ch, K_DOWN, keys[i].key_len);
 1154: 	i++;
 1155: 	keys[i].key_len = sizeof K_RIGHT - 1;
 1156: 	if (cli_buffer->line_prompt)
 1157: 		keys[i].key_func = bufRIGHT;
 1158: 	memcpy(keys[i].key_ch, K_RIGHT, keys[i].key_len);
 1159: 	i++;
 1160: 	keys[i].key_len = sizeof K_LEFT - 1;
 1161: 	if (cli_buffer->line_prompt)
 1162: 		keys[i].key_func = bufLEFT;
 1163: 	memcpy(keys[i].key_ch, K_LEFT, keys[i].key_len);
 1164: 	i++;
 1165: 	keys[i].key_len = sizeof K_BTAB - 1;
 1166: 	if (cli_buffer->line_prompt)
 1167: 		keys[i].key_func = bufBTAB;
 1168: 	memcpy(keys[i].key_ch, K_BTAB, keys[i].key_len);
 1169: 	i++;
 1170: 	/* 4 bytes */
 1171: 	keys[i].key_len = sizeof K_INS - 1;
 1172: 	if (cli_buffer->line_prompt)
 1173: 		keys[i].key_func = bufMODE;
 1174: 	memcpy(keys[i].key_ch, K_INS, keys[i].key_len);
 1175: 	i++;
 1176: 	keys[i].key_len = sizeof K_DEL - 1;
 1177: 	if (cli_buffer->line_prompt)
 1178: 		keys[i].key_func = bufDEL;
 1179: 	memcpy(keys[i].key_ch, K_DEL, keys[i].key_len);
 1180: 	i++;
 1181: 	keys[i].key_len = sizeof K_PGUP - 1;
 1182: 	memcpy(keys[i].key_ch, K_PGUP, keys[i].key_len);
 1183: 	i++;
 1184: 	keys[i].key_len = sizeof K_PGDN - 1;
 1185: 	memcpy(keys[i].key_ch, K_PGDN, keys[i].key_len);
 1186: 	i++;
 1187: 	/* 5 bytes */
 1188: 	keys[i].key_len = sizeof K_F5 - 1;
 1189: 	memcpy(keys[i].key_ch, K_F5, keys[i].key_len);
 1190: 	i++;
 1191: 	keys[i].key_len = sizeof K_F6 - 1;
 1192: 	memcpy(keys[i].key_ch, K_F6, keys[i].key_len);
 1193: 	i++;
 1194: 	keys[i].key_len = sizeof K_F7 - 1;
 1195: 	memcpy(keys[i].key_ch, K_F7, keys[i].key_len);
 1196: 	i++;
 1197: 	keys[i].key_len = sizeof K_F8 - 1;
 1198: 	memcpy(keys[i].key_ch, K_F8, keys[i].key_len);
 1199: 	i++;
 1200: 	keys[i].key_len = sizeof K_F9 - 1;
 1201: 	memcpy(keys[i].key_ch, K_F9, keys[i].key_len);
 1202: 	i++;
 1203: 	keys[i].key_len = sizeof K_F10 - 1;
 1204: 	memcpy(keys[i].key_ch, K_F10, keys[i].key_len);
 1205: 	i++;
 1206: 	keys[i].key_len = sizeof K_F11 - 1;
 1207: 	memcpy(keys[i].key_ch, K_F11, keys[i].key_len);
 1208: 	i++;
 1209: 	keys[i].key_len = sizeof K_F12 - 1;
 1210: 	memcpy(keys[i].key_ch, K_F12, keys[i].key_len);
 1211: 	i++;
 1212: 
 1213: 	cli_buffer->line_keys = keys;
 1214: 	return cli_buffer;
 1215: }
 1216: 
 1217: /*
 1218:  * cliInitLine() - Init CLI input line terminal
 1219:  *
 1220:  * @cli_buffer = CLI buffer
 1221:  * return: none
 1222: */
 1223: int
 1224: cliInitLine(linebuffer_t * __restrict cli_buffer)
 1225: {
 1226: 	struct termios t;
 1227: 
 1228: 	memset(&t, 0, sizeof t);
 1229: 	tcgetattr(cli_buffer->line_in, &t);
 1230: 	t.c_lflag &= ~(ICANON | ISIG | IEXTEN | ECHO | 
 1231: 			ECHOCTL | ECHOE | ECHOK | ECHOKE | ECHONL | ECHOPRT);
 1232: 	t.c_iflag |= IGNBRK;
 1233: 	t.c_cc[VMIN] = 1;
 1234: 	t.c_cc[VTIME] = 0;
 1235: 	return tcsetattr(cli_buffer->line_in, TCSANOW, &t);
 1236: }
 1237: 
 1238: /*
 1239:  * cliReadLine() - Read line from opened CLI session
 1240:  *
 1241:  * @cli_buffer = CLI buffer
 1242:  * return: NULL if error or !=NULL readed line, must be e_free after use!
 1243: */
 1244: char *
 1245: cliReadLine(linebuffer_t * __restrict cli_buffer)
 1246: {
 1247: 	int code, readLen;
 1248: 	register int i;
 1249: 	struct pollfd fds;
 1250: 	char buf[BUFSIZ], *str = NULL;
 1251: 
 1252: 	if (!cli_buffer) {
 1253: 		cli_SetErr(EINVAL, "Invalid input parameters ...");
 1254: 		return NULL;
 1255: 	}
 1256: 
 1257: 	memset(&fds, 0, sizeof fds);
 1258: 	fds.fd = cli_buffer->line_in;
 1259: 	fds.events = POLLIN;
 1260: 
 1261: 	printfCR(cli_buffer, 1);
 1262: 	while (42) {
 1263: 		if (poll(&fds, 1, -1) < 1) {
 1264: 			LOGERR;
 1265: 			return str;
 1266: 		}
 1267: 
 1268: 		memset(buf, 0, sizeof buf);
 1269: 		readLen = read(cli_buffer->line_in, buf, BUFSIZ);
 1270: 		if (readLen == -1) {
 1271: 			LOGERR;
 1272: 			return str;
 1273: 		}
 1274: 		if (!readLen) {
 1275: 			if (cli_buffer->line_buf)
 1276: 				str = e_strdup(cli_buffer->line_buf);
 1277: 			else
 1278: 				cli_SetErr(EPIPE, "Unknown state ...");
 1279: 			return str;
 1280: 		}
 1281: 
 1282: recheck:
 1283: 		for (code = RETCODE_OK, i = MAX_BINDKEY - 1; i > -1; i--)
 1284: 			if (readLen >= cli_buffer->line_keys[i].key_len && 
 1285: 					!memcmp(cli_buffer->line_keys[i].key_ch, buf, 
 1286: 						cli_buffer->line_keys[i].key_len)) {
 1287: 				readLen -= cli_buffer->line_keys[i].key_len;
 1288: 				if (readLen)
 1289: 					memmove(buf, buf + cli_buffer->line_keys[i].key_len, readLen);
 1290: 				else
 1291: 					memset(buf, 0, cli_buffer->line_keys[i].key_len);
 1292: 
 1293: 				if (cli_buffer->line_keys[i].key_func)
 1294: 					if ((code = cli_buffer->line_keys[i].key_func(i, cli_buffer)))
 1295: 						readLen = 0;
 1296: 
 1297: 				if (readLen)
 1298: 					goto recheck;
 1299: 				else
 1300: 					break;
 1301: 			}
 1302: 
 1303: 		if (code)
 1304: 			break;
 1305: 	}
 1306: 
 1307: 	if (code != RETCODE_ERR && code != RETCODE_EOF && cli_buffer->line_buf)
 1308: 		str = e_strdup(cli_buffer->line_buf);
 1309: 	return str;
 1310: }
 1311: 
 1312: 
 1313: /*
 1314:  * cliNetLoop() - CLI network main loop binded to socket
 1315:  *
 1316:  * @cli_buffer = CLI buffer
 1317:  * @csHistFile = History file name
 1318:  * @sock = client socket
 1319:  * return: RETCODE_ERR error, RETCODE_OK ok
 1320: */
 1321: int
 1322: cliNetLoop(linebuffer_t * __restrict cli_buffer, const char *csHistFile, int sock)
 1323: {
 1324: 	u_char buf[BUFSIZ];
 1325: 	int pid, stat, pty, r, s, alen, flg, attrlen = 0, ret = 0;
 1326: 	fd_set fds;
 1327: 	struct timeval tv = { DEFAULT_SOCK_TIMEOUT, 0 };
 1328: 	struct telnetAttrs *a, Attr[10];
 1329: 
 1330: 	switch ((pid = forkpty(&pty, NULL, NULL, NULL))) {
 1331: 		case -1:
 1332: 			LOGERR;
 1333: 			return -1;
 1334: 		case 0:
 1335: 			if (!cli_buffer) {
 1336: 				cli_SetErr(EINVAL, "Invalid input parameters ...");
 1337: 				return -1;
 1338: 			} else
 1339: 				close(sock);
 1340: 
 1341: 			ret = cliLoop(cli_buffer, csHistFile) < 0 ? 1 : 0;
 1342: 			cliEnd(cli_buffer);
 1343: 
 1344: 			_exit(ret);
 1345: 		default:
 1346: 			cli_telnet_SetCmd(Attr + 0, DO, TELOPT_TTYPE);
 1347: 			cli_telnet_SetCmd(Attr + 1, WILL, TELOPT_ECHO);
 1348: 			cli_telnet_Set_SubOpt(Attr + 2, TELOPT_LFLOW, LFLOW_OFF, NULL, 0);
 1349: 			cli_telnet_Set_SubOpt(Attr + 3, TELOPT_LFLOW, LFLOW_RESTART_XON, NULL, 0);
 1350: 			cli_telnet_SetCmd(Attr + 4, DO, TELOPT_LINEMODE);
 1351: 			if ((ret = cli_telnetSend(sock, Attr, 5, NULL, 0, 0)) == -1)
 1352: 				return -1;
 1353: 			else
 1354: 				flg = 0;
 1355: 
 1356: 			while (42) {
 1357: 				if (waitpid(pid, &stat, WNOHANG))
 1358: 					break;
 1359: 
 1360: 				FD_ZERO(&fds);
 1361: 				FD_SET(sock, &fds);
 1362: 				FD_SET(pty, &fds);
 1363: 				if ((ret = select(FD_SETSIZE, &fds, NULL, NULL, &tv)) < 1) {
 1364: 					if (!ret)
 1365: 						cli_SetErr(ETIMEDOUT, "Client session timeout ...");
 1366: 
 1367: 					break;
 1368: 				}
 1369: 
 1370: 				r = FD_ISSET(sock, &fds) ? sock : pty;
 1371: 				s = FD_ISSET(sock, &fds) ? pty : sock;
 1372: 
 1373: 				if (FD_ISSET(sock, &fds)) {
 1374: 					memset(buf, 0, BUFSIZ);
 1375: 					if ((ret = cli_telnetRecv(sock, &a, &alen, buf, BUFSIZ)) < 0) {
 1376: 						if (a)
 1377: 							e_free(a);
 1378: 
 1379: 						if (-2 == ret)
 1380: 							continue;
 1381: 						// EOF
 1382: 						if (-3 == ret)
 1383: 							shutdown(sock, SHUT_RD);
 1384: 						break;
 1385: 					}
 1386: 					attrlen = 0;
 1387: 					if (1 == flg && alen) {
 1388: 						cli_telnet_SetCmd(&Attr[attrlen++], DONT, TELOPT_SGA);
 1389: 						cli_telnet_SetCmd(&Attr[attrlen++], DO, TELOPT_ECHO);
 1390: 					}
 1391: 					if (2 == flg && alen) {
 1392: 						cli_telnet_SetCmd(&Attr[attrlen++], WILL, TELOPT_ECHO);
 1393: 						cli_telnet_Set_SubOpt(&Attr[attrlen++], TELOPT_LFLOW, 
 1394: 								LFLOW_OFF, NULL, 0);
 1395: 						cli_telnet_Set_SubOpt(&Attr[attrlen++], TELOPT_LFLOW, 
 1396: 								LFLOW_RESTART_XON, NULL, 0);
 1397: 						cli_telnet_SetCmd(&Attr[attrlen++], DONT, TELOPT_LINEMODE);
 1398: 					}
 1399: 					if (a)
 1400: 						e_free(a);
 1401: 
 1402: 					if ((ret = write(pty, buf, ret)) == -1) {
 1403: 						LOGERR;
 1404: 						break;
 1405: 					}
 1406: 				}
 1407: 
 1408: 				if (FD_ISSET(pty, &fds)) {
 1409: 					memset(buf, 0, BUFSIZ);
 1410: 					if ((ret = read(pty, buf, BUFSIZ)) == -1) {
 1411: 						LOGERR;
 1412: 						break;
 1413: 					}
 1414: 
 1415: 					if ((ret = cli_telnetSend(sock, Attr, pty == s ? 0 : attrlen, 
 1416: 									buf, ret, 0)) == -1)
 1417: 						break;
 1418: 					else
 1419: 						flg++;
 1420: 				}
 1421: 			}
 1422: 
 1423: 			close(pty);
 1424: 	}
 1425: 
 1426: 	return ret;
 1427: }
 1428: 
 1429: /*
 1430:  * cliLoop() - CLI main loop
 1431:  *
 1432:  * @cli_buffer = CLI buffer
 1433:  * @csHistFile = History file name
 1434:  * return: RETCODE_ERR error, RETCODE_OK ok
 1435: */
 1436: int
 1437: cliLoop(linebuffer_t * __restrict cli_buffer, const char *csHistFile)
 1438: {
 1439: 	char *line, *s, *t, **app, *items[MAX_PROMPT_ITEMS];
 1440: 	register int i;
 1441: 	int ret = RETCODE_OK;
 1442: 	struct tagCommand *cmd;
 1443: 
 1444: 	/* --- main body of CLI --- */
 1445: 	cliInitLine(cli_buffer);
 1446: 
 1447: 	if (cli_loadHistory(cli_buffer, csHistFile) == RETCODE_ERR)
 1448: 		return RETCODE_ERR;
 1449: 
 1450: 	do {
 1451: 		line = cliReadLine(cli_buffer);
 1452: 		if (!line) {
 1453: 			printfNL(cli_buffer, 0);
 1454: 			break;
 1455: 		} else
 1456: 			cli_addHistory(cli_buffer, NULL);
 1457: 		// clear whitespaces
 1458: 		for (s = line; isspace((int) *s); s++);
 1459: 		if (*s) {
 1460: 			for (t = s + strlen(s) - 1; t > s && isspace((int) *t); t--);
 1461: 			*++t = 0;
 1462: 		}
 1463: 
 1464: 		if (*s) {
 1465: 			memset(items, 0, sizeof(char*) * MAX_PROMPT_ITEMS);
 1466: 			for (app = items; app < items + MAX_PROMPT_ITEMS - 1 && 
 1467: 					(*app = strsep(&s, " \t")); *app ? app++ : app);
 1468: 
 1469: 			// exec_cmd ...
 1470: 			i = 0;
 1471: 			SLIST_FOREACH(cmd, &cli_buffer->line_cmds, cmd_next) {
 1472: 				if (*items[0] && !strncmp(cmd->cmd_name, items[0], strlen(items[0])))
 1473: 					break;
 1474: 				else
 1475: 					i++;
 1476: 			}
 1477: 
 1478: 			if (!cmd) {
 1479: 				cli_Printf(cli_buffer, "\nCommand '%s' not found!\n", items[0]);
 1480: 				ret = -1;
 1481: 			} else
 1482: 				if (cmd->cmd_func) {
 1483: 					cli_Printf(cli_buffer, "\n");
 1484: 					ret = cmd->cmd_func(cli_buffer, i, items);
 1485: 				} else {
 1486: 					clrscrEOL(cli_buffer);
 1487: 					printfCR(cli_buffer, 1);
 1488: 				}
 1489: 		}
 1490: 
 1491: 		cli_freeLine(cli_buffer);
 1492: 		cli_resetHistory(cli_buffer);
 1493: 		e_free(line);
 1494: 	} while (ret < 1);
 1495: 
 1496: 	cli_saveHistory(cli_buffer, csHistFile, HISTORY_LINES);
 1497: 	return ret;
 1498: }

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