* mipsread.c (read_alphacoff_dynamic_symtab): Replace alloca calls
[deliverable/binutils-gdb.git] / gdb / ser-mac.c
CommitLineData
c906108c 1/* Remote serial interface for local (hardwired) serial ports for Macintosh.
d9fcf2fb 2 Copyright 1994, 2000 Free Software Foundation, Inc.
c906108c
SS
3 Contributed by Cygnus Support. Written by Stan Shebs.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
c5aa993b
JM
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
c906108c
SS
21
22#include "defs.h"
23#include "serial.h"
24
25#include <Types.h>
26#include <Devices.h>
27/* This is the regular Mac Serial.h, but copied to a different name
28 so as not to get confused with the GDB serial.h above. */
29#include "MacSerial.h"
30
31/* This is unused for now. We just return a placeholder. */
32
33struct mac_ttystate
34 {
35 int bogus;
36 };
37
a14ed312
KB
38static int mac_open (serial_t scb, const char *name);
39static void mac_raw (serial_t scb);
40static int mac_readchar (serial_t scb, int timeout);
41static int mac_setbaudrate (serial_t scb, int rate);
42static int mac_write (serial_t scb, const char *str, int len);
43static void mac_close (serial_t scb);
44static serial_ttystate mac_get_tty_state (serial_t scb);
45static int mac_set_tty_state (serial_t scb, serial_ttystate state);
46static char *aptr (short p);
c906108c
SS
47
48short input_refnum;
49short output_refnum;
50
51char *mac_input_buffer;
52char *mac_output_buffer;
53
54static int
fba45db2 55mac_open (serial_t scb, const char *name)
c906108c
SS
56{
57 OSErr err;
58
59 /* Alloc buffer space first - that way any allocation failures are
60 intercepted before the serial driver gets involved. */
61 if (mac_input_buffer == NULL)
62 mac_input_buffer = (char *) xmalloc (4096);
63 /* Match on a name and open a port. */
64 if (strcmp (name, "modem") == 0)
65 {
66 err = OpenDriver ("\p.AIn", &input_refnum);
67 if (err != 0)
68 {
69 return (-1);
70 }
71 err = OpenDriver ("\p.AOut", &output_refnum);
72 if (err != 0)
73 {
74 CloseDriver (input_refnum);
75 return (-1);
76 }
77 }
78 else if (strcmp (name, "printer") == 0)
79 {
80 err = OpenDriver ("\p.BIn", &input_refnum);
81 if (err != 0)
82 {
83 return (-1);
84 }
85 err = OpenDriver ("\p.BOut", &output_refnum);
86 if (err != 0)
87 {
88 CloseDriver (input_refnum);
89 return (-1);
90 }
91 /* fake */
92 scb->fd = 1;
93 return 0;
94 }
95 else
96 {
97 error ("You must specify a valid serial port name; your choices are `modem' or `printer'.");
98 errno = ENOENT;
99 return (-1);
100 }
101 /* We got something open. */
c5aa993b 102 if (1 /* using custom buffer */ )
c906108c
SS
103 SerSetBuf (input_refnum, mac_input_buffer, 4096);
104 /* Set to a GDB-preferred state. */
c5aa993b
JM
105 SerReset (input_refnum, stop10 | noParity | data8 | baud9600);
106 SerReset (output_refnum, stop10 | noParity | data8 | baud9600);
c906108c
SS
107 {
108 CntrlParam cb;
109 struct SerShk *handshake;
110
111 cb.ioCRefNum = output_refnum;
112 cb.csCode = 14;
113 handshake = (struct SerShk *) &cb.csParam[0];
114 handshake->fXOn = 0;
115 handshake->fCTS = 0;
116 handshake->xOn = 0;
117 handshake->xOff = 0;
118 handshake->errs = 0;
119 handshake->evts = 0;
120 handshake->fInX = 0;
121 handshake->fDTR = 0;
c5aa993b 122 err = PBControl ((ParmBlkPtr) & cb, 0);
c906108c
SS
123 if (err < 0)
124 return (-1);
125 }
126 /* fake */
127 scb->fd = 1;
128 return 0;
129}
130
131static int
fba45db2 132mac_noop (serial_t scb)
c906108c
SS
133{
134 return 0;
135}
136
137static void
fba45db2 138mac_raw (serial_t scb)
c906108c
SS
139{
140 /* Always effectively in raw mode. */
141}
142
143/* Read a character with user-specified timeout. TIMEOUT is number of seconds
144 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
145 char if successful. Returns -2 if timeout expired, EOF if line dropped
146 dead, or -3 for any other error (see errno in that case). */
147
148static int
fba45db2 149mac_readchar (serial_t scb, int timeout)
c906108c
SS
150{
151 int status, n;
152 /* time_t */ unsigned long start_time, now;
153 OSErr err;
154 CntrlParam cb;
155 IOParam pb;
156
157 if (scb->bufcnt-- > 0)
158 return *scb->bufp++;
159
160 time (&start_time);
161
162 while (1)
163 {
164 cb.ioCRefNum = input_refnum;
165 cb.csCode = 2;
c5aa993b 166 err = PBStatus ((ParmBlkPtr) & cb, 0);
c906108c
SS
167 if (err < 0)
168 return SERIAL_ERROR;
169 n = *((long *) &cb.csParam[0]);
170 if (n > 0)
171 {
172 pb.ioRefNum = input_refnum;
173 pb.ioBuffer = (Ptr) (scb->buf);
174 pb.ioReqCount = (n > 64 ? 64 : n);
c5aa993b 175 err = PBRead ((ParmBlkPtr) & pb, 0);
c906108c
SS
176 if (err < 0)
177 return SERIAL_ERROR;
178 scb->bufcnt = pb.ioReqCount;
179 scb->bufcnt--;
180 scb->bufp = scb->buf;
181 return *scb->bufp++;
182 }
183 else if (timeout == 0)
184 return SERIAL_TIMEOUT;
185 else if (timeout == -1)
186 ;
187 else
188 {
189 time (&now);
190 if (now > start_time + timeout)
191 return SERIAL_TIMEOUT;
192 }
193 PROGRESS (1);
194 }
195}
196
197/* mac_{get set}_tty_state() are both dummys to fill out the function
198 vector. Someday, they may do something real... */
199
200static serial_ttystate
fba45db2 201mac_get_tty_state (serial_t scb)
c906108c
SS
202{
203 struct mac_ttystate *state;
204
205 state = (struct mac_ttystate *) xmalloc (sizeof *state);
206
207 return (serial_ttystate) state;
208}
209
210static int
fba45db2 211mac_set_tty_state (serial_t scb, serial_ttystate ttystate)
c906108c
SS
212{
213 return 0;
214}
215
216static int
fba45db2
KB
217mac_noflush_set_tty_state (serial_t scb, serial_ttystate new_ttystate,
218 serial_ttystate old_ttystate)
c906108c
SS
219{
220 return 0;
221}
222
223static void
c2c6d25f
JM
224mac_print_tty_state (serial_t scb,
225 serial_ttystate ttystate,
d9fcf2fb 226 struct ui_file *stream)
c906108c
SS
227{
228 /* Nothing to print. */
229 return;
230}
231
232/* If there is a tricky formula to relate real baud rates
233 to what the serial driver wants, we should use it. Until
234 we get one, this table will have to do. */
235
c5aa993b
JM
236static struct
237{
c906108c
SS
238 int real_rate;
239 int bits;
c5aa993b
JM
240}
241mac_baud_rate_table[] =
242{
243 {
244 57600, baud57600
245 }
246 ,
247 {
248 38400, 1
249 }
250 ,
251 {
252 19200, baud19200
253 }
254 ,
255 {
256 9600, baud9600
257 }
258 ,
259 {
260 7200, baud7200
261 }
262 ,
263 {
264 4800, baud4800
265 }
266 ,
267 {
268 3600, baud3600
269 }
270 ,
271 {
272 2400, baud2400
273 }
274 ,
275 {
276 1800, baud1800
277 }
278 ,
279 {
280 1200, baud1200
281 }
282 ,
283 {
284 600, baud600
285 }
286 ,
287 {
288 300, baud300
289 }
290 ,
291 {
292 0, 0
293 }
c906108c
SS
294};
295
296static int
fba45db2 297mac_set_baud_rate (serial_t scb, int rate)
c906108c
SS
298{
299 int i, bits;
300
301 for (i = 0; mac_baud_rate_table[i].real_rate != 0; ++i)
302 {
303 if (mac_baud_rate_table[i].real_rate == rate)
304 {
305 bits = mac_baud_rate_table[i].bits;
306 break;
307 }
308 }
c5aa993b
JM
309 SerReset (input_refnum, stop10 | noParity | data8 | bits);
310 SerReset (output_refnum, stop10 | noParity | data8 | bits);
c906108c
SS
311}
312
313static int
fba45db2 314mac_set_stop_bits (serial_t scb, int num)
c906108c
SS
315{
316 return 0;
317}
318
319int first_mac_write = 0;
320
321static int
fba45db2 322mac_write (serial_t scb, const char *str, int len)
c906108c
SS
323{
324 OSErr err;
325 IOParam pb;
326
327 if (first_mac_write++ < 4)
328 {
329 sleep (1);
330 }
331 pb.ioRefNum = output_refnum;
332 pb.ioBuffer = (Ptr) str;
333 pb.ioReqCount = len;
c5aa993b 334 err = PBWrite ((ParmBlkPtr) & pb, 0);
c906108c
SS
335 if (err < 0)
336 {
337 return 1;
338 }
339 return 0;
340}
341
342static void
343mac_close (serial_t scb)
344{
345 if (input_refnum)
346 {
c5aa993b 347 if (1 /* custom buffer */ )
c906108c
SS
348 SerSetBuf (input_refnum, mac_input_buffer, 0);
349 CloseDriver (input_refnum);
350 input_refnum = 0;
351 }
352 if (output_refnum)
353 {
c5aa993b 354 if (0 /* custom buffer */ )
c906108c
SS
355 SerSetBuf (input_refnum, mac_output_buffer, 0);
356 CloseDriver (output_refnum);
357 output_refnum = 0;
358 }
359}
360
361static struct serial_ops mac_ops =
362{
363 "hardwire",
364 0,
365 mac_open,
366 mac_close,
367 mac_readchar,
368 mac_write,
369 mac_noop, /* flush output */
370 mac_noop, /* flush input */
371 mac_noop, /* send break -- currently only for nindy */
372 mac_raw,
373 mac_get_tty_state,
374 mac_set_tty_state,
375 mac_print_tty_state,
376 mac_noflush_set_tty_state,
377 mac_set_baud_rate,
378 mac_set_stop_bits,
379 mac_noop, /* wait for output to drain */
380};
381
382void
fba45db2 383_initialize_ser_mac (void)
c906108c
SS
384{
385 serial_add_interface (&mac_ops);
386}
This page took 0.133327 seconds and 4 git commands to generate.