* Makefile.in (VERSION): Bump to 4.7.4.
[deliverable/binutils-gdb.git] / gdb / ser-bsd.c
1 /* Remote serial interface for OS's with sgttyb
2 Copyright 1992 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 <sys/types.h>
22 #include <sys/time.h>
23 #include <fcntl.h>
24 #include "serial.h"
25
26 static int desc = -1;
27
28 void
29 serial_raw(fd, oldstate)
30 int fd;
31 struct ttystate *oldstate;
32 {
33 struct sgttyb sgttyb;
34
35 oldstate->flags = fcntl(fd, F_GETFL, 0);
36
37 fcntl(fd, F_SETFL, oldstate->flags|FNDELAY);
38
39 if (ioctl(fd, TIOCGETP, &sgttyb))
40 {
41 fprintf(stderr, "TIOCGETP failed: %s\n", safe_strerror(errno));
42 }
43
44 oldstate->sgttyb = sgttyb;
45
46 sgttyb.sg_flags = RAW;
47
48 if (ioctl(fd, TIOCSETP, &sgttyb))
49 {
50 fprintf(stderr, "TIOCSETP failed: %s\n", safe_strerror(errno));
51 }
52 }
53
54 void
55 serial_restore(fd, oldstate)
56 int fd;
57 struct ttystate *oldstate;
58 {
59 fcntl(fd, F_SETFL, oldstate->flags);
60
61 ioctl(fd, TIOCSETP, &oldstate->sgttyb);
62 }
63
64 static struct ttystate oldstate;
65
66 static fd_set readfds;
67
68 int
69 serial_open(name)
70 const char *name;
71 {
72 desc = open (name, O_RDWR);
73 if (desc < 0)
74 error("Open of %s failed: %s", name, safe_strerror(errno));
75
76 serial_raw(desc, &oldstate);
77
78 /* Setup constant stuff for select */
79
80 FD_ZERO(&readfds);
81
82 return desc;
83 }
84
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). */
89
90 int
91 serial_readchar(timeout)
92 int timeout;
93 {
94 static unsigned char buf[BUFSIZ];
95 static unsigned char *bufp;
96 static int bufcnt = 0;
97 int numfds;
98 struct timeval tv;
99
100 if (bufcnt-- > 0)
101 return *bufp++;
102
103 tv.tv_sec = timeout;
104 tv.tv_usec = 0;
105
106 FD_SET(desc, &readfds);
107
108 if (timeout >= 0)
109 numfds = select(desc+1, &readfds, 0, 0, &tv);
110 else
111 numfds = select(desc+1, &readfds, 0, 0, 0);
112
113 if (numfds <= 0)
114 if (numfds == 0)
115 return -2; /* Timeout */
116 else
117 return -3; /* Got an error from select */
118
119 bufcnt = read(desc, buf, BUFSIZ);
120
121 if (bufcnt <= 0)
122 if (bufcnt == 0)
123 return EOF; /* 0 chars means end of file */
124 else
125 return -3; /* Got an error from read */
126
127 bufcnt--;
128 bufp = buf;
129 return *bufp++;
130 }
131
132 #ifndef B19200
133 #define B19200 EXTA
134 #endif
135
136 #ifndef B38400
137 #define B38400 EXTB
138 #endif
139
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. */
142
143 static struct
144 {
145 int rate;
146 int code;
147 } baudtab[] = {
148 {50, B50},
149 {75, B75},
150 {110, B110},
151 {134, B134},
152 {150, B150},
153 {200, B200},
154 {300, B300},
155 {600, B600},
156 {1200, B1200},
157 {1800, B1800},
158 {2400, B2400},
159 {4800, B4800},
160 {9600, B9600},
161 {19200, B19200},
162 {38400, B38400},
163 {-1, -1},
164 };
165
166 static int
167 rate_to_code(rate)
168 int rate;
169 {
170 int i;
171
172 for (i = 0; baudtab[i].rate != -1; i++)
173 if (rate == baudtab[i].rate)
174 return baudtab[i].code;
175
176 return -1;
177 }
178
179 int
180 serial_setbaudrate(rate)
181 int rate;
182 {
183 struct sgttyb sgttyb;
184
185 if (ioctl(desc, TIOCGETP, &sgttyb))
186 error("TIOCGETP failed: %s\n", safe_strerror(errno));
187
188 sgttyb.sg_ospeed = rate_to_code(rate);
189 sgttyb.sg_ispeed = rate_to_code(rate);
190
191 if (ioctl(desc, TIOCSETP, &sgttyb))
192 error("TIOCSETP failed: %s\n", safe_strerror(errno));
193
194 return 1;
195 }
196
197 int
198 serial_write(str, len)
199 const char *str;
200 int len;
201 {
202 int cc;
203
204 while (len > 0)
205 {
206 cc = write(desc, str, len);
207
208 if (cc < 0)
209 return 0;
210 len -= cc;
211 str += cc;
212 }
213 return 1;
214 }
215
216 void
217 serial_close()
218 {
219 if (desc < 0)
220 return;
221
222 serial_restore(desc, &oldstate);
223
224 close(desc);
225 desc = -1;
226 }
This page took 0.037656 seconds and 4 git commands to generate.