Home | History | Annotate | Line # | Download | only in hack
hack.tty.c revision 1.4
      1  1.1      cgd /*-
      2  1.3      cgd  * Copyright (c) 1988, 1993
      3  1.3      cgd  *	The Regents of the University of California.  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.3      cgd #if 0
     36  1.3      cgd static char sccsid[] = "@(#)hack.tty.c	8.1 (Berkeley) 5/31/93";
     37  1.3      cgd #else
     38  1.4  mycroft static char rcsid[] = "$NetBSD: hack.tty.c,v 1.4 1995/04/28 23:01:47 mycroft Exp $";
     39  1.3      cgd #endif
     40  1.1      cgd #endif /* not lint */
     41  1.1      cgd 
     42  1.1      cgd /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
     43  1.1      cgd /* hack.tty.c - version 1.0.3 */
     44  1.1      cgd /* With thanks to the people who sent code for SYSV - hpscdi!jon,
     45  1.1      cgd    arnold@ucsf-cgl, wcs@bo95b, cbcephus!pds and others. */
     46  1.1      cgd 
     47  1.1      cgd #include	"hack.h"
     48  1.1      cgd #include	<stdio.h>
     49  1.4  mycroft #include	<termios.h>
     50  1.1      cgd 
     51  1.1      cgd /*
     52  1.1      cgd  * Some systems may have getchar() return EOF for various reasons, and
     53  1.1      cgd  * we should not quit before seeing at least NR_OF_EOFS consecutive EOFs.
     54  1.1      cgd  */
     55  1.1      cgd #ifndef BSD
     56  1.1      cgd #define	NR_OF_EOFS	20
     57  1.1      cgd #endif BSD
     58  1.1      cgd 
     59  1.1      cgd extern short ospeed;
     60  1.1      cgd static char erase_char, kill_char;
     61  1.1      cgd static boolean settty_needed = FALSE;
     62  1.4  mycroft struct termios inittyb, curttyb;
     63  1.1      cgd 
     64  1.1      cgd /*
     65  1.1      cgd  * Get initial state of terminal, set ospeed (for termcap routines)
     66  1.1      cgd  * and switch off tab expansion if necessary.
     67  1.1      cgd  * Called by startup() in termcap.c and after returning from ! or ^Z
     68  1.1      cgd  */
     69  1.1      cgd gettty(){
     70  1.4  mycroft 	if(tcgetattr(0, &inittyb) < 0)
     71  1.1      cgd 		perror("Hack (gettty)");
     72  1.1      cgd 	curttyb = inittyb;
     73  1.4  mycroft 	ospeed = cfgetospeed(&inittyb);
     74  1.4  mycroft 	erase_char = inittyb.c_cc[VERASE];
     75  1.4  mycroft 	kill_char = inittyb.c_cc[VKILL];
     76  1.1      cgd 	getioctls();
     77  1.1      cgd 
     78  1.1      cgd 	/* do not expand tabs - they might be needed inside a cm sequence */
     79  1.4  mycroft 	if(curttyb.c_oflag & OXTABS) {
     80  1.4  mycroft 		curttyb.c_oflag &= ~OXTABS;
     81  1.1      cgd 		setctty();
     82  1.1      cgd 	}
     83  1.1      cgd 	settty_needed = TRUE;
     84  1.1      cgd }
     85  1.1      cgd 
     86  1.1      cgd /* reset terminal to original state */
     87  1.1      cgd settty(s) char *s; {
     88  1.1      cgd 	clear_screen();
     89  1.1      cgd 	end_screen();
     90  1.1      cgd 	if(s) printf(s);
     91  1.1      cgd 	(void) fflush(stdout);
     92  1.4  mycroft 	if(tcsetattr(0, TCSADRAIN, &inittyb) < 0)
     93  1.1      cgd 		perror("Hack (settty)");
     94  1.4  mycroft 	flags.echo = (inittyb.c_lflag & ECHO) ? ON : OFF;
     95  1.4  mycroft 	flags.cbreak = (inittyb.c_lflag & ICANON) ? OFF : ON;
     96  1.1      cgd 	setioctls();
     97  1.1      cgd }
     98  1.1      cgd 
     99  1.1      cgd setctty(){
    100  1.4  mycroft 	if(tcsetattr(0, TCSADRAIN, &curttyb) < 0)
    101  1.1      cgd 		perror("Hack (setctty)");
    102  1.1      cgd }
    103  1.1      cgd 
    104  1.1      cgd 
    105  1.1      cgd setftty(){
    106  1.1      cgd register int change = 0;
    107  1.1      cgd 	flags.cbreak = ON;
    108  1.1      cgd 	flags.echo = OFF;
    109  1.1      cgd 	/* Should use (ECHO|CRMOD) here instead of ECHO */
    110  1.4  mycroft 	if(curttyb.c_lflag & ECHO){
    111  1.4  mycroft 		curttyb.c_lflag &= ~ECHO;
    112  1.1      cgd 		change++;
    113  1.1      cgd 	}
    114  1.4  mycroft 	if(curttyb.c_lflag & ICANON){
    115  1.4  mycroft 		curttyb.c_lflag &= ~ICANON;
    116  1.1      cgd 		/* be satisfied with one character; no timeout */
    117  1.4  mycroft 		curttyb.c_cc[VMIN] = 1;
    118  1.4  mycroft 		curttyb.c_cc[VTIME] = 0;
    119  1.1      cgd 		change++;
    120  1.1      cgd 	}
    121  1.1      cgd 	if(change){
    122  1.1      cgd 		setctty();
    123  1.1      cgd 	}
    124  1.1      cgd 	start_screen();
    125  1.1      cgd }
    126  1.1      cgd 
    127  1.1      cgd 
    128  1.1      cgd /* fatal error */
    129  1.1      cgd /*VARARGS1*/
    130  1.1      cgd error(s,x,y) char *s; {
    131  1.1      cgd 	if(settty_needed)
    132  1.1      cgd 		settty((char *) 0);
    133  1.1      cgd 	printf(s,x,y);
    134  1.1      cgd 	putchar('\n');
    135  1.1      cgd 	exit(1);
    136  1.1      cgd }
    137  1.1      cgd 
    138  1.1      cgd /*
    139  1.1      cgd  * Read a line closed with '\n' into the array char bufp[BUFSZ].
    140  1.1      cgd  * (The '\n' is not stored. The string is closed with a '\0'.)
    141  1.1      cgd  * Reading can be interrupted by an escape ('\033') - now the
    142  1.1      cgd  * resulting string is "\033".
    143  1.1      cgd  */
    144  1.1      cgd getlin(bufp)
    145  1.1      cgd register char *bufp;
    146  1.1      cgd {
    147  1.1      cgd 	register char *obufp = bufp;
    148  1.1      cgd 	register int c;
    149  1.1      cgd 
    150  1.1      cgd 	flags.toplin = 2;		/* nonempty, no --More-- required */
    151  1.1      cgd 	for(;;) {
    152  1.1      cgd 		(void) fflush(stdout);
    153  1.1      cgd 		if((c = getchar()) == EOF) {
    154  1.1      cgd 			*bufp = 0;
    155  1.1      cgd 			return;
    156  1.1      cgd 		}
    157  1.1      cgd 		if(c == '\033') {
    158  1.1      cgd 			*obufp = c;
    159  1.1      cgd 			obufp[1] = 0;
    160  1.1      cgd 			return;
    161  1.1      cgd 		}
    162  1.1      cgd 		if(c == erase_char || c == '\b') {
    163  1.1      cgd 			if(bufp != obufp) {
    164  1.1      cgd 				bufp--;
    165  1.1      cgd 				putstr("\b \b"); /* putsym converts \b */
    166  1.1      cgd 			} else	bell();
    167  1.1      cgd 		} else if(c == '\n') {
    168  1.1      cgd 			*bufp = 0;
    169  1.1      cgd 			return;
    170  1.1      cgd 		} else if(' ' <= c && c < '\177') {
    171  1.1      cgd 				/* avoid isprint() - some people don't have it
    172  1.1      cgd 				   ' ' is not always a printing char */
    173  1.1      cgd 			*bufp = c;
    174  1.1      cgd 			bufp[1] = 0;
    175  1.1      cgd 			putstr(bufp);
    176  1.1      cgd 			if(bufp-obufp < BUFSZ-1 && bufp-obufp < COLNO)
    177  1.1      cgd 				bufp++;
    178  1.1      cgd 		} else if(c == kill_char || c == '\177') { /* Robert Viduya */
    179  1.1      cgd 				/* this test last - @ might be the kill_char */
    180  1.1      cgd 			while(bufp != obufp) {
    181  1.1      cgd 				bufp--;
    182  1.1      cgd 				putstr("\b \b");
    183  1.1      cgd 			}
    184  1.1      cgd 		} else
    185  1.1      cgd 			bell();
    186  1.1      cgd 	}
    187  1.1      cgd }
    188  1.1      cgd 
    189  1.1      cgd getret() {
    190  1.1      cgd 	cgetret("");
    191  1.1      cgd }
    192  1.1      cgd 
    193  1.1      cgd cgetret(s)
    194  1.1      cgd register char *s;
    195  1.1      cgd {
    196  1.1      cgd 	putsym('\n');
    197  1.1      cgd 	if(flags.standout)
    198  1.1      cgd 		standoutbeg();
    199  1.1      cgd 	putstr("Hit ");
    200  1.1      cgd 	putstr(flags.cbreak ? "space" : "return");
    201  1.1      cgd 	putstr(" to continue: ");
    202  1.1      cgd 	if(flags.standout)
    203  1.1      cgd 		standoutend();
    204  1.1      cgd 	xwaitforspace(s);
    205  1.1      cgd }
    206  1.1      cgd 
    207  1.1      cgd char morc;	/* tell the outside world what char he used */
    208  1.1      cgd 
    209  1.1      cgd xwaitforspace(s)
    210  1.1      cgd register char *s;	/* chars allowed besides space or return */
    211  1.1      cgd {
    212  1.1      cgd register int c;
    213  1.1      cgd 
    214  1.1      cgd 	morc = 0;
    215  1.1      cgd 
    216  1.1      cgd 	while((c = readchar()) != '\n') {
    217  1.1      cgd 	    if(flags.cbreak) {
    218  1.1      cgd 		if(c == ' ') break;
    219  1.1      cgd 		if(s && index(s,c)) {
    220  1.1      cgd 			morc = c;
    221  1.1      cgd 			break;
    222  1.1      cgd 		}
    223  1.1      cgd 		bell();
    224  1.1      cgd 	    }
    225  1.1      cgd 	}
    226  1.1      cgd }
    227  1.1      cgd 
    228  1.1      cgd char *
    229  1.1      cgd parse()
    230  1.1      cgd {
    231  1.1      cgd 	static char inputline[COLNO];
    232  1.1      cgd 	register foo;
    233  1.1      cgd 
    234  1.1      cgd 	flags.move = 1;
    235  1.1      cgd 	if(!Invisible) curs_on_u(); else home();
    236  1.1      cgd 	while((foo = readchar()) >= '0' && foo <= '9')
    237  1.1      cgd 		multi = 10*multi+foo-'0';
    238  1.1      cgd 	if(multi) {
    239  1.1      cgd 		multi--;
    240  1.1      cgd 		save_cm = inputline;
    241  1.1      cgd 	}
    242  1.1      cgd 	inputline[0] = foo;
    243  1.1      cgd 	inputline[1] = 0;
    244  1.1      cgd 	if(foo == 'f' || foo == 'F'){
    245  1.1      cgd 		inputline[1] = getchar();
    246  1.1      cgd #ifdef QUEST
    247  1.1      cgd 		if(inputline[1] == foo) inputline[2] = getchar(); else
    248  1.1      cgd #endif QUEST
    249  1.1      cgd 		inputline[2] = 0;
    250  1.1      cgd 	}
    251  1.1      cgd 	if(foo == 'm' || foo == 'M'){
    252  1.1      cgd 		inputline[1] = getchar();
    253  1.1      cgd 		inputline[2] = 0;
    254  1.1      cgd 	}
    255  1.1      cgd 	clrlin();
    256  1.1      cgd 	return(inputline);
    257  1.1      cgd }
    258  1.1      cgd 
    259  1.1      cgd char
    260  1.1      cgd readchar() {
    261  1.1      cgd 	register int sym;
    262  1.1      cgd 
    263  1.1      cgd 	(void) fflush(stdout);
    264  1.1      cgd 	if((sym = getchar()) == EOF)
    265  1.1      cgd #ifdef NR_OF_EOFS
    266  1.1      cgd 	{ /*
    267  1.1      cgd 	   * Some SYSV systems seem to return EOFs for various reasons
    268  1.1      cgd 	   * (?like when one hits break or for interrupted systemcalls?),
    269  1.1      cgd 	   * and we must see several before we quit.
    270  1.1      cgd 	   */
    271  1.1      cgd 		register int cnt = NR_OF_EOFS;
    272  1.1      cgd 		while (cnt--) {
    273  1.1      cgd 		    clearerr(stdin);	/* omit if clearerr is undefined */
    274  1.1      cgd 		    if((sym = getchar()) != EOF) goto noteof;
    275  1.1      cgd 		}
    276  1.1      cgd 		end_of_input();
    277  1.1      cgd 	     noteof:	;
    278  1.1      cgd 	}
    279  1.1      cgd #else
    280  1.1      cgd 		end_of_input();
    281  1.1      cgd #endif NR_OF_EOFS
    282  1.1      cgd 	if(flags.toplin == 1)
    283  1.1      cgd 		flags.toplin = 2;
    284  1.1      cgd 	return((char) sym);
    285  1.1      cgd }
    286  1.1      cgd 
    287  1.1      cgd end_of_input()
    288  1.1      cgd {
    289  1.1      cgd 	settty("End of input?\n");
    290  1.1      cgd 	clearlocks();
    291  1.1      cgd 	exit(0);
    292  1.1      cgd }
    293