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
);
55 rl_resize_terminal ();
63 char *buf
= (char *)malloc(MAX
+1);
65 size
= read (STDIN_FILENO
, buf
, MAX
);
69 size
= write (masterfd
, buf
, size
);
80 char *buf
= (char *)malloc(MAX
+1);
83 size
= read (masterfd
, buf
, MAX
);
93 /* Display output from readline */
95 fprintf(stderr
, "%s", buf
);
103 rlctx_send_user_command(char *line
)
105 /* This happens when rl_callback_read_char gets EOF */
109 if (strcmp (line
, "exit") == 0) {
110 tty_reset (STDIN_FILENO
);
117 /* Don't add the enter command */
118 if ( line
&& *line
!= '\0' )
123 custom_deprep_term_function ()
128 init_readline (int inputfd
, int outputfd
)
130 FILE *inputFILE
, *outputFILE
;
132 inputFILE
= fdopen (inputfd
, "r");
136 outputFILE
= fdopen (outputfd
, "w");
140 rl_instream
= inputFILE
;
141 rl_outstream
= outputFILE
;
143 /* Tell readline what the prompt is if it needs to put it back */
144 rl_callback_handler_install("(rltest): ", rlctx_send_user_command
);
146 /* Set the terminal type to dumb so the output of readline can be
147 * understood by tgdb */
148 if ( rl_reset_terminal("dumb") == -1 )
151 /* For some reason, readline can not deprep the terminal.
152 * However, it doesn't matter because no other application is working on
153 * the terminal besides readline */
154 rl_deprep_term_function
= custom_deprep_term_function
;
157 read_history(".history");
168 max
= (masterfd
> STDIN_FILENO
) ? masterfd
: STDIN_FILENO
;
169 max
= (max
> slavefd
) ? max
: slavefd
;
173 /* Reset the fd_set, and watch for input from GDB or stdin */
176 FD_SET(STDIN_FILENO
, &rset
);
177 FD_SET(slavefd
, &rset
);
178 FD_SET(masterfd
, &rset
);
181 if (select(max
+ 1, &rset
, NULL
, NULL
, NULL
) == -1)
189 /* Input received through the pty: Handle it
190 * Wrote to masterfd, slave fd has that input, alert readline to read it.
192 if (FD_ISSET(slavefd
, &rset
))
193 rl_callback_read_char();
195 /* Input received through the pty.
196 * Readline read from slavefd, and it wrote to the masterfd.
198 if (FD_ISSET(masterfd
, &rset
))
199 if ( readline_input() == -1 )
202 /* Input received: Handle it, write to masterfd (input to readline) */
203 if (FD_ISSET(STDIN_FILENO
, &rset
))
204 if ( user_input() == -1 )
211 /* The terminal attributes before calling tty_cbreak */
212 static struct termios save_termios
;
213 static struct winsize size
;
214 static enum { RESET
, TCBREAK
} ttystate
= RESET
;
216 /* tty_cbreak: Sets terminal to cbreak mode. Also known as noncanonical mode.
217 * 1. Signal handling is still turned on, so the user can still type those.
219 * 3. Read in one char at a time.
221 * fd - The file descriptor of the terminal
223 * Returns: 0 on sucess, -1 on error
225 int tty_cbreak(int fd
){
229 if(tcgetattr(fd
, &save_termios
) < 0)
233 buf
.c_lflag
&= ~(ECHO
| ICANON
);
234 buf
.c_iflag
&= ~(ICRNL
| INLCR
);
238 #if defined (VLNEXT) && defined (_POSIX_VDISABLE)
239 buf
.c_cc
[VLNEXT
] = _POSIX_VDISABLE
;
242 #if defined (VDSUSP) && defined (_POSIX_VDISABLE)
243 buf
.c_cc
[VDSUSP
] = _POSIX_VDISABLE
;
246 /* enable flow control; only stty start char can restart output */
248 buf
.c_iflag
|= (IXON
|IXOFF
);
250 buf
.c_iflag
&= ~IXANY
;
254 /* disable flow control; let ^S and ^Q through to pty */
255 buf
.c_iflag
&= ~(IXON
|IXOFF
);
257 buf
.c_iflag
&= ~IXANY
;
260 if(tcsetattr(fd
, TCSAFLUSH
, &buf
) < 0)
267 if(ioctl(fd
, TIOCGWINSZ
, (char *)&size
) < 0)
271 err_msg("%d rows and %d cols\n", size
.ws_row
, size
.ws_col
);
278 tty_off_xon_xoff (int fd
)
283 if(tcgetattr(fd
, &buf
) < 0)
286 buf
.c_iflag
&= ~(IXON
|IXOFF
);
288 if(tcsetattr(fd
, TCSAFLUSH
, &buf
) < 0)
294 /* tty_reset: Sets the terminal attributes back to their previous state.
295 * PRE: tty_cbreak must have already been called.
297 * fd - The file descrioptor of the terminal to reset.
299 * Returns: 0 on success, -1 on error
301 int tty_reset(int fd
)
303 if(ttystate
!= TCBREAK
)
306 if(tcsetattr(fd
, TCSAFLUSH
, &save_termios
) < 0)
318 val
= openpty (&masterfd
, &slavefd
, NULL
, NULL
, NULL
);
322 val
= tty_off_xon_xoff (masterfd
);
326 signal (SIGWINCH
, sigwinch
);
327 signal (SIGINT
, sigint
);
329 val
= init_readline (slavefd
, slavefd
);
333 val
= tty_cbreak (STDIN_FILENO
);
339 tty_reset (STDIN_FILENO
);