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