1 /* Generic serial interface routines
2 Copyright 1992, 1993 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 #include "gdb_string.h"
24 /* Linked list of serial I/O handlers */
26 static struct serial_ops *serial_ops_list = NULL;
28 /* This is the last serial stream opened. Used by connect command. */
30 static serial_t last_serial_opened = NULL;
32 /* Pointer to list of scb's. */
34 static serial_t scb_base;
36 static struct serial_ops *
37 serial_interface_lookup (name)
40 struct serial_ops *ops;
42 for (ops = serial_ops_list; ops; ops = ops->next)
43 if (strcmp (name, ops->name) == 0)
50 serial_add_interface(optable)
51 struct serial_ops *optable;
53 optable->next = serial_ops_list;
54 serial_ops_list = optable;
57 /* Open up a device or a network socket, depending upon the syntax of NAME. */
64 struct serial_ops *ops;
66 for (scb = scb_base; scb; scb = scb->next)
67 if (scb->name && strcmp (scb->name, name) == 0)
73 if (strcmp (name, "pc") == 0)
74 ops = serial_interface_lookup ("pc");
75 else if (strchr (name, ':'))
76 ops = serial_interface_lookup ("tcp");
78 ops = serial_interface_lookup ("hardwire");
83 scb = (serial_t)xmalloc (sizeof (struct _serial_t));
90 if (scb->ops->open(scb, name))
96 scb->name = strsave (name);
101 last_serial_opened = scb;
111 struct serial_ops *ops;
113 for (scb = scb_base; scb; scb = scb->next)
120 ops = serial_interface_lookup ("hardwire");
125 scb = (serial_t)xmalloc (sizeof (struct _serial_t));
130 scb->bufp = scb->buf;
135 scb->next = scb_base;
139 last_serial_opened = scb;
145 serial_close(scb, really_close)
151 last_serial_opened = NULL;
153 /* This is bogus. It's not our fault if you pass us a bad scb...! Rob, you
154 should fix your code instead. */
164 scb->ops->close (scb);
170 scb_base = scb_base->next;
172 for (tmp_scb = scb_base; tmp_scb; tmp_scb = tmp_scb->next)
174 if (tmp_scb->next != scb)
177 tmp_scb->next = tmp_scb->next->next;
186 The connect command is #if 0 because I hadn't thought of an elegant
187 way to wait for I/O on two serial_t's simultaneously. Two solutions
190 1) Fork, and have have one fork handle the to user direction,
191 and have the other hand the to target direction. This
192 obviously won't cut it for MSDOS.
194 2) Use something like select. This assumes that stdin and
195 the target side can both be waited on via the same
196 mechanism. This may not be true for DOS, if GDB is
197 talking to the target via a TCP socket.
201 /* Connect the user directly to the remote system. This command acts just like
202 the 'cu' or 'tip' command. Use <CR>~. or <CR>~^D to break out. */
204 static serial_t tty_desc; /* Controlling terminal */
207 cleanup_tty(ttystate)
208 serial_ttystate ttystate;
210 printf_unfiltered ("\r\n[Exiting connect mode]\r\n");
211 SERIAL_SET_TTY_STATE (tty_desc, ttystate);
213 SERIAL_CLOSE (tty_desc);
217 connect_command (args, fromtty)
223 serial_ttystate ttystate;
224 serial_t port_desc; /* TTY port */
229 fprintf_unfiltered(gdb_stderr, "This command takes no args. They have been ignored.\n");
231 printf_unfiltered("[Entering connect mode. Use ~. or ~^D to escape]\n");
233 tty_desc = SERIAL_FDOPEN (0);
234 port_desc = last_serial_opened;
236 ttystate = SERIAL_GET_TTY_STATE (tty_desc);
238 SERIAL_RAW (tty_desc);
239 SERIAL_RAW (port_desc);
241 make_cleanup (cleanup_tty, ttystate);
247 mask = SERIAL_WAIT_2 (tty_desc, port_desc, -1);
255 c = SERIAL_READCHAR(tty_desc, 0);
257 if (c == SERIAL_TIMEOUT)
261 perror_with_name("connect");
264 SERIAL_WRITE(port_desc, &cx, 1);
279 if (c == '.' || c == '\004')
293 c = SERIAL_READCHAR(port_desc, 0);
295 if (c == SERIAL_TIMEOUT)
299 perror_with_name("connect");
303 SERIAL_WRITE(tty_desc, &cx, 1);
312 #ifdef ANSI_PROTOTYPES
313 serial_printf (serial_t desc, const char *format, ...)
315 serial_printf (va_alist)
321 #ifdef ANSI_PROTOTYPES
322 va_start (args, format);
328 desc = va_arg (args, serial_t);
329 format = va_arg (args, char *);
332 vasprintf (&buf, format, args);
333 SERIAL_WRITE (desc, buf, strlen (buf));
340 _initialize_serial ()
343 add_com ("connect", class_obscure, connect_command,
344 "Connect the terminal directly up to the command monitor.\n\
345 Use <CR>~. or <CR>~^D to break out.");