Annotation of libaitcli/src/aitcli.c, revision 1.5.4.2
1.1 misho 1: /*************************************************************************
2: * (C) 2010 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
3: * by Michael Pounov <misho@openbsd-bg.org>
4: *
5: * $Author: misho $
1.5.4.2 ! misho 6: * $Id: aitcli.c,v 1.5.4.1 2013/01/17 16:04:31 misho Exp $
1.1 misho 7: *
1.4 misho 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:
1.5.4.1 misho 15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
1.4 misho 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: */
1.1 misho 46: #include "global.h"
1.3 misho 47: #include "cli.h"
1.1 misho 48:
49:
50: #pragma GCC visibility push(hidden)
51:
52: int cli_Errno;
53: char cli_Error[STRSIZ];
54:
1.3 misho 55: #pragma GCC visibility pop
56:
57: // cli_GetErrno() Get error code of last operation
58: inline int
59: cli_GetErrno()
60: {
61: return cli_Errno;
62: }
63:
1.5.4.1 misho 64: // cli_GetError() Get error text of last operation
1.3 misho 65: inline const char *
66: cli_GetError()
67: {
68: return cli_Error;
69: }
70:
71: // cli_SetErr() Set error to variables for internal use!!!
72: inline void
73: cli_SetErr(int eno, char *estr, ...)
74: {
75: va_list lst;
76:
77: cli_Errno = eno;
1.5.4.1 misho 78: memset(cli_Error, 0, sizeof cli_Error);
1.3 misho 79: va_start(lst, estr);
1.5.4.1 misho 80: vsnprintf(cli_Error, sizeof cli_Error, estr, lst);
1.3 misho 81: va_end(lst);
82: }
1.2 misho 83:
1.3 misho 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);
1.1 misho 93:
1.3 misho 94: for (i = 0; i < buf->line_len; i++)
95: write(buf->line_out, K_SPACE, 1);
96: }
97: }
1.1 misho 98:
1.3 misho 99: static inline void
100: printfEOL(linebuffer_t * __restrict buf, int len, int prompt)
1.2 misho 101: {
1.3 misho 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: }
1.2 misho 110: }
111:
1.3 misho 112: static inline void
113: printfCR(linebuffer_t * __restrict buf, int prompt)
1.2 misho 114: {
1.3 misho 115: if (buf) {
116: write(buf->line_out, K_CR, 1);
1.2 misho 117:
1.3 misho 118: if (prompt)
119: if (prompt && buf->line_prompt)
120: write(buf->line_out, buf->line_prompt, buf->line_bol);
1.2 misho 121: }
1.3 misho 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 buffer)
140: {
141: linebuffer_t *buf = buffer;
142: int pos;
143:
144: if (!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);
1.2 misho 165: }
1.3 misho 166: return RETCODE_OK;
167: }
168:
169: static int
170: bufEOL(int idx, void * __restrict buffer)
171: {
172: if (!buffer || idx < 0 || idx > MAX_BINDKEY)
173: return RETCODE_ERR;
174:
175: printfCR(buffer, 1);
176: return RETCODE_EOL;
177: }
178:
179: static int
180: bufEOF(int idx, void * __restrict buffer)
181: {
182: /*
183: if (!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 buffer)
192: {
193: linebuffer_t *buf = buffer;
194: int pos;
195:
196: if (!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 buffer)
223: {
224: linebuffer_t *buf = buffer;
225: int pos;
226:
227: if (!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 buffer)
254: {
255: if (!buffer || idx < 0 || idx > MAX_BINDKEY)
256: return RETCODE_ERR;
257:
258: clrscrEOL(buffer);
259: cli_freeLine(buffer);
260:
261: printfCR(buffer, 1);
262: return RETCODE_OK;
263: }
264:
265: static int
266: bufBS(int idx, void * __restrict buffer)
267: {
268: linebuffer_t *buf = buffer;
269:
270: if (!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 buffer)
292: {
293: linebuffer_t *buf = buffer;
294:
295: if (!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);
1.2 misho 305: }
306:
1.3 misho 307: return RETCODE_OK;
308: }
309:
310: static int
311: bufMODE(int idx, void * __restrict buffer)
312: {
313: linebuffer_t *buf = buffer;
314:
315: if (!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 buffer)
324: {
325: linebuffer_t *buf = buffer;
326:
327: if (!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 buffer)
338: {
339: linebuffer_t *buf = buffer;
340:
341: if (!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;
1.2 misho 348: }
349:
1.3 misho 350: static int
351: bufLEFT(int idx, void * __restrict buffer)
1.2 misho 352: {
1.3 misho 353: linebuffer_t *buf = buffer;
354:
355: if (!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: }
1.2 misho 363:
1.3 misho 364: static int
365: bufRIGHT(int idx, void * __restrict buffer)
366: {
367: linebuffer_t *buf = buffer;
368:
369: if (!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 buffer)
380: {
381: linebuffer_t *buf = buffer;
382:
383: if (!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 buffer)
402: {
403: linebuffer_t *buf = 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 (!buffer || idx < 0 || idx > MAX_BINDKEY)
410: return RETCODE_ERR;
411:
1.5.4.1 misho 412: str = e_strdup(buf->line_buf);
1.3 misho 413: if (!str)
414: return RETCODE_ERR;
1.2 misho 415: else {
1.3 misho 416: s = str;
1.5.4.1 misho 417: str_Trim(s);
1.3 misho 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: }
1.2 misho 440:
1.3 misho 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;
1.2 misho 450: }
1.3 misho 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: }
1.2 misho 463:
1.3 misho 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);
1.2 misho 471: }
1.3 misho 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;
1.2 misho 484:
1.3 misho 485: printfEOL(buf, -1, 1);
1.2 misho 486: }
487:
1.3 misho 488: endcomp:
1.5.4.1 misho 489: e_free(str);
1.3 misho 490: return ret;
491: }
492:
493: static int
494: bufHelp(int idx, void * __restrict buffer)
495: {
496: linebuffer_t *buf = buffer;
497:
498: if (!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: /*
1.5.4.1 misho 510: * cli_Printf() - Send message to CLI session
511: *
1.3 misho 512: * @buffer = CLI buffer
513: * @fmt = printf format string
514: * @... = arguments defined in fmt
515: * return: none
516: */
517: inline void
518: cli_Printf(linebuffer_t * __restrict buffer, char *fmt, ...)
519: {
520: va_list lst;
521: FILE *f;
522:
523: if (fmt) {
524: f = fdopen(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
1.5.4.2 ! misho 534: cli_SetErr(EINVAL, "Invalid input parameters ...");
1.3 misho 535: }
536:
537: /*
1.5.4.1 misho 538: * cli_PrintHelp() - Print help screen
539: *
1.3 misho 540: * @buffer = CLI buffer
541: * return: none
542: */
543: inline void
544: cli_PrintHelp(linebuffer_t * __restrict buffer)
545: {
546: if (buffer) {
547: bufHelp(0, buffer);
548: clrscrEOL(buffer);
549: } else
1.5.4.2 ! misho 550: cli_SetErr(EINVAL, "Invalid input parameters ...");
1.3 misho 551: }
552:
553:
554: /*
1.5.4.1 misho 555: * cli_BindKey() - Bind function to key
556: *
1.3 misho 557: * @key = key structure
558: * @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 buffer)
563: {
564: register int i;
565:
566: if (!key || !buffer) {
1.5.4.2 ! misho 567: cli_SetErr(EINVAL, "Invalid input parameters ...");
1.3 misho 568: return RETCODE_ERR;
1.2 misho 569: }
1.3 misho 570:
571: for (i = 0; i < MAX_BINDKEY; i++)
572: if (key->key_len == buffer->line_keys[i].key_len &&
573: !memcmp(key->key_ch, buffer->line_keys[i].key_ch, key->key_len)) {
574: buffer->line_keys[i].key_func = key->key_func;
575: return i;
576: }
577:
578: return RETCODE_OK;
1.2 misho 579: }
580:
581:
1.3 misho 582: /*
1.5.4.1 misho 583: * cli_addCommand() - Add command to CLI session
584: *
1.3 misho 585: * @buffer = CLI buffer
586: * @csCmd = Command name
587: * @cliLevel = Level in CLI, -1 unprivi(view from all), 0 main config, 1 sub config ...
588: * @funcCmd = Callback function when user call command
589: * @csInfo = Inline information for command
590: * @csHelp = Help line when call help
591: * return: RETCODE_ERR error, RETCODE_OK ok
592: */
593: int
594: cli_addCommand(linebuffer_t * __restrict buffer, const char *csCmd, int cliLevel, cmd_func_t funcCmd,
595: const char *csInfo, const char *csHelp)
1.1 misho 596: {
1.3 misho 597: struct tagCommand *cmd;
598:
599: if (!buffer || !csCmd) {
1.5.4.2 ! misho 600: cli_SetErr(EINVAL, "Invalid input parameters ...");
1.3 misho 601: return RETCODE_ERR;
602: }
603:
1.5.4.1 misho 604: cmd = e_malloc(sizeof(struct tagCommand));
1.3 misho 605: if (!cmd) {
606: LOGERR;
607: return RETCODE_ERR;
608: } else
609: memset(cmd, 0, sizeof(struct tagCommand));
610:
611: cmd->cmd_level = cliLevel;
612: cmd->cmd_func = funcCmd;
613: cmd->cmd_len = strlcpy(cmd->cmd_name, csCmd, STRSIZ);
614: if (csInfo)
615: strlcpy(cmd->cmd_info, csInfo, STRSIZ);
616: if (csHelp)
617: strlcpy(cmd->cmd_help, csHelp, STRSIZ);
618: SLIST_INSERT_HEAD(&buffer->line_cmds, cmd, cmd_next);
619: return RETCODE_OK;
1.1 misho 620: }
621:
1.3 misho 622: /*
1.5.4.1 misho 623: * cli_delCommand() - Delete command from CLI session
624: *
1.3 misho 625: * @buffer = CLI buffer
626: * @csCmd = Command name
627: * @cliLevel = Level in CLI, -1 unprivi(view from all), 0 main config, 1 sub config ...
628: * return: RETCODE_ERR error, RETCODE_OK ok
629: */
630: int
631: cli_delCommand(linebuffer_t * __restrict buffer, const char *csCmd, int cliLevel)
1.1 misho 632: {
1.3 misho 633: struct tagCommand *cmd;
634: int ret = RETCODE_OK;
635:
636: if (!buffer || !csCmd) {
1.5.4.2 ! misho 637: cli_SetErr(EINVAL, "Invalid input parameters ...");
1.3 misho 638: return RETCODE_ERR;
639: }
640:
641: SLIST_FOREACH(cmd, &buffer->line_cmds, cmd_next)
642: if (cmd->cmd_level == cliLevel && !strcmp(cmd->cmd_name, csCmd)) {
643: ret = 1;
644: SLIST_REMOVE(&buffer->line_cmds, cmd, tagCommand, cmd_next);
1.5.4.1 misho 645: e_free(cmd);
1.3 misho 646: break;
647: }
648:
649: return ret;
1.1 misho 650: }
651:
1.3 misho 652: /*
1.5.4.1 misho 653: * cli_updCommand() - Update command in CLI session
654: *
1.3 misho 655: * @buffer = CLI buffer
656: * @csCmd = Command name
657: * @cliLevel = Level in CLI, -1 unprivi(view from all), 0 main config, 1 sub config ...
658: * @funcCmd = Callback function when user call command
659: * @csInfo = Inline information for command
660: * @csHelp = Help line when call help
661: * return: RETCODE_ERR error, RETCODE_OK ok
662: */
663: int
664: cli_updCommand(linebuffer_t * __restrict buffer, const char *csCmd, int cliLevel, cmd_func_t funcCmd,
665: const char *csInfo, const char *csHelp)
1.1 misho 666: {
1.3 misho 667: struct tagCommand *cmd;
668: int ret = RETCODE_OK;
669:
670: if (!buffer || !csCmd) {
1.5.4.2 ! misho 671: cli_SetErr(EINVAL, "Invalid input parameters ...");
1.3 misho 672: return RETCODE_ERR;
673: }
674:
675: SLIST_FOREACH(cmd, &buffer->line_cmds, cmd_next)
676: if (cmd->cmd_level == cliLevel && !strcmp(cmd->cmd_name, csCmd)) {
677: ret = 1;
678:
679: if (funcCmd)
680: cmd->cmd_func = funcCmd;
681: if (csInfo)
682: strlcpy(cmd->cmd_info, csInfo, STRSIZ);
683: if (csHelp)
684: strlcpy(cmd->cmd_help, csHelp, STRSIZ);
685:
686: break;
687: }
1.1 misho 688:
1.3 misho 689: return ret;
1.1 misho 690: }
691:
692:
693: /*
1.5.4.1 misho 694: * cli_addHistory() - Add line to history
695: *
1.3 misho 696: * @buffer = CLI buffer
697: * @str = Add custom text or if NULL use readed line from CLI buffer
698: * return: RETCODE_ERR error, RETCODE_OK ok
1.1 misho 699: */
1.3 misho 700: int
701: cli_addHistory(linebuffer_t * __restrict buffer, const char * __restrict str)
1.1 misho 702: {
1.3 misho 703: struct tagHistory *h;
704:
705: if (!buffer) {
1.5.4.2 ! misho 706: cli_SetErr(EINVAL, "Invalid input parameters ...");
1.3 misho 707: return RETCODE_ERR;
708: }
709:
1.5.4.1 misho 710: if (!(h = e_malloc(sizeof(struct tagHistory)))) {
1.3 misho 711: LOGERR;
712: return RETCODE_ERR;
713: } else
714: memset(h, 0, sizeof(struct tagHistory));
715:
716: if (str) {
717: if (!*str) {
1.5.4.1 misho 718: e_free(h);
1.3 misho 719: return RETCODE_OK;
720: }
721:
722: h->hist_len = strlcpy(h->hist_line, str, BUFSIZ);
723: } else {
724: if (!*buffer->line_buf || buffer->line_len < 2) {
1.5.4.1 misho 725: e_free(h);
1.3 misho 726: return RETCODE_OK;
727: }
728:
729: memcpy(h->hist_line, buffer->line_buf, (h->hist_len = buffer->line_len));
1.5.4.1 misho 730: str_Trim(h->hist_line);
1.3 misho 731: h->hist_len = strlen(h->hist_line);
732: }
1.1 misho 733:
1.3 misho 734: TAILQ_INSERT_HEAD(&buffer->line_history, h, hist_next);
735: return h->hist_len;
736: }
1.1 misho 737:
1.3 misho 738: /*
1.5.4.1 misho 739: * cli_saveHistory() - Save history to file
740: *
1.3 misho 741: * @buffer = CLI buffer
742: * @histfile = History filename, if NULL will be use default name
743: * @lines = Maximum history lines to save
744: * return: RETCODE_ERR error, RETCODE_OK ok
745: */
746: int
747: cli_saveHistory(linebuffer_t * __restrict buffer, const char *histfile, int lines)
748: {
749: FILE *f;
750: mode_t mode;
751: char szFName[MAXPATHLEN];
752: struct tagHistory *h;
753:
754: if (!buffer) {
1.5.4.2 ! misho 755: cli_SetErr(EINVAL, "Invalid input parameters ...");
1.3 misho 756: return RETCODE_ERR;
757: }
758: if (!histfile)
759: strlcpy(szFName, HISTORY_FILE, MAXPATHLEN);
760: else
761: strlcpy(szFName, histfile, MAXPATHLEN);
762:
763: mode = umask(0177);
764: f = fopen(szFName, "w");
765: if (!f) {
1.1 misho 766: LOGERR;
1.3 misho 767: return RETCODE_ERR;
768: }
769:
770: TAILQ_FOREACH(h, &buffer->line_history, hist_next) {
771: fprintf(f, "%s\n", h->hist_line);
772:
773: if (lines)
774: lines--;
775: else
776: break;
777: }
1.1 misho 778:
1.3 misho 779: fclose(f);
780: umask(mode);
781:
782: return RETCODE_OK;
1.1 misho 783: }
784:
1.3 misho 785: /*
1.5.4.1 misho 786: * cli_loadHistory() - Load history from file
787: *
1.3 misho 788: * @buffer = CLI buffer
789: * @histfile = History filename, if NULL will be use default name
790: * return: RETCODE_ERR error, RETCODE_OK ok
791: */
792: int
793: cli_loadHistory(linebuffer_t * __restrict buffer, const char *histfile)
794: {
795: FILE *f;
796: char szFName[MAXPATHLEN], buf[BUFSIZ];
797: struct tagHistory *h;
798:
799: if (!buffer) {
1.5.4.2 ! misho 800: cli_SetErr(EINVAL, "Invalid input parameters ...");
1.3 misho 801: return RETCODE_ERR;
802: }
803: if (!histfile)
804: strlcpy(szFName, HISTORY_FILE, MAXPATHLEN);
805: else
806: strlcpy(szFName, histfile, MAXPATHLEN);
807:
808: f = fopen(szFName, "r");
809: if (!f)
810: return RETCODE_OK;
811:
812: while (fgets(buf, BUFSIZ, f)) {
813: if (!*buf || *buf == '#')
814: continue;
815: else
1.5.4.1 misho 816: str_Trim(buf);
1.3 misho 817:
1.5.4.1 misho 818: if (!(h = e_malloc(sizeof(struct tagHistory)))) {
1.3 misho 819: LOGERR;
820: fclose(f);
821: return RETCODE_ERR;
822: } else
823: memset(h, 0, sizeof(struct tagHistory));
824:
825: h->hist_len = strlcpy(h->hist_line, buf, BUFSIZ);
826: TAILQ_INSERT_TAIL(&buffer->line_history, h, hist_next);
827: }
828:
829: fclose(f);
830:
831: return RETCODE_OK;
832: }
1.1 misho 833:
834: /*
1.5.4.1 misho 835: * cli_resetHistory() - Reset history search in CLI session
836: *
1.3 misho 837: * @buffer = CLI buffer
1.1 misho 838: * return: none
839: */
1.3 misho 840: inline void
841: cli_resetHistory(linebuffer_t * __restrict buffer)
1.1 misho 842: {
1.3 misho 843: buffer->line_h = NULL;
1.1 misho 844: }
845:
1.3 misho 846:
1.1 misho 847: /*
1.5.4.1 misho 848: * cli_freeLine() - Clear entire line
849: *
1.3 misho 850: * @buffer = CLI buffer
851: * return: RETCODE_ERR error, RETCODE_OK ok
1.2 misho 852: */
1.3 misho 853: inline int
854: cli_freeLine(linebuffer_t * __restrict buffer)
1.2 misho 855: {
1.3 misho 856: int code = RETCODE_ERR;
1.2 misho 857:
1.3 misho 858: if (buffer) {
859: if (buffer->line_buf)
1.5.4.1 misho 860: e_free(buffer->line_buf);
1.3 misho 861:
1.5.4.1 misho 862: buffer->line_buf = e_malloc(BUFSIZ);
1.3 misho 863: if (buffer->line_buf) {
864: memset(buffer->line_buf, 0, BUFSIZ);
865: buffer->line_eol = buffer->line_bol;
866: buffer->line_len = 1 + buffer->line_eol;
1.2 misho 867:
1.3 misho 868: code = RETCODE_OK;
869: } else
870: LOGERR;
871: } else
1.5.4.2 ! misho 872: cli_SetErr(EINVAL, "Invalid input parameters ...");
1.2 misho 873:
1.3 misho 874: return code;
1.2 misho 875: }
876:
877: /*
1.5.4.1 misho 878: * cli_setPrompt() - Set new prompt for CLI session
879: *
1.3 misho 880: * @buffer = CLI buffer
881: * @prompt = new text for prompt or if NULL disable prompt
882: * return: none
1.2 misho 883: */
1.3 misho 884: inline void
885: cli_setPrompt(linebuffer_t * __restrict buffer, const char *prompt)
1.2 misho 886: {
1.3 misho 887: if (buffer) {
888: if (buffer->line_prompt) {
1.5.4.1 misho 889: e_free(buffer->line_prompt);
1.3 misho 890: buffer->line_prompt = NULL;
891: buffer->line_bol = 0;
892: }
893:
894: if (prompt) {
1.5.4.1 misho 895: buffer->line_prompt = e_strdup(prompt);
1.3 misho 896: if (buffer->line_prompt) {
897: buffer->line_bol = strlen(buffer->line_prompt);
898: buffer->line_eol = buffer->line_bol;
899: buffer->line_len = 1 + buffer->line_eol;
900: } else
901: LOGERR;
902: }
903: } else
1.5.4.2 ! misho 904: cli_SetErr(EINVAL, "Invalid input parameters ...");
1.2 misho 905: }
906:
1.3 misho 907:
1.2 misho 908: /*
1.5.4.1 misho 909: * cliEnd() - Clear data, Free resources and close CLI session
910: *
1.3 misho 911: * @buffer = CLI buffer
912: * return: RETCODE_ERR error, RETCODE_OK ok
1.2 misho 913: */
1.3 misho 914: void
915: cliEnd(linebuffer_t * __restrict buffer)
1.2 misho 916: {
1.3 misho 917: struct tagHistory *h;
918: struct tagCommand *c;
919:
920: if (buffer) {
921: while ((c = SLIST_FIRST(&buffer->line_cmds))) {
922: SLIST_REMOVE_HEAD(&buffer->line_cmds, cmd_next);
1.5.4.1 misho 923: e_free(c);
1.3 misho 924: }
925: while ((h = TAILQ_FIRST(&buffer->line_history))) {
926: TAILQ_REMOVE(&buffer->line_history, h, hist_next);
1.5.4.1 misho 927: e_free(h);
1.3 misho 928: }
929:
930: if (buffer->line_prompt)
1.5.4.1 misho 931: e_free(buffer->line_prompt);
1.2 misho 932:
1.3 misho 933: if (buffer->line_keys)
1.5.4.1 misho 934: e_free(buffer->line_keys);
1.3 misho 935: if (buffer->line_buf)
1.5.4.1 misho 936: e_free(buffer->line_buf);
1.2 misho 937:
1.5.4.1 misho 938: e_free(buffer);
1.3 misho 939: buffer = NULL;
940: } else
1.5.4.2 ! misho 941: cli_SetErr(EINVAL, "Invalid input parameters ...");
1.3 misho 942: }
943:
944: /*
1.5.4.1 misho 945: * cliInit() - Start CLI session, allocate memory for resources and bind keys
946: *
1.3 misho 947: * @fin = Input device handle
948: * @fout = Output device handle
949: * @prompt = text for prompt, if NULL disable prompt
950: * return: NULL if error or !=NULL CLI buffer
951: */
952: linebuffer_t *
953: cliInit(int fin, int fout, const char *prompt)
954: {
955: linebuffer_t *buffer;
956: bindkey_t *keys;
957: register int i;
958:
959: /* init buffer */
1.5.4.1 misho 960: buffer = e_malloc(sizeof(linebuffer_t));
1.3 misho 961: if (!buffer) {
962: LOGERR;
963: return NULL;
964: } else {
965: memset(buffer, 0, sizeof(linebuffer_t));
966:
967: buffer->line_in = fin;
968: buffer->line_out = fout;
969:
970: TAILQ_INIT(&buffer->line_history);
971: SLIST_INIT(&buffer->line_cmds);
972:
973: if (prompt) {
1.5.4.1 misho 974: buffer->line_prompt = e_strdup(prompt);
1.3 misho 975: if (!buffer->line_prompt) {
976: LOGERR;
1.5.4.1 misho 977: e_free(buffer);
1.3 misho 978: return NULL;
979: } else
980: buffer->line_eol = buffer->line_bol = strlen(buffer->line_prompt);
981: }
982: }
1.5.4.1 misho 983: buffer->line_buf = e_malloc(BUFSIZ);
1.3 misho 984: if (!buffer->line_buf) {
985: LOGERR;
986: if (buffer->line_prompt)
1.5.4.1 misho 987: e_free(buffer->line_prompt);
988: e_free(buffer);
1.3 misho 989: return NULL;
990: } else {
991: memset(buffer->line_buf, 0, BUFSIZ);
992: buffer->line_len = 1 + buffer->line_eol;
993: }
1.5.4.1 misho 994: keys = e_calloc(MAX_BINDKEY + 1, sizeof(bindkey_t));
1.3 misho 995: if (!keys) {
996: LOGERR;
997: if (buffer->line_prompt)
1.5.4.1 misho 998: e_free(buffer->line_prompt);
999: e_free(buffer->line_buf);
1000: e_free(buffer);
1.3 misho 1001: return NULL;
1002: } else
1003: memset(keys, 0, sizeof(bindkey_t) * (MAX_BINDKEY + 1));
1004:
1005: /* add helper functions */
1006: cli_addCommand(buffer, "exit", 0, cli_Cmd_Exit, "exit <cr>", "Exit from console");
1007: cli_addCommand(buffer, "help", 0, cli_Cmd_Help, "help [command] <cr>", "Help screen");
1008: cli_addCommand(buffer, "-------", 0, NULL, "-------------------------", NULL);
1009:
1010: /* fill key bindings */
1.5.4.1 misho 1011: /* ascii chars & ctrl+chars */
1.3 misho 1012: for (i = 0; i < 256; i++) {
1013: *keys[i].key_ch = (u_char) i;
1014: keys[i].key_len = 1;
1015:
1016: if (!i || i == *K_CTRL_D)
1017: keys[i].key_func = bufEOF;
1018: if (i == *K_CTRL_M || i == *K_CTRL_J)
1019: keys[i].key_func = bufEOL;
1020: if (i == *K_CTRL_H || i == *K_BACKSPACE)
1021: keys[i].key_func = bufBS;
1022: if (i == *K_CTRL_C)
1023: keys[i].key_func = bufCLR;
1024: if (i == *K_CTRL_A)
1025: keys[i].key_func = bufBEGIN;
1026: if (i == *K_CTRL_E)
1027: keys[i].key_func = bufEND;
1028: if (i == *K_TAB)
1029: keys[i].key_func = bufComp;
1030: if (i >= *K_SPACE && i < *K_BACKSPACE)
1031: keys[i].key_func = bufCHAR;
1032: if (i > *K_BACKSPACE && i < 0xff)
1033: keys[i].key_func = bufCHAR;
1034: if (i == '?')
1035: keys[i].key_func = bufHelp;
1036: }
1.5.4.1 misho 1037: /* alt+chars */
1.3 misho 1038: for (i = 256; i < 512; i++) {
1039: keys[i].key_ch[0] = 0x1b;
1040: keys[i].key_ch[1] = (u_char) i - 256;
1041: keys[i].key_len = 2;
1042: }
1043:
1.5.4.1 misho 1044: /* 3 bytes */
1.3 misho 1045: keys[i].key_len = sizeof K_F1 - 1;
1046: memcpy(keys[i].key_ch, K_F1, keys[i].key_len);
1047: i++;
1048: keys[i].key_len = sizeof K_F2 - 1;
1049: memcpy(keys[i].key_ch, K_F2, keys[i].key_len);
1050: i++;
1051: keys[i].key_len = sizeof K_F3 - 1;
1052: memcpy(keys[i].key_ch, K_F3, keys[i].key_len);
1053: i++;
1054: keys[i].key_len = sizeof K_F4 - 1;
1055: memcpy(keys[i].key_ch, K_F4, keys[i].key_len);
1056: i++;
1057: keys[i].key_len = sizeof K_CTRL_SH_F1 - 1;
1058: memcpy(keys[i].key_ch, K_CTRL_SH_F1, keys[i].key_len);
1059: i++;
1060: keys[i].key_len = sizeof K_CTRL_SH_F2 - 1;
1061: memcpy(keys[i].key_ch, K_CTRL_SH_F2, keys[i].key_len);
1062: i++;
1063: keys[i].key_len = sizeof K_CTRL_SH_F3 - 1;
1064: memcpy(keys[i].key_ch, K_CTRL_SH_F3, keys[i].key_len);
1065: i++;
1066: keys[i].key_len = sizeof K_CTRL_SH_F4 - 1;
1067: memcpy(keys[i].key_ch, K_CTRL_SH_F4, keys[i].key_len);
1068: i++;
1069: keys[i].key_len = sizeof K_CTRL_SH_F5 - 1;
1070: memcpy(keys[i].key_ch, K_CTRL_SH_F5, keys[i].key_len);
1071: i++;
1072: keys[i].key_len = sizeof K_CTRL_SH_F6 - 1;
1073: memcpy(keys[i].key_ch, K_CTRL_SH_F6, keys[i].key_len);
1074: i++;
1075: keys[i].key_len = sizeof K_CTRL_SH_F7 - 1;
1076: memcpy(keys[i].key_ch, K_CTRL_SH_F7, keys[i].key_len);
1077: i++;
1078: keys[i].key_len = sizeof K_CTRL_SH_F8 - 1;
1079: memcpy(keys[i].key_ch, K_CTRL_SH_F8, keys[i].key_len);
1080: i++;
1081: keys[i].key_len = sizeof K_CTRL_SH_F9 - 1;
1082: memcpy(keys[i].key_ch, K_CTRL_SH_F9, keys[i].key_len);
1083: i++;
1084: keys[i].key_len = sizeof K_CTRL_SH_F10 - 1;
1085: memcpy(keys[i].key_ch, K_CTRL_SH_F10, keys[i].key_len);
1086: i++;
1087: keys[i].key_len = sizeof K_CTRL_SH_F11 - 1;
1088: memcpy(keys[i].key_ch, K_CTRL_SH_F11, keys[i].key_len);
1089: i++;
1090: keys[i].key_len = sizeof K_CTRL_SH_F12 - 1;
1091: memcpy(keys[i].key_ch, K_CTRL_SH_F12, keys[i].key_len);
1092: i++;
1093: keys[i].key_len = sizeof K_CTRL_F1 - 1;
1094: memcpy(keys[i].key_ch, K_CTRL_F1, keys[i].key_len);
1095: i++;
1096: keys[i].key_len = sizeof K_CTRL_F2 - 1;
1097: memcpy(keys[i].key_ch, K_CTRL_F2, keys[i].key_len);
1098: i++;
1099: keys[i].key_len = sizeof K_CTRL_F3 - 1;
1100: memcpy(keys[i].key_ch, K_CTRL_F3, keys[i].key_len);
1101: i++;
1102: keys[i].key_len = sizeof K_CTRL_F4 - 1;
1103: memcpy(keys[i].key_ch, K_CTRL_F4, keys[i].key_len);
1104: i++;
1105: keys[i].key_len = sizeof K_CTRL_F5 - 1;
1106: memcpy(keys[i].key_ch, K_CTRL_F5, keys[i].key_len);
1107: i++;
1108: keys[i].key_len = sizeof K_CTRL_F6 - 1;
1109: memcpy(keys[i].key_ch, K_CTRL_F6, keys[i].key_len);
1110: i++;
1111: keys[i].key_len = sizeof K_CTRL_F7 - 1;
1112: memcpy(keys[i].key_ch, K_CTRL_F7, keys[i].key_len);
1113: i++;
1114: keys[i].key_len = sizeof K_CTRL_F8 - 1;
1115: memcpy(keys[i].key_ch, K_CTRL_F8, keys[i].key_len);
1116: i++;
1117: keys[i].key_len = sizeof K_CTRL_F9 - 1;
1118: memcpy(keys[i].key_ch, K_CTRL_F9, keys[i].key_len);
1119: i++;
1120: keys[i].key_len = sizeof K_CTRL_F10 - 1;
1121: memcpy(keys[i].key_ch, K_CTRL_F10, keys[i].key_len);
1122: i++;
1123: keys[i].key_len = sizeof K_CTRL_F11 - 1;
1124: memcpy(keys[i].key_ch, K_CTRL_F11, keys[i].key_len);
1125: i++;
1126: keys[i].key_len = sizeof K_CTRL_F12 - 1;
1127: memcpy(keys[i].key_ch, K_CTRL_F12, keys[i].key_len);
1128: i++;
1129: keys[i].key_len = sizeof K_HOME - 1;
1130: keys[i].key_func = bufBEGIN;
1131: memcpy(keys[i].key_ch, K_HOME, keys[i].key_len);
1132: i++;
1133: keys[i].key_len = sizeof K_END - 1;
1134: keys[i].key_func = bufEND;
1135: memcpy(keys[i].key_ch, K_END, keys[i].key_len);
1136: i++;
1137: keys[i].key_len = sizeof K_UP - 1;
1138: keys[i].key_func = bufUP;
1139: memcpy(keys[i].key_ch, K_UP, keys[i].key_len);
1140: i++;
1141: keys[i].key_len = sizeof K_DOWN - 1;
1142: keys[i].key_func = bufDOWN;
1143: memcpy(keys[i].key_ch, K_DOWN, keys[i].key_len);
1144: i++;
1145: keys[i].key_len = sizeof K_RIGHT - 1;
1146: keys[i].key_func = bufRIGHT;
1147: memcpy(keys[i].key_ch, K_RIGHT, keys[i].key_len);
1148: i++;
1149: keys[i].key_len = sizeof K_LEFT - 1;
1150: keys[i].key_func = bufLEFT;
1151: memcpy(keys[i].key_ch, K_LEFT, keys[i].key_len);
1152: i++;
1153: keys[i].key_len = sizeof K_BTAB - 1;
1154: keys[i].key_func = bufBTAB;
1155: memcpy(keys[i].key_ch, K_BTAB, keys[i].key_len);
1156: i++;
1.5.4.1 misho 1157: /* 4 bytes */
1.3 misho 1158: keys[i].key_len = sizeof K_INS - 1;
1159: keys[i].key_func = bufMODE;
1160: memcpy(keys[i].key_ch, K_INS, keys[i].key_len);
1161: i++;
1162: keys[i].key_len = sizeof K_DEL - 1;
1163: keys[i].key_func = bufDEL;
1164: memcpy(keys[i].key_ch, K_DEL, keys[i].key_len);
1165: i++;
1166: keys[i].key_len = sizeof K_PGUP - 1;
1167: memcpy(keys[i].key_ch, K_PGUP, keys[i].key_len);
1168: i++;
1169: keys[i].key_len = sizeof K_PGDN - 1;
1170: memcpy(keys[i].key_ch, K_PGDN, keys[i].key_len);
1171: i++;
1.5.4.1 misho 1172: /* 5 bytes */
1.3 misho 1173: keys[i].key_len = sizeof K_F5 - 1;
1174: memcpy(keys[i].key_ch, K_F5, keys[i].key_len);
1175: i++;
1176: keys[i].key_len = sizeof K_F6 - 1;
1177: memcpy(keys[i].key_ch, K_F6, keys[i].key_len);
1178: i++;
1179: keys[i].key_len = sizeof K_F7 - 1;
1180: memcpy(keys[i].key_ch, K_F7, keys[i].key_len);
1181: i++;
1182: keys[i].key_len = sizeof K_F8 - 1;
1183: memcpy(keys[i].key_ch, K_F8, keys[i].key_len);
1184: i++;
1185: keys[i].key_len = sizeof K_F9 - 1;
1186: memcpy(keys[i].key_ch, K_F9, keys[i].key_len);
1187: i++;
1188: keys[i].key_len = sizeof K_F10 - 1;
1189: memcpy(keys[i].key_ch, K_F10, keys[i].key_len);
1190: i++;
1191: keys[i].key_len = sizeof K_F11 - 1;
1192: memcpy(keys[i].key_ch, K_F11, keys[i].key_len);
1193: i++;
1194: keys[i].key_len = sizeof K_F12 - 1;
1195: memcpy(keys[i].key_ch, K_F12, keys[i].key_len);
1196: i++;
1197:
1198: buffer->line_keys = keys;
1199: return buffer;
1.2 misho 1200: }
1201:
1202: /*
1.5.4.1 misho 1203: * cliInitLine() - Init CLI input line terminal
1204: *
1.3 misho 1205: * @buffer = CLI buffer
1.2 misho 1206: * return: none
1207: */
1.3 misho 1208: int
1209: cliInitLine(linebuffer_t * __restrict buffer)
1.2 misho 1210: {
1211: struct termios t;
1212:
1213: memset(&t, 0, sizeof t);
1.3 misho 1214: tcgetattr(buffer->line_in, &t);
1.2 misho 1215: t.c_lflag &= ~(ICANON | ISIG | IEXTEN | ECHO | ECHOCTL | ECHOE | ECHOK | ECHOKE | ECHONL | ECHOPRT);
1216: t.c_iflag |= IGNBRK;
1217: t.c_cc[VMIN] = 1;
1218: t.c_cc[VTIME] = 0;
1.3 misho 1219: return tcsetattr(buffer->line_in, TCSANOW, &t);
1220: }
1221:
1222: /*
1.5.4.1 misho 1223: * cliReadLine() - Read line from opened CLI session
1224: *
1.3 misho 1225: * @buffer = CLI buffer
1.5.4.1 misho 1226: * return: NULL if error or !=NULL readed line, must be e_free after use!
1.3 misho 1227: */
1228: char *
1229: cliReadLine(linebuffer_t * __restrict buffer)
1230: {
1231: int code, readLen;
1232: register int i;
1233: struct pollfd fds;
1234: char buf[BUFSIZ], *str = NULL;
1.2 misho 1235:
1.3 misho 1236: if (!buffer) {
1.5.4.2 ! misho 1237: cli_SetErr(EINVAL, "Invalid input parameters ...");
1.3 misho 1238: return NULL;
1239: }
1.2 misho 1240:
1.3 misho 1241: memset(&fds, 0, sizeof fds);
1242: fds.fd = buffer->line_in;
1243: fds.events = POLLIN;
1244:
1245: printfCR(buffer, 1);
1246: while (42) {
1247: if (poll(&fds, 1, -1) < 1) {
1248: LOGERR;
1249: return str;
1250: }
1251:
1252: memset(buf, 0, sizeof buf);
1253: readLen = read(buffer->line_in, buf, BUFSIZ);
1254: if (readLen == -1) {
1255: LOGERR;
1256: return str;
1257: }
1258: if (!readLen) {
1259: if (buffer->line_buf)
1.5.4.1 misho 1260: str = e_strdup(buffer->line_buf);
1.3 misho 1261: else
1.5.4.2 ! misho 1262: cli_SetErr(EPIPE, "Unknown state ...");
1.3 misho 1263: return str;
1264: }
1265:
1266: recheck:
1267: for (code = RETCODE_OK, i = MAX_BINDKEY - 1; i > -1; i--)
1268: if (readLen >= buffer->line_keys[i].key_len &&
1269: !memcmp(buffer->line_keys[i].key_ch, buf,
1270: buffer->line_keys[i].key_len)) {
1271: readLen -= buffer->line_keys[i].key_len;
1272: if (readLen)
1273: memmove(buf, buf + buffer->line_keys[i].key_len, readLen);
1274: else
1275: memset(buf, 0, buffer->line_keys[i].key_len);
1276:
1277: if (buffer->line_keys[i].key_func)
1278: if ((code = buffer->line_keys[i].key_func(i, buffer)))
1279: readLen = 0;
1280:
1281: if (readLen)
1282: goto recheck;
1283: else
1284: break;
1285: }
1286:
1287: if (code)
1288: break;
1289: }
1.2 misho 1290:
1.3 misho 1291: if (code != RETCODE_ERR && code != RETCODE_EOF && buffer->line_buf)
1.5.4.1 misho 1292: str = e_strdup(buffer->line_buf);
1.3 misho 1293: return str;
1.2 misho 1294: }
1295:
1.3 misho 1296:
1.2 misho 1297: /*
1.5.4.1 misho 1298: * cliNetLoop() - CLI network main loop binded to socket
1299: *
1.3 misho 1300: * @buffer = CLI buffer
1301: * @csHistFile = History file name
1.2 misho 1302: * @sock = client socket
1.3 misho 1303: * return: RETCODE_ERR error, RETCODE_OK ok
1.2 misho 1304: */
1.3 misho 1305: int
1306: cliNetLoop(linebuffer_t * __restrict buffer, const char *csHistFile, int sock)
1.2 misho 1307: {
1.3 misho 1308: u_char buf[BUFSIZ];
1309: int pid, stat, pty, r, s, alen, flg, attrlen = 0, ret = 0;
1.2 misho 1310: fd_set fds;
1311: struct timeval tv = { DEFAULT_SOCK_TIMEOUT, 0 };
1312: struct telnetAttrs *a, Attr[10];
1313:
1.3 misho 1314: switch ((pid = forkpty(&pty, NULL, NULL, NULL))) {
1.2 misho 1315: case -1:
1316: LOGERR;
1317: return -1;
1318: case 0:
1.3 misho 1319: if (!buffer) {
1.5.4.2 ! misho 1320: cli_SetErr(EINVAL, "Invalid input parameters ...");
1.3 misho 1321: return -1;
1322: } else
1323: close(sock);
1.2 misho 1324:
1.3 misho 1325: ret = cliLoop(buffer, csHistFile) < 0 ? 1 : 0;
1326: cliEnd(buffer);
1.2 misho 1327:
1.3 misho 1328: exit(ret);
1.2 misho 1329: default:
1.4 misho 1330: cli_telnet_SetCmd(Attr + 0, DO, TELOPT_TTYPE);
1331: cli_telnet_SetCmd(Attr + 1, WILL, TELOPT_ECHO);
1332: cli_telnet_Set_SubOpt(Attr + 2, TELOPT_LFLOW, LFLOW_OFF, NULL, 0);
1333: cli_telnet_Set_SubOpt(Attr + 3, TELOPT_LFLOW, LFLOW_RESTART_XON, NULL, 0);
1334: cli_telnet_SetCmd(Attr + 4, DO, TELOPT_LINEMODE);
1335: if ((ret = cli_telnetSend(sock, Attr, 5, NULL, 0, 0)) == -1)
1.2 misho 1336: return -1;
1.4 misho 1337: else
1.2 misho 1338: flg = 0;
1339:
1340: while (42) {
1.3 misho 1341: if (waitpid(pid, &stat, WNOHANG))
1342: break;
1343:
1.2 misho 1344: FD_ZERO(&fds);
1345: FD_SET(sock, &fds);
1346: FD_SET(pty, &fds);
1347: if ((ret = select(FD_SETSIZE, &fds, NULL, NULL, &tv)) < 1) {
1348: if (!ret)
1349: cli_SetErr(ETIMEDOUT, "Client session timeout ...");
1350:
1351: break;
1352: }
1353:
1354: r = FD_ISSET(sock, &fds) ? sock : pty;
1355: s = FD_ISSET(sock, &fds) ? pty : sock;
1356:
1.3 misho 1357: if (FD_ISSET(sock, &fds)) {
1358: memset(buf, 0, BUFSIZ);
1.4 misho 1359: if ((ret = cli_telnetRecv(sock, &a, &alen, buf, BUFSIZ)) < 0) {
1.3 misho 1360: if (a)
1.5.4.1 misho 1361: e_free(a);
1.3 misho 1362:
1363: if (-2 == ret)
1364: continue;
1365: // EOF
1366: if (-3 == ret)
1367: shutdown(sock, SHUT_RD);
1368: break;
1369: }
1370: attrlen = 0;
1371: if (1 == flg && alen) {
1.4 misho 1372: cli_telnet_SetCmd(&Attr[attrlen++], DONT, TELOPT_SGA);
1373: cli_telnet_SetCmd(&Attr[attrlen++], DO, TELOPT_ECHO);
1.3 misho 1374: }
1375: if (2 == flg && alen) {
1.4 misho 1376: cli_telnet_SetCmd(&Attr[attrlen++], WILL, TELOPT_ECHO);
1377: cli_telnet_Set_SubOpt(&Attr[attrlen++], TELOPT_LFLOW,
1.3 misho 1378: LFLOW_OFF, NULL, 0);
1.4 misho 1379: cli_telnet_Set_SubOpt(&Attr[attrlen++], TELOPT_LFLOW,
1.3 misho 1380: LFLOW_RESTART_XON, NULL, 0);
1.4 misho 1381: cli_telnet_SetCmd(&Attr[attrlen++], DONT, TELOPT_LINEMODE);
1.3 misho 1382: }
1.2 misho 1383: if (a)
1.5.4.1 misho 1384: e_free(a);
1.2 misho 1385:
1.3 misho 1386: if ((ret = write(pty, buf, ret)) == -1) {
1387: LOGERR;
1388: break;
1389: }
1390: }
1391:
1392: if (FD_ISSET(pty, &fds)) {
1393: memset(buf, 0, BUFSIZ);
1394: if ((ret = read(pty, buf, BUFSIZ)) == -1) {
1395: LOGERR;
1396: break;
1397: }
1398:
1.4 misho 1399: if ((ret = cli_telnetSend(sock, Attr, pty == s ? 0 : attrlen,
1400: buf, ret, 0)) == -1)
1.3 misho 1401: break;
1.4 misho 1402: else
1.3 misho 1403: flg++;
1.2 misho 1404: }
1405: }
1406:
1407: close(pty);
1408: }
1409:
1410: return ret;
1411: }
1412:
1413: /*
1.5.4.1 misho 1414: * cliLoop() - CLI main loop
1415: *
1.3 misho 1416: * @buffer = CLI buffer
1417: * @csHistFile = History file name
1418: * return: RETCODE_ERR error, RETCODE_OK ok
1.1 misho 1419: */
1.3 misho 1420: int
1421: cliLoop(linebuffer_t * __restrict buffer, const char *csHistFile)
1.1 misho 1422: {
1423: char *line, *s, *t, **app, *items[MAX_PROMPT_ITEMS];
1424: register int i;
1.3 misho 1425: int ret = RETCODE_OK;
1426: struct tagCommand *cmd;
1.1 misho 1427:
1428: /* --- main body of CLI --- */
1.3 misho 1429: cliInitLine(buffer);
1.1 misho 1430:
1.3 misho 1431: if (cli_loadHistory(buffer, csHistFile) == RETCODE_ERR)
1432: return RETCODE_ERR;
1.1 misho 1433:
1434: do {
1.3 misho 1435: line = cliReadLine(buffer);
1436: if (!line) {
1437: printfNL(buffer, 0);
1.1 misho 1438: break;
1.3 misho 1439: } else
1440: cli_addHistory(buffer, NULL);
1.1 misho 1441: // clear whitespaces
1.4 misho 1442: for (s = line; isspace((int) *s); s++);
1.1 misho 1443: if (*s) {
1.4 misho 1444: for (t = s + strlen(s) - 1; t > s && isspace((int) *t); t--);
1.1 misho 1445: *++t = 0;
1446: }
1447:
1448: if (*s) {
1449: memset(items, 0, sizeof(char*) * MAX_PROMPT_ITEMS);
1450: for (app = items; app < items + MAX_PROMPT_ITEMS - 1 && (*app = strsep(&s, " \t"));
1451: *app ? app++ : app);
1452:
1453: // exec_cmd ...
1.3 misho 1454: i = 0;
1455: SLIST_FOREACH(cmd, &buffer->line_cmds, cmd_next) {
1456: if (*items[0] && !strncmp(cmd->cmd_name, items[0], strlen(items[0])))
1.1 misho 1457: break;
1.3 misho 1458: else
1459: i++;
1460: }
1461:
1.1 misho 1462: if (!cmd) {
1.3 misho 1463: cli_Printf(buffer, "\nCommand '%s' not found!\n", items[0]);
1.1 misho 1464: ret = -1;
1465: } else
1.3 misho 1466: if (cmd->cmd_func) {
1467: cli_Printf(buffer, "\n");
1468: ret = cmd->cmd_func(buffer, i, items);
1469: } else {
1470: clrscrEOL(buffer);
1471: printfCR(buffer, 1);
1472: }
1.1 misho 1473: }
1474:
1.3 misho 1475: cli_freeLine(buffer);
1476: cli_resetHistory(buffer);
1.5.4.1 misho 1477: e_free(line);
1.1 misho 1478: } while (ret < 1);
1479:
1.3 misho 1480: cli_saveHistory(buffer, csHistFile, HISTORY_LINES);
1.1 misho 1481: return ret;
1482: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>