/*
|
/*
|
*
|
*
|
* Another test harness for the readline callback interface.
|
* Another test harness for the readline callback interface.
|
*
|
*
|
* Author: Bob Rossi <bob@brasko.net>
|
* Author: Bob Rossi <bob@brasko.net>
|
*/
|
*/
|
|
|
#if defined (HAVE_CONFIG_H)
|
#if defined (HAVE_CONFIG_H)
|
#include <config.h>
|
#include <config.h>
|
#endif
|
#endif
|
|
|
#include <stdio.h>
|
#include <stdio.h>
|
#include <sys/types.h>
|
#include <sys/types.h>
|
#include <errno.h>
|
#include <errno.h>
|
#include <curses.h>
|
#include <curses.h>
|
|
|
#include <stdlib.h>
|
#include <stdlib.h>
|
#include <unistd.h>
|
#include <unistd.h>
|
|
|
#include <signal.h>
|
#include <signal.h>
|
|
|
#if 0 /* LINUX */
|
#if 0 /* LINUX */
|
#include <pty.h>
|
#include <pty.h>
|
#else
|
#else
|
#include <util.h>
|
#include <util.h>
|
#endif
|
#endif
|
|
|
#ifdef READLINE_LIBRARY
|
#ifdef READLINE_LIBRARY
|
# include "readline.h"
|
# include "readline.h"
|
#else
|
#else
|
# include <readline/readline.h>
|
# include <readline/readline.h>
|
#endif
|
#endif
|
|
|
/**
|
/**
|
* Master/Slave PTY used to keep readline off of stdin/stdout.
|
* Master/Slave PTY used to keep readline off of stdin/stdout.
|
*/
|
*/
|
static int masterfd = -1;
|
static int masterfd = -1;
|
static int slavefd;
|
static int slavefd;
|
|
|
void
|
void
|
sigint (s)
|
sigint (s)
|
int s;
|
int s;
|
{
|
{
|
tty_reset (STDIN_FILENO);
|
tty_reset (STDIN_FILENO);
|
close (masterfd);
|
close (masterfd);
|
close (slavefd);
|
close (slavefd);
|
printf ("\n");
|
printf ("\n");
|
exit (0);
|
exit (0);
|
}
|
}
|
|
|
static int
|
static int
|
user_input()
|
user_input()
|
{
|
{
|
int size;
|
int size;
|
const int MAX = 1024;
|
const int MAX = 1024;
|
char *buf = (char *)malloc(MAX+1);
|
char *buf = (char *)malloc(MAX+1);
|
|
|
size = read (STDIN_FILENO, buf, MAX);
|
size = read (STDIN_FILENO, buf, MAX);
|
if (size == -1)
|
if (size == -1)
|
return -1;
|
return -1;
|
|
|
size = write (masterfd, buf, size);
|
size = write (masterfd, buf, size);
|
if (size == -1)
|
if (size == -1)
|
return -1;
|
return -1;
|
|
|
return 0;
|
return 0;
|
}
|
}
|
|
|
static int
|
static int
|
readline_input()
|
readline_input()
|
{
|
{
|
const int MAX = 1024;
|
const int MAX = 1024;
|
char *buf = (char *)malloc(MAX+1);
|
char *buf = (char *)malloc(MAX+1);
|
int size;
|
int size;
|
|
|
size = read (masterfd, buf, MAX);
|
size = read (masterfd, buf, MAX);
|
if (size == -1)
|
if (size == -1)
|
{
|
{
|
free( buf );
|
free( buf );
|
buf = NULL;
|
buf = NULL;
|
return -1;
|
return -1;
|
}
|
}
|
|
|
buf[size] = 0;
|
buf[size] = 0;
|
|
|
/* Display output from readline */
|
/* Display output from readline */
|
if ( size > 0 )
|
if ( size > 0 )
|
fprintf(stderr, "%s", buf);
|
fprintf(stderr, "%s", buf);
|
|
|
free( buf );
|
free( buf );
|
buf = NULL;
|
buf = NULL;
|
return 0;
|
return 0;
|
}
|
}
|
|
|
static void
|
static void
|
rlctx_send_user_command(char *line)
|
rlctx_send_user_command(char *line)
|
{
|
{
|
/* This happens when rl_callback_read_char gets EOF */
|
/* This happens when rl_callback_read_char gets EOF */
|
if ( line == NULL )
|
if ( line == NULL )
|
return;
|
return;
|
|
|
if (strcmp (line, "exit") == 0) {
|
if (strcmp (line, "exit") == 0) {
|
tty_reset (STDIN_FILENO);
|
tty_reset (STDIN_FILENO);
|
close (masterfd);
|
close (masterfd);
|
close (slavefd);
|
close (slavefd);
|
printf ("\n");
|
printf ("\n");
|
exit (0);
|
exit (0);
|
}
|
}
|
|
|
/* Don't add the enter command */
|
/* Don't add the enter command */
|
if ( line && *line != '\0' )
|
if ( line && *line != '\0' )
|
add_history(line);
|
add_history(line);
|
}
|
}
|
|
|
static void
|
static void
|
custom_deprep_term_function ()
|
custom_deprep_term_function ()
|
{
|
{
|
}
|
}
|
|
|
static int
|
static int
|
init_readline (int inputfd, int outputfd)
|
init_readline (int inputfd, int outputfd)
|
{
|
{
|
FILE *inputFILE, *outputFILE;
|
FILE *inputFILE, *outputFILE;
|
|
|
inputFILE = fdopen (inputfd, "r");
|
inputFILE = fdopen (inputfd, "r");
|
if (!inputFILE)
|
if (!inputFILE)
|
return -1;
|
return -1;
|
|
|
outputFILE = fdopen (outputfd, "w");
|
outputFILE = fdopen (outputfd, "w");
|
if (!outputFILE)
|
if (!outputFILE)
|
return -1;
|
return -1;
|
|
|
rl_instream = inputFILE;
|
rl_instream = inputFILE;
|
rl_outstream = outputFILE;
|
rl_outstream = outputFILE;
|
|
|
/* Tell readline what the prompt is if it needs to put it back */
|
/* Tell readline what the prompt is if it needs to put it back */
|
rl_callback_handler_install("(rltest): ", rlctx_send_user_command);
|
rl_callback_handler_install("(rltest): ", rlctx_send_user_command);
|
|
|
/* Set the terminal type to dumb so the output of readline can be
|
/* Set the terminal type to dumb so the output of readline can be
|
* understood by tgdb */
|
* understood by tgdb */
|
if ( rl_reset_terminal("dumb") == -1 )
|
if ( rl_reset_terminal("dumb") == -1 )
|
return -1;
|
return -1;
|
|
|
/* For some reason, readline can not deprep the terminal.
|
/* For some reason, readline can not deprep the terminal.
|
* However, it doesn't matter because no other application is working on
|
* However, it doesn't matter because no other application is working on
|
* the terminal besides readline */
|
* the terminal besides readline */
|
rl_deprep_term_function = custom_deprep_term_function;
|
rl_deprep_term_function = custom_deprep_term_function;
|
|
|
using_history();
|
using_history();
|
read_history(".history");
|
read_history(".history");
|
|
|
return 0;
|
return 0;
|
}
|
}
|
|
|
static int
|
static int
|
main_loop(void)
|
main_loop(void)
|
{
|
{
|
fd_set rset;
|
fd_set rset;
|
int max;
|
int max;
|
|
|
max = (masterfd > STDIN_FILENO) ? masterfd : STDIN_FILENO;
|
max = (masterfd > STDIN_FILENO) ? masterfd : STDIN_FILENO;
|
max = (max > slavefd) ? max : slavefd;
|
max = (max > slavefd) ? max : slavefd;
|
|
|
for (;;)
|
for (;;)
|
{
|
{
|
/* Reset the fd_set, and watch for input from GDB or stdin */
|
/* Reset the fd_set, and watch for input from GDB or stdin */
|
FD_ZERO(&rset);
|
FD_ZERO(&rset);
|
|
|
FD_SET(STDIN_FILENO, &rset);
|
FD_SET(STDIN_FILENO, &rset);
|
FD_SET(slavefd, &rset);
|
FD_SET(slavefd, &rset);
|
FD_SET(masterfd, &rset);
|
FD_SET(masterfd, &rset);
|
|
|
/* Wait for input */
|
/* Wait for input */
|
if (select(max + 1, &rset, NULL, NULL, NULL) == -1)
|
if (select(max + 1, &rset, NULL, NULL, NULL) == -1)
|
{
|
{
|
if (errno == EINTR)
|
if (errno == EINTR)
|
continue;
|
continue;
|
else
|
else
|
return -1;
|
return -1;
|
}
|
}
|
|
|
/* Input received through the pty: Handle it
|
/* Input received through the pty: Handle it
|
* Wrote to masterfd, slave fd has that input, alert readline to read it.
|
* Wrote to masterfd, slave fd has that input, alert readline to read it.
|
*/
|
*/
|
if (FD_ISSET(slavefd, &rset))
|
if (FD_ISSET(slavefd, &rset))
|
rl_callback_read_char();
|
rl_callback_read_char();
|
|
|
/* Input received through the pty.
|
/* Input received through the pty.
|
* Readline read from slavefd, and it wrote to the masterfd.
|
* Readline read from slavefd, and it wrote to the masterfd.
|
*/
|
*/
|
if (FD_ISSET(masterfd, &rset))
|
if (FD_ISSET(masterfd, &rset))
|
if ( readline_input() == -1 )
|
if ( readline_input() == -1 )
|
return -1;
|
return -1;
|
|
|
/* Input received: Handle it, write to masterfd (input to readline) */
|
/* Input received: Handle it, write to masterfd (input to readline) */
|
if (FD_ISSET(STDIN_FILENO, &rset))
|
if (FD_ISSET(STDIN_FILENO, &rset))
|
if ( user_input() == -1 )
|
if ( user_input() == -1 )
|
return -1;
|
return -1;
|
}
|
}
|
|
|
return 0;
|
return 0;
|
}
|
}
|
|
|
/* The terminal attributes before calling tty_cbreak */
|
/* The terminal attributes before calling tty_cbreak */
|
static struct termios save_termios;
|
static struct termios save_termios;
|
static struct winsize size;
|
static struct winsize size;
|
static enum { RESET, TCBREAK } ttystate = RESET;
|
static enum { RESET, TCBREAK } ttystate = RESET;
|
|
|
/* tty_cbreak: Sets terminal to cbreak mode. Also known as noncanonical mode.
|
/* tty_cbreak: Sets terminal to cbreak mode. Also known as noncanonical mode.
|
* 1. Signal handling is still turned on, so the user can still type those.
|
* 1. Signal handling is still turned on, so the user can still type those.
|
* 2. echo is off
|
* 2. echo is off
|
* 3. Read in one char at a time.
|
* 3. Read in one char at a time.
|
*
|
*
|
* fd - The file descriptor of the terminal
|
* fd - The file descriptor of the terminal
|
*
|
*
|
* Returns: 0 on sucess, -1 on error
|
* Returns: 0 on sucess, -1 on error
|
*/
|
*/
|
int tty_cbreak(int fd){
|
int tty_cbreak(int fd){
|
struct termios buf;
|
struct termios buf;
|
int ttysavefd = -1;
|
int ttysavefd = -1;
|
|
|
if(tcgetattr(fd, &save_termios) < 0)
|
if(tcgetattr(fd, &save_termios) < 0)
|
return -1;
|
return -1;
|
|
|
buf = save_termios;
|
buf = save_termios;
|
buf.c_lflag &= ~(ECHO | ICANON);
|
buf.c_lflag &= ~(ECHO | ICANON);
|
buf.c_iflag &= ~(ICRNL | INLCR);
|
buf.c_iflag &= ~(ICRNL | INLCR);
|
buf.c_cc[VMIN] = 1;
|
buf.c_cc[VMIN] = 1;
|
buf.c_cc[VTIME] = 0;
|
buf.c_cc[VTIME] = 0;
|
|
|
#if defined (VLNEXT) && defined (_POSIX_VDISABLE)
|
#if defined (VLNEXT) && defined (_POSIX_VDISABLE)
|
buf.c_cc[VLNEXT] = _POSIX_VDISABLE;
|
buf.c_cc[VLNEXT] = _POSIX_VDISABLE;
|
#endif
|
#endif
|
|
|
#if defined (VDSUSP) && defined (_POSIX_VDISABLE)
|
#if defined (VDSUSP) && defined (_POSIX_VDISABLE)
|
buf.c_cc[VDSUSP] = _POSIX_VDISABLE;
|
buf.c_cc[VDSUSP] = _POSIX_VDISABLE;
|
#endif
|
#endif
|
|
|
/* enable flow control; only stty start char can restart output */
|
/* enable flow control; only stty start char can restart output */
|
#if 0
|
#if 0
|
buf.c_iflag |= (IXON|IXOFF);
|
buf.c_iflag |= (IXON|IXOFF);
|
#ifdef IXANY
|
#ifdef IXANY
|
buf.c_iflag &= ~IXANY;
|
buf.c_iflag &= ~IXANY;
|
#endif
|
#endif
|
#endif
|
#endif
|
|
|
/* disable flow control; let ^S and ^Q through to pty */
|
/* disable flow control; let ^S and ^Q through to pty */
|
buf.c_iflag &= ~(IXON|IXOFF);
|
buf.c_iflag &= ~(IXON|IXOFF);
|
#ifdef IXANY
|
#ifdef IXANY
|
buf.c_iflag &= ~IXANY;
|
buf.c_iflag &= ~IXANY;
|
#endif
|
#endif
|
|
|
if(tcsetattr(fd, TCSAFLUSH, &buf) < 0)
|
if(tcsetattr(fd, TCSAFLUSH, &buf) < 0)
|
return -1;
|
return -1;
|
|
|
ttystate = TCBREAK;
|
ttystate = TCBREAK;
|
ttysavefd = fd;
|
ttysavefd = fd;
|
|
|
/* set size */
|
/* set size */
|
if(ioctl(fd, TIOCGWINSZ, (char *)&size) < 0)
|
if(ioctl(fd, TIOCGWINSZ, (char *)&size) < 0)
|
return -1;
|
return -1;
|
|
|
#ifdef DEBUG
|
#ifdef DEBUG
|
err_msg("%d rows and %d cols\n", size.ws_row, size.ws_col);
|
err_msg("%d rows and %d cols\n", size.ws_row, size.ws_col);
|
#endif
|
#endif
|
|
|
return (0);
|
return (0);
|
}
|
}
|
|
|
int
|
int
|
tty_off_xon_xoff (int fd)
|
tty_off_xon_xoff (int fd)
|
{
|
{
|
struct termios buf;
|
struct termios buf;
|
int ttysavefd = -1;
|
int ttysavefd = -1;
|
|
|
if(tcgetattr(fd, &buf) < 0)
|
if(tcgetattr(fd, &buf) < 0)
|
return -1;
|
return -1;
|
|
|
buf.c_iflag &= ~(IXON|IXOFF);
|
buf.c_iflag &= ~(IXON|IXOFF);
|
|
|
if(tcsetattr(fd, TCSAFLUSH, &buf) < 0)
|
if(tcsetattr(fd, TCSAFLUSH, &buf) < 0)
|
return -1;
|
return -1;
|
|
|
return 0;
|
return 0;
|
}
|
}
|
|
|
/* tty_reset: Sets the terminal attributes back to their previous state.
|
/* tty_reset: Sets the terminal attributes back to their previous state.
|
* PRE: tty_cbreak must have already been called.
|
* PRE: tty_cbreak must have already been called.
|
*
|
*
|
* fd - The file descrioptor of the terminal to reset.
|
* fd - The file descrioptor of the terminal to reset.
|
*
|
*
|
* Returns: 0 on success, -1 on error
|
* Returns: 0 on success, -1 on error
|
*/
|
*/
|
int tty_reset(int fd)
|
int tty_reset(int fd)
|
{
|
{
|
if(ttystate != TCBREAK)
|
if(ttystate != TCBREAK)
|
return (0);
|
return (0);
|
|
|
if(tcsetattr(fd, TCSAFLUSH, &save_termios) < 0)
|
if(tcsetattr(fd, TCSAFLUSH, &save_termios) < 0)
|
return (-1);
|
return (-1);
|
|
|
ttystate = RESET;
|
ttystate = RESET;
|
|
|
return 0;
|
return 0;
|
}
|
}
|
|
|
int
|
int
|
main()
|
main()
|
{
|
{
|
int val;
|
int val;
|
val = openpty (&masterfd, &slavefd, NULL, NULL, NULL);
|
val = openpty (&masterfd, &slavefd, NULL, NULL, NULL);
|
if (val == -1)
|
if (val == -1)
|
return -1;
|
return -1;
|
|
|
val = tty_off_xon_xoff (masterfd);
|
val = tty_off_xon_xoff (masterfd);
|
if (val == -1)
|
if (val == -1)
|
return -1;
|
return -1;
|
|
|
val = init_readline (slavefd, slavefd);
|
val = init_readline (slavefd, slavefd);
|
if (val == -1)
|
if (val == -1)
|
return -1;
|
return -1;
|
|
|
val = tty_cbreak (STDIN_FILENO);
|
val = tty_cbreak (STDIN_FILENO);
|
if (val == -1)
|
if (val == -1)
|
return -1;
|
return -1;
|
|
|
signal (SIGINT, sigint);
|
signal (SIGINT, sigint);
|
|
|
val = main_loop ();
|
val = main_loop ();
|
|
|
tty_reset (STDIN_FILENO);
|
tty_reset (STDIN_FILENO);
|
|
|
if (val == -1)
|
if (val == -1)
|
return -1;
|
return -1;
|
|
|
return 0;
|
return 0;
|
}
|
}
|
|
|