Annotation of libaitcli/src/aitcli.c, revision 1.1.1.1.2.2
1.1 misho 1: /*************************************************************************
2: * (C) 2010 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
3: * by Michael Pounov <misho@openbsd-bg.org>
4: *
5: * $Author: misho $
1.1.1.1.2.2! misho 6: * $Id: aitcli.c,v 1.1.1.1.2.1 2010/04/18 20:42:23 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: * @csPrompt = Prompt text
107: * return: -1 error, 0 = exit w/^+D, 1 done.
108: */
1.1.1.1.2.2! misho 109: int cliExec(cliCommands_t *cmdList, const char *csPrompt)
1.1 misho 110: {
111: char *line, *s, *t, **app, *items[MAX_PROMPT_ITEMS];
112: int ret = 0;
113: register int i;
114: cliCommands_t *cmd = NULL;
1.1.1.1.2.2! misho 115: FILE *out;
1.1 misho 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:
1.1.1.1.2.2! misho 161: out = rl_outstream;
! 162: if (!out)
! 163: out = stdout;
! 164:
1.1 misho 165: rl_bind_key('?', inline_help);
166: if (!rl_attempted_completion_function)
167: cliComp(cli_stdCompletion, cli_stdCompEntry);
168:
169: do {
170: line = readline(csPrompt);
171: if (!line) { // ^+d
172: cli_Printf(out, "\n");
173: break;
174: }
175: // clear whitespaces
176: for (s = line; isspace(*s); s++);
177: if (*s) {
178: for (t = s + strlen(s) - 1; t > s && isspace(*t); t--);
179: *++t = 0;
180: }
181:
182: if (*s) {
183: add_history(s);
184:
185: memset(items, 0, sizeof(char*) * MAX_PROMPT_ITEMS);
186: for (app = items; app < items + MAX_PROMPT_ITEMS - 1 && (*app = strsep(&s, " \t"));
187: *app ? app++ : app);
188:
189: /*
190: for (i = 0; i < MAX_PROMPT_ITEMS; i++)
191: cli_Printf(out, "i=%d %s\n", i, items[i]);
192: */
193:
194: // exec_cmd ...
195: for (cmd = NULL, i = 0; cmdList[i].cmd_name; i++)
196: if (*items[0] && !strncmp(cmdList[i].cmd_name, items[0], strlen(items[0]))) {
197: cmd = &cmdList[i];
198: break;
199: }
200: if (!cmd) {
201: cli_Printf(out, "Command '%s' not found!\n", items[0]);
202: ret = -1;
203: } else
204: ret = cmd->cmd_func(cmdList, i, out, items);
205: }
206:
207: free(line);
208: } while (ret < 1);
209:
210: return ret;
211: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>