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;
74 desc = open (name, O_RDWR);
76 error("Open of %s failed: %s", name, safe_strerror(errno));
78 serial_raw(desc, &oldstate);
80 /* Setup constant stuff for select */
87 /* Read a character with user-specified timeout. TIMEOUT is number of seconds
88 to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns
89 char if successful. Returns -2 if timeout expired, EOF if line dropped
90 dead, or -3 for any other error (see errno in that case). */
93 serial_readchar(timeout)
96 static unsigned char buf[BUFSIZ];
97 static unsigned char *bufp;
98 static int bufcnt = 0;
108 FD_SET(desc, &readfds);
111 numfds = select(desc+1, &readfds, 0, 0, &tv);
113 numfds = select(desc+1, &readfds, 0, 0, 0);
117 return -2; /* Timeout */
119 return -3; /* Got an error from select */
121 bufcnt = read(desc, buf, BUFSIZ);
125 return EOF; /* 0 chars means end of file */
127 return -3; /* Got an error from read */
142 /* Translate baud rates from integers to damn B_codes. Unix should
143 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
174 for (i = 0; baudtab[i].rate != -1; i++)
175 if (rate == baudtab[i].rate)
176 return baudtab[i].code;
182 serial_setbaudrate(rate)
185 struct sgttyb sgttyb;
187 if (ioctl(desc, TIOCGETP, &sgttyb))
188 error("TIOCGETP failed: %s\n", safe_strerror(errno));
190 sgttyb.sg_ospeed = rate_to_code(rate);
191 sgttyb.sg_ispeed = rate_to_code(rate);
193 if (ioctl(desc, TIOCSETP, &sgttyb))
194 error("TIOCSETP failed: %s\n", safe_strerror(errno));
200 serial_write(str, len)
208 cc = write(desc, str, len);
224 serial_restore(desc, oldstate);