4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 #pragma ident "%Z%%M% %I% %E% SMI"
38 #include <sys/stermio.h>
39 #include <sys/termiox.h>
43 #include <sys/types.h>
44 #include "sys/stropts.h"
45 #include "sys/signal.h"
53 * set_termio - set termio on device
54 * fd - fd for the device
55 * options - stty termio options
56 * aspeed - autobaud speed
57 * clear - if TRUE, current flags will be set to some defaults
58 * before applying the options
59 * - if FALSE, current flags will not be cleared
60 * mode - terminal mode, CANON, RAW
66 set_termio(fd
,options
,aspeed
,clear
,mode
)
74 struct termios termios
;
76 struct termiox termiox
;
77 struct winsize winsize
;
78 struct winsize owinsize
;
82 char *argvp
[MAXARGS
]; /* stty args */
83 static char *binstty
= "/usr/bin/stty";
84 static char buf
[BUFSIZ
];
85 extern int get_ttymode(), set_ttymode();
86 extern char *sttyparse();
89 debug("in set_termio");
92 if ((term
= get_ttymode(fd
, &termio
, &termios
, &stermio
,
93 &termiox
, &winsize
)) < 0) {
94 log("set_termio: get_ttymode failed: %s", strerror(errno
));
100 /* could have removed these too - rely on defaults */
101 termios
.c_cc
[VEOF
] = CEOF
;
102 termios
.c_cc
[VEOL
] = CNUL
;
105 termios
.c_lflag
&= ECHO
;
106 termios
.c_cc
[VMIN
] = 1;
107 termios
.c_cc
[VTIME
] = 0;
112 if (options
!= NULL
&& *options
!= '\0') {
113 /* just a place holder to make it look like invoking stty */
115 (void)strcpy(buf
,options
);
116 mkargv(buf
,&argvp
[1],&cnt
,MAXARGS
-1);
117 if (aspeed
!= NULL
&& *aspeed
!= '\0') {
118 argvp
[cnt
++] = aspeed
;
120 argvp
[cnt
] = (char *)0;
121 if ((uarg
= sttyparse(cnt
, argvp
, term
, &termio
, &termios
,
122 &termiox
, &winsize
)) != NULL
) {
123 log("sttyparse unknown mode: %s", uarg
);
129 if (set_ttymode(fd
, term
, &termio
, &termios
, &stermio
,
130 &termiox
, &winsize
, &owinsize
) != 0) {
131 log("set_termio: set_ttymode failed", strerror(errno
));
140 * turnon_canon - turn on canonical processing
141 * - return 0 if succeeds, -1 if fails
146 struct termio termio
;
149 debug("in turnon_canon");
151 if (ioctl(fd
, TCGETA
, &termio
) != 0) {
152 log("turnon_canon: TCGETA failed, fd = %d: %s", fd
,
156 termio
.c_lflag
|= (ISIG
|ICANON
|ECHO
|ECHOE
|ECHOK
);
157 termio
.c_cc
[VEOF
] = CEOF
;
158 termio
.c_cc
[VEOL
] = CNUL
;
159 if (ioctl(fd
, TCSETA
, &termio
) != 0) {
160 log("turnon_canon: TCSETA failed, fd = %d: %s", fd
,
169 * flush_input - flush the input queue
175 if (ioctl(fd
, I_FLUSH
, FLUSHR
) == -1)
176 log("flush_input failed, fd = %d: %s", fd
, strerror(errno
));
178 if (ioctl(fd
, TCSBRK
, 1) == -1)
179 log("drain of ouput failed, fd = %d: %s", fd
, strerror(errno
));
185 * push_linedisc - if modules is not NULL, pop everything
186 * - then push modules specified by "modules"
190 int fd
, /* fd to push modules on */
191 char *modules
, /* ptr to a list of comma separated module names */
192 char *device
) /* device name for printing msg */
198 debug("in push_linedisc");
201 * copy modules into buf so we won't mess up the original buffer
202 * because strtok will chop the string
204 p
= strcpy(buf
,modules
);
206 while(ioctl(fd
, I_POP
, 0) >= 0) /* pop everything */
208 for (p
=(char *)strtok(p
,","); p
!=(char *)NULL
;
209 p
=(char *)strtok(NULL
,",")) {
210 for (tp
= p
+ strlen(p
) - 1; tp
>= p
&& isspace(*tp
); --tp
)
212 if (ioctl(fd
, I_PUSH
, p
) == -1) {
213 log("push (%s) on %s failed: %s", p
, device
,
222 * hang_up_line - set speed to B0. This will drop DTR
227 struct termio termio
;
228 struct termios termios
;
231 debug("in hang_up_line");
233 if (ioctl(fd
,TCGETS
,&termios
) < 0) {
234 if (ioctl(fd
,TCGETA
,&termio
) < 0) {
235 log("hang_up_line: TCGETA failed: %s", strerror(errno
));
238 termio
.c_cflag
&= ~CBAUD
;
239 termio
.c_cflag
|= B0
;
241 if (ioctl(fd
,TCSETA
,&termio
) < 0) {
242 log("hang_up_line: TCSETA failed: %s", strerror(errno
));
246 cfsetospeed(&termios
, B0
);
248 if (ioctl(fd
,TCSETS
,&termios
) < 0) {
249 log("hang_up_line: TCSETS failed: %s", strerror(errno
));
257 * initial_termio - set initial termios
258 * - return 0 if successful, -1 if failed.
261 initial_termio(fd
,pmptr
)
266 struct Gdef
*speedef
;
267 struct Gdef
*get_speed();
268 extern int auto_termio();
270 speedef
= get_speed(pmptr
->p_ttylabel
);
271 if (speedef
->g_autobaud
& A_FLAG
) {
272 pmptr
->p_ttyflags
|= A_FLAG
;
273 if (auto_termio(fd
) == -1) {
279 if (pmptr
->p_ttyflags
& R_FLAG
)
280 ret
= set_termio(fd
,speedef
->g_iflags
,
281 (char *)NULL
, TRUE
, (long)RAW
);
283 ret
= set_termio(fd
,speedef
->g_iflags
,
284 (char *)NULL
, TRUE
, (long)CANON
);
286 log("initial termio on (%s) failed", pmptr
->p_device
);