* config/m68k/monitor.mt (TDEPFILE): Add remote-es.o.
[deliverable/binutils-gdb.git] / gdb / ser-unix.c
CommitLineData
4e772f44
SG
1/* Serial interface for local (hardwired) serial ports on Un*x like systems
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#include <fcntl.h>
23#include <sys/types.h>
4e772f44
SG
24
25#if !defined (HAVE_TERMIOS) && !defined (HAVE_TERMIO) && !defined (HAVE_SGTTY)
26#define HAVE_SGTTY
27#endif
28
29#ifdef HAVE_TERMIOS
30#include <termios.h>
31#include <unistd.h>
38dc5e12
SG
32
33struct hardwire_ttystate
34{
35 struct termios termios;
36};
dc34b11d 37#endif /* termios */
38dc5e12 38
4e772f44 39#ifdef HAVE_TERMIO
ebd99d54 40#include <termio.h>
38dc5e12 41
dc34b11d
JK
42/* It is believed that all systems which have added job control to SVR3
43 (e.g. sco) have also added termios. Even if not, trying to figure out
44 all the variations (TIOCGPGRP vs. TCGETPGRP, etc.) would be pretty
45 bewildering. So we don't attempt it. */
46
38dc5e12
SG
47struct hardwire_ttystate
48{
49 struct termio termio;
50};
dc34b11d 51#endif /* termio */
38dc5e12 52
4e772f44 53#ifdef HAVE_SGTTY
68d2db62
JK
54/* Needed for the code which uses select(). We would include <sys/select.h>
55 too if it existed on all systems. */
56#include <sys/time.h>
57
4e772f44 58#include <sgtty.h>
38dc5e12
SG
59
60struct hardwire_ttystate
61{
62 struct sgttyb sgttyb;
c2e247c4
JK
63 struct tchars tc;
64 struct ltchars ltc;
65 /* Line discipline flags. */
66 int lmode;
38dc5e12 67};
dc34b11d 68#endif /* sgtty */
4e772f44 69
9775789d
SG
70static int hardwire_open PARAMS ((serial_t scb, const char *name));
71static void hardwire_raw PARAMS ((serial_t scb));
72static int wait_for PARAMS ((serial_t scb, int timeout));
73static int hardwire_readchar PARAMS ((serial_t scb, int timeout));
74static int rate_to_code PARAMS ((int rate));
75static int hardwire_setbaudrate PARAMS ((serial_t scb, int rate));
76static int hardwire_write PARAMS ((serial_t scb, const char *str, int len));
0ac0a9f6 77/* FIXME: static void hardwire_restore PARAMS ((serial_t scb)); */
9775789d 78static void hardwire_close PARAMS ((serial_t scb));
38dc5e12
SG
79static int get_tty_state PARAMS ((serial_t scb, struct hardwire_ttystate *state));
80static int set_tty_state PARAMS ((serial_t scb, struct hardwire_ttystate *state));
81static serial_ttystate hardwire_get_tty_state PARAMS ((serial_t scb));
82static int hardwire_set_tty_state PARAMS ((serial_t scb, serial_ttystate state));
9775789d 83
4e772f44
SG
84/* Open up a real live device for serial I/O */
85
86static int
87hardwire_open(scb, name)
88 serial_t scb;
89 const char *name;
90{
91 scb->fd = open (name, O_RDWR);
92 if (scb->fd < 0)
4febd102 93 return -1;
4e772f44
SG
94
95 return 0;
96}
97
38dc5e12
SG
98static int
99get_tty_state(scb, state)
4e772f44 100 serial_t scb;
38dc5e12 101 struct hardwire_ttystate *state;
4e772f44
SG
102{
103#ifdef HAVE_TERMIOS
057c2f47 104 extern int errno;
c2e247c4
JK
105
106 if (tcgetattr(scb->fd, &state->termios) < 0)
107 return -1;
108
c2e247c4 109 return 0;
38dc5e12 110#endif
4e772f44 111
38dc5e12 112#ifdef HAVE_TERMIO
c2e247c4
JK
113 if (ioctl (scb->fd, TCGETA, &state->termio) < 0)
114 return -1;
dc34b11d 115 return 0;
38dc5e12 116#endif
4e772f44 117
38dc5e12 118#ifdef HAVE_SGTTY
c2e247c4
JK
119 if (ioctl (scb->fd, TIOCGETP, &state->sgttyb) < 0)
120 return -1;
121 if (ioctl (scb->fd, TIOCGETC, &state->tc) < 0)
122 return -1;
123 if (ioctl (scb->fd, TIOCGLTC, &state->ltc) < 0)
124 return -1;
125 if (ioctl (scb->fd, TIOCLGET, &state->lmode) < 0)
126 return -1;
127
a14a8fad 128 return 0;
38dc5e12
SG
129#endif
130}
4e772f44 131
38dc5e12
SG
132static int
133set_tty_state(scb, state)
134 serial_t scb;
135 struct hardwire_ttystate *state;
136{
38dc5e12 137#ifdef HAVE_TERMIOS
c2e247c4
JK
138 if (tcsetattr(scb->fd, TCSANOW, &state->termios) < 0)
139 return -1;
140
a14a8fad 141 return 0;
4e772f44
SG
142#endif
143
144#ifdef HAVE_TERMIO
c2e247c4
JK
145 if (ioctl (scb->fd, TCSETA, &state->termio) < 0)
146 return -1;
c2e247c4 147 return 0;
38dc5e12 148#endif
4e772f44 149
38dc5e12 150#ifdef HAVE_SGTTY
c2e247c4
JK
151 if (ioctl (scb->fd, TIOCSETN, &state->sgttyb) < 0)
152 return -1;
88cc9a42
RP
153 if (ioctl (scb->fd, TIOCSETC, &state->tc) < 0)
154 return -1;
155 if (ioctl (scb->fd, TIOCSLTC, &state->ltc) < 0)
156 return -1;
157 if (ioctl (scb->fd, TIOCLSET, &state->lmode) < 0)
158 return -1;
c2e247c4 159
a14a8fad 160 return 0;
38dc5e12
SG
161#endif
162}
4e772f44 163
38dc5e12
SG
164static serial_ttystate
165hardwire_get_tty_state(scb)
166 serial_t scb;
167{
168 struct hardwire_ttystate *state;
4e772f44 169
38dc5e12 170 state = (struct hardwire_ttystate *)xmalloc(sizeof *state);
4e772f44 171
38dc5e12
SG
172 if (get_tty_state(scb, state))
173 return NULL;
4e772f44 174
38dc5e12
SG
175 return (serial_ttystate)state;
176}
4e772f44 177
38dc5e12
SG
178static int
179hardwire_set_tty_state(scb, ttystate)
180 serial_t scb;
181 serial_ttystate ttystate;
182{
183 struct hardwire_ttystate *state;
4e772f44 184
38dc5e12
SG
185 state = (struct hardwire_ttystate *)ttystate;
186
187 return set_tty_state(scb, state);
188}
189
c2e247c4
JK
190static int
191hardwire_noflush_set_tty_state (scb, new_ttystate, old_ttystate)
192 serial_t scb;
193 serial_ttystate new_ttystate;
194 serial_ttystate old_ttystate;
195{
3fe11d47 196 struct hardwire_ttystate new_state;
c2e247c4
JK
197 struct hardwire_ttystate *state = (struct hardwire_ttystate *) old_ttystate;
198
3fe11d47
JK
199 new_state = *(struct hardwire_ttystate *)new_ttystate;
200
d881dd86
JK
201 /* Don't change in or out of raw mode; we don't want to flush input.
202 termio and termios have no such restriction; for them flushing input
203 is separate from setting the attributes. */
c2e247c4
JK
204
205#ifdef HAVE_SGTTY
206 if (state->sgttyb.sg_flags & RAW)
207 new_state.sgttyb.sg_flags |= RAW;
208 else
209 new_state.sgttyb.sg_flags &= ~RAW;
210
211 /* I'm not sure whether this is necessary; the manpage just mentions
212 RAW not CBREAK. */
213 if (state->sgttyb.sg_flags & CBREAK)
214 new_state.sgttyb.sg_flags |= CBREAK;
215 else
216 new_state.sgttyb.sg_flags &= ~CBREAK;
217#endif
218
219 return set_tty_state (scb, &new_state);
220}
221
222static void
223hardwire_print_tty_state (scb, ttystate)
224 serial_t scb;
225 serial_ttystate ttystate;
226{
227 struct hardwire_ttystate *state = (struct hardwire_ttystate *) ttystate;
228 int i;
229
dc34b11d 230#ifdef HAVE_TERMIOS
c2e247c4
JK
231 printf_filtered ("c_iflag = 0x%x, c_oflag = 0x%x,\n",
232 state->termios.c_iflag, state->termios.c_oflag);
a77a5278
JK
233 printf_filtered ("c_cflag = 0x%x, c_lflag = 0x%x\n",
234 state->termios.c_cflag, state->termios.c_lflag);
235#if 0
236 /* This not in POSIX, and is not really documented by those systems
237 which have it (at least not Sun). */
238 printf_filtered ("c_line = 0x%x.\n", state->termios.c_line);
239#endif
c2e247c4
JK
240 printf_filtered ("c_cc: ");
241 for (i = 0; i < NCCS; i += 1)
242 printf_filtered ("0x%x ", state->termios.c_cc[i]);
243 printf_filtered ("\n");
244#endif
245
246#ifdef HAVE_TERMIO
247 printf_filtered ("c_iflag = 0x%x, c_oflag = 0x%x,\n",
248 state->termio.c_iflag, state->termio.c_oflag);
249 printf_filtered ("c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n",
250 state->termio.c_cflag, state->termio.c_lflag,
251 state->termio.c_line);
252 printf_filtered ("c_cc: ");
253 for (i = 0; i < NCC; i += 1)
254 printf_filtered ("0x%x ", state->termio.c_cc[i]);
255 printf_filtered ("\n");
256#endif
257
258#ifdef HAVE_SGTTY
259 printf_filtered ("sgttyb.sg_flags = 0x%x.\n", state->sgttyb.sg_flags);
260
261 printf_filtered ("tchars: ");
262 for (i = 0; i < (int)sizeof (struct tchars); i++)
263 printf_filtered ("0x%x ", ((unsigned char *)&state->tc)[i]);
264 printf_filtered ("\n");
265
266 printf_filtered ("ltchars: ");
267 for (i = 0; i < (int)sizeof (struct ltchars); i++)
268 printf_filtered ("0x%x ", ((unsigned char *)&state->ltc)[i]);
269 printf_filtered ("\n");
270
271 printf_filtered ("lmode: 0x%x\n", state->lmode);
272#endif
273}
274
275static int
276hardwire_flush_output (scb)
277 serial_t scb;
278{
279#ifdef HAVE_TERMIOS
280 return tcflush (scb->fd, TCOFLUSH);
281#endif
282
283#ifdef HAVE_TERMIO
284 return ioctl (scb->fd, TCFLSH, 1);
285#endif
286
287#ifdef HAVE_SGTTY
288 /* This flushes both input and output, but we can't do better. */
289 return ioctl (scb->fd, TIOCFLUSH, 0);
290#endif
291}
292
704deef2
JK
293static int
294hardwire_flush_input (scb)
295 serial_t scb;
296{
297#ifdef HAVE_TERMIOS
298 return tcflush (scb->fd, TCIFLUSH);
299#endif
300
301#ifdef HAVE_TERMIO
302 return ioctl (scb->fd, TCFLSH, 0);
303#endif
304
305#ifdef HAVE_SGTTY
306 /* This flushes both input and output, but we can't do better. */
307 return ioctl (scb->fd, TIOCFLUSH, 0);
308#endif
309}
310
311static int
312hardwire_send_break (scb)
313 serial_t scb;
314{
704deef2
JK
315#ifdef HAVE_TERMIOS
316 return tcsendbreak (scb->fd, 0);
317#endif
318
319#ifdef HAVE_TERMIO
320 return ioctl (scb->fd, TCSBRK, 0);
321#endif
322
323#ifdef HAVE_SGTTY
95a98b5e 324 {
0ac0a9f6 325 int status;
95a98b5e
JK
326 struct timeval timeout;
327
328 status = ioctl (scb->fd, TIOCSBRK, 0);
329
330 /* Can't use usleep; it doesn't exist in BSD 4.2. */
331 /* Note that if this select() is interrupted by a signal it will not wait
332 the full length of time. I think that is OK. */
333 timeout.tv_sec = 0;
334 timeout.tv_usec = 250000;
335 select (0, 0, 0, 0, &timeout);
336 status = ioctl (scb->fd, TIOCCBRK, 0);
337 return status;
338 }
704deef2
JK
339#endif
340}
341
38dc5e12
SG
342static void
343hardwire_raw(scb)
344 serial_t scb;
345{
346 struct hardwire_ttystate state;
347
348 if (get_tty_state(scb, &state))
199b2450 349 fprintf_unfiltered(gdb_stderr, "get_tty_state failed: %s\n", safe_strerror(errno));
38dc5e12
SG
350
351#ifdef HAVE_TERMIOS
352 state.termios.c_iflag = 0;
353 state.termios.c_oflag = 0;
354 state.termios.c_lflag = 0;
355 state.termios.c_cflag &= ~(CSIZE|PARENB);
356 state.termios.c_cflag |= CS8;
357 state.termios.c_cc[VMIN] = 0;
358 state.termios.c_cc[VTIME] = 0;
359#endif
360
361#ifdef HAVE_TERMIO
362 state.termio.c_iflag = 0;
363 state.termio.c_oflag = 0;
364 state.termio.c_lflag = 0;
365 state.termio.c_cflag &= ~(CSIZE|PARENB);
366 state.termio.c_cflag |= CS8;
367 state.termio.c_cc[VMIN] = 0;
368 state.termio.c_cc[VTIME] = 0;
369#endif
370
371#ifdef HAVE_SGTTY
372 state.sgttyb.sg_flags |= RAW | ANYP;
373 state.sgttyb.sg_flags &= ~(CBREAK | ECHO);
4e772f44 374#endif
9e15da4a
SG
375
376 scb->current_timeout = 0;
38dc5e12
SG
377
378 if (set_tty_state (scb, &state))
199b2450 379 fprintf_unfiltered(gdb_stderr, "set_tty_state failed: %s\n", safe_strerror(errno));
4e772f44
SG
380}
381
9e15da4a
SG
382/* Wait for input on scb, with timeout seconds. Returns 0 on success,
383 otherwise SERIAL_TIMEOUT or SERIAL_ERROR.
384
385 For termio{s}, we actually just setup VTIME if necessary, and let the
386 timeout occur in the read() in hardwire_read().
387 */
4e772f44
SG
388
389static int
9775789d 390wait_for(scb, timeout)
4e772f44
SG
391 serial_t scb;
392 int timeout;
393{
9db58d3a
JK
394 scb->timeout_remaining = 0;
395
9775789d 396#ifdef HAVE_SGTTY
9db58d3a
JK
397 {
398 struct timeval tv;
399 fd_set readfds;
eca29634 400
9db58d3a 401 FD_ZERO (&readfds);
eca29634 402
9db58d3a
JK
403 tv.tv_sec = timeout;
404 tv.tv_usec = 0;
eca29634 405
9db58d3a 406 FD_SET(scb->fd, &readfds);
eca29634 407
9db58d3a
JK
408 while (1)
409 {
410 int numfds;
411
412 if (timeout >= 0)
413 numfds = select(scb->fd+1, &readfds, 0, 0, &tv);
a037b21e 414 else
9db58d3a 415 numfds = select(scb->fd+1, &readfds, 0, 0, 0);
a037b21e 416
9db58d3a
JK
417 if (numfds <= 0)
418 if (numfds == 0)
419 return SERIAL_TIMEOUT;
420 else if (errno == EINTR)
421 continue;
422 else
423 return SERIAL_ERROR; /* Got an error from select or poll */
9e15da4a 424
9db58d3a
JK
425 return 0;
426 }
427 }
9775789d 428#endif /* HAVE_SGTTY */
4e772f44 429
9775789d 430#if defined HAVE_TERMIO || defined HAVE_TERMIOS
9e15da4a
SG
431 if (timeout == scb->current_timeout)
432 return 0;
4e772f44 433
9db58d3a
JK
434 scb->current_timeout = timeout;
435
9e15da4a 436 {
38dc5e12 437 struct hardwire_ttystate state;
eca29634 438
38dc5e12 439 if (get_tty_state(scb, &state))
199b2450 440 fprintf_unfiltered(gdb_stderr, "get_tty_state failed: %s\n", safe_strerror(errno));
eca29634 441
38dc5e12 442#ifdef HAVE_TERMIOS
864df7e6
JK
443 if (timeout < 0)
444 {
445 /* No timeout. */
446 state.termios.c_cc[VTIME] = 0;
447 state.termios.c_cc[VMIN] = 1;
448 }
449 else
450 {
451 state.termios.c_cc[VMIN] = 0;
452 state.termios.c_cc[VTIME] = timeout * 10;
453 if (state.termios.c_cc[VTIME] != timeout * 10)
454 {
9db58d3a
JK
455
456 /* If c_cc is an 8-bit signed character, we can't go
457 bigger than this. If it is always unsigned, we could use
458 25. */
459
460 scb->current_timeout = 12;
461 state.termios.c_cc[VTIME] = scb->current_timeout * 10;
462 scb->timeout_remaining = timeout - scb->current_timeout;
864df7e6
JK
463 }
464 }
38dc5e12 465#endif
9775789d 466
9e15da4a 467#ifdef HAVE_TERMIO
864df7e6
JK
468 if (timeout < 0)
469 {
470 /* No timeout. */
471 state.termio.c_cc[VTIME] = 0;
472 state.termio.c_cc[VMIN] = 1;
473 }
474 else
475 {
476 state.termio.c_cc[VMIN] = 0;
477 state.termio.c_cc[VTIME] = timeout * 10;
478 if (state.termio.c_cc[VTIME] != timeout * 10)
479 {
9db58d3a
JK
480 /* If c_cc is an 8-bit signed character, we can't go
481 bigger than this. If it is always unsigned, we could use
482 25. */
483
484 scb->current_timeout = 12;
485 state.termios.c_cc[VTIME] = scb->current_timeout * 10;
486 scb->timeout_remaining = timeout - scb->current_timeout;
864df7e6
JK
487 }
488 }
38dc5e12 489#endif
9e15da4a 490
38dc5e12 491 if (set_tty_state (scb, &state))
199b2450 492 fprintf_unfiltered(gdb_stderr, "set_tty_state failed: %s\n", safe_strerror(errno));
9e15da4a 493
9e15da4a
SG
494 return 0;
495 }
496#endif /* HAVE_TERMIO || HAVE_TERMIOS */
9775789d
SG
497}
498
499/* Read a character with user-specified timeout. TIMEOUT is number of seconds
500 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
057c2f47
RP
501 char if successful. Returns SERIAL_TIMEOUT if timeout expired, EOF if line
502 dropped dead, or SERIAL_ERROR for any other error (see errno in that case). */
9775789d
SG
503
504static int
505hardwire_readchar(scb, timeout)
506 serial_t scb;
507 int timeout;
508{
509 int status;
510
511 if (scb->bufcnt-- > 0)
512 return *scb->bufp++;
513
9db58d3a
JK
514 while (1)
515 {
516 status = wait_for (scb, timeout);
517
518 if (status < 0)
519 return status;
520
521 scb->bufcnt = read (scb->fd, scb->buf, BUFSIZ);
522
523 if (scb->bufcnt <= 0)
524 {
525 if (scb->bufcnt == 0)
526 {
527 /* Zero characters means timeout (it could also be EOF, but
528 we don't (yet at least) distinguish). */
529 if (scb->timeout_remaining > 0)
530 {
531 timeout = scb->timeout_remaining;
532 continue;
533 }
534 else
535 return SERIAL_TIMEOUT;
536 }
537 else
538 return SERIAL_ERROR; /* Got an error from read */
539 }
540
541 scb->bufcnt--;
542 scb->bufp = scb->buf;
543 return *scb->bufp++;
544 }
4e772f44
SG
545}
546
547#ifndef B19200
548#define B19200 EXTA
549#endif
550
551#ifndef B38400
552#define B38400 EXTB
553#endif
554
555/* Translate baud rates from integers to damn B_codes. Unix should
556 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
557
558static struct
559{
560 int rate;
561 int code;
562}
563baudtab[] =
564{
565 {50, B50},
566 {75, B75},
567 {110, B110},
568 {134, B134},
569 {150, B150},
570 {200, B200},
571 {300, B300},
572 {600, B600},
573 {1200, B1200},
574 {1800, B1800},
575 {2400, B2400},
576 {4800, B4800},
577 {9600, B9600},
578 {19200, B19200},
579 {38400, B38400},
580 {-1, -1},
581};
582
583static int
584rate_to_code(rate)
585 int rate;
586{
587 int i;
588
589 for (i = 0; baudtab[i].rate != -1; i++)
590 if (rate == baudtab[i].rate)
591 return baudtab[i].code;
592
593 return -1;
594}
595
596static int
597hardwire_setbaudrate(scb, rate)
598 serial_t scb;
599 int rate;
600{
38dc5e12 601 struct hardwire_ttystate state;
4e772f44 602
38dc5e12 603 if (get_tty_state(scb, &state))
4febd102 604 return -1;
4e772f44 605
38dc5e12
SG
606#ifdef HAVE_TERMIOS
607 cfsetospeed (&state.termios, rate_to_code (rate));
608 cfsetispeed (&state.termios, rate_to_code (rate));
4e772f44
SG
609#endif
610
611#ifdef HAVE_TERMIO
4e772f44
SG
612#ifndef CIBAUD
613#define CIBAUD CBAUD
614#endif
615
38dc5e12
SG
616 state.termio.c_cflag &= ~(CBAUD | CIBAUD);
617 state.termio.c_cflag |= rate_to_code (rate);
4e772f44
SG
618#endif
619
620#ifdef HAVE_SGTTY
38dc5e12
SG
621 state.sgttyb.sg_ispeed = rate_to_code (rate);
622 state.sgttyb.sg_ospeed = rate_to_code (rate);
4e772f44 623#endif
38dc5e12
SG
624
625 return set_tty_state (scb, &state);
4e772f44
SG
626}
627
628static int
629hardwire_write(scb, str, len)
630 serial_t scb;
631 const char *str;
632 int len;
633{
634 int cc;
635
636 while (len > 0)
637 {
638 cc = write(scb->fd, str, len);
639
640 if (cc < 0)
641 return 1;
642 len -= cc;
643 str += cc;
644 }
645 return 0;
646}
647
4e772f44
SG
648static void
649hardwire_close(scb)
650 serial_t scb;
651{
652 if (scb->fd < 0)
653 return;
654
4e772f44
SG
655 close(scb->fd);
656 scb->fd = -1;
657}
658
659static struct serial_ops hardwire_ops =
660{
661 "hardwire",
662 0,
663 hardwire_open,
664 hardwire_close,
665 hardwire_readchar,
666 hardwire_write,
c2e247c4 667 hardwire_flush_output,
704deef2
JK
668 hardwire_flush_input,
669 hardwire_send_break,
4e772f44 670 hardwire_raw,
38dc5e12
SG
671 hardwire_get_tty_state,
672 hardwire_set_tty_state,
c2e247c4
JK
673 hardwire_print_tty_state,
674 hardwire_noflush_set_tty_state,
38dc5e12 675 hardwire_setbaudrate,
4e772f44
SG
676};
677
9775789d 678void
4e772f44
SG
679_initialize_ser_hardwire ()
680{
681 serial_add_interface (&hardwire_ops);
682}
This page took 0.116368 seconds and 4 git commands to generate.