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 | { | |
224 | struct hardwire_ttystate new_state = | |
225 | *(struct hardwire_ttystate *)new_ttystate; | |
226 | struct hardwire_ttystate *state = (struct hardwire_ttystate *) old_ttystate; | |
227 | ||
228 | #ifdef HAVE_TERMIOS | |
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->termios.c_lflag & ICANON) | |
232 | new_state.termios.c_lflag |= ICANON; | |
233 | else | |
234 | new_state.termios.c_lflag &= ~ICANON; | |
235 | #endif | |
236 | ||
237 | #ifdef HAVE_TERMIO | |
238 | /* I'm not sure whether this is necessary; the manpage makes no mention | |
239 | of discarding input when switching to/from ICANON. */ | |
240 | if (state->termio.c_lflag & ICANON) | |
241 | new_state.termio.c_lflag |= ICANON; | |
242 | else | |
243 | new_state.termio.c_lflag &= ~ICANON; | |
244 | #endif | |
245 | ||
246 | #ifdef HAVE_SGTTY | |
247 | if (state->sgttyb.sg_flags & RAW) | |
248 | new_state.sgttyb.sg_flags |= RAW; | |
249 | else | |
250 | new_state.sgttyb.sg_flags &= ~RAW; | |
251 | ||
252 | /* I'm not sure whether this is necessary; the manpage just mentions | |
253 | RAW not CBREAK. */ | |
254 | if (state->sgttyb.sg_flags & CBREAK) | |
255 | new_state.sgttyb.sg_flags |= CBREAK; | |
256 | else | |
257 | new_state.sgttyb.sg_flags &= ~CBREAK; | |
258 | #endif | |
259 | ||
260 | return set_tty_state (scb, &new_state); | |
261 | } | |
262 | ||
263 | static void | |
264 | hardwire_print_tty_state (scb, ttystate) | |
265 | serial_t scb; | |
266 | serial_ttystate ttystate; | |
267 | { | |
268 | struct hardwire_ttystate *state = (struct hardwire_ttystate *) ttystate; | |
269 | int i; | |
270 | ||
271 | printf_filtered ("Process group = %d\n", state->process_group); | |
272 | ||
273 | #ifdef HAVE_TERMIOS | |
274 | printf_filtered ("c_iflag = 0x%x, c_oflag = 0x%x,\n", | |
275 | state->termios.c_iflag, state->termios.c_oflag); | |
276 | printf_filtered ("c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n", | |
277 | state->termios.c_cflag, state->termios.c_lflag, | |
278 | state->termios.c_line); | |
279 | printf_filtered ("c_cc: "); | |
280 | for (i = 0; i < NCCS; i += 1) | |
281 | printf_filtered ("0x%x ", state->termios.c_cc[i]); | |
282 | printf_filtered ("\n"); | |
283 | #endif | |
284 | ||
285 | #ifdef HAVE_TERMIO | |
286 | printf_filtered ("c_iflag = 0x%x, c_oflag = 0x%x,\n", | |
287 | state->termio.c_iflag, state->termio.c_oflag); | |
288 | printf_filtered ("c_cflag = 0x%x, c_lflag = 0x%x, c_line = 0x%x.\n", | |
289 | state->termio.c_cflag, state->termio.c_lflag, | |
290 | state->termio.c_line); | |
291 | printf_filtered ("c_cc: "); | |
292 | for (i = 0; i < NCC; i += 1) | |
293 | printf_filtered ("0x%x ", state->termio.c_cc[i]); | |
294 | printf_filtered ("\n"); | |
295 | #endif | |
296 | ||
297 | #ifdef HAVE_SGTTY | |
298 | printf_filtered ("sgttyb.sg_flags = 0x%x.\n", state->sgttyb.sg_flags); | |
299 | ||
300 | printf_filtered ("tchars: "); | |
301 | for (i = 0; i < (int)sizeof (struct tchars); i++) | |
302 | printf_filtered ("0x%x ", ((unsigned char *)&state->tc)[i]); | |
303 | printf_filtered ("\n"); | |
304 | ||
305 | printf_filtered ("ltchars: "); | |
306 | for (i = 0; i < (int)sizeof (struct ltchars); i++) | |
307 | printf_filtered ("0x%x ", ((unsigned char *)&state->ltc)[i]); | |
308 | printf_filtered ("\n"); | |
309 | ||
310 | printf_filtered ("lmode: 0x%x\n", state->lmode); | |
311 | #endif | |
312 | } | |
313 | ||
314 | static int | |
315 | hardwire_flush_output (scb) | |
316 | serial_t scb; | |
317 | { | |
318 | #ifdef HAVE_TERMIOS | |
319 | return tcflush (scb->fd, TCOFLUSH); | |
320 | #endif | |
321 | ||
322 | #ifdef HAVE_TERMIO | |
323 | return ioctl (scb->fd, TCFLSH, 1); | |
324 | #endif | |
325 | ||
326 | #ifdef HAVE_SGTTY | |
327 | /* This flushes both input and output, but we can't do better. */ | |
328 | return ioctl (scb->fd, TIOCFLUSH, 0); | |
329 | #endif | |
330 | } | |
331 | ||
38dc5e12 SG |
332 | static void |
333 | hardwire_raw(scb) | |
334 | serial_t scb; | |
335 | { | |
336 | struct hardwire_ttystate state; | |
337 | ||
338 | if (get_tty_state(scb, &state)) | |
339 | fprintf(stderr, "get_tty_state failed: %s\n", safe_strerror(errno)); | |
340 | ||
341 | #ifdef HAVE_TERMIOS | |
342 | state.termios.c_iflag = 0; | |
343 | state.termios.c_oflag = 0; | |
344 | state.termios.c_lflag = 0; | |
345 | state.termios.c_cflag &= ~(CSIZE|PARENB); | |
346 | state.termios.c_cflag |= CS8; | |
347 | state.termios.c_cc[VMIN] = 0; | |
348 | state.termios.c_cc[VTIME] = 0; | |
349 | #endif | |
350 | ||
351 | #ifdef HAVE_TERMIO | |
352 | state.termio.c_iflag = 0; | |
353 | state.termio.c_oflag = 0; | |
354 | state.termio.c_lflag = 0; | |
355 | state.termio.c_cflag &= ~(CSIZE|PARENB); | |
356 | state.termio.c_cflag |= CS8; | |
357 | state.termio.c_cc[VMIN] = 0; | |
358 | state.termio.c_cc[VTIME] = 0; | |
359 | #endif | |
360 | ||
361 | #ifdef HAVE_SGTTY | |
362 | state.sgttyb.sg_flags |= RAW | ANYP; | |
363 | state.sgttyb.sg_flags &= ~(CBREAK | ECHO); | |
4e772f44 | 364 | #endif |
9e15da4a SG |
365 | |
366 | scb->current_timeout = 0; | |
38dc5e12 SG |
367 | |
368 | if (set_tty_state (scb, &state)) | |
369 | fprintf(stderr, "set_tty_state failed: %s\n", safe_strerror(errno)); | |
4e772f44 SG |
370 | } |
371 | ||
9e15da4a SG |
372 | /* Wait for input on scb, with timeout seconds. Returns 0 on success, |
373 | otherwise SERIAL_TIMEOUT or SERIAL_ERROR. | |
374 | ||
375 | For termio{s}, we actually just setup VTIME if necessary, and let the | |
376 | timeout occur in the read() in hardwire_read(). | |
377 | */ | |
4e772f44 SG |
378 | |
379 | static int | |
9775789d | 380 | wait_for(scb, timeout) |
4e772f44 SG |
381 | serial_t scb; |
382 | int timeout; | |
383 | { | |
9775789d | 384 | int numfds; |
4e772f44 | 385 | |
9775789d SG |
386 | #ifdef HAVE_SGTTY |
387 | struct timeval tv; | |
388 | fd_set readfds; | |
eca29634 | 389 | |
9775789d | 390 | FD_ZERO (&readfds); |
eca29634 | 391 | |
9775789d SG |
392 | tv.tv_sec = timeout; |
393 | tv.tv_usec = 0; | |
eca29634 | 394 | |
9775789d | 395 | FD_SET(scb->fd, &readfds); |
eca29634 | 396 | |
a037b21e SG |
397 | while (1) |
398 | { | |
399 | if (timeout >= 0) | |
400 | numfds = select(scb->fd+1, &readfds, 0, 0, &tv); | |
401 | else | |
402 | numfds = select(scb->fd+1, &readfds, 0, 0, 0); | |
403 | ||
404 | if (numfds <= 0) | |
405 | if (numfds == 0) | |
406 | return SERIAL_TIMEOUT; | |
407 | else if (errno == EINTR) | |
408 | continue; | |
409 | else | |
410 | return SERIAL_ERROR; /* Got an error from select or poll */ | |
411 | ||
412 | return 0; | |
413 | } | |
9e15da4a | 414 | |
9775789d | 415 | #endif /* HAVE_SGTTY */ |
4e772f44 | 416 | |
9775789d | 417 | #if defined HAVE_TERMIO || defined HAVE_TERMIOS |
9e15da4a SG |
418 | if (timeout == scb->current_timeout) |
419 | return 0; | |
4e772f44 | 420 | |
9e15da4a | 421 | { |
38dc5e12 | 422 | struct hardwire_ttystate state; |
eca29634 | 423 | |
38dc5e12 SG |
424 | if (get_tty_state(scb, &state)) |
425 | fprintf(stderr, "get_tty_state failed: %s\n", safe_strerror(errno)); | |
eca29634 | 426 | |
38dc5e12 SG |
427 | #ifdef HAVE_TERMIOS |
428 | state.termios.c_cc[VTIME] = timeout * 10; | |
429 | #endif | |
9775789d | 430 | |
9e15da4a | 431 | #ifdef HAVE_TERMIO |
38dc5e12 SG |
432 | state.termio.c_cc[VTIME] = timeout * 10; |
433 | #endif | |
9e15da4a | 434 | |
38dc5e12 | 435 | scb->current_timeout = timeout; |
9e15da4a | 436 | |
38dc5e12 SG |
437 | if (set_tty_state (scb, &state)) |
438 | fprintf(stderr, "set_tty_state failed: %s\n", safe_strerror(errno)); | |
9e15da4a | 439 | |
9e15da4a SG |
440 | return 0; |
441 | } | |
442 | #endif /* HAVE_TERMIO || HAVE_TERMIOS */ | |
9775789d SG |
443 | } |
444 | ||
445 | /* Read a character with user-specified timeout. TIMEOUT is number of seconds | |
446 | to wait, or -1 to wait forever. Use timeout of 0 to effect a poll. Returns | |
447 | char if successful. Returns -2 if timeout expired, EOF if line dropped | |
448 | dead, or -3 for any other error (see errno in that case). */ | |
449 | ||
450 | static int | |
451 | hardwire_readchar(scb, timeout) | |
452 | serial_t scb; | |
453 | int timeout; | |
454 | { | |
455 | int status; | |
456 | ||
457 | if (scb->bufcnt-- > 0) | |
458 | return *scb->bufp++; | |
459 | ||
460 | status = wait_for(scb, timeout); | |
461 | ||
462 | if (status < 0) | |
463 | return status; | |
4e772f44 | 464 | |
9775789d | 465 | scb->bufcnt = read(scb->fd, scb->buf, BUFSIZ); |
4e772f44 SG |
466 | |
467 | if (scb->bufcnt <= 0) | |
468 | if (scb->bufcnt == 0) | |
9e15da4a SG |
469 | return SERIAL_TIMEOUT; /* 0 chars means timeout [may need to |
470 | distinguish between EOF & timeouts | |
471 | someday] */ | |
4e772f44 | 472 | else |
4febd102 | 473 | return SERIAL_ERROR; /* Got an error from read */ |
4e772f44 SG |
474 | |
475 | scb->bufcnt--; | |
476 | scb->bufp = scb->buf; | |
477 | return *scb->bufp++; | |
478 | } | |
479 | ||
480 | #ifndef B19200 | |
481 | #define B19200 EXTA | |
482 | #endif | |
483 | ||
484 | #ifndef B38400 | |
485 | #define B38400 EXTB | |
486 | #endif | |
487 | ||
488 | /* Translate baud rates from integers to damn B_codes. Unix should | |
489 | have outgrown this crap years ago, but even POSIX wouldn't buck it. */ | |
490 | ||
491 | static struct | |
492 | { | |
493 | int rate; | |
494 | int code; | |
495 | } | |
496 | baudtab[] = | |
497 | { | |
498 | {50, B50}, | |
499 | {75, B75}, | |
500 | {110, B110}, | |
501 | {134, B134}, | |
502 | {150, B150}, | |
503 | {200, B200}, | |
504 | {300, B300}, | |
505 | {600, B600}, | |
506 | {1200, B1200}, | |
507 | {1800, B1800}, | |
508 | {2400, B2400}, | |
509 | {4800, B4800}, | |
510 | {9600, B9600}, | |
511 | {19200, B19200}, | |
512 | {38400, B38400}, | |
513 | {-1, -1}, | |
514 | }; | |
515 | ||
516 | static int | |
517 | rate_to_code(rate) | |
518 | int rate; | |
519 | { | |
520 | int i; | |
521 | ||
522 | for (i = 0; baudtab[i].rate != -1; i++) | |
523 | if (rate == baudtab[i].rate) | |
524 | return baudtab[i].code; | |
525 | ||
526 | return -1; | |
527 | } | |
528 | ||
529 | static int | |
530 | hardwire_setbaudrate(scb, rate) | |
531 | serial_t scb; | |
532 | int rate; | |
533 | { | |
38dc5e12 | 534 | struct hardwire_ttystate state; |
4e772f44 | 535 | |
38dc5e12 | 536 | if (get_tty_state(scb, &state)) |
4febd102 | 537 | return -1; |
4e772f44 | 538 | |
38dc5e12 SG |
539 | #ifdef HAVE_TERMIOS |
540 | cfsetospeed (&state.termios, rate_to_code (rate)); | |
541 | cfsetispeed (&state.termios, rate_to_code (rate)); | |
4e772f44 SG |
542 | #endif |
543 | ||
544 | #ifdef HAVE_TERMIO | |
4e772f44 SG |
545 | #ifndef CIBAUD |
546 | #define CIBAUD CBAUD | |
547 | #endif | |
548 | ||
38dc5e12 SG |
549 | state.termio.c_cflag &= ~(CBAUD | CIBAUD); |
550 | state.termio.c_cflag |= rate_to_code (rate); | |
4e772f44 SG |
551 | #endif |
552 | ||
553 | #ifdef HAVE_SGTTY | |
38dc5e12 SG |
554 | state.sgttyb.sg_ispeed = rate_to_code (rate); |
555 | state.sgttyb.sg_ospeed = rate_to_code (rate); | |
4e772f44 | 556 | #endif |
38dc5e12 SG |
557 | |
558 | return set_tty_state (scb, &state); | |
4e772f44 SG |
559 | } |
560 | ||
c2e247c4 JK |
561 | static int |
562 | hardwire_set_process_group (scb, ttystate, group) | |
563 | serial_t scb; | |
564 | serial_ttystate ttystate; | |
565 | int group; | |
566 | { | |
567 | ((struct hardwire_ttystate *)ttystate)->process_group = group; | |
568 | return 0; | |
569 | } | |
570 | ||
4e772f44 SG |
571 | static int |
572 | hardwire_write(scb, str, len) | |
573 | serial_t scb; | |
574 | const char *str; | |
575 | int len; | |
576 | { | |
577 | int cc; | |
578 | ||
579 | while (len > 0) | |
580 | { | |
581 | cc = write(scb->fd, str, len); | |
582 | ||
583 | if (cc < 0) | |
584 | return 1; | |
585 | len -= cc; | |
586 | str += cc; | |
587 | } | |
588 | return 0; | |
589 | } | |
590 | ||
4e772f44 SG |
591 | static void |
592 | hardwire_close(scb) | |
593 | serial_t scb; | |
594 | { | |
595 | if (scb->fd < 0) | |
596 | return; | |
597 | ||
4e772f44 SG |
598 | close(scb->fd); |
599 | scb->fd = -1; | |
600 | } | |
601 | ||
602 | static struct serial_ops hardwire_ops = | |
603 | { | |
604 | "hardwire", | |
605 | 0, | |
606 | hardwire_open, | |
607 | hardwire_close, | |
608 | hardwire_readchar, | |
609 | hardwire_write, | |
c2e247c4 | 610 | hardwire_flush_output, |
4e772f44 | 611 | hardwire_raw, |
38dc5e12 SG |
612 | hardwire_get_tty_state, |
613 | hardwire_set_tty_state, | |
c2e247c4 JK |
614 | hardwire_print_tty_state, |
615 | hardwire_noflush_set_tty_state, | |
38dc5e12 | 616 | hardwire_setbaudrate, |
c2e247c4 | 617 | hardwire_set_process_group |
4e772f44 SG |
618 | }; |
619 | ||
c2e247c4 JK |
620 | int job_control; |
621 | #if defined (HAVE_TERMIOS) | |
622 | #include <unistd.h> | |
623 | #endif | |
624 | ||
625 | /* This is here because this is where we figure out whether we (probably) | |
626 | have job control. Just using job_control only does part of it because | |
627 | setpgid or setpgrp might not exist on a system without job control. | |
628 | It might be considered misplaced (on the other hand, process groups and | |
629 | job control are closely related to ttys). | |
630 | ||
631 | For a more clean implementation, in libiberty, put a setpgid which merely | |
632 | calls setpgrp and a setpgrp which does nothing (any system with job control | |
633 | will have one or the other). */ | |
634 | int | |
635 | gdb_setpgid () | |
636 | { | |
637 | int retval = 0; | |
638 | if (job_control) | |
639 | { | |
640 | #if defined (NEED_POSIX_SETPGID) || defined (HAVE_TERMIOS) | |
641 | /* Do all systems with termios have setpgid? I hope so. */ | |
642 | retval = setpgid (0, 0); | |
643 | #else | |
644 | #if defined (TIOCGPGRP) | |
645 | #if defined(USG) && !defined(SETPGRP_ARGS) | |
646 | retval = setpgrp (); | |
647 | #else | |
648 | retval = setpgrp (getpid (), getpid ()); | |
649 | #endif /* USG */ | |
650 | #endif /* TIOCGPGRP. */ | |
651 | #endif /* NEED_POSIX_SETPGID */ | |
652 | } | |
653 | return retval; | |
654 | } | |
655 | ||
9775789d | 656 | void |
4e772f44 SG |
657 | _initialize_ser_hardwire () |
658 | { | |
659 | serial_add_interface (&hardwire_ops); | |
c2e247c4 JK |
660 | |
661 | /* OK, figure out whether we have job control. */ | |
662 | ||
663 | #if defined (HAVE_TERMIOS) | |
664 | /* Do all systems with termios have the POSIX way of identifying job | |
665 | control? I hope so. */ | |
666 | #ifdef _POSIX_JOB_CONTROL | |
667 | job_control = _POSIX_JOB_CONTROL; | |
668 | #else | |
669 | job_control = sysconf (_SC_JOB_CONTROL); | |
670 | #endif | |
671 | ||
672 | #else /* not termios. */ | |
673 | ||
674 | #ifdef TIOCGPGRP | |
675 | job_control = 1; | |
676 | #else | |
677 | job_control = 0; | |
678 | #endif /* TIOCGPGRP */ | |
679 | ||
680 | #endif /* not termios. */ | |
4e772f44 | 681 | } |