* linux-low.c (regsets_fetch_inferior_registers): Fix memory leak.
[gdb/SamB.git] / readline / examples / rlptytest.c
blob79257db4005f9b477d15a9216783960931615f65
1 /*
3 * Another test harness for the readline callback interface.
5 * Author: Bob Rossi <bob@brasko.net>
6 */
8 #if defined (HAVE_CONFIG_H)
9 #include <config.h>
10 #endif
12 #include <stdio.h>
13 #include <sys/types.h>
14 #include <errno.h>
15 #include <curses.h>
17 #include <stdlib.h>
18 #include <unistd.h>
20 #include <signal.h>
22 #if 0 /* LINUX */
23 #include <pty.h>
24 #else
25 #include <util.h>
26 #endif
28 #ifdef READLINE_LIBRARY
29 # include "readline.h"
30 #else
31 # include <readline/readline.h>
32 #endif
34 /**
35 * Master/Slave PTY used to keep readline off of stdin/stdout.
37 static int masterfd = -1;
38 static int slavefd;
40 void
41 sigint (s)
42 int s;
44 tty_reset (STDIN_FILENO);
45 close (masterfd);
46 close (slavefd);
47 printf ("\n");
48 exit (0);
51 static int
52 user_input()
54 int size;
55 const int MAX = 1024;
56 char *buf = (char *)malloc(MAX+1);
58 size = read (STDIN_FILENO, buf, MAX);
59 if (size == -1)
60 return -1;
62 size = write (masterfd, buf, size);
63 if (size == -1)
64 return -1;
66 return 0;
69 static int
70 readline_input()
72 const int MAX = 1024;
73 char *buf = (char *)malloc(MAX+1);
74 int size;
76 size = read (masterfd, buf, MAX);
77 if (size == -1)
79 free( buf );
80 buf = NULL;
81 return -1;
84 buf[size] = 0;
86 /* Display output from readline */
87 if ( size > 0 )
88 fprintf(stderr, "%s", buf);
90 free( buf );
91 buf = NULL;
92 return 0;
95 static void
96 rlctx_send_user_command(char *line)
98 /* This happens when rl_callback_read_char gets EOF */
99 if ( line == NULL )
100 return;
102 if (strcmp (line, "exit") == 0) {
103 tty_reset (STDIN_FILENO);
104 close (masterfd);
105 close (slavefd);
106 printf ("\n");
107 exit (0);
110 /* Don't add the enter command */
111 if ( line && *line != '\0' )
112 add_history(line);
115 static void
116 custom_deprep_term_function ()
120 static int
121 init_readline (int inputfd, int outputfd)
123 FILE *inputFILE, *outputFILE;
125 inputFILE = fdopen (inputfd, "r");
126 if (!inputFILE)
127 return -1;
129 outputFILE = fdopen (outputfd, "w");
130 if (!outputFILE)
131 return -1;
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 )
142 return -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;
149 using_history();
150 read_history(".history");
152 return 0;
155 static int
156 main_loop(void)
158 fd_set rset;
159 int max;
161 max = (masterfd > STDIN_FILENO) ? masterfd : STDIN_FILENO;
162 max = (max > slavefd) ? max : slavefd;
164 for (;;)
166 /* Reset the fd_set, and watch for input from GDB or stdin */
167 FD_ZERO(&rset);
169 FD_SET(STDIN_FILENO, &rset);
170 FD_SET(slavefd, &rset);
171 FD_SET(masterfd, &rset);
173 /* Wait for input */
174 if (select(max + 1, &rset, NULL, NULL, NULL) == -1)
176 if (errno == EINTR)
177 continue;
178 else
179 return -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 )
193 return -1;
195 /* Input received: Handle it, write to masterfd (input to readline) */
196 if (FD_ISSET(STDIN_FILENO, &rset))
197 if ( user_input() == -1 )
198 return -1;
201 return 0;
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.
211 * 2. echo is off
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){
219 struct termios buf;
220 int ttysavefd = -1;
222 if(tcgetattr(fd, &save_termios) < 0)
223 return -1;
225 buf = save_termios;
226 buf.c_lflag &= ~(ECHO | ICANON);
227 buf.c_iflag &= ~(ICRNL | INLCR);
228 buf.c_cc[VMIN] = 1;
229 buf.c_cc[VTIME] = 0;
231 #if defined (VLNEXT) && defined (_POSIX_VDISABLE)
232 buf.c_cc[VLNEXT] = _POSIX_VDISABLE;
233 #endif
235 #if defined (VDSUSP) && defined (_POSIX_VDISABLE)
236 buf.c_cc[VDSUSP] = _POSIX_VDISABLE;
237 #endif
239 /* enable flow control; only stty start char can restart output */
240 #if 0
241 buf.c_iflag |= (IXON|IXOFF);
242 #ifdef IXANY
243 buf.c_iflag &= ~IXANY;
244 #endif
245 #endif
247 /* disable flow control; let ^S and ^Q through to pty */
248 buf.c_iflag &= ~(IXON|IXOFF);
249 #ifdef IXANY
250 buf.c_iflag &= ~IXANY;
251 #endif
253 if(tcsetattr(fd, TCSAFLUSH, &buf) < 0)
254 return -1;
256 ttystate = TCBREAK;
257 ttysavefd = fd;
259 /* set size */
260 if(ioctl(fd, TIOCGWINSZ, (char *)&size) < 0)
261 return -1;
263 #ifdef DEBUG
264 err_msg("%d rows and %d cols\n", size.ws_row, size.ws_col);
265 #endif
267 return (0);
270 int
271 tty_off_xon_xoff (int fd)
273 struct termios buf;
274 int ttysavefd = -1;
276 if(tcgetattr(fd, &buf) < 0)
277 return -1;
279 buf.c_iflag &= ~(IXON|IXOFF);
281 if(tcsetattr(fd, TCSAFLUSH, &buf) < 0)
282 return -1;
284 return 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)
297 return (0);
299 if(tcsetattr(fd, TCSAFLUSH, &save_termios) < 0)
300 return (-1);
302 ttystate = RESET;
304 return 0;
307 int
308 main()
310 int val;
311 val = openpty (&masterfd, &slavefd, NULL, NULL, NULL);
312 if (val == -1)
313 return -1;
315 val = tty_off_xon_xoff (masterfd);
316 if (val == -1)
317 return -1;
319 val = init_readline (slavefd, slavefd);
320 if (val == -1)
321 return -1;
323 val = tty_cbreak (STDIN_FILENO);
324 if (val == -1)
325 return -1;
327 signal (SIGINT, sigint);
329 val = main_loop ();
331 tty_reset (STDIN_FILENO);
333 if (val == -1)
334 return -1;
336 return 0;