Home | History | Annotate | Line # | Download | only in lpd
ttcompat.c revision 1.5
      1 /*
      2  * Copyright (c) 1995
      3  *	The Regents of the University of California.  All rights reserved.
      4  *
      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  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. All advertising materials mentioning features or use of this software
     15  *    must display the following acknowledgement:
     16  *	This product includes software developed by the University of
     17  *	California, Berkeley and its contributors.
     18  * 4. Neither the name of the University nor the names of its contributors
     19  *    may be used to endorse or promote products derived from this software
     20  *    without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  */
     34 
     35 /*
     36  * ttcompat.c -- convert sgtty flags to termios
     37  *	originally from /sys/kern/tty_compat.c
     38  */
     39 
     40 #include <sys/param.h>
     41 #include <sys/types.h>
     42 
     43 #include <unistd.h>
     44 #include <sys/ioctl_compat.h>
     45 #include <termios.h>
     46 #include <syslog.h>
     47 #include <fcntl.h>
     48 #include <dirent.h>
     49 #include <errno.h>
     50 #include <stdio.h>
     51 #include <string.h>
     52 #include <stdlib.h>
     53 #include "extern.h"
     54 
     55 /* Macros to clear/set/test flags. */
     56 #define	SET(t, f)	(t) |= (f)
     57 #define	CLR(t, f)	(t) &= ~(f)
     58 #define	ISSET(t, f)	((t) & (f))
     59 
     60 void
     61 sttyclearflags(tp, flags)
     62 	struct termios *tp;
     63 	int flags;
     64 {
     65 	register tcflag_t iflag = tp->c_iflag;
     66 	register tcflag_t oflag = tp->c_oflag;
     67 	register tcflag_t lflag = tp->c_lflag;
     68 	register tcflag_t cflag = tp->c_cflag;
     69 
     70 	if (ISSET(flags, TANDEM))
     71 		CLR(iflag, IXOFF);
     72 	if (ISSET(flags, ECHO))
     73 		CLR(lflag, ECHO);
     74 	if (ISSET(flags, CRMOD)) {
     75 		CLR(iflag, ICRNL);
     76 		CLR(oflag, ONLCR);
     77 	}
     78 	if (ISSET(flags, XTABS))
     79 		CLR(oflag, OXTABS);
     80 
     81 
     82 	if (ISSET(flags, RAW)) {
     83 		SET(iflag, BRKINT|IXON|IMAXBEL);
     84 		SET(lflag, ISIG|IEXTEN);
     85 		if (ISSET(flags, CBREAK))
     86 			CLR(lflag, ICANON);
     87 		else
     88 			SET(lflag, ICANON);
     89 	}
     90 
     91 	/* XXX */
     92 	switch (ISSET(flags, ANYP)) {
     93 	case EVENP:
     94 		SET(iflag, INPCK);
     95 		SET(cflag, PARODD);
     96 		break;
     97 	case ODDP:
     98 		SET(iflag, INPCK);
     99 		CLR(cflag, PARODD);
    100 		break;
    101 	}
    102 
    103 	/* XXX */
    104 	if (ISSET(flags, RAW|LITOUT|PASS8)) {
    105 		CLR(cflag, CSIZE);
    106 		SET(cflag, CS7|PARENB);
    107 		SET(iflag, ISTRIP);
    108 		SET(oflag, OPOST);
    109 	}
    110 
    111 	tp->c_iflag = iflag;
    112 	tp->c_oflag = oflag;
    113 	tp->c_lflag = lflag;
    114 	tp->c_cflag = cflag;
    115 }
    116 
    117 void
    118 sttysetflags(tp, flags)
    119 	struct termios *tp;
    120 	int flags;
    121 {
    122 	register tcflag_t iflag = tp->c_iflag;
    123 	register tcflag_t oflag = tp->c_oflag;
    124 	register tcflag_t lflag = tp->c_lflag;
    125 	register tcflag_t cflag = tp->c_cflag;
    126 
    127 	if (ISSET(flags, TANDEM))
    128 		SET(iflag, IXOFF);
    129 	if (ISSET(flags, ECHO))
    130 		SET(lflag, ECHO);
    131 	if (ISSET(flags, CRMOD)) {
    132 		SET(iflag, ICRNL);
    133 		SET(oflag, ONLCR);
    134 	}
    135 	if (ISSET(flags, XTABS))
    136 		SET(oflag, OXTABS);
    137 
    138 	if (ISSET(flags, RAW)) {
    139 		iflag &= IXOFF;
    140 		CLR(lflag, ISIG|ICANON|IEXTEN);
    141 	}
    142 
    143 	/* XXX */
    144 	switch (ISSET(flags, ANYP)) {
    145 	case EVENP:
    146 		SET(iflag, INPCK);
    147 		CLR(cflag, PARODD);
    148 		break;
    149 	case ODDP:
    150 		SET(iflag, INPCK);
    151 		SET(cflag, PARODD);
    152 		break;
    153 	}
    154 
    155 	/* XXX */
    156 	if (ISSET(flags, RAW|LITOUT|PASS8)) {
    157 		CLR(cflag, CSIZE|PARENB);
    158 		SET(cflag, CS8);
    159 		if (!ISSET(flags, RAW|PASS8))
    160 			SET(iflag, ISTRIP);
    161 		else
    162 			CLR(iflag, ISTRIP);
    163 		if (!ISSET(flags, RAW|LITOUT))
    164 			SET(oflag, OPOST);
    165 		else
    166 			CLR(oflag, OPOST);
    167 	}
    168 
    169 	tp->c_iflag = iflag;
    170 	tp->c_oflag = oflag;
    171 	tp->c_lflag = lflag;
    172 	tp->c_cflag = cflag;
    173 }
    174 
    175 void
    176 sttyclearlflags(tp, flags)
    177 	struct termios *tp;
    178 	int flags;
    179 {
    180 	register tcflag_t iflag = tp->c_iflag;
    181 	register tcflag_t oflag = tp->c_oflag;
    182 	register tcflag_t lflag = tp->c_lflag;
    183 	register tcflag_t cflag = tp->c_cflag;
    184 
    185 	/* Nothing we can do with CRTBS. */
    186 	if (ISSET(flags, PRTERA))
    187 		CLR(lflag, ECHOPRT);
    188 	if (ISSET(flags, CRTERA))
    189 		CLR(lflag, ECHOE);
    190 	/* Nothing we can do with TILDE. */
    191 	if (ISSET(flags, MDMBUF))
    192 		CLR(cflag, MDMBUF);
    193 	if (ISSET(flags, NOHANG))
    194 		SET(cflag, HUPCL);
    195 	if (ISSET(flags, CRTKIL))
    196 		CLR(lflag, ECHOKE);
    197 	if (ISSET(flags, CTLECH))
    198 		CLR(lflag, ECHOCTL);
    199 	if (ISSET(flags, DECCTQ))
    200 		SET(iflag, IXANY);
    201 	CLR(lflag, ISSET(flags, TOSTOP|FLUSHO|PENDIN|NOFLSH));
    202 
    203 	/* XXX */
    204 	if (ISSET(flags, RAW|LITOUT|PASS8)) {
    205 		CLR(cflag, CSIZE);
    206 		SET(cflag, CS7|PARENB);
    207 		SET(iflag, ISTRIP);
    208 		SET(oflag, OPOST);
    209 	}
    210 
    211 	tp->c_iflag = iflag;
    212 	tp->c_oflag = oflag;
    213 	tp->c_lflag = lflag;
    214 	tp->c_cflag = cflag;
    215 }
    216 
    217 void
    218 sttysetlflags(tp, flags)
    219 	struct termios *tp;
    220 	int flags;
    221 {
    222 	register tcflag_t iflag = tp->c_iflag;
    223 	register tcflag_t oflag = tp->c_oflag;
    224 	register tcflag_t lflag = tp->c_lflag;
    225 	register tcflag_t cflag = tp->c_cflag;
    226 
    227 	/* Nothing we can do with CRTBS. */
    228 	if (ISSET(flags, PRTERA))
    229 		SET(lflag, ECHOPRT);
    230 	if (ISSET(flags, CRTERA))
    231 		SET(lflag, ECHOE);
    232 	/* Nothing we can do with TILDE. */
    233 	if (ISSET(flags, MDMBUF))
    234 		SET(cflag, MDMBUF);
    235 	if (ISSET(flags, NOHANG))
    236 		CLR(cflag, HUPCL);
    237 	if (ISSET(flags, CRTKIL))
    238 		SET(lflag, ECHOKE);
    239 	if (ISSET(flags, CTLECH))
    240 		SET(lflag, ECHOCTL);
    241 	if (ISSET(flags, DECCTQ))
    242 		CLR(iflag, IXANY);
    243 	SET(lflag, ISSET(flags, TOSTOP|FLUSHO|PENDIN|NOFLSH));
    244 
    245 	/* XXX */
    246 	if (ISSET(flags, RAW|LITOUT|PASS8)) {
    247 		CLR(cflag, CSIZE|PARENB);
    248 		SET(cflag, CS8);
    249 		if (!ISSET(flags, RAW|PASS8))
    250 			SET(iflag, ISTRIP);
    251 		else
    252 			CLR(iflag, ISTRIP);
    253 		if (!ISSET(flags, RAW|LITOUT))
    254 			SET(oflag, OPOST);
    255 		else
    256 			CLR(oflag, OPOST);
    257 	}
    258 
    259 	tp->c_iflag = iflag;
    260 	tp->c_oflag = oflag;
    261 	tp->c_lflag = lflag;
    262 	tp->c_cflag = cflag;
    263 }
    264