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