Source:Hack 1.0/hack.tty.c

From NetHackWiki
Jump to: navigation, search

Below is the full text to hack.tty.c from the source code of Hack 1.0. To link to a particular line, write [[Hack 1.0/hack.tty.c#line123]], for example.

Warning! This is the source code from an old release. For the latest release, see Source code

Screenshots and source code from Hack are used under the CWI license.

1.    /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1984. */
2.    
3.    #include	"hack.h"
4.    #include	<stdio.h>
5.    #include	<sgtty.h>
6.    
7.    struct sgttyb inittyb, curttyb;
8.    extern short ospeed;
9.    
10.   
11.   gettty(){
12.   	(void) gtty(0, &inittyb);
13.   	(void) gtty(0, &curttyb);
14.   	ospeed = inittyb.sg_ospeed;
15.   /*
16.   	if(ospeed <= B300) flags.oneline = 1;
17.   */
18.   	getioctls();
19.   	xtabs();
20.   }
21.   
22.   /* reset terminal to original state */
23.   settty(s) char *s; {
24.   	clear_screen();
25.   	if(s) printf(s);
26.   	(void) fflush(stdout);
27.   	if(stty(0, &inittyb) == -1) puts("Cannot change tty");
28.   	flags.echo = (inittyb.sg_flags & ECHO) ? ON : OFF;
29.   	flags.cbreak = (inittyb.sg_flags & CBREAK) ? ON : OFF;
30.   	setioctls();
31.   }
32.   
33.   setctty(){
34.   	if(stty(0, &curttyb) == -1) puts("Cannot change tty");
35.   }
36.   
37.   setftty(){
38.   register int ef = (flags.echo == ON) ? ECHO : 0;
39.   register int cf = (flags.cbreak == ON) ? CBREAK : 0;
40.   register int change = 0;
41.   	if((curttyb.sg_flags & ECHO) != ef){
42.   		curttyb.sg_flags &= ~ECHO;
43.   		curttyb.sg_flags |= ef;
44.   		change++;
45.   	}
46.   	if((curttyb.sg_flags & CBREAK) != cf){
47.   		curttyb.sg_flags &= ~CBREAK;
48.   		curttyb.sg_flags |= cf;
49.   		change++;
50.   	}
51.   	if(change){
52.   		setctty();
53.   	}
54.   }
55.   
56.   echo(n)
57.   register n;
58.   {
59.   
60.   	/* (void) gtty(0,&curttyb); */
61.   	if(n == ON)
62.   		curttyb.sg_flags |= ECHO;
63.   	else
64.   		curttyb.sg_flags &= ~ECHO;
65.   	setctty();
66.   }
67.   
68.   /* always want to expand tabs, or to send a clear line char before
69.      printing something on topline */
70.   xtabs()
71.   {
72.   
73.   	/* (void) gtty(0, &curttyb); */
74.   	curttyb.sg_flags |= XTABS;
75.   	setctty();
76.   }
77.   
78.   #ifdef LONG_CMD
79.   cbreak(n)
80.   register n;
81.   {
82.   
83.   	/* (void) gtty(0,&curttyb); */
84.   	if(n == ON)
85.   		curttyb.sg_flags |= CBREAK;
86.   	else
87.   		curttyb.sg_flags &= ~CBREAK;
88.   	setctty();
89.   }
90.   #endif LONG_CMD
91.   
92.   getlin(bufp)
93.   register char *bufp;
94.   {
95.   	register char *obufp = bufp;
96.   	register int c;
97.   
98.   	flags.topl = 2;		/* nonempty, no --More-- required */
99.   	for(;;) {
100.  		(void) fflush(stdout);
101.  		if((c = getchar()) == EOF) {
102.  			*bufp = 0;
103.  			return;
104.  		}
105.  		if(c == '\b') {
106.  			if(bufp != obufp) {
107.  				bufp--;
108.  				putstr("\b \b"); /* putsym converts \b */
109.  			} else	bell();
110.  		} else if(c == '\n') {
111.  			*bufp = 0;
112.  			return;
113.  		} else {
114.  			*bufp = c;
115.  			bufp[1] = 0;
116.  			putstr(bufp);
117.  			if(bufp-obufp < BUFSZ-1 && bufp-obufp < COLNO)
118.  				bufp++;
119.  		}
120.  	}
121.  }
122.  
123.  getret() {
124.  	xgetret(TRUE);
125.  }
126.  
127.  cgetret() {
128.  	xgetret(FALSE);
129.  }
130.  
131.  xgetret(spaceflag)
132.  boolean spaceflag;	/* TRUE if space (return) required */
133.  {
134.  	printf("\nHit %s to continue: ",
135.  		flags.cbreak ? "space" : "return");
136.  	xwaitforspace(spaceflag);
137.  }
138.  
139.  char morc;	/* tell the outside world what char he used */
140.  
141.  xwaitforspace(spaceflag)
142.  boolean spaceflag;
143.  {
144.  register int c;
145.  
146.  	(void) fflush(stdout);
147.  	morc = 0;
148.  
149.  	while((c = getchar()) != '\n') {
150.  	    if(c == EOF) {
151.  		settty("End of input?\n");
152.  		exit(0);
153.  	    }
154.  	    if(flags.cbreak) {
155.  		if(c == ' ') break;
156.  		if(!spaceflag && letter(c)) {
157.  			morc = c;
158.  			break;
159.  		}
160.    }
161.  	}
162.  }
163.  
164.  char *
165.  parse()
166.  {
167.  	static char inline[COLNO];
168.  	register foo;
169.  
170.  	flags.move = 1;
171.  	if(!Invis) curs(u.ux,u.uy+2); else home();
172.  	(void) fflush(stdout);
173.  	while((foo = getchar()) >= '0' && foo <= '9')
174.  		multi += 10*multi+foo-'0';
175.  	if(multi) {
176.  		multi--;
177.  		save_cm = inline;
178.  	}
179.  	inline[0] = foo;
180.  	inline[1] = 0;
181.  	if(foo == EOF) {
182.  		settty("End of input?\n");
183.  		exit(0);
184.  	}
185.  	if(foo == 'f' || foo == 'F'){
186.  		inline[1] = getchar();
187.  #ifdef QUEST
188.  		if(inline[1] == foo) inline[2] = getchar(); else
189.  #endif QUEST
190.  		inline[2] = 0;
191.  	}
192.  	if(foo == 'm' || foo == 'M'){
193.  		inline[1] = getchar();
194.  		inline[2] = 0;
195.  	}
196.  	clrlin();
197.  	return(inline);
198.  }
199.  
200.  char
201.  readchar() {
202.  	register int sym;
203.  	(void) fflush(stdout);
204.  	if((sym = getchar()) == EOF) {
205.  		settty("End of input?\n");
206.  		exit(0);
207.  	}
208.  	if(flags.topl == 1) flags.topl = 2;
209.  	return((char) sym);
210.  }