@@ -20,141 +20,161 @@ enum { Iflags, Oflags, Cflags, Lflags };
20
20
21
21
/* Structure of the terminal_io record. Cf. unix.mli */
22
22
23
- static long terminal_io_descr [] = {
24
- /* Input modes */
25
- Bool , Iflags , IGNBRK , Bool , Iflags , BRKINT , Bool , Iflags , IGNPAR , Bool ,
26
- Iflags , PARMRK , Bool , Iflags , INPCK , Bool , Iflags , ISTRIP , Bool , Iflags ,
27
- INLCR , Bool , Iflags , IGNCR , Bool , Iflags , ICRNL , Bool , Iflags , IXON , Bool ,
28
- Iflags , IXOFF ,
29
- /* Output modes */
30
- Bool , Oflags , OPOST ,
31
- /* Control modes */
32
- Speed , Output , Speed , Input , Enum , Cflags , 5 , 4 , CSIZE , CS5 , CS6 , CS7 , CS8 ,
33
- Enum , Cflags , 1 , 2 , CSTOPB , 0 , CSTOPB , Bool , Cflags , CREAD , Bool , Cflags ,
34
- PARENB , Bool , Cflags , PARODD , Bool , Cflags , HUPCL , Bool , Cflags , CLOCAL ,
35
- /* Local modes */
36
- Bool , Lflags , ISIG , Bool , Lflags , ICANON , Bool , Lflags , NOFLSH , Bool ,
37
- Lflags , ECHO , Bool , Lflags , ECHOE , Bool , Lflags , ECHOK , Bool , Lflags ,
38
- ECHONL ,
39
- /* Control characters */
40
- Char , VINTR , Char , VQUIT , Char , VERASE , Char , VKILL , Char , VEOF , Char , VEOL ,
41
- Char , VMIN , Char , VTIME , Char , VSTART , Char , VSTOP , End };
23
+ static tcflag_t * choose_field (struct termios * terminal_status , long field )
24
+ {
25
+ switch (field ) {
26
+ case Iflags :
27
+ return & terminal_status -> c_iflag ;
28
+ case Oflags :
29
+ return & terminal_status -> c_oflag ;
30
+ case Cflags :
31
+ return & terminal_status -> c_cflag ;
32
+ case Lflags :
33
+ return & terminal_status -> c_lflag ;
34
+ default :
35
+ return 0 ;
36
+ }
37
+ }
42
38
43
- static struct {
39
+ struct speed_t {
44
40
speed_t speed ;
45
41
int baud ;
46
- } speedtable [] = {{B50 , 50 },
47
- {B75 , 75 },
48
- {B110 , 110 },
49
- {B134 , 134 },
50
- {B150 , 150 },
42
+ };
43
+
44
+ long _speedtable (struct speed_t dst []) {
45
+ struct speed_t speedtable [] = {
46
+ {B50 , 50 },
47
+ {B75 , 75 },
48
+ {B110 , 110 },
49
+ {B134 , 134 },
50
+ {B150 , 150 },
51
51
#ifdef B200
52
- {B200 , 200 },
52
+ {B200 , 200 },
53
53
#endif
54
- {B300 , 300 },
55
- {B600 , 600 },
56
- {B1200 , 1200 },
57
- {B1800 , 1800 },
58
- {B2400 , 2400 },
59
- {B4800 , 4800 },
60
- {B9600 , 9600 },
61
- {B19200 , 19200 },
62
- {B38400 , 38400 },
54
+ {B300 , 300 },
55
+ {B600 , 600 },
56
+ {B1200 , 1200 },
57
+ {B1800 , 1800 },
58
+ {B2400 , 2400 },
59
+ {B4800 , 4800 },
60
+ {B9600 , 9600 },
61
+ {B19200 , 19200 },
62
+ {B38400 , 38400 },
63
63
#ifdef B57600
64
- {B57600 , 57600 },
64
+ {B57600 , 57600 },
65
65
#endif
66
66
#ifdef B115200
67
- {B115200 , 115200 },
67
+ {B115200 , 115200 },
68
68
#endif
69
69
#ifdef B230400
70
- {B230400 , 230400 },
70
+ {B230400 , 230400 },
71
71
#endif
72
- {B0 , 0 },
72
+ {B0 , 0 },
73
73
74
74
/* Linux extensions */
75
75
#ifdef B460800
76
- {B460800 , 460800 },
76
+ {B460800 , 460800 },
77
77
#endif
78
78
#ifdef B500000
79
- {B500000 , 500000 },
79
+ {B500000 , 500000 },
80
80
#endif
81
81
#ifdef B576000
82
- {B576000 , 576000 },
82
+ {B576000 , 576000 },
83
83
#endif
84
84
#ifdef B921600
85
- {B921600 , 921600 },
85
+ {B921600 , 921600 },
86
86
#endif
87
87
#ifdef B1000000
88
- {B1000000 , 1000000 },
88
+ {B1000000 , 1000000 },
89
89
#endif
90
90
#ifdef B1152000
91
- {B1152000 , 1152000 },
91
+ {B1152000 , 1152000 },
92
92
#endif
93
93
#ifdef B1500000
94
- {B1500000 , 1500000 },
94
+ {B1500000 , 1500000 },
95
95
#endif
96
96
#ifdef B2000000
97
- {B2000000 , 2000000 },
97
+ {B2000000 , 2000000 },
98
98
#endif
99
99
#ifdef B2500000
100
- {B2500000 , 2500000 },
100
+ {B2500000 , 2500000 },
101
101
#endif
102
102
#ifdef B3000000
103
- {B3000000 , 3000000 },
103
+ {B3000000 , 3000000 },
104
104
#endif
105
105
#ifdef B3500000
106
- {B3500000 , 3500000 },
106
+ {B3500000 , 3500000 },
107
107
#endif
108
108
#ifdef B4000000
109
- {B4000000 , 4000000 },
109
+ {B4000000 , 4000000 },
110
110
#endif
111
111
112
- /* MacOS extensions */
112
+ /* MacOS extensions */
113
113
#ifdef B7200
114
- {B7200 , 7200 },
114
+ {B7200 , 7200 },
115
115
#endif
116
116
#ifdef B14400
117
- {B14400 , 14400 },
117
+ {B14400 , 14400 },
118
118
#endif
119
119
#ifdef B28800
120
- {B28800 , 28800 },
120
+ {B28800 , 28800 },
121
121
#endif
122
122
#ifdef B76800
123
- {B76800 , 76800 },
123
+ {B76800 , 76800 },
124
124
#endif
125
125
126
126
/* Cygwin extensions (in addition to the Linux ones) */
127
127
#ifdef B128000
128
- {B128000 , 128000 },
128
+ {B128000 , 128000 },
129
129
#endif
130
130
#ifdef B256000
131
- {B256000 , 256000 },
131
+ {B256000 , 256000 },
132
132
#endif
133
- };
133
+ };
134
134
135
- #define NSPEEDS (sizeof(speedtable) / sizeof(speedtable[0]))
135
+ if (dst != NULL ) memcpy (dst , speedtable , sizeof (speedtable ));
136
+ return (sizeof (speedtable ) / sizeof (speedtable [0 ]));
137
+ }
136
138
137
- static tcflag_t * choose_field (struct termios * terminal_status , long field )
138
- {
139
- switch (field ) {
140
- case Iflags :
141
- return & terminal_status -> c_iflag ;
142
- case Oflags :
143
- return & terminal_status -> c_oflag ;
144
- case Cflags :
145
- return & terminal_status -> c_cflag ;
146
- case Lflags :
147
- return & terminal_status -> c_lflag ;
148
- default :
149
- return 0 ;
150
- }
139
+ long _terminal_io_descr (long dst []) {
140
+ long terminal_io_descr [] = {
141
+ /* Input modes */
142
+ Bool , Iflags , IGNBRK , Bool , Iflags , BRKINT , Bool , Iflags , IGNPAR , Bool ,
143
+ Iflags , PARMRK , Bool , Iflags , INPCK , Bool , Iflags , ISTRIP , Bool , Iflags ,
144
+ INLCR , Bool , Iflags , IGNCR , Bool , Iflags , ICRNL , Bool , Iflags , IXON , Bool ,
145
+ Iflags , IXOFF ,
146
+ /* Output modes */
147
+ Bool , Oflags , OPOST ,
148
+ /* Control modes */
149
+ Speed , Output , Speed , Input , Enum , Cflags , 5 , 4 , CSIZE , CS5 , CS6 , CS7 , CS8 ,
150
+ Enum , Cflags , 1 , 2 , CSTOPB , 0 , CSTOPB , Bool , Cflags , CREAD , Bool , Cflags ,
151
+ PARENB , Bool , Cflags , PARODD , Bool , Cflags , HUPCL , Bool , Cflags , CLOCAL ,
152
+ /* Local modes */
153
+ Bool , Lflags , ISIG , Bool , Lflags , ICANON , Bool , Lflags , NOFLSH , Bool ,
154
+ Lflags , ECHO , Bool , Lflags , ECHOE , Bool , Lflags , ECHOK , Bool , Lflags ,
155
+ ECHONL ,
156
+ /* Control characters */
157
+ Char , VINTR , Char , VQUIT , Char , VERASE , Char , VKILL , Char , VEOF , Char , VEOL ,
158
+ Char , VMIN , Char , VTIME , Char , VSTART , Char , VSTOP , End };
159
+
160
+ if (dst != NULL ) memcpy (dst , terminal_io_descr , sizeof (terminal_io_descr ));
161
+ return (sizeof (terminal_io_descr ) / sizeof (long ));
151
162
}
152
163
164
+
153
165
void encode_terminal_status (struct termios * terminal_status , value * dst )
154
166
{
155
167
long * pc ;
156
168
int i ;
157
169
170
+ long _NSPEEDS = _speedtable (NULL );
171
+ struct speed_t speedtable [_NSPEEDS ];
172
+ _speedtable (speedtable );
173
+
174
+ long nterminal_io_descr = _terminal_io_descr (NULL );
175
+ long terminal_io_descr [nterminal_io_descr ];
176
+ _terminal_io_descr (terminal_io_descr );
177
+
158
178
for (pc = terminal_io_descr ; * pc != End ; dst ++ ) {
159
179
switch (* pc ++ ) {
160
180
case Bool : {
@@ -190,7 +210,7 @@ void encode_terminal_status(struct termios *terminal_status, value *dst)
190
210
speed = cfgetispeed (terminal_status );
191
211
break ;
192
212
}
193
- for (i = 0 ; i < NSPEEDS ; i ++ ) {
213
+ for (i = 0 ; i < _NSPEEDS ; i ++ ) {
194
214
if (speed == speedtable [i ].speed ) {
195
215
* dst = Val_int (speedtable [i ].baud );
196
216
break ;
@@ -212,6 +232,14 @@ int decode_terminal_status(struct termios *terminal_status, value *src)
212
232
long * pc ;
213
233
int i ;
214
234
235
+ long _NSPEEDS = _speedtable (NULL );
236
+ struct speed_t speedtable [_NSPEEDS ];
237
+ _speedtable (speedtable );
238
+
239
+ long nterminal_io_descr = _terminal_io_descr (NULL );
240
+ long terminal_io_descr [nterminal_io_descr ];
241
+ _terminal_io_descr (terminal_io_descr );
242
+
215
243
for (pc = terminal_io_descr ; * pc != End ; src ++ ) {
216
244
switch (* pc ++ ) {
217
245
case Bool : {
@@ -242,7 +270,7 @@ int decode_terminal_status(struct termios *terminal_status, value *src)
242
270
int which = * pc ++ ;
243
271
int baud = Int_val (* src );
244
272
int res = 0 ;
245
- for (i = 0 ; i < NSPEEDS ; i ++ ) {
273
+ for (i = 0 ; i < _NSPEEDS ; i ++ ) {
246
274
if (baud == speedtable [i ].baud ) {
247
275
switch (which ) {
248
276
case Output :
0 commit comments