3 * Another test harness for the readline callback interface.
5 * Author: Bob Rossi <bob@brasko.net>
8 #if defined (HAVE_CONFIG_H)
13 #include <sys/types.h>
28 #ifdef READLINE_LIBRARY
29 # include "readline.h"
31 # include <readline/readline.h>
35 * Master/Slave PTY used to keep readline off of stdin/stdout.
37 static int masterfd = -1;
44 tty_reset (STDIN_FILENO);
56 char *buf = (char *)malloc(MAX+1);
58 size = read (STDIN_FILENO, buf, MAX);
62 size = write (masterfd, buf, size);
73 char *buf = (char *)malloc(MAX+1);
76 size = read (masterfd, buf, MAX);
86 /* Display output from readline */
88 fprintf(stderr, "%s", buf);
96 rlctx_send_user_command(char *line)
98 /* This happens when rl_callback_read_char gets EOF */
102 if (strcmp (line, "exit") == 0) {
103 tty_reset (STDIN_FILENO);
110 /* Don't add the enter command */
111 if ( line && *line != '\0' )
116 custom_deprep_term_function ()
121 init_readline (int inputfd, int outputfd)
123 FILE *inputFILE, *outputFILE;
125 inputFILE = fdopen (inputfd, "r");
129 outputFILE = fdopen (outputfd, "w");
133 rl_instream = inputFILE;
134 rl_outstream = outputFILE;
136 /* Tell readline what the prompt is if it needs to put it back */
137 rl_callback_handler_install("(rltest): ", rlctx_send_user_command);
139 /* Set the terminal type to dumb so the output of readline can be
140 * understood by tgdb */
141 if ( rl_reset_terminal("dumb") == -1 )
144 /* For some reason, readline can not deprep the terminal.
145 * However, it doesn't matter because no other application is working on
146 * the terminal besides readline */
147 rl_deprep_term_function = custom_deprep_term_function;
150 read_history(".history");
161 max = (masterfd > STDIN_FILENO) ? masterfd : STDIN_FILENO;
162 max = (max > slavefd) ? max : slavefd;
166 /* Reset the fd_set, and watch for input from GDB or stdin */
169 FD_SET(STDIN_FILENO, &rset);
170 FD_SET(slavefd, &rset);
171 FD_SET(masterfd, &rset);
174 if (select(max + 1, &rset, NULL, NULL, NULL) == -1)
182 /* Input received through the pty: Handle it
183 * Wrote to masterfd, slave fd has that input, alert readline to read it.
185 if (FD_ISSET(slavefd, &rset))
186 rl_callback_read_char();
188 /* Input received through the pty.
189 * Readline read from slavefd, and it wrote to the masterfd.
191 if (FD_ISSET(masterfd, &rset))
192 if ( readline_input() == -1 )
195 /* Input received: Handle it, write to masterfd (input to readline) */
196 if (FD_ISSET(STDIN_FILENO, &rset))
197 if ( user_input() == -1 )
204 /* The terminal attributes before calling tty_cbreak */
205 static struct termios save_termios;
206 static struct winsize size;
207 static enum { RESET, TCBREAK } ttystate = RESET;
209 /* tty_cbreak: Sets terminal to cbreak mode. Also known as noncanonical mode.
210 * 1. Signal handling is still turned on, so the user can still type those.
212 * 3. Read in one char at a time.
214 * fd - The file descriptor of the terminal
216 * Returns: 0 on sucess, -1 on error
218 int tty_cbreak(int fd){
222 if(tcgetattr(fd, &save_termios) < 0)
226 buf.c_lflag &= ~(ECHO | ICANON);
227 buf.c_iflag &= ~(ICRNL | INLCR);
231 #if defined (VLNEXT) && defined (_POSIX_VDISABLE)
232 buf.c_cc[VLNEXT] = _POSIX_VDISABLE;
235 #if defined (VDSUSP) && defined (_POSIX_VDISABLE)
236 buf.c_cc[VDSUSP] = _POSIX_VDISABLE;
239 /* enable flow control; only stty start char can restart output */
241 buf.c_iflag |= (IXON|IXOFF);
243 buf.c_iflag &= ~IXANY;
247 /* disable flow control; let ^S and ^Q through to pty */
248 buf.c_iflag &= ~(IXON|IXOFF);
250 buf.c_iflag &= ~IXANY;
253 if(tcsetattr(fd, TCSAFLUSH, &buf) < 0)
260 if(ioctl(fd, TIOCGWINSZ, (char *)&size) < 0)
264 err_msg("%d rows and %d cols\n", size.ws_row, size.ws_col);
271 tty_off_xon_xoff (int fd)
276 if(tcgetattr(fd, &buf) < 0)
279 buf.c_iflag &= ~(IXON|IXOFF);
281 if(tcsetattr(fd, TCSAFLUSH, &buf) < 0)
287 /* tty_reset: Sets the terminal attributes back to their previous state.
288 * PRE: tty_cbreak must have already been called.
290 * fd - The file descrioptor of the terminal to reset.
292 * Returns: 0 on success, -1 on error
294 int tty_reset(int fd)
296 if(ttystate != TCBREAK)
299 if(tcsetattr(fd, TCSAFLUSH, &save_termios) < 0)
311 val = openpty (&masterfd, &slavefd, NULL, NULL, NULL);
315 val = tty_off_xon_xoff (masterfd);
319 val = init_readline (slavefd, slavefd);
323 val = tty_cbreak (STDIN_FILENO);
327 signal (SIGINT, sigint);
331 tty_reset (STDIN_FILENO);