Home | History | Annotate | Line # | Download | only in stty
key.c revision 1.8
      1 /*-
      2  * Copyright (c) 1991 The Regents of the University of California.
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Berkeley and its contributors.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  */
     33 
     34 #ifndef lint
     35 /*static char sccsid[] = "from: @(#)key.c	5.3 (Berkeley) 6/10/91";*/
     36 static char rcsid[] = "$Id: key.c,v 1.8 1994/03/23 04:05:29 mycroft Exp $";
     37 #endif /* not lint */
     38 
     39 #include <sys/types.h>
     40 #include <err.h>
     41 #include <errno.h>
     42 #include <stdlib.h>
     43 #include <stdio.h>
     44 #include <string.h>
     45 #include "stty.h"
     46 #include "extern.h"
     47 
     48 __BEGIN_DECLS
     49 void	f_all __P((struct info *));
     50 void	f_cbreak __P((struct info *));
     51 void	f_columns __P((struct info *));
     52 void	f_dec __P((struct info *));
     53 void	f_everything __P((struct info *));
     54 void	f_extproc __P((struct info *));
     55 void	f_ispeed __P((struct info *));
     56 void	f_nl __P((struct info *));
     57 void	f_ospeed __P((struct info *));
     58 void	f_raw __P((struct info *));
     59 void	f_rows __P((struct info *));
     60 void	f_sane __P((struct info *));
     61 void	f_size __P((struct info *));
     62 void	f_speed __P((struct info *));
     63 void	f_tty __P((struct info *));
     64 __END_DECLS
     65 
     66 static struct key {
     67 	char *name;				/* name */
     68 	void (*f) __P((struct info *));		/* function */
     69 #define	F_NEEDARG	0x01			/* needs an argument */
     70 #define	F_OFFOK		0x02			/* can turn off */
     71 	int flags;
     72 } keys[] = {
     73 	"all",		f_all,		0,
     74 	"cbreak",	f_cbreak,	F_OFFOK,
     75 	"cols",		f_columns,	F_NEEDARG,
     76 	"columns",	f_columns,	F_NEEDARG,
     77 	"cooked", 	f_sane,		0,
     78 	"dec",		f_dec,		0,
     79 	"everything",	f_everything,	0,
     80 	"extproc",	f_extproc,	F_OFFOK,
     81 	"ispeed",	f_ispeed,	F_NEEDARG,
     82 	"new",		f_tty,		0,
     83 	"nl",		f_nl,		F_OFFOK,
     84 	"old",		f_tty,		0,
     85 	"ospeed",	f_ospeed,	F_NEEDARG,
     86 	"raw",		f_raw,		F_OFFOK,
     87 	"rows",		f_rows,		F_NEEDARG,
     88 	"sane",		f_sane,		0,
     89 	"size",		f_size,		0,
     90 	"speed",	f_speed,	0,
     91 	"tty",		f_tty,		0,
     92 };
     93 
     94 ksearch(argvp, ip)
     95 	char ***argvp;
     96 	struct info *ip;
     97 {
     98 	register struct key *kp;
     99 	register char *name;
    100 	struct key tmp;
    101 	static int c_key __P((const void *, const void *));
    102 
    103 	name = **argvp;
    104 	if (*name == '-') {
    105 		ip->off = 1;
    106 		++name;
    107 	} else
    108 		ip->off = 0;
    109 
    110 	tmp.name = name;
    111 	if (!(kp = (struct key *)bsearch(&tmp, keys,
    112 	    sizeof(keys)/sizeof(struct key), sizeof(struct key), c_key)))
    113 		return(0);
    114 	if (!(kp->flags & F_OFFOK) && ip->off) {
    115 		warnx("illegal option -- %s", name);
    116 		usage();
    117 	}
    118 	if (kp->flags & F_NEEDARG && !(ip->arg = *++*argvp)) {
    119 		warnx("option requires an argument -- %s", name);
    120 		usage();
    121 	}
    122 	kp->f(ip);
    123 	return(1);
    124 }
    125 
    126 static
    127 c_key(a, b)
    128         const void *a, *b;
    129 {
    130         return(strcmp(((struct key *)a)->name, ((struct key *)b)->name));
    131 }
    132 
    133 void
    134 f_all(ip)
    135 	struct info *ip;
    136 {
    137 	print(&ip->t, &ip->win, ip->ldisc, BSD);
    138 }
    139 
    140 void
    141 f_cbreak(ip)
    142 	struct info *ip;
    143 {
    144 	if (ip->off)
    145 		f_sane(ip);
    146 	else {
    147 		ip->t.c_iflag |= BRKINT|IXON|IMAXBEL;
    148 		ip->t.c_oflag |= OPOST;
    149 		ip->t.c_lflag |= ISIG|IEXTEN;
    150 		ip->t.c_lflag &= ~ICANON;
    151 		ip->set = 1;
    152 	}
    153 }
    154 
    155 void
    156 f_columns(ip)
    157 	struct info *ip;
    158 {
    159 	ip->win.ws_col = atoi(ip->arg);
    160 	ip->wset = 1;
    161 }
    162 
    163 void
    164 f_dec(ip)
    165 	struct info *ip;
    166 {
    167 	ip->t.c_cc[VERASE] = (u_char)0177;
    168 	ip->t.c_cc[VKILL] = CTRL('u');
    169 	ip->t.c_cc[VINTR] = CTRL('c');
    170 	ip->t.c_lflag &= ~ECHOPRT;
    171 	ip->t.c_lflag |= ECHOE|ECHOKE|ECHOCTL;
    172 	ip->t.c_iflag &= ~IXANY;
    173 	ip->set = 1;
    174 }
    175 
    176 void
    177 f_everything(ip)
    178 	struct info *ip;
    179 {
    180 	print(&ip->t, &ip->win, ip->ldisc, BSD);
    181 }
    182 
    183 void
    184 f_extproc(ip)
    185 	struct info *ip;
    186 {
    187 	int tmp;
    188 
    189 	if (ip->set) {
    190 		tmp = 1;
    191 		(void)ioctl(ip->fd, TIOCEXT, &tmp);
    192 	} else {
    193 		tmp = 0;
    194 		(void)ioctl(ip->fd, TIOCEXT, &tmp);
    195 	}
    196 }
    197 
    198 void
    199 f_ispeed(ip)
    200 	struct info *ip;
    201 {
    202 	cfsetispeed(&ip->t, atoi(ip->arg));
    203 	ip->set = 1;
    204 }
    205 
    206 void
    207 f_nl(ip)
    208 	struct info *ip;
    209 {
    210 	if (ip->off) {
    211 		ip->t.c_iflag |= ICRNL;
    212 		ip->t.c_oflag |= ONLCR;
    213 	} else {
    214 		ip->t.c_iflag &= ~ICRNL;
    215 		ip->t.c_oflag &= ~ONLCR;
    216 	}
    217 	ip->set = 1;
    218 }
    219 
    220 void
    221 f_ospeed(ip)
    222 	struct info *ip;
    223 {
    224 	cfsetospeed(&ip->t, atoi(ip->arg));
    225 	ip->set = 1;
    226 }
    227 
    228 void
    229 f_raw(ip)
    230 	struct info *ip;
    231 {
    232 	if (ip->off)
    233 		f_sane(ip);
    234 	else {
    235 		cfmakeraw(&ip->t);
    236 		ip->t.c_cflag &= ~(CSIZE|PARENB);
    237 		ip->t.c_cflag |= CS8;
    238 		ip->set = 1;
    239 	}
    240 }
    241 
    242 void
    243 f_rows(ip)
    244 	struct info *ip;
    245 {
    246 	ip->win.ws_row = atoi(ip->arg);
    247 	ip->wset = 1;
    248 }
    249 
    250 void
    251 f_sane(ip)
    252 	struct info *ip;
    253 {
    254 	ip->t.c_cflag = TTYDEF_CFLAG | (ip->t.c_cflag & (CLOCAL|CRTSCTS));
    255 	ip->t.c_iflag = TTYDEF_IFLAG;
    256 	ip->t.c_iflag |= ICRNL;
    257 	/* preserve user-preference flags in lflag */
    258 #define	LKEEP	(ECHOKE|ECHOE|ECHOK|ECHOPRT|ECHOCTL|ALTWERASE|TOSTOP|NOFLSH)
    259 	ip->t.c_lflag = TTYDEF_LFLAG | (ip->t.c_lflag & LKEEP);
    260 	ip->t.c_oflag = TTYDEF_OFLAG;
    261 	ip->set = 1;
    262 }
    263 
    264 void
    265 f_size(ip)
    266 	struct info *ip;
    267 {
    268 	(void)printf("%d %d\n", ip->win.ws_row, ip->win.ws_col);
    269 }
    270 
    271 void
    272 f_speed(ip)
    273 	struct info *ip;
    274 {
    275 	(void)printf("%d\n", cfgetospeed(&ip->t));
    276 }
    277 
    278 void
    279 f_tty(ip)
    280 	struct info *ip;
    281 {
    282 	int tmp;
    283 
    284 	tmp = TTYDISC;
    285 	if (ioctl(0, TIOCSETD, &tmp) < 0)
    286 		err(1, "TIOCSETD");
    287 }
    288