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