Annotation of libaitcli/src/aitcli.c, revision 1.8.2.4

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

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