1 /* Remote serial interface for OS's with sgttyb
2 Copyright 1992 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., 675 Mass Ave, Cambridge, MA 02139, USA. */
21 #include <sys/types.h>
29 serial_raw(fd, oldstate)
31 struct ttystate *oldstate;
35 oldstate->flags = fcntl(fd, F_GETFL, 0);
37 fcntl(fd, F_SETFL, oldstate->flags|FNDELAY);
39 if (ioctl(fd, TIOCGETP, &sgttyb))
41 fprintf(stderr, "TIOCGETP failed: %s\n", safe_strerror(errno));
44 oldstate->sgttyb = sgttyb;
46 sgttyb.sg_flags = RAW;
48 if (ioctl(fd, TIOCSETP, &sgttyb))
50 fprintf(stderr, "TIOCSETP failed: %s\n", safe_strerror(errno));
55 serial_restore(fd, oldstate)
57 struct ttystate *oldstate;
59 fcntl(fd, F_SETFL, oldstate->flags);
61 ioctl(fd, TIOCSETP, &oldstate->sgttyb);
64 static struct ttystate oldstate;
66 static fd_set readfds;
72 desc = open (name, O_RDWR);
74 error("Open of %s failed: %s", name, safe_strerror(errno));
76 serial_raw(desc, &oldstate);
78 /* Setup constant stuff for select */
85 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
86 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
87 char if successful. Returns -2 if timeout expired, EOF if line dropped
88 dead, or -3 for any other error (see errno in that case). */
91 serial_readchar(timeout)
94 static unsigned char buf[BUFSIZ];
95 static unsigned char *bufp;
96 static int bufcnt = 0;
106 FD_SET(desc, &readfds);
109 numfds = select(desc+1, &readfds, 0, 0, &tv);
111 numfds = select(desc+1, &readfds, 0, 0, 0);
115 return -2; /* Timeout */
117 return -3; /* Got an error from select */
119 bufcnt = read(desc, buf, BUFSIZ);
123 return EOF; /* 0 chars means end of file */
125 return -3; /* Got an error from read */
140 /* Translate baud rates from integers to damn B_codes. Unix should
141 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
172 for (i = 0; baudtab[i].rate != -1; i++)
173 if (rate == baudtab[i].rate)
174 return baudtab[i].code;
180 serial_setbaudrate(rate)
183 struct sgttyb sgttyb;
185 if (ioctl(desc, TIOCGETP, &sgttyb))
186 error("TIOCGETP failed: %s\n", safe_strerror(errno));
188 sgttyb.sg_ospeed = rate_to_code(rate);
189 sgttyb.sg_ispeed = rate_to_code(rate);
191 if (ioctl(desc, TIOCSETP, &sgttyb))
192 error("TIOCSETP failed: %s\n", safe_strerror(errno));
198 serial_write(str, len)
206 cc = write(desc, str, len);
222 serial_restore(desc, &oldstate);