Home | History | Annotate | Line # | Download | only in stty
key.c revision 1.11
      1 /*	$NetBSD: key.c,v 1.11 1995/09/07 06:57:11 jtc Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1991, 1993, 1994
      5  *	The Regents of the University of California.  All rights reserved.
      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 #ifndef lint
     37 #if 0
     38 static char sccsid[] = "@(#)key.c	8.4 (Berkeley) 2/20/95";
     39 #else
     40 static char rcsid[] = "$NetBSD: key.c,v 1.11 1995/09/07 06:57:11 jtc Exp $";
     41 #endif
     42 #endif /* not lint */
     43 
     44 #include <sys/types.h>
     45 
     46 #include <err.h>
     47 #include <errno.h>
     48 #include <stdlib.h>
     49 #include <stdio.h>
     50 #include <string.h>
     51 
     52 #include "stty.h"
     53 #include "extern.h"
     54 
     55 __BEGIN_DECLS
     56 void	f_all __P((struct info *));
     57 void	f_cbreak __P((struct info *));
     58 void	f_columns __P((struct info *));
     59 void	f_dec __P((struct info *));
     60 void	f_everything __P((struct info *));
     61 void	f_extproc __P((struct info *));
     62 void	f_ispeed __P((struct info *));
     63 void	f_nl __P((struct info *));
     64 void	f_ospeed __P((struct info *));
     65 void	f_raw __P((struct info *));
     66 void	f_rows __P((struct info *));
     67 void	f_sane __P((struct info *));
     68 void	f_size __P((struct info *));
     69 void	f_speed __P((struct info *));
     70 void	f_tty __P((struct info *));
     71 __END_DECLS
     72 
     73 static struct key {
     74 	char *name;				/* name */
     75 	void (*f) __P((struct info *));		/* function */
     76 #define	F_NEEDARG	0x01			/* needs an argument */
     77 #define	F_OFFOK		0x02			/* can turn off */
     78 	int flags;
     79 } keys[] = {
     80 	{ "all",	f_all,		0 },
     81 	{ "cbreak",	f_cbreak,	F_OFFOK },
     82 	{ "cols",	f_columns,	F_NEEDARG },
     83 	{ "columns",	f_columns,	F_NEEDARG },
     84 	{ "cooked", 	f_sane,		0 },
     85 	{ "dec",	f_dec,		0 },
     86 	{ "everything",	f_everything,	0 },
     87 	{ "extproc",	f_extproc,	F_OFFOK },
     88 	{ "ispeed",	f_ispeed,	F_NEEDARG },
     89 	{ "new",	f_tty,		0 },
     90 	{ "nl",		f_nl,		F_OFFOK },
     91 	{ "old",	f_tty,		0 },
     92 	{ "ospeed",	f_ospeed,	F_NEEDARG },
     93 	{ "raw",	f_raw,		F_OFFOK },
     94 	{ "rows",	f_rows,		F_NEEDARG },
     95 	{ "sane",	f_sane,		0 },
     96 	{ "size",	f_size,		0 },
     97 	{ "speed",	f_speed,	0 },
     98 	{ "tty",	f_tty,		0 },
     99 };
    100 
    101 static int
    102 c_key(a, b)
    103         const void *a, *b;
    104 {
    105 
    106         return (strcmp(((struct key *)a)->name, ((struct key *)b)->name));
    107 }
    108 
    109 int
    110 ksearch(argvp, ip)
    111 	char ***argvp;
    112 	struct info *ip;
    113 {
    114 	char *name;
    115 	struct key *kp, tmp;
    116 
    117 	name = **argvp;
    118 	if (*name == '-') {
    119 		ip->off = 1;
    120 		++name;
    121 	} else
    122 		ip->off = 0;
    123 
    124 	tmp.name = name;
    125 	if (!(kp = (struct key *)bsearch(&tmp, keys,
    126 	    sizeof(keys)/sizeof(struct key), sizeof(struct key), c_key)))
    127 		return (0);
    128 	if (!(kp->flags & F_OFFOK) && ip->off) {
    129 		warnx("illegal option -- %s", name);
    130 		usage();
    131 	}
    132 	if (kp->flags & F_NEEDARG && !(ip->arg = *++*argvp)) {
    133 		warnx("option requires an argument -- %s", name);
    134 		usage();
    135 	}
    136 	kp->f(ip);
    137 	return (1);
    138 }
    139 
    140 void
    141 f_all(ip)
    142 	struct info *ip;
    143 {
    144 	print(&ip->t, &ip->win, ip->ldisc, BSD);
    145 }
    146 
    147 void
    148 f_cbreak(ip)
    149 	struct info *ip;
    150 {
    151 
    152 	if (ip->off)
    153 		f_sane(ip);
    154 	else {
    155 		ip->t.c_iflag |= BRKINT|IXON|IMAXBEL;
    156 		ip->t.c_oflag |= OPOST;
    157 		ip->t.c_lflag |= ISIG|IEXTEN;
    158 		ip->t.c_lflag &= ~ICANON;
    159 		ip->set = 1;
    160 	}
    161 }
    162 
    163 void
    164 f_columns(ip)
    165 	struct info *ip;
    166 {
    167 
    168 	ip->win.ws_col = atoi(ip->arg);
    169 	ip->wset = 1;
    170 }
    171 
    172 void
    173 f_dec(ip)
    174 	struct info *ip;
    175 {
    176 
    177 	ip->t.c_cc[VERASE] = (u_char)0177;
    178 	ip->t.c_cc[VKILL] = CTRL('u');
    179 	ip->t.c_cc[VINTR] = CTRL('c');
    180 	ip->t.c_lflag &= ~ECHOPRT;
    181 	ip->t.c_lflag |= ECHOE|ECHOKE|ECHOCTL;
    182 	ip->t.c_iflag &= ~IXANY;
    183 	ip->set = 1;
    184 }
    185 
    186 void
    187 f_everything(ip)
    188 	struct info *ip;
    189 {
    190 
    191 	print(&ip->t, &ip->win, ip->ldisc, BSD);
    192 }
    193 
    194 void
    195 f_extproc(ip)
    196 	struct info *ip;
    197 {
    198 
    199 	if (ip->off) {
    200 		int tmp = 0;
    201 		(void)ioctl(ip->fd, TIOCEXT, &tmp);
    202 	} else {
    203 		int tmp = 1;
    204 		(void)ioctl(ip->fd, TIOCEXT, &tmp);
    205 	}
    206 	ip->set = 1;
    207 }
    208 
    209 void
    210 f_ispeed(ip)
    211 	struct info *ip;
    212 {
    213 
    214 	cfsetispeed(&ip->t, atoi(ip->arg));
    215 	ip->set = 1;
    216 }
    217 
    218 void
    219 f_nl(ip)
    220 	struct info *ip;
    221 {
    222 
    223 	if (ip->off) {
    224 		ip->t.c_iflag |= ICRNL;
    225 		ip->t.c_oflag |= ONLCR;
    226 	} else {
    227 		ip->t.c_iflag &= ~ICRNL;
    228 		ip->t.c_oflag &= ~ONLCR;
    229 	}
    230 	ip->set = 1;
    231 }
    232 
    233 void
    234 f_ospeed(ip)
    235 	struct info *ip;
    236 {
    237 
    238 	cfsetospeed(&ip->t, atoi(ip->arg));
    239 	ip->set = 1;
    240 }
    241 
    242 void
    243 f_raw(ip)
    244 	struct info *ip;
    245 {
    246 
    247 	if (ip->off)
    248 		f_sane(ip);
    249 	else {
    250 		cfmakeraw(&ip->t);
    251 		ip->t.c_cflag &= ~(CSIZE|PARENB);
    252 		ip->t.c_cflag |= CS8;
    253 		ip->set = 1;
    254 	}
    255 }
    256 
    257 void
    258 f_rows(ip)
    259 	struct info *ip;
    260 {
    261 
    262 	ip->win.ws_row = atoi(ip->arg);
    263 	ip->wset = 1;
    264 }
    265 
    266 void
    267 f_sane(ip)
    268 	struct info *ip;
    269 {
    270 
    271 	ip->t.c_cflag = TTYDEF_CFLAG | (ip->t.c_cflag & (CLOCAL|CRTSCTS));
    272 	ip->t.c_iflag = TTYDEF_IFLAG;
    273 	ip->t.c_iflag |= ICRNL;
    274 	/* preserve user-preference flags in lflag */
    275 #define	LKEEP	(ECHOKE|ECHOE|ECHOK|ECHOPRT|ECHOCTL|ALTWERASE|TOSTOP|NOFLSH)
    276 	ip->t.c_lflag = TTYDEF_LFLAG | (ip->t.c_lflag & LKEEP);
    277 	ip->t.c_oflag = TTYDEF_OFLAG;
    278 	ip->set = 1;
    279 }
    280 
    281 void
    282 f_size(ip)
    283 	struct info *ip;
    284 {
    285 
    286 	(void)printf("%d %d\n", ip->win.ws_row, ip->win.ws_col);
    287 }
    288 
    289 void
    290 f_speed(ip)
    291 	struct info *ip;
    292 {
    293 
    294 	(void)printf("%d\n", cfgetospeed(&ip->t));
    295 }
    296 
    297 void
    298 f_tty(ip)
    299 	struct info *ip;
    300 {
    301 	int tmp;
    302 
    303 	tmp = TTYDISC;
    304 	if (ioctl(0, TIOCSETD, &tmp) < 0)
    305 		err(1, "TIOCSETD");
    306 }
    307