Annotation of libaitcli/src/aitcli.c, revision 1.1.1.1.2.5
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.5! misho 6: * $Id: aitcli.c,v 1.1.1.1.2.4 2010/04/20 11:48:49 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:
1.1.1.1.2.4 misho 30: static void cli_Null_Prep_Term(int meta)
31: {
32: }
33:
34: static void cli_Null_Deprep_Term()
35: {
36: }
37:
38: static int cli_Pre_Input_Change_Mode()
39: {
40: return 0;
41: }
42:
43: static int cli_GetC(FILE *dummy)
44: {
45: int ch = rl_getc(stdin);
46:
47: /*
48: if (is_special_char(ch)) {
49: pending_special_char = ch;
50: return '\r';
51: }
52: */
53:
54: return ch;
55: }
56:
57:
1.1 misho 58: // cli_GetErrno() Get error code of last operation
59: inline int cli_GetErrno()
60: {
61: return cli_Errno;
62: }
63:
64: // io_GetError() Get error text of last operation
65: inline const char *cli_GetError()
66: {
67: return cli_Error;
68: }
69:
70: // cli_SetErr() Set error to variables for internal use!!!
71: inline void cli_SetErr(int eno, char *estr, ...)
72: {
73: va_list lst;
74:
75: cli_Errno = eno;
76: memset(cli_Error, 0, STRSIZ);
77: va_start(lst, estr);
78: vsnprintf(cli_Error, STRSIZ, estr, lst);
79: va_end(lst);
80: }
81:
82: // ------------------------------------------------------------
83:
84: /*
85: * cli_Printf() Printf CLI features
86: * @out = Output stream
87: * @csFormat = Printf format string
1.1.1.1.2.4 misho 88: * return: -1 error, != -1 printed chars
1.1 misho 89: */
90: inline int cli_Printf(FILE *out, const char *csFormat, ...)
91: {
92: va_list lst;
93: int ret;
94:
95: va_start(lst, csFormat);
96:
97: ret = vfprintf(out, csFormat, lst);
98: if (-1 == ret)
99: LOGERR;
100:
101: va_end(lst);
102: return ret;
103: }
104:
105:
106: /*
107: * cliComp() Initialize completion CLI features
108: * @cmdComplete = Completion function
109: * @cmdEntry = Compentry function
110: * return: none
111: */
112: inline void cliComp(cli_Completion_t *cmdComplete, cli_CompEntry_t *cmdEntry)
113: {
114: // command completon
115: rl_attempted_completion_function = cmdComplete;
116: rl_completion_entry_function = cmdEntry;
117: }
118:
119: /*
1.1.1.1.2.1 misho 120: * cliTTY() Initialize I/O TTY CLI features
1.1.1.1.2.3 misho 121: * @term = terminal name
1.1.1.1.2.1 misho 122: * @inp = input handle
123: * @out = output handle
1.1.1.1.2.4 misho 124: * @win = window size
125: * return: -1 error, != -1 ok
126: */
127: inline int cliTTY(const char *term, FILE *inp, FILE *out, struct winsize *win)
128: {
129: if (term)
130: rl_terminal_name = term;
131:
132: if (inp)
133: rl_instream = inp;
134: if (out)
135: rl_outstream = out;
136:
137: if (win)
138: if (ioctl(!rl_outstream ? STDOUT_FILENO : fileno(rl_outstream), TIOCSWINSZ, win) == -1) {
139: LOGERR;
140: return -1;
141: }
142:
143: return 0;
144: }
145:
146: /*
1.1.1.1.2.5! misho 147: * cli_ReadHistory() Read CLI History from file
! 148: * @csFile = history file name, if NULL default history name is ".aitcli.history"
! 149: * return: -1 error; != -1 readed ok
! 150: */
! 151: inline int cli_ReadHistory(const char *csFile)
! 152: {
! 153: return read_history(!csFile ? ".aitcli.history" : csFile);
! 154: }
! 155:
! 156: /*
! 157: * cli_WriteHistory() Write CLI History to file
! 158: * @csFile = history file name, if NULL default history name is ".aitcli.history"
! 159: * return: -1 error; != -1 readed ok
! 160: */
! 161: inline int cli_WriteHistory(const char *csFile)
! 162: {
! 163: return write_history(!csFile ? ".aitcli.history" : csFile);
! 164: }
! 165:
! 166: /*
1.1.1.1.2.4 misho 167: * cliInit() Initialize Readline
168: * @csProg = program name
1.1.1.1.2.1 misho 169: * return: none
170: */
1.1.1.1.2.4 misho 171: inline void cliInit(const char *csProg)
1.1.1.1.2.1 misho 172: {
1.1.1.1.2.4 misho 173: rl_readline_name = csProg;
174:
175: rl_variable_bind("editing-mode", "emacs");
176: }
177:
178: /*
179: * cliNetInit() Initialize Readline if CLI bind to socket
180: * @csProg = program name
181: * @pty = Master pty
182: * @term = stdin termios
183: * return: none
184: */
185: void cliNetInit(const char *csProg, int pty, struct termios *term)
186: {
187: struct termios t;
188:
189: if (term) {
190: t = *term;
191: t.c_lflag &= ~(ICANON | ISIG | ECHO | ECHOCTL | ECHOE | ECHOK | ECHOKE | ECHONL | ECHOPRT);
192: t.c_iflag &= ~ICRNL;
193: t.c_iflag |= IGNBRK;
194: t.c_cc[VMIN] = 1;
195: t.c_cc[VTIME] = 0;
196: tcsetattr(pty, TCSANOW, &t);
197: }
198:
199: cliInit(csProg);
200:
201: rl_instream = fdopen(pty, "r");
202:
203: rl_prep_term_function = cli_Null_Prep_Term;
204: rl_deprep_term_function = cli_Null_Deprep_Term;
205: rl_pre_input_hook = cli_Pre_Input_Change_Mode;
206:
207: rl_getc_function = cli_GetC;
208:
1.1.1.1.2.1 misho 209: }
210:
211: /*
1.1 misho 212: * cliExec() Execute CLI main loop
213: * @cmdList = Commands list
214: * @csPrompt = Prompt text
215: * return: -1 error, 0 = exit w/^+D, 1 done.
216: */
1.1.1.1.2.2 misho 217: int cliExec(cliCommands_t *cmdList, const char *csPrompt)
1.1 misho 218: {
219: char *line, *s, *t, **app, *items[MAX_PROMPT_ITEMS];
220: int ret = 0;
221: register int i;
222: cliCommands_t *cmd = NULL;
1.1.1.1.2.2 misho 223: FILE *out;
1.1 misho 224:
225: inline int inline_help()
226: {
227: cli_Cmd_Help(cmdList ? cmdList : cli_stdCmds, -1, out, NULL);
228: rl_on_new_line();
229: return 0;
230: }
231:
232: char **cli_stdCompletion(const char *text, int start, int end)
233: {
234: register int i;
235: char **matches = NULL;
236:
237: char *cmdCompGet(const char *text, int state)
238: {
239: int len = strlen(text);
240:
241: for (i = state; cmdList[i].cmd_name; i++) {
242: if (strncmp(cmdList[i].cmd_name, "---", 3) &&
243: !strncmp(cmdList[i].cmd_name, text, len))
244: return strdup(cmdList[i].cmd_name);
245: }
246:
247: return NULL;
248: }
249:
250: if (!start)
251: matches = rl_completion_matches(text, cmdCompGet);
252: else
253: for (i = 0; cmdList[i].cmd_name; i++) {
254: if (!cmdList[i].cmd_comp)
255: continue;
256: if (!strncmp(rl_line_buffer, cmdList[i].cmd_name, strlen(cmdList[i].cmd_name)))
257: matches = rl_completion_matches(text, cmdList[i].cmd_comp);
258: }
259:
260: return matches;
261: }
262: char *cli_stdCompEntry(const char *ignore, int invoking_key)
263: {
264: return NULL;
265: }
266:
267: /* --- main body of CLI --- */
268:
1.1.1.1.2.2 misho 269: out = rl_outstream;
270: if (!out)
271: out = stdout;
272:
1.1 misho 273: rl_bind_key('?', inline_help);
274: if (!rl_attempted_completion_function)
275: cliComp(cli_stdCompletion, cli_stdCompEntry);
276:
277: do {
278: line = readline(csPrompt);
279: if (!line) { // ^+d
280: cli_Printf(out, "\n");
281: break;
282: }
283: // clear whitespaces
284: for (s = line; isspace(*s); s++);
285: if (*s) {
286: for (t = s + strlen(s) - 1; t > s && isspace(*t); t--);
287: *++t = 0;
288: }
289:
290: if (*s) {
291: add_history(s);
292:
293: memset(items, 0, sizeof(char*) * MAX_PROMPT_ITEMS);
294: for (app = items; app < items + MAX_PROMPT_ITEMS - 1 && (*app = strsep(&s, " \t"));
295: *app ? app++ : app);
296:
297: /*
298: for (i = 0; i < MAX_PROMPT_ITEMS; i++)
299: cli_Printf(out, "i=%d %s\n", i, items[i]);
300: */
301:
302: // exec_cmd ...
303: for (cmd = NULL, i = 0; cmdList[i].cmd_name; i++)
304: if (*items[0] && !strncmp(cmdList[i].cmd_name, items[0], strlen(items[0]))) {
305: cmd = &cmdList[i];
306: break;
307: }
308: if (!cmd) {
309: cli_Printf(out, "Command '%s' not found!\n", items[0]);
310: ret = -1;
311: } else
312: ret = cmd->cmd_func(cmdList, i, out, items);
313: }
314:
315: free(line);
316: } while (ret < 1);
317:
318: return ret;
319: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>