--- libaitcli/src/aitcli.c 2010/04/16 13:20:29 1.1.1.1 +++ libaitcli/src/aitcli.c 2010/04/24 10:02:33 1.1.1.1.2.7 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: aitcli.c,v 1.1.1.1 2010/04/16 13:20:29 misho Exp $ +* $Id: aitcli.c,v 1.1.1.1.2.7 2010/04/24 10:02:33 misho Exp $ * *************************************************************************/ #include "global.h" @@ -27,6 +27,34 @@ char cli_Error[STRSIZ]; #pragma GCC visibility pop +static void cli_Null_Prep_Term(int meta) +{ +} + +static void cli_Null_Deprep_Term() +{ +} + +static int cli_Pre_Input_Change_Mode() +{ + return 0; +} + +static int cli_GetC(FILE *dummy) +{ + int ch = rl_getc(stdin); + + /* + if (is_special_char(ch)) { + pending_special_char = ch; + return '\r'; + } + */ + + return ch; +} + + // cli_GetErrno() Get error code of last operation inline int cli_GetErrno() { @@ -57,7 +85,7 @@ inline void cli_SetErr(int eno, char *estr, ...) * cli_Printf() Printf CLI features * @out = Output stream * @csFormat = Printf format string - * return: none + * return: -1 error, != -1 printed chars */ inline int cli_Printf(FILE *out, const char *csFormat, ...) { @@ -89,18 +117,202 @@ inline void cliComp(cli_Completion_t *cmdComplete, cli } /* + * cliTTY() Initialize I/O TTY CLI features + * @term = terminal name + * @inp = input handle + * @out = output handle + * @win = window size + * return: -1 error, != -1 ok +*/ +inline int cliTTY(const char *term, FILE *inp, FILE *out, struct winsize *win) +{ + if (term) + rl_terminal_name = term; + + if (inp) + rl_instream = inp; + if (out) + rl_outstream = out; + + if (win) + if (ioctl(!rl_outstream ? STDOUT_FILENO : fileno(rl_outstream), TIOCSWINSZ, win) == -1) { + LOGERR; + return -1; + } + + return 0; +} + +/* + * cli_ReadHistory() Read CLI History from file + * @csFile = history file name, if NULL default history name is ".aitcli.history" + * return: -1 error; != -1 readed ok +*/ +inline int cli_ReadHistory(const char *csFile) +{ + return read_history(!csFile ? ".aitcli.history" : csFile); +} + +/* + * cli_WriteHistory() Write CLI History to file + * @csFile = history file name, if NULL default history name is ".aitcli.history" + * @lineNum = save number of history entry lines, if -1 all lines saved without limit + * return: -1 error; != -1 readed ok +*/ +inline int cli_WriteHistory(const char *csFile, int lineNum) +{ + int ret; + const char *psFile = !csFile ? ".aitcli.history" : csFile; + + ret = write_history(psFile); + if (-1 != ret && -1 != lineNum) + history_truncate_file(psFile, lineNum); + + return ret; +} + +/* + * cliInit() Initialize Readline + * @csProg = program name + * return: none +*/ +inline void cliInit(const char *csProg) +{ + rl_readline_name = csProg; + + rl_variable_bind("editing-mode", "emacs"); +} + +/* + * cliNetInit() Initialize Readline if CLI bind to socket + * @csProg = program name + * @pty = Master pty + * @term = stdin termios + * return: none +*/ +void cliNetInit(const char *csProg, int pty, struct termios *term) +{ + struct termios t; + + if (term) { + t = *term; +// t.c_lflag &= ~(ICANON | ISIG | ECHO | ECHOCTL | ECHOE | ECHOK | ECHOKE | ECHONL | ECHOPRT); + t.c_iflag &= ~ICRNL; + t.c_iflag |= IGNBRK; + t.c_cc[VMIN] = 1; + t.c_cc[VTIME] = 0; + tcsetattr(pty, TCSANOW, &t); + } + + cliInit(csProg); + + rl_instream = fdopen(pty, "r"); + + rl_prep_term_function = cli_Null_Prep_Term; + rl_deprep_term_function = cli_Null_Deprep_Term; + rl_pre_input_hook = cli_Pre_Input_Change_Mode; + + rl_getc_function = cli_GetC; + +} + +/* + * cliNetExec() Execute net CLI main loop + * @cmdList = Commands list + * @csPrompt = Prompt text + * @sock = client socket + * @term = stdin termios + * @win = window size of tty + * return: -1 error, 0 = exit w/^+D, 1 done. +*/ +int cliNetExec(cliCommands_t *cmdList, const char *csPrompt, int sock, struct termios *term, struct winsize *win) +{ + int pty, ret = 0, r, s, alen; + fd_set fds; + struct timeval tv = { DEFAULT_SOCK_TIMEOUT, 0 }; + u_char buf[BUFSIZ]; + struct telnetAttrs *a, Attr[3]; + + switch (forkpty(&pty, NULL, term, win)) { + case -1: + LOGERR; + return -1; + case 0: + close(sock); + + cliTTY(NULL, NULL, NULL, win); + ret = cliExec(cmdList, csPrompt) < 0 ? 1 : 0; + _exit(ret); + default: + cliNetInit(getprogname(), pty, term); + + telnet_SetCmd(Attr, DO, TELOPT_TTYPE); + telnet_SetCmd(Attr + 1, DO, TELOPT_LINEMODE); + telnet_SetCmd(Attr + 2, GA, 0); + if ((ret = telnetSend(sock, Attr, 3, NULL, 0, 0)) == -1) { + cli_Errno = telnet_GetErrno(); + strlcpy(cli_Error, telnet_GetError(), STRSIZ); + return -1; + } + + while (42) { + FD_ZERO(&fds); + FD_SET(sock, &fds); + FD_SET(pty, &fds); + if (select(FD_SETSIZE, &fds, NULL, NULL, &tv) < 1) + break; + + r = FD_ISSET(sock, &fds) ? sock : pty; + s = FD_ISSET(sock, &fds) ? pty : sock; + + if ((ret = telnetRecv(r, &a, &alen, buf, BUFSIZ)) < 0) { + if (-2 == ret) + continue; + // EOF + if (-3 == ret) + shutdown(r, SHUT_RD); + else { + cli_Errno = telnet_GetErrno(); + strlcpy(cli_Error, telnet_GetError(), STRSIZ); + } + break; + } + if ((ret = telnetSend(s, NULL, 0, buf, ret, 0)) == -1) { + cli_Errno = telnet_GetErrno(); + strlcpy(cli_Error, telnet_GetError(), STRSIZ); + break; + } + + /* + if ((ret = read(r, &ch, 1)) < 1) { + if (!ret) + shutdown(r, SHUT_RD); + break; + } + if (write(s, &ch, 1) == -1) + break; + */ + } + + close(pty); + } + + return ret; +} + +/* * cliExec() Execute CLI main loop * @cmdList = Commands list - * @out = Output handle * @csPrompt = Prompt text * return: -1 error, 0 = exit w/^+D, 1 done. */ -int cliExec(cliCommands_t *cmdList, FILE *out, const char *csPrompt) +int cliExec(cliCommands_t *cmdList, const char *csPrompt) { char *line, *s, *t, **app, *items[MAX_PROMPT_ITEMS]; int ret = 0; register int i; cliCommands_t *cmd = NULL; + FILE *out; inline int inline_help() { @@ -145,6 +357,10 @@ int cliExec(cliCommands_t *cmdList, FILE *out, const c } /* --- main body of CLI --- */ + + out = rl_outstream; + if (!out) + out = stdout; rl_bind_key('?', inline_help); if (!rl_attempted_completion_function)