move to hosts
[deliverable/binutils-gdb.git] / gdb / serial.c
CommitLineData
4e772f44
SG
1/* Generic serial interface routines
2 Copyright 1992, 1993 Free Software Foundation, Inc.
3
4This file is part of GDB.
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
18Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20#include "defs.h"
21#include "serial.h"
22
38dc5e12 23/* Linked list of serial I/O handlers */
4e772f44
SG
24
25static struct serial_ops *serial_ops_list = NULL;
26
38dc5e12
SG
27/* This is the last serial stream opened. Used by connect command. */
28
29static serial_t last_serial_opened = NULL;
30
4e772f44
SG
31static struct serial_ops *
32serial_interface_lookup (name)
33 char *name;
34{
35 struct serial_ops *ops;
36
37 for (ops = serial_ops_list; ops; ops = ops->next)
38 if (strcmp (name, ops->name) == 0)
39 return ops;
40
41 return NULL;
42}
43
44void
45serial_add_interface(optable)
46 struct serial_ops *optable;
47{
48 optable->next = serial_ops_list;
49 serial_ops_list = optable;
50}
51
38dc5e12
SG
52/* Open up a device or a network socket, depending upon the syntax of NAME. */
53
4e772f44
SG
54serial_t
55serial_open(name)
56 const char *name;
57{
58 serial_t scb;
59 struct serial_ops *ops;
60
38dc5e12
SG
61 if (strchr (name, ':'))
62 ops = serial_interface_lookup ("tcp");
63 else
64 ops = serial_interface_lookup ("hardwire");
4e772f44
SG
65
66 if (!ops)
67 return NULL;
68
69 scb = (serial_t)xmalloc (sizeof (struct _serial_t));
70
71 scb->ops = ops;
72
73 scb->bufcnt = 0;
74 scb->bufp = scb->buf;
75
4febd102 76 if (scb->ops->open(scb, name))
4e772f44
SG
77 {
78 free (scb);
79 return NULL;
80 }
81
38dc5e12
SG
82 last_serial_opened = scb;
83
84 return scb;
85}
86
87serial_t
88serial_fdopen(fd)
89 const int fd;
90{
91 serial_t scb;
92 struct serial_ops *ops;
93
94 ops = serial_interface_lookup ("hardwire");
95
96 if (!ops)
97 return NULL;
98
99 scb = (serial_t)xmalloc (sizeof (struct _serial_t));
100
101 scb->ops = ops;
102
103 scb->bufcnt = 0;
104 scb->bufp = scb->buf;
105
106 scb->fd = fd;
107
108 last_serial_opened = scb;
109
4e772f44
SG
110 return scb;
111}
112
4febd102
SG
113void
114serial_close(scb)
115 serial_t scb;
116{
38dc5e12
SG
117 last_serial_opened = NULL;
118
4febd102
SG
119 scb->ops->close(scb);
120
121 free(scb);
122}
123
4e772f44 124#if 0
38dc5e12 125
4e772f44
SG
126/* Connect the user directly to the remote system. This command acts just like
127 the 'cu' or 'tip' command. Use <CR>~. or <CR>~^D to break out. */
128
38dc5e12
SG
129static serial_t tty_desc; /* Controlling terminal */
130
4e772f44
SG
131static void
132cleanup_tty(ttystate)
38dc5e12 133 serial_ttystate ttystate;
4e772f44 134{
38dc5e12
SG
135 printf ("\r\n[Exiting connect mode]\r\n");
136 SERIAL_SET_TTY_STATE (tty_desc, ttystate);
137 free (ttystate);
138 SERIAL_CLOSE (tty_desc);
4e772f44
SG
139}
140
141static void
142connect_command (args, fromtty)
143 char *args;
144 int fromtty;
145{
4e772f44
SG
146 int c;
147 char cur_esc = 0;
38dc5e12
SG
148 serial_ttystate ttystate;
149 serial_t port_desc; /* TTY port */
4e772f44
SG
150
151 dont_repeat();
152
4e772f44 153 if (args)
38dc5e12 154 fprintf(stderr, "This command takes no args. They have been ignored.\n");
4e772f44
SG
155
156 printf("[Entering connect mode. Use ~. or ~^D to escape]\n");
157
38dc5e12
SG
158 tty_desc = SERIAL_FDOPEN (0);
159 port_desc = last_serial_opened;
4e772f44 160
38dc5e12 161 ttystate = SERIAL_GET_TTY_STATE (tty_desc);
4e772f44 162
38dc5e12
SG
163 SERIAL_RAW (tty_desc);
164 SERIAL_RAW (port_desc);
165
166 make_cleanup (cleanup_tty, ttystate);
4e772f44
SG
167
168 while (1)
169 {
38dc5e12 170 int mask;
4e772f44 171
38dc5e12 172 mask = SERIAL_WAIT_2 (tty_desc, port_desc, -1);
4e772f44 173
38dc5e12
SG
174 if (mask & 2)
175 { /* tty input */
4e772f44
SG
176 char cx;
177
38dc5e12 178 while (1)
4e772f44 179 {
38dc5e12
SG
180 c = SERIAL_READCHAR(tty_desc, 0);
181
182 if (c == SERIAL_TIMEOUT)
183 break;
184
185 if (c < 0)
186 perror_with_name("connect");
187
188 cx = c;
189 SERIAL_WRITE(port_desc, &cx, 1);
190
191 switch (cur_esc)
192 {
193 case 0:
194 if (c == '\r')
195 cur_esc = c;
196 break;
197 case '\r':
198 if (c == '~')
199 cur_esc = c;
200 else
201 cur_esc = 0;
202 break;
203 case '~':
204 if (c == '.' || c == '\004')
205 return;
206 else
207 cur_esc = 0;
208 }
4e772f44
SG
209 }
210 }
211
38dc5e12
SG
212 if (mask & 1)
213 { /* Port input */
214 char cx;
215
4e772f44
SG
216 while (1)
217 {
38dc5e12
SG
218 c = SERIAL_READCHAR(port_desc, 0);
219
220 if (c == SERIAL_TIMEOUT)
221 break;
222
4e772f44 223 if (c < 0)
38dc5e12
SG
224 perror_with_name("connect");
225
226 cx = c;
227
228 SERIAL_WRITE(tty_desc, &cx, 1);
4e772f44 229 }
4e772f44
SG
230 }
231 }
232}
4e772f44 233
4e772f44
SG
234void
235_initialize_serial ()
236{
237 add_com ("connect", class_obscure, connect_command,
238 "Connect the terminal directly up to the command monitor.\n\
239Use <CR>~. or <CR>~^D to break out.");
240}
38dc5e12 241#endif /* 0 */
This page took 0.044022 seconds and 4 git commands to generate.