File:  [ELWIX - Embedded LightWeight unIX -] / libaitcli / src / aitcli.c
Revision 1.16: download - view: text, annotated - select for diffs - revision graph
Mon Feb 4 21:22:31 2019 UTC (5 years, 4 months ago) by misho
Branches: MAIN
CVS tags: cli4_2, HEAD, CLI4_1
ver 4.1

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

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