* coffread.c (init_stringtab): Fix bug where sizeof(long) != 4.
[deliverable/binutils-gdb.git] / gdb / serial.c
1 /* Generic serial interface routines
2 Copyright 1992, 1993 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
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.
10
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.
15
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., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 #include "defs.h"
21 #include "serial.h"
22
23 /* Linked list of serial I/O handlers */
24
25 static struct serial_ops *serial_ops_list = NULL;
26
27 /* This is the last serial stream opened. Used by connect command. */
28
29 static serial_t last_serial_opened = NULL;
30
31 static struct serial_ops *
32 serial_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
44 void
45 serial_add_interface(optable)
46 struct serial_ops *optable;
47 {
48 optable->next = serial_ops_list;
49 serial_ops_list = optable;
50 }
51
52 /* Open up a device or a network socket, depending upon the syntax of NAME. */
53
54 serial_t
55 serial_open(name)
56 const char *name;
57 {
58 serial_t scb;
59 struct serial_ops *ops;
60
61 if (strchr (name, ':'))
62 ops = serial_interface_lookup ("tcp");
63 else
64 ops = serial_interface_lookup ("hardwire");
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
76 if (scb->ops->open(scb, name))
77 {
78 free (scb);
79 return NULL;
80 }
81
82 last_serial_opened = scb;
83
84 return scb;
85 }
86
87 serial_t
88 serial_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
110 return scb;
111 }
112
113 void
114 serial_close(scb)
115 serial_t scb;
116 {
117 last_serial_opened = NULL;
118
119 scb->ops->close(scb);
120
121 free(scb);
122 }
123
124 #if 0
125 /*
126 The connect command is #if 0 because I hadn't thought of an elegant
127 way to wait for I/O on two serial_t's simultaneously. Two solutions
128 came to mind:
129
130 1) Fork, and have have one fork handle the to user direction,
131 and have the other hand the to target direction. This
132 obviously won't cut it for MSDOS.
133
134 2) Use something like select. This assumes that stdin and
135 the target side can both be waited on via the same
136 mechanism. This may not be true for DOS, if GDB is
137 talking to the target via a TCP socket.
138 -grossman, 8 Jun 93
139 */
140
141 /* Connect the user directly to the remote system. This command acts just like
142 the 'cu' or 'tip' command. Use <CR>~. or <CR>~^D to break out. */
143
144 static serial_t tty_desc; /* Controlling terminal */
145
146 static void
147 cleanup_tty(ttystate)
148 serial_ttystate ttystate;
149 {
150 printf ("\r\n[Exiting connect mode]\r\n");
151 SERIAL_SET_TTY_STATE (tty_desc, ttystate);
152 free (ttystate);
153 SERIAL_CLOSE (tty_desc);
154 }
155
156 static void
157 connect_command (args, fromtty)
158 char *args;
159 int fromtty;
160 {
161 int c;
162 char cur_esc = 0;
163 serial_ttystate ttystate;
164 serial_t port_desc; /* TTY port */
165
166 dont_repeat();
167
168 if (args)
169 fprintf(stderr, "This command takes no args. They have been ignored.\n");
170
171 printf("[Entering connect mode. Use ~. or ~^D to escape]\n");
172
173 tty_desc = SERIAL_FDOPEN (0);
174 port_desc = last_serial_opened;
175
176 ttystate = SERIAL_GET_TTY_STATE (tty_desc);
177
178 SERIAL_RAW (tty_desc);
179 SERIAL_RAW (port_desc);
180
181 make_cleanup (cleanup_tty, ttystate);
182
183 while (1)
184 {
185 int mask;
186
187 mask = SERIAL_WAIT_2 (tty_desc, port_desc, -1);
188
189 if (mask & 2)
190 { /* tty input */
191 char cx;
192
193 while (1)
194 {
195 c = SERIAL_READCHAR(tty_desc, 0);
196
197 if (c == SERIAL_TIMEOUT)
198 break;
199
200 if (c < 0)
201 perror_with_name("connect");
202
203 cx = c;
204 SERIAL_WRITE(port_desc, &cx, 1);
205
206 switch (cur_esc)
207 {
208 case 0:
209 if (c == '\r')
210 cur_esc = c;
211 break;
212 case '\r':
213 if (c == '~')
214 cur_esc = c;
215 else
216 cur_esc = 0;
217 break;
218 case '~':
219 if (c == '.' || c == '\004')
220 return;
221 else
222 cur_esc = 0;
223 }
224 }
225 }
226
227 if (mask & 1)
228 { /* Port input */
229 char cx;
230
231 while (1)
232 {
233 c = SERIAL_READCHAR(port_desc, 0);
234
235 if (c == SERIAL_TIMEOUT)
236 break;
237
238 if (c < 0)
239 perror_with_name("connect");
240
241 cx = c;
242
243 SERIAL_WRITE(tty_desc, &cx, 1);
244 }
245 }
246 }
247 }
248
249 void
250 _initialize_serial ()
251 {
252 add_com ("connect", class_obscure, connect_command,
253 "Connect the terminal directly up to the command monitor.\n\
254 Use <CR>~. or <CR>~^D to break out.");
255 }
256 #endif /* 0 */
This page took 0.033906 seconds and 4 git commands to generate.