* {ser-bsd.c, ser-termios.c} (serial_close): Pass address of
[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 struct sgttyb sgttyb;
73
74 desc = open (name, O_RDWR);
75 if (desc < 0)
76 error("Open of %s failed: %s", name, safe_strerror(errno));
77
78 serial_raw(desc, &oldstate);
79
80 /* Setup constant stuff for select */
81
82 FD_ZERO(&readfds);
83
84 return desc;
85 }
86
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). */
91
92 int
93 serial_readchar(timeout)
94 int timeout;
95 {
96 static unsigned char buf[BUFSIZ];
97 static unsigned char *bufp;
98 static int bufcnt = 0;
99 int numfds;
100 struct timeval tv;
101
102 if (bufcnt-- > 0)
103 return *bufp++;
104
105 tv.tv_sec = timeout;
106 tv.tv_usec = 0;
107
108 FD_SET(desc, &readfds);
109
110 if (timeout >= 0)
111 numfds = select(desc+1, &readfds, 0, 0, &tv);
112 else
113 numfds = select(desc+1, &readfds, 0, 0, 0);
114
115 if (numfds <= 0)
116 if (numfds == 0)
117 return -2; /* Timeout */
118 else
119 return -3; /* Got an error from select */
120
121 bufcnt = read(desc, buf, BUFSIZ);
122
123 if (bufcnt <= 0)
124 if (bufcnt == 0)
125 return EOF; /* 0 chars means end of file */
126 else
127 return -3; /* Got an error from read */
128
129 bufcnt--;
130 bufp = buf;
131 return *bufp++;
132 }
133
134 #ifndef B19200
135 #define B19200 EXTA
136 #endif
137
138 #ifndef B38400
139 #define B38400 EXTB
140 #endif
141
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. */
144
145 static struct
146 {
147 int rate;
148 int code;
149 } baudtab[] = {
150 {50, B50},
151 {75, B75},
152 {110, B110},
153 {134, B134},
154 {150, B150},
155 {200, B200},
156 {300, B300},
157 {600, B600},
158 {1200, B1200},
159 {1800, B1800},
160 {2400, B2400},
161 {4800, B4800},
162 {9600, B9600},
163 {19200, B19200},
164 {38400, B38400},
165 {-1, -1},
166 };
167
168 static int
169 rate_to_code(rate)
170 int rate;
171 {
172 int i;
173
174 for (i = 0; baudtab[i].rate != -1; i++)
175 if (rate == baudtab[i].rate)
176 return baudtab[i].code;
177
178 return -1;
179 }
180
181 int
182 serial_setbaudrate(rate)
183 int rate;
184 {
185 struct sgttyb sgttyb;
186
187 if (ioctl(desc, TIOCGETP, &sgttyb))
188 error("TIOCGETP failed: %s\n", safe_strerror(errno));
189
190 sgttyb.sg_ospeed = rate_to_code(rate);
191 sgttyb.sg_ispeed = rate_to_code(rate);
192
193 if (ioctl(desc, TIOCSETP, &sgttyb))
194 error("TIOCSETP failed: %s\n", safe_strerror(errno));
195
196 return 1;
197 }
198
199 int
200 serial_write(str, len)
201 const char *str;
202 int len;
203 {
204 int cc;
205
206 while (len > 0)
207 {
208 cc = write(desc, str, len);
209
210 if (cc < 0)
211 return 0;
212 len -= cc;
213 str += cc;
214 }
215 return 1;
216 }
217
218 void
219 serial_close()
220 {
221 if (desc < 0)
222 return;
223
224 serial_restore(desc, &oldstate);
225
226 close(desc);
227 desc = -1;
228 }
This page took 0.033279 seconds and 4 git commands to generate.