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