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