* ser-termios.c, ser-go32.c: Remove DEFUN crap, clean up.
[deliverable/binutils-gdb.git] / gdb / ser-termios.c
CommitLineData
b52373a2
JG
1/* Remote serial interface for OS's with termios, for GDB.
2 Copyright 1992 Free Software Foundation, Inc.
ae0ea72e
SC
3
4This file is part of GDB.
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
18Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20#include "defs.h"
21#include "serial.h"
22#include <fcntl.h>
23#include <termios.h>
24#include <sys/time.h>
25
b52373a2 26/* File descriptor used in termios routines to access serial line. */
ae0ea72e
SC
27static int desc;
28
b52373a2
JG
29/* Saved state about the terminal. */
30static struct termios otermios;
31static int oflags;
ae0ea72e 32
b52373a2
JG
33const char *
34serial_default_name ()
ae0ea72e
SC
35{
36 return "/dev/ttya";
37}
38
39void
b52373a2 40serial_raw ()
ae0ea72e
SC
41{
42 /* Now happens inside of serial_open */
43}
44
ae0ea72e 45void
b52373a2 46serial_normal ()
ae0ea72e
SC
47{
48 fcntl(desc, oflags, 0);
49
50 if (tcsetattr(desc, TCSANOW, &otermios))
51 {
52 printf("tcgetattr failed: errno=%d\n", errno);
53 }
54}
55
56int
b52373a2
JG
57serial_open (name)
58 const char *name;
ae0ea72e
SC
59{
60 struct termios termios;
61
62 desc = open (name, O_RDWR);
63 if (desc < 0)
64 perror("Open failed: ");
65
66 oflags = fcntl(desc, F_GETFL, 0);
67
68 fcntl(desc, F_SETFL, oflags|FNDELAY);
69
70 if (tcgetattr(desc, &termios)) {
71 printf("tcgetattr failed: errno=%d\n", errno);
72 }
73
74 otermios = termios;
75
76 termios.c_iflag = 0;
77 termios.c_oflag = 0;
78 termios.c_lflag = 0;
79 termios.c_cc[VMIN] = 0;
80 termios.c_cc[VTIME] = 0;
81
82 if (tcsetattr(desc, TCSANOW, &termios)) {
83 printf("tcgetattr failed: errno=%d\n", errno);
84 }
85
86 return 1;
87}
88
89int
b52373a2
JG
90serial_timedreadchar (timeout, ok)
91 int timeout;
92 int *ok;
ae0ea72e
SC
93{
94 unsigned char buf;
95 fd_set readfds;
96 int val;
97 struct timeval tv;
98
99 FD_ZERO(&readfds);
100 FD_SET(desc, &readfds);
101
102 tv.tv_sec = timeout;
103 tv.tv_usec = 0;
104
105 val = select(desc+1, &readfds, 0, 0, &tv);
106
107 if (val > 0 && FD_ISSET(desc, &readfds))
108 {
109 val = read (desc, &buf, 1);
110
111 if (val == 1)
112 {
113 *ok = 1;
114 return buf;
115 }
116 }
117
118 *ok = 0;
119
120 return 0;
121}
122
123/* Translate baud rates from integers to damn B_codes. Unix should
124 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
125
126#ifndef B19200
127#define B19200 EXTA
128#endif
129#ifndef B38400
130#define B38400 EXTB
131#endif
132
133static struct {int rate, damn_b;} baudtab[] = {
134 {9600, B9600},
135
136 {19200, B19200},
b52373a2 137 {38400, B38400},
ae0ea72e
SC
138#if 0
139 {300, B300},
140 {1200, B1200},
141 {2400, B2400},
142 {4800, B4800},
143#endif
144 {-1, -1},
145};
146
147static int
b52373a2
JG
148damn_b (rate)
149 int rate;
ae0ea72e
SC
150{
151 int i;
152 for (i = 0; baudtab[i].rate != -1; i++)
153 {
154 if (rate == baudtab[i].rate)
155 {
156 return i;
157 }
158 }
159 return -1;
160}
161
162int
b52373a2
JG
163serial_setbaudrate (rate)
164 int rate;
ae0ea72e
SC
165{
166 struct termios termios;
167
168 if (tcgetattr(desc, &termios)) {
169 printf("tcgetattr failed: errno=%d\n", errno);
170 }
171
172 cfsetospeed(&termios, baudtab[damn_b(rate)].damn_b);
173 cfsetispeed(&termios, baudtab[damn_b(rate)].damn_b);
174
175 if (tcsetattr(desc, TCSANOW, &termios)) {
176 printf("tcgetattr failed: errno=%d\n", errno);
177 }
178
179 return 1;
180}
181
182int
b52373a2
JG
183serial_nextbaudrate (rate)
184 int rate;
ae0ea72e
SC
185{
186 int lookup;
187 lookup = damn_b(rate);
188 if (lookup == -1)
189 return baudtab[0].rate;
190 lookup++;
191 if (baudtab[lookup].rate == -1)
192 return baudtab[0].rate;
193 return baudtab[lookup].rate;
194}
195
196int
b52373a2
JG
197serial_write (str, len)
198 const char *str;
199 int len;
ae0ea72e 200{
e676a15f 201 return (write (desc, str, len));
ae0ea72e
SC
202}
203
204
205int
b52373a2 206serial_close ()
ae0ea72e 207{
e676a15f 208 return (close(desc));
ae0ea72e 209}
This page took 0.040027 seconds and 4 git commands to generate.