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

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.1.1.1.2.1! misho       6: * $Id: aitcli.c,v 1.1.1.1 2010/04/16 13:20:29 misho Exp $
1.1       misho       7: *
                      8: *************************************************************************/
                      9: #include "global.h"
                     10: 
                     11: 
                     12: #pragma GCC visibility push(hidden)
                     13: 
                     14: cliCommands_t cli_stdCmds[] = {
                     15:        { "test", cli_Cmd_Unsupported, "Test - Don`t use default command structure!", "test <cr>", cli_Comp_Filename }, 
                     16:        { "-------", NULL, "---------------------", NULL, NULL }, 
                     17:        { "help", cli_Cmd_Help, "Help screen", "help [command] <cr>", NULL }, 
                     18:        { "exit", cli_Cmd_Exit, "Exit from console", "exit <cr>", NULL }, 
                     19:        { NULL, NULL, NULL, NULL }
                     20: };
                     21: 
                     22: // ------------------------------------------------
                     23: 
                     24: int cli_Errno;
                     25: char cli_Error[STRSIZ];
                     26: 
                     27: #pragma GCC visibility pop
                     28: 
                     29: 
                     30: // cli_GetErrno() Get error code of last operation
                     31: inline int cli_GetErrno()
                     32: {
                     33:        return cli_Errno;
                     34: }
                     35: 
                     36: // io_GetError() Get error text of last operation
                     37: inline const char *cli_GetError()
                     38: {
                     39:        return cli_Error;
                     40: }
                     41: 
                     42: // cli_SetErr() Set error to variables for internal use!!!
                     43: inline void cli_SetErr(int eno, char *estr, ...)
                     44: {
                     45:        va_list lst;
                     46: 
                     47:        cli_Errno = eno;
                     48:        memset(cli_Error, 0, STRSIZ);
                     49:        va_start(lst, estr);
                     50:        vsnprintf(cli_Error, STRSIZ, estr, lst);
                     51:        va_end(lst);
                     52: }
                     53: 
                     54: // ------------------------------------------------------------
                     55: 
                     56: /*
                     57:  * cli_Printf() Printf CLI features
                     58:  * @out = Output stream
                     59:  * @csFormat = Printf format string
                     60:  * return: none
                     61: */
                     62: inline int cli_Printf(FILE *out, const char *csFormat, ...)
                     63: {
                     64:        va_list lst;
                     65:        int ret;
                     66: 
                     67:        va_start(lst, csFormat);
                     68: 
                     69:        ret = vfprintf(out, csFormat, lst);
                     70:        if (-1 == ret)
                     71:                LOGERR;
                     72: 
                     73:        va_end(lst);
                     74:        return ret;
                     75: }
                     76: 
                     77: 
                     78: /*
                     79:  * cliComp() Initialize completion CLI features
                     80:  * @cmdComplete = Completion function
                     81:  * @cmdEntry = Compentry function
                     82:  * return: none
                     83: */
                     84: inline void cliComp(cli_Completion_t *cmdComplete, cli_CompEntry_t *cmdEntry)
                     85: {
                     86:        // command completon
                     87:        rl_attempted_completion_function = cmdComplete;
                     88:        rl_completion_entry_function = cmdEntry;
                     89: }
                     90: 
                     91: /*
1.1.1.1.2.1! misho      92:  * cliTTY() Initialize I/O TTY CLI features
        !            93:  * @inp = input handle
        !            94:  * @out = output handle
        !            95:  * return: none
        !            96: */
        !            97: inline void cliTTY(FILE *inp, FILE *out)
        !            98: {
        !            99:        rl_outstream = inp;
        !           100:        rl_outstream = out;
        !           101: }
        !           102: 
        !           103: /*
1.1       misho     104:  * cliExec() Execute CLI main loop
                    105:  * @cmdList = Commands list
                    106:  * @out = Output handle
                    107:  * @csPrompt = Prompt text
                    108:  * return: -1 error, 0 = exit w/^+D, 1 done.
                    109: */
                    110: int cliExec(cliCommands_t *cmdList, FILE *out, const char *csPrompt)
                    111: {
                    112:        char *line, *s, *t, **app, *items[MAX_PROMPT_ITEMS];
                    113:        int ret = 0;
                    114:        register int i;
                    115:        cliCommands_t *cmd = NULL;
                    116: 
                    117:        inline int inline_help()
                    118:        {
                    119:                cli_Cmd_Help(cmdList ? cmdList : cli_stdCmds, -1, out, NULL);
                    120:                rl_on_new_line();
                    121:                return 0;
                    122:        }
                    123: 
                    124:        char **cli_stdCompletion(const char *text, int start, int end)
                    125:        {
                    126:                register int i;
                    127:                char **matches = NULL;
                    128: 
                    129:                char *cmdCompGet(const char *text, int state)
                    130:                {
                    131:                        int len = strlen(text);
                    132: 
                    133:                        for (i = state; cmdList[i].cmd_name; i++) {
                    134:                                if (strncmp(cmdList[i].cmd_name, "---", 3) && 
                    135:                                                !strncmp(cmdList[i].cmd_name, text, len))
                    136:                                        return strdup(cmdList[i].cmd_name);
                    137:                        }
                    138: 
                    139:                        return NULL;
                    140:                }
                    141: 
                    142:                if (!start)
                    143:                        matches = rl_completion_matches(text, cmdCompGet);
                    144:                else
                    145:                        for (i = 0; cmdList[i].cmd_name; i++) {
                    146:                                if (!cmdList[i].cmd_comp)
                    147:                                        continue;
                    148:                                if (!strncmp(rl_line_buffer, cmdList[i].cmd_name, strlen(cmdList[i].cmd_name)))
                    149:                                        matches = rl_completion_matches(text, cmdList[i].cmd_comp);
                    150:                        }
                    151: 
                    152:                return matches;
                    153:        }
                    154:        char *cli_stdCompEntry(const char *ignore, int invoking_key)
                    155:        {
                    156:                return NULL;
                    157:        }
                    158: 
                    159:        /* --- main body of CLI --- */
                    160: 
                    161:        rl_bind_key('?', inline_help);
                    162:        if (!rl_attempted_completion_function) 
                    163:                cliComp(cli_stdCompletion, cli_stdCompEntry);
                    164: 
                    165:        do {
                    166:                line = readline(csPrompt);
                    167:                if (!line) {    // ^+d
                    168:                        cli_Printf(out, "\n");
                    169:                        break;
                    170:                }
                    171:                // clear whitespaces
                    172:                for (s = line; isspace(*s); s++);
                    173:                if (*s) {
                    174:                        for (t = s + strlen(s) - 1; t > s && isspace(*t); t--);
                    175:                        *++t = 0;
                    176:                }
                    177: 
                    178:                if (*s) {
                    179:                        add_history(s);
                    180: 
                    181:                        memset(items, 0, sizeof(char*) * MAX_PROMPT_ITEMS);
                    182:                        for (app = items; app < items + MAX_PROMPT_ITEMS - 1 && (*app = strsep(&s, " \t")); 
                    183:                                        *app ? app++ : app);
                    184: 
                    185:                        /*
                    186:                        for (i = 0; i < MAX_PROMPT_ITEMS; i++)
                    187:                                cli_Printf(out, "i=%d %s\n", i, items[i]);
                    188:                                */
                    189: 
                    190:                        // exec_cmd ...
                    191:                        for (cmd = NULL, i = 0; cmdList[i].cmd_name; i++)
                    192:                                if (*items[0] && !strncmp(cmdList[i].cmd_name, items[0], strlen(items[0]))) {
                    193:                                        cmd = &cmdList[i];
                    194:                                        break;
                    195:                                }
                    196:                        if (!cmd) {
                    197:                                cli_Printf(out, "Command '%s' not found!\n", items[0]);
                    198:                                ret = -1;
                    199:                        } else
                    200:                                ret = cmd->cmd_func(cmdList, i, out, items);
                    201:                }
                    202: 
                    203:                free(line);
                    204:        } while (ret < 1);
                    205: 
                    206:        return ret;
                    207: }

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