* ser-termios.c, ser-go32.c: Remove DEFUN crap, clean up.
[deliverable/binutils-gdb.git] / gdb / ser-termios.c
1 /* Remote serial interface for OS's with termios, for GDB.
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 "serial.h"
22 #include <fcntl.h>
23 #include <termios.h>
24 #include <sys/time.h>
25
26 /* File descriptor used in termios routines to access serial line. */
27 static int desc;
28
29 /* Saved state about the terminal. */
30 static struct termios otermios;
31 static int oflags;
32
33 const char *
34 serial_default_name ()
35 {
36 return "/dev/ttya";
37 }
38
39 void
40 serial_raw ()
41 {
42 /* Now happens inside of serial_open */
43 }
44
45 void
46 serial_normal ()
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
56 int
57 serial_open (name)
58 const char *name;
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
89 int
90 serial_timedreadchar (timeout, ok)
91 int timeout;
92 int *ok;
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
133 static struct {int rate, damn_b;} baudtab[] = {
134 {9600, B9600},
135
136 {19200, B19200},
137 {38400, B38400},
138 #if 0
139 {300, B300},
140 {1200, B1200},
141 {2400, B2400},
142 {4800, B4800},
143 #endif
144 {-1, -1},
145 };
146
147 static int
148 damn_b (rate)
149 int rate;
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
162 int
163 serial_setbaudrate (rate)
164 int rate;
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
182 int
183 serial_nextbaudrate (rate)
184 int rate;
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
196 int
197 serial_write (str, len)
198 const char *str;
199 int len;
200 {
201 return (write (desc, str, len));
202 }
203
204
205 int
206 serial_close ()
207 {
208 return (close(desc));
209 }
This page took 0.03474 seconds and 5 git commands to generate.