1 /* Serial interface for a pipe to a separate program
2 Copyright 1999 Free Software Foundation, Inc.
4 Contributed by Cygnus Solutions.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
25 #include <sys/types.h>
27 #include <sys/socket.h>
35 #include "gdb_string.h"
37 extern int (*ui_loop_hook) PARAMS ((int));
39 static int pipe_open PARAMS ((serial_t scb, const char *name));
40 static void pipe_raw PARAMS ((serial_t scb));
41 static int wait_for PARAMS ((serial_t scb, int timeout));
42 static int pipe_readchar PARAMS ((serial_t scb, int timeout));
43 static int pipe_setbaudrate PARAMS ((serial_t scb, int rate));
44 static int pipe_setstopbits PARAMS ((serial_t scb, int num));
45 static int pipe_write PARAMS ((serial_t scb, const char *str, int len));
46 /* FIXME: static void pipe_restore PARAMS ((serial_t scb)); */
47 static void pipe_close PARAMS ((serial_t scb));
48 static serial_ttystate pipe_get_tty_state PARAMS ((serial_t scb));
49 static int pipe_set_tty_state PARAMS ((serial_t scb, serial_ttystate state));
50 static int pipe_return_0 PARAMS ((serial_t));
51 static int pipe_noflush_set_tty_state PARAMS ((serial_t, serial_ttystate,
53 static void pipe_print_tty_state PARAMS ((serial_t, serial_ttystate));
55 extern void _initialize_ser_pipe PARAMS ((void));
57 /* Open up a raw pipe */
64 #if !defined(O_NONBLOCK) || !defined(F_GETFL) || !defined(F_SETFL)
67 #if defined (__NetBSD__) || defined (__FreeBSD__)
69 /* check the BSD popen sources for where "r+" comes from :-) */
71 stream = popen (name + 1, "r+");
74 fprintf_unfiltered (gdb_stderr, "%s: popen failed\n", name + 1);
77 scb->ttystate = stream; /* borrow that space */
78 scb->fd = fileno (stream);
84 /* Copyright (c) 1988, 1993
85 * The Regents of the University of California. All rights reserved.
87 * This code is derived from software written by Ken Arnold and
88 * published in UNIX Review, Vol. 6, No. 8.
92 if (socketpair (AF_UNIX, SOCK_STREAM, 0, pdes) < 0)
95 switch (pid = vfork ())
103 /* POSIX.2 B.3.2.2 "popen() shall ensure that any streams
104 from previous popen() calls that remain open in the
105 parent process are closed in the new child process. */
106 for (old = pidlist; old; old = old->next)
107 close (fileno (old->fp)); /* don't allow a flush */
110 if (pdes[1] != STDOUT_FILENO)
112 dup2 (pdes[1], STDOUT_FILENO);
115 dup2 (STDOUT_FILENO, STDIN_FILENO);
116 execl ("/bin/sh", "sh", "-c", name + 1, NULL);
120 /* Parent; assume fdopen can't fail. */
123 scb->ttystate = NULL;
126 /* Make it non-blocking */
128 int flags = fcntl (scb->fd, F_GETFL, 0);
129 if (fcntl (scb->fd, F_SETFL, flags | O_NONBLOCK) < 0)
137 /* If we don't do this, GDB simply exits when the remote side dies. */
138 signal (SIGPIPE, SIG_IGN);
143 static serial_ttystate
144 pipe_get_tty_state (scb)
148 return xmalloc (sizeof (int));
152 pipe_set_tty_state (scb, ttystate)
154 serial_ttystate ttystate;
170 return; /* Always in raw mode */
173 /* Wait for input on scb, with timeout seconds. Returns 0 on success,
174 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
176 For termio{s}, we actually just setup VTIME if necessary, and let the
177 timeout occur in the read() in pipe_read().
181 wait_for (scb, timeout)
187 fd_set readfds, exceptfds;
190 FD_ZERO (&exceptfds);
195 FD_SET (scb->fd, &readfds);
196 FD_SET (scb->fd, &exceptfds);
201 numfds = select (scb->fd + 1, &readfds, 0, &exceptfds, &tv);
203 numfds = select (scb->fd + 1, &readfds, 0, &exceptfds, 0);
208 return SERIAL_TIMEOUT;
209 else if (errno == EINTR)
212 return SERIAL_ERROR; /* Got an error from select or poll */
219 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
220 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
221 char if successful. Returns -2 if timeout expired, EOF if line dropped
222 dead, or -3 for any other error (see errno in that case). */
225 pipe_readchar (scb, timeout)
232 if (scb->bufcnt-- > 0)
235 /* We have to be able to keep the GUI alive here, so we break the original
236 timeout into steps of 1 second, running the "keep the GUI alive" hook
237 each time through the loop.
239 Also, timeout = 0 means to poll, so we just set the delta to 0, so we
240 will only go through the loop once. */
242 delta = (timeout == 0 ? 0 : 1);
246 /* N.B. The UI may destroy our world (for instance by calling
247 remote_stop,) in which case we want to get out of here as
248 quickly as possible. It is not safe to touch scb, since
249 someone else might have freed it. The ui_loop_hook signals that
250 we should exit by returning 1. */
254 if (ui_loop_hook (0))
255 return SERIAL_TIMEOUT;
258 status = wait_for (scb, delta);
261 /* If we got a character or an error back from wait_for, then we can
262 break from the loop before the timeout is completed. */
264 if (status != SERIAL_TIMEOUT)
269 /* If we have exhausted the original timeout, then generate
270 a SERIAL_TIMEOUT, and pass it out of the loop. */
272 else if (timeout == 0)
274 status == SERIAL_TIMEOUT;
284 scb->bufcnt = read (scb->fd, scb->buf, BUFSIZ);
285 if (scb->bufcnt != -1 || errno != EINTR)
289 if (scb->bufcnt <= 0)
291 if (scb->bufcnt == 0)
292 return SERIAL_TIMEOUT; /* 0 chars means timeout [may need to
293 distinguish between EOF & timeouts
296 return SERIAL_ERROR; /* Got an error from read */
300 scb->bufp = scb->buf;
305 pipe_noflush_set_tty_state (scb, new_ttystate, old_ttystate)
307 serial_ttystate new_ttystate;
308 serial_ttystate old_ttystate;
314 pipe_print_tty_state (scb, ttystate)
316 serial_ttystate ttystate;
318 /* Nothing to print. */
323 pipe_setbaudrate (scb, rate)
327 return 0; /* Never fails! */
331 pipe_setstopbits (scb, num)
335 return 0; /* Never fails! */
339 pipe_write (scb, str, len)
348 cc = write (scb->fd, str, len);
364 if (scb->ttystate != NULL)
365 pclose ((FILE *) scb->ttystate);
368 scb->ttystate = NULL;
372 static struct serial_ops pipe_ops =
380 pipe_return_0, /* flush output */
381 pipe_return_0, /* flush input */
382 pipe_return_0, /* send break */
386 pipe_print_tty_state,
387 pipe_noflush_set_tty_state,
390 pipe_return_0, /* wait for output to drain */
394 _initialize_ser_pipe ()
396 serial_add_interface (&pipe_ops);