Annotation of libaitcli/src/aitcli.c, revision 1.1.1.1.2.14
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.14! misho 6: * $Id: aitcli.c,v 1.1.1.1.2.13 2010/05/03 08:10:05 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.11 misho 30: #ifdef NULL_PREP_TERM
31:
1.1.1.1.2.4 misho 32: static void cli_Null_Prep_Term(int meta)
33: {
34: }
35:
1.1.1.1.2.11 misho 36: #endif
37:
1.1.1.1.2.4 misho 38:
1.1 misho 39: // cli_GetErrno() Get error code of last operation
40: inline int cli_GetErrno()
41: {
42: return cli_Errno;
43: }
44:
45: // io_GetError() Get error text of last operation
46: inline const char *cli_GetError()
47: {
48: return cli_Error;
49: }
50:
51: // cli_SetErr() Set error to variables for internal use!!!
52: inline void cli_SetErr(int eno, char *estr, ...)
53: {
54: va_list lst;
55:
56: cli_Errno = eno;
57: memset(cli_Error, 0, STRSIZ);
58: va_start(lst, estr);
59: vsnprintf(cli_Error, STRSIZ, estr, lst);
60: va_end(lst);
61: }
62:
63: // ------------------------------------------------------------
64:
65: /*
66: * cli_Printf() Printf CLI features
67: * @out = Output stream
68: * @csFormat = Printf format string
1.1.1.1.2.4 misho 69: * return: -1 error, != -1 printed chars
1.1 misho 70: */
71: inline int cli_Printf(FILE *out, const char *csFormat, ...)
72: {
73: va_list lst;
74: int ret;
75:
76: va_start(lst, csFormat);
77:
78: ret = vfprintf(out, csFormat, lst);
79: if (-1 == ret)
80: LOGERR;
81:
82: va_end(lst);
83: return ret;
84: }
85:
86:
87: /*
88: * cliComp() Initialize completion CLI features
89: * @cmdComplete = Completion function
90: * @cmdEntry = Compentry function
91: * return: none
92: */
93: inline void cliComp(cli_Completion_t *cmdComplete, cli_CompEntry_t *cmdEntry)
94: {
95: // command completon
96: rl_attempted_completion_function = cmdComplete;
97: rl_completion_entry_function = cmdEntry;
98: }
99:
100: /*
1.1.1.1.2.1 misho 101: * cliTTY() Initialize I/O TTY CLI features
1.1.1.1.2.3 misho 102: * @term = terminal name
1.1.1.1.2.1 misho 103: * @inp = input handle
104: * @out = output handle
1.1.1.1.2.4 misho 105: * @win = window size
106: * return: -1 error, != -1 ok
107: */
108: inline int cliTTY(const char *term, FILE *inp, FILE *out, struct winsize *win)
109: {
110: if (term)
111: rl_terminal_name = term;
112:
113: if (inp)
114: rl_instream = inp;
115: if (out)
116: rl_outstream = out;
117:
118: if (win)
119: if (ioctl(!rl_outstream ? STDOUT_FILENO : fileno(rl_outstream), TIOCSWINSZ, win) == -1) {
120: LOGERR;
121: return -1;
122: }
123:
124: return 0;
125: }
126:
127: /*
1.1.1.1.2.5 misho 128: * cli_ReadHistory() Read CLI History from file
129: * @csFile = history file name, if NULL default history name is ".aitcli.history"
130: * return: -1 error; != -1 readed ok
131: */
132: inline int cli_ReadHistory(const char *csFile)
133: {
134: return read_history(!csFile ? ".aitcli.history" : csFile);
135: }
136:
137: /*
138: * cli_WriteHistory() Write CLI History to file
139: * @csFile = history file name, if NULL default history name is ".aitcli.history"
1.1.1.1.2.6 misho 140: * @lineNum = save number of history entry lines, if -1 all lines saved without limit
1.1.1.1.2.5 misho 141: * return: -1 error; != -1 readed ok
142: */
1.1.1.1.2.6 misho 143: inline int cli_WriteHistory(const char *csFile, int lineNum)
1.1.1.1.2.5 misho 144: {
1.1.1.1.2.6 misho 145: int ret;
146: const char *psFile = !csFile ? ".aitcli.history" : csFile;
147:
148: ret = write_history(psFile);
149: if (-1 != ret && -1 != lineNum)
150: history_truncate_file(psFile, lineNum);
151:
152: return ret;
1.1.1.1.2.5 misho 153: }
154:
155: /*
1.1.1.1.2.4 misho 156: * cliNetInit() Initialize Readline if CLI bind to socket
157: * @csProg = program name
158: * @pty = Master pty
159: * @term = stdin termios
160: * return: none
161: */
162: void cliNetInit(const char *csProg, int pty, struct termios *term)
163: {
164: struct termios t;
1.1.1.1.2.10 misho 165: int on = 1;
1.1.1.1.2.4 misho 166:
1.1.1.1.2.10 misho 167: memset(&t, 0, sizeof t);
168: if (term)
1.1.1.1.2.4 misho 169: t = *term;
1.1.1.1.2.10 misho 170: else {
171: t.c_lflag = TTYDEF_LFLAG;
172: t.c_iflag = TTYDEF_IFLAG;
173: t.c_oflag = TTYDEF_OFLAG;
174: cfsetspeed(&t, B9600);
1.1.1.1.2.4 misho 175: }
176:
1.1.1.1.2.10 misho 177: t.c_lflag &= ~(ICANON | ISIG | IEXTEN | ECHO | ECHOCTL | ECHOE | ECHOK | ECHOKE | ECHONL | ECHOPRT);
178: t.c_iflag &= ~(ICRNL | BRKINT | INPCK | ISTRIP | IXON);
179: t.c_oflag &= ~OPOST;
180: t.c_cflag &= ~PARENB;
181: t.c_cc[VMIN] = 1;
182: t.c_cc[VTIME] = 0;
183: tcsetattr(pty, TCSANOW, &t);
1.1.1.1.2.4 misho 184:
1.1.1.1.2.10 misho 185: ioctl(pty, TIOCPKT, &on);
1.1.1.1.2.4 misho 186:
1.1.1.1.2.11 misho 187: rl_readline_name = csProg;
188: rl_variable_bind("editing-mode", "emacs");
189:
190: #ifdef NULL_PREP_TERM
1.1.1.1.2.10 misho 191: rl_instream = fdopen(pty, "r");
1.1.1.1.2.11 misho 192: #endif
1.1.1.1.2.1 misho 193: }
194:
195: /*
1.1.1.1.2.7 misho 196: * cliNetExec() Execute net CLI main loop
197: * @cmdList = Commands list
198: * @csPrompt = Prompt text
199: * @sock = client socket
200: * @term = stdin termios
201: * @win = window size of tty
202: * return: -1 error, 0 = exit w/^+D, 1 done.
203: */
204: int cliNetExec(cliCommands_t *cmdList, const char *csPrompt, int sock, struct termios *term, struct winsize *win)
205: {
1.1.1.1.2.13 misho 206: int pty, ret = 0, r, s, alen, attrlen, flg;
1.1.1.1.2.7 misho 207: fd_set fds;
208: struct timeval tv = { DEFAULT_SOCK_TIMEOUT, 0 };
209: u_char buf[BUFSIZ];
1.1.1.1.2.12 misho 210: struct telnetAttrs *a, Attr[10];
1.1.1.1.2.7 misho 211:
212: switch (forkpty(&pty, NULL, term, win)) {
213: case -1:
214: LOGERR;
215: return -1;
216: case 0:
217: close(sock);
218:
1.1.1.1.2.12 misho 219: #ifdef NULL_PREP_TERM
220: rl_prep_term_function = cli_Null_Prep_Term;
221: #endif
1.1.1.1.2.10 misho 222:
223: cliNetInit(getprogname(), STDIN_FILENO, term);
1.1.1.1.2.7 misho 224: cliTTY(NULL, NULL, NULL, win);
1.1.1.1.2.10 misho 225: // ret = cliExec(cmdList, csPrompt) < 0 ? 1 : 0;
226: execl("/bin/tcsh", "tcsh", NULL);
1.1.1.1.2.7 misho 227: _exit(ret);
228: default:
1.1.1.1.2.13 misho 229: telnet_SetCmd(Attr + 0, DO, TELOPT_TTYPE);
230: telnet_SetCmd(Attr + 1, WILL, TELOPT_ECHO);
1.1.1.1.2.14! misho 231: telnet_Set_SubOpt(Attr + 2, TELOPT_LFLOW, LFLOW_OFF, NULL, 0);
! 232: telnet_Set_SubOpt(Attr + 3, TELOPT_LFLOW, LFLOW_RESTART_XON, NULL, 0);
! 233: telnet_SetCmd(Attr + 4, DO, TELOPT_LINEMODE);
1.1.1.1.2.13 misho 234: if ((ret = telnetSend(sock, Attr, 5, NULL, 0, 0)) == -1) {
1.1.1.1.2.7 misho 235: cli_Errno = telnet_GetErrno();
236: strlcpy(cli_Error, telnet_GetError(), STRSIZ);
237: return -1;
1.1.1.1.2.13 misho 238: } else
239: flg = 0;
1.1.1.1.2.7 misho 240:
241: while (42) {
242: FD_ZERO(&fds);
243: FD_SET(sock, &fds);
244: FD_SET(pty, &fds);
1.1.1.1.2.10 misho 245: if ((ret = select(FD_SETSIZE, &fds, NULL, NULL, &tv)) < 1) {
246: if (!ret)
247: cli_SetErr(ETIMEDOUT, "Client session timeout ...");
248:
1.1.1.1.2.7 misho 249: break;
1.1.1.1.2.10 misho 250: }
1.1.1.1.2.7 misho 251:
252: r = FD_ISSET(sock, &fds) ? sock : pty;
253: s = FD_ISSET(sock, &fds) ? pty : sock;
254:
255: if ((ret = telnetRecv(r, &a, &alen, buf, BUFSIZ)) < 0) {
1.1.1.1.2.10 misho 256: if (a)
257: free(a);
258:
1.1.1.1.2.7 misho 259: if (-2 == ret)
260: continue;
261: // EOF
262: if (-3 == ret)
263: shutdown(r, SHUT_RD);
264: else {
265: cli_Errno = telnet_GetErrno();
266: strlcpy(cli_Error, telnet_GetError(), STRSIZ);
267: }
268: break;
269: }
1.1.1.1.2.13 misho 270: attrlen = 0;
271: if (1 == flg && alen) {
272: telnet_SetCmd(&Attr[attrlen++], DONT, TELOPT_SGA);
273: telnet_SetCmd(&Attr[attrlen++], DO, TELOPT_ECHO);
274: }
275: if (2 == flg && alen) {
276: telnet_SetCmd(&Attr[attrlen++], WILL, TELOPT_ECHO);
277: telnet_Set_SubOpt(&Attr[attrlen++], TELOPT_LFLOW,
278: LFLOW_OFF, NULL, 0);
279: telnet_Set_SubOpt(&Attr[attrlen++], TELOPT_LFLOW,
280: LFLOW_RESTART_XON, NULL, 0);
281: telnet_SetCmd(&Attr[attrlen++], DONT, TELOPT_LINEMODE);
1.1.1.1.2.10 misho 282: }
283: if (a)
284: free(a);
1.1.1.1.2.11 misho 285:
286: /*
1.1.1.1.2.13 misho 287: if (s == pty && ret > 2 && 0x1b == buf[0] && 0x5b == buf[1]) {
1.1.1.1.2.11 misho 288: memmove(buf, buf + 3, ret);
289: ret -= 3;
1.1.1.1.2.13 misho 290: }*/
1.1.1.1.2.10 misho 291: if ((ret = telnetSend(s, Attr, attrlen, buf, ret, 0)) == -1) {
1.1.1.1.2.7 misho 292: cli_Errno = telnet_GetErrno();
293: strlcpy(cli_Error, telnet_GetError(), STRSIZ);
294: break;
1.1.1.1.2.13 misho 295: } else
296: flg++;
1.1.1.1.2.7 misho 297:
298: /*
299: if ((ret = read(r, &ch, 1)) < 1) {
300: if (!ret)
301: shutdown(r, SHUT_RD);
302: break;
303: }
304: if (write(s, &ch, 1) == -1)
305: break;
306: */
307: }
308:
309: close(pty);
310: }
311:
312: return ret;
313: }
314:
315: /*
1.1 misho 316: * cliExec() Execute CLI main loop
317: * @cmdList = Commands list
318: * @csPrompt = Prompt text
319: * return: -1 error, 0 = exit w/^+D, 1 done.
320: */
1.1.1.1.2.2 misho 321: int cliExec(cliCommands_t *cmdList, const char *csPrompt)
1.1 misho 322: {
323: char *line, *s, *t, **app, *items[MAX_PROMPT_ITEMS];
324: int ret = 0;
325: register int i;
326: cliCommands_t *cmd = NULL;
1.1.1.1.2.2 misho 327: FILE *out;
1.1 misho 328:
329: inline int inline_help()
330: {
331: cli_Cmd_Help(cmdList ? cmdList : cli_stdCmds, -1, out, NULL);
332: rl_on_new_line();
333: return 0;
334: }
335:
336: char **cli_stdCompletion(const char *text, int start, int end)
337: {
338: register int i;
339: char **matches = NULL;
340:
341: char *cmdCompGet(const char *text, int state)
342: {
343: int len = strlen(text);
344:
345: for (i = state; cmdList[i].cmd_name; i++) {
346: if (strncmp(cmdList[i].cmd_name, "---", 3) &&
347: !strncmp(cmdList[i].cmd_name, text, len))
348: return strdup(cmdList[i].cmd_name);
349: }
350:
351: return NULL;
352: }
353:
354: if (!start)
355: matches = rl_completion_matches(text, cmdCompGet);
356: else
357: for (i = 0; cmdList[i].cmd_name; i++) {
358: if (!cmdList[i].cmd_comp)
359: continue;
360: if (!strncmp(rl_line_buffer, cmdList[i].cmd_name, strlen(cmdList[i].cmd_name)))
361: matches = rl_completion_matches(text, cmdList[i].cmd_comp);
362: }
363:
364: return matches;
365: }
366: char *cli_stdCompEntry(const char *ignore, int invoking_key)
367: {
368: return NULL;
369: }
370:
371: /* --- main body of CLI --- */
372:
1.1.1.1.2.2 misho 373: out = rl_outstream;
374: if (!out)
375: out = stdout;
376:
1.1 misho 377: rl_bind_key('?', inline_help);
378: if (!rl_attempted_completion_function)
379: cliComp(cli_stdCompletion, cli_stdCompEntry);
380:
381: do {
382: line = readline(csPrompt);
383: if (!line) { // ^+d
384: cli_Printf(out, "\n");
385: break;
386: }
387: // clear whitespaces
388: for (s = line; isspace(*s); s++);
389: if (*s) {
390: for (t = s + strlen(s) - 1; t > s && isspace(*t); t--);
391: *++t = 0;
392: }
393:
394: if (*s) {
395: add_history(s);
396:
397: memset(items, 0, sizeof(char*) * MAX_PROMPT_ITEMS);
398: for (app = items; app < items + MAX_PROMPT_ITEMS - 1 && (*app = strsep(&s, " \t"));
399: *app ? app++ : app);
400:
401: /*
402: for (i = 0; i < MAX_PROMPT_ITEMS; i++)
403: cli_Printf(out, "i=%d %s\n", i, items[i]);
404: */
405:
406: // exec_cmd ...
407: for (cmd = NULL, i = 0; cmdList[i].cmd_name; i++)
408: if (*items[0] && !strncmp(cmdList[i].cmd_name, items[0], strlen(items[0]))) {
409: cmd = &cmdList[i];
410: break;
411: }
412: if (!cmd) {
413: cli_Printf(out, "Command '%s' not found!\n", items[0]);
414: ret = -1;
415: } else
416: ret = cmd->cmd_func(cmdList, i, out, items);
417: }
418:
419: free(line);
420: } while (ret < 1);
421:
422: return ret;
423: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>