|
version 1.1.1.1.2.4, 2010/04/20 11:48:49
|
version 1.1.1.1.2.14, 2010/05/03 08:28:35
|
|
Line 27 char cli_Error[STRSIZ];
|
Line 27 char cli_Error[STRSIZ];
|
| #pragma GCC visibility pop |
#pragma GCC visibility pop |
| |
|
| |
|
| |
#ifdef NULL_PREP_TERM |
| |
|
| static void cli_Null_Prep_Term(int meta) |
static void cli_Null_Prep_Term(int meta) |
| { |
{ |
| } |
} |
| |
|
| static void cli_Null_Deprep_Term() | #endif |
| { | |
| } | |
| |
|
| 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 |
// cli_GetErrno() Get error code of last operation |
| inline int cli_GetErrno() |
inline int cli_GetErrno() |
| { |
{ |
|
Line 144 inline int cliTTY(const char *term, FILE *inp, FILE *o
|
Line 125 inline int cliTTY(const char *term, FILE *inp, FILE *o
|
| } |
} |
| |
|
| /* |
/* |
| * cliInit() Initialize Readline | * cli_ReadHistory() Read CLI History from file |
| * @csProg = program name | * @csFile = history file name, if NULL default history name is ".aitcli.history" |
| * return: none | * return: -1 error; != -1 readed ok |
| */ |
*/ |
| inline void cliInit(const char *csProg) | inline int cli_ReadHistory(const char *csFile) |
| { |
{ |
| rl_readline_name = csProg; | return read_history(!csFile ? ".aitcli.history" : csFile); |
| | } |
| |
|
| rl_variable_bind("editing-mode", "emacs"); | /* |
| | * 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; |
| } |
} |
| |
|
| /* |
/* |
|
Line 165 inline void cliInit(const char *csProg)
|
Line 162 inline void cliInit(const char *csProg)
|
| void cliNetInit(const char *csProg, int pty, struct termios *term) |
void cliNetInit(const char *csProg, int pty, struct termios *term) |
| { |
{ |
| struct termios t; |
struct termios t; |
| |
int on = 1; |
| |
|
| if (term) { | memset(&t, 0, sizeof t); |
| | if (term) |
| t = *term; |
t = *term; |
| t.c_lflag &= ~(ICANON | ISIG | ECHO | ECHOCTL | ECHOE | ECHOK | ECHOKE | ECHONL | ECHOPRT); | else { |
| t.c_iflag &= ~ICRNL; | t.c_lflag = TTYDEF_LFLAG; |
| t.c_iflag |= IGNBRK; | t.c_iflag = TTYDEF_IFLAG; |
| t.c_cc[VMIN] = 1; | t.c_oflag = TTYDEF_OFLAG; |
| t.c_cc[VTIME] = 0; | cfsetspeed(&t, B9600); |
| tcsetattr(pty, TCSANOW, &t); | |
| } |
} |
| |
|
| cliInit(csProg); | t.c_lflag &= ~(ICANON | ISIG | IEXTEN | ECHO | ECHOCTL | ECHOE | ECHOK | ECHOKE | ECHONL | ECHOPRT); |
| | t.c_iflag &= ~(ICRNL | BRKINT | INPCK | ISTRIP | IXON); |
| | t.c_oflag &= ~OPOST; |
| | t.c_cflag &= ~PARENB; |
| | t.c_cc[VMIN] = 1; |
| | t.c_cc[VTIME] = 0; |
| | tcsetattr(pty, TCSANOW, &t); |
| |
|
| |
ioctl(pty, TIOCPKT, &on); |
| |
|
| |
rl_readline_name = csProg; |
| |
rl_variable_bind("editing-mode", "emacs"); |
| |
|
| |
#ifdef NULL_PREP_TERM |
| rl_instream = fdopen(pty, "r"); |
rl_instream = fdopen(pty, "r"); |
| |
#endif |
| |
} |
| |
|
| rl_prep_term_function = cli_Null_Prep_Term; | /* |
| rl_deprep_term_function = cli_Null_Deprep_Term; | * cliNetExec() Execute net CLI main loop |
| rl_pre_input_hook = cli_Pre_Input_Change_Mode; | * @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, attrlen, flg; |
| | fd_set fds; |
| | struct timeval tv = { DEFAULT_SOCK_TIMEOUT, 0 }; |
| | u_char buf[BUFSIZ]; |
| | struct telnetAttrs *a, Attr[10]; |
| |
|
| rl_getc_function = cli_GetC; | switch (forkpty(&pty, NULL, term, win)) { |
| | case -1: |
| | LOGERR; |
| | return -1; |
| | case 0: |
| | close(sock); |
| |
|
| |
#ifdef NULL_PREP_TERM |
| |
rl_prep_term_function = cli_Null_Prep_Term; |
| |
#endif |
| |
|
| |
cliNetInit(getprogname(), STDIN_FILENO, term); |
| |
cliTTY(NULL, NULL, NULL, win); |
| |
// ret = cliExec(cmdList, csPrompt) < 0 ? 1 : 0; |
| |
execl("/bin/tcsh", "tcsh", NULL); |
| |
_exit(ret); |
| |
default: |
| |
telnet_SetCmd(Attr + 0, DO, TELOPT_TTYPE); |
| |
telnet_SetCmd(Attr + 1, WILL, TELOPT_ECHO); |
| |
telnet_Set_SubOpt(Attr + 2, TELOPT_LFLOW, LFLOW_OFF, NULL, 0); |
| |
telnet_Set_SubOpt(Attr + 3, TELOPT_LFLOW, LFLOW_RESTART_XON, NULL, 0); |
| |
telnet_SetCmd(Attr + 4, DO, TELOPT_LINEMODE); |
| |
if ((ret = telnetSend(sock, Attr, 5, NULL, 0, 0)) == -1) { |
| |
cli_Errno = telnet_GetErrno(); |
| |
strlcpy(cli_Error, telnet_GetError(), STRSIZ); |
| |
return -1; |
| |
} else |
| |
flg = 0; |
| |
|
| |
while (42) { |
| |
FD_ZERO(&fds); |
| |
FD_SET(sock, &fds); |
| |
FD_SET(pty, &fds); |
| |
if ((ret = select(FD_SETSIZE, &fds, NULL, NULL, &tv)) < 1) { |
| |
if (!ret) |
| |
cli_SetErr(ETIMEDOUT, "Client session timeout ..."); |
| |
|
| |
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 (a) |
| |
free(a); |
| |
|
| |
if (-2 == ret) |
| |
continue; |
| |
// EOF |
| |
if (-3 == ret) |
| |
shutdown(r, SHUT_RD); |
| |
else { |
| |
cli_Errno = telnet_GetErrno(); |
| |
strlcpy(cli_Error, telnet_GetError(), STRSIZ); |
| |
} |
| |
break; |
| |
} |
| |
attrlen = 0; |
| |
if (1 == flg && alen) { |
| |
telnet_SetCmd(&Attr[attrlen++], DONT, TELOPT_SGA); |
| |
telnet_SetCmd(&Attr[attrlen++], DO, TELOPT_ECHO); |
| |
} |
| |
if (2 == flg && alen) { |
| |
telnet_SetCmd(&Attr[attrlen++], WILL, TELOPT_ECHO); |
| |
telnet_Set_SubOpt(&Attr[attrlen++], TELOPT_LFLOW, |
| |
LFLOW_OFF, NULL, 0); |
| |
telnet_Set_SubOpt(&Attr[attrlen++], TELOPT_LFLOW, |
| |
LFLOW_RESTART_XON, NULL, 0); |
| |
telnet_SetCmd(&Attr[attrlen++], DONT, TELOPT_LINEMODE); |
| |
} |
| |
if (a) |
| |
free(a); |
| |
|
| |
/* |
| |
if (s == pty && ret > 2 && 0x1b == buf[0] && 0x5b == buf[1]) { |
| |
memmove(buf, buf + 3, ret); |
| |
ret -= 3; |
| |
}*/ |
| |
if ((ret = telnetSend(s, Attr, attrlen, buf, ret, 0)) == -1) { |
| |
cli_Errno = telnet_GetErrno(); |
| |
strlcpy(cli_Error, telnet_GetError(), STRSIZ); |
| |
break; |
| |
} else |
| |
flg++; |
| |
|
| |
/* |
| |
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; |
| } |
} |
| |
|
| /* |
/* |