]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - hack/hack.tty.c
2 * Copyright (c) 1988, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
36 static char sccsid
[] = "@(#)hack.tty.c 8.1 (Berkeley) 5/31/93";
38 static char rcsid
[] = "$NetBSD: hack.tty.c,v 1.5 1995/04/29 01:08:54 mycroft Exp $";
42 /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
43 /* hack.tty.c - version 1.0.3 */
44 /* With thanks to the people who sent code for SYSV - hpscdi!jon,
45 arnold@ucsf-cgl, wcs@bo95b, cbcephus!pds and others. */
52 * Some systems may have getchar() return EOF for various reasons, and
53 * we should not quit before seeing at least NR_OF_EOFS consecutive EOFs.
59 extern speed_t ospeed
;
60 static char erase_char
, kill_char
;
61 static boolean settty_needed
= FALSE
;
62 struct termios inittyb
, curttyb
;
65 * Get initial state of terminal, set ospeed (for termcap routines)
66 * and switch off tab expansion if necessary.
67 * Called by startup() in termcap.c and after returning from ! or ^Z
70 if(tcgetattr(0, &inittyb
) < 0)
71 perror("Hack (gettty)");
73 ospeed
= cfgetospeed(&inittyb
);
74 erase_char
= inittyb
.c_cc
[VERASE
];
75 kill_char
= inittyb
.c_cc
[VKILL
];
78 /* do not expand tabs - they might be needed inside a cm sequence */
79 if(curttyb
.c_oflag
& OXTABS
) {
80 curttyb
.c_oflag
&= ~OXTABS
;
86 /* reset terminal to original state */
91 (void) fflush(stdout
);
92 if(tcsetattr(0, TCSADRAIN
, &inittyb
) < 0)
93 perror("Hack (settty)");
94 flags
.echo
= (inittyb
.c_lflag
& ECHO
) ? ON
: OFF
;
95 flags
.cbreak
= (inittyb
.c_lflag
& ICANON
) ? OFF
: ON
;
100 if(tcsetattr(0, TCSADRAIN
, &curttyb
) < 0)
101 perror("Hack (setctty)");
106 register int change
= 0;
109 /* Should use (ECHO|CRMOD) here instead of ECHO */
110 if(curttyb
.c_lflag
& ECHO
){
111 curttyb
.c_lflag
&= ~ECHO
;
114 if(curttyb
.c_lflag
& ICANON
){
115 curttyb
.c_lflag
&= ~ICANON
;
116 /* be satisfied with one character; no timeout */
117 curttyb
.c_cc
[VMIN
] = 1;
118 curttyb
.c_cc
[VTIME
] = 0;
130 error(s
,x
,y
) char *s
; {
139 * Read a line closed with '\n' into the array char bufp[BUFSZ].
140 * (The '\n' is not stored. The string is closed with a '\0'.)
141 * Reading can be interrupted by an escape ('\033') - now the
142 * resulting string is "\033".
147 register char *obufp
= bufp
;
150 flags
.toplin
= 2; /* nonempty, no --More-- required */
152 (void) fflush(stdout
);
153 if((c
= getchar()) == EOF
) {
162 if(c
== erase_char
|| c
== '\b') {
165 putstr("\b \b"); /* putsym converts \b */
167 } else if(c
== '\n') {
170 } else if(' ' <= c
&& c
< '\177') {
171 /* avoid isprint() - some people don't have it
172 ' ' is not always a printing char */
176 if(bufp
-obufp
< BUFSZ
-1 && bufp
-obufp
< COLNO
)
178 } else if(c
== kill_char
|| c
== '\177') { /* Robert Viduya */
179 /* this test last - @ might be the kill_char */
180 while(bufp
!= obufp
) {
200 putstr(flags
.cbreak
? "space" : "return");
201 putstr(" to continue: ");
207 char morc
; /* tell the outside world what char he used */
210 register char *s
; /* chars allowed besides space or return */
216 while((c
= readchar()) != '\n') {
219 if(s
&& index(s
,c
)) {
231 static char inputline
[COLNO
];
235 if(!Invisible
) curs_on_u(); else home();
236 while((foo
= readchar()) >= '0' && foo
<= '9')
237 multi
= 10*multi
+foo
-'0';
244 if(foo
== 'f' || foo
== 'F'){
245 inputline
[1] = getchar();
247 if(inputline
[1] == foo
) inputline
[2] = getchar(); else
251 if(foo
== 'm' || foo
== 'M'){
252 inputline
[1] = getchar();
263 (void) fflush(stdout
);
264 if((sym
= getchar()) == EOF
)
267 * Some SYSV systems seem to return EOFs for various reasons
268 * (?like when one hits break or for interrupted systemcalls?),
269 * and we must see several before we quit.
271 register int cnt
= NR_OF_EOFS
;
273 clearerr(stdin
); /* omit if clearerr is undefined */
274 if((sym
= getchar()) != EOF
) goto noteof
;
282 if(flags
.toplin
== 1)
289 settty("End of input?\n");