/* [<][>][^][v][top][bottom][index][help] */
DEFINITIONS
This source file includes following definitions.
- termios_allocate
- TERMIOS_GET_N_SET
- termios_c_cc_set
- Scm_MakeSysTermios
- Scm_Openpty
- Scm_Forkpty
- Scm_Init_termios
1 /*
2 * termios.c - termios interface
3 *
4 * Copyright (c) 2000-2004 Shiro Kawai, All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of the authors nor the names of its contributors
18 * may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 * $Id: termios.c,v 1.16 2005/07/22 09:26:55 shirok Exp $
34 */
35
36 #include <string.h>
37 #include "gauche/termios.h"
38 #include <gauche/class.h>
39 #include <gauche/extend.h>
40
41 #if !defined(__MINGW32__)
42
43 /*
44 * Termios interface
45 */
46 static ScmObj termios_allocate(ScmClass *klass, ScmObj initargs);
47
48 SCM_DEFINE_BUILTIN_CLASS(Scm_SysTermiosClass,
49 NULL, NULL, NULL,
50 termios_allocate,
51 NULL);
52
53 static ScmObj termios_allocate(ScmClass *klass, ScmObj initargs)
54 {
55 ScmSysTermios *t = SCM_NEW(ScmSysTermios);
56 SCM_SET_CLASS(t, SCM_CLASS_SYS_TERMIOS);
57 memset(&t->term, 0, sizeof(t->term));
58 return SCM_OBJ(t);
59 }
60
61 /* slot accessors */
62 #define TERMIOS_GET_N_SET(name) \
63 static ScmObj SCM_CPP_CAT3(termios_, name, _get)(ScmSysTermios* t) \
64 { return Scm_MakeIntegerFromUI((u_long)t->term.name); } \
65 static void SCM_CPP_CAT3(termios_, name, _set)(ScmSysTermios* t, ScmObj v) \
66 { \
67 if (!SCM_INTEGERP(v)) Scm_Error("integer required, but got %S", v); \
68 t->term.name = (tcflag_t)Scm_GetUInteger(v); \
69 }
70
71 TERMIOS_GET_N_SET(c_iflag)
72 TERMIOS_GET_N_SET(c_oflag)
73 TERMIOS_GET_N_SET(c_cflag)
74 TERMIOS_GET_N_SET(c_lflag)
75
76 static ScmObj termios_c_cc_get(ScmSysTermios* t)
77 {
78 return Scm_MakeU8VectorFromArray(NCCS, (const unsigned char*)t->term.c_cc);
79 }
80
81 static void termios_c_cc_set(ScmSysTermios* t, ScmObj val)
82 {
83 if (!SCM_U8VECTORP(val)) {
84 Scm_Error("cc type must be a u8vector, but got %S", val);
85 }
86 if (SCM_U8VECTOR_SIZE(val) != NCCS) {
87 Scm_Error("size of cc must be %u, but got %u",
88 NCCS, SCM_U8VECTOR_SIZE(val));
89 }
90 memcpy(t->term.c_cc, SCM_U8VECTOR_ELEMENTS(val), NCCS);
91 }
92
93 static ScmClassStaticSlotSpec termios_slots[] = {
94 SCM_CLASS_SLOT_SPEC("iflag", termios_c_iflag_get, termios_c_iflag_set),
95 SCM_CLASS_SLOT_SPEC("oflag", termios_c_oflag_get, termios_c_oflag_set),
96 SCM_CLASS_SLOT_SPEC("cflag", termios_c_cflag_get, termios_c_cflag_set),
97 SCM_CLASS_SLOT_SPEC("lflag", termios_c_lflag_get, termios_c_lflag_set),
98 SCM_CLASS_SLOT_SPEC("cc", termios_c_cc_get, termios_c_cc_set),
99 { NULL }
100 };
101
102 ScmObj Scm_MakeSysTermios(void)
103 {
104 return termios_allocate(NULL, SCM_NIL);
105 }
106
107 /*
108 * pty
109 */
110
111 #ifdef HAVE_OPENPTY
112 ScmObj Scm_Openpty(ScmObj slaveterm)
113 {
114 int master, slave;
115 struct termios *term = NULL;
116
117 if (SCM_SYS_TERMIOS_P(slaveterm)) {
118 term = &SCM_SYS_TERMIOS(slaveterm)->term;
119 }
120 if (openpty(&master, &slave, NULL, term, NULL) < 0) {
121 Scm_SysError("openpty failed");
122 }
123 return Scm_Values2(SCM_MAKE_INT(master), SCM_MAKE_INT(slave));
124 }
125 #endif /*HAVE_OPENPTY*/
126
127 #ifdef HAVE_FORKPTY
128 ScmObj Scm_Forkpty(ScmObj slaveterm)
129 {
130 int master;
131 pid_t pid;
132 struct termios *term = NULL;
133 if (SCM_SYS_TERMIOS_P(slaveterm)) {
134 term = &SCM_SYS_TERMIOS(slaveterm)->term;
135 }
136 if ((pid = forkpty(&master, NULL, term, NULL)) < 0) {
137 Scm_SysError("forkpty failed");
138 }
139 return Scm_Values2(Scm_MakeInteger(pid), SCM_MAKE_INT(master));
140 }
141 #endif /*HAVE_FORKPTY*/
142
143 #endif /*!defined(__MINGW32) */
144
145 /*
146 * Initializaion
147 */
148
149 extern void Scm_Init_termiolib(ScmModule *mod);
150
151 void Scm_Init_termios(void)
152 {
153 ScmModule *mod;
154 SCM_INIT_EXTENSION(termios);
155 mod = SCM_FIND_MODULE("gauche.termios", SCM_FIND_MODULE_CREATE);
156 Scm_Init_termiolib(mod);
157
158 #ifndef __MINGW32__
159 Scm_InitStaticClass(&Scm_SysTermiosClass, "<sys-termios>", mod,
160 termios_slots, 0);
161
162 /* Constants for termios. Non-POSIX symbols are guarded by #ifdef's */
163 #define DEFSYM(sym) \
164 SCM_DEFINE(mod, #sym, Scm_MakeIntegerFromUI(sym))
165
166 /* c_iflag masks */
167 DEFSYM(IGNBRK);
168 DEFSYM(BRKINT);
169 DEFSYM(IGNPAR);
170 DEFSYM(PARMRK);
171 DEFSYM(INPCK);
172 DEFSYM(ISTRIP);
173 DEFSYM(INLCR);
174 DEFSYM(IGNCR);
175 DEFSYM(ICRNL);
176 DEFSYM(IXON);
177 DEFSYM(IXOFF);
178 #ifdef IXANY
179 DEFSYM(IXANY);
180 #endif
181 #ifdef IUCLC
182 DEFSYM(IUCLC);
183 #endif
184 #ifdef IMAXBEL
185 DEFSYM(IMAXBEL);
186 #endif
187
188 /* c_oflag masks */
189 DEFSYM(OPOST);
190 #ifdef OLCUC
191 DEFSYM(OLCUC);
192 #endif
193 #ifdef ONLCR
194 DEFSYM(ONLCR);
195 #endif
196 #ifdef OCRNL
197 DEFSYM(OCRNL);
198 #endif
199 #ifdef ONOCR
200 DEFSYM(ONOCR);
201 #endif
202 #ifdef ONLRET
203 DEFSYM(ONLRET);
204 #endif
205 #ifdef OFILL
206 DEFSYM(OFILL);
207 #endif
208 #ifdef OFDEL
209 DEFSYM(OFDEL);
210 #endif
211 #ifdef NLDLY
212 DEFSYM(NLDLY);
213 #endif
214 #ifdef NL0
215 DEFSYM(NL0);
216 #endif
217 #ifdef NL1
218 DEFSYM(NL1);
219 #endif
220 #ifdef CRDLY
221 DEFSYM(CRDLY);
222 #endif
223 #ifdef CR0
224 DEFSYM(CR0);
225 #endif
226 #ifdef CR1
227 DEFSYM(CR1);
228 #endif
229 #ifdef CR2
230 DEFSYM(CR2);
231 #endif
232 #ifdef CR3
233 DEFSYM(CR3);
234 #endif
235 #ifdef BSDLY
236 DEFSYM(BSDLY);
237 #endif
238 #ifdef BS0
239 DEFSYM(BS0);
240 #endif
241 #ifdef BS1
242 DEFSYM(BS1);
243 #endif
244 #ifdef VTDLY
245 DEFSYM(VTDLY);
246 #endif
247 #ifdef VT0
248 DEFSYM(VT0);
249 #endif
250 #ifdef VT1
251 DEFSYM(VT1);
252 #endif
253 #ifdef FFDLY
254 DEFSYM(FFDLY);
255 #endif
256 #ifdef FF0
257 DEFSYM(FF0);
258 #endif
259 #ifdef FF1
260 DEFSYM(FF1);
261 #endif
262
263 /* c_cflag masks */
264 DEFSYM(CLOCAL);
265 DEFSYM(CREAD);
266 DEFSYM(CSIZE);
267 DEFSYM(CS5);
268 DEFSYM(CS6);
269 DEFSYM(CS7);
270 DEFSYM(CS8);
271 DEFSYM(CSTOPB);
272 DEFSYM(HUPCL);
273 DEFSYM(PARENB);
274 DEFSYM(PARODD);
275 #ifdef CIBAUD
276 DEFSYM(CIBAUD);
277 #endif
278 #ifdef CRTSCTS
279 DEFSYM(CRTSCTS);
280 #endif
281
282 /* c_lflag masks */
283 DEFSYM(ECHO);
284 DEFSYM(ECHOE);
285 DEFSYM(ECHOK);
286 DEFSYM(ECHONL);
287 DEFSYM(ICANON);
288 DEFSYM(ISIG);
289 DEFSYM(NOFLSH);
290 DEFSYM(TOSTOP);
291 DEFSYM(IEXTEN);
292 #ifdef XCASE
293 DEFSYM(XCASE);
294 #endif
295 #ifdef ECHOCTL
296 DEFSYM(ECHOCTL);
297 #endif
298 #ifdef ECHOPRT
299 DEFSYM(ECHOPRT);
300 #endif
301 #ifdef ECHOKE
302 DEFSYM(ECHOKE);
303 #endif
304 #ifdef FLUSH0
305 DEFSYM(FLUSH0);
306 #endif
307 #ifdef PENDIN
308 DEFSYM(PENDIN);
309 #endif
310
311 /* c_cc size */
312 DEFSYM(NCCS);
313
314 /* disable character */
315 DEFSYM(_POSIX_VDISABLE);
316
317 /* c_cc subscripts */
318 DEFSYM(VEOF);
319 DEFSYM(VEOL);
320 DEFSYM(VERASE);
321 DEFSYM(VINTR);
322 DEFSYM(VKILL);
323 DEFSYM(VMIN);
324 DEFSYM(VQUIT);
325 DEFSYM(VSTART);
326 DEFSYM(VSTOP);
327 DEFSYM(VSUSP);
328 DEFSYM(VTIME);
329 #ifdef VDISCARD
330 DEFSYM(VDISCARD);
331 #endif
332 #ifdef VDSUSP
333 DEFSYM(VDSUSP);
334 #endif
335 #ifdef EOL2
336 DEFSYM(VEOL2);
337 #endif
338 #ifdef LNEXT
339 DEFSYM(VLNEXT);
340 #endif
341 #ifdef VREPRINT
342 DEFSYM(VREPRINT);
343 #endif
344 #ifdef VSTATUS
345 DEFSYM(VSTATUS);
346 #endif
347 #ifdef WERASE
348 DEFSYM(VWERASE);
349 #endif
350 #ifdef VSWTCH
351 DEFSYM(VSWTCH);
352 #endif
353 #ifdef VSWTC
354 DEFSYM(VSWTC);
355 #endif
356
357 /* extra baudrates. <= B38400 is defined in termiolib.stub */
358 #ifdef B57600
359 DEFSYM(B57600);
360 #endif
361 #ifdef B115200
362 DEFSYM(B115200);
363 #endif
364 #ifdef B230400
365 DEFSYM(B230400);
366 #endif
367
368 #endif /*!__MINGW32__*/
369 }